ReceiptContainerDetailServiceImpl.java 16.9 KB
package com.huaheng.pc.receipt.receiptContainerDetail.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.common.utils.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.http.HttpUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.mobile.receipt.ReceiptBill;
import com.huaheng.pc.config.address.domain.Address;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.container.domain.Container;
import com.huaheng.pc.config.container.service.ContainerService;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.inventory.inventoryClear.domain.InventorySearch;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.inventory.inventoryTransaction.domain.InventoryTransaction;
import com.huaheng.pc.inventory.inventoryTransaction.service.InventoryTransactionService;
import com.huaheng.pc.receipt.receiptContainerDetail.domain.ReceiptContainerDetail;
import com.huaheng.pc.receipt.receiptContainerDetail.mapper.ReceiptContainerDetailMapper;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerHeader;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerView;
import com.huaheng.pc.receipt.receiptContainerHeader.service.ReceiptContainerHeaderService;
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.system.role.domain.Role;
import com.huaheng.pc.system.user.domain.User;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerHeader;
import com.huaheng.pc.receipt.receiptContainerHeader.service.ReceiptContainerHeaderService;
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 org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@Service
public class ReceiptContainerDetailServiceImpl extends ServiceImpl<ReceiptContainerDetailMapper, ReceiptContainerDetail> implements ReceiptContainerDetailService{

    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ReceiptContainerHeaderService receiptContainerHeaderService;
    @Resource
    private MaterialService materialService;
    @Resource
    private LocationService locationService;
    @Resource
    private AddressService addressService;
    @Resource
    private ReceiptContainerDetailMapper receiptContainerDetailMapper;
    @Resource
    private ContainerService containerService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private InventoryTransactionService inventoryTransactionService;
    /**
     * 根据入库单编码查询入库组盘明细
     * @param receiptCode 入库单编码
     * @return AjaxResult
     */
    @Override
    public AjaxResult<List<Map<String, Object>>> getReceiptInfoByBill(String receiptCode) {
        LambdaQueryWrapper<ReceiptContainerDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ReceiptContainerDetail::getReceiptCode, receiptCode)
                .eq(ReceiptContainerDetail::getWarehouseCode, ShiroUtils.getWarehouseCode());

        List<Map<String, Object>> detail = this.listMaps(lambdaQueryWrapper);
        return AjaxResult.success(detail);
    }

    /**
     * 撤销入库组盘明细
     * @param idList
     * @return
     */
    @Override
    public Boolean detailRemove(List<Integer> idList) {
        for (Integer id : idList){
            ReceiptContainerDetail receiptContainerDetail = this.getById(id);
            //如果已生成任务不允许撤销
            if (receiptContainerDetail.getStatus() < QuantityConstant.RECEIPT_CONTAINER_TASK){
                //回滚入库单明细收货数量
                ReceiptDetail receiptDetail = receiptDetailService.getById(receiptContainerDetail.getReceiptDetailId());
                receiptDetail.setTaskQty(receiptDetail.getTaskQty().subtract(receiptContainerDetail.getQty()));

                //如果为临时容器,则删除
                receiptContainerDetail.getContainerCode();

                if (!receiptDetailService.updateById(receiptDetail)){throw new SecurityException("回滚入库单明细失败");}
                //删除组盘明细
                if (!this.removeById(id)){ throw new ServiceException("回滚入库组盘失败");}
                LambdaQueryWrapper<ReceiptContainerDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
                lambdaQueryWrapper.eq(ReceiptContainerDetail::getReceiptContainerId, receiptContainerDetail.getReceiptContainerId());
                List<ReceiptContainerDetail> list = this.list(lambdaQueryWrapper);
                if (list.size() == 0){
                    String locationCode = receiptContainerDetail.getLocationCode();
                    if(StringUtils.isNotEmpty(locationCode)) {
                        locationService.updateStatus(locationCode, QuantityConstant.STATUS_LOCATION_EMPTY);
                    }
                    if (!receiptContainerHeaderService.removeById(receiptContainerDetail.getReceiptContainerId())){
                        throw new ServiceException("删除入库组盘头失败");
                    }
                }

                //查询入库头表
                LambdaQueryWrapper<ReceiptContainerDetail> containerDetailLambda = Wrappers.lambdaQuery();
                containerDetailLambda.eq(ReceiptContainerDetail::getReceiptId, receiptContainerDetail.getReceiptId());
                List<ReceiptContainerDetail> containerDetailList = this.list(containerDetailLambda);


                //如果入库组盘没有该入库单的组盘信息,回滚入库单状态
                if (containerDetailList.size() == 0){
                    ReceiptHeader receiptHeader = new ReceiptHeader();
                    receiptHeader.setId(receiptContainerDetail.getReceiptId());
                    receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_RECEIVING);
                    receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_RECEIVING);
                    receiptHeader.setLastUpdatedBy(ShiroUtils.getLoginName());

                    if (!receiptHeaderService.updateById(receiptHeader)){ throw new ServiceException("回滚头表状态失败"); }
                }

            } else {
                throw new SecurityException("已生成任务,不允许取消");
            }
        }
        return true;
    }


//    /**
//     * 移动端收货
//     *
//     * @param code
//     * @return
//     */
//    @Override
//    public AjaxResult scanReceiptCode(String code) {
//        LambdaQueryWrapper<ReceiptDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
//        lambdaQueryWrapper.eq(ReceiptDetail::getReceiptCode, code);
//        List<ReceiptDetail> receiptContainerDetails = list(lambdaQueryWrapper);
//        return AjaxResult.success(receiptContainerDetails);
//    }

    /**保存到组盘详细*/
    @Override
    public List<Integer> insertTodayReceiptcContainerDetail(int headerId, int receiptId, List<Integer> receiptDetailId, List<ReceiptBill> receiptBills) {
        List<Integer> mReceiptContainerIds = new ArrayList<>();

        ReceiptHeader receiptHeader = receiptHeaderService.getById(receiptId);
        if(receiptHeader == null) {
            throw new ServiceException("没有找到入库单表头!");
        }
        for(int i=0; i<receiptBills.size(); i++) {
            ReceiptBill receiptBill = receiptBills.get(i);
            Material condition2 = new Material();
            condition2.setCode(receiptBill.getMaterialCode());
            condition2.setWarehouseCode(ShiroUtils.getWarehouseCode());
            condition2.setDeleted(false);
            condition2.setEnable(true);
            LambdaQueryWrapper<Material> lambdaQueryWrapper = Wrappers.lambdaQuery();
            lambdaQueryWrapper.eq(Material::getCode, receiptBill.getMaterialCode())
                    .eq(Material::getWarehouseCode, ShiroUtils.getWarehouseCode());
            Material material = materialService.getOne(lambdaQueryWrapper);
            ReceiptContainerDetail record = new ReceiptContainerDetail();
            record.setReceiptContainerId(headerId);
            record.setReceiptCode(receiptHeader.getCode());
            record.setReceiptId(receiptId);
            record.setReceiptDetailId(receiptDetailId.get(i));
            record.setMaterialCode(receiptBill.getMaterialCode());
            record.setMaterialName(material.getName());
            record.setMaterialSpec(material.getSpec());
            record.setInventorySts("good");
            record.setQty(receiptBill.getQty());
            record.setContainerCode(receiptBills.get(0).getReceiptContainerCode());
            if(receiptBill.getWeight() != null) {
                record.setWeight(receiptBill.getWeight());
            }
            record.setCreatedBy(ShiroUtils.getLoginName());
            record.setLastUpdatedBy(ShiroUtils.getLoginName());

            LambdaQueryWrapper<ReceiptContainerDetail> detailyWrapper = Wrappers.lambdaQuery();
            detailyWrapper.eq(ReceiptContainerDetail::getReceiptContainerId, headerId)
                    .eq(ReceiptContainerDetail::getReceiptCode, receiptHeader.getCode());
            ReceiptContainerDetail receiptContainerDetail = getOne(detailyWrapper);
            if(receiptContainerDetail == null) {
                save(record);
                mReceiptContainerIds.add(record.getId());
            } else {
                mReceiptContainerIds.add(receiptContainerDetail.getId());
            }
        }
        return mReceiptContainerIds;
    }

    @Override
    public void autoSendMmsByReceiptContainer(ReceiptContainerDetail receiptContainerDetail){
        //调用mms更改采购状态
        Address address = addressService.getAddressByUrl(QuantityConstant.ADDRESS_MMS,receiptContainerDetail.getWarehouseCode());
        try{
            String url=address.getUrl();
            JSONObject json=new JSONObject();
            json.put("code",receiptContainerDetail.getReceiptCode().trim());
            json.put("materialCode",receiptContainerDetail.getMaterialCode().trim());
            json.put("status","2");
            json.put("inQty",receiptContainerDetail.getQty());
            String workNo=receiptDetailService.getWorkNo(receiptContainerDetail.getReceiptDetailId());
            json.put("workNo",workNo);
            String JsonParam = JSON.toJSONString(json);
            String result2 = HttpUtils.bodypost(url, JsonParam,receiptContainerDetail.getWarehouseCode());
            if(result2!=null){
                receiptContainerDetail.setSyncMms(2);
            }else{
                receiptContainerDetail.setSyncMmsNum(receiptContainerDetail.getSyncMmsNum()+1);
            }
            this.updateById(receiptContainerDetail);
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public List<ReceiptContainerDetail> getReceiptContainerDetailByHeaderId(int receiptContainerHeaderId){
        //查询入库组盘明细
        LambdaQueryWrapper<ReceiptContainerDetail> containerDetailLambda = Wrappers.lambdaQuery();
        containerDetailLambda.eq(ReceiptContainerDetail::getReceiptContainerId, receiptContainerHeaderId);
        List<ReceiptContainerDetail> list = this.list(containerDetailLambda);
        return list;
    }
    @Override
    public ReceiptContainerDetail selectFirstEntity(ReceiptContainerDetail domain){
        LambdaQueryWrapper<ReceiptContainerDetail> containerDetailLambda = Wrappers.lambdaQuery();
        containerDetailLambda.eq(StringUtils.isNotNull(domain.getReceiptContainerId()),ReceiptContainerDetail::getReceiptContainerId, domain.getReceiptContainerId());
        containerDetailLambda.eq(StringUtils.isNotNull(domain.getReceiptDetailId()),ReceiptContainerDetail::getReceiptDetailId, domain.getReceiptDetailId());
        containerDetailLambda.last("limit 1");
        return  this.getOne(containerDetailLambda);
    }

    @Override
    public List<ReceiptContainerView> selectOfView(ReceiptContainerView receiptContainerView) {
        return receiptContainerDetailMapper.selectOfView(receiptContainerView);
    }

    @Override
    public AjaxResult getReceiptInfoByBillKS(String code,Integer id) {
        //找到主单的账套和u8仓库
        ReceiptHeader receiptHeader=new ReceiptHeader();
        receiptHeader.setCode(code);
        receiptHeader=receiptHeaderService.selectFirstEntity(receiptHeader);
        if(receiptHeader==null){
            throw new ServiceException("找不到主单");
        }
        //找到子单物料
        ReceiptDetail receiptDetail = receiptDetailService.getOne(new LambdaQueryWrapper<ReceiptDetail>().eq(ReceiptDetail::getId, id).eq(ReceiptDetail::getReceiptCode, code));
        if(receiptDetail==null){
            throw new ServiceException("找不到子单");
        }
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper=Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper.eq(InventoryDetail::getWarehouseCode,receiptHeader.getWarehouseCode());
        inventoryDetailLambdaQueryWrapper.eq((StringUtils.isNotEmpty(receiptHeader.getCompanyCode())),InventoryDetail::getCompanyCode,receiptHeader.getCompanyCode());
        inventoryDetailLambdaQueryWrapper.eq((StringUtils.isNotEmpty(receiptHeader.getUWarehouseCode())),InventoryDetail::getUWarehouseCode,receiptHeader.getUWarehouseCode());
        inventoryDetailLambdaQueryWrapper.eq((StringUtils.isNotEmpty(receiptDetail.getMaterialCode())),InventoryDetail::getMaterialCode,receiptDetail.getMaterialCode());
        List<InventoryDetail> list = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);

        LambdaQueryWrapper<InventoryTransaction> inventoryTransactionLambdaQueryWrapper=Wrappers.lambdaQuery();
        inventoryTransactionLambdaQueryWrapper.eq(InventoryTransaction::getWarehouseCode,receiptHeader.getWarehouseCode());
        inventoryTransactionLambdaQueryWrapper.eq((StringUtils.isNotEmpty(receiptHeader.getCompanyCode())),InventoryTransaction::getCompanyCode,receiptHeader.getCompanyCode());
        inventoryTransactionLambdaQueryWrapper.eq((StringUtils.isNotEmpty(receiptHeader.getUWarehouseCode())),InventoryTransaction::getUWarehouseCode,receiptHeader.getUWarehouseCode());
        inventoryTransactionLambdaQueryWrapper.eq((StringUtils.isNotEmpty(receiptDetail.getMaterialCode())),InventoryTransaction::getMaterialCode,receiptDetail.getMaterialCode());
        List<InventoryTransaction> transactionList = inventoryTransactionService.list(inventoryTransactionLambdaQueryWrapper);

        LambdaQueryWrapper<Container> lambdaQueryWrapper=Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(Container::getWarehouseCode,receiptHeader.getWarehouseCode());
        lambdaQueryWrapper.eq((StringUtils.isNotEmpty(receiptHeader.getCompanyCode())),Container::getCompanyCode,receiptHeader.getCompanyCode());
        lambdaQueryWrapper.eq((StringUtils.isNotEmpty(receiptHeader.getUWarehouseCode())),Container::getUWarehouseCode,receiptHeader.getUWarehouseCode());
        lambdaQueryWrapper.eq(Container::getStatus,QuantityConstant.STATUS_CONTAINER_EMPTY);
        lambdaQueryWrapper.last("and containerType in ('X','D')");
        List<Container> containers = containerService.list(lambdaQueryWrapper);

        Map map=new ModelMap();
        map.put("list",list);
        map.put("transactionList",transactionList);
        map.put("containers",containers);

        return AjaxResult.success(map);
    }
}