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.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 java.util.ArrayList;
import com.hw.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.hw.dms.domain.DmsPlanRepairDetail;
import com.hw.dms.mapper.DmsPlanRepairMapper;
import com.hw.dms.domain.DmsPlanRepair;
import com.hw.dms.service.IDmsPlanRepairService;

/**
 * 检修计划信息Service业务层处理
 *
 * @author Open Platform
 * @date 2024-01-15
 */
@Service
public class DmsPlanRepairServiceImpl implements IDmsPlanRepairService
{
    @Autowired
    private DmsPlanRepairMapper dmsPlanRepairMapper;

    @Autowired
    private RemoteJobService remoteJobService;

    /**
     * 查询检修计划信息
     *
     * @param planRepairId 检修计划信息主键
     * @return 检修计划信息
     */
    @Override
    public DmsPlanRepair selectDmsPlanRepairByPlanRepairId(Long planRepairId)
    {

        DmsPlanRepair dmsPlanRepair = dmsPlanRepairMapper.selectDmsPlanRepairByPlanRepairId(planRepairId);


            Long timeLimit = dmsPlanRepair.getTimeLimit();
            if (timeLimit !=null){
                Long days = timeLimit / (24 * 60 * 60l);
                Long hours = timeLimit % (24 * 60 * 60l);
//            System.out.println(days);
//            System.out.println(hours);
                dmsPlanRepair.setTimeLimitDays(days);
                dmsPlanRepair.setTimeLimitHours(hours/(60*60l));
            }

        return dmsPlanRepair;
    }

    /**
     * 查询检修计划信息列表
     *
     * @param dmsPlanRepair 检修计划信息
     * @return 检修计划信息
     */
    @Override
    public List<DmsPlanRepair> selectDmsPlanRepairList(DmsPlanRepair dmsPlanRepair)
    {
        List<DmsPlanRepair> dmsPlanRepairs = dmsPlanRepairMapper.selectDmsPlanRepairList(dmsPlanRepair);
        for (DmsPlanRepair dmsPlanReapirFor : dmsPlanRepairs){
            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 dmsPlanRepairs;
    }

    /**
     * 新增检修计划信息
     *
     * @param dmsPlanRepair 检修计划信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDmsPlanRepair(DmsPlanRepair dmsPlanRepair)
    {

//        String planRepairCode = Seq.getId(Seq.planRepairSeqType, Seq.planRepairCode);
//
//        SysJob job = new SysJob();
//        job.setJobName("保养计划编号" + planRepairCode + "工单任务");
//        job.setJobGroup("DEFAULT");
//        job.setInvokeTarget("ryTask.getDmsRepairInstance(\"" + planRepairCode + "\")");
//        job.setCronExpression(dmsPlanRepair.getCronExpression());
//        job.setMisfirePolicy("1");
//        job.setConcurrent("1");
//        job.setStatus("1");
//        R<Integer> jobIdR = remoteJobService.add(SecurityConstants.INNER, job);
//
//        String time = dmsPlanRepair.getCronExpression();
//        Date nextExecution = CronUtils.getNextExecution(time);
//        dmsPlanRepair.setRepairTime(nextExecution);
//        dmsPlanRepair.setJobId(jobIdR.getData().longValue());
//        dmsPlanRepair.setPlanRepairCode(planRepairCode);
        dmsPlanRepair.setTimeLimit((dmsPlanRepair.getTimeLimitDays()*24*60*60)
                +(dmsPlanRepair.getTimeLimitHours()*60*60));

        dmsPlanRepair.setCreateTime(DateUtils.getNowDate());
        int rows = dmsPlanRepairMapper.insertDmsPlanRepair(dmsPlanRepair);
        insertDmsPlanRepairDetail(dmsPlanRepair);
        return rows;
    }

    /**
     * 修改检修计划信息
     *
     * @param dmsPlanRepair 检修计划信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDmsPlanRepair(DmsPlanRepair dmsPlanRepair)
    {

        dmsPlanRepair.setTimeLimit((dmsPlanRepair.getTimeLimitDays()*24*60*60)
                +(dmsPlanRepair.getTimeLimitHours()*60*60));
        dmsPlanRepair.setUpdateTime(DateUtils.getNowDate());
        dmsPlanRepairMapper.deleteDmsPlanRepairDetailByPlanRepairId(dmsPlanRepair.getPlanRepairId());
        insertDmsPlanRepairDetail(dmsPlanRepair);
        return dmsPlanRepairMapper.updateDmsPlanRepair(dmsPlanRepair);
    }

    /**
     * 批量删除检修计划信息
     *
     * @param planRepairIds 需要删除的检修计划信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDmsPlanRepairByPlanRepairIds(Long[] planRepairIds)
    {
        dmsPlanRepairMapper.deleteDmsPlanRepairDetailByPlanRepairIds(planRepairIds);
        return dmsPlanRepairMapper.deleteDmsPlanRepairByPlanRepairIds(planRepairIds);
    }

    /**
     * 删除检修计划信息信息
     *
     * @param planRepairId 检修计划信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDmsPlanRepairByPlanRepairId(Long planRepairId)
    {
        dmsPlanRepairMapper.deleteDmsPlanRepairDetailByPlanRepairId(planRepairId);
        return dmsPlanRepairMapper.deleteDmsPlanRepairByPlanRepairId(planRepairId);
    }

    /**
     * 新增检修计划明细信息
     *
     * @param dmsPlanRepair 检修计划信息对象
     */
    public void insertDmsPlanRepairDetail(DmsPlanRepair dmsPlanRepair)
    {
        List<DmsPlanRepairDetail> dmsPlanRepairDetailList = dmsPlanRepair.getDmsPlanRepairDetailList();
        Long planRepairId = dmsPlanRepair.getPlanRepairId();
        if (StringUtils.isNotNull(dmsPlanRepairDetailList))
        {
            List<DmsPlanRepairDetail> list = new ArrayList<DmsPlanRepairDetail>();
            for (DmsPlanRepairDetail dmsPlanRepairDetail : dmsPlanRepairDetailList)
            {
                dmsPlanRepairDetail.setPlanRepairId(planRepairId);
                list.add(dmsPlanRepairDetail);
            }
            if (list.size() > 0)
            {
                dmsPlanRepairMapper.batchDmsPlanRepairDetail(list);
            }
        }
    }
}
