OrderAssignApiService.java 19.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
package com.huaheng.api.SSP.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.api.SSP.domain.ConfirmHeaderModel;
import com.huaheng.api.SSP.domain.ConfirmModel;
import com.huaheng.api.SSP.domain.ConfirmTypeModel;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.http.OkHttpUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.inventory.inventoryHeader.service.InventoryHeaderService;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 出入库单下发Service
 * <p>
 * 1. 此Service的作用是将系统中未被钱柜扫描过的出入库单据发送给钱柜,目前考虑到系统资源,一次只能查询或发送一条单据,发送规则是根据子单的库区是否为钱柜
 * 进行发送,发送成功后会将子单的状态改为100,若主单内的所有子单都不属于钱柜库区,则将不进行发送,但是无论发送与否,都会对主单的扫描标识进行修改,目前钱柜
 * 的标识是1,若主单据的扫描标识的第一位为1则代表此单据被钱柜扫描过。1`
 * <p>
 * 2. 目前此Service出入库单的下发逻辑是先扫描入库单据,若入库单据已全部下发成功,将自动扫描出库单据。
 * <p>
 * 3. 目前钱柜编码写死为QG,默认为库区编码为QG的物料为钱柜的物料。
 */
@Transactional
@Service
@Slf4j
public class OrderAssignApiService {

    @Autowired
    ReceiptHeaderService receiptHeaderService;

    @Autowired
    ReceiptDetailService receiptDetailService;

    @Autowired
    ShipmentHeaderService shipmentHeaderService;

    @Autowired
    ShipmentDetailService shipmentDetailService;

    @Autowired
    MaterialService materialService;
    @Autowired
    ItemDownloadApiService itemDownloadApiService;
    @Autowired
    InventoryDetailService inventoryDetailService;
    @Autowired
    InventoryHeaderService inventoryHeaderService;
    @Autowired
    TaskDetailService taskDetailService;
    @Autowired
    TaskHeaderService taskHeaderService;

    //private  static String address="http://172.16.30.238:4322/api/SSP/";

//    @Transactional
//    public AjaxResult receiptAssign(String code){
//        //查询出一条未被钱柜扫描过的入库单
//        AjaxResult ajaxResult=new AjaxResult();
//        ReceiptHeader item =  iReceiptHeaderService.selectNosScanSSP(code);
//        ConfirmHeaderModel cfh = new ConfirmHeaderModel();
//        cfh.setOrderHeaderId(item.getId());
//        cfh.setOrderCode(item.getCode());
//        cfh.setOrderType("receipt");
//        cfh.setWarehouseCode(item.getWarehouseCode());
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        cfh.setcDatetime(df.format(new Date()));
//        ConfirmModel confirmModel = new ConfirmModel();
////        confirmModel.setConfirmHeaderModel(cfh);
//
//
//        ReceiptDetail receiptDetail = new ReceiptDetail();
//        receiptDetail.setReceiptId(item.getId());
//        receiptDetail.setZoneCode("QG");
//        List<ReceiptDetail> details = iReceiptDetailService.selectListEntityByEqual(receiptDetail);
//        List<ConfirmTypeModel> typeModels = new ArrayList<>();
//        //遍历入库子单
//        int i = 0;
//        List<Integer> list = new ArrayList();
//        for (ReceiptDetail ds:details){
//            Material material = new Material();
//            material.setCode(ds.getMaterialCode());
//            material=iMaterialService.selectFirstEntity(material);
//            if (material != null ) {
//                if(StringUtils.isEmpty(material.getZoneCode())){
//                    material.setZoneCode("QG");
//                    if(!itemDownloadApiService.sendMaterial(material).hasErr()){
////                        iMaterialService.updateByModel(material);
//                    }else {
//                        throw new ServiceException(material.getCode()+"物料同步失败");
//                    }
//                }
//                if(StringUtils.isNotEmpty(material.getZoneCode()) && !material.getZoneCode().equals("QG") ){
//                    if(itemDownloadApiService.sendMaterial(material).hasErr()){
//                        throw new ServiceException(material.getCode()+"物料同步失败");
//                    }
//                }
//                ConfirmTypeModel cft = new ConfirmTypeModel();
//                cft.setOrderDetailId(ds.getId());
//                cft.setCompanyCode(ds.getCompanyCode());
//                Inventory inventory=new Inventory();
//                inventory.setZoneCode("QG");
//                inventory.setWarehouseCode(ShiroUtils.getWarehouseCode());
//                inventory.setMaterialCode(material.getCode());
//                inventory=inventoryService.selectFirstEntity(inventory);
//                if(inventory!=null){
//                    cft.setContainerCode(inventory.getContainerCode());
//                }else {
//                    cft.setContainerCode("0");
//                }
//                cft.setItemCode(ds.getMaterialCode());
//                cft.setItemName(material.getName());
//                cft.setItemBarcode(material.getCode());
//                cft.setItemState("良品");
//                float a = Convert.toFloat(ds.getQty());
//                cft.setQty(a);
//                cft.setItemUnit(material.getMasterUnit());
//                typeModels.add(cft);
//                list.add(ds.getId());
//                i++;
//            }
//        }
//        //若此单据无钱柜物料 则修改状态
//        ReceiptHeader receiptHeaderOver = new ReceiptHeader();
//        receiptHeaderOver.setId(item.getId());
//        if (i < 1){
//            StringBuffer str = new StringBuffer();
//            str.append(item.getScanid());
//            str.replace(0, 1, "1");
//            receiptHeaderOver.setScanid(String.valueOf(str));
//            iReceiptHeaderService.updateByModel(receiptHeaderOver);
//            return AjaxResult.success("已扫描一条入单据,没有发现钱柜物料");
//        }else {
//            //此处将入库主单,入库子单整合成发送给钱柜的的实体
//            cfh.setCubeCode("QG");
//            confirmModel.setConfirmHeaderModel(cfh);
//            confirmModel.setOrderItems(typeModels);
//            try {
//                String url=itemDownloadApiService.getQGUrl()+"OrderAssign";
//                String JsonParam = JSON.toJSONString(confirmModel);
//                String result = HttpUtils.bodypost(url, JsonParam);
//                if(StringUtils.isEmpty(result)){
//                    throw new ServiceException("接口地址错误");
//                }
//                ajaxResult = JSON.parseObject(result, AjaxResult.class);
//                if(ajaxResult.code==200) {
//                    //修改已经下发的子单状态
//                    short statusover = 100;
//                    ReceiptDetail detail = new ReceiptDetail();
//                    for (int j = 0; j < list.size(); j++) {
//                        detail.setId(list.get(j));
//                        detail.setStatus(statusover);
//                        iReceiptDetailService.updateByModel(detail);
//                    }
//                    StringBuffer str = new StringBuffer();
//                    str.append(item.getScanid());
//                    str.replace(0, 1, "1");
//                    receiptHeaderOver.setScanid(String.valueOf(str));
//                    if (iReceiptHeaderService.updateByModel(receiptHeaderOver) < 1) {
//                        throw new ServiceException("入库订单状态更新失败!");
//                    }
//                }
//            } catch (Exception e) {
//                throw new ServiceException("入库订单下发发送失败!");
//            }
//            return ajaxResult;
//        }
//    }


//    @Transactional
//    public AjaxResult shipmentAssign(ShipmentHeader item,List<ShipmentDetail> shipmentDetails) {
//        //查询出一条未被钱柜扫描过的出库单
//        AjaxResult ajaxResult=new AjaxResult();
//        ConfirmHeaderModel cfh = new ConfirmHeaderModel();
//        cfh.setOrderHeaderId(item.getId());
//        cfh.setOrderCode(item.getCode());
//        cfh.setOrderType("shipment");
//        cfh.setWarehouseCode(item.getWarehouseCode());
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        cfh.setcDatetime(df.format(new Date()));
//        ConfirmModel confirmModel = new ConfirmModel();
////        confirmModel.setConfirmHeaderModel(cfh);
//
//        List<ConfirmTypeModel> typeModels = new ArrayList<>();
//        //遍历出库子单
//        int i = 0;
//        List<Integer> list = new ArrayList();
//        for (ShipmentDetail ds : shipmentDetails) {
//            if(ds.getStatus()==0) {
//                Material material = new Material();
//                material.setCode(ds.getMaterialCode());
//                material = iMaterialService.selectFirstEntity(material);
//                if (material != null) {
//                    if (StringUtils.isEmpty(material.getZoneCode())) {
//                        material.setZoneCode("QG");
//                        if (!itemDownloadApiService.sendMaterial(material).hasErr()) {
////                        iMaterialService.updateByModel(material);
//                        } else {
//                            throw new ServiceException(material.getCode() + "物料同步失败");
//                        }
//                    }
//                    if (StringUtils.isNotEmpty(material.getZoneCode()) && !material.getZoneCode().equals("QG")) {
//                        if (itemDownloadApiService.sendMaterial(material).hasErr()) {
//                            throw new ServiceException(material.getCode() + "物料同步失败");
//                        }
//                    }
//                    ConfirmTypeModel cft = new ConfirmTypeModel();
//                    Inventory inventory = new Inventory();
//                    inventory.setZoneCode("QG");
//                    inventory.setWarehouseCode(ShiroUtils.getWarehouseCode());
//                    inventory.setMaterialCode(material.getCode());
//                    inventory = inventoryService.selectFirstEntity(inventory);
//                    if (inventory == null) {
//                        throw new ServiceException("该物料没有库存");
//                    } else {
//                        cft.setContainerCode(inventory.getContainerCode());
//                    }
//                    cft.setOrderDetailId(ds.getId());
//                    cft.setCompanyCode(ds.getCompanyCode());
//                    cft.setItemCode(ds.getMaterialCode());
//                    cft.setItemName(material.getName());
//                    cft.setItemBarcode(material.getCode());
//                    cft.setItemState("良品");
//                    float a = Convert.toFloat(ds.getQty());
//                    cft.setQty(a);
//                    cft.setItemUnit(material.getMasterUnit());
//                    typeModels.add(cft);
//                    list.add(ds.getId());
//                    i++;
//                }
//            }
//        }
//        //若此单据无钱柜物料 则修改状态
////        ShipmentHeader shipmentHeaderOver = new ShipmentHeader();
////        shipmentHeaderOver.setId(item.getId());
//        if (i < 1) {
////            StringBuffer str = new StringBuffer();
////            str.append(item.getScanid());
////            str.replace(0, 1, "1");
////            shipmentHeaderOver.setScanid(String.valueOf(str));
////            iShipmentHeaderService.updateByModel(shipmentHeaderOver);
//            return AjaxResult.success("选中的子单都已下发过");
//        } else {
//            //此处将出库主单,出库子单整合成发送给钱柜的的实体
//            cfh.setCubeCode("QG");
//            confirmModel.setConfirmHeaderModel(cfh);
//            confirmModel.setOrderItems(typeModels);
//            try {
//                String url = itemDownloadApiService.getQGUrl()+"OrderAssign";
//                String JsonParam = JSON.toJSONString(confirmModel);
//                String result = HttpUtils.bodypost(url, JsonParam);
//                if(StringUtils.isEmpty(result)){
//                    throw new ServiceException("接口地址错误");
//                }
//                ajaxResult = JSON.parseObject(result, AjaxResult.class);
//
//                if (ajaxResult.code == 200) {
//                    //修改已经下发的子单状态
//                    short statusover = 100;
//                    ShipmentDetail detail = new ShipmentDetail();
//                    for (int j = 0; j < list.size(); j++) {
//                        detail.setId(list.get(j));
//                        detail.setStatus(statusover);
//                        iShipmentDetailService.updateByModel(detail);
//                    }
//                    //如果该主单据的所有钱柜子单都已下发,则改变该主单的状态
//                    ShipmentDetail dt=new ShipmentDetail();
//                    dt.setShipmentCode(item.getCode());
//                    dt.setZoneCode("QG");
//                    dt.setStatus((short)0);
//                    List<ShipmentDetail> details=iShipmentDetailService.selectListEntityByEqual(dt);
//                    if(details==null) {
//                        StringBuffer str = new StringBuffer();
//                        str.append(item.getScanid());
//                        str.replace(0, 1, "1");
//                        item.setScanid(String.valueOf(str));
//                        if (iShipmentHeaderService.updateByModel(item) < 1) {
//                            throw new ServiceException("出库单据状态更新失败");
//                        }
//                    }
//                }
//            } catch (Exception e) {
//                throw new ServiceException("出库订单发送失败!");
//            }
//        }
//        return ajaxResult;
//    }


    @Transactional
    public AjaxResult taskAssign(TaskHeader taskHeader) {

        AjaxResult ajaxResult = new AjaxResult();
        //查找子任务列表
        List<TaskDetail> taskDetails = taskDetailService.list(new LambdaQueryWrapper<TaskDetail>()
                .eq(TaskDetail::getTaskId, taskHeader.getId())
                .lt(TaskDetail::getStatus, 100));

        if (taskDetails == null || taskDetails.size() == 0) {
            return AjaxResult.error("没有可执行的钱柜子任务");
        }

        ConfirmHeaderModel cfh = new ConfirmHeaderModel();
        cfh.setOrderHeaderId(taskHeader.getId());
        cfh.setOrderCode(taskDetails.get(0).getBillCode());
        if (taskHeader.getTaskType() == 200) {
            cfh.setOrderType("receipt");
        } else {
            cfh.setOrderType("shipment");
        }
        cfh.setWarehouseCode(taskHeader.getWarehouseCode());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cfh.setcDatetime(df.format(new Date()));
        ConfirmModel confirmModel = new ConfirmModel();
        confirmModel.setConfirmHeaderModel(cfh);

        List<ConfirmTypeModel> typeModels = new ArrayList<>();
        //遍历入库子单
        int i = 0;
        List<Integer> list = new ArrayList();
        for (TaskDetail ds : taskDetails) {
            Material material = materialService.getMaterialByCode(ds.getMaterialCode());
            if (material != null) {
                if (StringUtils.isEmpty(material.getZoneCode())) {
                    material.setZoneCode("QG");
                    AjaxResult ajaxResult1=itemDownloadApiService.sendMaterial(material, taskHeader.getContainerCode());
                    if(!ajaxResult1.hasErr()){

                    }else{
                        throw new ServiceException(material.getCode() + "物料同步钱柜失败::"+ajaxResult1.getMsg());
                    }
//                    if (!itemDownloadApiService.sendMaterial(material, taskHeader.getContainerCode()).hasErr()) {
////                        iMaterialService.updateByModel(material);
//                    } else {
//                        throw new ServiceException(material.getCode() + "物料同步失败"+);
//                    }
                }
                if (StringUtils.isNotEmpty(material.getZoneCode()) && !material.getZoneCode().equals("QG")) {
                    if (itemDownloadApiService.sendMaterial(material, taskHeader.getContainerCode()).hasErr()) {
                        throw new ServiceException(material.getCode() + "物料同步失败");
                    }
                }
                ConfirmTypeModel cft = new ConfirmTypeModel();
                cft.setOrderDetailId(ds.getId());
                cft.setCompanyCode(ds.getCompanyCode());
                cft.setContainerCode(ds.getContainerCode());
                cft.setItemCode(ds.getMaterialCode());
                cft.setItemName(material.getName());
                cft.setItemBarcode(material.getCode());
                cft.setItemState("良品");
                float a = Convert.toFloat(ds.getQty());
                cft.setQty(a);
                cft.setItemUnit(material.getUnit());
                typeModels.add(cft);
            }
        }

        cfh.setCubeCode("QG");
        confirmModel.setConfirmHeaderModel(cfh);
        confirmModel.setOrderItems(typeModels);
        try {
            String url = itemDownloadApiService.getQGUrl(taskHeader.getContainerCode()) + "OrderAssign";
            String JsonParam = JSON.toJSONString(confirmModel);

            log.info("任务下发:{}", JsonParam);
            String result = OkHttpUtils.bodypost(url, JsonParam, ShiroUtils.getWarehouseCode());
//            String result = HttpUtils.bodypost(url, JsonParam, ShiroUtils.getWarehouseCode());
            if (StringUtils.isEmpty(result)) {
                throw new ServiceException("接口地址错误或服务器连接不到或返回为空");
            }
            ajaxResult = JSON.parseObject(result, AjaxResult.class);
            if (ajaxResult.code == 200) {
                //taskHeader.setFirstStatus((short)10);
                //taskHeader.setLastStatus((short) 10);
//                taskHeader.setStatus(10);
                //for (TaskDetail item:taskDetails){
                //    item.setStatus(10);
                //    if(taskDetailService.updateByModel(item)<1){
                //        throw new ServiceException("任务执行发送失败!");
                //    }
                //}
//                if (!taskHeaderService.updateById(taskHeader)) {
//                    throw new ServiceException("任务执行发送失败!");
//                }
            }
        } catch (Exception e) {
            throw new ServiceException("任务执行发送失败!");
        }
        return ajaxResult;
    }
}