ReceivingController.java 13.1 KB
package com.huaheng.pc.receipt.receiving.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.aspectj.lang.annotation.Log;
import com.huaheng.framework.aspectj.lang.constant.BusinessType;
import com.huaheng.framework.web.controller.BaseController;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.container.domain.Container;
import com.huaheng.pc.config.container.service.ContainerService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.monitor.job.task.RyTask;
import com.huaheng.pc.receipt.receiptContainerDetail.service.ReceiptContainerDetailService;
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.receipt.receiving.domain.ReceivingDamain;
import com.huaheng.pc.receipt.receiving.service.ReceivingService;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentContainerHeader;
import com.huaheng.pc.shipment.shipmentContainerHeader.service.ShipmentContainerHeaderService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.ReceiptTaskService;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author mahua
 * @ClassName ReceivingController
 * @projectName huaheng
 * @description: 入库组盘
 * @date 2019/8/31 15:51
 */
@Controller
@RequestMapping("/receipt/receiving")
public class ReceivingController extends BaseController {

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

    private String prefix = "receipt/receiving";

    @Resource
    private ReceivingService receivingService;
    @Resource
    private ReceiptContainerDetailService receiptContainerDetailService;
    @Resource
    private ReceiptContainerHeaderService receiptContainerHeaderService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ContainerService containerService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private ShipmentContainerHeaderService shipmentContainerHeaderService;
    @Resource
    private ReceiptTaskService receiptTaskService;

    @RequiresPermissions("receipt:receiving:view")
    @GetMapping()
    public String receiving() {
        if(ShiroUtils.getWarehouseCode().equals(QuantityConstant.WAREHOUSE_KS)){
            return prefix + "/receiving-new";
        }
        return prefix + "/receiving";
    }

    /**
     * 获取订单明细
     */
    @ResponseBody
    @PostMapping("/scanBill")
    @RequiresPermissions("receipt:receiving:receiving")
    @Log(title = "入库-获取订单明细", operating ="PC端扫描收货单号", action = BusinessType.OTHER)
    public AjaxResult scanBill(String code)  {
        AjaxResult result = receivingService.scanReceiptCode(code);
        return result;
    }

    /**
     * 获取收货信息
     */
    @ResponseBody
    @PostMapping("/getReceiptInfoByBill")
    @RequiresPermissions("receipt:receiving:receiving")
    @Log(title = "入库-获取收货信息", operating ="获取收货信息", action = BusinessType.OTHER)
    public AjaxResult getReceiptInfoByBill(String code)  {
        AjaxResult result = receiptContainerDetailService.getReceiptInfoByBill(code);
        return result;
    }

    /**
     * 获取收货信息
     */
    @ResponseBody
    @PostMapping("/getReceiptInfoByBillKS")
    @RequiresPermissions("receipt:receiving:receiving")
    @Log(title = "入库-获取收货信息", operating ="获取收货信息", action = BusinessType.OTHER)
    public AjaxResult getReceiptInfoByBillKS(String code,Integer id)  {
        AjaxResult result = receiptContainerDetailService.getReceiptInfoByBillKS(code,id);
        return result;
    }


    @GetMapping("/add")
    public String add(Long detailId, String containerCode, ModelMap modelMap) {
        modelMap.put("containerCode", containerCode);
        modelMap.put("receiptDetails", receiptDetailService.getById(detailId));
        return prefix + "/add";
    }

    /**
     * 保存收货
     */
    @ApiOperation(value="保存收货 ", notes="保存收货 ", httpMethod = "POST")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("receipt:receiving:receiving")
    @Log(title = "入库-保存收货", operating ="PC端保存收货", action = BusinessType.OTHER)
    public AjaxResult save(ReceiptContainerView receivingDamain){
        if(receivingDamain.getQty().compareTo(new BigDecimal(0)) <=0) {
            return AjaxResult.error("配盘数量必须大于0");
        }

        AjaxResult ajaxResult = handleMultiProcess(new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult = receiptContainerHeaderService.saveContainer(receivingDamain);
                if(ajaxResult!=null&&!ajaxResult.hasErr()){
                    //创建任务
                    LambdaQueryWrapper<ReceiptContainerHeader> lambdaQueryWrapper= Wrappers.lambdaQuery();
                    lambdaQueryWrapper.eq(ReceiptContainerHeader::getContainerCode,receivingDamain.getContainerCode());
                    lambdaQueryWrapper.le(ReceiptContainerHeader::getStatus,10);
                    List<ReceiptContainerHeader> receiptContainerHeaders=receiptContainerHeaderService.list(lambdaQueryWrapper);
                    for(ReceiptContainerHeader item:receiptContainerHeaders) {
                        String ids = item.getId().toString();
                        Integer[] idArray = Convert.toIntArray(ids);
                        List<Integer> islist= Arrays.asList(idArray);
                        try {
                            AjaxResult ajaxResult1=receiptTaskService.createReceiptTask(islist);
                        } catch (Exception e) {
                            e.printStackTrace();
                            logger.error("receipt/receiving/save:idArray="+idArray+"msg:"+e.getMessage());
                        }
                    }
                }
                return ajaxResult;
            }
        });
        return ajaxResult;
    }


    /**
     * 获取库存信息
     */
    @ResponseBody
    @PostMapping("/getInventoryInfo")
    @RequiresPermissions("receipt:receiving:add")
    @Log(title = "入库-入库单", operating ="获取库存信息", action = BusinessType.OTHER)
    public AjaxResult getInventoryInfo(String code,Integer id) throws Exception {
        //找到主单的账套和仓库
        LambdaQueryWrapper<ReceiptHeader> lambdaQueryWrapper= Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ReceiptHeader::getCode,code)
        .eq(ReceiptHeader::getWarehouseCode, ShiroUtils.getWarehouseCode());
        ReceiptHeader receiptHeader=receiptHeaderService.getOne(lambdaQueryWrapper);
        if(receiptHeader == null) {
            throw new ServiceException("找不到主单");
        }

        //找到子单物料
        LambdaQueryWrapper<ReceiptDetail> lambdaQuery=Wrappers.lambdaQuery();
        lambdaQuery.eq(ReceiptDetail::getReceiptCode,code)
                .eq(ReceiptDetail::getWarehouseCode, ShiroUtils.getWarehouseCode())
        .eq(ReceiptDetail::getId,id);
        ReceiptDetail receiptDetail=receiptDetailService.getOne(lambdaQuery);
        if(receiptDetail == null) {
            throw new ServiceException("找不到子单");
        }

        //排除有任务的托盘
        List<TaskHeader> unCompleteTaskList = taskHeaderService.getUnCompleteTaskList();
        List<ShipmentContainerHeader> shipmentContainerHeaderList =
                shipmentContainerHeaderService.getUnCompleteCombineList();
        List<String> containerList = new ArrayList<>();
        if(unCompleteTaskList != null && unCompleteTaskList.size() > 0) {
            for(TaskHeader taskHeader : unCompleteTaskList) {
                String containerCode = taskHeader.getContainerCode();
                containerList.add(containerCode);
            }
        }

        //排除有出库组盘的托盘
        if(shipmentContainerHeaderList != null && shipmentContainerHeaderList.size() > 0) {
            for(ShipmentContainerHeader shipmentContainerHeader : shipmentContainerHeaderList) {
                String containerCode = shipmentContainerHeader.getContainerCode();
                if(!containerList.contains(containerCode)) {
                    containerList.add(containerCode);
                }
            }
        }

        //本物料库存
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambda = Wrappers.lambdaQuery();
        inventoryDetailLambda.eq(InventoryDetail::getWarehouseCode,ShiroUtils.getWarehouseCode())
                .eq(InventoryDetail::getCompanyCode,receiptHeader.getCompanyCode())
                .eq(InventoryDetail::getMaterialCode,receiptDetail.getMaterialCode())
                .eq((StringUtils.isNotEmpty(receiptDetail.getProjectNo())),InventoryDetail::getProjectNo,
                    receiptDetail.getProjectNo());
        List<InventoryDetail> inventoryList = inventoryDetailService.list(inventoryDetailLambda);
        List<InventoryDetail> removeInventoryList = new ArrayList<>();

        //空容器
        LambdaQueryWrapper<Container> container=Wrappers.lambdaQuery();
        container.eq(Container::getWarehouseCode,ShiroUtils.getWarehouseCode())
                .eq(Container::getCompanyCode,receiptHeader.getCompanyCode())
                .eq(Container::getStatus, QuantityConstant.STATUS_CONTAINER_EMPTY);
        List<Container> emptyContainerList=containerService.list(container);
        List<Container> removeEmptyContainerList = new ArrayList<>();

        for(String containerCode : containerList) {
            if(inventoryList != null && inventoryList.size() > 0) {
                for (InventoryDetail inventoryDetail : inventoryList) {
                    if (inventoryDetail.getContainerCode().equals(containerCode)) {
                        removeInventoryList.add(inventoryDetail);
                    }
                }
            }
            if(emptyContainerList != null && emptyContainerList.size() > 0) {
                for (Container container1 : emptyContainerList) {
                    if (container1.getCode().equals(containerCode)) {
                        removeEmptyContainerList.add(container1);
                    }
                }
            }
        }
        inventoryList.removeAll(removeInventoryList);
        emptyContainerList.removeAll(removeEmptyContainerList);

        Map map=new ModelMap();
        map.put("inventoryList",inventoryList);
        map.put("containerList",emptyContainerList);
        return AjaxResult.success(map);
    }

    /**
     * 批量收货
     */
    @ResponseBody
    @PostMapping("/saveAll")
    @RequiresPermissions("receipt:receiving:add")
    @Log(title = "入库-入库单", operating ="PC端保存收货", action = BusinessType.OTHER)
    public synchronized AjaxResult saveAll(String ids,String receiptContainerCode) throws Exception {
        final List<String> list = Arrays.asList(ids.split(","));
        for(String i:list){
            final ReceiptDetail receiptDetail = receiptDetailService.getById(Integer.valueOf(i));
            ReceiptContainerView receiptContainerView=new ReceiptContainerView();
            receiptContainerView.setReceiptCode(receiptDetail.getReceiptCode());
            receiptContainerView.setQty(receiptDetail.getQty());
            receiptContainerView.setContainerCode(receiptContainerCode);
            receiptContainerView.setReceiptDetailId(Integer.valueOf(i));
            save(receiptContainerView);
        }
        return AjaxResult.success();

    }
}