package com.ruoyi.project.patrol.patrol.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.patrol.commonuse.domain.*;
import com.ruoyi.project.patrol.commonuse.mapper.*;
import com.ruoyi.project.patrol.dev.domain.*;
import com.ruoyi.project.patrol.dev.mapper.*;
import com.ruoyi.project.patrol.emergency.domain.*;
import com.ruoyi.project.patrol.emergency.mapper.*;
import com.ruoyi.project.patrol.fire.domain.*;
import com.ruoyi.project.patrol.fire.mapper.*;
import com.ruoyi.project.patrol.patrol.domain.TPatrolPlan;
import com.ruoyi.project.patrol.patrol.mapper.TPatrolMainMapper;
import com.ruoyi.project.patrol.patrol.mapper.TPatrolPlanMapper;
import com.ruoyi.project.patrol.patrol.service.ITPatrolPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 检查计划Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-29
 */
@Service
public class TPatrolPlanServiceImpl implements ITPatrolPlanService {
    @Autowired
    private TPatrolPlanMapper tPatrolPlanMapper;

    @Autowired
    private TDevAnnihilatorMapper tDevAnnihilatorMapper;

    @Autowired
    private TDevEyewashMapper tDevEyewashMapper;

    @Autowired
    private TDevDoorMapper tDevDoorMapper;

    @Autowired
    private TDevWaterCannonMapper tDevWaterCannonMapper;

    @Autowired
    private TDevPostFireMapper tDevPostFireMapper;

    @Autowired
    private TDevHoseBoxMapper tDevHoseBoxMapper;

    @Autowired
    private TDevStandpipeMapper tDevStandpipeMapper;

    @Autowired
    private TDevSprinklerMapper tDevSprinklerMapper;

    @Autowired
    private TDevAlarmMapper tDevAlarmMapper;

    @Autowired
    private TDevGasShieldMapper tDevGasShieldMapper;

    @Autowired
    private TDevSandboxMapper tDevSandboxMapper;

    @Autowired
    private TDevPagaMapper tDevPagaMapper;

    @Autowired
    private TDevTesterMapper tDevTesterMapper;

    @Autowired
    private TDevCctvMapper tDevCctvMapper;

    @Autowired
    private TDevWeathervaneMapper tDevWeathervaneMapper;

    @Autowired
    private TDevCofferdamMapper tDevCofferdamMapper;

    @Autowired
    private TDevSteamCurtainMapper tDevSteamCurtainMapper;

    @Autowired
    private TDevCabinetMapper tDevCabinetMapper;

    @Autowired
    private TDevEarplugMapper tDevEarplugMapper;

    @Autowired
    private TDevManholeCoverMapper tDevManholeCoverMapper;

    @Autowired
    private TDevMovementMapper tDevMovementMapper;

    @Autowired
    private TDevBicycleMapper tDevBicycleMapper;

    @Autowired
    private TDevWalkieTalkieMapper tDevWalkieTalkieMapper;

    @Autowired
    private TDevSafetyBeltMapper tDevSafetyBeltMapper;

    @Autowired
    private TDevDownspoutMapper tDevDownspoutMapper;

    @Autowired
    private TDevWaterPumpMapper tDevWaterPumpMapper;

    @Autowired
    private TDevAidkitMapper tDevAidkitMapper;

    @Autowired
    private TEmergencyEyewashMapper tEmergencyEyewashMapper;

    @Autowired
    private TEmergencyPagaMapper tEmergencyPagaMapper;

    @Autowired
    private TEmergencyCctvMapper tEmergencyCctvMapper;

    @Autowired
    private TEmergencyCofferdamMapper tEmergencyCofferdamMapper;

    @Autowired
    private TEmergencyCabinetMapper tEmergencyCabinetMapper;

    @Autowired
    private TFireAnnihilatorMapper tFireAnnihilatorMapper;

    @Autowired
    private TFireWaterCannonMapper tFireWaterCannonMapper;

    @Autowired
    private TFirePostFireMapper tFirePostFireMapper;

    @Autowired
    private TFireHoseBoxMapper tFireHoseBoxMapper;

    @Autowired
    private TFireStandpipeMapper tFireStandpipeMapper;

    @Autowired
    private TFireSprinklerMapper tFireSprinklerMapper;

    @Autowired
    private TFireAlarmMapper tFireAlarmMapper;

    @Autowired
    private TFireGasShieldMapper tFireGasShieldMapper;

    @Autowired
    private TFireSandboxMapper tFireSandboxMapper;

    @Autowired
    private TFireDoorMapper tFireDoorMapper;

    @Autowired
    private TFireSteamCurtainMapper tFireSteamCurtainMapper;

    @Autowired
    private TFireWeathervaneMapper tFireWeathervaneMapper;

    @Autowired
    private TFireAidkitMapper tFireAidkitMapper;

    @Autowired
    private TCommonuseTesterMapper tCommonuseTesterMapper;

    @Autowired
    private TCommonuseEarplugMapper tCommonuseEarplugMapper;

    @Autowired
    private TCommonuseManholeCoverMapper tCommonuseManholeCoverMapper;

    @Autowired
    private TCommonuseMovementMapper tCommonuseMovementMapper;

    @Autowired
    private TCommonuseBicycleMapper tCommonuseBicycleMapper;

    @Autowired
    private TCommonuseWalkieTalkieMapper tCommonuseWalkieTalkieMapper;

    @Autowired
    private TCommonuseSafetyBeltMapper tCommonuseSafetyBeltMapper;

    @Autowired
    private TCommonuseDownspoutMapper tCommonuseDownspoutMapper;

    @Autowired
    private TCommonuseWaterPumpMapper tCommonuseWaterPumpMapper;

    @Autowired
    private TPatrolMainMapper tPatrolMainMapper;

    /**
     * 查询检查计划
     *
     * @param id 检查计划主键
     * @return 检查计划
     */
    @Override
    public TPatrolPlan selectTPatrolPlanById(Long id) {
        return tPatrolPlanMapper.selectTPatrolPlanById(id);
    }

    /**
     * 查询检查计划列表
     *
     * @param tPatrolPlan 检查计划
     * @return 检查计划
     */
    @Override
    public List<TPatrolPlan> selectTPatrolPlanList(TPatrolPlan tPatrolPlan) {
        return tPatrolPlanMapper.selectTPatrolPlanList(tPatrolPlan);
    }

    /**
     * 新增检查计划
     *
     * @param tPatrolPlan 检查计划
     * @return 结果
     */
    @Override
    public int insertTPatrolPlan(TPatrolPlan tPatrolPlan) {
        return tPatrolPlanMapper.insertTPatrolPlan(tPatrolPlan);
    }

    @Override
    public TPatrolPlan add(TPatrolPlan tPatrolPlan, String userId) {
        tPatrolPlan.setStatus(0L);
        if (insertTPatrolPlan(tPatrolPlan) > 0) {
            Calendar cd = Calendar.getInstance();
            cd.set(Math.toIntExact(tPatrolPlan.getPlanYear()), Math.toIntExact(tPatrolPlan.getPlanQuarter()) - 1, 15);
            switch (tPatrolPlan.getPatrolType()) {
                case "1":
                    List<TDevAnnihilator> tDevAnnihilators = tDevAnnihilatorMapper.selectTDevAnnihilatorList(new TDevAnnihilator());
                    for (TDevAnnihilator annihilator : tDevAnnihilators) {
                        TFireAnnihilator tFireAnnihilator = getFireAnnihilator(tPatrolPlan, annihilator, userId);
                        tFireAnnihilatorMapper.insertTFireAnnihilator(tFireAnnihilator);
                    }
                    tPatrolPlan.setDevNum((long) tDevAnnihilators.size());
                    tPatrolPlan.setUncheckedNum((long) tDevAnnihilators.size());
                    tPatrolPlan.setCheckedNum(0L);
                    if ("1".equals(tPatrolPlan.getDetectionFrequency())) {
                        tPatrolPlan.setDeadline(cd.getTime());
                    } else
                        tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "2":
                    TDevWaterCannon tDevWaterCannon = new TDevWaterCannon();
                    tDevWaterCannon.setStatus("1");
                    List<TDevWaterCannon> tDevWaterCannons = tDevWaterCannonMapper.selectTDevWaterCannonList(tDevWaterCannon);
                    for (TDevWaterCannon dev : tDevWaterCannons) {
                        TFireWaterCannon fireWaterCannon = new TFireWaterCannon(dev.getDevNo(), dev.getPosition(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tFireWaterCannonMapper.insertTFireWaterCannon(fireWaterCannon);
                    }
                    tPatrolPlan.setDevNum((long) tDevWaterCannons.size());
                    tPatrolPlan.setUncheckedNum((long) tDevWaterCannons.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "3":
                    TDevPostFire tDevPostFire = new TDevPostFire();
                    tDevPostFire.setStatus("1");
                    List<TDevPostFire> tDevPostFires = tDevPostFireMapper.selectTDevPostFireList(tDevPostFire);
                    for (TDevPostFire dev : tDevPostFires) {
                        TFirePostFire firePostFire = new TFirePostFire(dev.getDevNo(), dev.getPosition(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tFirePostFireMapper.insertTFirePostFire(firePostFire);
                    }
                    tPatrolPlan.setDevNum((long) tDevPostFires.size());
                    tPatrolPlan.setUncheckedNum((long) tDevPostFires.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "4":
                    TDevHoseBox tDevHoseBox = new TDevHoseBox();
                    tDevHoseBox.setStatus("1");
                    List<TDevHoseBox> tDevHoseBoxs = tDevHoseBoxMapper.selectTDevHoseBoxList(tDevHoseBox);
                    for (TDevHoseBox dev : tDevHoseBoxs) {
                        TFireHoseBox fireHoseBox = new TFireHoseBox(dev.getDevNo(), dev.getPosition(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tFireHoseBoxMapper.insertTFireHoseBox(fireHoseBox);
                    }
                    tPatrolPlan.setDevNum((long) tDevHoseBoxs.size());
                    tPatrolPlan.setUncheckedNum((long) tDevHoseBoxs.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "5":
                    TDevStandpipe tDevStandpipe = new TDevStandpipe();
                    tDevStandpipe.setStatus("1");
                    List<TDevStandpipe> tDevStandpipes = tDevStandpipeMapper.selectTDevStandpipeList(tDevStandpipe);
                    for (TDevStandpipe dev : tDevStandpipes) {
                        TFireStandpipePatrol item = new TFireStandpipePatrol(dev.getDevNo(), dev.getPosition(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tFireStandpipeMapper.insertTFireStandpipe(item);
                    }
                    tPatrolPlan.setDevNum((long) tDevStandpipes.size());
                    tPatrolPlan.setUncheckedNum((long) tDevStandpipes.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "6":
                    TDevSprinkler tDevSprinkler = new TDevSprinkler();
                    tDevSprinkler.setStatus("1");
                    List<TDevSprinkler> tDevSprinklers = tDevSprinklerMapper.selectTDevSprinklerList(tDevSprinkler);
                    for (TDevSprinkler dev : tDevSprinklers) {
                        TFireSprinkler item = new TFireSprinkler(dev.getDevNo(), dev.getPosition(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tFireSprinklerMapper.insertTFireSprinkler(item);
                    }
                    tPatrolPlan.setDevNum((long) tDevSprinklers.size());
                    tPatrolPlan.setUncheckedNum((long) tDevSprinklers.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "7":
                    TDevCabinet tDevCabinet = new TDevCabinet();
                    tDevCabinet.setStatus("1");
                    List<TDevCabinet> tDevCabinets = tDevCabinetMapper.selectTDevCabinetList(tDevCabinet);
                    for (TDevCabinet dev : tDevCabinets) {
                        TEmergencyCabinet item = new TEmergencyCabinet(dev.getDevName(), dev.getDevNum(), dev.getDevType(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tEmergencyCabinetMapper.insertTEmergencyCabinet(item);
                    }
                    tPatrolPlan.setDevNum((long) tDevCabinets.size());
                    tPatrolPlan.setUncheckedNum((long) tDevCabinets.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "8":
                    TDevAlarm alarm = new TDevAlarm();
                    alarm.setStatus("1");
                    List<TDevAlarm> alarms = tDevAlarmMapper.selectTDevAlarmList(alarm);
                    for (TDevAlarm dev : alarms) {
                        TFireAlarm item = new TFireAlarm(dev.getDevNo(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tFireAlarmMapper.insertTFireAlarm(item);
                    }
                    tPatrolPlan.setDevNum((long) alarms.size());
                    tPatrolPlan.setUncheckedNum((long) alarms.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "9":
                    TDevGasShield shield = new TDevGasShield();
                    shield.setStatus("1");
                    List<TDevGasShield> shields = tDevGasShieldMapper.selectTDevGasShieldList(shield);
                    for (TDevGasShield dev : shields) {
                        TFireGasShield item = new TFireGasShield(dev.getDevNo(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tFireGasShieldMapper.insertTFireGasShield(item);
                    }
                    tPatrolPlan.setDevNum((long) shields.size());
                    tPatrolPlan.setUncheckedNum((long) shields.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "10":
                    TDevPaga tDevPaga = new TDevPaga();
                    tDevPaga.setStatus("1");
                    List<TDevPaga> tDevPagas = tDevPagaMapper.selectTDevPagaList(tDevPaga);
                    for (TDevPaga dev : tDevPagas) {
                        TEmergencyPaga item = new TEmergencyPaga(dev.getDevNo(), dev.getPosition(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tEmergencyPagaMapper.insertTEmergencyPaga(item);
                    }
                    tPatrolPlan.setDevNum((long) tDevPagas.size());
                    tPatrolPlan.setUncheckedNum((long) tDevPagas.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "11":
                    TDevCctv tDevCctv = new TDevCctv();
                    tDevCctv.setStatus("1");
                    List<TDevCctv> tDevCctvs = tDevCctvMapper.selectTDevCctvList(tDevCctv);
                    for (TDevCctv dev : tDevCctvs) {
                        TEmergencyCctv item = new TEmergencyCctv(dev.getDevNo(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tEmergencyCctvMapper.insertTEmergencyCctv(item);
                    }
                    tPatrolPlan.setDevNum((long) tDevCctvs.size());
                    tPatrolPlan.setUncheckedNum((long) tDevCctvs.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "12":
                    TDevEyewash tDevEyewash = new TDevEyewash();
                    tDevEyewash.setStatus("1");
                    List<TDevEyewash> tDevEyewashes = tDevEyewashMapper.selectTDevEyewashList(tDevEyewash);
                    for (TDevEyewash devEyewash : tDevEyewashes) {
                        TEmergencyEyewash emergencyEyewash = new TEmergencyEyewash(devEyewash.getDevNo(),
                                devEyewash.getPosition(), tPatrolPlan.getId(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), userId);
                        tEmergencyEyewashMapper.insertTEmergencyEyewash(emergencyEyewash);
                    }
                    tPatrolPlan.setDevNum((long) tDevEyewashes.size());
                    tPatrolPlan.setUncheckedNum((long) tDevEyewashes.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "13":
                    TDevSandbox tDevSandbox = new TDevSandbox();
                    tDevSandbox.setStatus("1");
                    List<TDevSandbox> tDevSandboxs = tDevSandboxMapper.selectTDevSandboxList(tDevSandbox);
                    for (TDevSandbox dev : tDevSandboxs) {
                        TFireSandbox item = new TFireSandbox(dev.getDevNo(), dev.getPosition(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tFireSandboxMapper.insertTFireSandbox(item);
                    }
                    tPatrolPlan.setDevNum((long) tDevSandboxs.size());
                    tPatrolPlan.setUncheckedNum((long) tDevSandboxs.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "14":
                    TDevDoor tDevDoor = new TDevDoor();
                    tDevDoor.setStatus("1");
                    List<TDevDoor> tDevDoors = tDevDoorMapper.selectTDevDoorList(tDevDoor);
                    for (TDevDoor dev : tDevDoors) {
                        TFireDoor item = new TFireDoor(dev.getDevNo(), dev.getDevType(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tFireDoorMapper.insertTFireDoor(item);
                    }
                    tPatrolPlan.setDevNum((long) tDevDoors.size());
                    tPatrolPlan.setUncheckedNum((long) tDevDoors.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "15":
                    TDevWeathervane tDevWeathervane = new TDevWeathervane();
                    tDevWeathervane.setStatus("1");
                    List<TDevWeathervane> tDevWeathervanes = tDevWeathervaneMapper.selectTDevWeathervaneList(tDevWeathervane);
                    for (TDevWeathervane dev : tDevWeathervanes) {
                        TFireWeathervane item = new TFireWeathervane(dev.getDevNo(), dev.getPosition(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tFireWeathervaneMapper.insertTFireWeathervane(item);
                    }
                    tPatrolPlan.setDevNum((long) tDevWeathervanes.size());
                    tPatrolPlan.setUncheckedNum((long) tDevWeathervanes.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "16":
                    TDevSteamCurtain tDevSteamCurtain = new TDevSteamCurtain();
                    tDevSteamCurtain.setStatus("1");
                    List<TDevSteamCurtain> tDevSteamCurtains = tDevSteamCurtainMapper.selectTDevSteamCurtainList(tDevSteamCurtain);
                    for (TDevSteamCurtain dev : tDevSteamCurtains) {
                        TFireSteamCurtain item = new TFireSteamCurtain(dev.getDevNo(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tFireSteamCurtainMapper.insertTFireSteamCurtain(item);
                    }
                    tPatrolPlan.setDevNum((long) tDevSteamCurtains.size());
                    tPatrolPlan.setUncheckedNum((long) tDevSteamCurtains.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "17":
                    TDevCofferdam tDevCofferdam = new TDevCofferdam();
                    tDevCofferdam.setStatus("1");
                    List<TDevCofferdam> tDevCofferdams = tDevCofferdamMapper.selectTDevCofferdamList(tDevCofferdam);
                    for (TDevCofferdam dev : tDevCofferdams) {
                        TEmergencyCofferdam item = new TEmergencyCofferdam(dev.getDevNo(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tEmergencyCofferdamMapper.insertTEmergencyCofferdam(item);
                    }
                    tPatrolPlan.setDevNum((long) tDevCofferdams.size());
                    tPatrolPlan.setUncheckedNum((long) tDevCofferdams.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "18":
                    TDevTester tDevTester = new TDevTester();
                    tDevTester.setStatus("1");
                    List<TDevTester> tDevTesters = tDevTesterMapper.selectTDevTesterList(tDevTester);
                    for (TDevTester dev : tDevTesters) {
                        TCommonuseTester item = new TCommonuseTester(dev.getTesterType(), dev.getTesterModel(), dev.getTesterNo(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tCommonuseTesterMapper.insertTCommonuseTester(item);
                    }
                    tPatrolPlan.setDevNum((long) tDevTesters.size());
                    tPatrolPlan.setUncheckedNum((long) tDevTesters.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "19":
                    TDevEarplug tDevEarplug = new TDevEarplug();
                    tDevEarplug.setStatus("1");
                    List<TDevEarplug> tDevEarplugs = tDevEarplugMapper.selectTDevEarplugList(tDevEarplug);
                    for (TDevEarplug dev : tDevEarplugs) {
                        TCommonuseEarplug item = new TCommonuseEarplug(dev.getDevNo(), dev.getPosition(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tCommonuseEarplugMapper.insertTCommonuseEarplug(item);
                    }
                    tPatrolPlan.setDevNum((long) tDevEarplugs.size());
                    tPatrolPlan.setUncheckedNum((long) tDevEarplugs.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "20":
                    TDevMovement tDevMovement = new TDevMovement();
                    tDevMovement.setStatus("1");
                    List<TDevMovement> tDevMovements = tDevMovementMapper.selectTDevMovementList(tDevMovement);
                    for (TDevMovement dev : tDevMovements) {
                        TCommonuseMovement item = new TCommonuseMovement(dev.getDevNo(), dev.getPosition(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tCommonuseMovementMapper.insertTCommonuseMovement(item);
                    }
                    tPatrolPlan.setDevNum((long) tDevMovements.size());
                    tPatrolPlan.setUncheckedNum((long) tDevMovements.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "21":
                    TDevBicycle tDevBicycle = new TDevBicycle();
                    tDevBicycle.setStatus("1");
                    List<TDevBicycle> tDevBicycles = tDevBicycleMapper.selectTDevBicycleList(tDevBicycle);
                    for (TDevBicycle dev : tDevBicycles) {
                        TCommonuseBicycle item = new TCommonuseBicycle(dev.getDevNo(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tCommonuseBicycleMapper.insertTCommonuseBicycle(item);
                    }
                    tPatrolPlan.setDevNum((long) tDevBicycles.size());
                    tPatrolPlan.setUncheckedNum((long) tDevBicycles.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "22":
                    TDevManholeCover tDevManholeCover = new TDevManholeCover();
                    tDevManholeCover.setStatus("1");
                    List<TDevManholeCover> tDevManholeCovers = tDevManholeCoverMapper.selectTDevManholeCoverList(tDevManholeCover);
                    for (TDevManholeCover dev : tDevManholeCovers) {
                        TCommonuseManholeCover item = new TCommonuseManholeCover(dev.getDevNo(), dev.getPosition(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tCommonuseManholeCoverMapper.insertTCommonuseManholeCover(item);
                    }
                    tPatrolPlan.setDevNum((long) tDevManholeCovers.size());
                    tPatrolPlan.setUncheckedNum((long) tDevManholeCovers.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "23":
                    TDevWalkieTalkie tDevWalkieTalkie = new TDevWalkieTalkie();
                    tDevWalkieTalkie.setStatus("1");
                    List<TDevWalkieTalkie> tDevWalkieTalkies = tDevWalkieTalkieMapper.selectTDevWalkieTalkieList(tDevWalkieTalkie);
                    for (TDevWalkieTalkie dev : tDevWalkieTalkies) {
                        TCommonuseWalkieTalkie item = new TCommonuseWalkieTalkie(dev.getDevNo(),  tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tCommonuseWalkieTalkieMapper.insertTCommonuseWalkieTalkie(item);
                    }
                    tPatrolPlan.setDevNum((long) tDevWalkieTalkies.size());
                    tPatrolPlan.setUncheckedNum((long) tDevWalkieTalkies.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "24":
                    TDevSafetyBelt tDevSafetyBelt = new TDevSafetyBelt();
                    tDevSafetyBelt.setStatus("1");
                    List<TDevSafetyBelt> tDevSafetyBelts = tDevSafetyBeltMapper.selectTDevSafetyBeltList(tDevSafetyBelt);
                    for (TDevSafetyBelt dev : tDevSafetyBelts) {
                        TCommonuseSafetyBelt item = new TCommonuseSafetyBelt(dev.getDevNo(), dev.getPosition(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tCommonuseSafetyBeltMapper.insertTCommonuseSafetyBelt(item);
                    }
                    tPatrolPlan.setDevNum((long) tDevSafetyBelts.size());
                    tPatrolPlan.setUncheckedNum((long) tDevSafetyBelts.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "25":
                    TDevDownspout tDevDownspout = new TDevDownspout();
                    tDevDownspout.setStatus("1");
                    List<TDevDownspout> tDevDownspouts = tDevDownspoutMapper.selectTDevDownspoutList(tDevDownspout);
                    for (TDevDownspout dev : tDevDownspouts) {
                        TCommonuseDownspout item = new TCommonuseDownspout( dev.getPosition(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tCommonuseDownspoutMapper.insertTCommonuseDownspout(item);
                    }
                    tPatrolPlan.setDevNum((long) tDevDownspouts.size());
                    tPatrolPlan.setUncheckedNum((long) tDevDownspouts.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "26":
                    TDevWaterPump tDevWaterPump = new TDevWaterPump();
                    tDevWaterPump.setStatus("1");
                    List<TDevWaterPump> tDevWaterPumps = tDevWaterPumpMapper.selectTDevWaterPumpList(tDevWaterPump);
                    for (TDevWaterPump dev : tDevWaterPumps) {
                        TCommonuseWaterPump item = new TCommonuseWaterPump(dev.getDevNo(), dev.getPosition(), tPatrolPlan.getPlanQuarter(), tPatrolPlan.getPlanYear(), tPatrolPlan.getId(), userId);
                        tCommonuseWaterPumpMapper.insertTCommonuseWaterPump(item);
                    }
                    tPatrolPlan.setDevNum((long) tDevWaterPumps.size());
                    tPatrolPlan.setUncheckedNum((long) tDevWaterPumps.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    break;
                case "27":
                    TDevAidkit aidkit = new TDevAidkit();
                    aidkit.setStatus("1");
                    List<TDevAidkit> tDevAidkits = tDevAidkitMapper.selectTDevAidkitList(aidkit);
                    for (TDevAidkit dev : tDevAidkits) {
                        TFireAidkit tFireAidkit = new TFireAidkit();
                        tFireAidkit.setPosition(dev.getPosition());
                        tFireAidkit.setMonth(tPatrolPlan.getPlanQuarter());
                        tFireAidkit.setYear(tPatrolPlan.getPlanYear());
                        tFireAidkit.setPlanId(tPatrolPlan.getId());
                        tFireAidkit.setCreaterCode(userId);
                        tFireAidkit.setCreatedate(new Date());
                        tFireAidkitMapper.insertTFireAidkit(tFireAidkit);
                    }
                    tPatrolPlan.setDevNum((long) tDevAidkits.size());
                    tPatrolPlan.setUncheckedNum((long) tDevAidkits.size());
                    tPatrolPlan.setCheckedNum(0L);
                    tPatrolPlan.setDeadline(DateUtils.getLastMonthDay(cd.getTime()));
                    System.err.println("===================tPatrolPlan=================:"+ JSON.toJSONString(tPatrolPlan));
                    break;
            }
            updateTPatrolPlan(tPatrolPlan);
            tPatrolPlan.setDevNum(0L);
            tPatrolPlan.setUncheckedNum(0L);
            tPatrolPlan.setCheckedNum(0L);
            return tPatrolPlan;
        }
        return null;
    }

    private TFireAnnihilator getFireAnnihilator(TPatrolPlan tPatrolPlan, TDevAnnihilator annihilator, String userId) {
        TFireAnnihilator tFireAnnihilator = new TFireAnnihilator();
        tFireAnnihilator.setPosition(annihilator.getPosition());
        tFireAnnihilator.setDevNo(annihilator.getDevNo());
        tFireAnnihilator.setAnniNum(annihilator.getAnniNum());
        tFireAnnihilator.setAnniType(annihilator.getAnniType());
        tFireAnnihilator.setMonth(tPatrolPlan.getPlanQuarter());
        tFireAnnihilator.setYear(tPatrolPlan.getPlanYear());
        tFireAnnihilator.setRate(tPatrolPlan.getDetectionFrequency());
        tFireAnnihilator.setCreatedate(new Date());
        tFireAnnihilator.setCreaterCode(userId);
        tFireAnnihilator.setPlanId(tPatrolPlan.getId());
        tFireAnnihilator.setCheckStatus("0");
        tFireAnnihilator.setIssuesStatus("0");
        return tFireAnnihilator;
    }

    /**
     * 修改检查计划
     *
     * @param tPatrolPlan 检查计划
     * @return 结果
     */
    @Override
    public int updateTPatrolPlan(TPatrolPlan tPatrolPlan) {
        return tPatrolPlanMapper.updateTPatrolPlan(tPatrolPlan);
    }

    /**
     * 批量删除检查计划
     *
     * @param ids 需要删除的检查计划主键
     * @return 结果
     */
    @Override
    public int deleteTPatrolPlanByIds(Long[] ids) {
        return tPatrolPlanMapper.deleteTPatrolPlanByIds(ids);
    }

    /**
     * 删除检查计划信息
     *
     * @param id 检查计划主键
     * @return 结果
     */
    @Override
    public int deleteTPatrolPlanById(Long id) {
        return tPatrolPlanMapper.deleteTPatrolPlanById(id);
    }
}
