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.DmsBillsMaintInstance;
//import com.aucma.dms.domain.DmsMaintInstanceActivity;
//import com.aucma.dms.mapper.DmsBillsMaintInstanceMapper;
//import com.aucma.dms.mapper.DmsMaintInstanceActivityMapper;
//import com.aucma.dms.service.IDmsMaintInstanceActivityService;
//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.DmsBillsMaintInstance;
import com.aucma.dms.domain.DmsMaintInstanceActivity;
import com.aucma.dms.mapper.DmsBillsMaintInstanceMapper;
import com.aucma.dms.mapper.DmsMaintInstanceActivityMapper;
import com.aucma.dms.service.IDmsMaintInstanceActivityService;
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-02-29
 */
@Service
public class DmsMaintInstanceActivityServiceImpl implements IDmsMaintInstanceActivityService
{
    @Autowired
    private DmsMaintInstanceActivityMapper dmsMaintInstanceActivityMapper;
    @Autowired
    private DmsBillsMaintInstanceMapper dmsBillsMaintInstanceMapper;

    /**
     * 查询保养工单实例节点
     *
     * @param instanceActivityId 保养工单实例节点主键
     * @return 保养工单实例节点
     */
    @Override
    public DmsMaintInstanceActivity selectDmsMaintInstanceActivityByInstanceActivityId(Long instanceActivityId)
    {
        return dmsMaintInstanceActivityMapper.selectDmsMaintInstanceActivityByInstanceActivityId(instanceActivityId);
    }

    /**
     * 查询保养工单实例节点列表
     *
     * @param dmsMaintInstanceActivity 保养工单实例节点
     * @return 保养工单实例节点
     */
    @Override
    public List<DmsMaintInstanceActivity> selectDmsMaintInstanceActivityList(DmsMaintInstanceActivity dmsMaintInstanceActivity)
    {
        return dmsMaintInstanceActivityMapper.selectDmsMaintInstanceActivityList(dmsMaintInstanceActivity);
    }

    /**
     * 新增保养工单实例节点
     *
     * @param dmsMaintInstanceActivity 保养工单实例节点
     * @return 结果
     */
    @Override
    public int insertDmsMaintInstanceActivity(DmsMaintInstanceActivity dmsMaintInstanceActivity)
    {
        LoginUser user = SecurityUtils.getLoginUser();
        dmsMaintInstanceActivity.setCreateBy(user.getUserId());
        dmsMaintInstanceActivity.setCreateTime(DateUtils.getNowDate());
        dmsMaintInstanceActivity.setStartTime(DateUtils.getNowDate());
        dmsMaintInstanceActivity.setProcessHandleStatus("2");
        //如果是最后一步，则添加一条检修记录（检修记录暂时不用)
        //创建步骤2时修改步骤1的状态，处理人，处理时间等数据
        if (dmsMaintInstanceActivity.getProcessStepOrder()!=1){
            //最后一步 直接将
            if (dmsMaintInstanceActivity.getProcessStepOrder()==dmsMaintInstanceActivity.getWfLength()){
                dmsMaintInstanceActivity.setEndTime(DateUtils.getNowDate());
                dmsMaintInstanceActivity.setHandleUserId(user.getUserId());
                dmsMaintInstanceActivity.setHandleBy(user.getUsername());
                dmsMaintInstanceActivity.setHandleTime(DateUtils.getNowDate());
                DmsMaintInstanceActivity dms = new DmsMaintInstanceActivity();
                dms.setInstanceActivityId(dmsMaintInstanceActivity.getInstanceActivityId());
                List<DmsMaintInstanceActivity> dmsMaintInstanceActivities = dmsMaintInstanceActivityMapper.selectDmsMaintInstanceActivityList(dms);
                int i = dmsMaintInstanceActivityMapper.insertDmsMaintInstanceActivity(dmsMaintInstanceActivity);
                for (DmsMaintInstanceActivity dmsRepairInstanceActivityFor:dmsMaintInstanceActivities){
                    dmsRepairInstanceActivityFor.setProcessHandleStatus("0");
                    dmsMaintInstanceActivityMapper.updateDmsMaintInstanceActivity(dmsRepairInstanceActivityFor);
                }
                DmsBillsMaintInstance dmsBillsMaintInstance = dmsBillsMaintInstanceMapper.selectDmsBillsMaintInstanceByMaintInstanceId(dmsMaintInstanceActivity.getMaintInstanceId());
                dmsBillsMaintInstance.setBillsMaintCode("2");
                dmsBillsMaintInstance.setRealEndTime(DateUtils.getNowDate());
                dmsBillsMaintInstanceMapper.updateDmsBillsMaintInstance(dmsBillsMaintInstance);

//             //更新本次计划时间，当上一个计划生成的工单结束时，本机计划时间为下一个计划工单的时间
//             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 (dmsMaintInstanceActivity.getProcessStepOrder()==2){
                    DmsBillsMaintInstance dmsBillsMaintInstance = dmsBillsMaintInstanceMapper.selectDmsBillsMaintInstanceByMaintInstanceId(dmsMaintInstanceActivity.getMaintInstanceId());
                    dmsBillsMaintInstance.setRealBeginTime(DateUtils.getNowDate());
                    dmsBillsMaintInstanceMapper.updateDmsBillsMaintInstance(dmsBillsMaintInstance);
                }
                DmsMaintInstanceActivity dmsMaintInstanceActivityOrderSmall = new DmsMaintInstanceActivity();
                dmsMaintInstanceActivityOrderSmall.setMaintInstanceId(dmsMaintInstanceActivity.getMaintInstanceId());
                dmsMaintInstanceActivityOrderSmall.setProcessStepOrder(dmsMaintInstanceActivity.getProcessStepOrder() - 1);
                List<DmsMaintInstanceActivity> dmsMaintInstanceActivities = dmsMaintInstanceActivityMapper.selectDmsMaintInstanceActivityList(dmsMaintInstanceActivityOrderSmall);
                dmsMaintInstanceActivities.get(0).setProcessHandleStatus("3");
                dmsMaintInstanceActivities.get(0).setEndTime(DateUtils.getNowDate());
                dmsMaintInstanceActivities.get(0).setHandleBy(user.getUsername());
                dmsMaintInstanceActivities.get(0).setHandleUserId(user.getUserId());
                dmsMaintInstanceActivityMapper.updateDmsMaintInstanceActivity(dmsMaintInstanceActivities.get(0));


            }
        }
        return dmsMaintInstanceActivityMapper.insertDmsMaintInstanceActivity(dmsMaintInstanceActivity);
    }

    /**
     * 修改保养工单实例节点
     *
     * @param dmsMaintInstanceActivity 保养工单实例节点
     * @return 结果
     */
    @Override
    public int updateDmsMaintInstanceActivity(DmsMaintInstanceActivity dmsMaintInstanceActivity)
    {
        return dmsMaintInstanceActivityMapper.updateDmsMaintInstanceActivity(dmsMaintInstanceActivity);
    }

    /**
     * 批量删除保养工单实例节点
     *
     * @param instanceActivityIds 需要删除的保养工单实例节点主键
     * @return 结果
     */
    @Override
    public int deleteDmsMaintInstanceActivityByInstanceActivityIds(Long[] instanceActivityIds)
    {
        return dmsMaintInstanceActivityMapper.deleteDmsMaintInstanceActivityByInstanceActivityIds(instanceActivityIds);
    }

    /**
     * 删除保养工单实例节点信息
     *
     * @param instanceActivityId 保养工单实例节点主键
     * @return 结果
     */
    @Override
    public int deleteDmsMaintInstanceActivityByInstanceActivityId(Long instanceActivityId)
    {
        return dmsMaintInstanceActivityMapper.deleteDmsMaintInstanceActivityByInstanceActivityId(instanceActivityId);
    }



    @Override
    public boolean selectUserIdByDmsMaintInstanceActivityId(Long maintInstanceId, Long userId) {
        Long[] longs = dmsMaintInstanceActivityMapper.selectUserIdByDmsMaintInstanceActivityId(maintInstanceId);
        boolean contains = Arrays.asList(longs).contains(userId);
        return contains;
    }

    @Override
    public List<DmsMaintInstanceActivity> selectDmsMaintInfo(Long maintInstanceId) {
        return null;
    }


}
