package com.aucma.dms.service.impl;

//import com.hw.common.core.utils.DateUtils;
//import com.hw.common.security.utils.SecurityUtils;
//import com.aucma.dms.domain.*;
//import com.aucma.dms.mapper.DmsBillsLubeInstanceActivityMapper;
//import com.aucma.dms.mapper.DmsBillsLubeInstanceMapper;
//import com.aucma.dms.service.IDmsBillsLubeInstanceActivityService;
//import com.aucma.dms.service.IDmsPlanLubeService;
//import com.hw.system.api.model.LoginUser;

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.DmsBillsLubeInstance;
import com.aucma.dms.domain.DmsBillsLubeInstanceActivity;
import com.aucma.dms.domain.DmsRecordLube;
import com.aucma.dms.mapper.DmsBillsLubeInstanceActivityMapper;
import com.aucma.dms.mapper.DmsBillsLubeInstanceMapper;
import com.aucma.dms.service.IDmsBillsLubeInstanceActivityService;
import com.aucma.dms.service.IDmsPlanLubeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 润滑工单实例节点Service业务层处理
 *
 * @author Open Platform
 * @date 2024-02-26
 */
@Service
public class DmsBillsLubeInstanceActivityServiceImpl implements IDmsBillsLubeInstanceActivityService
{
    @Autowired
    private DmsBillsLubeInstanceActivityMapper dmsBillsLubeInstanceActivityMapper;
    @Autowired
    private DmsBillsLubeInstanceMapper dmsBillsLubeInstanceMapper;
    @Autowired
    private IDmsPlanLubeService dmsPlanLubeService;

    /**
     * 查询润滑工单实例节点
     *
     * @param instanceActivityId 润滑工单实例节点主键
     * @return 润滑工单实例节点
     */
    @Override
    public DmsBillsLubeInstanceActivity selectDmsBillsLubeInstanceActivityByInstanceActivityId(Long instanceActivityId)
    {
        return dmsBillsLubeInstanceActivityMapper.selectDmsBillsLubeInstanceActivityByInstanceActivityId(instanceActivityId);
    }

    /**
     * 查询润滑工单实例节点列表
     *
     * @param dmsBillsLubeInstanceActivity 润滑工单实例节点
     * @return 润滑工单实例节点
     */
    @Override
    public List<DmsBillsLubeInstanceActivity> selectDmsBillsLubeInstanceActivityList(DmsBillsLubeInstanceActivity dmsBillsLubeInstanceActivity)
    {
        return dmsBillsLubeInstanceActivityMapper.selectDmsBillsLubeInstanceActivityList(dmsBillsLubeInstanceActivity);
    }

    /**
     * 新增润滑工单实例节点
     *
     * @param dmsBillsLubeInstanceActivity 润滑工单实例节点
     * @return 结果
     */
    @Override
    public int insertDmsBillsLubeInstanceActivity(DmsBillsLubeInstanceActivity dmsBillsLubeInstanceActivity)
    {
        LoginUser user = SecurityUtils.getLoginUser();
        dmsBillsLubeInstanceActivity.setCreateBy(user.getUserId());
        dmsBillsLubeInstanceActivity.setStartTime(DateUtils.getNowDate());
        dmsBillsLubeInstanceActivity.setCreateTime(DateUtils.getNowDate());
        dmsBillsLubeInstanceActivity.setProcessHandleStatus("2");

        if (dmsBillsLubeInstanceActivity.getProcessStepOrder()!=1){
            //最后一步 直接将
            if (dmsBillsLubeInstanceActivity.getProcessStepOrder()==dmsBillsLubeInstanceActivity.getWfLength()){
                dmsBillsLubeInstanceActivity.setEndTime(DateUtils.getNowDate());
                dmsBillsLubeInstanceActivity.setHandleUserId(user.getUserId());
                dmsBillsLubeInstanceActivity.setHandleBy(user.getUsername());
                dmsBillsLubeInstanceActivity.setHandleTime(DateUtils.getNowDate());
                DmsBillsLubeInstanceActivity dms = new DmsBillsLubeInstanceActivity();
                dms.setLubeInstanceId(dmsBillsLubeInstanceActivity.getLubeInstanceId());
                List<DmsBillsLubeInstanceActivity> dmsBillsLubeInstanceActivities = dmsBillsLubeInstanceActivityMapper.selectDmsBillsLubeInstanceActivityList(dms);


                int i = dmsBillsLubeInstanceActivityMapper.insertDmsBillsLubeInstanceActivity(dmsBillsLubeInstanceActivity);
                for (DmsBillsLubeInstanceActivity dmsBillsLubeInstanceActivityFor : dmsBillsLubeInstanceActivities){
                    dmsBillsLubeInstanceActivityFor.setProcessHandleStatus("0");
                    dmsBillsLubeInstanceActivityMapper.updateDmsBillsLubeInstanceActivity(dmsBillsLubeInstanceActivityFor);
                }
                DmsBillsLubeInstance dmsBillsLubeInstance = dmsBillsLubeInstanceMapper.selectDmsBillsLubeInstanceByLubeInstanceId(dmsBillsLubeInstanceActivity.getLubeInstanceId());
                dmsBillsLubeInstance.setLubeStatus("2");
                dmsBillsLubeInstance.setRealEndTime(DateUtils.getNowDate());
                dmsBillsLubeInstanceMapper.updateDmsBillsLubeInstance(dmsBillsLubeInstance);
                DmsRecordLube dmsRecordLube = new DmsRecordLube();
                dmsRecordLube.setPlanLubeId(dmsBillsLubeInstance.getPlanLubeId());
                dmsRecordLube.setBillsLubeCode(dmsBillsLubeInstance.getBillsLubeCode());
                dmsRecordLube.setLubeGroup(dmsBillsLubeInstanceActivity.getLubeGroup());
                dmsRecordLube.setLubeLevel(dmsBillsLubeInstanceActivity.getLubeLevel());
                dmsRecordLube.setLubeSupervisor(dmsBillsLubeInstanceActivity.getLubeSupervisor());
                dmsRecordLube.setLubeStatus(dmsBillsLubeInstance.getLubeStatus());
                dmsRecordLube.setIsFlag("1");
                dmsRecordLube.setPlanBeginTime(dmsBillsLubeInstance.getPlanBeginTime());
                dmsRecordLube.setRealBeginTime(dmsBillsLubeInstance.getRealBeginTime());
                dmsRecordLube.setPlanEndTime(dmsBillsLubeInstance.getPlanEndTime());
                dmsRecordLube.setRealEndTime(dmsBillsLubeInstance.getRealEndTime());

                return i;
            }
            //中间步骤
            else {
                if (dmsBillsLubeInstanceActivity.getProcessStepOrder()==2){
                    DmsBillsLubeInstance dmsBillsLubeInstance = dmsBillsLubeInstanceMapper.selectDmsBillsLubeInstanceByLubeInstanceId(dmsBillsLubeInstanceActivity.getLubeInstanceId());
                    dmsBillsLubeInstance.setRealBeginTime(DateUtils.getNowDate());
                    dmsBillsLubeInstanceMapper.updateDmsBillsLubeInstance(dmsBillsLubeInstance);
                }
                DmsBillsLubeInstanceActivity dmsBillsLubeInstanceActivityOrderSmall = new DmsBillsLubeInstanceActivity();
                dmsBillsLubeInstanceActivityOrderSmall.setLubeInstanceId(dmsBillsLubeInstanceActivity.getLubeInstanceId());
                dmsBillsLubeInstanceActivityOrderSmall.setProcessStepOrder(dmsBillsLubeInstanceActivity.getProcessStepOrder() - 1);
                List<DmsBillsLubeInstanceActivity> dmsBillsLubeInstanceActivities = dmsBillsLubeInstanceActivityMapper.selectDmsBillsLubeInstanceActivityList(dmsBillsLubeInstanceActivityOrderSmall);
                dmsBillsLubeInstanceActivities.get(0).setProcessHandleStatus("3");
                dmsBillsLubeInstanceActivities.get(0).setEndTime(DateUtils.getNowDate());
                dmsBillsLubeInstanceActivities.get(0).setHandleTime(DateUtils.getNowDate());
                dmsBillsLubeInstanceActivities.get(0).setHandleBy(user.getUsername());
                dmsBillsLubeInstanceActivities.get(0).setHandleUserId(user.getUserId());
                dmsBillsLubeInstanceActivityMapper.updateDmsBillsLubeInstanceActivity(dmsBillsLubeInstanceActivities.get(0));

            }
        }
        return dmsBillsLubeInstanceActivityMapper.insertDmsBillsLubeInstanceActivity(dmsBillsLubeInstanceActivity);
    }

    /**
     * 修改润滑工单实例节点
     *
     * @param dmsBillsLubeInstanceActivity 润滑工单实例节点
     * @return 结果
     */
    @Override
    public int updateDmsBillsLubeInstanceActivity(DmsBillsLubeInstanceActivity dmsBillsLubeInstanceActivity)
    {
        return dmsBillsLubeInstanceActivityMapper.updateDmsBillsLubeInstanceActivity(dmsBillsLubeInstanceActivity);
    }

    /**
     * 批量删除润滑工单实例节点
     *
     * @param instanceActivityIds 需要删除的润滑工单实例节点主键
     * @return 结果
     */
    @Override
    public int deleteDmsBillsLubeInstanceActivityByInstanceActivityIds(Long[] instanceActivityIds)
    {
        return dmsBillsLubeInstanceActivityMapper.deleteDmsBillsLubeInstanceActivityByInstanceActivityIds(instanceActivityIds);
    }

    /**
     * 删除润滑工单实例节点信息
     *
     * @param instanceActivityId 润滑工单实例节点主键
     * @return 结果
     */
    @Override
    public int deleteDmsBillsLubeInstanceActivityByInstanceActivityId(Long instanceActivityId)
    {
        return dmsBillsLubeInstanceActivityMapper.deleteDmsBillsLubeInstanceActivityByInstanceActivityId(instanceActivityId);
    }

    @Override
    public boolean selectUserIdByDmsBillsLubeInstanceActivityId(Long instanceActivityId, Long userId) {
        Long[] longs = dmsBillsLubeInstanceActivityMapper.selectUserIdByDmsBillsLubeInstanceActivityId(instanceActivityId);
        boolean contains = Arrays.asList(longs).contains(userId);

        return contains;

    }


}
