package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.DataException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.domain.SysTenantPackage;
import com.ruoyi.system.domain.vo.SysTenantVo;
import com.ruoyi.system.mapper.SysConfigMapper;
import com.ruoyi.system.mapper.SysTenantPackageMapper;
import com.ruoyi.system.service.ISysTenantPackageService;
import com.ruoyi.system.service.ISysTenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 租户套餐Service业务层处理
 *
 * @author fengcheng
 * @date 2025-01-04
 */
@Service
public class SysTenantPackageServiceImpl extends ServiceImpl<SysTenantPackageMapper, SysTenantPackage> implements ISysTenantPackageService {
    @Autowired
    private SysTenantPackageMapper sysTenantPackageMapper;

    @Autowired
    private ISysTenantService sysTenantService;

    /**
     * 查询租户套餐
     *
     * @param tenantPackageId 租户套餐主键
     * @return 租户套餐
     */
    @Override
    public SysTenantPackage selectSysTenantPackageByTenantPackageId(Long tenantPackageId) {
        return sysTenantPackageMapper.selectSysTenantPackageByTenantPackageId(tenantPackageId);
    }

    /**
     * 查询租户套餐列表
     *
     * @param sysTenantPackage 租户套餐
     * @return 租户套餐
     */
    @Override
    public List<SysTenantPackage> selectSysTenantPackageList(SysTenantPackage sysTenantPackage) {
        return sysTenantPackageMapper.selectSysTenantPackageList(sysTenantPackage);
    }

    /**
     * 新增租户套餐
     *
     * @param sysTenantPackage 租户套餐
     * @return 结果
     */
    @Override
    public int insertSysTenantPackage(SysTenantPackage sysTenantPackage) {
        sysTenantPackage.setCreateTime(DateUtils.getNowDate());
        return sysTenantPackageMapper.insertSysTenantPackage(sysTenantPackage);
    }

    /**
     * 修改租户套餐
     *
     * @param sysTenantPackage 租户套餐
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSysTenantPackage(SysTenantPackage sysTenantPackage) {
        int i = sysTenantPackageMapper.updateSysTenantPackage(sysTenantPackage);
        List<SysTenantVo> tenantList = sysTenantService.selectSysTenantListByTenantPackageId(sysTenantPackage.getTenantPackageId());
        for (SysTenantVo sysTenant : tenantList) {
            sysTenantService.updateSysTenant(sysTenant);
        }

        sysTenantPackage.setUpdateTime(DateUtils.getNowDate());
        return i;
    }

    /**
     * 批量删除租户套餐
     *
     * @param tenantPackageIds 需要删除的租户套餐主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSysTenantPackageByTenantPackageIds(Long[] tenantPackageIds) {
        for (Long tenantPackageId : tenantPackageIds) {
            if (sysTenantService.selectSysTenantByTenantPackageId(tenantPackageId) > 0) {
                throw new DataException("租户套餐已关联租户，不允许删除");
            }
        }
        return sysTenantPackageMapper.deleteSysTenantPackageByTenantPackageIds(tenantPackageIds);
    }

    /**
     * 删除租户套餐信息
     *
     * @param tenantPackageId 租户套餐主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSysTenantPackageByTenantPackageId(Long tenantPackageId) {
        if (sysTenantService.selectSysTenantByTenantPackageId(tenantPackageId) > 0) {
            throw new DataException("租户套餐已关联租户，不允许删除");
        }
        return sysTenantPackageMapper.deleteSysTenantPackageByTenantPackageId(tenantPackageId);
    }

    /**
     * 根据套餐名称查询租户套餐
     *
     * @param name 餐名称
     * @return 结果
     */
    @Override
    public SysTenantPackage selectSysTenantPackageByName(String name) {
        return sysTenantPackageMapper.selectSysTenantPackageByName(name);
    }

    /**
     * 查询租户套餐列表（不分页）
     *
     * @return 租户套餐集合
     */
    @Override
    public List<SysTenantPackage> selectSysTenantPackageListVo() {
        return sysTenantPackageMapper.selectSysTenantPackageListVo();
    }
}
