SrmDetailService.java 14.5 KB
package com.huaheng.pc.srm.service;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.common.utils.http.HttpUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.address.domain.Address;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.company.domain.Company;
import com.huaheng.pc.config.company.service.CompanyService;

import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.srm.domain.*;
import com.huaheng.pc.srm.mapper.ICSASNMapper;
import com.huaheng.pc.srm.mapper.SrmDetailMapper;
import com.huaheng.pc.stompwebsocket.domain.StompMessage;
import com.huaheng.pc.stompwebsocket.mapper.StompMessageMapper;
import com.huaheng.pc.u8.domain.ICSReceiptHeader;
import com.huaheng.pc.u8.service.ICSReceiptHeaderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@Service
public class SrmDetailService extends ServiceImpl<SrmDetailMapper, SrmDetail> {

    private static final Logger logger = LoggerFactory.getLogger(SrmDetailService.class);

    @Resource
    private SrmHeaderService srmHeaderService;
    @Resource
    private ICSITEMLotService icsitemLotService;
    @Resource
    private CompanyService companyService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private ICSReceiptHeaderService icsReceiptHeaderService;
    @Resource
    private AddressService addressService;


    public SrmDetail selectFirstModel(SrmDetail srmDetail){
        LambdaQueryWrapper<SrmDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotEmpty(srmDetail.getLineCode()),SrmDetail::getLineCode,srmDetail.getLineCode());
        queryWrapper.eq(StringUtils.isNotEmpty(srmDetail.getCode()),SrmDetail::getCode,srmDetail.getCode());
        queryWrapper.eq(StringUtils.isNotNull(srmDetail.getId()),SrmDetail::getId,srmDetail.getId());

        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }
    public List<SrmDetail> selectBySTNO(List<String> STNOS) {
        if(STNOS!=null&&STNOS.size()>0){
            String[] split=STNOS.toArray(new String[0]);
            LambdaQueryWrapper<SrmDetail> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.in(SrmDetail::getCode,split);
            return this.list(queryWrapper);
        }
        return null;
    }
    public List<SrmDetail> selectList(SrmDetail srmDetail) {
        LambdaQueryWrapper<SrmDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotEmpty(srmDetail.getCode()),SrmDetail::getCode,srmDetail.getCode());
        queryWrapper.eq(StringUtils.isNotNull(srmDetail.getEnable()),SrmDetail::getEnable,srmDetail.getEnable());
        queryWrapper.eq(StringUtils.isNotNull(srmDetail.getCheckCode()),SrmDetail::getCheckCode,srmDetail.getCheckCode());
        queryWrapper.eq(StringUtils.isNotNull(srmDetail.getHeaderId()),SrmDetail::getHeaderId,srmDetail.getHeaderId());

        return this.list(queryWrapper);
    }

    @Transactional
    public AjaxResult review(String code) {
        ICSITEMLot queryIcsitemLot = new ICSITEMLot();
        queryIcsitemLot.setLotNO(code);
        ICSITEMLot icsitemLot = icsitemLotService.selectFirstModel(queryIcsitemLot);
        if(icsitemLot == null){
            return AjaxResult.error("没有此条码信息,SRM系统未下发或者条码错误");
        }
        SrmDetail srmDetail = new SrmDetail();
        srmDetail.setLineCode(code);
        srmDetail = this.selectFirstModel(srmDetail);
        if(srmDetail == null){
            return AjaxResult.error("没有此条码的送货单信息,SRM系统未下发或者条码错误");
        }
        Company company = new Company();
        company.setCode(srmDetail.getCompanyCode());
        List<Company> companylist =companyService.getByDomain(company);
        if(companylist!=null&&companylist.size()>0){
            company=companylist.get(0);
            srmDetail.setCompanyName(company.getName());
        }
        srmDetail.setConfirmQty(icsitemLot.getLOTQTY());
        return AjaxResult.success(srmDetail);
    }

    @Transactional
    public AjaxResult reviewSrmModel(List<SrmDetail> srmDetails) {
        Set<String> codes = new HashSet<>();
        for (SrmDetail srmDetail : srmDetails) {
            //该明细是否之前送过,送过则新生成一条明细
            if(StringUtils.isEmpty(srmDetail.getUWarehouseCode())){
                return AjaxResult.error("仓库为空");
            }

            SrmDetail model = new SrmDetail();
            model.setId(srmDetail.getId());
            model = this.selectFirstModel(model);
            if(model.getEnable() == 1){
               break;
            }else {
                srmDetail.setEnable(1);
                srmDetail.setLastUpdate(new Date());
                srmDetail.setLastUpdateBy(ShiroUtils.getLoginName());
                this.updateById(srmDetail);
            }
            codes.add(model.getCode());
        }
        if(codes.isEmpty()){
            return AjaxResult.success("该编码已通过扫描确认,无法再次扫描确认");
        }
        //判断送货单主表,是否全收货
        for (String code : codes) {
            Integer number = 0;
            //查看该单据明细是否全收货
            SrmDetail srmDetail = new SrmDetail();
            srmDetail.setCode(code);
            List<SrmDetail> srmDetailList = this.selectList(srmDetail);
            srmDetail.setEnable(1);
            List<SrmDetail> details = this.selectList(srmDetail);
            if(srmDetailList.size() == details.size()){
                number = 2;
            }else {
                number = 1;
            }

            SrmHeader srmHeader = new SrmHeader();
            srmHeader.setCode(code);
            srmHeader = srmHeaderService.selectFirstModel(srmHeader);
            if(srmHeader != null){
                srmHeader.setEnable(number);
                srmHeaderService.updateById(srmHeader);
            }
        }

        return AjaxResult.success("复核成功");
    }

    @Transactional
    public AjaxResult checkDetail(SrmDetail srmDetail) {
        SrmDetail model = new SrmDetail();
        model.setId(srmDetail.getId());
        model = this.selectFirstModel(model);
        if(srmDetail.getUnqualifiedQty().compareTo(new BigDecimal(0))==1 && StringUtils.isEmpty(srmDetail.getReason())){
            return AjaxResult.error("不合格原因未填写");
        }
        BigDecimal qty = srmDetail.getConfirmQty().subtract(srmDetail.getQualifiedQty()).subtract(srmDetail.getUnqualifiedQty());
        if(qty.compareTo(new BigDecimal(0)) == -1){
            return AjaxResult.error("条码为"+model.getLineCode()+"的物料质检后的总数量大于到货数量");
        }
        if(qty.compareTo(new BigDecimal(0)) == 1){
            return AjaxResult.error("条码为"+model.getLineCode()+"的物料质检后的总数量小于到货数量");
        }
        srmDetail.setCheckCode(2);
        srmDetail.setCheckUser(ShiroUtils.getLoginName());
        srmDetail.setCheckTime(new Date());
        this.updateById(srmDetail);

        //判断子表是否全部质检完成
        SrmDetail detail = new SrmDetail();
        detail.setCode(model.getCode());
        detail.setEnable(1);
        List<SrmDetail> srmDetailList = this.selectList(detail);

        detail.setCheckCode(2);
        List<SrmDetail> srmDetails = this.selectList(detail);
        //修改主表字段
        SrmHeader srmHeader = new SrmHeader();
        srmHeader.setCode(model.getCode());
        srmHeader = srmHeaderService.selectFirstModel(srmHeader);
        AjaxResult result = AjaxResult.success();
        if(srmDetailList.size() == srmDetails.size()){
            srmHeader.setCheckCode(2);
            List<ReceiptHeader> receiptHeaderList = receiptHeaderService.list(new LambdaQueryWrapper<ReceiptHeader>().eq(ReceiptHeader::getUserDef3,srmHeader.getCode()));
            result = checkWhetherQualified(srmDetails, srmHeader);
            if(receiptHeaderList.size() > 0){
                receiptHeaderList.forEach(item->{
                    item.setDeleted(false);
                    receiptHeaderService.updateById(item);
                });
            }
        }else {
            srmHeader.setCheckCode(1);
        }
        if (200 != result.getCode()){
            result.setMsg("创建U8拒收单失败:"+result.getMsg());
            srmHeader.setUserDef1(result.getMsg());
            srmHeaderService.updateById(srmHeader);
            return result;
        }
        srmHeaderService.updateById(srmHeader);
        return AjaxResult.success("质检成功");
    }

    public AjaxResult checkWhetherQualified(List<SrmDetail> list, SrmHeader srmHeader) {
        if (list.isEmpty()){
            return AjaxResult.error("送货单明细为空值!");
        }
        if (srmHeader == null){
            return AjaxResult.error("送货单头表为空!");
        }
        List<ReceiptDetail> detailList = new ArrayList<>();
        // 以主单据id作为主键
        Map<Integer, SrmRejectHeader> map = new HashMap<>();
        for (SrmDetail srmDetail : list){
            // 判断不合格数量是否大于0
            if (srmDetail.getUnqualifiedQty().compareTo(new BigDecimal(0)) == 1){
                List<SrmRejectDetail> rejectDetails = new ArrayList<>();
                SrmRejectHeader srmRejectHeader = new SrmRejectHeader();
                // 查询入库单子表
                ReceiptDetail temp = new ReceiptDetail();
                temp.setUserDef2(srmDetail.getLineCode());
                ReceiptDetail receiptDetail = receiptDetailService.selectFirstEntity(temp);
                if (receiptDetail == null){
                    return AjaxResult.error("未查询到入库单子表信息!");
                }
                // 通过上游系统单号查询出库下发或回传主单
                ICSReceiptHeader icsReceiptHeader = new ICSReceiptHeader();
                icsReceiptHeader.setCCode(receiptDetail.getReferCode());
                icsReceiptHeader = icsReceiptHeaderService.selectModel(icsReceiptHeader);
                if (icsReceiptHeader == null){
                    return AjaxResult.error("查询出库下发回传主单失败,请检查该单据是否获取erp出货单!");
                }
                // 到货单主表id
                srmRejectHeader.setID(icsReceiptHeader.getID());
                // 制单人编码
                srmRejectHeader.setUsercode(ShiroUtils.getLoginName());
                // 修改实际入库数量
                receiptDetail.setQty(srmDetail.getQualifiedQty());
                // 如果实际入库数量为0,删除该条入库单详情
                if (receiptDetail.getQty().compareTo(new BigDecimal(0)) == 0){
                    detailList.add(receiptDetail);
                }else {
                    receiptDetailService.updateById(receiptDetail);
                }
                if (StringUtils.isEmpty(srmRejectHeader.getCompanyCode())){
                    // 账套名称
                    srmRejectHeader.setCompanyCode(srmDetail.getCompanyCode());
                }
                SrmRejectDetail rejectDetail = new SrmRejectDetail();
                // 到货单子表id
                rejectDetail.setAutoID(receiptDetail.getAutoId());
                // 物料编码
                rejectDetail.setCInvCode(srmDetail.getMaterialCode());
                // 不良数量
                rejectDetail.setINGQuantity(srmDetail.getUnqualifiedQty());
                //  判断是否为同一个单据
                if (map.containsKey(srmRejectHeader.getID())){
                    map.get(srmRejectHeader.getID()).getList().add(rejectDetail);
                }else {
                    rejectDetails.add(rejectDetail);
                    srmRejectHeader.setList(rejectDetails);
                    srmRejectHeader.setWarehouseCode(srmDetail.getWarehouseCode());
                    map.put(srmRejectHeader.getID(), srmRejectHeader);
                }
            }
        }
        if (map.size() == 0){
            return AjaxResult.success("质检成功!");
        }
        AjaxResult result  = this.sendSrmRejectData(map);
        if (!result.hasErr() && detailList.size() > 0){
            receiptDetailService.removeByIds(detailList);
        }
        return result;
    }

    public AjaxResult sendSrmRejectData(Map<Integer, SrmRejectHeader> map) {
        if (map.isEmpty()){
            return AjaxResult.error("退货单数据为空!");
        }
        Address address = new Address();
        address.setParam("U8");
        address.setWarehouseCode("KS0001");
        address = addressService.selectEntity(address);
        if (address == null){
            return AjaxResult.error("请检查U8地址是否存在!");
        }
        String url = address.getUrl() +"CreateRF";
        for (SrmRejectHeader srmRejectHeader:map.values()){
            List list = new ArrayList<>();
            list.add(srmRejectHeader);
            String jsonString = JSON.toJSONString(list);
            String result = HttpUtils.bodypost(url, jsonString,srmRejectHeader.getWarehouseCode());
            logger.info("U8返回信息:"+ result);
            AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
            if (ajaxResult.hasErr()){
                return ajaxResult;
            }
        }
        return AjaxResult.success("退货单发送成功!");
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateByLineCode(String lineCode) {
        //       update srmdetail set  UploadStatus2 =3 where lineCode=#{lineCode, jdbcType=VARCHAR}
        SrmDetail srmDetail=new SrmDetail();
        srmDetail.setUploadStatus2(3);
        LambdaQueryWrapper<SrmDetail> lambdaQueryWrapper=Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(SrmDetail::getLineCode,lineCode);
        this.update(srmDetail,lambdaQueryWrapper);
    }
}