EquipmentCostAnalysisController.java 13.4 KB
package com.skua.modules.equipment.controller;

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.EquipmentInfo;
import com.skua.modules.equipment.entity.EquipmentRepair;
import com.skua.modules.equipment.entity.EquipmentRepairChild;
import com.skua.modules.equipment.entity.EquipmentSparepart;
import com.skua.modules.equipment.util.EquipmentUtils;
import com.skua.modules.equipment.vo.EquipmentCostAnalysisVO;
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.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 javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author sonin
 * @date 2021/12/2 10:07
 */
@Slf4j
@Api(tags = "设备成本核算")
@RestController
@RequestMapping("/equipment/cost/analysis")
public class EquipmentCostAnalysisController {

    @Resource
    private CustomRedisUtil customRedisUtil;

    @AutoLog(value = "累计维修成本")
    @ApiOperation(value = "累计维修成本", notes = "累计维修成本")
    @CustomExceptionAnno(description = "累计维修成本")
    @GetMapping(value = "/totalRepairCost")
    public Result<EquipmentCostAnalysisVO> totalRepairCostCtrl(@RequestParam(value = "startTime") String startTime) throws Exception {
        Result<EquipmentCostAnalysisVO> result = new Result<>();
        EquipmentCostAnalysisVO equipmentCostAnalysisVO = new EquipmentCostAnalysisVO();
        // 实际维修成本
        Map<String, Object> equipmentRepairMap = WrapperFactory.joinWrapper()
                .select("ifnull(sum(equipment_repair_child.repair_num * equipment_sparepart.unit_price + equipment_repair.commission_cost), 0) as actualRepairCost")
                .from(EquipmentRepairChild.class)
                .innerJoin(EquipmentRepair.class, EquipmentRepair.class.getDeclaredField("id"), EquipmentRepairChild.class.getDeclaredField("repairId"))
                .innerJoin(EquipmentSparepart.class, EquipmentSparepart.class.getDeclaredField("id"), EquipmentRepairChild.class.getDeclaredField("sparepartId"))
                .where()
                .apply(!startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y') = '" + startTime + "'")
                .apply(startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y-%m') = '" + startTime + "'")
                .queryWrapperForMap();
        equipmentCostAnalysisVO.setActualRepairCost("" + equipmentRepairMap.get("actualRepairCost"));
        // 备件消耗情况
        List<Map<String, Object>> sparepartMapList = WrapperFactory.joinWrapper()
                .select("ifnull(sum(equipment_repair_child.repair_num * equipment_sparepart.unit_price), 0) as cost", "equipment_sparepart.sparepart_type as sparepartType", "count(*) as num")
                .from(EquipmentRepairChild.class)
                .innerJoin(EquipmentRepair.class, EquipmentRepair.class.getDeclaredField("id"), EquipmentRepairChild.class.getDeclaredField("repairId"))
                .innerJoin(EquipmentSparepart.class, EquipmentSparepart.class.getDeclaredField("id"), EquipmentRepairChild.class.getDeclaredField("sparepartId"))
                .where()
                .apply(!startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y') = '" + startTime + "'")
                .apply(startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y-%m') = '" + startTime + "'")
                .groupBy(true, "equipment_sparepart.sparepart_type")
                .queryWrapperForList();
        Map<String, String> sparepartTypeVal2KeyMap = EquipmentUtils.convertFunc("sparepartType", false);
        List<EquipmentCostAnalysisVO.Charts> sparepartCharts = WrapperFactory.result()
                .addCallback("sparepartType", "sparepartType_dictText")
                .maps2Beans(sparepartMapList, EquipmentCostAnalysisVO.Charts.class, (targetFieldName, srcFieldVal) -> {
                    if ("sparepartType_dictText".equals(targetFieldName)) {
                        return sparepartTypeVal2KeyMap.get("" + srcFieldVal);
                    }
                    return srcFieldVal;
                });
        for (Map.Entry<String, String> item : sparepartTypeVal2KeyMap.entrySet()) {
            if (!sparepartCharts.stream().map(EquipmentCostAnalysisVO.Charts::getSparepartType_dictText).collect(Collectors.toList()).contains(item.getKey())) {
                EquipmentCostAnalysisVO.Charts charts = new EquipmentCostAnalysisVO.Charts();
                charts.setSparepartType_dictText(item.getValue());
                sparepartCharts.add(charts);
            }
        }
        equipmentCostAnalysisVO.setFirstCharts(sparepartCharts);
        result.setResult(equipmentCostAnalysisVO);
        return result;
    }

    @AutoLog(value = "故障次数")
    @ApiOperation(value = "故障次数", notes = "故障次数")
    @CustomExceptionAnno(description = "故障次数")
    @GetMapping(value = "/fault")
    public Result<EquipmentCostAnalysisVO> faultCtrl(@RequestParam(value = "startTime") String startTime) throws Exception {
        Result<EquipmentCostAnalysisVO> result = new Result<>();
        EquipmentCostAnalysisVO equipmentCostAnalysisVO = new EquipmentCostAnalysisVO();
        // 故障次数
        List<Map<String, Object>> faultTimesMapList = WrapperFactory.joinWrapper()
                .select("count(*) as num", "equipment_info.depart_id as departId")
                .from(EquipmentRepair.class)
                .innerJoin(EquipmentInfo.class, EquipmentInfo.class.getDeclaredField("id"), EquipmentRepair.class.getDeclaredField("infoId"))
                .where()
                .apply(!startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y') = '" + startTime + "'")
                .apply(startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y-%m') = '" + startTime + "'")
                .groupBy(true, "equipment_info.depart_id")
                .orderBy(true, false, "num")
                .queryWrapperForList();
        List<EquipmentCostAnalysisVO.Charts> faultTimesCharts = WrapperFactory.result()
                .addCallback("departId", "departId_dictText")
                .maps2Beans(faultTimesMapList, EquipmentCostAnalysisVO.Charts.class, (targetFieldName, srcFieldVal) -> {
                    if ("departId_dictText".equals(targetFieldName)) {
                        return customRedisUtil.hget("sys_depart", "" + srcFieldVal, "id", "depart_name");
                    }
                    return srcFieldVal;
                });
        equipmentCostAnalysisVO.setFirstCharts(faultTimesCharts);
        // 故障类型
        List<Map<String, Object>> faultTypeMapList = WrapperFactory.joinWrapper()
                .select("count(*) as num", "equipment_repair.fault_type as faultType")
                .from(EquipmentRepair.class)
                .innerJoin(EquipmentInfo.class, EquipmentInfo.class.getDeclaredField("id"), EquipmentRepair.class.getDeclaredField("infoId"))
                .where()
                .apply(!startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y') = '" + startTime + "'")
                .apply(startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y-%m') = '" + startTime + "'")
                .groupBy(true, "equipment_repair.fault_type")
                .queryWrapperForList();
        Map<String, String> faultTypeVal2KeyMap = EquipmentUtils.convertFunc("faultType", false);
        List<EquipmentCostAnalysisVO.Charts> faultTypeCharts = WrapperFactory.result()
                .addCallback("faultType", "faultType_dictText")
                .maps2Beans(faultTypeMapList, EquipmentCostAnalysisVO.Charts.class, (targetFieldName, srcFieldVal) -> {
                    if ("faultType_dictText".equals(targetFieldName)) {
                        return faultTypeVal2KeyMap.get("" + srcFieldVal);
                    }
                    return srcFieldVal;
                });
        for (Map.Entry<String, String> item : faultTypeVal2KeyMap.entrySet()) {
            if (!faultTypeCharts.stream().map(EquipmentCostAnalysisVO.Charts::getFaultType_dictText).collect(Collectors.toList()).contains(item.getValue())) {
                EquipmentCostAnalysisVO.Charts charts = new EquipmentCostAnalysisVO.Charts();
                charts.setFaultType_dictText(item.getValue());
                faultTypeCharts.add(charts);
            }
        }
        equipmentCostAnalysisVO.setSecondCharts(faultTypeCharts);
        result.setResult(equipmentCostAnalysisVO);
        return result;
    }

    @AutoLog(value = "各厂站维修成本")
    @ApiOperation(value = "各厂站维修成本", notes = "各厂站维修成本")
    @CustomExceptionAnno(description = "各厂站维修成本")
    @GetMapping(value = "/departRepairCost")
    public Result<EquipmentCostAnalysisVO> departRepairCostCtrl(@RequestParam(value = "startTime") String startTime) throws Exception {
        Result<EquipmentCostAnalysisVO> result = new Result<>();
        EquipmentCostAnalysisVO equipmentCostAnalysisVO = new EquipmentCostAnalysisVO();
        List<Map<String, Object>> mapList = WrapperFactory.joinWrapper()
                .select("equipment_info.depart_id as departId", "ifnull(sum(equipment_repair_child.repair_num * equipment_sparepart.unit_price), 0) as sparepartCost", "ifnull(sum(equipment_repair.commission_cost), 0) as commissionCost", "ifnull(sum(equipment_repair_child.repair_num * equipment_sparepart.unit_price), 0) + ifnull(sum(equipment_repair.commission_cost), 0) as totalCost")
                .from(EquipmentRepairChild.class)
                .innerJoin(EquipmentRepair.class, EquipmentRepair.class.getDeclaredField("id"), EquipmentRepairChild.class.getDeclaredField("repairId"))
                .innerJoin(EquipmentSparepart.class, EquipmentSparepart.class.getDeclaredField("id"), EquipmentRepairChild.class.getDeclaredField("sparepartId"))
                .innerJoin(EquipmentInfo.class, EquipmentInfo.class.getDeclaredField("id"), EquipmentRepair.class.getDeclaredField("infoId"))
                .where()
                .apply(!startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y') = '" + startTime + "'")
                .apply(startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y-%m') = '" + startTime + "'")
                .groupBy(true, "equipment_info.depart_id")
                .queryWrapperForList();
        List<EquipmentCostAnalysisVO.DepartCost> departCostList = WrapperFactory.result()
                .addCallback("departId", "departId_dictText")
                .maps2Beans(mapList, EquipmentCostAnalysisVO.DepartCost.class, (targetFieldName, srcFieldVal) -> {
                    if ("departId_dictText".equals(targetFieldName)) {
                        return customRedisUtil.hget("sys_depart", "" + srcFieldVal, "id", "depart_name");
                    }
                    return srcFieldVal;
                });
        equipmentCostAnalysisVO.setDepartCostList(departCostList);
        result.setResult(equipmentCostAnalysisVO);
        return result;
    }

    @AutoLog(value = "设备故障Top10")
    @ApiOperation(value = "设备故障Top10", notes = "设备故障Top10")
    @CustomExceptionAnno(description = "设备故障Top10")
    @GetMapping(value = "/equipmentFault")
    public Result<EquipmentCostAnalysisVO> equipmentFaultCtrl(@RequestParam(value = "startTime") String startTime) throws Exception {
        Result<EquipmentCostAnalysisVO> result = new Result<>();
        EquipmentCostAnalysisVO equipmentCostAnalysisVO = new EquipmentCostAnalysisVO();
        List<Map<String, Object>> mapList = WrapperFactory.joinWrapper()
                .select("depart_id as departId", "equipment_name as equipmentName", "count(*) as equipmentFaultTimes", "install_position as installPosition", "depart_id as departId", "manufacturer")
                .from(EquipmentRepair.class)
                .innerJoin(EquipmentInfo.class, EquipmentInfo.class.getDeclaredField("id"), EquipmentRepair.class.getDeclaredField("infoId"))
                .where()
                .apply(!startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y') = '" + startTime + "'")
                .apply(startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y-%m') = '" + startTime + "'")
                .groupBy(true, "equipment_info.id")
                .orderBy(true, false, "equipmentFaultTimes")
                .last(true, "limit 10")
                .queryWrapperForList();
        List<EquipmentCostAnalysisVO.EquipmentFault> equipmentFaultList = WrapperFactory.result()
                .addCallback("departId", "departId_dictText")
                .maps2Beans(mapList, EquipmentCostAnalysisVO.EquipmentFault.class, (targetFieldName, srcFieldVal) -> {
                    if ("departId_dictText".equals(targetFieldName)) {
                        return customRedisUtil.hget("sys_depart", "" + srcFieldVal, "id", "depart_name");
                    }
                    return srcFieldVal;
                });
        equipmentCostAnalysisVO.setEquipmentFaultList(equipmentFaultList);
        result.setResult(equipmentCostAnalysisVO);
        return result;
    }

}