WmsInfoController.java 5.35 KB
package com.huaheng.api.wmsinfo.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.wmsinfo.domain.*;
import com.huaheng.common.constant.Constants;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.utils.DateUtils;
import com.huaheng.pc.config.location.domain.CscLocation;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.CscLocationServiceImpl;
import com.huaheng.pc.config.location.service.LocationServiceImpl;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailServiceImpl;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailServiceImpl;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderServiceImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 数字孪生面板 控制器
 */
@RestController
@RequestMapping("/api/wms/info")
public class WmsInfoController {

    public static final String TIME = "2023-03-28 12:12:00";
    public static final String MATERIAL_NAME = "纸箱";
    public static final String FROM_LOCATION = "L020202";
    public static final String TO_LOCATION = "L010101";

    @Resource
    private LocationServiceImpl locationService;

    @Resource
    private CscLocationServiceImpl cscLocationService;

    @Resource
    private InventoryDetailServiceImpl inventoryDetailService;

    @Resource
    private TaskDetailServiceImpl taskDetailService;

    @Resource
    private TaskHeaderServiceImpl taskHeaderService;

    /**
     * 仓库数据概况
     */
    @GetMapping()
    @ResponseBody
    public WmsInfoResult getInfo() {
        WmsInfoResult result = new WmsInfoResult();
        result.status = 200;
        result.data.add(getLocationInfo());
        result.data.add(getMaterialInfo());
        result.data.add(getTurnOverRateInfo());
        result.data.add(getWarehouseUsageInfo());
        result.data.add(getWarehouseActionInfo());
        return result;
    }

    private DataItem getLocationInfo(){
        DataItem data = new DataItem("库位信息");
        List<LocationInfo> list = new ArrayList<>();

        LambdaQueryWrapper<Location> query = Wrappers.lambdaQuery();
        query.eq(Location::getZoneCode, "L");
        for(Location location: locationService.list(query)) {
            list.add(LocationInfo.parse(location));
        }

        for(CscLocation location: cscLocationService.list()) {
            list.add(LocationInfo.parse(location));
        }
        data.Json = JSON.toJSONString(list);
        return data;
    }

    private DataItem getMaterialInfo(){
        DataItem data = new DataItem("物资信息");
        List<MaterialInfo> list = new ArrayList<>();

        for(InventoryDetail inventoryDetail: inventoryDetailService.list()) {
            list.add(MaterialInfo.parse(inventoryDetail));
        }
        data.Json = JSON.toJSONString(list);;
        return data;
    }

    private DataItem getWarehouseUsageInfo(){
        DataItem data = new DataItem("库容使用率");
        WarehouseUsageInfo body = new WarehouseUsageInfo();
        body.addUsage(cscLocationService.getUsedLocationCount(),
                cscLocationService.getTotalLocationCount(), "CSC", "穿梭车库");
        body.addUsage(locationService.getUsedLocationCount("L"),
                locationService.getTotalLocationCount("L"), "LK", "立体库");
        data.Json = JSON.toJSONString(body);
        return data;
    }

    private DataItem getTurnOverRateInfo(){
        DataItem data = new DataItem("物资周转退库率");
        TurnOverRateInfo body = new TurnOverRateInfo();
        body.setBackoutRate(1.0f, TIME);
        body.setTurnOverRate(1.0f, TIME);
        data.Json = JSON.toJSONString(body);
        return data;
    }

    private DataItem getWarehouseActionInfo(){
        DataItem data = new DataItem("仓库变化信息");
        WarehouseActionInfo body = new WarehouseActionInfo();

        //找出10分钟前更新的任务或者未完成的任务
        LambdaQueryWrapper<TaskHeader> headerQuery = Wrappers.lambdaQuery();
        headerQuery.lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED)
                .last(" or lastUpdated > '" + DateUtils.getDateTimeOf(10) + "'");

        //根据id查询明细
        List<Integer> list = taskHeaderService.list(headerQuery).stream().map(TaskHeader::getId).collect(Collectors.toList());
        LambdaQueryWrapper<TaskDetail> detailQuery = Wrappers.lambdaQuery();
        detailQuery.in(TaskDetail::getTaskId, list);

        for(TaskDetail detail: taskDetailService.list(detailQuery)){
            body.add(detail);
        }
        data.Json = JSON.toJSONString(body);
        return data;
    }
}