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.utils.DateUtils;
import com.hw.dms.domain.DmsMaintInstanceActivity;
import com.hw.dms.domain.DmsPlanMaint;
import com.hw.dms.mapper.DmsMaintInstanceActivityMapper;
import com.hw.dms.mapper.DmsPlanMaintMapper;
import com.hw.system.api.RemoteSysCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hw.dms.mapper.DmsBillsMaintInstanceMapper;
import com.hw.dms.domain.DmsBillsMaintInstance;
import com.hw.dms.service.IDmsBillsMaintInstanceService;

/**
 * 保养工单Service业务层处理
 *
 * @author Open Platform
 * @date 2024-02-29
 */
@Service
public class DmsBillsMaintInstanceServiceImpl implements IDmsBillsMaintInstanceService
{
    @Autowired
    private DmsBillsMaintInstanceMapper dmsBillsMaintInstanceMapper;
    @Autowired
    private DmsPlanMaintMapper dmsPlanMaintMapper;
    @Autowired
    private DmsMaintInstanceActivityMapper dmsMaintInstanceActivityMapper;
    @Autowired
    private RemoteSysCommonService remoteSysCommonService;

    /**
     * 查询保养工单
     *
     * @param maintInstanceId 保养工单主键
     * @return 保养工单
     */
    @Override
    public DmsBillsMaintInstance selectDmsBillsMaintInstanceByMaintInstanceId(Long maintInstanceId)
    {
        return dmsBillsMaintInstanceMapper.selectDmsBillsMaintInstanceByMaintInstanceId(maintInstanceId);
    }

    /**
     * 查询保养工单列表
     *
     * @param dmsBillsMaintInstance 保养工单
     * @return 保养工单
     */
    @Override
    public List<DmsBillsMaintInstance> selectDmsBillsMaintInstanceList(DmsBillsMaintInstance dmsBillsMaintInstance)
    {
        return dmsBillsMaintInstanceMapper.selectDmsBillsMaintInstanceList(dmsBillsMaintInstance);
    }

    /**
     * 新增保养工单
     *
     * @param dmsBillsMaintInstance 保养工单
     * @return 结果
     */
    @Override
    public int insertDmsBillsMaintInstance(DmsBillsMaintInstance dmsBillsMaintInstance)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        dmsBillsMaintInstance.setCreateTime(DateUtils.getNowDate());
        DmsPlanMaint dmsPlanMaint = dmsPlanMaintMapper.selectDmsPlanMaintByPlanMaintId(dmsBillsMaintInstance.getPlanMaintId());
        dmsBillsMaintInstance.setPlanBeginTime(dmsPlanMaint.getMaintTime());
        dmsBillsMaintInstance.setBillsMaintCode(sdf.format(DateUtils.getNowDate()));
        dmsBillsMaintInstance.setMaintStatus(1l);
        Date lubeTime = dmsPlanMaint.getMaintTime();
        Long timeLimit = dmsPlanMaint.getTimeLimit();
        dmsBillsMaintInstance.setIsFlag(1l);
        dmsBillsMaintInstance.setWfProcessId(103l);
        dmsBillsMaintInstance.setPlanEndTime(new Date(lubeTime.getTime() + timeLimit*1000l));
        int i = dmsBillsMaintInstanceMapper.insertDmsBillsMaintInstance(dmsBillsMaintInstance);

        DmsMaintInstanceActivity maintInstanceActivity = new DmsMaintInstanceActivity();
        maintInstanceActivity.setMaintInstanceId(dmsBillsMaintInstance.getMaintInstanceId());
        maintInstanceActivity.setProcessActivityId(103l);
        maintInstanceActivity.setMaintLevel(dmsPlanMaint.getMaintLevel());
        maintInstanceActivity.setMaintGroup(dmsPlanMaint.getMaintGroup());
        maintInstanceActivity.setMaintSupervisor(dmsPlanMaint.getMaintSupervisor());
        maintInstanceActivity.setProcessStepOrder(1l);
        dmsMaintInstanceActivityMapper.insertDmsMaintInstanceActivity(maintInstanceActivity);
        return i;
    }

    /**
     * 修改保养工单
     *
     * @param dmsBillsMaintInstance 保养工单
     * @return 结果
     */
    @Override
    public int updateDmsBillsMaintInstance(DmsBillsMaintInstance dmsBillsMaintInstance)
    {
        dmsBillsMaintInstance.setUpdateTime(DateUtils.getNowDate());
        return dmsBillsMaintInstanceMapper.updateDmsBillsMaintInstance(dmsBillsMaintInstance);
    }

    /**
     * 批量删除保养工单
     *
     * @param maintInstanceIds 需要删除的保养工单主键
     * @return 结果
     */
    @Override
    public int deleteDmsBillsMaintInstanceByMaintInstanceIds(Long[] maintInstanceIds)
    {
        return dmsBillsMaintInstanceMapper.deleteDmsBillsMaintInstanceByMaintInstanceIds(maintInstanceIds);
    }

    /**
     * 删除保养工单信息
     *
     * @param maintInstanceId 保养工单主键
     * @return 结果
     */
    @Override
    public int deleteDmsBillsMaintInstanceByMaintInstanceId(Long maintInstanceId)
    {
        return dmsBillsMaintInstanceMapper.deleteDmsBillsMaintInstanceByMaintInstanceId(maintInstanceId);
    }

    @Override
    public int insertDmsBillsMaintInstancePDA(DmsBillsMaintInstance dmsBillsMaintInstance) {
        Long aLong= remoteSysCommonService.selectProcessActivityIdByWfProcessId(SecurityConstants.INNER, 103l);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
        dmsBillsMaintInstance.setWfProcessId(103l);
        dmsBillsMaintInstance.setBillsMaintCode(sdf.format(DateUtils.getNowDate()));
        dmsBillsMaintInstance.setMaintStatus(1l);
        dmsBillsMaintInstance.setIsFlag(1l);
        int i = dmsBillsMaintInstanceMapper.insertDmsBillsMaintInstance(dmsBillsMaintInstance);
        //创建保养工单实例节点第一步
        DmsMaintInstanceActivity dmsMaintInstanceActivity = new DmsMaintInstanceActivity();
        dmsMaintInstanceActivity.setMaintInstanceId(dmsBillsMaintInstance.getMaintInstanceId());
        dmsMaintInstanceActivity.setProcessActivityId(aLong);
        dmsMaintInstanceActivity.setMaintGroup(dmsBillsMaintInstance.getMaintGroup());
        dmsMaintInstanceActivity.setMaintLevel(dmsBillsMaintInstance.getMaintLevel());
        dmsMaintInstanceActivity.setMaintSupervisor(dmsBillsMaintInstance.getMaintSupervisor());
        dmsMaintInstanceActivity.setProcessStepOrder(1l);
        dmsMaintInstanceActivity.setProcessHandleStatus("1");
        dmsMaintInstanceActivityMapper.insertDmsMaintInstanceActivity(dmsMaintInstanceActivity);
        return i;
    }
}
