package com.aucma.dms.service.impl;

//import com.hw.common.core.utils.DateUtils;
//import com.hw.common.security.utils.SecurityUtils;
//import com.aucma.dms.domain.DmsRepairInstance;
//import com.aucma.dms.domain.DmsRepairInstanceActivity;
//import com.aucma.dms.mapper.DmsPlanRepairMapper;
//import com.aucma.dms.mapper.DmsRepairInstanceActivityMapper;
//import com.aucma.dms.mapper.DmsRepairInstanceMapper;
//import com.aucma.dms.service.IDmsRepairInstanceActivityService;
//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.dms.domain.DmsRepairInstance;
import com.aucma.dms.domain.DmsRepairInstanceActivity;
import com.aucma.dms.mapper.DmsPlanRepairMapper;
import com.aucma.dms.mapper.DmsRepairInstanceActivityMapper;
import com.aucma.dms.mapper.DmsRepairInstanceMapper;
import com.aucma.dms.service.IDmsRepairInstanceActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 检修工单实例节点;最后一步作为检修记录Service业务层处理
 *
 * @author Open Platform
 * @date 2024-01-23
 */
@Service
public class DmsRepairInstanceActivityServiceImpl implements IDmsRepairInstanceActivityService
{
    @Autowired
    private DmsRepairInstanceActivityMapper dmsRepairInstanceActivityMapper;
    @Autowired
    private DmsRepairInstanceMapper dmsRepairInstanceMapper;
    @Autowired
    private DmsPlanRepairMapper dmsPlanRepairMapper;

    /**
     * 查询检修工单实例节点;最后一步作为检修记录
     *
     * @param instanceActivityId 检修工单实例节点;最后一步作为检修记录主键
     * @return 检修工单实例节点;最后一步作为检修记录
     */
    @Override
    public DmsRepairInstanceActivity selectDmsRepairInstanceActivityByInstanceActivityId(Long instanceActivityId)
    {
        return dmsRepairInstanceActivityMapper.selectDmsRepairInstanceActivityByInstanceActivityId(instanceActivityId);
    }

    /**
     * 查询检修工单实例节点;最后一步作为检修记录列表
     *
     * @param dmsRepairInstanceActivity 检修工单实例节点;最后一步作为检修记录
     * @return 检修工单实例节点;最后一步作为检修记录
     */
    @Override
    public List<DmsRepairInstanceActivity> selectDmsRepairInstanceActivityList(DmsRepairInstanceActivity dmsRepairInstanceActivity)
    {
        return dmsRepairInstanceActivityMapper.selectDmsRepairInstanceActivityList(dmsRepairInstanceActivity);
    }

    /**
     * 新增检修工单实例节点;最后一步作为检修记录
     *
     * @param dmsRepairInstanceActivity 检修工单实例节点;最后一步作为检修记录
     * @return 结果
     */
    @Override
    public int insertDmsRepairInstanceActivity(DmsRepairInstanceActivity dmsRepairInstanceActivity)
    {

        LoginUser user = SecurityUtils.getLoginUser();
        dmsRepairInstanceActivity.setCreateTime(DateUtils.getNowDate());
        dmsRepairInstanceActivity.setCreateBy(user.getUserId());
        dmsRepairInstanceActivity.setStartTime(DateUtils.getNowDate());
        dmsRepairInstanceActivity.setProcessHandleStatus("2");
        //如果是最后一步，则添加一条检修记录（检修记录暂时不用)
        //创建步骤2时修改步骤1的状态，处理人，处理时间等数据
        if (dmsRepairInstanceActivity.getProcessStepOrder()!=1){
            //最后一步 直接将
         if (dmsRepairInstanceActivity.getProcessStepOrder()==dmsRepairInstanceActivity.getWfLength()){
             dmsRepairInstanceActivity.setEndTime(DateUtils.getNowDate());
//             dmsRepairInstanceActivity.setHandleUserId(user.getUserId());
             dmsRepairInstanceActivity.setHandleBy(user.getUsername());
             dmsRepairInstanceActivity.setHandleTime(DateUtils.getNowDate());

             List<DmsRepairInstanceActivity> dmsRepairInstanceActivities = dmsRepairInstanceActivityMapper.selectDmsRepairInstanceActivityList(new DmsRepairInstanceActivity(dmsRepairInstanceActivity.getRepairInstanceId()));
             int i = dmsRepairInstanceActivityMapper.insertDmsRepairInstanceActivity(dmsRepairInstanceActivity);
             for (DmsRepairInstanceActivity dmsRepairInstanceActivityFor:dmsRepairInstanceActivities){
                    dmsRepairInstanceActivityFor.setProcessHandleStatus("0");
                    dmsRepairInstanceActivityMapper.updateDmsRepairInstanceActivity(dmsRepairInstanceActivityFor);
                }
             DmsRepairInstance dmsRepairInstance = dmsRepairInstanceMapper.selectDmsRepairInstanceByRepairInstanceId(dmsRepairInstanceActivity.getRepairInstanceId());
                dmsRepairInstance.setBillsStatus("2");
                dmsRepairInstance.setRealEndTime(DateUtils.getNowDate());
                dmsRepairInstanceMapper.updateDmsRepairInstance(dmsRepairInstance);
//             //更新本次计划时间，当上一个计划生成的工单结束时，本机计划时间为下一个计划工单的时间
//             DmsPlanRepair dmsPlanRepair = dmsPlanRepairMapper.selectDmsPlanRepairByPlanRepairId(dmsRepairInstance.getPlanRepairId());
//                //根据jobid获取cron表达式
//             String cron = dmsPlanRepairMapper.getCron(dmsPlanRepair.getJobId());
//             dmsPlanRepair.setRepairTime(CronUtils.getNextExecution(cron));
//             dmsPlanRepairMapper.updateDmsPlanRepair(dmsPlanRepair);
             return i;
         }
         //中间步骤
         else {
             if (dmsRepairInstanceActivity.getProcessStepOrder()==2){
                 DmsRepairInstance dmsRepairInstance = dmsRepairInstanceMapper.selectDmsRepairInstanceByRepairInstanceId(dmsRepairInstanceActivity.getRepairInstanceId());
                 dmsRepairInstance.setRealBeginTime(DateUtils.getNowDate());
                 dmsRepairInstanceMapper.updateDmsRepairInstance(dmsRepairInstance);
             }
             DmsRepairInstanceActivity dmsRepairInstanceActivityOrderSmall = new DmsRepairInstanceActivity();
             dmsRepairInstanceActivityOrderSmall.setInstanceActivityId(dmsRepairInstanceActivity.getRepairInstanceId());
             dmsRepairInstanceActivityOrderSmall.setProcessStepOrder(dmsRepairInstanceActivity.getProcessStepOrder() - 1);
             List<DmsRepairInstanceActivity> dmsRepairInstanceActivities = dmsRepairInstanceActivityMapper.selectDmsRepairInstanceActivityList(dmsRepairInstanceActivityOrderSmall);
             dmsRepairInstanceActivities.get(0).setProcessHandleStatus("3");
             dmsRepairInstanceActivities.get(0).setEndTime(DateUtils.getNowDate());
             dmsRepairInstanceActivities.get(0).setHandleTime(DateUtils.getNowDate());
             dmsRepairInstanceActivities.get(0).setHandleBy(user.getUsername());
             dmsRepairInstanceActivities.get(0).setHandleUserId(user.getUserId());
             dmsRepairInstanceActivityMapper.updateDmsRepairInstanceActivity(dmsRepairInstanceActivities.get(0));
         }
        }
        return dmsRepairInstanceActivityMapper.insertDmsRepairInstanceActivity(dmsRepairInstanceActivity);
    }

    /**
     * 修改检修工单实例节点;最后一步作为检修记录
     *
     * @param dmsRepairInstanceActivity 检修工单实例节点;最后一步作为检修记录
     * @return 结果
     */
    @Override
    public int updateDmsRepairInstanceActivity(DmsRepairInstanceActivity dmsRepairInstanceActivity)
    {
        LoginUser user = SecurityUtils.getLoginUser();
        //第一步
        if (dmsRepairInstanceActivity.getProcessStepOrder()==1){
            dmsRepairInstanceActivity.setProcessHandleStatus("2");
            dmsRepairInstanceActivity.setStartTime(DateUtils.getNowDate());
            dmsRepairInstanceActivity.setHandleUserId(user.getUserId());
            dmsRepairInstanceActivity.setHandleBy(user.getUsername());
            dmsRepairInstanceActivity.setHandleTime(DateUtils.getNowDate());
        }
        //最后一步
        else if (dmsRepairInstanceActivity.getRepairInstanceId()==dmsRepairInstanceActivity.getWfLength()){
            List<DmsRepairInstanceActivity> dmsRepairInstanceActivities = dmsRepairInstanceActivityMapper.selectDmsRepairInstanceActivityList(new DmsRepairInstanceActivity(dmsRepairInstanceActivity.getRepairInstanceId()));
            for (DmsRepairInstanceActivity dmsRepairInstanceActivityFor:dmsRepairInstanceActivities){
                dmsRepairInstanceActivityFor.setProcessHandleStatus("0");
            }
            dmsRepairInstanceActivity.setEndTime(DateUtils.getNowDate());
            dmsRepairInstanceActivity.setHandleUserId(user.getUserId());
            dmsRepairInstanceActivity.setHandleBy(user.getUsername());
            dmsRepairInstanceActivity.setHandleTime(DateUtils.getNowDate());
        }
        else{
            DmsRepairInstanceActivity dmsRepairInstanceActivity2 = new DmsRepairInstanceActivity();
            //查找上一步骤
            dmsRepairInstanceActivity2.setProcessStepOrder(dmsRepairInstanceActivity.getProcessStepOrder()-1);
            dmsRepairInstanceActivity2.setRepairInstanceId(dmsRepairInstanceActivity.getRepairInstanceId());
            //修改上一步骤的状态与结束时间
            List<DmsRepairInstanceActivity> dmsRepairInstanceActivities = dmsRepairInstanceActivityMapper.selectDmsRepairInstanceActivityList(dmsRepairInstanceActivity2);
            dmsRepairInstanceActivities.get(0).setProcessHandleStatus("3");
            dmsRepairInstanceActivities.get(0).setEndTime(DateUtils.getNowDate());
            //
            dmsRepairInstanceActivity.setProcessHandleStatus("2");
            dmsRepairInstanceActivity.setStartTime(DateUtils.getNowDate());
            dmsRepairInstanceActivity.setHandleUserId(user.getUserId());
            dmsRepairInstanceActivity.setHandleBy(user.getUsername());
            dmsRepairInstanceActivity.setHandleTime(DateUtils.getNowDate());
        }
        return dmsRepairInstanceActivityMapper.updateDmsRepairInstanceActivity(dmsRepairInstanceActivity);
    }

    /**
     * 批量删除检修工单实例节点;最后一步作为检修记录
     *
     * @param instanceActivityIds 需要删除的检修工单实例节点;最后一步作为检修记录主键
     * @return 结果
     */
    @Override
    public int deleteDmsRepairInstanceActivityByInstanceActivityIds(Long[] instanceActivityIds)
    {
        return dmsRepairInstanceActivityMapper.deleteDmsRepairInstanceActivityByInstanceActivityIds(instanceActivityIds);
    }
    @Override
    public int deleteDmsRepairInstanceActivityByRepairInstanceIds(Long[] repairInstanceIds)
    {
        return dmsRepairInstanceActivityMapper.deleteDmsRepairInstanceActivityByRepairInstanceIds(repairInstanceIds);
    }

    @Override
    public boolean selectUserIdByRepairInstanceId(Long repairInstanceId,Long userId) {
        Long[] integers = dmsRepairInstanceActivityMapper.selectUserIdByRepairInstanceId(repairInstanceId);
        boolean contains = Arrays.asList(integers).contains(userId);

        return contains;
    }

    /**
     * 删除检修工单实例节点;最后一步作为检修记录信息
     *
     * @param instanceActivityId 检修工单实例节点;最后一步作为检修记录主键
     * @return 结果
     */
    @Override
    public int deleteDmsRepairInstanceActivityByInstanceActivityId(Long instanceActivityId)
    {
        return dmsRepairInstanceActivityMapper.deleteDmsRepairInstanceActivityByInstanceActivityId(instanceActivityId);
    }

}
