# CbCaseOpenApiController.java

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

    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.Edition;
    import cloud.wconnect.commonlib.service.PropertyInternalService;
    import cloud.wconnect.finance.pojo.bo.chargeback.CaseBO;
    import cloud.wconnect.finance.pojo.bo.chargeback.JournalBO;
    import cloud.wconnect.finance.pojo.query.chargeback.CheckCaseExistQuery;
    import cloud.wconnect.finance.service.chargeback.ICbCaseService;
    import cloud.wconnect.finance.service.chargeback.ICbJournalService;
    import com.google.common.collect.ImmutableMap;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.validation.annotation.Validated;
    import org.springframework.web.bind.annotation.*;

    import jakarta.servlet.http.HttpServletResponse;
    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 Open Api
     *
     * @author yyt
     * 2023/9/8
     */
    @RestController
    @RequestMapping("/openapi/v1/api/chargeback")
    public class CbCaseOpenApiController extends BaseInternalController {

        @Autowired
        private PropertyInternalService propertyInternalService;
        @Autowired
        private ICbCaseService cbCaseService;
        @Autowired
        private ICbJournalService cbJournalService;

        /**
         * 处理 journal 数据
         *
         * @param appId     第三方应用注册时被分配的id标识（w-connect内部请求时为0）
         * @param signature 签名
         * @param property  资产标识（w-connect内部请求时才需要）
         * @param tenant    租户标识（w-connect内部请求时才需要）
         * @param journalBO journal 数据
         * @param response  响应信息
         * @author yyt
         * @date 2023/9/8
         */
        @PostMapping("/journal.json")
        public Map handleJournalData(@RequestHeader Integer appId, @RequestHeader String signature, String property, String tenant,
                                     @Valid @RequestBody JournalBO journalBO, HttpServletResponse response) {

            if (appId != 0) {
                response.setStatus(HttpServletResponse.SC_FORBIDDEN);
                return Collections.emptyMap();
            }

            try {

                // 处理数据
                PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);
                cbJournalService.handleJournalData(propertyDTO, BaseConstant.ZERO, journalBO.getDate(), journalBO.getData());
            } catch (Exception e) {
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                return ImmutableMap.of("error", e.getMessage());
            }

            response.setStatus(HttpServletResponse.SC_OK);
            return Collections.emptyMap();
        }

        /**
         * 检查 case 存在
         *
         * @param appId    第三方应用注册时被分配的id标识（w-connect内部请求时为0）
         * @param signature 签名
         * @param property 资产标识（w-connect内部请求时才需要）
         * @param tenant   租户标识（w-connect内部请求时才需要）
         * @param query    查询条件
         * @return true / false
         * @author yyt
         * @date 2023/9/8
         */
        @GetMapping("/existed")
        public Map<String, Boolean> checkCaseExist(@RequestHeader Integer appId, @RequestHeader String signature, String property,
                                               String tenant, @Valid CheckCaseExistQuery query) {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(BaseConstant.DATE_FORMAT_YYYY_MM_DD);
            PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);

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

        /**
         * 处理 case 数据
         *
         * @param appId    第三方应用注册时被分配的id标识（w-connect内部请求时为0）
         * @param signature 签名
         * @param property 资产标识（w-connect内部请求时才需要）
         * @param tenant   租户标识（w-connect内部请求时才需要）
         * @param caseBO   case 数据
         * @param response 响应信息
         * @author yyt
         * @date 2023/9/8
         */
        @PostMapping("/record.json")
        public Map handleCaseData(@RequestHeader Integer appId, @RequestHeader String signature, String property, String tenant,
                                  @Validated(Edition.class) @RequestBody CaseBO caseBO, HttpServletResponse response) {

            if (appId != 0) {
                response.setStatus(HttpServletResponse.SC_FORBIDDEN);
                return Collections.emptyMap();
            }

            try {
                // 处理数据
                PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);
                cbCaseService.handleCaseData(propertyDTO, BaseConstant.ZERO, caseBO.getData());
            } catch (Exception e) {
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                return ImmutableMap.of("error", e.getMessage());
            }

            response.setStatus(HttpServletResponse.SC_OK);
            return Collections.emptyMap();
        }

    }