# CbCaseBankServiceImpl.java

    package cloud.wconnect.finance.service.chargeback.impl;

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.exception.ForbiddenException;
    import cloud.wconnect.finance.entity.chargeback.CbCaseBank;
    import cloud.wconnect.finance.mapper.chargeback.CbCaseBankMapper;
    import cloud.wconnect.finance.service.chargeback.ICbCaseBankService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.google.common.collect.Lists;
    import org.springframework.stereotype.Service;

    import java.util.List;
    import java.util.Map;
    import java.util.Objects;
    import java.util.Set;
    import java.util.stream.Collectors;

    /**
     * <p>
     *  服务实现类
     * </p>
     *
     * @author yyt
     * @since 2023-08-22
     */
    @Service
    @DS("finance")
    public class CbCaseBankServiceImpl extends ServiceImpl<CbCaseBankMapper, CbCaseBank> implements ICbCaseBankService {

        /**
         * 校验存在
         *
         * @param id 数据 id
         * @author yyt
         * @date 2023/8/24
         */
        @Override
        public CbCaseBank checkExist(Integer id) {
            CbCaseBank bank = getById(id);
            if (Objects.isNull(bank)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND, BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "the bank is not found;id:" + id);
            }
            return bank;
        }

        /**
         * 获取某资产可用的 平台
         *
         * @param propertyId 资产 id
         * @return 平台
         * @author yyt
         * @date 2023/9/12
         */
        @Override
        public List<CbCaseBank> list(Integer propertyId) {
            return list(new QueryWrapper<CbCaseBank>().lambda().in(CbCaseBank::getPropertyId,
                            Lists.newArrayList(BaseConstant.ZERO, propertyId))
                    .orderByAsc(CbCaseBank::getId));
        }

        /**
         * 获取资产可用的 平台
         *
         * @param propertyIds 资产 id
         * @return 平台
         * @author yyt
         * @date 2023/9/12
         */
        @Override
        public Map<Integer, String> list(Set<Integer> propertyIds) {
            return list(new QueryWrapper<CbCaseBank>().lambda().nested(query -> {
                query.in(CbCaseBank::getPropertyId, propertyIds).or().eq(CbCaseBank::getPropertyId, BaseConstant.ZERO);
            })).stream().collect(Collectors.toMap(CbCaseBank::getId, CbCaseBank::getName));
        }

        /**
         * 校验 bank 属于某个资产
         *
         * @param propertyId 资产 id
         * @param bankId     bank id
         * @author yyt
         * @date 2023/9/12
         */
        @Override
        public void checkBank(Integer propertyId, Integer bankId) {
            CbCaseBank bank = checkExist(bankId);
            // 公共的 不校验
            if (bank.getPropertyId() == BaseConstant.ZERO) {
                return;
            }
            if (!bank.getPropertyId().equals(propertyId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(), "the bank is not belong to the property;" +
                        "propertyId:" + propertyId + ",bankPropertyId:" + bank.getPropertyId());
            }
        }

    }