package com.hw.dms.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.hw.common.core.constant.SecurityConstants;
import com.hw.common.core.domain.R;
import com.hw.common.core.utils.DateUtils;
import com.hw.dms.domain.DmsFaultInstanceActivity;
import com.hw.dms.domain.DmsFaultInstanceFile;
import com.hw.dms.domain.DmsRepairInstanceActivity;
import com.hw.dms.mapper.DmsFaultInstanceActivityMapper;
import com.hw.dms.mapper.DmsFaultInstanceFileMapper;
import com.hw.dms.mapper.DmsRepairInstanceActivityMapper;
import com.hw.dms.service.IDmsRepairInstanceActivityService;
import com.hw.system.api.RemoteSysCommonService;
import com.hw.system.api.domain.SysFile;
import com.hw.system.api.model.LoginUser;
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 RemoteSysCommonService remoteSysCommonService;
    @Autowired
    private DmsFaultInstanceFileMapper dmsFaultInstanceFileMapper;

    /**
     * 查询报修工单
     *
     * @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 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertDmsBillsFaultInstance(DmsBillsFaultInstance dmsBillsFaultInstance)
    {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        String str = sdf.format(DateUtils.getNowDate());
        LoginUser user = new LoginUser();
        dmsBillsFaultInstance.setWfProcessId(102l);
        dmsBillsFaultInstance.setApplyUser(user.getUsername());
        dmsBillsFaultInstance.setApplyTime(DateUtils.getNowDate());
        dmsBillsFaultInstance.setCreateTime(DateUtils.getNowDate());
        dmsBillsFaultInstance.setBillsFaultCode(str);
        dmsBillsFaultInstance.setBillsStatus("0");
        dmsBillsFaultInstance.setInstanceType("1");
        int i = dmsBillsFaultInstanceMapper.insertDmsBillsFaultInstance(dmsBillsFaultInstance);
        Long aLong = dmsBillsFaultInstanceMapper.selectProcessActivityId(102l);
        DmsFaultInstanceActivity dmsFaultInstanceActivity = new DmsFaultInstanceActivity();
        dmsFaultInstanceActivity.setRepairInstanceId(dmsBillsFaultInstance.getRepairInstanceId());
        dmsFaultInstanceActivity.setProcessActivityId(aLong);
        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("2");
        dmsFaultInstanceActivity.setProcessStepOrder(1l);
        dmsFaultInstanceActivityMapper.insertDmsFaultInstanceActivity(dmsFaultInstanceActivity);
        //存储图片

        DmsFaultInstanceFile dmsFaultInstanceFile = new DmsFaultInstanceFile();
        if (dmsBillsFaultInstance.getImgUrl()!=""&&dmsBillsFaultInstance.getImgUrl()!=null){
        dmsFaultInstanceFile.setTargetId(dmsFaultInstanceActivity.getInstanceActivityId());
        dmsFaultInstanceFile.setTargetType("1");
        dmsFaultInstanceFile.setFaultFile(dmsBillsFaultInstance.getImgUrl());
        dmsFaultInstanceFileMapper.insertDmsFaultInstanceFile(dmsFaultInstanceFile);}
        return  i;
    }

    /**
     * 修改报修工单
     *
     * @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;
    }

}
