package com.xiaoniu.system.platformsettings.rule.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.xiaoniu.common.core.enums.ServiceResponseEnum;
import com.xiaoniu.common.core.exception.ServiceException;
import com.xiaoniu.common.core.utils.DateUtils;
import com.xiaoniu.common.security.utils.SecurityUtils;
import com.xiaoniu.system.platformsettings.rule.domain.TbRule;
import com.xiaoniu.system.platformsettings.rule.mapper.TbRuleMapper;
import com.xiaoniu.system.platformsettings.rule.service.ITbRuleService;
import com.xiaoniu.system.platformsettings.rule.vo.RuleInfoVO;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 平台规则Service业务层处理
 * 
 * @author Wzp
 * @date 2024-03-12
 */
@Service
public class TbRuleServiceImpl implements ITbRuleService
{
    @Resource
    private TbRuleMapper tbRuleMapper;

    /**
     * 查询平台规则
     * 
     * @param ruleId 平台规则主键
     * @return 平台规则
     */
    @Override
    public TbRule selectTbRuleByRuleId(Long ruleId)
    {
        return tbRuleMapper.selectTbRuleByRuleId(ruleId);
    }

    /**
     * 查询平台规则列表
     * 
     * @param tbRule 平台规则
     * @return 平台规则
     */
    @Override
    public List<TbRule> selectTbRuleList(TbRule tbRule)
    {
        return tbRuleMapper.selectTbRuleList(tbRule);
    }

    /**
     * 新增平台规则
     * 
     * @param tbRule 平台规则
     * @return 结果
     */
    @Override
    public int insertTbRule(TbRule tbRule)
    {
        // 查询是否存在同类型
        if (ObjectUtils.isNotEmpty(tbRuleMapper.selectRuleByRuleType(tbRule.getRuleType()))){
            throw new ServiceException("已有相同类型数据，请勿重复新增");
        }

        tbRule.setCreateTime(DateUtils.getNowDate());
        tbRule.setCreateBy(SecurityUtils.getUsername());
        tbRule.setDelFlag(0);
        return tbRuleMapper.insertTbRule(tbRule);
    }

    /**
     * 修改平台规则
     * 
     * @param tbRule 平台规则
     * @return 结果
     */
    @Override
    public int updateTbRule(TbRule tbRule)
    {
        // 查询是否存在同类型
        TbRule rule = tbRuleMapper.selectRuleByRuleType(tbRule.getRuleType());
        if (ObjectUtils.isNotEmpty(rule) && !rule.getRuleId().equals(tbRule.getRuleId())){
            throw new ServiceException("已有相同类型数据，请勿重复新增");
        }

        tbRule.setUpdateTime(DateUtils.getNowDate());
        tbRule.setUpdateBy(SecurityUtils.getUsername());
        return tbRuleMapper.updateTbRule(tbRule);
    }

    /**
     * 批量删除平台规则
     * 
     * @param ruleIds 需要删除的平台规则主键
     * @return 结果
     */
    @Override
    public int deleteTbRuleByRuleIds(Long[] ruleIds)
    {
        return tbRuleMapper.deleteTbRuleByRuleIds(ruleIds, SecurityUtils.getUsername());
    }

    /**
     * 删除平台规则信息
     * 
     * @param ruleId 平台规则主键
     * @return 结果
     */
    @Override
    public int deleteTbRuleByRuleId(Long ruleId)
    {
        return tbRuleMapper.deleteTbRuleByRuleId(ruleId, SecurityUtils.getUsername());
    }

    /**
     * 查询平台规则
     * @param ruleType 类型
     * @return 结果
     */
    @Override
    public RuleInfoVO selectRuleByType(String ruleType){
        TbRule rule = tbRuleMapper.selectRuleByRuleType(ruleType);
        if (ObjectUtils.isEmpty(rule) || rule.getStatus() == 1){
            throw new ServiceException(ServiceResponseEnum.RULE_INFO_NULL);
        }
        return BeanUtil.copyProperties(rule, RuleInfoVO.class);
    }
}
