package com.aucma.dms.service.impl;

//import com.hw.common.core.exception.ServiceException;
//import com.hw.common.core.utils.DateUtils;
//import com.hw.common.security.utils.SecurityUtils;
//import com.aucma.dms.domain.DmsBaseShutReason;
//import com.aucma.dms.mapper.DmsBaseShutReasonMapper;
//import com.aucma.dms.service.IDmsBaseShutReasonService;
//import com.hw.system.api.model.LoginUser;

import com.aucma.common.core.domain.model.LoginUser;
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.mapper.DmsBaseShutReasonMapper;
import com.aucma.dms.service.IDmsBaseShutReasonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 停机原因信息Service业务层处理
 *
 * @author Open Platform
 * @date 2024-01-12
 */
@Service
public class DmsBaseShutReasonServiceImpl implements IDmsBaseShutReasonService
{
    @Autowired
    private DmsBaseShutReasonMapper dmsBaseShutReasonMapper;

    /**
     * 查询停机原因信息
     *
     * @param shutReasonId 停机原因信息主键
     * @return 停机原因信息
     */
    @Override
    public DmsBaseShutReason selectDmsBaseShutReasonByShutReasonId(Long shutReasonId)
    {
        return dmsBaseShutReasonMapper.selectDmsBaseShutReasonByShutReasonId(shutReasonId);
    }

    /**
     * 查询停机原因信息列表
     *
     * @param dmsBaseShutReason 停机原因信息
     * @return 停机原因信息
     */
    @Override
    public List<DmsBaseShutReason> selectDmsBaseShutReasonList(DmsBaseShutReason dmsBaseShutReason)
    {
        return dmsBaseShutReasonMapper.selectDmsBaseShutReasonList(dmsBaseShutReason);
    }

    /**
     * 新增停机原因信息
     *
     * @param dmsBaseShutReason 停机原因信息
     * @return 结果
     */
    @Override
    public int insertDmsBaseShutReason(DmsBaseShutReason dmsBaseShutReason)
    {
        DmsBaseShutReason ShutReason = new DmsBaseShutReason();
        ShutReason.setReasonCode(dmsBaseShutReason.getReasonCode());
        List<DmsBaseShutReason> dmsBaseShutReasonList =  dmsBaseShutReasonMapper
                .selectDmsBaseShutReasonList(ShutReason);
       if (dmsBaseShutReasonList.size()>0){throw new ServiceException("停机原因编号已存在");}
        LoginUser user = SecurityUtils.getLoginUser();
        dmsBaseShutReason.setIsFlag("1");
        dmsBaseShutReason.setCreateBy(user.getUserId());
        dmsBaseShutReason.setCreateTime(DateUtils.getNowDate());
        return dmsBaseShutReasonMapper.insertDmsBaseShutReason(dmsBaseShutReason);
    }

    /**
     * 修改停机原因信息
     *
     * @param dmsBaseShutReason 停机原因信息
     * @return 结果
     */
    @Override
    public int updateDmsBaseShutReason(DmsBaseShutReason dmsBaseShutReason)
    {
        DmsBaseShutReason ShutReason = new DmsBaseShutReason();
        ShutReason.setReasonCode(dmsBaseShutReason.getReasonCode());
        List<DmsBaseShutReason> dmsBaseShutReasonList = dmsBaseShutReasonMapper
                .selectDmsBaseShutReasonList(ShutReason);
        if (dmsBaseShutReasonList.size()>0 && !dmsBaseShutReason.getShutReasonId()
                .equals(dmsBaseShutReasonList.get(0).getShutReasonId())){
            throw new ServiceException("停机原因编号已存在");
        }
        LoginUser user = SecurityUtils.getLoginUser();
        dmsBaseShutReason.setUpdateBy(user.getUserId());
        dmsBaseShutReason.setUpdateTime(DateUtils.getNowDate());
        return dmsBaseShutReasonMapper.updateDmsBaseShutReason(dmsBaseShutReason);
    }

    /**
     * 批量删除停机原因信息
     *
     * @param shutReasonIds 需要删除的停机原因信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseShutReasonByShutReasonIds(Long[] shutReasonIds)
    {
        return dmsBaseShutReasonMapper.deleteDmsBaseShutReasonByShutReasonIds(shutReasonIds);
    }

    /**
     * 删除停机原因信息信息
     *
     * @param shutReasonId 停机原因信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseShutReasonByShutReasonId(Long shutReasonId)
    {
        return dmsBaseShutReasonMapper.deleteDmsBaseShutReasonByShutReasonId(shutReasonId);
    }
}
