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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.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.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.mobile.receipt.ReceiptBill;
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.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.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.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;
    /**
     * 根据入库单编码查询入库组盘明细
     * @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.setOpenQty(receiptDetail.getOpenQty().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){
                    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.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;
    }
}