package cn.iocoder.yudao.module.pms.controller.admin.stat;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.pms.controller.admin.vo.IotDevicePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.vo.IotDeviceRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.vo.IotDeviceSimple;
import cn.iocoder.yudao.module.pms.controller.admin.vo.IotReportDeviceRespVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.IotDeviceDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.IotProductClassifyDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yfclass.IotYfClassifyDO;
import cn.iocoder.yudao.module.pms.dal.mysql.IotDeviceMapper;
import cn.iocoder.yudao.module.pms.dal.mysql.IotProductClassifyMapper;
import cn.iocoder.yudao.module.pms.dal.mysql.yfclass.IotYfClassifyMapper;
import cn.iocoder.yudao.module.pms.service.IotProductClassifyService;
import cn.iocoder.yudao.module.pms.util.IotDeviceConvert;
import cn.iocoder.yudao.module.supplier.dal.dataobject.product.SupplierDO;
import cn.iocoder.yudao.module.supplier.service.product.SupplierService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictDataDO;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import cn.iocoder.yudao.module.system.service.dict.DictDataService;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "报表设备接口")
@RestController
@RequestMapping("/rq/report")
@Validated
@PermitAll
public class IotReportDeviceController {
    private static final ImmutableList<Integer> jt = ImmutableList.<Integer>builder()
            .add(159, 160, 232, 157, 158) // 第一批5个
            .add(107, 119, 106, 108, 19)
            .add(191, 28,30,35)
            .add(85, 90, 177, 144, 31) // 第三批5个
            .build();
    private static final ImmutableList<Integer> rh = ImmutableList.<Integer>builder()
            .add(159, 160, 232, 157, 158) // 第一批5个
            .add(107, 119, 106, 108,191).build();
    private static final ImmutableList<Integer> ry = ImmutableList.<Integer>builder()
            .add(85, 90, 177, 144,19).build();
    private static final ImmutableList<Integer> rd = ImmutableList.<Integer>builder()
            .add(31,28,30,35).build();
    private final DeptApi deptApi;
    private final IotDeviceMapper iotDeviceMapper;
    private final IotProductClassifyMapper iotProductClassifyMapper;
    private final DeptUtil deptUtil;
    @Autowired
    private DeptService deptService;
    @Autowired
    private IotProductClassifyService iotProductClassifyService;
    @Resource
    private SupplierService supplierService;
    @Autowired
    private IotYfClassifyMapper iotYfClassifyMapper;

    public IotReportDeviceController(DeptApi deptApi, IotDeviceMapper iotDeviceMapper, IotProductClassifyMapper iotProductClassifyMapper, DeptUtil deptUtil) {
        this.deptApi = deptApi;
        this.iotDeviceMapper = iotDeviceMapper;
        this.iotProductClassifyMapper = iotProductClassifyMapper;
        this.deptUtil = deptUtil;
    }

    @Operation(summary = "类别统计")
    @GetMapping("/device/type")
    public CommonResult<List<IotDeviceSimple>> getDeviceTypeStat(@Valid IotDevicePageReqVO iotDevicePageReqVO) {
        Set<Long> ids;
        String companyCode;
        if (Objects.isNull(iotDevicePageReqVO.getDeptId())){
            Long loginUserDeptId = SecurityFrameworkUtils.getLoginUserDeptId();
            companyCode = deptUtil.getCompanyCode(loginUserDeptId);
            ids = deptUtil.getDeptIds(companyCode);
        } else {
            companyCode = deptUtil.getCompanyCode(iotDevicePageReqVO.getDeptId());
            ids = deptService.getChildDeptIdListFromCache(iotDevicePageReqVO.getDeptId());
            ids.add(iotDevicePageReqVO.getDeptId());
        }
        IotDevicePageReqVO pageReqVO = new IotDevicePageReqVO();
        pageReqVO.setDeptIds(new ArrayList<>(ids));
        if ("jt".equals(companyCode)) {
            List<IotDeviceSimple> iotDeviceSimples = iotDeviceMapper.selectTypeNumber(pageReqVO, jt);
            AtomicInteger zyj = new AtomicInteger();
            AtomicInteger kyj = new AtomicInteger();
            iotDeviceSimples.forEach(e ->{
                if ("电驱增压机".equals(e.getCategory())||"柴驱增压机".equals(e.getCategory())||"液驱增压机".equals(e.getCategory())) {
                    zyj.addAndGet(Math.toIntExact(e.getValue()==null?0:e.getValue()));
                }
                if ("电驱空压机".equals(e.getCategory())||"柴驱空压机".equals(e.getCategory())||"天然气驱空压机".equals(e.getCategory())) {
                    kyj.addAndGet(Math.toIntExact(e.getValue()==null?0:e.getValue()));
                }
            });
            List<IotDeviceSimple> result = iotDeviceSimples.stream().filter(e -> !"电驱增压机".equals(e.getCategory()) && !"柴驱增压机".equals(e.getCategory()) && !"液驱增压机".equals(e.getCategory())
                    && !"电驱空压机".equals(e.getCategory()) && !"柴驱空压机".equals(e.getCategory()) && !"天然气驱空压机".equals(e.getCategory())).collect(Collectors.toList());
            IotDeviceSimple iotDeviceSimple = new IotDeviceSimple();
            iotDeviceSimple.setCategory("增压机");
            iotDeviceSimple.setValue(zyj.longValue());
            result.add(iotDeviceSimple);
            IotDeviceSimple iotDeviceSimple1 = new IotDeviceSimple();
            iotDeviceSimple1.setCategory("空压机");
            iotDeviceSimple1.setValue(kyj.longValue());
            result.add(iotDeviceSimple1);
            return CommonResult.success((result.stream().sorted(Comparator.comparing(IotDeviceSimple::getValue).reversed())).collect(Collectors.toList()));
        } else if ("rh".equals(companyCode)) {
            List<IotDeviceSimple> iotDeviceSimples = iotDeviceMapper.selectTypeNumber(pageReqVO, rh);
            AtomicInteger zyj = new AtomicInteger();
            AtomicInteger kyj = new AtomicInteger();
            iotDeviceSimples.forEach(e ->{
                if ("电驱增压机".equals(e.getCategory())||"柴驱增压机".equals(e.getCategory())||"液驱增压机".equals(e.getCategory())) {
                    zyj.addAndGet(Math.toIntExact(e.getValue()==null?0:e.getValue()));
                }
                if ("电驱空压机".equals(e.getCategory())||"柴驱空压机".equals(e.getCategory())||"天然气驱空压机".equals(e.getCategory())) {
                    kyj.addAndGet(Math.toIntExact(e.getValue()==null?0:e.getValue()));
                }
            });
            List<IotDeviceSimple> result = iotDeviceSimples.stream().filter(e -> !"电驱增压机".equals(e.getCategory()) && !"柴驱增压机".equals(e.getCategory()) && !"液驱增压机".equals(e.getCategory())
                    && !"电驱空压机".equals(e.getCategory()) && !"柴驱空压机".equals(e.getCategory()) && !"天然气驱空压机".equals(e.getCategory())).collect(Collectors.toList());
            IotDeviceSimple iotDeviceSimple = new IotDeviceSimple();
            iotDeviceSimple.setCategory("增压机");
            iotDeviceSimple.setValue(zyj.longValue());
            result.add(iotDeviceSimple);
            IotDeviceSimple iotDeviceSimple1 = new IotDeviceSimple();
            iotDeviceSimple1.setCategory("空压机");
            iotDeviceSimple1.setValue(kyj.longValue());
            result.add(iotDeviceSimple1);
            return CommonResult.success((result.stream().sorted(Comparator.comparing(IotDeviceSimple::getValue).reversed())).collect(Collectors.toList()));
        } else if ("ry".equals(companyCode)) {
            List<IotDeviceSimple> iotDeviceSimples = iotDeviceMapper.selectTypeNumber(pageReqVO, ry);
            return CommonResult.success((iotDeviceSimples.stream().sorted(Comparator.comparing(IotDeviceSimple::getValue).reversed())).collect(Collectors.toList()));
        } else if ("rd".equals(companyCode)) {
            // 设置 新设备分类 数量统计
            List<IotDeviceDO> devices = iotDeviceMapper.selectListAlone(pageReqVO);
            Map<String, Long> assetCountMap = devices.stream()
                    .filter(device -> StringUtils.isNotBlank(device.getYfClass()))
                    .collect(Collectors.groupingBy(IotDeviceDO::getYfClass, Collectors.counting()));

            // 获取新分类字典（主设备类别）
            List<DictDataDO> newClassifyDictData = dictDataService.getDictDataListByDictType("rq_iot_rd_device_new_classify");
            Set<String> mainClassifyCodes = newClassifyDictData.stream()
                    .map(DictDataDO::getValue)
                    .filter(StringUtils::isNotBlank)
                    .collect(Collectors.toSet());

            List<IotDeviceSimple> result = assetCountMap.entrySet().stream()
                    .filter(e -> StringUtils.isNotBlank(e.getKey()))
                    .map(e -> {
                        String code = StringUtils.substringAfterLast(e.getKey(), ",");
                        List<IotYfClassifyDO> yfClassList = iotYfClassifyMapper.selectList("code", code);
                        if (CollUtil.isEmpty(yfClassList)) {
                            return null;
                        }
                        IotDeviceSimple simple = new IotDeviceSimple();
                        simple.setCategory(yfClassList.get(0).getName());
                        simple.setValue(e.getValue());
                        simple.setDeviceCode(mainClassifyCodes.contains(code) ? "top" : "");
                        return simple;
                    })
                    .filter(Objects::nonNull)
                    .sorted((a, b) -> {
                        boolean aTop = "top".equals(a.getDeviceCode());
                        boolean bTop = "top".equals(b.getDeviceCode());
                        if (aTop != bTop) {
                            return aTop ? -1 : 1;
                        }
                        return Long.compare(b.getValue(), a.getValue());
                    })
                    .collect(Collectors.toList());

            return CommonResult.success(result);
            // List<IotDeviceSimple> iotDeviceSimples = iotDeviceMapper.selectTypeNumber(pageReqVO, rd);
            // return CommonResult.success((iotDeviceSimples.stream().sorted(Comparator.comparing(IotDeviceSimple::getValue).reversed())).collect(Collectors.toList()));
        } else {
            return CommonResult.success(new ArrayList<>());
        }
//        List<IotDeviceDO> iotDeviceDOS = iotDeviceMapper.selectList(pageReqVO);
//        LinkedList<Map<String, Object>> results = new LinkedList<>();
//        List<Map.Entry<Long, Long>> collect = iotDeviceDOS.stream().collect(Collectors.groupingBy(IotDeviceDO::getAssetClass, Collectors.counting())).entrySet().stream().sorted(Map.Entry.<Long, Long>comparingByValue().reversed()).collect(Collectors.toList());
//        collect.forEach(e->{
//            IotProductClassifyDO iotProductClassifyDO = iotProductClassifyMapper.selectById(e.getKey());
//            if ("jt".equals(companyCode)) {
//                if (jt.contains(iotProductClassifyDO.getName())||"空压机".equals(iotProductClassifyDO.getName())||iotProductClassifyDO.getName().contains("驱增压机")) {
//                    Map<String, Object> map = new HashMap<>(4);
//                    map.put("category", iotProductClassifyDO.getName());
//                    map.put("value", e.getValue());
//                    results.add(map);
//                }
//            } else if ("rh".equals(companyCode)) {
//                if (rh.contains(iotProductClassifyDO.getName())||"空压机".equals(iotProductClassifyDO.getName())||iotProductClassifyDO.getName().contains("驱增压机")){
//                    Map<String, Object> map = new HashMap<>(4);
//                    map.put("category", iotProductClassifyDO.getName());
//                    map.put("value", e.getValue());
//                    results.add(map);
//                }
//            } else if ("ry".equals(companyCode)) {
//                if (ry.contains(iotProductClassifyDO.getName())){
//                    Map<String, Object> map = new HashMap<>(4);
//                    map.put("category", iotProductClassifyDO.getName());
//                    map.put("value", e.getValue());
//                    results.add(map);
//                }
//            } else if ("rd".equals(companyCode)) {
//                if (rd.contains(iotProductClassifyDO.getName())||iotProductClassifyDO.getName().contains("压裂泵车")){
//                    Map<String, Object> map = new HashMap<>(4);
//                    map.put("category", iotProductClassifyDO.getName());
//                    map.put("value", e.getValue());
//                    results.add(map);
//                }
//            }
//        });
//        return CommonResult.success(results);
    }


    @Operation(summary = "获取设备数量")
    @GetMapping("/device/count")
    public CommonResult<Long> getDeviceCount(@Valid IotDevicePageReqVO iotDevicePageReqVO) {
        Set<Long> ids;
        if (Objects.isNull(iotDevicePageReqVO.getDeptId())){
            Long loginUserDeptId = SecurityFrameworkUtils.getLoginUserDeptId();
            String companyCode = deptUtil.getCompanyCode(loginUserDeptId);
            ids = deptUtil.getDeptIds(companyCode);
        } else {
            ids = deptService.getChildDeptIdListFromCache(iotDevicePageReqVO.getDeptId());
            ids.add(iotDevicePageReqVO.getDeptId());
        }
        Long l = iotDeviceMapper.selectCountByParam(ids,iotDevicePageReqVO);
        return CommonResult.success(l);
    }

    @Resource
    private DictDataService dictDataService;
    @GetMapping("/device/status")
    public CommonResult<List<Map<String, Object>>> getDeviceStatusStat(@Valid IotDevicePageReqVO iotDevicePageReqVO) {
        Set<Long> ids;
        if (Objects.isNull(iotDevicePageReqVO.getDeptId())){
            Long loginUserDeptId = SecurityFrameworkUtils.getLoginUserDeptId();
            String companyCode = deptUtil.getCompanyCode(loginUserDeptId);
            ids = deptUtil.getDeptIds(companyCode);
        } else {
            ids = deptService.getChildDeptIdListFromCache(iotDevicePageReqVO.getDeptId());
            ids.add(iotDevicePageReqVO.getDeptId());
        }
        IotDevicePageReqVO pageReqVO = new IotDevicePageReqVO();
        pageReqVO.setDeptIds(new ArrayList<>(ids));
        List<IotDeviceDO> iotDeviceDOS = iotDeviceMapper.selectList(pageReqVO);
        Map<String, Long> order = iotDeviceDOS.stream().collect(Collectors.groupingBy(IotDeviceDO::getDeviceStatus, Collectors.counting()));
        List<Map<String, Object>> results = new ArrayList<>();
        List<DictDataDO> pmsDeviceStatus = dictDataService.getDictDataListByDictType("pms_device_status");
        order.forEach((k,v)->{
            pmsDeviceStatus.stream().filter(e -> e.getValue().equals(k)).findFirst().ifPresent(e -> {
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("name", e.getLabel());
                resultMap.put("value", v);
                results.add(resultMap);
            });
        });
        return CommonResult.success(results);
    }

    @GetMapping("/device/page")
    @Operation(summary = "获得设备报表分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-device:query')")
    public CommonResult<PageResult<IotDeviceRespVO>> getIotDevicePage(@Valid IotDevicePageReqVO pageReqVO) {
        Set<Long> ids;
        if (Objects.isNull(pageReqVO.getDeptId())){
            Long loginUserDeptId = SecurityFrameworkUtils.getLoginUserDeptId();
            String companyCode = deptUtil.getCompanyCode(loginUserDeptId);
            ids = deptUtil.getDeptIds(companyCode);
        } else {
            ids = deptService.getChildDeptIdListFromCache(pageReqVO.getDeptId());
            ids.add(pageReqVO.getDeptId());
        }
        PageResult<IotDeviceDO> pageResult = iotDeviceMapper.selectPageReport(pageReqVO, ids);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal()));
        }
        Map<Long, DeptDO> deptMap = deptService.getDeptMap(
                convertList(pageResult.getList(), IotDeviceDO::getDeptId));
        List<IotDeviceRespVO> iotDeviceRespVOS = IotDeviceConvert.INSTANCE.convertList(pageResult.getList(), deptMap);
        iotDeviceRespVOS.forEach(e ->{
            if (Objects.nonNull(e.getAssetClass())) {
                IotProductClassifyDO iotProductClassify = iotProductClassifyService.getIotProductClassify(e.getAssetClass());
                e.setAssetClassName(Objects.nonNull(iotProductClassify)?iotProductClassify.getName():"");
            }
            SupplierDO supplierDO = supplierService.get(e.getManufacturerId());
            if (Objects.nonNull(supplierDO)) {
                e.setManufacturer(supplierDO.getName());
            }
            DeptRespDTO dept = deptApi.getDept(e.getDeptId());
            if (Objects.nonNull(dept)) {
                if ("1".equals(dept.getType())) {
                    e.setCompany(dept.getName());
                } else if ("2".equals(dept.getType())) {
                    e.setProject(dept.getName());
                    DeptRespDTO dept1 = deptApi.getDept(dept.getParentId());
                    if (Objects.nonNull(dept1)) {
                        e.setCompany(dept1.getName());
                    }
                } else if ("3".equals(dept.getType())) {
                    e.setDeptName(dept.getName());
                    DeptRespDTO dept1 = deptApi.getDept(dept.getParentId());
                    if (Objects.nonNull(dept1)) {
                        e.setProject(dept1.getName());
                        DeptRespDTO dept2 = deptApi.getDept(dept1.getParentId());
                        if (Objects.nonNull(dept2)) {
                            e.setCompany(dept2.getName());
                        }
                    }
                }
            }
        });
        return success(new PageResult<>(iotDeviceRespVOS, pageResult.getTotal()));
    }


    @GetMapping("/dept/{deptId}")
    public CommonResult<String> getCompanyCode(@PathVariable("deptId") Long deptId) {
        String companyCode = deptUtil.getCompanyCode(deptId);
        return CommonResult.success(companyCode);
    }

//    @PostMapping("/export-excel")
//    @Operation(summary = "导出设备台账 Excel")
//    @PreAuthorize("@ss.hasPermission('rq:iot-device:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportIotDeviceExcel(@RequestBody IotDevicePageReqVO pageReqVO,
//                                     HttpServletResponse response) throws IOException {
//
//    }

    @GetMapping("/export-excel")
    public void downloadExcelByStringHeader(@Valid IotDevicePageReqVO pageReqVO,
            HttpServletResponse response) throws IOException {
        String includeFields = pageReqVO.getExportFields();
        Set<Long> ids;
        if (Objects.isNull(pageReqVO.getDeptId())){
            Long loginUserDeptId = SecurityFrameworkUtils.getLoginUserDeptId();
            String companyCode = deptUtil.getCompanyCode(loginUserDeptId);
            ids = deptUtil.getDeptIds(companyCode);
        } else {
            ids = deptService.getChildDeptIdListFromCache(pageReqVO.getDeptId());
            ids.add(pageReqVO.getDeptId());
        }
        pageReqVO.setDeptIds(new ArrayList<>(ids));
        List<IotDeviceRespVO> iotDeviceRespVOS = iotDeviceMapper.selectExport(pageReqVO);
        List<IotReportDeviceRespVO> bean = BeanUtils.toBean(iotDeviceRespVOS, IotReportDeviceRespVO.class);
        ExcelUtils.write(response, "设备报表.xls", "数据", IotReportDeviceRespVO.class, bean);
    }

    // Excel下载响应配置（解决中文乱码、指定文件类型）
    private void setExcelDownloadResponse(HttpServletResponse response, String fileName) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String encodeFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + encodeFileName + ".xlsx");
    }

    @GetMapping("/rd/whl")
    @PermitAll
    public CommonResult<ImmutableMap<String, ImmutableMap<String, Number>>> getWhl() {
        Set<Long> ids;
//        if (Objects.nonNull(pageReqVO.getDeptId())) {
        ids = deptService.getChildDeptIdListFromCache(163L);
        ids.add(163L);
        IotDevicePageReqVO iotDevicePageReqVO = new IotDevicePageReqVO();
        iotDevicePageReqVO.setDeptIds(new ArrayList<>(ids));
        iotDevicePageReqVO.setRemark("A");
        Long totalA = iotDeviceMapper.getTotalA(iotDevicePageReqVO);
        List<Long> failureCount = iotDeviceMapper.getFailureCount(iotDevicePageReqVO);
        List<Long> repairCount = iotDeviceMapper.getRepairCount(iotDevicePageReqVO);
        List<Long> combined = Stream.concat(failureCount.stream(), repairCount.stream())
                .distinct()
                .collect(Collectors.toList());
        int size = combined.size();
        BigDecimal total = BigDecimal.valueOf(totalA);
        BigDecimal sizeA = BigDecimal.valueOf(size);
        BigDecimal diff = total.subtract(sizeA);
        // 计算百分比并保留两位小数
        BigDecimal percentA = diff.multiply(BigDecimal.valueOf(100))
                .divide(total, 2, RoundingMode.HALF_UP);

        iotDevicePageReqVO.setRemark("B");
        Long totalB = iotDeviceMapper.getTotalA(iotDevicePageReqVO);
        List<Long> failureCountB = iotDeviceMapper.getFailureCount(iotDevicePageReqVO);
        List<Long> repairCountB = iotDeviceMapper.getRepairCount(iotDevicePageReqVO);
        List<Long> combinedB = Stream.concat(failureCountB.stream(), repairCountB.stream())
                .distinct()
                .collect(Collectors.toList());
        int sizeB = combinedB.size();

        BigDecimal totalBB = BigDecimal.valueOf(totalB);
        BigDecimal sizeBB = BigDecimal.valueOf(sizeB);
        BigDecimal diffB = totalBB.subtract(sizeBB);
        // 计算百分比并保留两位小数
        BigDecimal percentB = diffB.multiply(BigDecimal.valueOf(100))
                .divide(totalBB, 2, RoundingMode.HALF_UP);


        iotDevicePageReqVO.setRemark("C");
        Long totalC = iotDeviceMapper.getTotalA(iotDevicePageReqVO);
        List<Long> failureCountC = iotDeviceMapper.getFailureCount(iotDevicePageReqVO);
        List<Long> repairCountC = iotDeviceMapper.getRepairCount(iotDevicePageReqVO);

        List<Long> combinedC = Stream.concat(failureCountC.stream(), repairCountC.stream())
                .distinct()
                .collect(Collectors.toList());
        int sizeC = combinedC.size();

        BigDecimal totalCC = BigDecimal.valueOf(totalC);
        BigDecimal sizeCC = BigDecimal.valueOf(sizeC);
        BigDecimal diffC = totalCC.subtract(sizeCC);
        // 计算百分比并保留两位小数
        BigDecimal percentC = diffC.multiply(BigDecimal.valueOf(100))
                .divide(totalCC, 2, RoundingMode.HALF_UP);


        return CommonResult.success(ImmutableMap.of("a",ImmutableMap.of("percent", percentA,"total", totalA),
                "b",ImmutableMap.of("percent", percentB,"total",totalB),"c",ImmutableMap.of("percent", percentC,"total", totalC)));
    }
}
