package com.hw.dms.service.impl;

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

import com.hw.common.core.constant.SecurityConstants;
import com.hw.common.core.utils.DateUtils;
import com.hw.dms.domain.DmsBillsInspectInstanceActivity;
import com.hw.dms.mapper.DmsBillsInspectInstanceActivityMapper;
import com.hw.dms.mapper.DmsRepairInstanceMapper;
import com.hw.system.api.RemoteSysCommonService;
import com.hw.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hw.dms.mapper.DmsBillsInspectInstanceMapper;
import com.hw.dms.domain.DmsBillsInspectInstance;
import com.hw.dms.service.IDmsBillsInspectInstanceService;

/**
 * 巡检工单Service业务层处理
 *
 * @author Open Platform
 * @date 2024-01-23
 */
@Service
public class DmsBillsInspectInstanceServiceImpl implements IDmsBillsInspectInstanceService
{
    @Autowired
    private DmsBillsInspectInstanceMapper dmsBillsInspectInstanceMapper;
    @Autowired
    private DmsBillsInspectInstanceActivityMapper dmsBillsInspectInstanceActivityMapper;
    @Autowired
    private DmsRepairInstanceMapper dmsRepairInstanceMapper;
    @Autowired
    private RemoteSysCommonService remoteSysCommonService;

    /**
     * 查询巡检工单
     *
     * @param inspectInstanceId 巡检工单主键
     * @return 巡检工单
     */
    @Override
    public DmsBillsInspectInstance selectDmsBillsInspectInstanceByInspectInstanceId(Long inspectInstanceId)
    {
        return dmsBillsInspectInstanceMapper.selectDmsBillsInspectInstanceByInspectInstanceId(inspectInstanceId);
    }

    /**
     * 查询巡检工单列表
     *
     * @param dmsBillsInspectInstance 巡检工单
     * @return 巡检工单
     */
    @Override
    public List<DmsBillsInspectInstance> selectDmsBillsInspectInstanceList(DmsBillsInspectInstance dmsBillsInspectInstance)
    {
        return dmsBillsInspectInstanceMapper.selectDmsBillsInspectInstanceList(dmsBillsInspectInstance);
    }

    @Override
    public List<DmsBillsInspectInstance> selectPoint() {
        return dmsBillsInspectInstanceMapper.selectPoint();
    }

    @Override
    public List<DmsBillsInspectInstance> selectInspection() {
        return dmsBillsInspectInstanceMapper.selectInspection();
    }

    /**
     * 新增巡检工单
     *
     * @param dmsBillsInspectInstance 巡检工单
     * @return 结果
     */
    @Override
    public int insertDmsBillsInspectInstance(DmsBillsInspectInstance dmsBillsInspectInstance)
    {
        LoginUser user = new LoginUser();
        //写死105，也可根据传入对象的wfprocessid作为传参。
        Long aLong = dmsRepairInstanceMapper.selectWfProcessActivityId(105l);
        if (dmsBillsInspectInstance.getInspectType().equals("1")){
            dmsBillsInspectInstance.setWfProcessId(105l);
        }else if (dmsBillsInspectInstance.getInspectType().equals("2")){
            dmsBillsInspectInstance.setWfProcessId(104l);
        }
        dmsBillsInspectInstance.setCreateTime(DateUtils.getNowDate());
        dmsBillsInspectInstance.setIsFlag("1");
        dmsBillsInspectInstance.setCreateBy(user.getUsername());
        int i = dmsBillsInspectInstanceMapper.insertDmsBillsInspectInstance(dmsBillsInspectInstance);
        List<DmsBillsInspectInstance> dmsBillsInspectInstances = dmsBillsInspectInstanceMapper.selectDmsBillsInspectInstanceList(dmsBillsInspectInstance);
        //新建第一步工单实例节点
        DmsBillsInspectInstanceActivity dmsBillsInspectInstanceActivity = new DmsBillsInspectInstanceActivity();
        dmsBillsInspectInstanceActivity.setInspectInstanceId(dmsBillsInspectInstances.get(0).getInspectInstanceId());
        dmsBillsInspectInstanceActivity.setInspectRouteId(dmsBillsInspectInstance.getInspectRouteId());
        dmsBillsInspectInstanceActivity.setDeviceAmount(dmsBillsInspectInstance.getDeviceAmount());
        dmsBillsInspectInstanceActivity.setPerformer(dmsBillsInspectInstance.getPerformer());
        dmsBillsInspectInstanceActivity.setProcessStepOrder(1l);
        dmsBillsInspectInstanceActivity.setProcessActivityId(aLong);
        dmsBillsInspectInstanceActivityMapper.insertDmsBillsInspectInstanceActivity(dmsBillsInspectInstanceActivity);
        return i;
    }

    /**
     * 修改巡检工单
     *
     * @param dmsBillsInspectInstance 巡检工单
     * @return 结果
     */
    @Override
    public int updateDmsBillsInspectInstance(DmsBillsInspectInstance dmsBillsInspectInstance)
    {
        dmsBillsInspectInstance.setUpdateTime(DateUtils.getNowDate());
        return dmsBillsInspectInstanceMapper.updateDmsBillsInspectInstance(dmsBillsInspectInstance);
    }

    /**
     * 批量删除巡检工单
     *
     * @param inspectInstanceIds 需要删除的巡检工单主键
     * @return 结果
     */
    @Override
    public int deleteDmsBillsInspectInstanceByInspectInstanceIds(Long[] inspectInstanceIds)
    {
        return dmsBillsInspectInstanceMapper.deleteDmsBillsInspectInstanceByInspectInstanceIds(inspectInstanceIds);
    }

    /**
     * 删除巡检工单信息
     *
     * @param inspectInstanceId 巡检工单主键
     * @return 结果
     */
    @Override
    public int deleteDmsBillsInspectInstanceByInspectInstanceId(Long inspectInstanceId)
    {
        return dmsBillsInspectInstanceMapper.deleteDmsBillsInspectInstanceByInspectInstanceId(inspectInstanceId);
    }

    @Override
    public int insertDmsBillsInspectInstancePDA(DmsBillsInspectInstance dmsBillsInspectInstance) {
        Long along = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
        if (dmsBillsInspectInstance.getInspectType().equals("1")){
            dmsBillsInspectInstance.setWfProcessId(105l);
            along = remoteSysCommonService.selectProcessActivityIdByWfProcessId(SecurityConstants.INNER,105l);
        }
        else if (dmsBillsInspectInstance.getInspectType().equals("2")){
            dmsBillsInspectInstance.setWfProcessId(104l);
            along = remoteSysCommonService.selectProcessActivityIdByWfProcessId(SecurityConstants.INNER,104l);

        }
        dmsBillsInspectInstance.setBillsInspectCode(sdf.format(DateUtils.getNowDate()));
        dmsBillsInspectInstance.setInspectStatus("1");
        dmsBillsInspectInstance.setIsFlag("1");
        //创建点巡检工单实例节点第一步
        int i = dmsBillsInspectInstanceMapper.insertDmsBillsInspectInstance(dmsBillsInspectInstance);

        DmsBillsInspectInstanceActivity dmsBillsInspectInstanceActivity = new DmsBillsInspectInstanceActivity();
        dmsBillsInspectInstanceActivity.setInspectInstanceId(dmsBillsInspectInstance.getInspectInstanceId());
        dmsBillsInspectInstanceActivity.setProcessStepOrder(1l);
        dmsBillsInspectInstanceActivity.setProcessActivityId(along);
        dmsBillsInspectInstanceActivity.setInspectRouteId(dmsBillsInspectInstance.getInspectRouteId());
        dmsBillsInspectInstanceActivity.setDeviceAmount(dmsBillsInspectInstance.getDeviceAmount());
        dmsBillsInspectInstanceActivity.setPerformer(dmsBillsInspectInstance.getPerformer());
        dmsBillsInspectInstanceActivity.setProcessHandleStatus("1");
        dmsBillsInspectInstanceActivityMapper.insertDmsBillsInspectInstanceActivity(dmsBillsInspectInstanceActivity);
        return 0;
    }


}
