AssetAnalysisController.java 10.3 KB
package com.skua.modules.equipment.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.skua.aop.annotation.CustomExceptionAnno;
import com.skua.core.api.vo.Result;
import com.skua.core.aspect.annotation.AutoLog;
import com.skua.modules.equipment.entity.EquipmentAsset;
import com.skua.modules.equipment.entity.EquipmentInfo;
import com.skua.modules.equipment.entity.EquipmentOutChild;
import com.skua.modules.equipment.entity.EquipmentSparepart;
import com.skua.modules.equipment.service.IEquipmentSparepartService;
import com.skua.modules.equipment.util.EquipmentUtils;
import com.skua.redis.util.CustomRedisUtil;
import com.skua.tool.query.WrapperFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sonin
 * @date 2021/12/8 11:19
 */
@Slf4j
@Api(tags = "资产分析")
@RestController
@RequestMapping("/equipment/assetAnalysis")
public class AssetAnalysisController {

    @Autowired
    private CustomRedisUtil customRedisUtil;
    @Autowired
    private IEquipmentSparepartService equipmentSparepartService;

    @AutoLog(value = "资产占比")
    @ApiOperation(value = "资产占比", notes = "资产占比")
    @GetMapping(value = "/assetPercent")
    @CustomExceptionAnno(description = "资产占比")
    public Result<Object> assetPercentCtrl(@RequestParam String startTime) throws Exception {
        Result<Object> result = new Result<>();
        // assetType: 资产类型value:key的转换
        Map<String, String> assetTypeVal2KeyMap = EquipmentUtils.convertFunc("assetType", false);
        List<Map<String, Object>> mapList = WrapperFactory.joinWrapper()
                .select(EquipmentAsset.class.getDeclaredField("assetType"), EquipmentInfo.class.getDeclaredField("departId"))
                .select("count(*) as total")
                .from(EquipmentInfo.class)
                .innerJoin(EquipmentAsset.class, EquipmentAsset.class.getDeclaredField("id"), EquipmentInfo.class.getDeclaredField("assetId"))
                .where()
                .eq(true, "date_format(equipment_info.create_time, '%Y')", startTime)
                .groupBy(true, "equipment_asset.asset_type", "equipment_info.depart_id")
                .queryWrapperForList();
        List<Map<String, Object>> convertMapList = WrapperFactory.result()
                .addCallback(EquipmentInfo.class.getDeclaredField("departId"), "departId_dictText")
                .addCallback(EquipmentAsset.class.getDeclaredField("assetType"), "assetType_dictText")
                .maps2MapsWithoutPrefix(mapList, (targetFieldName, srcFieldVal) -> {
                    if ("departId_dictText".equals(targetFieldName)) {
                        return customRedisUtil.hget("sys_depart", "" + srcFieldVal, "id", "depart_name");
                    } else if ("assetType_dictText".equals(targetFieldName)) {
                        return assetTypeVal2KeyMap.getOrDefault("" + srcFieldVal, "");
                    }
                    return srcFieldVal;
                });
        Map<String, List<Map<String, Object>>> assetType2EntityMap = new HashMap<>();
        for (Map<String, Object> item : convertMapList) {
            assetType2EntityMap.computeIfAbsent("" + item.get("assetType_dictText"), k -> new ArrayList<>());
            assetType2EntityMap.get("" + item.get("assetType_dictText")).add(item);
        }
        result.setResult(assetType2EntityMap);
        return result;
    }

    @AutoLog(value = "污水厂资产")
    @ApiOperation(value = "污水厂资产", notes = "污水厂资产")
    @GetMapping(value = "/assetOfFactory")
    @CustomExceptionAnno(description = "污水厂资产")
    public Result<Object> assetOfFactoryCtrl(@RequestParam String startTime) throws Exception {
        Result<Object> result = new Result<>();
        // assetType: 资产类型value:key的转换
        Map<String, String> assetTypeVal2KeyMap = EquipmentUtils.convertFunc("assetType", false);
        List<Map<String, Object>> mapList = WrapperFactory.joinWrapper()
                .select(EquipmentAsset.class.getDeclaredField("assetType"), EquipmentInfo.class.getDeclaredField("departId"))
                .select("count(*) as total")
                .from(EquipmentInfo.class)
                .innerJoin(EquipmentAsset.class, EquipmentAsset.class.getDeclaredField("id"), EquipmentInfo.class.getDeclaredField("assetId"))
                .where()
                .eq(true, "date_format(equipment_info.create_time, '%Y')", startTime)
                .groupBy(true, "equipment_info.depart_id", "equipment_asset.asset_type")
                .queryWrapperForList();
        List<Map<String, Object>> convertMapList = WrapperFactory.result()
                .addCallback(EquipmentInfo.class.getDeclaredField("departId"), "departId_dictText")
                .addCallback(EquipmentAsset.class.getDeclaredField("assetType"), "assetType_dictText")
                .maps2MapsWithoutPrefix(mapList, (targetFieldName, srcFieldVal) -> {
                    if ("departId_dictText".equals(targetFieldName)) {
                        return customRedisUtil.hget("sys_depart", "" + srcFieldVal, "id", "depart_name");
                    } else if ("assetType_dictText".equals(targetFieldName)) {
                        return assetTypeVal2KeyMap.getOrDefault("" + srcFieldVal, "");
                    }
                    return srcFieldVal;
                });
        Map<String, List<Map<String, Object>>> departId2EntityMap = new HashMap<>();
        for (Map<String, Object> item : convertMapList) {
            departId2EntityMap.computeIfAbsent("" + item.get("departId_dictText"), k -> new ArrayList<>());
            departId2EntityMap.get("" + item.get("departId_dictText")).add(item);
        }
        for (List<Map<String, Object>> item : departId2EntityMap.values()) {
            double sum = item.stream().mapToDouble(it -> Double.parseDouble("" + it.get("total"))).sum();
            for (Map<String, Object> it : item) {
                DecimalFormat decimalFormat = new DecimalFormat("0.00");
                it.put("percentage", decimalFormat.format(Integer.parseInt("" + it.get("total")) / sum * 100) + "%");
            }
        }
        result.setResult(departId2EntityMap);
        return result;
    }

    @AutoLog(value = "备件情况")
    @ApiOperation(value = "备件情况", notes = "备件情况")
    @GetMapping(value = "/equipmentSparepart")
    @CustomExceptionAnno(description = "备件情况")
    public Result<Object> equipmentSparepartCtrl(@RequestParam String startTime) throws Exception {
        Result<Object> result = new Result<>();
        Map<String, Object> resMap = new HashMap<>();
        // 备件过多
        QueryWrapper<EquipmentSparepart> moreQueryWrapper = new QueryWrapper<>();
        moreQueryWrapper
                .eq(true, "date_format(create_time, '%Y')", startTime)
                .apply("storage_num > storage_max_num");
        List<EquipmentSparepart> moreEquipmentSparepartList = equipmentSparepartService.list(moreQueryWrapper);
        resMap.put("more", moreEquipmentSparepartList);
        // 备件过少
        QueryWrapper<EquipmentSparepart> lessQueryWrapper = new QueryWrapper<>();
        lessQueryWrapper
                .eq(true, "date_format(create_time, '%Y')", startTime)
                .apply("storage_num < storage_min_num");
        List<EquipmentSparepart> lessEquipmentSparepartList = equipmentSparepartService.list(lessQueryWrapper);
        resMap.put("less", lessEquipmentSparepartList);
        // 出库数量最多的备件top5
        List<Map<String, Object>> top5MapList = WrapperFactory.joinWrapper()
                .select("sum(equipment_out_child.out_num) as sumOutNum", "sparepart_name as sparepartName", "storage_num as storageNum", "storage_max_num as storageMaxNum", "storage_min_num as storageMinNum")
                .from(EquipmentOutChild.class)
                .innerJoin(EquipmentSparepart.class, EquipmentSparepart.class.getDeclaredField("id"), EquipmentOutChild.class.getDeclaredField("sparepartId"))
                .where()
                .eq(true, "date_format(equipment_out_child.create_time, '%Y')", startTime)
                .groupBy(true, "equipment_sparepart.id")
                .orderBy(true, false, "sum(equipment_out_child.out_num)")
                .last(true, "limit 5")
                .queryWrapperForList();
        resMap.put("top", top5MapList);
        result.setResult(resMap);
        return result;
    }

    @AutoLog(value = "物资分析")
    @ApiOperation(value = "物资分析", notes = "物资分析")
    @GetMapping(value = "/sparepartType")
    @CustomExceptionAnno(description = "物资分析")
    public Result<Object> sparepartTypeCtrl(@RequestParam String startTime) throws Exception {
        Result<Object> result = new Result<>();
        QueryWrapper<EquipmentSparepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("count(*) as total", "sparepart_type as sparepartType")
                .eq("date_format(create_time, '%Y')", startTime)
                .groupBy("sparepart_type");
        List<Map<String, Object>> mapList = equipmentSparepartService.listMaps(queryWrapper);
        Map<String, String> sparepartTypeVal2KeyMap = EquipmentUtils.convertFunc("sparepartType", false);
        List<Map<String, Object>> resList = new ArrayList<>();
        for (Map.Entry<String, String> item : sparepartTypeVal2KeyMap.entrySet()) {
            Map<String, Object> resMap = new HashMap<>();
            resMap.put("sparepartType", item.getKey());
            resMap.put("sparepartType_dictText", sparepartTypeVal2KeyMap.get(item.getKey()));
            resMap.put("total", 0);
            for (Map<String, Object> it : mapList) {
                if (item.getKey().equals(it.get("sparepartType"))) {
                    resMap.put("total", it.get("total"));
                }
            }
            resList.add(resMap);
        }
        result.setResult(resList);
        return result;
    }

}