package com.hw.dms.service.impl;

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

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.core.web.domain.AjaxResult;
import com.hw.common.security.utils.SecurityUtils;
import com.hw.dms.domain.DmsBaseInspectRoute;
import com.hw.dms.domain.DmsPlanMaint;
import com.hw.dms.mapper.DmsBaseInspectRouteMapper;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hw.dms.mapper.DmsPlanInspectMapper;
import com.hw.dms.domain.DmsPlanInspect;
import com.hw.dms.service.IDmsPlanInspectService;

/**
 * 巡检计划信息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;
    }

    /**
     * 查询巡检计划信息列表
     *
     * @param dmsPlanInspect 巡检计划信息
     * @return 巡检计划信息
     */
    @Override
    public List<DmsPlanInspect> selectDmsPlanInspectList(DmsPlanInspect dmsPlanInspect)
    {
        List<DmsPlanInspect> dmsPlanInspects = dmsPlanInspectMapper.selectDmsPlanInspectList(dmsPlanInspect);
        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 planInspectCode = Seq.getId(Seq.planInspectSeqType, Seq.planInspectCode);

        SysJob job = new SysJob();
        job.setJobName("点巡检计划编号" + planInspectCode + "工单任务");
        job.setJobGroup("DEFAULT");
        job.setInvokeTarget("ryTask.getDmsBillsInstance(\"" + planInspectCode + "\")");
        job.setCronExpression(dmsPlanInspect.getCronExpression());
        job.setMisfirePolicy("1");
        job.setConcurrent("1");
        job.setStatus("1");
        R<Integer> jobIdR = remoteJobService.add(SecurityConstants.INNER, job);

        String time = dmsPlanInspect.getCronExpression();
        //通过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.getUsername());
        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.getUsername());
        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);
    }
}
