package com.aucma.dms.service.impl;

//import com.hw.common.core.constant.DmsConstants;
//import com.hw.common.core.utils.DateUtils;
//import com.hw.common.security.utils.SecurityUtils;
//import com.aucma.dms.domain.*;
//import com.aucma.dms.mapper.DmsBillsFaultInstanceMapper;
//import com.aucma.dms.mapper.DmsFaultInstanceActivityMapper;
//import com.aucma.dms.mapper.DmsInstanceFileMapper;
//import com.aucma.dms.service.IDmsFaultInstanceActivityService;
//import com.hw.system.api.model.LoginUser;

import com.aucma.common.constant.DmsConstants;
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.DmsBillsFaultInstance;
import com.aucma.dms.domain.DmsFaultInstanceActivity;
import com.aucma.dms.domain.DmsInstanceFile;
import com.aucma.dms.mapper.DmsBillsFaultInstanceMapper;
import com.aucma.dms.mapper.DmsFaultInstanceActivityMapper;
import com.aucma.dms.mapper.DmsInstanceFileMapper;
import com.aucma.dms.service.IDmsFaultInstanceActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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

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

    /**
     * 查询故障报修工单实例节点列表
     *
     * @param dmsFaultInstanceActivity 故障报修工单实例节点
     * @return 故障报修工单实例节点
     */
    @Override
    public List<DmsFaultInstanceActivity> selectDmsFaultInstanceActivityList(DmsFaultInstanceActivity dmsFaultInstanceActivity)
    {
        List<DmsFaultInstanceActivity> dmsFaultInstanceActivities = dmsFaultInstanceActivityMapper.selectDmsFaultInstanceActivityList(dmsFaultInstanceActivity);
        DmsFaultInstanceActivity dmsFaultInstanceActivity1 = dmsFaultInstanceActivities.get(0);
        DmsInstanceFile queryInstanceFile = new DmsInstanceFile();
        queryInstanceFile.setTargetType(DmsConstants.DMS_INSTANCE_FILE_TARGET_TYPE_FAULT_INSTANCE_ACTIVITY);
        queryInstanceFile.setTargetId(dmsFaultInstanceActivity1.getInstanceActivityId());
        List<DmsInstanceFile> dmsInstanceFiles = dmsInstanceFileMapper.selectDmsInstanceFileConvertList(queryInstanceFile);
        dmsFaultInstanceActivity1.setDmsInstanceFileList(dmsInstanceFiles);
        List<DmsFaultInstanceActivity> dmsFaultInstanceActivities1 = new ArrayList<>();
        if (dmsFaultInstanceActivities.size()>1){
            DmsFaultInstanceActivity dmsFaultInstanceActivity2 = dmsFaultInstanceActivities.get(1);
            DmsInstanceFile queryInstanceFile1 = new DmsInstanceFile();
            queryInstanceFile1.setTargetType("2");
            queryInstanceFile1.setTargetId(dmsFaultInstanceActivity1.getInstanceActivityId());
            List<DmsInstanceFile> dmsInstanceFiles2 = dmsInstanceFileMapper.selectDmsInstanceFileConvertList(queryInstanceFile1);
            dmsFaultInstanceActivity2.setDmsInstanceFileList(dmsInstanceFiles2);
            dmsFaultInstanceActivities1.add(dmsFaultInstanceActivity2);
        }
//        DmsFaultInstanceActivity dmsFaultInstanceActivity2 = dmsFaultInstanceActivities.get(1);
//        DmsInstanceFile queryInstanceFile1 = new DmsInstanceFile();
//        queryInstanceFile1.setTargetType("2");
//        queryInstanceFile1.setTargetId(dmsFaultInstanceActivity1.getInstanceActivityId());
//        List<DmsInstanceFile> dmsInstanceFiles2 = dmsInstanceFileMapper.selectDmsInstanceFileConvertList(queryInstanceFile1);
//        dmsFaultInstanceActivity2.setDmsInstanceFileList(dmsInstanceFiles2);
//        List<DmsFaultInstanceActivity> dmsFaultInstanceActivities1 = new ArrayList<>();
        dmsFaultInstanceActivities1.add(dmsFaultInstanceActivity1);
//        dmsFaultInstanceActivities1.add(dmsFaultInstanceActivity2);
        return dmsFaultInstanceActivities1;
    }

    /**
     * 新增故障报修工单实例节点
     *
     * @param dmsFaultInstanceActivity 故障报修工单实例节点
     * @return 结果
     */
    @Override
    public int insertDmsFaultInstanceActivity(DmsFaultInstanceActivity dmsFaultInstanceActivity)
    {
        LoginUser user = SecurityUtils.getLoginUser();
        dmsFaultInstanceActivity.setCreateBy(user.getUserId());
        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;


    }
}
