TvCSService.java 19.5 KB
package com.huaheng.api.tv.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.tv.domain.*;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.redis.RedisUtils;
import com.huaheng.common.utils.DateUtils;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.domain.AjaxResultTv;
import com.huaheng.framework.web.service.DictService;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.materialType.domain.MaterialType;
import com.huaheng.pc.config.materialType.service.MaterialTypeService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.inventory.inventoryHeader.domain.InventoryHeader;
import com.huaheng.pc.inventory.inventoryHeader.service.InventoryHeaderService;
import com.huaheng.pc.inventory.inventoryTransaction.domain.InventoryTransaction;
import com.huaheng.pc.inventory.inventoryTransaction.domain.InventoryTransactionReport;
import com.huaheng.pc.inventory.inventoryTransaction.service.InventoryTransactionService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.report.excelReport.mapper.ExcelReportMapper;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class TvCSService {

    @Autowired
    protected RedisTemplate<String, Object> redisTemplate;

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


    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private TaskDetailService taskDetailService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ShipmentHeaderService shipmentHeaderService;
    @Resource
    private InventoryTransactionService inventoryTransactionService;

    @Resource
    private LocationService locationService;
    @Resource
    ExcelReportMapper mapper;


    public AjaxResult csTopParam(String warehouseCode){
        HashMap<String, Object> resultMap = new HashMap<>();
        //库存预警数
//        LambdaQueryWrapper<InventoryDetail> wrapper=new LambdaQueryWrapper<>();
//        wrapper.eq(InventoryDetail::getWarehouseCode,warehouseCode);
//        wrapper.groupBy(InventoryDetail::getMaterialCode);
        List<Map<String, Object>> list=inventoryDetailService.csMaterialWarningMax(warehouseCode);
        //库存预警-最大
        resultMap.put("warningMax",list.size());
        List<Map<String, Object>> list1=inventoryDetailService.csMaterialWarningMin(warehouseCode);
        //库存预警-最小
        resultMap.put("warningMin",list1.size());

        //近7天任务数
        String endTime= DateUtils.getNowPreDays("yyyy-MM-dd", 7);
        LambdaQueryWrapper<TaskHeader> taskWrapper=new LambdaQueryWrapper<>();
        taskWrapper.eq(TaskHeader::getWarehouseCode,warehouseCode);
        taskWrapper.in(TaskHeader::getInternalTaskType,QuantityConstant.TASK_INTENERTYPE_RECEIPT,QuantityConstant.TASK_INTENERTYPE_SHIPMENT);
        taskWrapper.ge(TaskHeader::getCreated,endTime);
        int taskCount=taskHeaderService.count(taskWrapper);
        resultMap.put("taskWeek",taskCount);
        return AjaxResult.success(resultMap);
    }

    /**
     * 近一周单据数量
     * 当天入库量 当天未入量 当月入库量 当月未入量
     * @param warehouseCode
     * @return
     */
    public AjaxResult csReceipt(String warehouseCode){
//        ArrayList<Object> result = new ArrayList<>();
        HashMap<String, Object> resultMap2 = new HashMap<>();
        String endTime1= DateUtils.getNowPreDays("yyyy-MM-dd", 6);
        String endTime= DateUtils.getNowPreDays("yyyy-MM-dd", 0);
        // 获取当月第一天
        LocalDate today = LocalDate.now();
        LocalDate firstDayOfMonth = today.withDayOfMonth(1);
        // 格式化输出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = firstDayOfMonth.format(formatter);

        LambdaQueryWrapper<ReceiptHeader> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ReceiptHeader::getWarehouseCode,warehouseCode);
        wrapper.ge(ReceiptHeader::getCreated,endTime);
        wrapper.eq(ReceiptHeader::getLastStatus,QuantityConstant.RECEIPT_HEADER_POSTING);
        int dayHaveComplete=receiptHeaderService.count(wrapper);
        LambdaQueryWrapper<ReceiptHeader> wrapper2=new LambdaQueryWrapper<>();
        wrapper2.eq(ReceiptHeader::getWarehouseCode,warehouseCode);
        wrapper2.ge(ReceiptHeader::getCreated,endTime);
        wrapper2.lt(ReceiptHeader::getLastStatus,QuantityConstant.RECEIPT_HEADER_POSTING);
        int dayNoComplete=receiptHeaderService.count(wrapper2);

        LambdaQueryWrapper<ReceiptHeader> wrapper3=new LambdaQueryWrapper<>();
        wrapper3.eq(ReceiptHeader::getWarehouseCode,warehouseCode);
        wrapper3.ge(ReceiptHeader::getCreated,formattedDate);
        wrapper3.lt(ReceiptHeader::getLastStatus,QuantityConstant.RECEIPT_HEADER_POSTING);
        int monthHaveComplete=receiptHeaderService.count(wrapper3);
        LambdaQueryWrapper<ReceiptHeader> wrapper4=new LambdaQueryWrapper<>();
        wrapper4.eq(ReceiptHeader::getWarehouseCode,warehouseCode);
        wrapper4.ge(ReceiptHeader::getCreated,formattedDate);
        wrapper4.lt(ReceiptHeader::getLastStatus,QuantityConstant.RECEIPT_HEADER_POSTING);
        int monthNoComplete=receiptHeaderService.count(wrapper4);

        List<Map<String, Object>> receiptHeaderList=receiptHeaderService.csReceipt(endTime1,warehouseCode);

        List<LocalDate> datelist=DateUtils.getDayList(endTime1,endTime);
        //查找receiptHeaderList中每天receiptType数量最多的前3种,并计算数量
        List<Map<String, Object>> list=new ArrayList<>();
        for (int a=0;a<datelist.size();a++){
            String days=datelist.get(a).toString();
            List<Map<String, Object>> shipmentList1 = receiptHeaderList.stream().filter(maps -> maps.get("days").equals(days)).collect(Collectors.toList());
            if(shipmentList1==null||shipmentList1.size()==0){
                shipmentList1=new ArrayList<>();
            }
            if(shipmentList1.size()>3){
                shipmentList1=shipmentList1.subList(0,3);
            }
            list.addAll(shipmentList1);
        }
//        result.add(resultMap);
        resultMap2.put("dayHaveComplete",dayHaveComplete);
        resultMap2.put("dayNoComplete",dayNoComplete);
        resultMap2.put("monthHaveComplete",monthHaveComplete);
        resultMap2.put("monthNoComplete",monthNoComplete);
        resultMap2.put("weekdaysList",list);
        return AjaxResult.success(resultMap2);
    }
    /**
     * 近一周单据数量
     * 当天出库量 当天未出量 当月出库量 当月未出量
     * @param warehouseCode
     * @return
     */
    public AjaxResult csShipment(String warehouseCode){
        ArrayList<Object> result = new ArrayList<>();
//        HashMap<String, Object> resultMap = new HashMap<>();
        HashMap<String, Object> resultMap2 = new HashMap<>();
        String endTime1= DateUtils.getNowPreDays("yyyy-MM-dd", 6);
        String endTime= DateUtils.getNowPreDays("yyyy-MM-dd", 0);
        // 获取当月第一天
        LocalDate today = LocalDate.now();
        LocalDate firstDayOfMonth = today.withDayOfMonth(1);
        // 格式化输出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = firstDayOfMonth.format(formatter);

        LambdaQueryWrapper<ShipmentHeader> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ShipmentHeader::getWarehouseCode,warehouseCode);
        wrapper.ge(ShipmentHeader::getCreated,endTime);
        wrapper.eq(ShipmentHeader::getLastStatus,QuantityConstant.SHIPMENT_HEADER_COMPLETED);
        int dayHaveComplete=shipmentHeaderService.count(wrapper);
        LambdaQueryWrapper<ShipmentHeader> wrapper2=new LambdaQueryWrapper<>();
        wrapper2.eq(ShipmentHeader::getWarehouseCode,warehouseCode);
        wrapper2.ge(ShipmentHeader::getCreated,endTime);
        wrapper2.lt(ShipmentHeader::getLastStatus,QuantityConstant.SHIPMENT_HEADER_COMPLETED);
        int dayNoComplete=shipmentHeaderService.count(wrapper2);

        LambdaQueryWrapper<ShipmentHeader> wrapper3=new LambdaQueryWrapper<>();
        wrapper3.eq(ShipmentHeader::getWarehouseCode,warehouseCode);
        wrapper3.ge(ShipmentHeader::getCreated,formattedDate);
        wrapper3.lt(ShipmentHeader::getLastStatus,QuantityConstant.SHIPMENT_HEADER_COMPLETED);
        int monthHaveComplete=shipmentHeaderService.count(wrapper3);
        LambdaQueryWrapper<ShipmentHeader> wrapper4=new LambdaQueryWrapper<>();
        wrapper4.eq(ShipmentHeader::getWarehouseCode,warehouseCode);
        wrapper4.ge(ShipmentHeader::getCreated,formattedDate);
        wrapper4.lt(ShipmentHeader::getLastStatus,QuantityConstant.SHIPMENT_HEADER_COMPLETED);
        int monthNoComplete=shipmentHeaderService.count(wrapper4);

        List<Map<String, Object>> receiptHeaderList=shipmentHeaderService.csShipment(endTime1,warehouseCode);
        List<LocalDate> datelist=DateUtils.getDayList(endTime1,endTime);
        //查找receiptHeaderList中每天receiptType数量最多的前3种,并计算数量
        List<Map<String, Object>> list=new ArrayList<>();
        for (int a=0;a<datelist.size();a++){
            String days=datelist.get(a).toString();
            List<Map<String, Object>> shipmentList1 = receiptHeaderList.stream().filter(maps -> maps.get("days").equals(days)).collect(Collectors.toList());
            if(shipmentList1==null||shipmentList1.size()==0){
                shipmentList1=new ArrayList<>();
                continue;
            }
            if(shipmentList1.size()>3){
                shipmentList1=shipmentList1.subList(0,3);
            }
            list.addAll(shipmentList1);
        }
        resultMap2.put("dayHaveComplete",dayHaveComplete);
        resultMap2.put("dayNoComplete",dayNoComplete);
        resultMap2.put("monthHaveComplete",monthHaveComplete);
        resultMap2.put("monthNoComplete",monthNoComplete);
        resultMap2.put("weekdaysList",list);
        return AjaxResult.success(resultMap2);
    }
    public AjaxResult csReceiptTask(String warehouseCode){
        HashMap<String, Object> resultMap = new HashMap<>();
        String endTime1= DateUtils.getNowPreDays("yyyy-MM-dd", 0);
        LambdaQueryWrapper<TaskHeader> taskWrapper=new LambdaQueryWrapper<>();
        taskWrapper.eq(TaskHeader::getWarehouseCode,warehouseCode);
        taskWrapper.in(TaskHeader::getInternalTaskType,QuantityConstant.TASK_INTENERTYPE_RECEIPT,QuantityConstant.TASK_INTENERTYPE_SHIPMENT);
        taskWrapper.lt(TaskHeader::getStatus,QuantityConstant.TASK_STATUS_COMPLETED);
        taskWrapper.ge(TaskHeader::getCreated,endTime1);
        taskWrapper.select(TaskHeader::getContainerCode,TaskHeader::getToLocation,TaskHeader::getPort,TaskHeader::getCreated,TaskHeader::getInternalTaskType);
        List<TaskHeader> taskHeaderList=taskHeaderService.list(taskWrapper);
        if(taskHeaderList.size()>0){
            resultMap.put("taskCount",taskHeaderList);
            resultMap.put("taskHistoryCount",new ArrayList<>());
            return AjaxResult.success(resultMap);
        }
        LambdaQueryWrapper<TaskHeader> taskHisWrapper=new LambdaQueryWrapper<>();
        taskHisWrapper.eq(TaskHeader::getWarehouseCode,warehouseCode);
        taskHisWrapper.in(TaskHeader::getInternalTaskType,QuantityConstant.TASK_INTENERTYPE_RECEIPT,QuantityConstant.TASK_INTENERTYPE_SHIPMENT);
        taskHisWrapper.eq(TaskHeader::getStatus,QuantityConstant.TASK_STATUS_COMPLETED);
        taskHisWrapper.last("limit 10");
        taskHisWrapper.orderByDesc(TaskHeader::getCreated);
        taskHisWrapper.select(TaskHeader::getContainerCode,TaskHeader::getToLocation,TaskHeader::getPort,TaskHeader::getCreated);
        List<TaskHeader> taskHeaderHis=taskHeaderService.list(taskHisWrapper);
        resultMap.put("taskCount",new ArrayList<>());
        resultMap.put("taskHistoryCount",taskHeaderHis);
//        result.add(resultMap);
        return AjaxResult.success(resultMap);
    }
    public AjaxResult csShipmentTask(String warehouseCode){
        HashMap<String, Object> resultMap = new HashMap<>();
        String endTime1= DateUtils.getNowPreDays("yyyy-MM-dd", 0);
        LambdaQueryWrapper<TaskHeader> taskWrapper=new LambdaQueryWrapper<>();
        taskWrapper.eq(TaskHeader::getWarehouseCode,warehouseCode);
        taskWrapper.eq(TaskHeader::getInternalTaskType,QuantityConstant.TASK_INTENERTYPE_SHIPMENT);
        taskWrapper.lt(TaskHeader::getStatus,QuantityConstant.TASK_STATUS_COMPLETED);
        taskWrapper.ge(TaskHeader::getCreated,endTime1);
        taskWrapper.select(TaskHeader::getContainerCode,TaskHeader::getToLocation,TaskHeader::getPort,TaskHeader::getCreated);
        List<TaskHeader> taskHeaderList=taskHeaderService.list(taskWrapper);
        if(taskHeaderList.size()>0){
            resultMap.put("taskCount",taskHeaderList);
            resultMap.put("taskHistoryCount",new ArrayList<>());
            return AjaxResult.success(resultMap);
        }
        LambdaQueryWrapper<TaskHeader> taskHisWrapper=new LambdaQueryWrapper<>();
        taskHisWrapper.eq(TaskHeader::getWarehouseCode,warehouseCode);
        taskHisWrapper.eq(TaskHeader::getInternalTaskType,QuantityConstant.TASK_INTENERTYPE_SHIPMENT);
        taskHisWrapper.eq(TaskHeader::getStatus,QuantityConstant.TASK_STATUS_COMPLETED);
//        taskHisWrapper.ge(TaskHeader::getCreated,endTime1);
        taskHisWrapper.last("limit 10");
        taskHisWrapper.orderByDesc(TaskHeader::getCreated);
        taskHisWrapper.select(TaskHeader::getContainerCode,TaskHeader::getFromLocation,TaskHeader::getPort,TaskHeader::getCreated);
        List<TaskHeader> taskHeaderHis=taskHeaderService.list(taskHisWrapper);
        resultMap.put("taskCount",new ArrayList<>());
        resultMap.put("taskHistoryCount",taskHeaderHis);
//        result.add(resultMap);
        return AjaxResult.success(resultMap);
    }
    public AjaxResult csLocation(String warehouseCode){
        ArrayList<Object> result = new ArrayList<>();
        HashMap<String, Object> resultMap = new HashMap<>();
        LambdaQueryWrapper<Location> locationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        locationLambdaQueryWrapper.eq(Location::getWarehouseCode, warehouseCode);
        locationLambdaQueryWrapper.eq(Location::getZoneCode, "LK");
        locationLambdaQueryWrapper.in(Location::getRoadway, 1,2);
        List<Location> locationList = locationService.list(locationLambdaQueryWrapper);

        int haveContainerNum = (int) locationList.stream().filter(l -> StringUtils.isNotEmpty(l.getContainerCode())).count();
        int unHaveContainerNum = locationList.size() - haveContainerNum;
        resultMap.put("haveContainerNum", String.valueOf(haveContainerNum));
        resultMap.put("unHaveContainerNum", String.valueOf(unHaveContainerNum));
        result.add(resultMap);
        return AjaxResult.success(result);
    }

    public AjaxResult csMaterialHot(String warehouseCode){
        String endTime1= DateUtils.getNowPreDays("yyyy-MM-dd", 30);
        InventoryTransaction inventoryTransaction=new InventoryTransaction();
        inventoryTransaction.setWarehouseCode(warehouseCode);
        inventoryTransaction.setUserDef1(endTime1);
        List<InventoryTransactionReport> list=inventoryTransactionService.csMaterialHot(inventoryTransaction);
        return AjaxResult.success(list);
    }
    public AjaxResult csMaterialDull(String warehouseCode){
        LambdaQueryWrapper<InventoryDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(InventoryDetail::getWarehouseCode, warehouseCode);
        wrapper.orderByAsc(InventoryDetail::getCreated);
        wrapper.last("LIMIT 15");
        wrapper.select(InventoryDetail::getMaterialCode,InventoryDetail::getMaterialName,InventoryDetail::getQty,InventoryDetail::getCreated);
        List<InventoryDetail> list = inventoryDetailService.list(wrapper);
        if(list.size()==0){
            return AjaxResult.success(list);
        }
        //list去掉重复的物料名称数据得到新的list
        List<InventoryDetail> list1 = list.stream()
                .collect(Collectors.collectingAndThen(
                        Collectors.toCollection(
                                () -> new TreeSet<>(Comparator.comparing(InventoryDetail::getMaterialName))
                        ), ArrayList::new
                ));
//        //取前10个
        if(list1.size()>10){
            list1=list1.subList(0,10);
        }
        for (InventoryDetail inventoryDetail:list1){
            Date now = DateUtils.getNowDate();
            Date date = inventoryDetail.getCreated();
            int days = Math.toIntExact((now.getTime() - date.getTime()) / (1000 * 60 * 60 * 24));
            inventoryDetail.setDays(days);
        }
        return AjaxResult.success(list1);
    }
    public AjaxResult csMaterialCont(String warehouseCode){
        ArrayList<Object> result = new ArrayList<>();
        HashMap<String, Object> resultMap = new HashMap<>();
        QueryWrapper<InventoryDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("warehouseCode", warehouseCode);
        wrapper.select("sum(qty) as qty");
        Map<String, Object> materialMap = inventoryDetailService.getMap(wrapper);
        BigDecimal totalqty=materialMap.get("qty")==null?BigDecimal.ZERO:BigDecimal.valueOf(Double.parseDouble(materialMap.get("qty").toString()));

        String sql = "SELECT i.`materialName`,sum(i.qty) as total from inventory_detail i where  i.warehouseCode = '"+ warehouseCode+"' \n" +
                "GROUP BY i.`materialName` ORDER BY total desc limit 10;";
        List<LinkedHashMap<String, Object>> results = mapper.selectCommon(sql);

        float sum=100;
        for (LinkedHashMap<String, Object> i:results){
            String materialName=i.get("materialName").toString();
            BigDecimal qty=BigDecimal.valueOf(Double.parseDouble(i.get("total").toString()));
            resultMap.put(materialName,qty.divide(totalqty,2,BigDecimal.ROUND_HALF_UP).floatValue()*100);
            sum-=qty.divide(totalqty,2,BigDecimal.ROUND_HALF_UP).floatValue()*100;
        }
        resultMap.put("其他",sum);
//        result.add(resultMap);
        return AjaxResult.success(resultMap);
    }

}