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.utils.DateUtils;
import com.hw.common.core.web.domain.AjaxResult;
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 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;

    /**
     * 查询巡检计划信息
     *
     * @param planInspectId 巡检计划信息主键
     * @return 巡检计划信息
     */
    @Override
    public DmsPlanInspect selectDmsPlanInspectByPlanInspectId(Long planInspectId)
    {
        return dmsPlanInspectMapper.selectDmsPlanInspectByPlanInspectId(planInspectId);
    }

    /**
     * 查询巡检计划信息列表
     *
     * @param dmsPlanInspect 巡检计划信息
     * @return 巡检计划信息
     */
    @Override
    public List<DmsPlanInspect> selectDmsPlanInspectList(DmsPlanInspect dmsPlanInspect)
    {
        return dmsPlanInspectMapper.selectDmsPlanInspectList(dmsPlanInspect);
    }

    /**
     * 新增巡检计划信息
     *
     * @param dmsPlanInspect 巡检计划信息
     * @return 结果
     */
    @Override
    public int insertDmsPlanInspect(DmsPlanInspect dmsPlanInspect)
    {
        String time = dmsPlanInspect.getCronExpression();
        //通过cron表达式获取下一次执行时间
        Date nextExecution = CronUtils.getNextExecution(time);
        dmsPlanInspect.setPlanTime(nextExecution);
        LoginUser user = SecurityUtils.getLoginUser();
        dmsPlanInspect.setCreateBy(user.getUsername());
        dmsPlanInspect.setIsFlag("1");
        dmsPlanInspect.setCreateTime(DateUtils.getNowDate());
        int i = dmsPlanInspectMapper.insertDmsPlanInspect(dmsPlanInspect);

        List<DmsPlanInspect> dmsPlanInspects = dmsPlanInspectMapper.selectDmsPlanInspectList(dmsPlanInspect);
        DmsPlanInspect dmsPlanInspect1 = dmsPlanInspects.get(0);
        SysJob job = new SysJob();
        job.setJobName("创建计划编号为"+dmsPlanInspect1.getPlanInspectCode()+"工单");
        job.setJobGroup("DEFAULT");
        job.setInvokeTarget("ryTask.getDmsRepairInstance(\""+dmsPlanInspect1.getPlanInspectId()+"\")");
        job.setCronExpression(dmsPlanInspect1.getCronExpression());
        job.setMisfirePolicy("1");
        job.setConcurrent("1");
        job.setStatus("1");
        remoteJobService.add(SecurityConstants.INNER,job);

        return i;
    }

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