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.StringUtils;
import com.hw.common.core.utils.uuid.Seq;
import com.hw.common.security.utils.SecurityUtils;
import com.hw.job.api.RemoteJobService;
import com.hw.job.api.domain.SysJob;
import com.hw.job.api.util.CronUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hw.dms.mapper.DmsPlanLubeMapper;
import com.hw.dms.domain.DmsPlanLube;
import com.hw.dms.service.IDmsPlanLubeService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 润滑计划Service业务层处理
 *
 * @author Open Platform
 * @date 2024-01-12
 */
@Service
public class DmsPlanLubeServiceImpl implements IDmsPlanLubeService {
    @Autowired
    private DmsPlanLubeMapper dmsPlanLubeMapper;

    @Resource
    private RemoteJobService remoteJobService;

    /**
     * 查询润滑计划
     *
     * @param planLubeId 润滑计划主键
     * @return 润滑计划
     */
    @Override
    public DmsPlanLube selectDmsPlanLubeByPlanLubeId(Long planLubeId) {
        DmsPlanLube dmsPlanLube = dmsPlanLubeMapper.selectPlanLubeJoinJobByPlanLubeId(planLubeId);
        Long timeLimit = dmsPlanLube.getTimeLimit();
        if (timeLimit != null) {
            Long days = timeLimit / (24 * 60 * 60l);
            Long hours = timeLimit % (24 * 60 * 60l);
//            System.out.println(days);
//            System.out.println(hours);
            dmsPlanLube.setTimeLimitDays(days);
            dmsPlanLube.setTimeLimitHours(hours / (60 * 60l));
        }
        return dmsPlanLube;
    }

    /**
     * 查询润滑计划列表
     *
     * @param dmsPlanLube 润滑计划
     * @return 润滑计划
     */
    @Override
    public List<DmsPlanLube> selectDmsPlanLubeList(DmsPlanLube dmsPlanLube) {
        List<DmsPlanLube> dmsPlanLubes = dmsPlanLubeMapper.selectDmsPlanLubeList(dmsPlanLube);
        for (DmsPlanLube dmsPlanReapirFor : dmsPlanLubes) {
            Long timeLimit = dmsPlanReapirFor.getTimeLimit();
            if (timeLimit != null) {
                Long days = timeLimit / (24 * 60 * 60l);
                Long hours = timeLimit % (24 * 60 * 60l);
//            System.out.println(days);
//            System.out.println(hours);
                dmsPlanReapirFor.setTimeLimitDays(days);
                dmsPlanReapirFor.setTimeLimitHours(hours / (60 * 60l));
            }
        }
        return dmsPlanLubes;
    }

    /**
     * 新增润滑计划
     *
     * @param dmsPlanLube 润滑计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDmsPlanLube(DmsPlanLube dmsPlanLube) {
        String planLubeCode = Seq.getId(Seq.planLubeSeqType, Seq.planLubeCode);

        SysJob job = new SysJob();
        job.setJobName("润滑计划编号" + planLubeCode + "工单任务");
        job.setJobGroup("DEFAULT");
        job.setInvokeTarget("ryTask.getDmsBillsLube(\"" + planLubeCode + "\")");
        job.setCronExpression(dmsPlanLube.getCronExpression());
        job.setMisfirePolicy("1");
        job.setConcurrent("1");
        job.setStatus("1");
        R<Integer> jobIdR = remoteJobService.add(SecurityConstants.INNER, job);

        String time = dmsPlanLube.getCronExpression();
        //通过cron表达式获取下一次执行时间
        Date nextExecution = CronUtils.getNextExecution(time);
        dmsPlanLube.setLubeTime(nextExecution);
        dmsPlanLube.setJobId(jobIdR.getData().longValue());
        dmsPlanLube.setPlanLubeCode(planLubeCode);
        Long timeLimitDays = dmsPlanLube.getTimeLimitDays() == null ? 0L : dmsPlanLube.getTimeLimitDays();
        Long timeLimitHours = dmsPlanLube.getTimeLimitHours() == null ? 0L : dmsPlanLube.getTimeLimitHours();
        dmsPlanLube.setTimeLimit((timeLimitDays * 24 * 60 * 60)
                + (timeLimitHours * 60 * 60));
        dmsPlanLube.setIsFlag("1");
        dmsPlanLube.setCreateBy(SecurityUtils.getUsername());
        dmsPlanLube.setCreateTime(DateUtils.getNowDate());
        int rows = dmsPlanLubeMapper.insertDmsPlanLube(dmsPlanLube);

        return rows;
    }

    /**
     * 修改润滑计划
     *
     * @param dmsPlanLube 润滑计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateDmsPlanLube(DmsPlanLube dmsPlanLube) {
        Long jobId = dmsPlanLube.getJobId();
        String cronExpression = dmsPlanLube.getCronExpression();
        if (StringUtils.isNotEmpty(cronExpression)) {
            //通过cron表达式获取下一次执行时间
            Date nextExecution = CronUtils.getNextExecution(cronExpression);
            dmsPlanLube.setLubeTime(nextExecution);
            if (jobId == null) {
                String planLubeCode = dmsPlanLube.getPlanLubeCode();
                SysJob job = new SysJob();
                job.setJobName("润滑计划编号" + planLubeCode + "工单任务");
                job.setJobGroup("DEFAULT");
                job.setInvokeTarget("ryTask.getDmsBillsLube(\"" + planLubeCode + "\")");
                job.setCronExpression(dmsPlanLube.getCronExpression());
                job.setMisfirePolicy("1");
                job.setConcurrent("1");
                job.setStatus("1");
                R<Integer> jobIdR = remoteJobService.add(SecurityConstants.INNER, job);
                dmsPlanLube.setJobId(jobIdR.getData().longValue());
            } else {
                SysJob sysJob = remoteJobService.getJobInfo(jobId, SecurityConstants.INNER);
                if (sysJob != null) {
                    sysJob.setCronExpression(dmsPlanLube.getCronExpression());
                    remoteJobService.update(SecurityConstants.INNER, sysJob);
                }
            }
        }


        Long timeLimitDays = dmsPlanLube.getTimeLimitDays() == null ? 0L : dmsPlanLube.getTimeLimitDays();
        Long timeLimitHours = dmsPlanLube.getTimeLimitHours() == null ? 0L : dmsPlanLube.getTimeLimitHours();
        dmsPlanLube.setTimeLimit((timeLimitDays * 24 * 60 * 60)
                + (timeLimitHours * 60 * 60));
        dmsPlanLube.setUpdateBy(SecurityUtils.getUsername());
        dmsPlanLube.setUpdateTime(DateUtils.getNowDate());
        return dmsPlanLubeMapper.updateDmsPlanLube(dmsPlanLube);
    }

    /**
     * 批量删除润滑计划
     *
     * @param planLubeIds 需要删除的润滑计划主键
     * @return 结果
     */
    @Override
    public int deleteDmsPlanLubeByPlanLubeIds(Long[] planLubeIds) {
        return dmsPlanLubeMapper.deleteDmsPlanLubeByPlanLubeIds(planLubeIds);
    }

    /**
     * 删除润滑计划信息
     *
     * @param planLubeId 润滑计划主键
     * @return 结果
     */
    @Override
    public int deleteDmsPlanLubeByPlanLubeId(Long planLubeId) {
        return dmsPlanLubeMapper.deleteDmsPlanLubeByPlanLubeId(planLubeId);
    }
}
