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

import com.ruoyi.project.patrol.patrol.domain.TPatrolMain;
import com.ruoyi.project.patrol.patrol.domain.TPatrolPlan;
import com.ruoyi.project.patrol.patrol.service.ITPatrolMainService;
import com.ruoyi.project.patrol.patrol.service.ITPatrolPlanService;
import com.ruoyi.project.pssr.domain.TPssrPatrol;
import com.ruoyi.project.pssr.mapper.TPssrPatrolMapper;
import com.ruoyi.project.pssr.service.ITPssrPatrolService;
import org.apache.commons.collections4.CollectionUtils;
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 ssy
 * @date 2024-09-18
 */
@Service
public class TPssrPatrolServiceImpl implements ITPssrPatrolService {
    @Autowired
    private TPssrPatrolMapper tPssrPatrolMapper;
    @Autowired
    private ITPatrolPlanService tPatrolPlanService;
    @Autowired
    private ITPatrolMainService tPatrolMainService;

    /**
     * 查询巡检
     *
     * @param id 巡检ID
     * @return 巡检
     */
    @Override
    public TPssrPatrol selectTPssrPatrolById(Long id) {
        return tPssrPatrolMapper.selectTPssrPatrolById(id);
    }

    @Override
    public int selectNoConfirmCount(Long subId) {
        return tPssrPatrolMapper.selectNoConfirmCount(subId);
    }

    /**
     * 查询巡检列表
     *
     * @param tPssrPatrol 巡检
     * @return 巡检
     */
    @Override
    public List<TPssrPatrol> selectTPssrPatrolList(TPssrPatrol tPssrPatrol) {
        return tPssrPatrolMapper.selectTPssrPatrolList(tPssrPatrol);
    }

    /**
     * 新增巡检
     *
     * @param tPssrPatrol 巡检
     * @return 结果
     */
    @Override
    public int insertTPssrPatrol(TPssrPatrol tPssrPatrol, String userId) {
        //根据设备类型查询当月的巡检计划
        TPatrolPlan tPatrolPlan = new TPatrolPlan();
        tPatrolPlan.setPatrolType(tPssrPatrol.getPlanType().toString());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        tPatrolPlan.setPlanYear((long) calendar.get(Calendar.YEAR));
        tPatrolPlan.setPlanQuarter((long) calendar.get(Calendar.MONTH) + 1);
        List<TPatrolPlan> tPatrolPlans = tPatrolPlanService.selectTPatrolPlanList(tPatrolPlan);
        Long planId = null;
        Long approveStatus = 0L;
        //存在对应计划时，取已存在的计划
        if (CollectionUtils.isNotEmpty(tPatrolPlans)) {
            TPatrolPlan plan = tPatrolPlans.get(0);
            planId = plan.getId();
            approveStatus = plan.getStatus() == 1 ? 2L : 0L;
            tPssrPatrol.setConfirmer(plan.getConfirmed());
            tPssrPatrol.setConfirmDate(plan.getConfirmDate());
        } else {
            //否则生成新的临时计划
            tPatrolPlan.setPlanName(getPlanName(tPssrPatrol.getPlanType().toString(), calendar));
            TPatrolPlan plan = tPatrolPlanService.add(tPatrolPlan, userId);
            planId = plan.getId();
        }
        tPssrPatrol.setPlanId(planId);
        tPssrPatrol.setApproveStatus(approveStatus);
        tPssrPatrol.setPlanStatus(approveStatus);
        return tPssrPatrolMapper.insertTPssrPatrol(tPssrPatrol);
    }

    // 根据巡检类型生成计划名称
    private String getPlanName(String planType, Calendar calendar) {
        if (planType.equals("27")) {
            return calendar.get(Calendar.YEAR) + "年-" + (calendar.get(Calendar.MONTH) + 1) + "月-PSSR临时计划-急救箱检查记录";
        }
        TPatrolMain tPatrolMain = new TPatrolMain();
        tPatrolMain.setPatrolType(planType);
        tPatrolMain.setYear(String.valueOf(calendar.get(Calendar.YEAR)));
        TPatrolMain main = tPatrolMainService.selectTPatrolMainList(tPatrolMain).get(0);
        String planName = calendar.get(Calendar.YEAR) + "年-" + (calendar.get(Calendar.MONTH) + 1) + "月-PSSR临时计划-" + main.getDevName();
        return planName;
    }

    /**
     * 修改巡检
     *
     * @param tPssrPatrol 巡检
     * @return 结果
     */
    @Override
    public int updateTPssrPatrol(TPssrPatrol tPssrPatrol) {
        return tPssrPatrolMapper.updateTPssrPatrol(tPssrPatrol);
    }

    /**
     * 批量删除巡检
     *
     * @param ids 需要删除的巡检ID
     * @return 结果
     */
    @Override
    public int deleteTPssrPatrolByIds(Long[] ids) {
        return tPssrPatrolMapper.deleteTPssrPatrolByIds(ids);
    }

    /**
     * 删除巡检信息
     *
     * @param id 巡检ID
     * @return 结果
     */
    @Override
    public int deleteTPssrPatrolById(Long id) {
        return tPssrPatrolMapper.deleteTPssrPatrolById(id);
    }
}
