package com.sw.mes.dingTalk.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sw.mes.base.factory.init.entity.Equipment;
import com.sw.mes.base.factory.init.service.EquipmentService;
import com.sw.mes.base.factory.init.service.FactoryModelService;
import com.sw.mes.core.enums.SeedMassageEnum;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.core.utils.DateUtil;
import com.sw.mes.dingTalk.dto.CapacityInputDto;
import com.sw.mes.dingTalk.dto.CuttingProcessDto;
import com.sw.mes.dingTalk.dto.LowYieldDeviceDto;
import com.sw.mes.dingTalk.service.DingTalkService;
import com.sw.mes.yb.pad.service.ProductionPaintingLineService;
import com.sw.mes.yb.plan.service.ProductionOrderService;
import com.sw.mes.yb.production.entity.LongCrystal;
import com.sw.mes.yb.production.entity.ProcessNoDiameter;
import com.sw.mes.yb.production.entity.ProcessWorkingHours;
import com.sw.mes.yb.production.service.LongCrystalService;
import com.sw.mes.yb.production.service.ProcessNoDiameterService;
import com.sw.mes.yb.production.service.ProcessWorkingHoursService;
import com.sw.mes.yb.report.dto.WorkOrderProgressReportDto;
import com.sw.mes.yb.report.entity.BrokenBractBase;
import com.sw.mes.yb.report.service.BrokenBractBaseService;
import com.sw.mes.yb.report.service.WorkOrderProgressReportService;
import com.xxl.job.core.handler.annotation.XxlJob;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.jetbrains.annotations.NotNull;
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.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * @author lf
 * @description
 * @date 2024/3/19 21:01
 */
@RestController
@RequestMapping("/seedMessage")
@Slf4j
@Api(description = "所有发送晶彩消息接口")
public class seedMessageController {

    @Autowired
    private DingTalkService dingTalkService;

    @Autowired
    private ProcessNoDiameterService productionProcessNoDiameterService;

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private BrokenBractBaseService brokenBractBaseService;

    @Autowired
    private ProcessWorkingHoursService processWorkingHoursService;

    @Autowired
    private ProductionPaintingLineService productionPaintingLineService;

    @Autowired
    private QualitySeed qualitySeed;

    @Autowired
    private WorkOrderProgressReportService workOrderProgressReportService;

    @Autowired
    private ProductionOrderService productionOrderService;

    @Autowired
    private FactoryModelService factoryModelService;

    @Autowired
    private LongCrystalService longCrystalService;

    @ApiOperation(value = "12小时未等径炉台消息推送")
    @GetMapping("/twelveHourUnequalDiameter")
    @XxlJob("twelveHourUnequalDiameter")
    public Result twelveHourUnequalDiameter() {
        String stepName = "12小时未等径";
        List<ProcessNoDiameter> processNoDiameterList = productionProcessNoDiameterService.findByStepName(stepName);
        ArrayList<ProcessNoDiameter> distinctProcessNoDiameterList = processNoDiameterList.stream()
                .collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(ProcessNoDiameter::getEquipmentCode))), ArrayList::new));

        StringBuilder equipmentCode = new StringBuilder();
        if (CollectionUtils.isNotEmpty(distinctProcessNoDiameterList)) {
            for (ProcessNoDiameter processNoDiameter : distinctProcessNoDiameterList) {
                equipmentCode.append(",").append(processNoDiameter.getEquipmentCode());
            }
            String content = "炉台:" + equipmentCode + ",12小时未等径";
            String source = "12h未等径炉台推送";
            ArrayList<String> sceneList = new ArrayList<>();
            ArrayList<String> groupNameList = new ArrayList<>();
            sceneList.add("prod");
            groupNameList.add("12h未等径炉台推送");
            dingTalkService.robotSeedMessage(content, groupNameList, source, sceneList, SeedMassageEnum.GROUP);
        }
        return ResultGenerator.success();
    }

    @ApiOperation(value = "断线、断线区间占比消息推送")
    @GetMapping("/disconnection")
    @XxlJob("equipmentMonitorSeedMassage")
    public Result disconnection() {
        HashSet<String> equipmentCodeSet = new HashSet<>();
        List<Equipment> equipmentList = equipmentService.findByCurrentStep();
        if (CollectionUtils.isNotEmpty(equipmentList)) {
            for (Equipment equipment : equipmentList) {
                String equipmentCode = equipment.getCode().substring(0, 2);
                equipmentCodeSet.add(equipmentCode);
            }
        }
        for (String equipmentCode : equipmentCodeSet) {
            StringBuilder sb = getStringBuilder(equipmentCode);
            ArrayList<String> groupNameList = new ArrayList<>();
            ArrayList<String> sceneList = new ArrayList<>();
            sceneList.add("prod");
            groupNameList.add("断线、断线区间占比消息推送");
            String source = "断线、断线区间占比消息推送";
            dingTalkService.robotSeedMessage(sb.toString(), groupNameList, source, sceneList, SeedMassageEnum.GROUP);
        }
        return ResultGenerator.success();
    }

    @ApiOperation(value = "按片区拆到熔时间消息推送")
    @GetMapping("/dismantlingByAreaToMeltingTime")
    @XxlJob("dismantlingByAreaToMeltingTimeSeedMessage")
    public Result dismantlingByAreaToMeltingTime() {
        String stepName = "拆清到熔料工时";

        List<ProcessWorkingHours> processWorkingHoursList = processWorkingHoursService.findByStepName(stepName);

        StringBuilder equipmentCode = new StringBuilder();
        if (CollectionUtils.isNotEmpty(processWorkingHoursList)) {
            for (ProcessWorkingHours processWorkingHours : processWorkingHoursList) {
                equipmentCode.append(",").append(processWorkingHours.getEquipmentCode());
            }
            String content = "炉台:" + equipmentCode + ",拆清到熔料工时";
            ArrayList<String> groupNameList = new ArrayList<>();
            ArrayList<String> sceneList = new ArrayList<>();
            sceneList.add("prod");
            groupNameList.add("按片区拆到熔时间消息推送");
            String source = "按片区拆到熔时间消息推送";
            dingTalkService.robotSeedMessage(content, groupNameList, source, sceneList, SeedMassageEnum.GROUP);
        }
        return ResultGenerator.success();
    }

    @ApiOperation(value = "产能录入推送")
    @GetMapping("/capacityInput")
    @XxlJob("capacityInputSeedMassage")
    public Result capacityInput() {
        StringBuilder sb = getCapacityInputStringBuilder();
        ArrayList<String> groupNameList = new ArrayList<>();
        ArrayList<String> sceneList = new ArrayList<>();
        sceneList.add("prod");
        groupNameList.add("产能录入推送");
        String source = "产能录入推送";
        dingTalkService.robotSeedMessage(sb.toString(), groupNameList, source, sceneList, SeedMassageEnum.GROUP);
        return ResultGenerator.success();
    }


    @ApiOperation(value = "低效炉台、单产低于120kg消息推送")
    @GetMapping("/lowYieldDevice")
    @XxlJob("lowYieldDeviceSeedMessage")
    public Result lowYieldDevice() {
        List<Map<String, Object>> lowYieldDeviceDtoList = equipmentService.findLowYieldDeviceDto();
        HashSet<String> equipmentCodeSet = new HashSet<>();


        List<LowYieldDeviceDto> lowYieldDeviceDtoList1 = lowYieldDeviceDtoList.stream()
                .map(e -> JSON.parseObject(JSONObject.toJSONString(e), LowYieldDeviceDto.class))
                .collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(lowYieldDeviceDtoList1)) {
            StringBuilder sb = new StringBuilder();
            sb.append("低效汇总");
            sb.append("\n");
            for (LowYieldDeviceDto lowYieldDeviceDto : lowYieldDeviceDtoList1) {
                String equipmentCode = lowYieldDeviceDto.getEquipmentCode().substring(0, 2);
                equipmentCodeSet.add(equipmentCode);
            }
            for (String equipmentCode : equipmentCodeSet) {
                List<LowYieldDeviceDto> yieldDeviceDtoList = lowYieldDeviceDtoList1.stream()
                        .filter(item -> item.getEquipmentCode().contains(equipmentCode))
                        .collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(yieldDeviceDtoList)) {
                    StringBuilder code = new StringBuilder();
                    for (LowYieldDeviceDto lowYieldDeviceDto : yieldDeviceDtoList) {
                        code.append(lowYieldDeviceDto.getEquipmentCode()).append(",");

                    }
                    sb.append(equipmentCode + "片区:" + code);
                    sb.append("\n");
                }
            }
            String source = "低效炉台、单产低于120kg消息推送";
            ArrayList<String> groupNameList = new ArrayList<>();
            ArrayList<String> sceneList = new ArrayList<>();
            sceneList.add("prod");
            groupNameList.add("低效炉台、单产低于120kg消息推送");
            dingTalkService.robotSeedMessage(String.valueOf(sb), groupNameList, source, sceneList, SeedMassageEnum.GROUP);
        }
        return ResultGenerator.success();
    }

    @ApiOperation(value = "工单超收异常消息推送")
    @GetMapping("/orderNoTimeOut")
    @XxlJob("orderNoTimeOutMessage")
    public Result orderNoTimeOut() {
        WorkOrderProgressReportDto workOrderProgressReportDto = new WorkOrderProgressReportDto();
        workOrderProgressReportDto.setField4("单晶");
        workOrderProgressReportDto.setStatus("REL");
        // 设置gltrp字段为一个月前的 0点0分0秒
        Calendar oneMonthAgo = Calendar.getInstance();
        oneMonthAgo.add(Calendar.MONTH, -1);
        oneMonthAgo.set(Calendar.HOUR_OF_DAY, 0);
        oneMonthAgo.set(Calendar.MINUTE, 0);
        oneMonthAgo.set(Calendar.SECOND, 0);
        workOrderProgressReportDto.setGltrp(oneMonthAgo.getTime());

        // 设置gstrp字段为今天的 0点0分0秒
        Calendar today = Calendar.getInstance();
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        workOrderProgressReportDto.setGstrp(today.getTime());
        List<WorkOrderProgressReportDto> orderProgressList = workOrderProgressReportService.findOrderProgress(workOrderProgressReportDto);
//        订单关闭状态
        workOrderProgressReportDto.setStatus("CLOSE");
        workOrderProgressReportDto.setGltrp(oneMonthAgo.getTime());
        workOrderProgressReportDto.setGstrp(today.getTime());
        List<WorkOrderProgressReportDto> orderProgressList2 = workOrderProgressReportService.findOrderProgress(workOrderProgressReportDto);
        orderProgressList.addAll(orderProgressList2);
        StringBuilder orderNO = new StringBuilder();
        Boolean isFlag = true;
        BigDecimal bigDecimal = new BigDecimal("1");
        if (CollectionUtils.isNotEmpty(orderProgressList)) {
            for (WorkOrderProgressReportDto dto : orderProgressList) {
                if (dto.getInputOutputRatio().compareTo(String.valueOf(bigDecimal)) > 0) {
                    orderNO.append(",").append(dto.getAufnr());
                    isFlag = false;
                }
            }
            if (!isFlag) {
                String content = "工单:" + orderNO + ",超收异常";
                String source = "工单超收异常消息推送";
                ArrayList<String> groupNameList = new ArrayList<>();
                ArrayList<String> sceneList = new ArrayList<>();
                sceneList.add("prod");
                groupNameList.add("工单超收异常消息推送");
                dingTalkService.robotSeedMessage(content, groupNameList, source, sceneList, SeedMassageEnum.GROUP);
            }
        }
        return ResultGenerator.success();
    }

    @ApiOperation(value = "氧数据推送")
    @GetMapping("/oxygen")
    @XxlJob("oxygenMonitor")
    public Result oxygen() {
        String source = "氧数据推送";
        Result<String> result = qualitySeed.sendOxygenInfo();
        if (result != null && result.getCode() == 200) {
            ArrayList<String> groupNameList = new ArrayList<>();
            ArrayList<String> sceneList = new ArrayList<>();
            sceneList.add("prod");
            groupNameList.add("氧数据推送");
            dingTalkService.robotSeedMessage(result.getData(), groupNameList, source, sceneList, SeedMassageEnum.GROUP);
        }
        return result;
    }

    @ApiOperation(value = "工单余量监控")
    @GetMapping("/workOrderMarginMonitor")
    @XxlJob("workOrderMarginMonitor")
    public Result workOrderMarginMonitor() {
        List<Map<String, Object>> list = productionOrderService.findByWorkOrderMarginMonitor();
        if (CollectionUtils.isNotEmpty(list)) {
            StringBuilder sb = new StringBuilder();
            sb.append("[MES推送]工单余量监控");
            sb.append("\n");
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = formatter.format(new Date());
            sb.append("..时间: " + date);
            sb.append("\n");
            for (Map<String, Object> map : list) {
                Object field5 = map.get("field5");
                Object productName = map.get("product_name");
                Object auart = map.get("auart");
                Object count = map.get("count");
                sb.append(field5 + " " + productName + " " + auart + " " + count);
                sb.append("\n");
            }
            String source = "工单余量监控";
            ArrayList<String> groupNameList = new ArrayList<>();
            ArrayList<String> sceneList = new ArrayList<>();
            sceneList.add("prod");
            groupNameList.add("工单余量监控");
            dingTalkService.robotSeedMessage(String.valueOf(sb), groupNameList, source, sceneList, SeedMassageEnum.GROUP);
        }
        return ResultGenerator.success();
    }

    @ApiOperation(value = "MES晶棒冷却超时推送")
    @GetMapping("/coolingTimeout")
    @XxlJob("coolingTimeout")
    public Result coolingTimeout() {
        List<Map<String, Object>> list = productionOrderService.findByWorkOrderMarginMonitor();
        if (CollectionUtils.isNotEmpty(list)) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = formatter.format(new Date());
            StringBuilder sb = new StringBuilder();
            sb.append("[" + date + "]MES晶棒冷却超时推送");
            sb.append("\n");
            List<LongCrystal> longCrystal = longCrystalService.findLongCrystal();
            if(longCrystal.size() > 0){
                for (LongCrystal crystal : longCrystal) {
                    sb.append(".晶棒编码: " + crystal.getCrystalNo());
                    sb.append("\t\t\t\t\t\t\t\t");
                    sb.append("长度: " + crystal.getLength());
                    sb.append("\t\t\t\t\t\t\t\t");
                    sb.append("产出时间: " + crystal.getOutputConfirmationTime());
                    sb.append("\n");
                }
            } else {
                sb.append(".晶棒编码: " + date);
                sb.append("\t\t\t\t\t\t\t\t");
                sb.append("长度: " + date);
                sb.append("\t\t\t\t\t\t\t\t");
                sb.append("产出时间: " + date);
                sb.append("\n");
            }
            String source = "MES晶棒冷却超时推送";
            ArrayList<String> groupNameList = new ArrayList<>();
            ArrayList<String> sceneList = new ArrayList<>();
            sceneList.add("prod");
            groupNameList.add("MES晶棒冷却超时推送");
            dingTalkService.robotSeedMessage(String.valueOf(sb), groupNameList, source, sceneList, SeedMassageEnum.GROUP);
        }
        return ResultGenerator.success();
    }


    @ApiOperation(value = "切方各工序推送")
    @GetMapping("/cuttingProcess")
    @XxlJob("cuttingProcess")
    public Result cuttingProcess() {
        List<Map<String, Object>> list = factoryModelService.cuttingProcess();
        List<CuttingProcessDto> cuttingProcessDtoList = list.stream()
                .map(item -> JSON.parseObject(JSONObject.toJSONString(item), CuttingProcessDto.class))
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(cuttingProcessDtoList)) {
            return ResultGenerator.success();
        }
        StringBuilder sb = new StringBuilder();
        sb.append("[MES推送]切方各工序推送");
        sb.append("\n");
        for (CuttingProcessDto cuttingProcessDto : cuttingProcessDtoList) {
            sb.append("工序:" + cuttingProcessDto.getName() + "," + "N型总数:" + cuttingProcessDto.getNCount() + "," + "N型折重:" + cuttingProcessDto.getNFoldWeight());
            sb.append("\n");
        }
        ArrayList<String> groupNameList = new ArrayList<>();
        ArrayList<String> sceneList = new ArrayList<>();
        sceneList.add("prod");
        groupNameList.add("切方各工序推送");
        String source = "切方各工序推送";
        dingTalkService.robotSeedMessage(String.valueOf(sb), groupNameList, source, sceneList, SeedMassageEnum.GROUP);
        return ResultGenerator.success();
    }

    @NotNull
    private StringBuilder getStringBuilder(String equipmentCode) {
        List<Equipment> equipmentList = equipmentService.findByCurrentStep();
        List<Equipment> filteredList = equipmentList.stream()
                .filter(equipment -> equipment.getCode().contains(equipmentCode))
                .collect(Collectors.collectingAndThen(
                        Collectors.toMap(Equipment::getCode, Function.identity(), (existing, replacement) -> existing),
                        map -> new ArrayList<>(map.values())
                ));
        Map<String, List<Equipment>> listMap = filteredList.stream().collect(Collectors.groupingBy(Equipment::getCurrentStep));

        StringBuilder sb = new StringBuilder();
        sb.append(". [MES推送]炉台状态提醒:");
        sb.append("\n");
        sb.append(". 片区:" + equipmentCode);
        sb.append("\n");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String format = formatter.format(date);
        sb.append(". 当前时间:" + format);
        sb.append("\n");
//        熔料
        List<Equipment> moltenMaterialList = listMap.get("熔料");
        if (CollectionUtils.isEmpty(moltenMaterialList)) {
            sb.append(". 熔料(0)");
            sb.append("\n");
        } else {
            sb.append(". 熔料(" + moltenMaterialList.size() + ")");
            sb.append("\n");
        }
//        复投
        List<Equipment> resubmissionList = listMap.get("复投");
        if (CollectionUtils.isEmpty(resubmissionList)) {
            sb.append(". 加料(0)");
            sb.append("\n");
        } else {
            sb.append(". 加料(" + resubmissionList.size() + ")");
            sb.append("\n");
        }
//        调温
        List<Equipment> auxiliaryRoomPurificationList = listMap.get("副室净化");
        List<Equipment> weldJointList = listMap.get("熔接");
        if (CollectionUtils.isEmpty(auxiliaryRoomPurificationList) && CollectionUtils.isEmpty(weldJointList)) {
            sb.append(". 调温(0)");
            sb.append("\n");
        } else if (CollectionUtils.isNotEmpty(auxiliaryRoomPurificationList)) {
            sb.append(". 调温(" + auxiliaryRoomPurificationList.size() + ")");
            sb.append("\n");
        } else if (CollectionUtils.isNotEmpty(weldJointList)) {
            sb.append(". 调温(" + weldJointList.size() + ")");
            sb.append("\n");
        } else if (CollectionUtils.isNotEmpty(auxiliaryRoomPurificationList) && CollectionUtils.isNotEmpty(weldJointList)) {
            sb.append(". 调温(" + auxiliaryRoomPurificationList.size() + weldJointList.size() + ")");
            sb.append("\n");
        }
//        引晶
        List<Equipment> leadCrystalList = listMap.get("引晶");
        if (CollectionUtils.isEmpty(leadCrystalList)) {
            sb.append(". 引晶(0)");
            sb.append("\n");
        } else {
            sb.append(". 引晶(" + leadCrystalList.size() + ")");
            sb.append("\n");
        }
//        放肩
        List<Equipment> shoulderingList = listMap.get("放肩");
        if (CollectionUtils.isEmpty(shoulderingList)) {
            sb.append(". 放肩(0)");
            sb.append("\n");
        } else {
            sb.append(". 放肩(" + shoulderingList.size() + ")");
            sb.append("\n");
        }
//        等径
        List<Equipment> equalDiameterList = listMap.get("等径");
        if (CollectionUtils.isNotEmpty(equalDiameterList)) {
            List<Equipment> equalDiameterList1 = equalDiameterList.stream().filter(item -> Objects.nonNull(item.getPower()) && item.getPower().compareTo(new BigDecimal(65)) > 0).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(equalDiameterList1)) {
                sb.append(". 等径(0)");
                sb.append("\n");
            } else {
                sb.append(". 等径(" + equalDiameterList1.size() + ")");
                sb.append("\n");
            }
        }
//        取段
        List<Equipment> takeSegmentList = listMap.get("取段");
        if (CollectionUtils.isEmpty(takeSegmentList)) {
            sb.append(". 取段(0)");
            sb.append("\n");
        } else {
            sb.append(". 取段(" + takeSegmentList.size() + ")");
            sb.append("\n");
        }
//        停炉
        List<Equipment> manualList = listMap.get("手动");
        List<Equipment> shutDownFurnaceList = listMap.get("停炉");
        if (CollectionUtils.isEmpty(shutDownFurnaceList) && CollectionUtils.isEmpty(manualList)) {
            sb.append(". 停炉(0)");
            sb.append("\n");
        } else if (CollectionUtils.isNotEmpty(shutDownFurnaceList)) {
            sb.append(". 停炉(" + shutDownFurnaceList.size() + ")");
            sb.append("\n");
        } else if (CollectionUtils.isNotEmpty(manualList)) {
            sb.append(". 停炉(" + manualList.size() + ")");
            sb.append("\n");
        } else if (CollectionUtils.isNotEmpty(manualList) && CollectionUtils.isNotEmpty(shutDownFurnaceList)) {
            sb.append(". 停炉(" + manualList.size() + shutDownFurnaceList.size() + ")");
            sb.append("\n");
        }
//      拉多晶
        List<Equipment> lapolycrystallineList = listMap.get("等径");
        if (CollectionUtils.isNotEmpty(lapolycrystallineList)) {
            List<Equipment> lapolycrystallineList1 = lapolycrystallineList.stream().filter(item -> Objects.nonNull(item.getPower()) && item.getPower().compareTo(new BigDecimal(65)) < 0).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(lapolycrystallineList1)) {
                sb.append(". 拉多晶(0)");
                sb.append("\n");
            } else {
                sb.append(". 拉多晶(" + lapolycrystallineList1.size() + ")");
                sb.append("\n");
            }
        }
//          查询断苞基础表
        List<BrokenBractBase> brokenBractBaseList = brokenBractBaseService.findByLineBreakType();
        List<BrokenBractBase> filteredList2 = brokenBractBaseList.stream()
                .filter(brokenBractBase -> brokenBractBase.getDeviceNo().contains(equipmentCode))
                .collect(Collectors.collectingAndThen(
                        Collectors.toMap(BrokenBractBase::getDeviceNo, Function.identity(), (existing, replacement) -> existing),
                        map -> new ArrayList<>(map.values())
                ));

        Map<String, List<BrokenBractBase>> listMap1 = filteredList2.stream().collect(Collectors.groupingBy(BrokenBractBase::getLineBreakType));
//      等径收尾
        List<BrokenBractBase> equalDiameterClosureList = listMap1.get("等径收尾");
        if (CollectionUtils.isEmpty(equalDiameterClosureList)) {
            sb.append(". 等径收尾(0):");
            sb.append("\n");
        } else {
            sb.append(". 等径收尾(" + equalDiameterClosureList.size() + ")");
            sb.append("\n");
        }
//        放肩断线
        List<BrokenBractBase> shoulderBreakageList = listMap1.get("放肩断线");
        if (CollectionUtils.isEmpty(shoulderBreakageList)) {
            sb.append(". 放肩断线(0):");
            sb.append("\n");
        } else {
            String shoulderBreakage = "";
            for (BrokenBractBase base : shoulderBreakageList) {
                shoulderBreakage = shoulderBreakage + base.getDeviceNo() + ",";
            }
            sb.append(". 放肩断线(" + shoulderBreakageList.size() + "):" + shoulderBreakage);
            sb.append("\n");
        }
//        等径断线
        List<BrokenBractBase> equalDiameterBrokenWireList = listMap1.get("等径断线");
        if (CollectionUtils.isEmpty(equalDiameterBrokenWireList)) {
            sb.append(". 等径断线分类(0):");
            sb.append("\n");
            sb.append("<=500(0):");
            sb.append("\n");
            sb.append("<=1000(0):");
            sb.append("\n");
            sb.append("<=2000(0):");
            sb.append("\n");
            sb.append("<=3000(0):");
            sb.append("\n");
            sb.append(" >3000(0):");
            sb.append("\n");
        } else {
            equalDiameterBrokenWireList.sort(Comparator.comparing(BrokenBractBase::getDeviceNo));
            StringBuilder equalDiameterBrokenWireSb = new StringBuilder();
            StringBuilder fiveHundredSb = new StringBuilder();
            int fiveHundredLength = 0;
            BigDecimal fiveHundred = new BigDecimal("500");
            StringBuilder oneThousandSb = new StringBuilder();
            int oneThousandLength = 0;
            BigDecimal oneThousand = new BigDecimal("1000");
            StringBuilder twoThousandSb = new StringBuilder();
            int twoThousandLength = 0;
            BigDecimal twoThousand = new BigDecimal("2000");
            StringBuilder threeThousandSb = new StringBuilder();
            int threeThousandLength = 0;
            BigDecimal threeThousand = new BigDecimal("3000");
            StringBuilder greaterThanThreeThousandSb = new StringBuilder();
            int greaterThanThreeThousandLength = 0;
            for (BrokenBractBase base : equalDiameterBrokenWireList) {
                equalDiameterBrokenWireSb.append(base.getDeviceNo()).append(",");
                if (base.getCrystalLength().compareTo(fiveHundred) <= 0) {
                    fiveHundredSb.append(base.getDeviceNo()).append(",");
                    fiveHundredLength++;
                }
                if (base.getCrystalLength().compareTo(oneThousand) <= 0 && base.getCrystalLength().compareTo(fiveHundred) > 0) {
                    oneThousandSb.append(base.getDeviceNo()).append(",");
                    oneThousandLength++;
                }
                if (base.getCrystalLength().compareTo(twoThousand) <= 0 && base.getCrystalLength().compareTo(oneThousand) > 0) {
                    twoThousandSb.append(base.getDeviceNo()).append(",");
                    twoThousandLength++;
                }
                if (base.getCrystalLength().compareTo(threeThousand) <= 0 && base.getCrystalLength().compareTo(twoThousand) > 0) {
                    threeThousandSb.append(base.getDeviceNo()).append(",");
                    threeThousandLength++;
                }
                if (base.getCrystalLength().compareTo(threeThousand) > 0) {
                    greaterThanThreeThousandSb.append(base.getDeviceNo()).append(",");
                    greaterThanThreeThousandLength++;
                }
            }
            int i = fiveHundredLength + oneThousandLength + twoThousandLength + threeThousandLength + greaterThanThreeThousandLength;
            sb.append(". 等径断线分类(" + i + ")");
            sb.append("\n");
            sb.append("<=500(" + fiveHundredLength + "):" + fiveHundredSb);
            sb.append("\n");
            sb.append("<=1000(" + oneThousandLength + "):" + oneThousandSb);
            sb.append("\n");
            sb.append("<=2000(" + twoThousandLength + "):" + twoThousandSb);
            sb.append("\n");
            sb.append("<=3000(" + threeThousandLength + "):" + threeThousandSb);
            sb.append("\n");
            sb.append(" >3000(" + greaterThanThreeThousandLength + "):" + greaterThanThreeThousandSb);
            sb.append("\n");
        }
        return sb;
    }


    private StringBuilder getCapacityInputStringBuilder() {
        StringBuilder sb = new StringBuilder();
        sb.append("[" + DateUtil.formatDate1(new Date()) + "]当日片区送棒总量:");
        sb.append("\n");
        List<Map<String, Object>> capacityInputMaps = productionPaintingLineService.capacityInputData();
        List<CapacityInputDto> capacityInputDtoList = capacityInputMaps.stream()
                .map(e -> JSON.parseObject(JSONObject.toJSONString(e), CapacityInputDto.class))
                .collect(Collectors.toList());
        for (CapacityInputDto capacityInputDto : capacityInputDtoList) {
            capacityInputDto.setSeries(capacityInputDto.getSeries().substring(0, 2));
        }
        if (CollectionUtils.isNotEmpty(capacityInputDtoList)) {
            Map<String, List<CapacityInputDto>> listMap = capacityInputDtoList.stream().collect(Collectors.groupingBy(CapacityInputDto::getSeries));
            BigDecimal all = BigDecimal.ZERO;
            String series = "";
            String area = "";
            for (Map.Entry<String, List<CapacityInputDto>> entry : listMap.entrySet()) {
                series = entry.getKey();
                List<CapacityInputDto> capacityInputDtoList1 = entry.getValue();
                BigDecimal weight = capacityInputDtoList1.stream()
                        .map(CapacityInputDto::getWeight)
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                CapacityInputDto capacityInputDto = capacityInputDtoList1.get(0);
                area = capacityInputDto.getArea();
                sb.append("拉晶" + area + series + "片区");
                sb.append("\n");
                sb.append(" 累计送检量: " + weight + "Kg");
                sb.append("\n");
                all = all.add(weight);
            }
            sb.append(area + "累计送检量: " + all.setScale(2, BigDecimal.ROUND_HALF_UP) + "Kg");
            sb.append("\n");
            sb.append("\n");
        }

        return sb;
    }

//    /**
//     * 晶彩发送消息
//     */
//    private void robotSeedMessage(String content, String groupName, String source) {
//        try {
//            ArrayList<String> sceneList = new ArrayList<>();
//            ArrayList<String> groupNameList = new ArrayList<>();
//            sceneList.add("prod");
//            groupNameList.add(groupName);
//            SeedMassageDto seedMassageDto = new SeedMassageDto();
//            seedMassageDto.setContent(content);
//            seedMassageDto.setScene(sceneList);
//            seedMassageDto.setGroupName(groupNameList);
//            seedMassageDto.setStatus(SeedMassageEnum.GROUP);
//            seedMassageDto.setSource(source);
//            dingTalkService.seedMassage(seedMassageDto);
//        } catch (JsonProcessingException e) {
//            log.error("error_in_sendEapSignal", e);
//        }
//    }
}
