package com.hw.dms.service.impl;

import java.util.Arrays;
import java.util.List;
import com.hw.common.core.utils.DateUtils;
import com.hw.common.security.utils.SecurityUtils;
import com.hw.dms.domain.*;
import com.hw.dms.mapper.DmsBillsFaultInstanceMapper;
import com.hw.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hw.dms.mapper.DmsFaultInstanceActivityMapper;
import com.hw.dms.service.IDmsFaultInstanceActivityService;

/**
 * 故障报修工单实例节点Service业务层处理
 *
 * @author Open Platform
 * @date 2024-02-27
 */
@Service
public class DmsFaultInstanceActivityServiceImpl implements IDmsFaultInstanceActivityService
{
    @Autowired
    private DmsFaultInstanceActivityMapper dmsFaultInstanceActivityMapper;
    @Autowired
    private DmsBillsFaultInstanceMapper dmsBillsFaultInstanceMapper;

    /**
     * 查询故障报修工单实例节点
     *
     * @param instanceActivityId 故障报修工单实例节点主键
     * @return 故障报修工单实例节点
     */
    @Override
    public DmsFaultInstanceActivity selectDmsFaultInstanceActivityByInstanceActivityId(Long instanceActivityId)
    {
        return dmsFaultInstanceActivityMapper.selectDmsFaultInstanceActivityByInstanceActivityId(instanceActivityId);
    }

    /**
     * 查询故障报修工单实例节点列表
     *
     * @param dmsFaultInstanceActivity 故障报修工单实例节点
     * @return 故障报修工单实例节点
     */
    @Override
    public List<DmsFaultInstanceActivity> selectDmsFaultInstanceActivityList(DmsFaultInstanceActivity dmsFaultInstanceActivity)
    {
        return dmsFaultInstanceActivityMapper.selectDmsFaultInstanceActivityList(dmsFaultInstanceActivity);
    }

    /**
     * 新增故障报修工单实例节点
     *
     * @param dmsFaultInstanceActivity 故障报修工单实例节点
     * @return 结果
     */
    @Override
    public int insertDmsFaultInstanceActivity(DmsFaultInstanceActivity dmsFaultInstanceActivity)
    {
        LoginUser user = SecurityUtils.getLoginUser();
        dmsFaultInstanceActivity.setCreateBy(user.getUsername());
        dmsFaultInstanceActivity.setStartTime(DateUtils.getNowDate());
        dmsFaultInstanceActivity.setCreateTime(DateUtils.getNowDate());
        dmsFaultInstanceActivity.setProcessHandleStatus("2");
        if (dmsFaultInstanceActivity.getProcessStepOrder()!=1){
            //最后一步 直接将
            if (dmsFaultInstanceActivity.getProcessStepOrder()==dmsFaultInstanceActivity.getWfLength()){
                dmsFaultInstanceActivity.setEndTime(DateUtils.getNowDate());
                dmsFaultInstanceActivity.setHandleUserId(user.getUserid());
                dmsFaultInstanceActivity.setHandleBy(user.getUsername());
                dmsFaultInstanceActivity.setHandleTime(DateUtils.getNowDate());
//                DmsBillsFaultInstance dms = new DmsBillsFaultInstance();
                DmsFaultInstanceActivity dms = new DmsFaultInstanceActivity();
                dms.setRepairInstanceId(dmsFaultInstanceActivity.getRepairInstanceId());
                List<DmsFaultInstanceActivity> dmsFaultInstanceActivities = dmsFaultInstanceActivityMapper.selectDmsFaultInstanceActivityList(dms);
                int i = dmsFaultInstanceActivityMapper.insertDmsFaultInstanceActivity(dmsFaultInstanceActivity);
                for (DmsFaultInstanceActivity dmsBillsLubeInstanceActivityFor :dmsFaultInstanceActivities) {
                        dmsBillsLubeInstanceActivityFor.setProcessHandleStatus("0");
                        dmsFaultInstanceActivityMapper.updateDmsFaultInstanceActivity(dmsBillsLubeInstanceActivityFor);
                }
                DmsBillsFaultInstance dmsBillsFaultInstance = dmsBillsFaultInstanceMapper.selectDmsBillsFaultInstanceByRepairInstanceId(dmsFaultInstanceActivity.getRepairInstanceId());
                dmsBillsFaultInstance.setBillsStatus("2");
                dmsBillsFaultInstance.setRealEndTime(DateUtils.getNowDate());
                dmsBillsFaultInstanceMapper.updateDmsBillsFaultInstance(dmsBillsFaultInstance);


                return i;
            }
            //中间步骤
            else {
                if (dmsFaultInstanceActivity.getProcessStepOrder()==2) {
                    DmsBillsFaultInstance dmsBillsFaultInstance = dmsBillsFaultInstanceMapper.selectDmsBillsFaultInstanceByRepairInstanceId(dmsFaultInstanceActivity.getRepairInstanceId());
                    dmsBillsFaultInstance.setRealBeginTime(DateUtils.getNowDate());
                    dmsBillsFaultInstanceMapper.updateDmsBillsFaultInstance(dmsBillsFaultInstance);
                }
                DmsFaultInstanceActivity dmsFaultInstanceActivityOrderSmall = new DmsFaultInstanceActivity();
                dmsFaultInstanceActivityOrderSmall.setRepairInstanceId(dmsFaultInstanceActivity.getRepairInstanceId());
                dmsFaultInstanceActivityOrderSmall.setProcessStepOrder(dmsFaultInstanceActivity.getProcessStepOrder() - 1);
                List<DmsFaultInstanceActivity> dmsFaultInstanceActivities = dmsFaultInstanceActivityMapper.selectDmsFaultInstanceActivityList(dmsFaultInstanceActivityOrderSmall);
                dmsFaultInstanceActivities.get(0).setProcessHandleStatus("3");
                dmsFaultInstanceActivities.get(0).setEndTime(DateUtils.getNowDate());
                dmsFaultInstanceActivities.get(0).setHandleTime(DateUtils.getNowDate());
                dmsFaultInstanceActivities.get(0).setHandleBy(user.getUsername());
                dmsFaultInstanceActivities.get(0).setHandleUserId(user.getUserid());
                dmsFaultInstanceActivityMapper.updateDmsFaultInstanceActivity(dmsFaultInstanceActivities.get(0));
            }
        }
        return dmsFaultInstanceActivityMapper.insertDmsFaultInstanceActivity(dmsFaultInstanceActivity);
    }

    /**
     * 修改故障报修工单实例节点
     *
     * @param dmsFaultInstanceActivity 故障报修工单实例节点
     * @return 结果
     */
    @Override
    public int updateDmsFaultInstanceActivity(DmsFaultInstanceActivity dmsFaultInstanceActivity)
    {
        return dmsFaultInstanceActivityMapper.updateDmsFaultInstanceActivity(dmsFaultInstanceActivity);
    }

    /**
     * 批量删除故障报修工单实例节点
     *
     * @param instanceActivityIds 需要删除的故障报修工单实例节点主键
     * @return 结果
     */
    @Override
    public int deleteDmsFaultInstanceActivityByInstanceActivityIds(Long[] instanceActivityIds)
    {
        return dmsFaultInstanceActivityMapper.deleteDmsFaultInstanceActivityByInstanceActivityIds(instanceActivityIds);
    }

    /**
     * 删除故障报修工单实例节点信息
     *
     * @param instanceActivityId 故障报修工单实例节点主键
     * @return 结果
     */
    @Override
    public int deleteDmsFaultInstanceActivityByInstanceActivityId(Long instanceActivityId)
    {
        return dmsFaultInstanceActivityMapper.deleteDmsFaultInstanceActivityByInstanceActivityId(instanceActivityId);
    }

    @Override
    public boolean selectUserIdByDmsFaultInstanceActivity(Long repairInstanceId, Long userId) {
        Long[] longs = dmsFaultInstanceActivityMapper.selectUserIdByDmsFaultInstanceActivity(repairInstanceId);
        boolean contains = Arrays.asList(longs).contains(userId);

        return contains;


    }
}
