ReceiptApiService.java 21.6 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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
package com.huaheng.api.general.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.general.domain.ReceiptDomain;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.FilterConfigDetail.domain.FilterConfigDetail;
import com.huaheng.pc.config.FilterConfigDetail.service.FilterConfigDetailService;
import com.huaheng.pc.config.company.domain.Company;
import com.huaheng.pc.config.company.service.CompanyService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.receiptType.domain.ReceiptType;
import com.huaheng.pc.config.receiptType.service.ReceiptTypeService;
import com.huaheng.pc.config.statusFlow.domain.StatusFlowHeader;
import com.huaheng.pc.config.statusFlow.service.StatusFlowHeaderService;
import com.huaheng.pc.config.supplier.domain.Supplier;
import com.huaheng.pc.config.supplier.service.SupplierService;
import com.huaheng.pc.config.warehouse.domain.Warehouse;
import com.huaheng.pc.config.warehouse.service.WarehouseService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptDetailHistory.domain.ReceiptDetailHistory;
import com.huaheng.pc.receipt.receiptDetailHistory.service.ReceiptDetailHistoryService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.receipt.receiptHeaderHistory.domain.ReceiptHeaderHistory;
import com.huaheng.pc.receipt.receiptHeaderHistory.service.ReceiptHeaderHistoryService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Transactional
public class ReceiptApiService {
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private ReceiptTypeService receiptTypeService;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private CompanyService companyService;
    @Resource
    private MaterialService materialService;
    @Resource
    private SupplierService supplierService;
    @Resource
    private FilterConfigDetailService filterConfigDetailService;
    @Resource
    private StatusFlowHeaderService statusFlowHeaderService;
    @Resource
    private ReceiptHeaderHistoryService receiptHeaderHistoryService;
    @Resource
    private ReceiptDetailHistoryService receiptDetailHistoryService;
    @Resource
    private TaskDetailService taskDetailService;

    /**
     * 入库单下发
     *
     * @param receipt 入库单
     * @return 是否下发成功
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult receipt(ReceiptDomain receipt) {

        /* 0.step 获取入库头表,获取入库明细*/
        ReceiptHeader receiptHeader = receipt.getReceiptHeader();
        List<ReceiptDetail> receiptDetails = receipt.getReceiptDetails();
        /* 1.step 校验入库单是否为空*/
        if (receiptHeader == null || receiptDetails == null) {
            return AjaxResult.error("入库主单或入库子单为空");
        }
        if(receiptDetails.isEmpty()) {
            return AjaxResult.error("入库子单为空");
        }
        String receiptCode = receiptHeaderService.createCode(receiptHeader.getReceiptType());
        receiptHeader.setCode(receiptCode);
        receiptHeader.setCreatedBy(QuantityConstant.PLATFORM_ERP);
        receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_RECEIVING);
        receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_RECEIVING);
        /* 2.step 检查入库头表合法性*/
        AjaxResult checkReceiptHeaderResult = checkReceiptHeader(receiptHeader);
        if (checkReceiptHeaderResult.hasErr()) {
            return AjaxResult.error(checkReceiptHeaderResult.getMsg());
        }
        /* 3.step 检查入库明细合法性*/
        AjaxResult checkReceiptDetailResult = checkReceiptDetail(receiptDetails);
        if (checkReceiptDetailResult.hasErr()) {
            return AjaxResult.error(checkReceiptDetailResult.getMsg());
        }

        /* 4.step 计算入库明细总行数、总数量*/
        BigDecimal totalQty = new BigDecimal(0);
        for (ReceiptDetail receiptDetail : receiptDetails) {
            totalQty = totalQty.add(receiptDetail.getTotalQty());
        }
        receiptHeader.setTotalLines(receiptDetails.size());
        receiptHeader.setTotalQty(totalQty);

        try {
            receiptHeaderService.save(receiptHeader);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }

        /* 6.step 保存入库明细*/
        LambdaQueryWrapper<ReceiptHeader> lambda = Wrappers.lambdaQuery();
        lambda.eq(ReceiptHeader::getWarehouseCode, receiptHeader.getWarehouseCode())
                .eq(ReceiptHeader::getCode, receiptHeader.getCode());
        receiptHeader = receiptHeaderService.getOne(lambda);
        List<ReceiptDetail> receiptDetailList = new ArrayList<ReceiptDetail>();
        for (ReceiptDetail receiptDetail : receiptDetails) {
            receiptDetail.setReceiptId(receiptHeader.getId());
            receiptDetail.setReceiptCode(receiptHeader.getCode());
            receiptDetail.setWarehouseCode(receiptHeader.getWarehouseCode());
            receiptDetail.setCompanyCode(receiptHeader.getCompanyCode());
            Material material = materialService.findAllByCode(receiptDetail.getMaterialCode(),
                    receiptHeader.getWarehouseCode());
            receiptDetail.setMaterialName(material.getName());
            receiptDetail.setMaterialSpec(material.getSpec());
            receiptDetail.setMaterialUnit(material.getUnit());
            if (!receiptDetailService.save(receiptDetail)) {
                throw new ServiceException("保存入库明细失败");
            }
            receiptDetailList.add(receiptDetail);

        }
        /* 7.step回传入库单和入库明细单 */
        ReceiptDomain receiptDomain = new ReceiptDomain();
        receiptDomain.setReceiptHeader(receiptHeader);
        receiptDomain.setReceiptDetails(receiptDetailList);

        return AjaxResult.success(receiptDomain);
    }

    /**
     * 检查入库头表合法性
     *
     * @param receiptHeader 入库头表
     * @return 检查结果
     */
    private AjaxResult checkReceiptHeader(ReceiptHeader receiptHeader) {
        /* 0.step 必填项是否为空*/
        if (StringUtils.isEmpty(receiptHeader.getCode()) || StringUtils.isEmpty(receiptHeader.getCompanyCode()) ||
                StringUtils.isEmpty(receiptHeader.getWarehouseCode()) || StringUtils.isEmpty(receiptHeader.getReceiptType())) {
            return AjaxResult.error("入库主单字段有误");
        }

        /* 1.step 查询该单据编码是否已存在*/
        LambdaQueryWrapper<ReceiptHeader> receiptHeaderLambda = Wrappers.lambdaQuery(receiptHeader);
        if (receiptHeaderService.getOne(receiptHeaderLambda) != null) {
            return AjaxResult.error("该单据已存在:" + receiptHeader.getCode());
        }

        /* 2.step 判断入库类型是否匹配*/
        LambdaQueryWrapper<ReceiptType> receiptTypeLambda = Wrappers.lambdaQuery();
        receiptTypeLambda.eq(ReceiptType::getCode, receiptHeader.getReceiptType())
                .eq(ReceiptType::getWarehouseCode, receiptHeader.getWarehouseCode())
                .eq(ReceiptType::getCompanyCode, receiptHeader.getCompanyCode());
        if (receiptTypeService.getOne(receiptTypeLambda) == null) {
            return AjaxResult.error("没有对应的入库单类型");
        }

        /* 3.step 判断仓库是否存在*/
        LambdaQueryWrapper<Warehouse> warehouseLambda = Wrappers.lambdaQuery();
        warehouseLambda.eq(Warehouse::getCode, receiptHeader.getWarehouseCode());
        if (warehouseService.getOne(warehouseLambda) == null) {
            return AjaxResult.error("该仓库不存在");
        }

        /* 4.step 判断货主是否存在*/
        LambdaQueryWrapper<Company> companyLambda = Wrappers.lambdaQuery();
        companyLambda.eq(Company::getCode, receiptHeader.getCompanyCode());
        if (companyService.getOne(companyLambda) == null) {
            return AjaxResult.error("该货主不存在");
        }

        return AjaxResult.success("");
    }

    /**
     * 检查入库明细合法性
     *
     * @param receiptDetails 入库明细
     * @return 检查结果
     */
    private AjaxResult checkReceiptDetail(List<ReceiptDetail> receiptDetails) {

        for (ReceiptDetail receiptDetail : receiptDetails) {
            /* 0.step 判断必填字段是否为空且总数量不能为0*/
            if (receiptDetail.getMaterialCode() == null ||
                    new BigDecimal(0).compareTo(receiptDetail.getTotalQty() != null ?
                            receiptDetail.getTotalQty() : new BigDecimal(0)) == 0) {
                return AjaxResult.error("入库明细字段有误");
            }

            /* 1.step 判断供应商是否存在*/
            if (receiptDetail.getSupplierCode() != null) {
                LambdaQueryWrapper<Supplier> supplierLambda = Wrappers.lambdaQuery();
                supplierLambda.eq(Supplier::getCode, receiptDetail.getSupplierCode());
                if (supplierService.getOne(supplierLambda) == null) {
                    return AjaxResult.error("供应商不存在");
                }
            }

            /* 2.step 判断定位规则是否存在*/
            if (receiptDetail.getLocatingRule() != null) {
                LambdaQueryWrapper<FilterConfigDetail> filterConfigDetailLambda = Wrappers.lambdaQuery();
                filterConfigDetailLambda.eq(FilterConfigDetail::getCode, receiptDetail.getLocatingRule())
                        .eq(FilterConfigDetail::getModuleType, "receipt")
                        .eq(FilterConfigDetail::getRecordType, "locationRule");
                FilterConfigDetail filterConfigDetail = filterConfigDetailService.getOne(filterConfigDetailLambda);
                if (filterConfigDetail == null) {
                    return AjaxResult.error(receiptDetail.getLocatingRule() + "定位规则不存在");
                }
            }

            /* 3.step 判断流程编码是否存在*/
            if (receiptDetail.getStatusFlowCode() != null) {
                LambdaQueryWrapper<StatusFlowHeader> statusFlowHeaderLambda = Wrappers.lambdaQuery();
                statusFlowHeaderLambda.eq(StatusFlowHeader::getCode, receiptDetail.getStatusFlowCode())
                        .eq(StatusFlowHeader::getModuleType, "receipt")
                        .eq(StatusFlowHeader::getRecordType, "receivingFlow");
                StatusFlowHeader statusFlowHeader = statusFlowHeaderService.getOne(statusFlowHeaderLambda);
                if (statusFlowHeader == null) {
                    return AjaxResult.error(receiptDetail.getLocatingRule() + "该流程不存在");
                }
            }
        }

        return AjaxResult.success("");
    }


    @Transactional
    public AjaxResult remove(List<String> receiptCodeList) {
        LambdaQueryWrapper<ReceiptHeader> headerQueryWrapper;
        for (String receiptCode : receiptCodeList) {
            headerQueryWrapper = Wrappers.lambdaQuery();
            headerQueryWrapper.eq(ReceiptHeader::getCode,receiptCode);
            ReceiptHeader receiptHeader = receiptHeaderService.getOne(headerQueryWrapper);
            if (receiptHeader == null) {
                return AjaxResult.success("");
            }
            if ((receiptHeader.getFirstStatus() >= QuantityConstant.RECEIPT_HEADER_POSTING && receiptHeader.getLastStatus() >= QuantityConstant.RECEIPT_HEADER_POSTING) ||
                    (receiptHeader.getFirstStatus() < QuantityConstant.RECEIPT_HEADER_POOL & receiptHeader.getLastStatus() < QuantityConstant.RECEIPT_HEADER_POOL)) {
                ReceiptHeaderHistory receiptHeaderHistory = new ReceiptHeaderHistory();
                List<ReceiptDetailHistory> receiptDetailHistoryList = new ArrayList<>();
                //查询入库单明细
                LambdaQueryWrapper<ReceiptDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
                lambdaQueryWrapper.eq(ReceiptDetail::getReceiptCode, receiptCode);
                List<ReceiptDetail> list = receiptDetailService.list(lambdaQueryWrapper);

                //复制到入库历史实体
                com.huaheng.common.utils.bean.BeanUtils.copyBeanProp(receiptHeaderHistory, receiptHeader);
                for (ReceiptDetail receiptDetail : list) {
                    ReceiptDetailHistory receiptDetailHistory = new ReceiptDetailHistory();
                    com.huaheng.common.utils.bean.BeanUtils.copyBeanProp(receiptDetailHistory, receiptDetail);
                    receiptDetailHistoryList.add(receiptDetailHistory);
                }


                receiptHeaderHistory.setLastUpdatedBy(ShiroUtils.getLoginName());
                if (!receiptHeaderService.removeById(receiptHeader.getId())) {
                    throw new ServiceException("删除头表失败");
                }
                if (!receiptHeaderHistoryService.save(receiptHeaderHistory)) {
                    throw new ServiceException("新增历史入库单失败");
                }
                // 当存在明细时删除
                if (list.size() != 0) {
                    //删除入库明细
                    List<Integer> receiptDetailIds = new ArrayList<>();
                    for (int i = 0; i < receiptDetailHistoryList.size(); i++) {
                        receiptDetailHistoryList.get(i).setLastUpdatedBy(ShiroUtils.getLoginName());
                        receiptDetailHistoryList.get(i).setReceiptId(receiptHeaderHistory.getId());
                        receiptDetailIds.add(receiptDetailHistoryList.get(i).getId());
                    }
                    if (!receiptDetailService.removeByIds(receiptDetailIds)) {
                        throw new ServiceException("删除明细表失败");
                    }
                    if (!receiptDetailHistoryService.saveBatch(receiptDetailHistoryList)) {
                        throw new ServiceException("新增明细失败");
                    }
                }
            } else {
                return AjaxResult.success("入库单没有完成,无法删除");
            }
        }
        return AjaxResult.success("删除成功");
    }

    public AjaxResult search(String receiptCode, String companyCode, String warehouseCode) {
        if (companyCode == null) {
            return AjaxResult.error("货主编码不能为空");
        }
        if (warehouseCode == null) {
            return AjaxResult.error("仓库编码不能为空");
        }
        ReceiptDomain receiptDomain = new ReceiptDomain();
        LambdaQueryWrapper<ReceiptHeader> headerLambdaQuery = Wrappers.lambdaQuery();
        LambdaQueryWrapper<ReceiptDetail> detailLambdaQuery = Wrappers.lambdaQuery();
        headerLambdaQuery.eq(ReceiptHeader::getWarehouseCode, warehouseCode)
                .eq(ReceiptHeader::getCompanyCode, companyCode)
                .eq(ReceiptHeader::getCode, receiptCode);
        detailLambdaQuery.eq(ReceiptDetail::getWarehouseCode, warehouseCode)
                .eq(ReceiptDetail::getCompanyCode, companyCode)
                .eq(ReceiptDetail::getReceiptCode, receiptCode);
        ReceiptHeader receiptHeader = receiptHeaderService.getOne(headerLambdaQuery);
        List<ReceiptDetail> receiptDetailList = receiptDetailService.list(detailLambdaQuery);
        receiptDomain.setReceiptHeader(receiptHeader);
        receiptDomain.setReceiptDetails(receiptDetailList);
        return AjaxResult.success("查询成功", receiptDomain);
    }


    //入库回传
    @Transactional
    public AjaxResult confirmReceipt(Map map) {
        AjaxResult ajaxResult = new AjaxResult();

        //1、查看数据是否正常
        if(map.isEmpty()){
            ajaxResult.setCode(400);
            ajaxResult.setMsg("查询的内容为空,请ERP核对查询信息 Document does not exist");
            ajaxResult.setData(this.getEmptyDate());
            return ajaxResult;        }
        Object code = map.get("referCode");
        if(StringUtils.isNull(code)){
            ajaxResult.setCode(400);
            ajaxResult.setMsg("查询的内容中没有按照协议要求传送referCode字段信息 Document does not exist");
            ajaxResult.setData(this.getEmptyDate());
            return ajaxResult;        }
        String referCode = code.toString();

        //2、查找入库单
        LambdaQueryWrapper<ReceiptHeader> receiptHeaderLambd = Wrappers.lambdaQuery();
        receiptHeaderLambd.eq(ReceiptHeader::getReferCode,referCode).last("limit 1");
        ReceiptHeader receiptHeader = receiptHeaderService.getOne(receiptHeaderLambd);
        if(receiptHeader == null){
            ajaxResult.setCode(400);
            ajaxResult.setMsg("传入的referCode信息在WMS系统中不存在,请ERP核对信息后,在查询 Document does not exist");
            ajaxResult.setData(this.getEmptyDate());
            return ajaxResult;
        }

        //3、查看入库单是否回传
//        if(receiptHeader.getLastStatus().equals(QuantityConstant.RECEIPT_HEADER_RETURN)){
//            return AjaxResult.success("Document returned");
//        }

        //4、判断入库单是否完成,先查任务,再查单据
        LambdaQueryWrapper<TaskDetail> taskDetailLambd = Wrappers.lambdaQuery();
        taskDetailLambd.eq(TaskDetail::getBillCode,receiptHeader.getCode())
                .lt(TaskDetail::getStatus,QuantityConstant.TASK_STATUS_COMPLETED);
        List<TaskDetail> taskDetails = taskDetailService.list(taskDetailLambd);
        if(!taskDetails.isEmpty()){
            ajaxResult.setCode(400);
            ajaxResult.setMsg("查询的单号:"+referCode +" 还有任务未完成,请找仓库人员核对是否还有任务没有去作业,稍后查询");
            ajaxResult.setData(this.getEmptyDate());
            return ajaxResult;        }

        //查看单据
        LambdaQueryWrapper<ReceiptDetail> receiptDetailLambd = Wrappers.lambdaQuery();
        receiptDetailLambd.eq(ReceiptDetail::getReceiptCode,receiptHeader.getCode())
                .gt(ReceiptDetail::getOpenQty,new BigDecimal(0)).last("and totalQty = openQty");
        List<ReceiptDetail> receiptDetails = receiptDetailService.list(receiptDetailLambd);
        if(receiptDetails.isEmpty()){
            ajaxResult.setCode(400);
            ajaxResult.setMsg("查询的单号:"+referCode +" 还有单据没有进行入库收货作业,请稍后查询");
            ajaxResult.setData(this.getEmptyDate());
            return ajaxResult;        }

        LambdaQueryWrapper<ReceiptDetail> receiptDetailLambda = Wrappers.lambdaQuery();
        receiptDetailLambda.eq(ReceiptDetail::getReceiptCode,receiptHeader.getCode());
        List<ReceiptDetail> receiptDetailList = receiptDetailService.list(receiptDetailLambda);
        if(receiptDetailList.size() > receiptDetails.size() && receiptHeader.getLastStatus() < QuantityConstant.RECEIPT_HEADER_RETURN){
            ajaxResult.setCode(400);
            ajaxResult.setMsg("查询的单号:"+referCode +" 还有未完成的单据明细 Unfinished documents");
            ajaxResult.setData(this.getEmptyDate());
            return ajaxResult;        }

        //修改头表
        if(!receiptHeader.getFirstStatus().equals(QuantityConstant.RECEIPT_HEADER_RETURN)) {
            receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_RETURN);
            receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_RETURN);
            receiptHeaderService.updateById(receiptHeader);
        }
        //5、设置回传数据
        Map<String,String> map1 = new HashMap<>();
        map1.put("referCode",referCode);
        map1.put("referId",receiptHeader.getReferId().toString());
        map1.put("referType",receiptHeader.getReceiptType());

        Map<String,Object> map2 = new HashMap<>();
        map2.put("receiptHeader",map1);

        List<ReceiptDetail> detailList = receiptDetailService.getERPDetail(receiptHeader.getReferCode());
        List<Map> list = new ArrayList<>();
        for (ReceiptDetail detail : detailList) {
            Map<String,String> map3 = new HashMap<>();
            map3.put("materialCode",detail.getMaterialCode());
            map3.put("referId",receiptHeader.getReferId().toString());
            map3.put("referCode",receiptHeader.getReferCode());
            map3.put("referLineNum",detail.getReferLineNum());
            map3.put("openQty",detail.getOpenQty().toString());
            list.add(map3);
        }
        map2.put("receiptDetails",list);
        ajaxResult.setCode(200);
        ajaxResult.setMsg("success");
        ajaxResult.setData(map2);
        return ajaxResult;
    }

    public Map getEmptyDate(){
        Map map = new HashMap();
        map.put("receiptHeader",new HashMap<>());
        map.put("receiptDetails",new ArrayList<>());
        String  param= JSON.toJSONString(map);
        System.out.println(param);
        return map;
    }
}