package com.aucma.dms.service.impl;

//import com.hw.common.core.constant.SecurityConstants;
//import com.hw.common.core.domain.R;
//import com.hw.common.core.utils.DateUtils;
//import com.hw.common.core.utils.uuid.Seq;
//import com.hw.common.security.utils.SecurityUtils;
//import com.aucma.dms.domain.DmsPlanInspect;
//import com.aucma.dms.mapper.DmsBaseInspectRouteMapper;
//import com.aucma.dms.mapper.DmsPlanInspectMapper;
//import com.aucma.dms.service.IDmsPlanInspectService;
//import com.hw.job.api.RemoteJobService;
//import com.hw.job.api.domain.SysJob;
//import com.hw.job.api.util.CronUtils;
//import com.hw.system.api.model.LoginUser;

import com.aucma.common.core.domain.model.LoginUser;
import com.aucma.common.utils.DateUtils;
import com.aucma.common.utils.SecurityUtils;
import com.aucma.common.utils.uuid.Seq;
import com.aucma.dms.domain.DmsPlanInspect;
import com.aucma.dms.mapper.DmsBaseInspectRouteMapper;
import com.aucma.dms.mapper.DmsPlanInspectMapper;
import com.aucma.dms.service.IDmsPlanInspectService;
import com.aucma.quartz.util.CronUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 巡检计划信息Service业务层处理
 *
 * @author Open Platform
 * @date 2024-01-17
 */
@Service
public class DmsPlanInspectServiceImpl implements IDmsPlanInspectService
{
    @Autowired
    private DmsPlanInspectMapper dmsPlanInspectMapper;
/*    @Autowired
    private RemoteJobService remoteJobService;*/
    @Autowired
    private DmsBaseInspectRouteMapper dmsBaseInspectRouteMapper;

    /**
     * 查询巡检计划信息
     *
     * @param planInspectId 巡检计划信息主键
     * @return 巡检计划信息
     */
    @Override
    public DmsPlanInspect selectDmsPlanInspectByPlanInspectId(Long planInspectId)
    {
        DmsPlanInspect dmsPlanInspect = dmsPlanInspectMapper.selectPlanInspectJoinJobByInspectId(planInspectId);
        Long timeLimit = dmsPlanInspect.getTimeLimit();
        if (timeLimit != null) {
            Long days = timeLimit / (24 * 60 * 60l);
            Long hours = timeLimit % (24 * 60 * 60l);
//            System.out.println(days);
//            System.out.println(hours);
            dmsPlanInspect.setTimeLimitDays(days);
            dmsPlanInspect.setTimeLimitHours(hours / (60 * 60l));
        }
        return dmsPlanInspect;
    }
    @Override
    public DmsPlanInspect selectDmsPlanInspectByPlanInspectId1(String planInspectId)
    {
        DmsPlanInspect dmsPlanInspect = dmsPlanInspectMapper.selectPlanInspectJoinJobByInspectId1(planInspectId);
        Long timeLimit = dmsPlanInspect.getTimeLimit();
        if (timeLimit != null) {
            Long days = timeLimit / (24 * 60 * 60l);
            Long hours = timeLimit % (24 * 60 * 60l);
//            System.out.println(days);
//            System.out.println(hours);
            dmsPlanInspect.setTimeLimitDays(days);
            dmsPlanInspect.setTimeLimitHours(hours / (60 * 60l));
        }
        return dmsPlanInspect;
    }

    /**
     * 查询巡检计划信息列表
     *
     * @param dmsPlanInspect 巡检计划信息
     * @return 巡检计划信息
     */
    @Override
    public List<DmsPlanInspect> selectDmsPlanInspectList(DmsPlanInspect dmsPlanInspect)
    {
        List<DmsPlanInspect> dmsPlanInspects = dmsPlanInspectMapper.selectDmsPlanInspectList(dmsPlanInspect);
        for (DmsPlanInspect planInspect : dmsPlanInspects) {
            String routeName = dmsBaseInspectRouteMapper.selectRouteName(planInspect.getInspectRouteId());
            planInspect.setRouteName(routeName);
        }


        for (DmsPlanInspect d : dmsPlanInspects) {
            Long timeLimit = d.getTimeLimit();
            d.setTimeLimitDays(timeLimit / (24 * 60 * 60L));
            d.setTimeLimitHours((timeLimit % (24 * 60 * 60L)) / (60 * 60l));
        }
        return dmsPlanInspects;
    }

    /**
     * 新增巡检计划信息
     *
     * @param dmsPlanInspect 巡检计划信息
     * @return 结果
     */
    @Override
    public int insertDmsPlanInspect(DmsPlanInspect dmsPlanInspect)
    {
        String day = "*";
        String month = "*";
        String week = "*";
        String year = "*";
        String cron = "";
        if (dmsPlanInspect.getDay()!=null){
            day = dmsPlanInspect.getDay().toString();
        }
        if (dmsPlanInspect.getMonth()!=null){
            month = dmsPlanInspect.getMonth().toString();
        }
        if (dmsPlanInspect.getWeek()!=null){
            Long weekDay = dmsPlanInspect.getWeek() + 1;
            week = weekDay.toString();
        }
        if (dmsPlanInspect.getYear()!=null){
            year = dmsPlanInspect.getYear().toString();
        }
        if (dmsPlanInspect.getInspectLevel().equals(1l)){
            cron = "0 0 "+dmsPlanInspect.getHour()+" "+day+" "+month+" ?";
        }
        if (dmsPlanInspect.getInspectLevel().equals(2l)){
            cron = "0 0 "+dmsPlanInspect.getHour()+" ?"+" "+month+" "+week;
        }
        if (dmsPlanInspect.getInspectLevel().equals(3l)){
            cron = "0 0 "+dmsPlanInspect.getHour()+" "+day+" "+ month+" ? "+year;
        }
        String planInspectCode = Seq.getId(Seq.planInspectSeqType, Seq.planInspectCode);
/*        SysJob job = new SysJob();
        job.setJobName("点巡检计划编号" + planInspectCode + "工单任务");
        job.setJobGroup("DEFAULT");
        job.setInvokeTarget("ryTask.getDmsBillsInstance(\"" + planInspectCode + "\")");
        job.setCronExpression(cron);
        job.setMisfirePolicy("1");
        job.setConcurrent("1");
        job.setStatus("1");
        R<Integer> jobIdR = remoteJobService.add(SecurityConstants.INNER, job);*/

        String time = cron;
        //通过cron表达式获取下一次执行时间
        Date nextExecution = CronUtils.getNextExecution(time);
        dmsPlanInspect.setPlanTime(nextExecution);
        dmsPlanInspect.setPlanInspectCode(planInspectCode);
//        dmsPlanInspect.setJobId(jobIdR.getData().longValue());

        Long timeLimitDays = dmsPlanInspect.getTimeLimitDays()==null?0L:dmsPlanInspect.getTimeLimitDays();
        Long timeLimitHours = dmsPlanInspect.getTimeLimitHours() == null?0L:dmsPlanInspect.getTimeLimitHours();
        dmsPlanInspect.setTimeLimit((timeLimitDays * 24 * 60 * 60)
                + (timeLimitHours * 60 * 60));
        dmsPlanInspect.setIsFlag("1");
        dmsPlanInspect.setCreateBy(SecurityUtils.getLoginUser()==null ? SecurityUtils.getUserId() :SecurityUtils.getLoginUser().getUser().getUserId());
        dmsPlanInspect.setCreateTime(DateUtils.getNowDate());
        //获取设备总数
        //Long aLong = dmsBaseInspectRouteMapper.selectAmountByInspectRouteId(dmsPlanInspect.getInspectRouteId());
        //dmsPlanInspect.setDeviceAmount(aLong);
        return dmsPlanInspectMapper.insertDmsPlanInspect(dmsPlanInspect);


    }

    /**
     * 修改巡检计划信息
     *
     * @param dmsPlanInspect 巡检计划信息
     * @return 结果
     */
    @Override
    public int updateDmsPlanInspect(DmsPlanInspect dmsPlanInspect)
    {
        LoginUser user = SecurityUtils.getLoginUser();
        dmsPlanInspect.setUpdateBy(user.getUserId());
        dmsPlanInspect.setUpdateTime(DateUtils.getNowDate());
        return dmsPlanInspectMapper.updateDmsPlanInspect(dmsPlanInspect);
    }

    /**
     * 批量删除巡检计划信息
     *
     * @param planInspectIds 需要删除的巡检计划信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsPlanInspectByPlanInspectIds(Long[] planInspectIds)
    {
        return dmsPlanInspectMapper.deleteDmsPlanInspectByPlanInspectIds(planInspectIds);
    }

    /**
     * 删除巡检计划信息信息
     *
     * @param planInspectId 巡检计划信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsPlanInspectByPlanInspectId(Long planInspectId)
    {
        return dmsPlanInspectMapper.deleteDmsPlanInspectByPlanInspectId(planInspectId);
    }

    @Override
    public DmsPlanInspect selectPlanInspectJoinJobByInspectId(Long planInspectId) {

        return dmsPlanInspectMapper.selectPlanInspectJoinJobByInspectId(planInspectId);
    }
}
