package com.hw.dms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.hw.common.core.constant.DmsConstants;
import com.hw.common.core.constant.WfConstants;
import com.hw.common.core.utils.DateUtils;
import com.hw.common.core.utils.uuid.Seq;
import com.hw.common.security.utils.SecurityUtils;
import com.hw.dms.domain.DmsFaultInstanceActivity;
import com.hw.dms.domain.DmsInstanceFile;
import com.hw.dms.mapper.DmsFaultInstanceActivityMapper;
import com.hw.dms.mapper.DmsInstanceFileMapper;
import com.hw.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hw.dms.mapper.DmsBillsFaultInstanceMapper;
import com.hw.dms.domain.DmsBillsFaultInstance;
import com.hw.dms.service.IDmsBillsFaultInstanceService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 报修工单Service业务层处理
 *
 * @author Open Platform
 * @date 2024-01-23
 */
@Service
public class DmsBillsFaultInstanceServiceImpl implements IDmsBillsFaultInstanceService {
    @Autowired
    private DmsBillsFaultInstanceMapper dmsBillsFaultInstanceMapper;
    @Autowired
    private DmsFaultInstanceActivityMapper dmsFaultInstanceActivityMapper;
    @Autowired
    private DmsInstanceFileMapper dmsInstanceFileMapper;


    /**
     * 查询报修工单
     *
     * @param repairInstanceId 报修工单主键
     * @return 报修工单
     */
    @Override
    public DmsBillsFaultInstance selectDmsBillsFaultInstanceByRepairInstanceId(Long repairInstanceId) {
        DmsFaultInstanceActivity dmsFaultInstanceActivity = dmsFaultInstanceActivityMapper.selectDmsFaultInstanceActivityByRepairIdMin(repairInstanceId);
        DmsBillsFaultInstance dmsBillsFaultInstance = dmsBillsFaultInstanceMapper.selectDmsBillsFaultInstanceByRepairInstanceId(repairInstanceId);
//        List<DmsFaultInstanceFile> dmsFaultInstanceFiles = dmsFaultInstanceFileMapper.selectDmsFaultInstanceFileByFaultId(repairInstanceId);
        List<SysFile> sysFiles = new ArrayList<SysFile>();
        SysFile sysFile = new SysFile();
        dmsBillsFaultInstance.setFaultType(dmsFaultInstanceActivity.getFaultType());
        dmsBillsFaultInstance.setFaultDescription(dmsFaultInstanceActivity.getFaultDescription());
        dmsBillsFaultInstance.setDesignOperations(dmsFaultInstanceActivity.getDesignOperations());
        dmsBillsFaultInstance.setDesignOperations(dmsFaultInstanceActivity.getDesignOperations());
        dmsBillsFaultInstance.setDeviceId(dmsFaultInstanceActivity.getDeviceId());
        dmsBillsFaultInstance.setRepairType(dmsFaultInstanceActivity.getRepairType());
        if (dmsFaultInstanceActivity.getOutsrcId() != null) {
            dmsBillsFaultInstance.setOutsrcId(dmsFaultInstanceActivity.getOutsrcId());
        }
//        for (DmsFaultInstanceFile dmsFaultFor : dmsFaultInstanceFiles) {
//            sysFile.setUrl(dmsFaultFor.getFaultFile());
//            sysFiles.add(sysFile);
//        }
//        dmsBillsFaultInstance.setDmsFaultInstanceFiles(dmsFaultInstanceFiles);
        dmsBillsFaultInstance.setSysFiles(sysFiles);
        return dmsBillsFaultInstance;
    }

    /**
     * 查询报修工单列表
     *
     * @param dmsBillsFaultInstance 报修工单
     * @return 报修工单
     */
    @Override
    public List<DmsBillsFaultInstance> selectDmsBillsFaultInstanceList(DmsBillsFaultInstance dmsBillsFaultInstance) {

        return dmsBillsFaultInstanceMapper.selectDmsBillsFaultInstanceList(dmsBillsFaultInstance);
    }

    /**
     * 新增报修工单
     *
     * @param dmsBillsFaultInstance 报修工单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDmsBillsFaultInstance(DmsBillsFaultInstance dmsBillsFaultInstance) {
        Date curretnDate = new Date();
        String userName = SecurityUtils.getUsername();
        dmsBillsFaultInstance.setWfProcessId(WfConstants.WF_BILLS_FAULT_INSTANCE_PROCESS_ID);
        dmsBillsFaultInstance.setApplyUser(userName);
        dmsBillsFaultInstance.setApplyTime(curretnDate);
        dmsBillsFaultInstance.setCreateTime(curretnDate);
        dmsBillsFaultInstance.setCreateBy(userName);
        dmsBillsFaultInstance.setBillsFaultCode(Seq.getId(Seq.dmsBillsFaultInstanceSeqType, Seq.dmsBillsFaultInstanceSeqCode));
        dmsBillsFaultInstance.setBillsStatus(DmsConstants.DMS_BILLS_FAULT_INSTANCE_STATUS_TO_REPAIR);
        dmsBillsFaultInstance.setInstanceType(DmsConstants.DMS_BILLS_FAULT_INSTANCE_TYPE_MAUAL);

        int rows = dmsBillsFaultInstanceMapper.insertDmsBillsFaultInstance(dmsBillsFaultInstance);

        DmsFaultInstanceActivity dmsFaultInstanceActivity = new DmsFaultInstanceActivity();
        dmsFaultInstanceActivity.setRepairInstanceId(dmsBillsFaultInstance.getRepairInstanceId());
        dmsFaultInstanceActivity.setProcessActivityId(WfConstants.WF_BILLS_FAULT_INSTANCE_PROCESS_ACTIVITY_FIRST_ID);
        dmsFaultInstanceActivity.setFaultType(dmsBillsFaultInstance.getFaultType());
        dmsFaultInstanceActivity.setFaultDescription(dmsBillsFaultInstance.getFaultDescription());
        dmsFaultInstanceActivity.setDesignOperations(dmsBillsFaultInstance.getDesignOperations());
        dmsFaultInstanceActivity.setDeviceId(dmsBillsFaultInstance.getDeviceId());
        dmsFaultInstanceActivity.setRepairType(dmsBillsFaultInstance.getRepairType());
        if (dmsBillsFaultInstance.getOutsrcId() != null) {
            dmsFaultInstanceActivity.setOutsrcId(dmsBillsFaultInstance.getOutsrcId());
        }
        dmsFaultInstanceActivity.setProcessHandleStatus(DmsConstants.DMS_BILLS_FAULT_INSTANCE_ACTIVITY_STATUS_TO_PICK);
        dmsFaultInstanceActivity.setProcessStepOrder(1l);
        dmsFaultInstanceActivityMapper.insertDmsFaultInstanceActivity(dmsFaultInstanceActivity);

        //存储图片
        List<String> fileUrls = dmsBillsFaultInstance.getFileUrls();
        if (fileUrls != null && !fileUrls.isEmpty()) {
            List<DmsInstanceFile> dmsInstanceFiles = new ArrayList<>();
            for (String fileUrl : fileUrls) {
                DmsInstanceFile dmsInstanceFile = new DmsInstanceFile();
                dmsInstanceFile.setTargetId(dmsFaultInstanceActivity.getInstanceActivityId());
                dmsInstanceFile.setTargetType("1");
                dmsInstanceFile.setFilePath(fileUrl);
                dmsInstanceFiles.add(dmsInstanceFile);
            }
            dmsInstanceFileMapper.batchDmsInstanceFile(dmsInstanceFiles);
        }

        return rows;
    }

    /**
     * 修改报修工单
     *
     * @param dmsBillsFaultInstance 报修工单
     * @return 结果
     */
    @Override
    public int updateDmsBillsFaultInstance(DmsBillsFaultInstance dmsBillsFaultInstance) {
        dmsBillsFaultInstance.setUpdateTime(DateUtils.getNowDate());
        return dmsBillsFaultInstanceMapper.updateDmsBillsFaultInstance(dmsBillsFaultInstance);
    }

    /**
     * 批量删除报修工单
     *
     * @param repairInstanceIds 需要删除的报修工单主键
     * @return 结果
     */
    @Override
    public int deleteDmsBillsFaultInstanceByRepairInstanceIds(Long[] repairInstanceIds) {
        return dmsBillsFaultInstanceMapper.deleteDmsBillsFaultInstanceByRepairInstanceIds(repairInstanceIds);
    }

    /**
     * 删除报修工单信息
     *
     * @param repairInstanceId 报修工单主键
     * @return 结果
     */
    @Override
    public int deleteDmsBillsFaultInstanceByRepairInstanceId(Long repairInstanceId) {
        return dmsBillsFaultInstanceMapper.deleteDmsBillsFaultInstanceByRepairInstanceId(repairInstanceId);
    }

    /**
     * pda-新增报修工单
     */
    public int insertDmsBillsFaultInstancePDA(DmsBillsFaultInstance dmsBillsFaultInstance) {
        //根据报修流程查询工单流程节点id
        Long aLong = dmsBillsFaultInstanceMapper.selectProcessActivityId(102l);
        int i = dmsBillsFaultInstanceMapper.insertDmsBillsFaultInstance(dmsBillsFaultInstance);
        DmsFaultInstanceActivity dmsFaultInstanceActivity = new DmsFaultInstanceActivity();
        //生成故障报修工单实例节点第一步
        dmsFaultInstanceActivity.setRepairInstanceId(dmsBillsFaultInstance.getRepairInstanceId());
        dmsFaultInstanceActivity.setFaultType(dmsBillsFaultInstance.getFaultType());
        dmsFaultInstanceActivity.setFaultDescription(dmsBillsFaultInstance.getFaultDescription());
        dmsFaultInstanceActivity.setDesignOperations(dmsBillsFaultInstance.getDesignOperations());
        dmsFaultInstanceActivity.setDeviceId(dmsBillsFaultInstance.getDeviceId());
        dmsFaultInstanceActivity.setRepairType(dmsBillsFaultInstance.getRepairType());
        dmsFaultInstanceActivity.setOutsrcId(dmsBillsFaultInstance.getOutsrcId());
        dmsFaultInstanceActivity.setProcessActivityId(aLong);
        dmsFaultInstanceActivity.setProcessStepOrder(1l);
        dmsFaultInstanceActivity.setCreateTime(DateUtils.getNowDate());
        dmsFaultInstanceActivityMapper.insertDmsFaultInstanceActivity(dmsFaultInstanceActivity);
        return i;
    }

}
