package com.aucma.dms.service.impl;


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.DmsPlanRepair;
import com.aucma.dms.domain.DmsRepairInstance;
import com.aucma.dms.domain.DmsRepairInstanceActivity;
import com.aucma.dms.mapper.DmsPlanRepairMapper;
import com.aucma.dms.mapper.DmsRepairInstanceActivityMapper;
import com.aucma.dms.mapper.DmsRepairInstanceMapper;
import com.aucma.dms.service.IDmsRepairInstanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单Service业务层处理
 *
 * @author Open Platform
 * @date 2024-01-24
 */
@Service
public class DmsRepairInstanceServiceImpl implements IDmsRepairInstanceService
{
    @Autowired
    private DmsRepairInstanceMapper dmsRepairInstanceMapper;
    @Autowired
    private DmsRepairInstanceActivityMapper dmsRepairInstanceActivityMapper;
    @Autowired
    private DmsPlanRepairMapper dmsPlanRepairMapper;
/*    @Autowired
    private RemoteSysCommonService remoteSysCommonService;*/


    public static String lastDate="2000-01-01";
    //流水号
    public static int serialNumber = 0;

    /**
     * 查询检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单
     *
     * @param repairInstanceId 检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单主键
     * @return 检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单
     */
    @Override
    public DmsRepairInstance selectDmsRepairInstanceByRepairInstanceId(Long repairInstanceId)
    {
        //为修改表单时添加手动创建所需的实力节点信息
        DmsRepairInstanceActivity dmsRepairInstanceActivity = new DmsRepairInstanceActivity();
        dmsRepairInstanceActivity.setRepairInstanceId(repairInstanceId);
        dmsRepairInstanceActivity.setProcessStepOrder(1l);
        List<DmsRepairInstanceActivity> dmsRepairInstanceActivities = dmsRepairInstanceActivityMapper.selectDmsRepairInstanceActivityList(dmsRepairInstanceActivity);
        DmsRepairInstance dmsRepairInstance = dmsRepairInstanceMapper.selectDmsRepairInstanceByRepairInstanceId(repairInstanceId);
        dmsRepairInstance.setRepairType(dmsRepairInstanceActivities.get(0).getRepairType());
        dmsRepairInstance.setRepairGroup(dmsRepairInstanceActivities.get(0).getRepairGroup());
        dmsRepairInstance.setRepairSupervisor(dmsRepairInstanceActivities.get(0).getRepairSupervisor());
        return dmsRepairInstance;
    }

    /**
     * 查询检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单列表
     *
     * @param dmsRepairInstance 检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单
     * @return 检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单
     */
    @Override
    public List<DmsRepairInstance> selectDmsRepairInstanceList(DmsRepairInstance dmsRepairInstance)
    {
        return dmsRepairInstanceMapper.selectDmsRepairInstanceList(dmsRepairInstance);
    }

    /**
     * 新增检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单
     *
     * @param dmsRepairInstance 检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单
     * @return 结果
     */
    @Override
    public int insertDmsRepairInstance(DmsRepairInstance dmsRepairInstance)
    {
        // 已废弃审批流功能 - 以下代码已注释
        // WfProcessActivity wfProcessActivity = new WfProcessActivity();
        // wfProcessActivity.setWfProcessId(dmsRepairInstance.getWfProcessId());
        // Long aLong = dmsRepairInstanceMapper.selectWfProcessActivityId(dmsRepairInstance.getWfProcessId());
//        System.out.println(aLong);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        String format = sdf.format(date);
        if (!format.equals(lastDate)){
            lastDate = format;
            serialNumber = 0;
        }
        serialNumber++;
        String serialNumberStr = String.format("%06d", serialNumber);
        String dailyCode = format + serialNumberStr;
        dmsRepairInstance.setBillsRepairCode(dailyCode);

        LoginUser user = SecurityUtils.getLoginUser();
        if (dmsRepairInstance.getPlanBeginTime()!=null){
            DmsPlanRepair dmsPlanRepair = dmsPlanRepairMapper.selectDmsPlanRepairByPlanRepairId(dmsRepairInstance.getPlanRepairId());
            Long timeLimit = dmsPlanRepair.getTimeLimit();
            Date planBeginTime = dmsRepairInstance.getPlanBeginTime();
            dmsRepairInstance.setPlanEndTime(new Date(planBeginTime.getTime() + timeLimit*1000l));
        }
        dmsRepairInstance.setIsFlag("1");
        dmsRepairInstance.setBillsStatus("1");
        if (dmsRepairInstance.getInstanceType()==null) {
            dmsRepairInstance.setInstanceType("1");
        }
        dmsRepairInstance.setApplyBy(user.getUsername());
        dmsRepairInstance.setApplyTime(DateUtils.getNowDate());
        // 已废弃审批流功能
        // dmsRepairInstance.setWfProcessId(101l);
        int flag = dmsRepairInstanceMapper.insertDmsRepairInstance(dmsRepairInstance);
        //创建工单时新建工单实例节点

        DmsRepairInstanceActivity dmsRepairInstanceActivity = new DmsRepairInstanceActivity();
        dmsRepairInstanceActivity.setRepairInstanceId(dmsRepairInstance.getRepairInstanceId());
        dmsRepairInstanceActivity.setProcessHandleStatus("1");
        // 已废弃审批流功能
        // dmsRepairInstanceActivity.setProcessActivityId(aLong);
        dmsRepairInstanceActivity.setProcessStepOrder(1l);
        dmsRepairInstanceActivity.setCreateBy(user.getUserId());
        dmsRepairInstanceActivity.setCreateTime(DateUtils.getNowDate());
        dmsRepairInstanceActivity.setStartTime(DateUtils.getNowDate());
        dmsRepairInstanceActivity.setRepairType(dmsRepairInstance.getRepairType());
        dmsRepairInstanceActivity.setRepairGroup(dmsRepairInstance.getRepairGroup());
        dmsRepairInstanceActivity.setRepairSupervisor(dmsRepairInstance.getRepairSupervisor());
        dmsRepairInstanceActivityMapper.insertDmsRepairInstanceActivity(dmsRepairInstanceActivity);
        return flag;
    }

    /**
     * 修改检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单
     *
     * @param dmsRepairInstance 检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单
     * @return 结果
     */

    @Override
    public int updateDmsRepairInstance(DmsRepairInstance dmsRepairInstance)
    {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
        String format = sdf.format(date);
        if (dmsRepairInstance.getPlanBeginTime()!=null){
            DmsPlanRepair dmsPlanRepair = dmsPlanRepairMapper.selectDmsPlanRepairByPlanRepairId(dmsRepairInstance.getPlanRepairId());
            Long timeLimit = dmsPlanRepair.getTimeLimit();
            Date planBeginTime = dmsRepairInstance.getPlanBeginTime();
            dmsRepairInstance.setPlanEndTime(new Date(planBeginTime.getTime() + timeLimit*1000l));
        }
        dmsRepairInstance.setUpdateTime(DateUtils.getNowDate());
        /** 修改检修工单时一同修改检修节点*/
        DmsRepairInstanceActivity dmsRepairInstanceActivity = new DmsRepairInstanceActivity();
        dmsRepairInstanceActivity.setRepairInstanceId(dmsRepairInstance.getRepairInstanceId());
        dmsRepairInstanceActivity.setProcessStepOrder(1l);
        List<DmsRepairInstanceActivity> dmsRepairInstanceActivities = dmsRepairInstanceActivityMapper.selectDmsRepairInstanceActivityList(dmsRepairInstanceActivity);
        dmsRepairInstanceActivity.setInstanceActivityId(dmsRepairInstanceActivities.get(0).getInstanceActivityId());
        dmsRepairInstanceActivity.setRepairType(dmsRepairInstance.getRepairType());
        dmsRepairInstanceActivity.setRepairGroup(dmsRepairInstance.getRepairGroup());
        dmsRepairInstanceActivity.setRepairSupervisor(dmsRepairInstance.getRepairSupervisor());
        dmsRepairInstanceActivityMapper.updateDmsRepairInstanceActivity(dmsRepairInstanceActivity);

        return dmsRepairInstanceMapper.updateDmsRepairInstance(dmsRepairInstance);
    }

    /**
     * 批量删除检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单
     *
     * @param repairInstanceIds 需要删除的检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单主键
     * @return 结果
     */
    @Override
    public int deleteDmsRepairInstanceByRepairInstanceIds(Long[] repairInstanceIds)
    {
        dmsRepairInstanceActivityMapper.deleteDmsRepairInstanceActivityByRepairInstanceIds(repairInstanceIds);
        return dmsRepairInstanceMapper.deleteDmsRepairInstanceByRepairInstanceIds(repairInstanceIds);
    }

    /**
     * 删除检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单信息
     *
     * @param repairInstanceId 检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单主键
     * @return 结果
     */
    @Override
    public int deleteDmsRepairInstanceByRepairInstanceId(Long repairInstanceId)
    {
        return dmsRepairInstanceMapper.deleteDmsRepairInstanceByRepairInstanceId(repairInstanceId);
    }

    @Override
    public int insertDmsRepairInstancePDA(DmsRepairInstance dmsRepairInstance) {
        // 已废弃审批流功能 - 以下代码已注释
        // Long aLong= remoteSysCommonService.selectProcessActivityIdByWfProcessId(SecurityConstants.INNER, 101l);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
        // dmsRepairInstance.setWfProcessId(101l);
        dmsRepairInstance.setBillsRepairCode(sdf.format(DateUtils.getNowDate()));
        dmsRepairInstance.setBillsStatus("0");
        dmsRepairInstance.setInstanceType("1");
        dmsRepairInstance.setIsFlag("1");
        int i = dmsRepairInstanceMapper.insertDmsRepairInstance(dmsRepairInstance);
        //创建工单实例节点
        DmsRepairInstanceActivity dmsRepairInstanceActivity = new DmsRepairInstanceActivity();
        dmsRepairInstanceActivity.setRepairInstanceId(dmsRepairInstance.getRepairInstanceId());
        // 已废弃审批流功能
        // dmsRepairInstanceActivity.setProcessActivityId(aLong);
        dmsRepairInstanceActivity.setRepairType(dmsRepairInstance.getRepairType());
        dmsRepairInstanceActivity.setRepairGroup(dmsRepairInstance.getRepairGroup());
        dmsRepairInstanceActivity.setRepairSupervisor(dmsRepairInstance.getRepairSupervisor());
        dmsRepairInstanceActivity.setProcessStepOrder(1l);
        dmsRepairInstanceActivity.setProcessHandleStatus("1");
        dmsRepairInstanceActivityMapper.insertDmsRepairInstanceActivity(dmsRepairInstanceActivity);
        return i;
    }

//    public static void main(String[] args) {
//
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//        Date date = new Date();
//        String format = sdf.format(date);
//        if (!format.equals(lastDate)){
//            lastDate = format;
//            serialNumber = 0;
//        }
//        serialNumber++;
//        String serialNumberStr = String.format("%03d", serialNumber);
//        String dailyCode = format + serialNumberStr;
//        System.out.println(dailyCode);
//    }
}
