package com.hw.dms.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
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.common.security.utils.SecurityUtils;
import com.hw.dms.domain.DmsPlanRepair;
import com.hw.dms.domain.DmsRepairInstanceActivity;
import com.hw.dms.mapper.DmsPlanRepairMapper;
import com.hw.dms.mapper.DmsRepairInstanceActivityMapper;
import com.hw.system.api.RemoteSysCommonService;
import com.hw.system.api.domain.common.WfProcessActivity;
import com.hw.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hw.dms.mapper.DmsRepairInstanceMapper;
import com.hw.dms.domain.DmsRepairInstance;
import com.hw.dms.service.IDmsRepairInstanceService;

/**
 * 检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单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");
        //先写死第一步所对应的步骤，后续通过wfProcessId来查询步骤对应的id
        dmsRepairInstanceActivity.setProcessActivityId(aLong);
        dmsRepairInstanceActivity.setProcessStepOrder(1l);
        dmsRepairInstanceActivity.setCreateBy(user.getUsername());
        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);
//    }
}
