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.DmsBillsLubeInstanceActivity;
import com.hw.dms.domain.DmsPlanLube;
import com.hw.dms.mapper.DmsBillsLubeInstanceActivityMapper;
import com.hw.dms.mapper.DmsPlanLubeMapper;
import com.hw.system.api.RemoteSysCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hw.dms.mapper.DmsBillsLubeInstanceMapper;
import com.hw.dms.domain.DmsBillsLubeInstance;
import com.hw.dms.service.IDmsBillsLubeInstanceService;

/**
 * 润滑工单Service业务层处理
 *
 * @author Open Platform
 * @date 2024-02-26
 */
@Service
public class DmsBillsLubeInstanceServiceImpl implements IDmsBillsLubeInstanceService
{
    @Autowired
    private DmsBillsLubeInstanceMapper dmsBillsLubeInstanceMapper;

    @Autowired
    private DmsPlanLubeMapper dmsPlanLubeMapper;

    @Autowired
    private DmsBillsLubeInstanceActivityMapper dmsBillsLubeInstanceActivityMapper;;

    @Autowired
    private RemoteSysCommonService remoteSysCommonService;
    /**
     * 查询润滑工单
     *
     * @param lubeInstanceId 润滑工单主键
     * @return 润滑工单
     */
    @Override
    public DmsBillsLubeInstance selectDmsBillsLubeInstanceByLubeInstanceId(Long lubeInstanceId)
    {
        return dmsBillsLubeInstanceMapper.selectDmsBillsLubeInstanceByLubeInstanceId(lubeInstanceId);
    }

    /**
     * 查询润滑工单列表
     *
     * @param dmsBillsLubeInstance 润滑工单
     * @return 润滑工单
     */
    @Override
    public List<DmsBillsLubeInstance> selectDmsBillsLubeInstanceList(DmsBillsLubeInstance dmsBillsLubeInstance)
    {
        return dmsBillsLubeInstanceMapper.selectDmsBillsLubeInstanceList(dmsBillsLubeInstance);
    }

    /**
     * 新增润滑工单
     *
     * @param dmsBillsLubeInstance 润滑工单
     * @return 结果
     */
    @Override
    public int insertDmsBillsLubeInstance(DmsBillsLubeInstance dmsBillsLubeInstance)
    {
        dmsBillsLubeInstance.setCreateTime(DateUtils.getNowDate());
        DmsPlanLube dmsPlanLube = dmsPlanLubeMapper.selectDmsPlanLubeByPlanLubeId(dmsBillsLubeInstance.getPlanLubeId());
        dmsBillsLubeInstance.setPlanBeginTime(dmsPlanLube.getLubeTime());
        dmsBillsLubeInstance.setLubeStatus("1");
        Date lubeTime = dmsPlanLube.getLubeTime();
        Long timeLimit = dmsPlanLube.getTimeLimit();
        dmsBillsLubeInstance.setPlanEndTime(new Date(lubeTime.getTime() + timeLimit*1000l));
        int i = dmsBillsLubeInstanceMapper.insertDmsBillsLubeInstance(dmsBillsLubeInstance);
        List<DmsBillsLubeInstance> dmsBillsLubeInstances = dmsBillsLubeInstanceMapper.selectDmsBillsLubeInstanceList(dmsBillsLubeInstance);
        DmsBillsLubeInstance dmsBillsLubeInstance1 = dmsBillsLubeInstances.get(0);
        //新建润滑工单实例节点
        DmsBillsLubeInstanceActivity dmsBillsLubeInstanceActivity = new DmsBillsLubeInstanceActivity();
        dmsBillsLubeInstanceActivity.setLubeInstanceId(dmsBillsLubeInstance1.getLubeInstanceId());
        dmsBillsLubeInstanceActivity.setProcessActivityId(106l);
        dmsBillsLubeInstanceActivity.setLubeGroup(dmsPlanLube.getLubeGroup());
        dmsBillsLubeInstanceActivity.setLubeSupervisor(dmsPlanLube.getLubeSupervisor());
        dmsBillsLubeInstanceActivity.setLubeLevel(dmsPlanLube.getLubeLevel());
        dmsBillsLubeInstanceActivity.setProcessStepOrder(1l);
        dmsBillsLubeInstanceActivityMapper.insertDmsBillsLubeInstanceActivity(dmsBillsLubeInstanceActivity);
        return i;
    }

    /**
     * 修改润滑工单
     *
     * @param dmsBillsLubeInstance 润滑工单
     * @return 结果
     */
    @Override
    public int updateDmsBillsLubeInstance(DmsBillsLubeInstance dmsBillsLubeInstance)
    {
        dmsBillsLubeInstance.setUpdateTime(DateUtils.getNowDate());
        return dmsBillsLubeInstanceMapper.updateDmsBillsLubeInstance(dmsBillsLubeInstance);
    }

    /**
     * 批量删除润滑工单
     *
     * @param lubeInstanceIds 需要删除的润滑工单主键
     * @return 结果
     */
    @Override
    public int deleteDmsBillsLubeInstanceByLubeInstanceIds(Long[] lubeInstanceIds)
    {
        return dmsBillsLubeInstanceMapper.deleteDmsBillsLubeInstanceByLubeInstanceIds(lubeInstanceIds);
    }

    /**
     * 删除润滑工单信息
     *
     * @param lubeInstanceId 润滑工单主键
     * @return 结果
     */
    @Override
    public int deleteDmsBillsLubeInstanceByLubeInstanceId(Long lubeInstanceId)
    {
        return dmsBillsLubeInstanceMapper.deleteDmsBillsLubeInstanceByLubeInstanceId(lubeInstanceId);
    }

    @Override
    public int insertDmsBillsLubeInstancePDA(DmsBillsLubeInstance dmsBillsLubeInstance) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
        Long aLong= remoteSysCommonService.selectProcessActivityIdByWfProcessId(SecurityConstants.INNER, 106l);
        dmsBillsLubeInstance.setBillsLubeCode(sdf.format(DateUtils.getNowDate()));
        dmsBillsLubeInstance.setLubeStatus("1");
        dmsBillsLubeInstance.setIsFlag("1");
        //创建润滑工单实例节点
        DmsBillsLubeInstanceActivity dmsBillsLubeInstanceActivity = new DmsBillsLubeInstanceActivity();
        dmsBillsLubeInstanceActivity.setLubeInstanceId(dmsBillsLubeInstance.getLubeInstanceId());
        dmsBillsLubeInstanceActivity.setProcessActivityId(aLong);
        dmsBillsLubeInstanceActivity.setLubeGroup(dmsBillsLubeInstance.getLubeGroup());
        dmsBillsLubeInstanceActivity.setLubeLevel(dmsBillsLubeInstance.getLubeLevel());
        dmsBillsLubeInstanceActivity.setLubeSupervisor(dmsBillsLubeInstance.getLubeSupervisor());

        return 0;
    }
}
