# CbCaseInternalController.java

    package cloud.wconnect.finance.controller.chargeback.internal;

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.controller.BaseInternalController;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.pojo.dto.group.Addition;
    import cloud.wconnect.commonlib.service.PropertyInternalService;
    import cloud.wconnect.finance.pojo.bo.chargeback.CaseListBO;
    import cloud.wconnect.finance.pojo.query.chargeback.CheckCaseExistQuery;
    import cloud.wconnect.finance.service.chargeback.ICbCaseBankService;
    import cloud.wconnect.finance.service.chargeback.ICbCaseService;
    import com.google.common.collect.ImmutableMap;
    import com.google.common.collect.Lists;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.validation.annotation.Validated;
    import org.springframework.web.bind.annotation.*;

    import jakarta.validation.Valid;
    import java.time.LocalDate;
    import java.time.ZoneId;
    import java.time.format.DateTimeFormatter;
    import java.util.Collections;
    import java.util.Map;

    /**
     * 控制层：Case
     *
     * @author yyt
     * 2023/9/9
     */
    @Validated
    @RestController
    @RequestMapping("/internal/charge-back")
    public class CbCaseInternalController extends BaseInternalController {

        @Autowired
        private PropertyInternalService propertyInternalService;
        @Autowired
        private ICbCaseBankService cbCaseBankService;
        @Autowired
        private ICbCaseService cbCaseService;

        @Value("${internal.service.auth-key.basic}")
        private String authKey;

        /**
         * 检查 case 存在
         *
         * @param sign     签名
         * @param tenant   租户标识
         * @param property 资产标识
         * @param query    查询条件
         * @return true / false
         * @author yyt
         * @date 2023/9/9
         */
        @GetMapping("/existed")
        public Map<String, Boolean> checkExist(@RequestHeader String sign, @RequestParam String tenant, @RequestParam String property,
                                               @Valid CheckCaseExistQuery query) {
            checkIntranetParams(authKey, sign, tenant, property, query.getVendor());

            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(BaseConstant.DATE_FORMAT_YYYY_MM_DD);
            PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);

            cbCaseBankService.checkBank(propertyDTO.getId(), query.getVendor());

            return ImmutableMap.of("existed", cbCaseService.checkExist(propertyDTO.getId(), query.getCaseNumber(),
                    LocalDate.parse(query.getIssueDate(), dateTimeFormatter).atStartOfDay(ZoneId.of(propertyDTO.getTimezone()))
                            .toEpochSecond(), null));
        }

        /**
         * 保存采集 case 数据
         *
         * @param sign       签名
         * @param property   资产标识
         * @param tenant     租户标识
         * @param caseListBO case 数据
         * @author yyt
         * @date 2023/9/9
         */
        @PostMapping
        public Map handleCaseData(@RequestHeader String sign, @RequestParam String tenant, @RequestParam String property,
                                  @Validated(Addition.class) @RequestBody CaseListBO caseListBO) {

            checkIntranetParams(authKey, sign, tenant, property, caseListBO.getVendor());

            // 处理数据
            PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);

            cbCaseBankService.checkBank(propertyDTO.getId(), caseListBO.getVendor());

            cbCaseService.handleCaseData(propertyDTO, BaseConstant.ZERO, Lists.newArrayList(caseListBO));

            return Collections.emptyMap();
        }

    }