# CbCaseBankController.java

    package cloud.wconnect.finance.controller.chargeback;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.exception.ForbiddenException;
    import cloud.wconnect.finance.entity.chargeback.CbCaseBankOverTimeDay;
    import cloud.wconnect.finance.pojo.dto.chargeback.BankDTO;
    import cloud.wconnect.finance.pojo.dto.chargeback.BankOverTimeDTO;
    import cloud.wconnect.finance.service.chargeback.ICbCaseBankOverTimeDayService;
    import cloud.wconnect.finance.service.chargeback.ICbCaseBankService;
    import jakarta.validation.Valid;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

    import java.util.Collections;
    import java.util.List;
    import java.util.Map;

    /**
     * <p>
     * 前端控制器
     * </p>
     *
     * @author yyt
     * @since 2023-08-22
     */
    @RestController
    @RequestMapping("/charge-back/bank")
    public class CbCaseBankController {

        @Autowired
        private ICbCaseBankService cbCaseBankService;
        @Autowired
        private ICbCaseBankOverTimeDayService cbCaseBankOverTimeDayService;
        @Autowired
        private BeanMapper beanMapper;
        @Autowired
        private SessionContext sessionContext;

        /**
         * 获取 bank 列表
         *
         * @param property 资产标识
         * @return bank 列表
         * @author yyt
         * @date 2023/8/24
         */
        @ApiId(588)
        @GetMapping
        public List<BankDTO> list(String property) {
            Integer propertyId = sessionContext.getPropertyId(property);
            return beanMapper.mapList(cbCaseBankService.list(propertyId), BankDTO.class);
        }

        /**
         * 获取 bank 超期天数列表
         *
         * @param property 资产标识
         * @return bank 超期天数列表
         * @author yyt
         * @date 2023/8/24
         */
        @ApiId(589)
        @GetMapping("/over-time")
        public List<BankOverTimeDTO> listOverTime(String property) {
            return cbCaseBankOverTimeDayService.listOverTimeDTO(sessionContext.getPropertyId(property));
        }

        /**
         * 保存 bank 超期天数
         *
         * @param id 数据 id
         * @return {}
         * @author yyt
         * @date 2023/8/24
         */
        @ApiId(590)
        @PostMapping("/over-time/{id:\\d+}")
        public Map saveOverTime(@PathVariable int id, String property, @Valid @RequestBody BankOverTimeDTO bankOverTimeDTO) {
            cbCaseBankService.checkExist(bankOverTimeDTO.getBankId());

            Integer propertyId = sessionContext.getPropertyId(property);
            int userId = sessionContext.getLoginUserId();
            if (id == BaseConstant.ZERO) {
                cbCaseBankOverTimeDayService.checkBankBound(propertyId, bankOverTimeDTO.getBankId(), null);

                cbCaseBankOverTimeDayService.save(propertyId, bankOverTimeDTO, userId);
            } else {
                CbCaseBankOverTimeDay overTimeDay = cbCaseBankOverTimeDayService.checkExist(id);
                checkBelong(overTimeDay, propertyId);

                cbCaseBankOverTimeDayService.checkBankBound(propertyId, bankOverTimeDTO.getBankId(), id);

                cbCaseBankOverTimeDayService.update(id, bankOverTimeDTO, userId);
            }

            return Collections.emptyMap();
        }

        /**
         * 删除 bank 超期天数
         *
         * @param id 数据 id
         * @return {}
         * @author yyt
         * @date 2023/8/24
         */
        @ApiId(591)
        @DeleteMapping("/over-time/{id:\\d+}")
        public Map deleteOverTime(@PathVariable int id, String property) {
            CbCaseBankOverTimeDay overTimeDay = cbCaseBankOverTimeDayService.checkExist(id);
            checkBelong(overTimeDay, sessionContext.getPropertyId(property));

            cbCaseBankOverTimeDayService.delete(id);

            return Collections.emptyMap();
        }

        /**
         * 校验数据归属
         *
         * @param overTimeDay 超期天数
         * @param propertyId  资产 id
         * @author yyt
         * @date 2023/8/25
         */
        private void checkBelong(CbCaseBankOverTimeDay overTimeDay, Integer propertyId) {
            if (!overTimeDay.getPropertyId().equals(propertyId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(), "the property id parameter is different from " +
                        "the property id of the cbCaseBankOverTimeDay;propertyId:" + propertyId + ",cbCaseBankOverTimeDayPropertyId:" +
                        overTimeDay.getPropertyId() + ",cbCaseBankOverTimeDayId:" + overTimeDay.getId());
            }
        }

    }