package com.ruoyi.asset.service.impl;

import java.util.List;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.asset.mapper.AmsAssetCategoryMapper;
import com.ruoyi.asset.domain.AmsAssetCategory;
import com.ruoyi.asset.service.IAmsAssetCategoryService;
import com.ruoyi.common.core.text.Convert;

/**
 * 资产类别Service业务层处理
 * 
 * @author Yangk
 * @date 2026-06-04
 */
@Service
public class AmsAssetCategoryServiceImpl implements IAmsAssetCategoryService 
{
    private static final String ENABLED_YES = "Y";

    private static final String DEL_FLAG_NORMAL = "0";

    private static final long DEFAULT_MONTHS = 0L;

    @Autowired
    private AmsAssetCategoryMapper amsAssetCategoryMapper;

    /**
     * 查询资产类别
     * 
     * @param categoryId 资产类别主键
     * @return 资产类别
     */
    @Override
    public AmsAssetCategory selectAmsAssetCategoryByCategoryId(Long categoryId)
    {
        return amsAssetCategoryMapper.selectAmsAssetCategoryByCategoryId(categoryId);
    }

    /**
     * 查询资产类别列表
     * 
     * @param amsAssetCategory 资产类别
     * @return 资产类别
     */
    @Override
    public List<AmsAssetCategory> selectAmsAssetCategoryList(AmsAssetCategory amsAssetCategory)
    {
        return amsAssetCategoryMapper.selectAmsAssetCategoryList(amsAssetCategory);
    }

    /**
     * 校验类别编码是否唯一
     *
     * @param amsAssetCategory 资产类别
     * @return 结果
     */
    @Override
    public boolean checkCategoryCodeUnique(AmsAssetCategory amsAssetCategory)
    {
        if (StringUtils.isEmpty(amsAssetCategory.getCategoryCode()))
        {
            return UserConstants.UNIQUE;
        }
        Long categoryId = StringUtils.isNull(amsAssetCategory.getCategoryId()) ? -1L : amsAssetCategory.getCategoryId();
        AmsAssetCategory info = amsAssetCategoryMapper.checkCategoryCodeUnique(amsAssetCategory.getCategoryCode());
        if (StringUtils.isNotNull(info) && info.getCategoryId().longValue() != categoryId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增资产类别
     * 
     * @param amsAssetCategory 资产类别
     * @return 结果
     */
    @Override
    public int insertAmsAssetCategory(AmsAssetCategory amsAssetCategory)
    {
        fillCategoryDefaults(amsAssetCategory);
        amsAssetCategory.setDelFlag(DEL_FLAG_NORMAL);
        amsAssetCategory.setCreateTime(DateUtils.getNowDate());
        return amsAssetCategoryMapper.insertAmsAssetCategory(amsAssetCategory);
    }

    /**
     * 修改资产类别
     * 
     * @param amsAssetCategory 资产类别
     * @return 结果
     */
    @Override
    public int updateAmsAssetCategory(AmsAssetCategory amsAssetCategory)
    {
        fillCategoryDefaults(amsAssetCategory);
        amsAssetCategory.setUpdateTime(DateUtils.getNowDate());
        return amsAssetCategoryMapper.updateAmsAssetCategory(amsAssetCategory);
    }

    /**
     * 批量删除资产类别
     * 
     * @param categoryIds 需要删除的资产类别主键
     * @return 结果
     */
    @Override
    public int deleteAmsAssetCategoryByCategoryIds(String categoryIds)
    {
        String[] ids = Convert.toStrArray(categoryIds);
        for (String id : ids)
        {
            checkCategoryReferenced(Long.valueOf(id));
        }
        return amsAssetCategoryMapper.deleteAmsAssetCategoryByCategoryIds(ids);
    }

    /**
     * 删除资产类别信息
     * 
     * @param categoryId 资产类别主键
     * @return 结果
     */
    @Override
    public int deleteAmsAssetCategoryByCategoryId(Long categoryId)
    {
        checkCategoryReferenced(categoryId);
        return amsAssetCategoryMapper.deleteAmsAssetCategoryByCategoryId(categoryId);
    }

    /**
     * 类别删除后新资产不能再选中该类别；已有资产引用时禁止删除，保留停用作为业务替代动作。
     */
    private void checkCategoryReferenced(Long categoryId)
    {
        AmsAssetCategory category = selectAmsAssetCategoryByCategoryId(categoryId);
        if (StringUtils.isNotNull(category) && amsAssetCategoryMapper.countCategoryReferences(categoryId) > 0)
        {
            throw new ServiceException(String.format("资产类别【%1$s】已被资产引用，不允许删除", category.getCategoryName()));
        }
    }

    /**
     * 默认周期类字段按 0 保存，避免空值在资产建档带出默认值时产生歧义。
     */
    private void fillCategoryDefaults(AmsAssetCategory amsAssetCategory)
    {
        if (StringUtils.isEmpty(amsAssetCategory.getEnabled()))
        {
            amsAssetCategory.setEnabled(ENABLED_YES);
        }
        if (StringUtils.isNull(amsAssetCategory.getDefaultMaintenanceCycle()))
        {
            amsAssetCategory.setDefaultMaintenanceCycle(DEFAULT_MONTHS);
        }
        if (StringUtils.isNull(amsAssetCategory.getDefaultUseLife()))
        {
            amsAssetCategory.setDefaultUseLife(DEFAULT_MONTHS);
        }
        if (amsAssetCategory.getDefaultMaintenanceCycle() < 0 || amsAssetCategory.getDefaultUseLife() < 0)
        {
            throw new ServiceException("默认维保周期和默认使用寿命不能小于0");
        }
    }
}
