package com.usky.fire.service.impl;

import cn.hutool.core.date.DateUnit;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.fire.domain.*;
import com.usky.fire.mapper.DemInspectListMapper;
import com.usky.fire.service.DemInspectListService;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.fire.service.util.OnlineMethod;
import com.usky.fire.service.vo.DemInspectExportVO;
import com.usky.fire.service.vo.InspectDetailVO;
import com.usky.fire.service.vo.InspectHandleVO;
import com.usky.fire.service.vo.InspectManagerVO;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.usky.fire.service.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * <p>
 * 督察单 服务实现类
 * </p>
 *
 * @author ZYJ
 * @since 2022-12-28
 */
@Service
public class DemInspectListServiceImpl extends AbstractCrudService<DemInspectListMapper, DemInspectList> implements DemInspectListService {
    @Autowired
    private AlarmFireService alarmFireService;

    @Autowired
    private BaseUserCompanyService baseUserCompanyService;

    @Autowired
    private BaseCompanyService baseCompanyService;

    @Autowired
    private BaseDeviceService baseDeviceService;

    @Autowired
    private BaseCompanyPersonService baseCompanyPersonService;

    @Override
    public CommonPage<InspectDetailVO> demInspectList(String inspectCode, String companyName, String deviceCode, Integer handlingStatus, Integer pageNum, Integer pageSize){
        IPage<DemInspectList> page = new Page<>(pageNum, pageSize);
        List<InspectDetailVO> list = new ArrayList<>();
        List<BaseCompany> companyIdList = new ArrayList<>();
        LambdaQueryWrapper<BaseUserCompany> QueryWrapper = Wrappers.lambdaQuery();
        QueryWrapper.select(BaseUserCompany::getCompanyId)
                .eq(BaseUserCompany::getUserId, SecurityUtils.getUserId());
        List<BaseUserCompany> comoanyList = baseUserCompanyService.list(QueryWrapper);

        LambdaQueryWrapper<BaseCompany> QueryWrapperCompany = Wrappers.lambdaQuery();
        if(StringUtils.isNotBlank(companyName)){
            QueryWrapperCompany.select(BaseCompany::getCompanyId)
                    .eq(BaseCompany::getEnable, 0)
                    .like(BaseCompany::getCompanyName, companyName);
            companyIdList = baseCompanyService.list(QueryWrapperCompany);
        }

        if(CollectionUtils.isNotEmpty(comoanyList)){
            List<String> companyList = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(companyIdList)){
                for(int j=0;j<companyIdList.size();j++){
                    companyList.add(companyIdList.get(j).getCompanyId());
                }
            }

            LambdaQueryWrapper<DemInspectList> QueryWrapper1 = Wrappers.lambdaQuery();
            if(StringUtils.isNotBlank(companyName)){
                if(CollectionUtils.isNotEmpty(companyList)){
                    QueryWrapper1.in(DemInspectList::getCompanyId,companyList)
                            .like(StringUtils.isNotBlank(inspectCode), DemInspectList::getInspectCode, inspectCode)
                            .like(StringUtils.isNotBlank(deviceCode), DemInspectList::getDeviceId, deviceCode)
                            .eq(null!=handlingStatus, DemInspectList::getHandlingStatus, handlingStatus);
                    QueryWrapper1.orderByDesc(DemInspectList::getId);
                }else{
                    companyList.add(null);
                    QueryWrapper1.like(StringUtils.isNotBlank(inspectCode), DemInspectList::getInspectCode, inspectCode)
                            .like(StringUtils.isNotBlank(deviceCode), DemInspectList::getDeviceId, deviceCode)
                            .eq(null!=handlingStatus, DemInspectList::getHandlingStatus, handlingStatus);
                    QueryWrapper1.orderByDesc(DemInspectList::getId);
                }
            }
            else{
                QueryWrapper1.like(StringUtils.isNotBlank(inspectCode), DemInspectList::getInspectCode, inspectCode)
                        .like(StringUtils.isNotBlank(deviceCode), DemInspectList::getDeviceId, deviceCode)
                        .eq(null!=handlingStatus, DemInspectList::getHandlingStatus, handlingStatus);
                QueryWrapper1.orderByDesc(DemInspectList::getId);
            }
            page = this.page(page, QueryWrapper1);
            if(page.getTotal() > 0){
                for(int k=0;k<page.getRecords().size();k++){
                    InspectDetailVO inspectDetailVO = new InspectDetailVO();
                    inspectDetailVO.setInspectCode(page.getRecords().get(k).getInspectCode());
                    inspectDetailVO.setDeviceCode(page.getRecords().get(k).getDeviceId());
                    //设备名称和安装位置
                    LambdaQueryWrapper<BaseDevice> QueryWrapperDevice = Wrappers.lambdaQuery();
                    QueryWrapperDevice.select(BaseDevice::getDeviceCode,BaseDevice::getDeviceName,BaseDevice::getInstallAddress)
                            .eq(BaseDevice::getDeviceCode, page.getRecords().get(k).getDeviceId());
                    List<BaseDevice> deviceList = baseDeviceService.list(QueryWrapperDevice);
                    if(CollectionUtils.isNotEmpty(deviceList)){
                        inspectDetailVO.setDeviceName(deviceList.get(0).getDeviceName());
                        inspectDetailVO.setInstallAddress(deviceList.get(0).getInstallAddress());
                    }
                    //设备报告内容和报告时间
                    LambdaQueryWrapper<AlarmFire> QueryWrapperAlarm = Wrappers.lambdaQuery();
                    QueryWrapperAlarm.select(AlarmFire::getAlarmTime, AlarmFire::getAttributeValue2, AlarmFire::getAttributeValue4)
                            .eq(AlarmFire::getId, page.getRecords().get(k).getAlarmId());
                    List<AlarmFire> alarmFireList = alarmFireService.list(QueryWrapperAlarm);
                    if(CollectionUtils.isNotEmpty(alarmFireList)){
                        inspectDetailVO.setAlarmTime(alarmFireList.get(0).getAlarmTime());
                        inspectDetailVO.setDeviceContent(alarmFireList.get(0).getAttributeValue2()+" "+alarmFireList.get(0).getAttributeValue4());
                    }
                    LambdaQueryWrapper<BaseCompanyPerson> queryWrapperPerson = Wrappers.lambdaQuery();
                    queryWrapperPerson.select(BaseCompanyPerson::getCompanyName, BaseCompanyPerson::getDelegateName, BaseCompanyPerson::getDelegatePhone,
                            BaseCompanyPerson::getFireDutyName, BaseCompanyPerson::getFireDutyPhone,
                            BaseCompanyPerson::getFireManageName, BaseCompanyPerson::getFireManagePhone)
                            .in(BaseCompanyPerson::getCompanyId, page.getRecords().get(k).getCompanyId());
                    List<BaseCompanyPerson> companyPersonList = baseCompanyPersonService.list(queryWrapperPerson);
                    if(CollectionUtils.isNotEmpty(companyPersonList)){
                        inspectDetailVO.setCompanyName(companyPersonList.get(0).getCompanyName());
                        inspectDetailVO.setDelegateName(companyPersonList.get(0).getDelegateName());
                        inspectDetailVO.setDelegatePhone(companyPersonList.get(0).getDelegatePhone());
                        inspectDetailVO.setFireDutyName(companyPersonList.get(0).getFireDutyName());
                        inspectDetailVO.setFireDutyPhone(companyPersonList.get(0).getFireDutyPhone());
                        inspectDetailVO.setFireManageName(companyPersonList.get(0).getFireManageName());
                        inspectDetailVO.setFireManagePhone(companyPersonList.get(0).getFireManagePhone());
                    }
                    inspectDetailVO.setHandlingStatus(page.getRecords().get(k).getHandlingStatus());
                    inspectDetailVO.setInspectTime(page.getRecords().get(k).getCreateTime());
                    list.add(inspectDetailVO);
                }
            }
        }
        return new CommonPage<>(list, page.getTotal(), pageSize, pageNum);
    }

    @Override
    public List<DemInspectExportVO> demInspectListExport(String inspectCode, String companyName, String deviceCode, Integer handlingStatus){
        List<DemInspectExportVO> list = new ArrayList<>();
        List<BaseCompany> companyIdList = new ArrayList<>();
        LambdaQueryWrapper<BaseUserCompany> QueryWrapper = Wrappers.lambdaQuery();
        QueryWrapper.select(BaseUserCompany::getCompanyId)
                .eq(BaseUserCompany::getUserId, SecurityUtils.getUserId());
        List<BaseUserCompany> comoanyList = baseUserCompanyService.list(QueryWrapper);

        LambdaQueryWrapper<BaseCompany> QueryWrapperCompany = Wrappers.lambdaQuery();
        if(StringUtils.isNotBlank(companyName)){
            QueryWrapperCompany.select(BaseCompany::getCompanyId)
                    .eq(BaseCompany::getEnable, 0)
                    .like(BaseCompany::getCompanyName, companyName);
            companyIdList = baseCompanyService.list(QueryWrapperCompany);
        }

        if(CollectionUtils.isNotEmpty(comoanyList)){
            List<String> companyList = new ArrayList<>();
            for(int j=0;j<companyIdList.size();j++){
                companyList.add(companyIdList.get(j).getCompanyId());
            }


            LambdaQueryWrapper<DemInspectList> QueryWrapper1 = Wrappers.lambdaQuery();
            if(StringUtils.isNotBlank(companyName)){
                if(CollectionUtils.isNotEmpty(companyList)){

                }else{
                    companyList.add(null);
                }
                QueryWrapper1.in(DemInspectList::getCompanyId,companyList)
                        .like(StringUtils.isNotBlank(inspectCode), DemInspectList::getInspectCode, inspectCode)
                        .like(StringUtils.isNotBlank(deviceCode), DemInspectList::getDeviceId, deviceCode)
                        .eq(handlingStatus != null, DemInspectList::getHandlingStatus, handlingStatus);
                QueryWrapper1.orderByDesc(DemInspectList::getId);
            }
            else{
                QueryWrapper1.like(StringUtils.isNotBlank(inspectCode), DemInspectList::getInspectCode, inspectCode)
                        .like(StringUtils.isNotBlank(deviceCode), DemInspectList::getDeviceId, deviceCode)
                        .eq(handlingStatus != null, DemInspectList::getHandlingStatus, handlingStatus);
                QueryWrapper1.orderByDesc(DemInspectList::getId).last("limit 1000");
            }
            List<DemInspectList> list1 = this.list(QueryWrapper1);
            if(CollectionUtils.isNotEmpty(list1)){
                for(int k=0;k<list1.size();k++){
                    DemInspectExportVO demInspectExportVO = new DemInspectExportVO();
                    demInspectExportVO.setInspectCode(list1.get(k).getInspectCode());
                    demInspectExportVO.setDeviceCode(list1.get(k).getDeviceId());
                    //设备名称和安装位置
                    LambdaQueryWrapper<BaseDevice> QueryWrapperDevice = Wrappers.lambdaQuery();
                    QueryWrapperDevice.select(BaseDevice::getDeviceCode,BaseDevice::getDeviceName,BaseDevice::getInstallAddress)
                            .eq(BaseDevice::getDeviceCode, list1.get(k).getDeviceId());
                    List<BaseDevice> deviceList = baseDeviceService.list(QueryWrapperDevice);
                    demInspectExportVO.setDeviceName(deviceList.get(0).getDeviceName());
                    demInspectExportVO.setInstallAddress(deviceList.get(0).getInstallAddress());
                    //设备报告内容和报告时间
                    LambdaQueryWrapper<AlarmFire> QueryWrapperAlarm = Wrappers.lambdaQuery();
                    QueryWrapperAlarm.select(AlarmFire::getAlarmTime, AlarmFire::getAttributeValue2, AlarmFire::getAttributeValue4)
                            .eq(AlarmFire::getId, list1.get(k).getAlarmId());
                    List<AlarmFire> alarmFireList = alarmFireService.list(QueryWrapperAlarm);
                    demInspectExportVO.setAlarmTime(alarmFireList.get(0).getAlarmTime());
                    demInspectExportVO.setDeviceContent(alarmFireList.get(0).getAttributeValue2()+" "+alarmFireList.get(0).getAttributeValue4());

                    LambdaQueryWrapper<BaseCompanyPerson> queryWrapperPerson = Wrappers.lambdaQuery();
                    queryWrapperPerson.select(BaseCompanyPerson::getCompanyName, BaseCompanyPerson::getDelegateName, BaseCompanyPerson::getDelegatePhone,
                            BaseCompanyPerson::getFireDutyName, BaseCompanyPerson::getFireDutyPhone,
                            BaseCompanyPerson::getFireManageName, BaseCompanyPerson::getFireManagePhone)
                            .in(BaseCompanyPerson::getCompanyId, list1.get(k).getCompanyId());
                    List<BaseCompanyPerson> companyPersonList = baseCompanyPersonService.list(queryWrapperPerson);
                    demInspectExportVO.setCompanyName(companyPersonList.get(0).getCompanyName());
                    demInspectExportVO.setDelegateName(companyPersonList.get(0).getDelegateName());
                    demInspectExportVO.setDelegatePhone(companyPersonList.get(0).getDelegatePhone());
                    demInspectExportVO.setFireDutyName(companyPersonList.get(0).getFireDutyName());
                    demInspectExportVO.setFireDutyPhone(companyPersonList.get(0).getFireDutyPhone());
                    demInspectExportVO.setFireManageName(companyPersonList.get(0).getFireManageName());
                    demInspectExportVO.setFireManagePhone(companyPersonList.get(0).getFireManagePhone());

                    demInspectExportVO.setHandlingStatus(list1.get(k).getHandlingStatus());
                    demInspectExportVO.setInspectTime(list1.get(k).getCreateTime());
                    demInspectExportVO.setHandlingRecord(null);
                    list.add(demInspectExportVO);
                }
            }
        }

        return list;
    }

    @Override
    public CommonPage<InspectManagerVO> inspectManageList(String inspectCode, String deviceCode, Integer handlingStatus, Integer pageNum, Integer pageSize){
        IPage<DemInspectList> page = new Page<>(pageNum, pageSize);
        List<InspectManagerVO> list = new ArrayList<>();
        LambdaQueryWrapper<BaseUserCompany> QueryWrapper = Wrappers.lambdaQuery();
        QueryWrapper.select(BaseUserCompany::getCompanyId)
                .eq(BaseUserCompany::getUserId, SecurityUtils.getUserId());
        List<BaseUserCompany> comoanyList = baseUserCompanyService.list(QueryWrapper);

        if(CollectionUtils.isNotEmpty(comoanyList)){
            List<String> companyList = new ArrayList<>();
            for(int i=0;i<comoanyList.size();i++){
                companyList.add(comoanyList.get(i).getCompanyId());
            }

            LambdaQueryWrapper<DemInspectList> QueryWrapper1 = Wrappers.lambdaQuery();
            QueryWrapper1.in(DemInspectList::getCompanyId,companyList)
                    .eq(StringUtils.isNotBlank(inspectCode), DemInspectList::getInspectCode, inspectCode)
                    .eq(StringUtils.isNotBlank(deviceCode), DemInspectList::getDeviceId, deviceCode)
                    .eq(handlingStatus != null, DemInspectList::getHandlingStatus, handlingStatus);
            QueryWrapper1.orderByDesc(DemInspectList::getId);
            page = this.page(page, QueryWrapper1);
            if(page.getTotal() > 0){
                for(int k=0;k<page.getRecords().size();k++){
                    InspectManagerVO inspectManagerVO = new InspectManagerVO();
                    inspectManagerVO.setInspectCode(page.getRecords().get(k).getInspectCode());
                    inspectManagerVO.setInspectTime(page.getRecords().get(k).getCreateTime());
                    inspectManagerVO.setInspectRequiredTime(page.getRecords().get(k).getCreateTime().plusMinutes(30));
                    inspectManagerVO.setCreater(page.getRecords().get(k).getCreator());
                    inspectManagerVO.setDeviceCode(page.getRecords().get(k).getDeviceId());
                    //设备名称
                    LambdaQueryWrapper<BaseDevice> QueryWrapperDevice = Wrappers.lambdaQuery();
                    QueryWrapperDevice.select(BaseDevice::getDeviceCode,BaseDevice::getDeviceName,BaseDevice::getInstallAddress)
                            .eq(BaseDevice::getDeviceCode, page.getRecords().get(k).getDeviceId());
                    List<BaseDevice> deviceList = baseDeviceService.list(QueryWrapperDevice);
                    inspectManagerVO.setDeviceName(deviceList.get(0).getDeviceName());
                    //设备报告内容
                    LambdaQueryWrapper<AlarmFire> QueryWrapperAlarm = Wrappers.lambdaQuery();
                    QueryWrapperAlarm.select(AlarmFire::getAlarmTime, AlarmFire::getAttributeValue2, AlarmFire::getAttributeValue4)
                            .eq(AlarmFire::getId, page.getRecords().get(k).getAlarmId());
                    List<AlarmFire> alarmFireList = alarmFireService.list(QueryWrapperAlarm);
                    inspectManagerVO.setDeviceContent(alarmFireList.get(0).getAttributeValue2()+" "+alarmFireList.get(0).getAttributeValue4());
                    inspectManagerVO.setHandlingStatus(page.getRecords().get(k).getHandlingStatus());
                    inspectManagerVO.setHandlingTime(page.getRecords().get(k).getHandlingTime());
                    list.add(inspectManagerVO);
                }
            }
        }
        return new CommonPage<>(list, page.getTotal(), pageSize, pageNum);
    }

    @Override
    public Map<String,Object> inspectStatistics(String startDate, String endDate){
        LambdaQueryWrapper<BaseUserCompany> QueryWrapper = Wrappers.lambdaQuery();
        QueryWrapper.select(BaseUserCompany::getCompanyId)
                .eq(BaseUserCompany::getUserId,SecurityUtils.getUserId());
        List<BaseUserCompany> comoanyList = baseUserCompanyService.list(QueryWrapper);
        int untreated = 0; //待处置
        int processed = 0; //已处置
        int current24Processed = 0; //过去24小时处置数
        int overtimeUntreated = 0; //超时未处置
        int count = 0; //督察单总数
        String disposalRate = "0.00%";  //督察单处置率
        if(CollectionUtils.isNotEmpty(comoanyList)){
            List<String> companyList = new ArrayList<>();
            for(int i=0;i<comoanyList.size();i++) {
                companyList.add(comoanyList.get(i).getCompanyId());
            }
                
            untreated = this.inspectCount(companyList, 2, startDate, endDate);
            processed = this.inspectCount(companyList, 1, startDate, endDate);
            current24Processed = this.inspectCount(companyList, 1, LocalDateTime.now().minusHours(24).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            overtimeUntreated = this.inspectCount(companyList, 3, startDate, endDate);
            count = untreated + processed + overtimeUntreated;
            disposalRate = OnlineMethod.myPercent(processed,count);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("current24Processed",current24Processed);
        map.put("processed",processed);
        map.put("untreated",untreated);
        map.put("overtimeUntreated",overtimeUntreated);
        map.put("disposalRate",disposalRate);

        return map;
    }

    @Override
    public Integer inspectCount(List<String> companyIdList, Integer handlingStatus, String startDate, String endDate){
        LambdaQueryWrapper<DemInspectList> QueryWrapper = Wrappers.lambdaQuery();
        if(companyIdList.size() > 0){
            QueryWrapper.in(CollectionUtils.isNotEmpty(companyIdList), DemInspectList::getCompanyId, companyIdList);
        }
        QueryWrapper.eq(handlingStatus != null, DemInspectList::getHandlingStatus, handlingStatus)
                .between(StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate), DemInspectList::getCreateTime, startDate, endDate);
        int total = this.count(QueryWrapper);
        return total;
    }

    @Override
    public List<InspectHandleVO> inspectInfo(String inspectCode){
        List<InspectHandleVO> list = new ArrayList<>();

        LambdaQueryWrapper<DemInspectList> QueryWrapper = Wrappers.lambdaQuery();
        QueryWrapper.select(DemInspectList::getHandlingTime, DemInspectList::getDeviceStatus, DemInspectList::getHandler, DemInspectList::getHandlerPhone,
                DemInspectList::getHandlingContent, DemInspectList::getSitePhoto1, DemInspectList::getSitePhoto2, DemInspectList::getSitePhoto3)
                .eq(DemInspectList::getInspectCode, inspectCode);
        List<DemInspectList> list1 = this.list(QueryWrapper);
        if(list1.size() > 0){
            InspectHandleVO inspectHandleVO = new InspectHandleVO();
            inspectHandleVO.setHandlingTime(list1.get(0).getHandlingTime());
            inspectHandleVO.setDeviceStatus(list1.get(0).getDeviceStatus());
            inspectHandleVO.setHandler(list1.get(0).getHandler());
            inspectHandleVO.setHandlerPhone(list1.get(0).getHandlerPhone());
            inspectHandleVO.setHandlingContent(list1.get(0).getHandlingContent());
            inspectHandleVO.setSitePhoto1(list1.get(0).getSitePhoto1());
            inspectHandleVO.setSitePhoto2(list1.get(0).getSitePhoto2());
            inspectHandleVO.setSitePhoto3(list1.get(0).getSitePhoto3());
            list.add(inspectHandleVO);
        }

        return list;
    }

    @Override
    public void updateInspectManage(String inspectCode, String handler, String handlerPhone, Integer deviceStatus, String handlingContent, String sitePhoto1, String sitePhoto2, String sitePhoto3){
        LambdaQueryWrapper<DemInspectList> QueryWrapper = Wrappers.lambdaQuery();
        QueryWrapper.eq(DemInspectList::getInspectCode, inspectCode);
        List<DemInspectList> list = this.list(QueryWrapper);
        if(list.size() > 0){
            list.get(0).setHandler(handler);
            list.get(0).setHandlerPhone(handlerPhone);
            list.get(0).setHandlingTime(LocalDateTime.now());
            list.get(0).setDeviceStatus(deviceStatus);
            list.get(0).setHandlingContent(handlingContent);
            list.get(0).setSitePhoto1(sitePhoto1);
            list.get(0).setSitePhoto2(sitePhoto2);
            list.get(0).setSitePhoto3(sitePhoto3);
            list.get(0).setHandlingStatus(1);
            this.updateById(list.get(0));
        }
    }


    @Transactional
    @Override
    //先判断处理状态为未处理的告警,然后再判断告警时间距离当前时间在过去一小时至过去半小时之间的数据
    public void demInspectAutoTask() {
        //未处置电子督察单
        LambdaQueryWrapper<AlarmFire> QueryWrapper = Wrappers.lambdaQuery();
        List<String> alarmTypeList = new ArrayList<>();
        alarmTypeList.add("2");
        alarmTypeList.add("4");
        QueryWrapper.select(AlarmFire::getDeviceCode,AlarmFire::getId,AlarmFire::getCompanyId)
                .in(AlarmFire::getAlarmType,alarmTypeList)
                .eq(AlarmFire::getHandlingStatus, 0)
                .between(AlarmFire::getAlarmTime, LocalDateTime.now().minusMinutes(60).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), LocalDateTime.now().minusMinutes(30).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        List<AlarmFire> list = alarmFireService.list(QueryWrapper);
        if(CollectionUtils.isNotEmpty(list)){
            for(int i=0;i<list.size();i++){
                DemInspectList demInspectList = new DemInspectList();
                UUID uuid = UUID.randomUUID();
                String uuidStr = uuid.toString();
                demInspectList.setInspectCode(uuidStr.replaceAll("-",""));
                demInspectList.setDeviceId(list.get(i).getDeviceCode());
                demInspectList.setAlarmId(list.get(i).getId());
                demInspectList.setCompanyId(list.get(i).getCompanyId());
                //2 待处置
                demInspectList.setHandlingStatus(2);
                demInspectList.setCreator("闵行消防支队");
                demInspectList.setCreateTime(LocalDateTime.now());

                this.save(demInspectList);

            }
        }

        //超时未处置电子督察单
        LambdaQueryWrapper<DemInspectList> QueryList = Wrappers.lambdaQuery();
        QueryList.eq(DemInspectList::getHandlingStatus,2)
                .lt(DemInspectList::getCreateTime,LocalDateTime.now().minusMinutes(30).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        List<DemInspectList> list1 = this.list(QueryList);
        if(CollectionUtils.isNotEmpty(list1)){
            for(int j=0;j<list1.size();j++){
                //3 超时未处置
                list1.get(j).setHandlingStatus(3);
                this.updateById(list1.get(j));
            }
        }

    }
}
