package cn.iocoder.yudao.module.system.service.tenantagencypackage;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.module.system.dal.dataobject.tenantagencypackage.TenantAgencyPackageDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.tenantagencypackage.vo.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.tenantagencypackage.TenantAgencyPackageMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 代理租户套餐 Service 实现类
 *
 * @author 总后台
 */
@Service
@Validated
public class TenantAgencyPackageServiceImpl implements TenantAgencyPackageService {

    @Resource
    private TenantAgencyPackageMapper tenantAgencyPackageMapper;

    @Override
    public Long createTenantAgencyPackage(TenantAgencyPackageSaveReqVO createReqVO) {
        // 插入
        TenantAgencyPackageDO tenantAgencyPackage = BeanUtils.toBean(createReqVO, TenantAgencyPackageDO.class);
        tenantAgencyPackageMapper.insert(tenantAgencyPackage);
        // 返回
        return tenantAgencyPackage.getId();
    }

    @Override
    public void updateTenantAgencyPackage(TenantAgencyPackageSaveReqVO updateReqVO) {
        // 校验存在
        validateTenantAgencyPackageExists(updateReqVO.getId());
        // 更新
        TenantAgencyPackageDO updateObj = BeanUtils.toBean(updateReqVO, TenantAgencyPackageDO.class);
        tenantAgencyPackageMapper.updateById(updateObj);
    }

    @Override
    public void deleteTenantAgencyPackage(Long id) {
        // 校验存在
        validateTenantAgencyPackageExists(id);
        // 删除
        tenantAgencyPackageMapper.deleteById(id);
    }

    @Override
        public void deleteTenantAgencyPackageListByIds(List<Long> ids) {
        // 校验存在
        validateTenantAgencyPackageExists(ids);
        // 删除
        tenantAgencyPackageMapper.deleteByIds(ids);
        }

    private void validateTenantAgencyPackageExists(List<Long> ids) {
        List<TenantAgencyPackageDO> list = tenantAgencyPackageMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(TENANT_AGENCY_PACKAGE_NOT_EXISTS);
        }
    }

    private void validateTenantAgencyPackageExists(Long id) {
        if (tenantAgencyPackageMapper.selectById(id) == null) {
            throw exception(TENANT_AGENCY_PACKAGE_NOT_EXISTS);
        }
    }

    @Override
    public TenantAgencyPackageDO getTenantAgencyPackage(Long id) {
        return tenantAgencyPackageMapper.selectById(id);
    }

    @Override
    public PageResult<TenantAgencyPackageDO> getTenantAgencyPackagePage(TenantAgencyPackagePageReqVO pageReqVO) {
        return tenantAgencyPackageMapper.selectPage(pageReqVO);
    }

    @Override
    public TenantAgencyPackageDO validTenantPackage(Long packageId) {

        TenantAgencyPackageDO tenantAgencyPackageDO = tenantAgencyPackageMapper.selectById(packageId);

        if (tenantAgencyPackageDO == null) {
            throw exception(TENANT_PACKAGE_NOT_EXISTS);
        }
        if (tenantAgencyPackageDO.getStatus().equals(CommonStatusEnum.DISABLE.getStatus())) {
            throw exception(TENANT_PACKAGE_DISABLE, tenantAgencyPackageDO.getName());
        }
        return tenantAgencyPackageDO;
    }

    @Override
    public List<TenantAgencyPackageDO> getTenantPackageListByStatus(Integer status) {
        return tenantAgencyPackageMapper.selectListByStatus(status);
    }

}