SrmDetailService.java 19.4 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.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.apache.commons.collections.CollectionUtils;
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.eq(StringUtils.isNotEmpty(srmDetail.getMark()),SrmDetail::getMark,srmDetail.getMark());
        queryWrapper.eq(StringUtils.isNotEmpty(srmDetail.getWarehouseCode()),SrmDetail::getWarehouseCode,srmDetail.getWarehouseCode());

        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.isNotNull(srmDetail.getHeaderId()),SrmDetail::getHeaderId,srmDetail.getHeaderId());
        queryWrapper.eq(StringUtils.isNotEmpty(srmDetail.getWarehouseCode()),SrmDetail::getWarehouseCode,srmDetail.getWarehouseCode());
        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.getUploadStatus1()),SrmDetail::getUploadStatus1,srmDetail.getUploadStatus1());
        queryWrapper.eq(StringUtils.isNotNull(srmDetail.getUploadStatus2()),SrmDetail::getUploadStatus2,srmDetail.getUploadStatus2());
        queryWrapper.eq(StringUtils.isNotNull(srmDetail.getUploadStatus4()),SrmDetail::getUploadStatus4,srmDetail.getUploadStatus4());


        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);
        detail.setWarehouseCode(ShiroUtils.getWarehouseCode());
        List<SrmDetail> srmDetailList = this.selectList(detail);

        detail.setCheckCode(2);
        List<SrmDetail> srmDetails = this.selectList(detail);
        //修改主表字段
        SrmHeader srmHeader = new SrmHeader();
        srmHeader.setCode(model.getCode());
        srmHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
        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);
        }
//        List<SrmDetail> list = this.list(new LambdaQueryWrapper<SrmDetail>()
////                .eq(SrmDetail::getCode, model.getCode())
////                .eq(SrmDetail::getWarehouseCode, ShiroUtils.getWarehouseCode())
////                .eq(SrmDetail::getEnable, 1)
////                .eq(SrmDetail::getCheckCode, 2)
////                .eq(SrmDetail::getUploadStatus1, 0)
////                .eq(SrmDetail::getUploadStatus4, 0)
////        );
//        List<SrmDetail> qcList = this.list(new LambdaQueryWrapper<SrmDetail>()
//                .eq(SrmDetail::getCode, model.getCode())
//                .eq(SrmDetail::getWarehouseCode, ShiroUtils.getWarehouseCode())
//                .likeRight(SrmDetail::getMaterialCode, 2)
//                .eq(SrmDetail::getQcFlag,1)
//        );
//        if(CollectionUtils.isNotEmpty(list)&&CollectionUtils.isNotEmpty(qcList)&&list.size()==qcList.size()){
//            srmHeaderService.confirmSRMModel(qcList);
//            result = srmHeaderService.confirmU8Model(qcList);
//            if (result.hasErr()){
//                result.setMsg("系统自动获取erp到货单失败请手动获取:" + result.getMsg());
//                srmHeader.setUserDef1(result.getMsg());
//                srmHeaderService.updateById(srmHeader);
//                return result;
//            }
//        }
        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 checkSRMDetail(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("质检成功");
    }
    @Transactional(rollbackFor = Exception.class)
    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 receiptDetail = receiptDetailService.getOne(new LambdaQueryWrapper<ReceiptDetail>().eq(ReceiptDetail::getUserDef2,srmDetail.getLineCode()).eq(ReceiptDetail::getWarehouseCode,ShiroUtils.getWarehouseCode()));
                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);
    }
}