package com.aucma.dms.service.impl;


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.DmsBaseMaintStandard;
import com.aucma.dms.mapper.DmsBaseMaintStandardMapper;
import com.aucma.dms.service.IDmsBaseMaintStandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 保养标准信息Service业务层处理
 *
 * @author xins
 * @date 2024-01-05
 */
@Service
public class DmsBaseMaintStandardServiceImpl implements IDmsBaseMaintStandardService
{
    @Autowired
    private DmsBaseMaintStandardMapper dmsBaseMaintStandardMapper;

    /**
     * 查询保养标准信息
     *
     * @param maintStandardId 保养标准信息主键
     * @return 保养标准信息
     */
    @Override
    public DmsBaseMaintStandard selectDmsBaseMaintStandardByMaintStandardId(Long maintStandardId)
    {
        return dmsBaseMaintStandardMapper.selectDmsBaseMaintStandardByMaintStandardId(maintStandardId);
    }

    /**
     * 查询保养标准信息列表
     *
     * @param dmsBaseMaintStandard 保养标准信息
     * @return 保养标准信息
     */
    @Override
    public List<DmsBaseMaintStandard> selectDmsBaseMaintStandardList(DmsBaseMaintStandard dmsBaseMaintStandard)
    {
        return dmsBaseMaintStandardMapper.selectDmsBaseMaintStandardList(dmsBaseMaintStandard);
    }

    /**
     * 新增保养标准信息
     *
     * @param dmsBaseMaintStandard 保养标准信息
     * @return 结果
     */
    @Override
    public int insertDmsBaseMaintStandard(DmsBaseMaintStandard dmsBaseMaintStandard)
    {
        DmsBaseMaintStandard MaintStandard = new DmsBaseMaintStandard();
        MaintStandard.setMaintStandardCode(dmsBaseMaintStandard.getMaintStandardCode());
        List<DmsBaseMaintStandard> dmsBaseMaintStandardList =  dmsBaseMaintStandardMapper
                .selectDmsBaseMaintStandardList(MaintStandard);
        if (dmsBaseMaintStandardList.size()>0 ) {
            throw new ServiceException("保养标准编码已存在");
        }
        LoginUser user = SecurityUtils.getLoginUser();
        // 设置默认值：IS_FLAG 和 ACTIVE_FLAG 为 null 时默认为 0
        if (dmsBaseMaintStandard.getIsFlag() == null) {
            dmsBaseMaintStandard.setIsFlag(0L);  // IS_FLAG 是 NUMBER 类型
        }
        if (dmsBaseMaintStandard.getActiveFlag() == null) {
            dmsBaseMaintStandard.setActiveFlag("0");  // ACTIVE_FLAG 是 CHAR(1) 类型
        }
        dmsBaseMaintStandard.setDeviceTypeId(0l);
        // 写入 Long 类型用户ID
        dmsBaseMaintStandard.setCreateBy(SecurityUtils.getUserId());
        dmsBaseMaintStandard.setCreateTime(DateUtils.getNowDate());
        return dmsBaseMaintStandardMapper.insertDmsBaseMaintStandard(dmsBaseMaintStandard);
    }
    /**
     * 修改保养标准信息
     *
     * @param dmsBaseMaintStandard 保养标准信息
     * @return 结果
     */
    @Override
    public int updateDmsBaseMaintStandard(DmsBaseMaintStandard dmsBaseMaintStandard)
    {
        DmsBaseMaintStandard MaintStandard = new DmsBaseMaintStandard();
        MaintStandard.setMaintStandardCode(dmsBaseMaintStandard.getMaintStandardCode());
        List<DmsBaseMaintStandard> dmsBaseMaintStandardList =  dmsBaseMaintStandardMapper
                .selectDmsBaseMaintStandardList(MaintStandard);
        if (dmsBaseMaintStandardList.size()>0 && !dmsBaseMaintStandardList.get(0).getMaintStandardId()
                .equals(dmsBaseMaintStandard.getMaintStandardId())) {
            throw new ServiceException("保养标准编码已存在");
        }
        LoginUser user = SecurityUtils.getLoginUser();
        // 写入 Long 类型用户ID
        dmsBaseMaintStandard.setUpdateBy(SecurityUtils.getUserId());
        dmsBaseMaintStandard.setUpdateTime(DateUtils.getNowDate());
        return dmsBaseMaintStandardMapper.updateDmsBaseMaintStandard(dmsBaseMaintStandard);
    }

    /**
     * 批量删除保养标准信息
     *
     * @param maintStandardIds 需要删除的保养标准信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseMaintStandardByMaintStandardIds(Long[] maintStandardIds)
    {
        return dmsBaseMaintStandardMapper.deleteDmsBaseMaintStandardByMaintStandardIds(maintStandardIds);
    }

    /**
     * 删除保养标准信息信息
     *
     * @param maintStandardId 保养标准信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseMaintStandardByMaintStandardId(Long maintStandardId)
    {
        return dmsBaseMaintStandardMapper.deleteDmsBaseMaintStandardByMaintStandardId(maintStandardId);
    }
}
