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


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.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.company.domain.Company;
import com.huaheng.pc.config.company.service.CompanyService;

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 org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

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

    @Resource
    private SrmHeaderService srmHeaderService;
    @Resource
    private ICSITEMLotService icsitemLotService;
    @Resource
    private CompanyService companyService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;


    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.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());

        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);

        if(srmDetailList.size() == srmDetails.size()){
            srmHeader.setCheckCode(2);
            ReceiptHeader receiptHeader = new ReceiptHeader();
            receiptHeader.setUserDef3(srmHeader.getCode());
            List<ReceiptHeader> receiptHeaderlist = receiptHeaderService.getByDomain(receiptHeader);
            if(receiptHeaderlist != null&&receiptHeaderlist.size()>0){
                receiptHeader=receiptHeaderlist.get(0);
                receiptHeader.setDeleted(false);
                receiptHeaderService.updateById(receiptHeader);
            }

        }else {
            srmHeader.setCheckCode(1);
        }
        srmHeaderService.updateById(srmHeader);
        return AjaxResult.success("质检成功");
    }
}