MobileTaskForASRSController.java 11.5 KB
package com.huaheng.mobile.shipment;

import com.alibaba.fastjson.JSONException;
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.domain.AjaxResult;
import com.huaheng.mobile.receipt.ReceiptBill;
import com.huaheng.pc.general.location.domain.Location;
import com.huaheng.pc.general.material.domain.Material;
import com.huaheng.pc.general.material.service.IMaterialService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentAutoCombinationModel;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentCombinationModel;
import com.huaheng.pc.shipment.shipmentContainerHeader.service.IShipmentContainerHeaderService;
import com.huaheng.pc.shipment.shipmentDetail.service.IShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.IShipmentHeaderService;
import com.huaheng.pc.system.dict.domain.DictData;
import com.huaheng.pc.system.dict.service.IDictDataService;
import com.huaheng.pc.task.task.domain.ShipmentTaskCreateModel;
import com.huaheng.pc.task.task.domain.Task;
import com.huaheng.pc.task.task.service.ITaskService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.domain.TaskWithDetailsModel;
import com.huaheng.pc.task.taskDetail.service.ITaskDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/mobile/task/taskForASRS")
@Api(tags = {"MobileTaskForASRSController"}, description = "手机立体库任务拣货相关")
public class MobileTaskForASRSController {

    @Resource
    private ITaskService taskService;
    @Resource
    private ITaskDetailService taskDetailService;
    @Resource
    private IMaterialService materialService;
    @Resource
    IDictDataService  dictDataService;
    @Resource
    private IShipmentHeaderService shipmentHeaderService;
    @Resource
    private IShipmentDetailService shipmentDetailService;
    @Resource
    private IShipmentContainerHeaderService shipmentContainerHeaderService;

    /**
     * 根据任务号或者托盘号,获取任务明细
     *
     * @return
     */
    @GetMapping("/taskAndDetails")
    @ApiOperation("移动端扫描容器号(托盘)或者任务号")
    @Log(title = "手机扫描容器收货上架", action = BusinessType.OTHER)
    public AjaxResult getAllTaskDetails(@Param("code") @ApiParam(value = "容器号或任务号") String code)  {
        //先检查是不是任务号
        int taskId;
        TaskWithDetailsModel taskWithDetailsModel = new TaskWithDetailsModel();
        try {
            taskId = Integer.parseInt(code);
        } catch (Throwable throwable) {
            taskId = -1;
        }
        if (taskId != -1) {
            //说明可以顺利转成int型,查找对应任务
            Task task = taskService.selectEntityById(taskId);
            if (task != null) {
                //就算是已完成的任务,也可以查看
//                if(task.getStatus()>=40){
//                    return RetResponse.makeErrRsp("任务已完成");
//                }
                if(task.getType() != 300 && task.getType() != 400) {
                    return AjaxResult.error("不是出库任务");
                }
                //获取明细
                TaskDetail condition = new TaskDetail();
                condition.setTaskId(task.getId());
                List<TaskDetail> taskDetails = taskDetailService.selectListEntityByEqual(condition);
                if (taskDetails == null || taskDetails.size() == 0) {
                    return AjaxResult.error("任务没有明细");
                }
                if (taskDetails.stream().anyMatch(x -> x.getStatus() < 40)) {
                    taskWithDetailsModel.setTask(task);
                    taskWithDetailsModel.setTaskDetails(taskDetails);
                } else {
                    return AjaxResult.error("任务已完成");
                }
            }
        }
        if (taskWithDetailsModel.getTask() == null) {
            //说明按照任务号并没有查找到,则再按照容器号来查找
            Task task = null;
            Task condition = new Task();
            condition.setContainerCode(code);
            condition.setWarehouseId(ShiroUtils.getWarehouseId());
            List<Task> tasks = taskService.selectListEntityByEqual(condition);
            if (tasks == null || tasks.size() == 0) {
                return AjaxResult.error("任务未找到");
            }
            for (Task item : tasks) {
                if (item.getFirstStatus()> 20 && item.getFirstStatus()< 40) {
                    task = item;
                    break;
                }
            }
            if (task != null) {
                //查找明细
                TaskDetail condition2 = new TaskDetail();
                condition2.setTaskId(task.getId());
                List<TaskDetail> taskDetails = taskDetailService.selectListEntityByEqual(condition2);
                if (taskDetails == null || taskDetails.size() == 0) {
                    return AjaxResult.error("任务没有明细");
                }
                if(task.getType() != 300 && task.getType() != 400) {
                    return AjaxResult.error("不是出库任务");
                }
                taskWithDetailsModel.setTask(task);
                taskWithDetailsModel.setTaskDetails(taskDetails);
            } else {
                task = tasks.stream().min((a, b) -> a.getCreated().compareTo(b.getCreated()) > 0 ? -1 : 1).get();
                //检查子任务是否都完成
                TaskDetail condition2 = new TaskDetail();
                condition2.setTaskId(task.getId());
                List<TaskDetail> taskDetails = taskDetailService.selectListEntityByEqual(condition2);
                if (taskDetails == null || taskDetails.size() == 0) {
                    return AjaxResult.error("任务没有明细");
                }
                for(TaskDetail taskDetail : taskDetails) {
                    Material materialCondition = new Material();
                    materialCondition.setCode(taskDetail.getMaterialCode());
                    Material material = materialService.selectFirstEntity(materialCondition);
                    taskDetail.setMaterialName(material.getName());
                }
                if(task.getType() != 300 && task.getType() != 400) {
                    return AjaxResult.error("不是出库任务");
                }
                if (taskDetails.stream().anyMatch(x -> x.getStatus() < 40)) {
                    taskWithDetailsModel.setTask(task);
                    taskWithDetailsModel.setTaskDetails(taskDetails);
                } else {
                    return AjaxResult.error("任务已完成");
                }
            }
        }
        //添加config
        List<DictData> result = dictDataService.selectDictDataByType("taskStatus");
        if (result.size() < 1) {
            taskWithDetailsModel.setTaskStatusConfig(null);
        } else {
            taskWithDetailsModel.setTaskStatusConfig(result);
        }
        result = dictDataService.selectDictDataByType("taskDetailStatus");
        if (result.size() < 1) {
            taskWithDetailsModel.setTaskDetailStatusConfig(null);
        } else {
            taskWithDetailsModel.setTaskDetailStatusConfig(result);
        }
        return AjaxResult.success(taskWithDetailsModel);
    }

    @PostMapping("/completeTaskDetails")
    @ApiOperation("完成立体库子任务")
    @Log(title = "完成立体库子任务", action = BusinessType.OTHER)
    public AjaxResult completeTaskDetails(@RequestBody int[] taskDetailIds) {
        return taskService.completeTaskDetailsForMobile(taskDetailIds);
    }


    @PostMapping("/autoShipment")
    @ApiOperation("生成立体库出库任务")
    @Log(title = "生成立体库出库任务", action = BusinessType.OTHER)
    public AjaxResult autoShipment(@RequestBody @ApiParam(value="收货单") List<ShipmentBill> shipmentBills) {
        int shipmentHeaderId = shipmentHeaderService.createTodayShipmentHeader(shipmentBills.get(0).getCompanyId(), shipmentBills.get(0).getCompanyCode());
        ShipmentHeader shipmentHeader = shipmentHeaderService.selectEntityById(shipmentHeaderId);
        List<Integer> shipmentDetailIds = shipmentDetailService.insertTodayShipmentDetail(shipmentHeader, shipmentBills,
                shipmentBills.get(0).getCompanyId(), shipmentBills.get(0).getCompanyCode());
        List<ShipmentAutoCombinationModel> shipmentAutoCombinationModels = new ArrayList<>();
        for(int i=0; i< shipmentDetailIds.size(); i++) {
            int shipmentDetailId = shipmentDetailIds.get(i);
            ShipmentAutoCombinationModel shipmentAutoCombinationModel = new ShipmentAutoCombinationModel();
            shipmentAutoCombinationModel.setShipmentDetailId(shipmentDetailId);
            shipmentAutoCombinationModel.setType((short)100);
            shipmentAutoCombinationModel.setCompanyId(shipmentBills.get(0).getCompanyId());
            shipmentAutoCombinationModel.setCompanyCode(shipmentBills.get(0).getCompanyCode());
            shipmentAutoCombinationModel.setLocation(shipmentBills.get(i).getLocation());
            shipmentAutoCombinationModels.add(shipmentAutoCombinationModel);
        }
        return shipmentContainerHeaderService.autoCombination(shipmentAutoCombinationModels);
    }

    @PostMapping("/getMaterialForecast")
    @ApiOperation("获得物料联想词")
    @Log(title = "获得物料联想词", action = BusinessType.OTHER)
    public AjaxResult getMaterialForecast(@RequestBody @ApiParam(value="物料号") Map<String, String> param) {
        if  (param.get("code") == null || param.get("code").trim().length() < 1)
            throw new JSONException("容器号(code)不能为空");
        List<Materialforecast> forecasts = materialService.getMaterialForecast(param.get("code"));
        return AjaxResult.success(forecasts);
    }

    @PostMapping("/createShipTask")
    @ApiOperation("移动端生成出库任务")
    @Log(title = "移动端扫描出库单", action = BusinessType.OTHER)
    public AjaxResult createShipTask(@RequestBody @ApiParam(value = "出库任务") ShipmentTaskModel shipmentTaskModel)  {
        int[] shipmentContainerHeaderIds = shipmentTaskModel.getShipmentContainerHeaderIds();
        AjaxResult result = null;
        List<Integer> taskIds = new ArrayList<>();
        for(int shipmentContainerHeaderId : shipmentContainerHeaderIds) {
            ShipmentTaskCreateModel shipmentTaskCreateModel = new ShipmentTaskCreateModel();
            shipmentTaskCreateModel.setShipmentContainerHeaderIds(shipmentContainerHeaderId);
            shipmentTaskCreateModel.setTaskType(shipmentTaskModel.getTaskType());
            shipmentTaskCreateModel.setPriority(shipmentTaskModel.getPriority());
            result = taskService.createTaskFromShipmentContainers(shipmentTaskCreateModel);
            if(result.hasErr()) {
                return result;
            } else {
                int taskId = (int)result.getData();
                taskIds.add(taskId);
            }
        }
        return AjaxResult.success(taskIds);
    }
}