ShippingCombinationController.java 15 KB
package com.huaheng.pc.shipment.shippingCombination.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.support.Convert;
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.framework.web.page.TableDataInfo;
import com.huaheng.mobile.shipment.Shipment;
import com.huaheng.pc.config.shipmentPreference.service.ShipmentPreferenceService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerHeader;
import com.huaheng.pc.receipt.receiptContainerHeader.service.ReceiptContainerHeaderService;
import com.huaheng.pc.shipment.shipmentContainerDetail.service.ShipmentContainerDetailService;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentCombinationModel;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentContainerHeader;
import com.huaheng.pc.shipment.shipmentContainerHeader.service.ShipmentContainerHeaderService;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailServiceImpl;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.shipment.shippingCombination.service.ShippingCombinationService;
import com.huaheng.pc.task.taskHeader.domain.ShipmentTaskCreateModel;
import com.huaheng.pc.task.taskHeader.service.ShipmentTaskService;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/shipment/shippingCombination")
public class ShippingCombinationController extends BaseController {

    private String prefix = "shipment/shippingCombination";


    @Resource
    ShipmentDetailServiceImpl shipmentDetailService;
    @Resource
    ShippingCombinationService shippingCombinationService;
    @Resource
    ShipmentContainerHeaderService shipmentContainerHeaderService;
    @Resource
    ShipmentContainerDetailService shipmentContainerDetailService;
    @Resource
    ShipmentHeaderService shipmentHeaderService;
    @Resource
    ShipmentPreferenceService shipmentPreferenceService;
    @Resource
    TaskHeaderService taskHeaderService;
    @Resource
    ShipmentTaskService shipmentTaskService;
    @Resource
    ReceiptContainerHeaderService receiptContainerHeaderService;



    /**
     *  打开出库组盘页面
     * @param shipmentCode
     * @param map
     * @return
     */
    @RequiresPermissions("shipment:shippingCombination:view")
    @GetMapping()
    public String ShippingCombination(String shipmentCode, ModelMap map){
        map.put("code", shipmentCode);
        if(ShiroUtils.getWarehouseCode().equals(QuantityConstant.WAREHOUSE_KS)){
            return prefix + "/shippingCombination-new";
        }
        return prefix+"/shippingCombination";
    }

    /**
     * 获取单据列表
     * @param code
     * @return
     */
    @GetMapping("/listShipmentDetail")
    @ResponseBody
    public AjaxResult listShipmentDetail(String code){
        if(StringUtils.isEmpty(code)){
            return null;
        }
        LambdaQueryWrapper<ShipmentDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ShipmentDetail::getShipmentCode,code)
                .in(ShipmentDetail::getCompanyCode,ShiroUtils.getCompanyCodeList())
                .eq(ShipmentDetail::getWarehouseCode,ShiroUtils.getWarehouseCode());
        List<ShipmentDetail> shipmentDetails =shipmentDetailService.list(lambdaQueryWrapper) ;
        return AjaxResult.success(shipmentDetails);
    }



    /**
     * 获取能出库的库存列表
     * @param code,id
     * @return
     */
    @PostMapping("/getInventory")
    @ResponseBody
    public TableDataInfo getInventory(String code, Integer id){
        //找到主单
        ShipmentHeader shipmentHeader=shipmentHeaderService.getOne(new LambdaQueryWrapper<ShipmentHeader>()
                .eq(ShipmentHeader::getCode,code)
                .eq(ShipmentHeader::getWarehouseCode,ShiroUtils.getWarehouseCode()));

        if(shipmentHeader == null){
            throw new ServiceException("找不到主单");
        }
        //找到子单
        ShipmentDetail shipmentDetail = new ShipmentDetail();
        shipmentDetail = shipmentDetailService.getById(id);
        if(shipmentDetail==null){
            throw new ServiceException("找不到子单");
        }

        List<InventoryDetail> list = shippingCombinationService.getInventorys(shipmentDetail);
        if(list.isEmpty()){
            throw new ServiceException("该物料没有库存或没有符合出库规则的库存");
        }
//        List<InventoryDetail> removeList = new ArrayList<>();
//        for(InventoryDetail item : list){
//            item.setQty(item.getQty().subtract(item.getTaskQty()));
//            //查询容器有无任务,返回任务头状态大于10的任务数
//            Integer number = taskHeaderService.UncompleteCount(item.getContainerCode());
//            if(number >= 1) {
//                removeList.add(item);
//            }
//        }
//        //查询所有小于上架完成的入库组盘头 (status<20)
//        List<ReceiptContainerHeader> unCompleteCombineList = receiptContainerHeaderService.getUnCompleteCombineList();
//        if(unCompleteCombineList != null && unCompleteCombineList.size() > 0) {
//            for(ReceiptContainerHeader receiptContainerHeader : unCompleteCombineList) {
//                String containerCode = receiptContainerHeader.getContainerCode();
//                for(InventoryDetail item : list) {
//                    if(containerCode.equals(item.getContainerCode())) {
//                        if(!removeList.contains(item)) {
//                            removeList.add(item);
//                        }
//                        break;
//                    }
//                }
//            }
//        }
//        list.removeAll(removeList);
        return getDataTable(list);
    }



    /**
     * 保存组盘
     * @param shipmentCombinationModel
     * @return
     */
    @RequiresPermissions("shipment:shippingCombination:combination")
    @PostMapping("/addcombination")
    @ResponseBody
    public  AjaxResult addcombination(ShipmentCombinationModel shipmentCombinationModel){
        AjaxResult ajaxResult = handleMultiProcess(new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                ShipmentContainerHeader shipmentContainerHeader=
                        shipmentContainerHeaderService.combination(shipmentCombinationModel);
                ShipmentTaskCreateModel shipmentTask = new ShipmentTaskCreateModel();
                shipmentTask.setShipmentContainerHeaderIds(shipmentContainerHeader.getId());

                //表头
                List<Integer> idList = Arrays.asList(Convert.toIntArray(shipmentContainerHeader.getId().toString()));
                if(shipmentContainerHeader.getStatus() >= QuantityConstant.TASK_STATUS_RELEASE){
                       return AjaxResult.success("OK!");
                    // return AjaxResult.error("出库货箱编码(" + shipmentContainerHeader.getContainerCode() + ")已经生成任务,请不要重复生成,操作中止");
                }
                shipmentContainerHeaderService.createTask(idList);
                return AjaxResult.success(shipmentContainerHeader.getId());
            }
        });
        return ajaxResult;
    }

    /**
     * 自动组盘并生成任务
     * @param shipmentCode
     * @return
     */
    @RequiresPermissions("shipment:shippingCombination:combination")
    @PostMapping("/autoCombinationAndCreateTask")
    @ResponseBody
    public AjaxResult autoCombinationAndCreateTask(String shipmentCode){
        AjaxResult ajaxResult = handleMultiProcess(new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult = shipmentContainerHeaderService.autoCombination(shipmentCode);
                LambdaQueryWrapper<ShipmentContainerHeader> shipmentContainerHeader=Wrappers.lambdaQuery();
                shipmentContainerHeader.eq(ShipmentContainerHeader::getStatus, QuantityConstant.SHIPMENT_CONTAINER_BUILD);
                List<ShipmentContainerHeader> shipmentContainerHeaders=shipmentContainerHeaderService.list(shipmentContainerHeader);
                for(ShipmentContainerHeader item: shipmentContainerHeaders){
                    ShipmentTaskCreateModel shipmentTask = new ShipmentTaskCreateModel();
                    shipmentTask.setShipmentContainerHeaderIds(item.getId());
                    AjaxResult ajaxResults = shipmentTaskService.createTaskFromShipmentContainers(shipmentTask);
                    if (ajaxResults.hasErr()){
                        throw new ServiceException("任务生成失败");
                    }
                }
                return ajaxResult;
            }
        });
        return ajaxResult;
    }

    /**
     * 自动组盘并生成任务
     * @param shipmentCode
     * @return
     */
    @RequiresPermissions("shipment:shippingCombination:combination")
    @PostMapping("/autoCombination")
    @ResponseBody
    public AjaxResult autoCombination(String shipmentCode) {
        AjaxResult ajaxResult = handleMultiProcess(new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                return shipmentContainerHeaderService.autoCombination(shipmentCode);
            }
        });
        return ajaxResult;
    }


    /** 取消组盘
     *
     * @param shipmentContainerDetailIds
     * @return
     */
    @RequiresPermissions("shipment:shippingCombination:remove")
    @PostMapping("/cancelCombination")
    @ResponseBody
    public AjaxResult cancelCombination(@RequestBody List<Integer> shipmentContainerDetailIds){
        AjaxResult ajaxResult = handleMultiProcess(new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                return shipmentContainerHeaderService.cancelCombinationDetail(shipmentContainerDetailIds);
            }
        });
        return ajaxResult;
    }

    /**
     * 获取出库组盘信息
     */
    @ResponseBody
    @PostMapping("/getShipmentInfoByCode")
    @Log(title = "出库-获取出库组盘信息", operating ="获取出库组盘信息", action = BusinessType.OTHER)
    public AjaxResult getShipmentInfoByCode(String code)  {
        AjaxResult result = shipmentContainerHeaderService.getShipmentInfoByCode(code);
        return result;
    }

    /**
     * 选择出库口
     */
    @GetMapping("/chooseStation/{ids}")
    public String chooseStation(@PathVariable("ids")String ids, ModelMap mmap) {
        List<Integer> idList = Arrays.asList(Convert.toIntArray(ids));
        List<Integer> idsList = idList.stream().distinct().collect(Collectors.toList());
        ShipmentContainerHeader shipmentContainerHeader = shipmentContainerHeaderService.getById(idsList.get(0));
        String containerCode = shipmentContainerHeader.getContainerCode();
        mmap.put("containerCode", containerCode);
        mmap.put("id", ids);
        int taskType = shipmentContainerHeader.getTaskType().intValue();
        if(taskType == QuantityConstant.TASK_TYPE_WHOLESHIPMENT ||
                taskType == QuantityConstant.TASK_TYPE_EMPTYSHIPMENT)  {
            mmap.put("type", QuantityConstant.STATION_OUT);
        } else if(taskType == QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT ||
                taskType == QuantityConstant.TASK_TYPE_SORTINGSHIPMENT ||
                taskType == QuantityConstant.TASK_TYPE_VIEW ||
                taskType == QuantityConstant.TASK_TYPE_CYCLECOUNT) {
            mmap.put("type", QuantityConstant.STATION_PICK);
        } else {
            throw new ServiceException("任务类型不需要选站台");
        }
        return prefix + "/chooseStation";
    }

    @Log(title = "设置站台", operating = "设置站台", action = BusinessType.GRANT)
    @PostMapping("/setStation")
    @ResponseBody
    public AjaxResult setStation (String ids, String type, String containerCode, String port){
        List<Integer> idList = Arrays.asList(Convert.toIntArray(ids));
        List<Integer> idsList = idList.stream().distinct().collect(Collectors.toList());
        for(Integer id : idsList) {
            LambdaQueryWrapper<ShipmentContainerHeader> shipmentContainerHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            shipmentContainerHeaderLambdaQueryWrapper.eq(ShipmentContainerHeader::getId, id);
            ShipmentContainerHeader shipmentContainerHeader1 = shipmentContainerHeaderService.getOne(shipmentContainerHeaderLambdaQueryWrapper);
            shipmentContainerHeader1.setPort(port);
            shipmentContainerHeaderService.update(shipmentContainerHeader1, shipmentContainerHeaderLambdaQueryWrapper);
        }
        return createTask(ids);
    }

    /**
     * 生成任务
     */
    @RequiresPermissions("shipment:container:add")
    @Log(title ="出库-任务生成", operating = "批量生成任务", action = BusinessType.UPDATE)
    @PostMapping( "/createTask")
    @ResponseBody
    public AjaxResult createTask(String ids)
    {
        AjaxResult ajaxResult = new AjaxResult();
        if (StringUtils.isEmpty(ids)) {
            return AjaxResult.error("id不能为空");
        }
        List<Integer> idList = Arrays.asList(Convert.toIntArray(ids));
        for (Integer id : idList) {
            //获取表头
            ShipmentContainerHeader shipmentContainerHeader = shipmentContainerHeaderService.getById(id);
            if(shipmentContainerHeader == null) {
                return AjaxResult.error("出库货箱id:" + id + "未找到,操作中止");
            }
            if(shipmentContainerHeader.getStatus() >= QuantityConstant.TASK_STATUS_RELEASE){
                return AjaxResult.error("出库货箱编码(" + shipmentContainerHeader.getContainerCode() + ")已经生成任务,请不要重复生成,操作中止");
            }
        }
        AjaxResult ajaxResult1 = handleMultiProcess(new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult = shipmentContainerHeaderService.createTask(idList);
                return ajaxResult;
            }
        });
        return ajaxResult1;
    }
}