package com.aucma.dms.service.impl;



import com.aucma.common.exception.ServiceException;
import com.aucma.common.utils.DateUtils;
import com.aucma.common.utils.SecurityUtils;
import com.aucma.dms.domain.DmsBaseShutReason;
import com.aucma.dms.domain.DmsRecordShutDown;
import com.aucma.dms.mapper.DmsRecordShutDownMapper;
import com.aucma.dms.service.IDmsBaseShutReasonService;
import com.aucma.dms.service.IDmsRecordShutDownService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 停机记录Service业务层处理
 *
 * @author Open Platform
 * @date 2024-01-15
 */
@Service
public class DmsRecordShutDownServiceImpl implements IDmsRecordShutDownService{

    @Autowired
    private DmsRecordShutDownMapper dmsRecordShutDownMapper;

    @Autowired
    private IDmsBaseShutReasonService dmsBaseShutReasonService;

    /**
     * 查询停机记录
     *
     * @param recordShutDownId 停机记录主键
     * @return 停机记录
     */
    @Override
    public DmsRecordShutDown selectDmsRecordShutDownByRecordShutDownId(Long recordShutDownId)
    {
        return dmsRecordShutDownMapper.selectDmsRecordShutDownByRecordShutDownId(recordShutDownId);
    }

    /**
     * 查询停机记录列表
     *
     * @param dmsRecordShutDown 停机记录
     * @return 停机记录
     */
    @Override
    public List<DmsRecordShutDown> selectDmsRecordShutDownList(DmsRecordShutDown dmsRecordShutDown)
    {
        return dmsRecordShutDownMapper.selectDmsRecordShutDownList(dmsRecordShutDown);
    }

    /**
     * 新增停机记录
     *
     * @param dmsRecordShutDown 停机记录
     * @return 结果
     */
    @Override
    public int insertDmsRecordShutDown(DmsRecordShutDown dmsRecordShutDown)
    {
        fillShutReasonFields(dmsRecordShutDown);
        dmsRecordShutDown.setCreateBy(SecurityUtils.getLoginUser()==null ? SecurityUtils.getUserId() :SecurityUtils.getLoginUser().getUser().getUserId());
        dmsRecordShutDown.setCreateTime(DateUtils.getNowDate());
        return dmsRecordShutDownMapper.insertDmsRecordShutDown(dmsRecordShutDown);
    }

    /**
     * 修改停机记录
     *
     * @param dmsRecordShutDown 停机记录
     * @return 结果
     */
    @Override
    public int updateDmsRecordShutDown(DmsRecordShutDown dmsRecordShutDown)
    {
        fillShutReasonFields(dmsRecordShutDown);
        dmsRecordShutDown.setUpdateBy(SecurityUtils.getLoginUser()==null ? SecurityUtils.getUserId() :SecurityUtils.getLoginUser().getUser().getUserId());
        dmsRecordShutDown.setUpdateTime(DateUtils.getNowDate());
        return dmsRecordShutDownMapper.updateDmsRecordShutDown(dmsRecordShutDown);
    }

    /**
     * 批量删除停机记录
     *
     * @param recordShutDownIds 需要删除的停机记录主键
     * @return 结果
     */
    @Override
    public int deleteDmsRecordShutDownByRecordShutDownIds(Long[] recordShutDownIds)
    {
        return dmsRecordShutDownMapper.deleteDmsRecordShutDownByRecordShutDownIds(recordShutDownIds);
    }

    /**
     * 删除停机记录信息
     *
     * @param recordShutDownId 停机记录主键
     * @return 结果
     */
    @Override
    public int deleteDmsRecordShutDownByRecordShutDownId(Long recordShutDownId)
    {
        return dmsRecordShutDownMapper.deleteDmsRecordShutDownByRecordShutDownId(recordShutDownId);
    }

    /**
     * 查询PDA待处理停机记录（结束时间为空 或 停机原因ID=1）
     *
     * @return 停机记录集合
     */
    @Override
    public List<DmsRecordShutDown> selectPdaShutDownList()
    {
        return dmsRecordShutDownMapper.selectPdaShutDownList();
    }

    /**
     * 批量更新停机原因
     *
     * @param recordShutDownIds 停机记录ID集合
     * @param shutReasonId 停机原因ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int batchUpdateShutReason(Long[] recordShutDownIds, Long shutReasonId)
    {
        if (recordShutDownIds == null || recordShutDownIds.length == 0)
        {
            return 0;
        }
        if (shutReasonId == null)
        {
            throw new ServiceException("停机原因不能为空");
        }

        DmsBaseShutReason shutReason = dmsBaseShutReasonService.selectDmsBaseShutReasonByShutReasonId(shutReasonId);
        if (shutReason == null)
        {
            throw new ServiceException("停机原因不存在");
        }

        Long updateBy = SecurityUtils.getLoginUser() == null ? SecurityUtils.getUserId() : SecurityUtils.getLoginUser().getUser().getUserId();
        return dmsRecordShutDownMapper.batchUpdateShutReason(recordShutDownIds, shutReasonId,
                shutReason.getShutTypeId(), shutReason.getShutReason(), updateBy, DateUtils.getNowDate());
    }

    /**
     * 为什么这样做：停机记录落库时必须把原因ID、类型ID和原因名称同时冻结，
     * 否则批量维护、PDA 完结和网页编辑会持续积累统计脏数据。
     */
    private void fillShutReasonFields(DmsRecordShutDown dmsRecordShutDown)
    {
        if (dmsRecordShutDown.getShutReasonId() == null)
        {
            return;
        }
        DmsBaseShutReason shutReason = dmsBaseShutReasonService
                .selectDmsBaseShutReasonByShutReasonId(dmsRecordShutDown.getShutReasonId());
        if (shutReason == null)
        {
            throw new ServiceException("停机原因不存在");
        }
        dmsRecordShutDown.setShutType(shutReason.getShutTypeId());
        dmsRecordShutDown.setShutReason(shutReason.getShutReason());
    }
}
