# CbCaseBankOverTimeDayServiceImpl.java

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

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.finance.entity.chargeback.CbCaseBank;
    import cloud.wconnect.finance.entity.chargeback.CbCaseBankOverTimeDay;
    import cloud.wconnect.finance.mapper.chargeback.CbCaseBankOverTimeDayMapper;
    import cloud.wconnect.finance.pojo.dto.chargeback.BankOverTimeDTO;
    import cloud.wconnect.finance.service.chargeback.ICbCaseBankOverTimeDayService;
    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 org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

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

    /**
     * <p>
     * bank 超期天数设置 服务实现类
     * </p>
     *
     * @author yyt
     * @since 2023-08-22
     */
    @Service
    @DS("finance")
    public class CbCaseBankOverTimeDayServiceImpl extends ServiceImpl<CbCaseBankOverTimeDayMapper, CbCaseBankOverTimeDay> implements ICbCaseBankOverTimeDayService {

        @Autowired
        private ICbCaseBankService cbCaseBankService;
        @Autowired
        private BeanMapper beanMapper;

        /**
         * 验证 bank 绑定过
         *
         * @param propertyId 资产 id
         * @param bankId     bank id
         * @param id         排除自身
         * @author yyt
         * @date 2023/8/24
         */
        @Override
        public void checkBankBound(Integer propertyId, Integer bankId, Integer id) {
            CbCaseBankOverTimeDay overTimeDay = getOne(new QueryWrapper<CbCaseBankOverTimeDay>().lambda()
                    .eq(CbCaseBankOverTimeDay::getPropertyId, propertyId)
                    .eq(CbCaseBankOverTimeDay::getBankId, bankId).ne(Objects.nonNull(id), CbCaseBankOverTimeDay::getId, id));
            if (Objects.nonNull(overTimeDay)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_EXISTED, BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "Failed to save existing data");
            }
        }

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

        /**
         * 获取资产下的超期天数设置
         *
         * @param propertyId 资产 id
         * @return 超期天数设置
         * @author yyt
         * @date 2023/8/25
         */
        @Override
        public List<CbCaseBankOverTimeDay> listByPropertyId(Integer propertyId) {
            return list(new QueryWrapper<CbCaseBankOverTimeDay>().lambda().eq(CbCaseBankOverTimeDay::getPropertyId, propertyId)
                    .orderByDesc(CbCaseBankOverTimeDay::getId));
        }

        /**
         * 获取超期天数
         *
         * @param propertyId 资产 id
         * @param bankId     bank id
         * @return 超期天数
         * @author yyt
         * @date 2023/9/14
         */
        @Override
        public CbCaseBankOverTimeDay get(Integer propertyId, Integer bankId) {
            return getOne(new QueryWrapper<CbCaseBankOverTimeDay>().lambda().eq(CbCaseBankOverTimeDay::getPropertyId, propertyId)
                    .eq(CbCaseBankOverTimeDay::getBankId, bankId));
        }

        /**
         * 删除超期天数设置
         *
         * @param id 数据 id
         * @author yyt
         * @date 2023/8/25
         */
        @Override
        public void delete(int id) {
            removeById(id);
        }

        /**
         * 获取超期天数设置
         *
         * @param propertyId 资产 id
         * @return 超期天数设置
         * @author yyt
         * @date 2026/1/19
         */
        @Override
        public List<BankOverTimeDTO> listOverTimeDTO(int propertyId) {
            List<CbCaseBankOverTimeDay> overTimeDays = listByPropertyId(propertyId);
            if (overTimeDays.isEmpty()) {
                return Collections.emptyList();
            }
            List<Integer> bankIds = overTimeDays.stream().map(CbCaseBankOverTimeDay::getBankId).distinct().collect(Collectors.toList());
            Map<Integer, String> baneNameById = cbCaseBankService.listByIds(bankIds)
                    .stream().collect(Collectors.toMap(CbCaseBank::getId, CbCaseBank::getName));
            return beanMapper.mapList(overTimeDays, BankOverTimeDTO.class,
                    item -> item.setName(baneNameById.get(item.getBankId())));
        }

        /**
         * 保存超期天数设置
         *
         * @param propertyId      资产 id
         * @param bankOverTimeDTO bank 超期天数设置
         * @param userId          操作人
         * @author yyt
         * @date 2026/1/19
         */
        @Override
        public void save(Integer propertyId, BankOverTimeDTO bankOverTimeDTO, int userId) {
            save(new CbCaseBankOverTimeDay().setPropertyId(propertyId)
                    .setBankId(bankOverTimeDTO.getBankId())
                    .setDay(bankOverTimeDTO.getDay())
                    .setUpdatedBy(userId)
                    .setUpdatedTime(DateTimeUtils.nowTimestamp()));
        }

        /**
         * 更新超期天数设置
         *
         * @param id              数据 id
         * @param bankOverTimeDTO bank 超期天数设置
         * @param userId          操作人
         * @author yyt
         * @date 2026/1/19
         */
        @Override
        public void update(int id, BankOverTimeDTO bankOverTimeDTO, int userId) {
            updateById(new CbCaseBankOverTimeDay().setId(id)
                    .setBankId(bankOverTimeDTO.getBankId())
                    .setDay(bankOverTimeDTO.getDay())
                    .setUpdatedBy(userId)
                    .setUpdatedTime(DateTimeUtils.nowTimestamp()));
        }

    }