package com.ruoyi.dxp.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.dxp.domain.DataItem;
import com.ruoyi.dxp.domain.ServiceItem;
import com.ruoyi.dxp.mapper.DataItemMapper;
import com.ruoyi.dxp.mapper.ServiceItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dxp.mapper.RuleDesensitizationMapper;
import com.ruoyi.dxp.domain.RuleDesensitization;
import com.ruoyi.dxp.service.IRuleDesensitizationService;

/**
 * 脱敏规则Service业务层处理
 * 
 * @author jy
 * @date 2022-12-03
 */
@Service
public class RuleDesensitizationServiceImpl implements IRuleDesensitizationService 
{
    @Autowired
    private RuleDesensitizationMapper ruleDesensitizationMapper;

    @Autowired
    private DataItemMapper dataItemMapper;

    @Autowired
    private ServiceItemMapper serviceItemMapper;

    /**
     * 查询脱敏规则
     * 
     * @param id 脱敏规则主键
     * @return 脱敏规则
     */
    @Override
    public RuleDesensitization selectRuleDesensitizationById(Long id)
    {
        return ruleDesensitizationMapper.selectRuleDesensitizationById(id);
    }

    /**
     * 查询脱敏规则列表
     * 
     * @param ruleDesensitization 脱敏规则
     * @return 脱敏规则
     */
    @Override
    public List<RuleDesensitization> selectRuleDesensitizationList(RuleDesensitization ruleDesensitization)
    {
        return ruleDesensitizationMapper.selectRuleDesensitizationList(ruleDesensitization);
    }

    /**
     * 新增脱敏规则
     * 
     * @param ruleDesensitization 脱敏规则
     * @return 结果
     */
    @Override
    public int insertRuleDesensitization(RuleDesensitization ruleDesensitization)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ruleDesensitization.setResourceCount(0L);
        ruleDesensitization.setItemCount(0L);
        ruleDesensitization.setCreateBy(loginUser.getUsername());
        ruleDesensitization.setCreateTime(DateUtils.getNowDate());
        return ruleDesensitizationMapper.insertRuleDesensitization(ruleDesensitization);
    }

    /**
     * 修改脱敏规则
     * 
     * @param ruleDesensitization 脱敏规则
     * @return 结果
     */
    @Override
    public int updateRuleDesensitization(RuleDesensitization ruleDesensitization)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ruleDesensitization.setUpdateBy(loginUser.getUsername());
        ruleDesensitization.setUpdateTime(DateUtils.getNowDate());
        return ruleDesensitizationMapper.updateRuleDesensitization(ruleDesensitization);
    }

    /**
     * 批量删除脱敏规则
     * 
     * @param ids 需要删除的脱敏规则主键
     * @return 结果
     */
    @Override
    public int deleteRuleDesensitizationByIds(Long[] ids)
    {
        return ruleDesensitizationMapper.deleteRuleDesensitizationByIds(ids);
    }

    /**
     * 删除脱敏规则信息
     * 
     * @param id 脱敏规则主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteRuleDesensitizationById(Long id)
    {
        // 检查是否关联数据/服务项
        DataItem dReq = new DataItem();
        dReq.setDesensitizationRuleId(id);
        List<DataItem> dataItems = dataItemMapper.selectDataItemList(dReq);
        if (dataItems != null && dataItems.size() > 0) {
            return AjaxResult.error("当前脱敏规则关联了数据项，不能删除");
        }

        ServiceItem sReq = new ServiceItem();
        sReq.setDesensitizationRuleId(id);
        List<ServiceItem> serviceItems = serviceItemMapper.selectServiceItemList(sReq);
        if (serviceItems != null && serviceItems.size() > 0) {
            return AjaxResult.error("当前脱敏规则关联了服务参数，不能删除");
        }

        ruleDesensitizationMapper.deleteRuleDesensitizationById(id);
        return AjaxResult.success();
    }

    /**
     * 添加数量
     * @param id id
     * @param type 资源数，项数
     * @param calc add加，mius减
     */
    @Override
    public void addCount(Long id, String type, String calc) {
        RuleDesensitization ruleDesensitization = ruleDesensitizationMapper.selectRuleDesensitizationById(id);
        if (ruleDesensitization == null) {
            return;
        }

        if (type.equals("resource")) {
            if (calc.equals("add")) {
                ruleDesensitization.setResourceCount(ruleDesensitization.getResourceCount() + 1);
            } else {
                ruleDesensitization.setResourceCount(ruleDesensitization.getResourceCount() - 1);
            }
        } else if (type.equals("item")) {
            if (calc.equals("add")) {
                ruleDesensitization.setItemCount(ruleDesensitization.getItemCount() + 1);
            } else {
                ruleDesensitization.setItemCount(ruleDesensitization.getItemCount() - 1);
            }
        }

        ruleDesensitizationMapper.updateRuleDesensitization(ruleDesensitization);
    }
}
