# ClcOpenApiController.java

    package cloud.wconnect.frontoffice.controller.clc.openapi;

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.controller.BaseInternalController;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.service.PropertyInternalService;
    import cloud.wconnect.frontoffice.pojo.bo.clc.*;
    import cloud.wconnect.frontoffice.pojo.group.Addition;
    import cloud.wconnect.frontoffice.pojo.group.General;
    import cloud.wconnect.frontoffice.service.clc.*;
    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 java.util.Collections;
    import java.util.Map;

    /**
     * 控制层：Clc Open Api
     *
     * @author yyt
     * 2023/7/27
     */
    @Validated
    @RestController
    @RequestMapping("/openapi/v1/clc")
    public class ClcOpenApiController extends BaseInternalController {

        @Autowired
        private PropertyInternalService propertyInternalService;
        @Autowired
        private IClcArrivalDetailedService clcArrivalDetailedService;
        @Autowired
        private IClcSiteDataService clcSiteDataService;
        @Autowired
        private IClcCardDataService clcCardDataService;
        @Autowired
        private IClcGuestInhService clcGuestInhService;
        @Autowired
        private IClcMissingReportService clcMissingReportService;
        @Autowired
        private IClcSiteCrewFaxService clcSiteCrewFaxService;

        /**
         * 处理 入住信息数据
         *
         * @param appId     第三方应用注册时被分配的id标识（w-connect内部请求时为0）
         * @param signature 签名
         * @param dataBO    入住信息
         * @param response  响应信息
         * @author yyt
         * @date 2023/7/27
         */
        @PostMapping("/arrival-detail.json")
        public Map handleArrivalData(@RequestHeader Integer appId, @RequestHeader String signature, String property, String tenant,
                                     @Validated(Addition.class) @RequestBody DataBO<ArrivalBO> dataBO, HttpServletResponse response) {

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

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

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

        /**
         * 处理 site data
         *
         * @param appId     第三方应用注册时被分配的id标识（w-connect内部请求时为0）
         * @param signature 签名
         * @param property  资产标识（w-connect内部请求时才需要）
         * @param tenant    租户标识（w-connect内部请求时才需要）
         * @param dataBO    site data
         * @param response  响应信息
         * @author yyt
         * @date 2023/7/27
         */
        @PostMapping("/site-data.json")
        public Map handleSiteData(@RequestHeader Integer appId, @RequestHeader String signature, String property, String tenant,
                                  @Validated(General.class) @RequestBody DataBO<SiteDataBO> dataBO, HttpServletResponse response) {
            if (appId != 0) {
                response.setStatus(HttpServletResponse.SC_FORBIDDEN);
                return Collections.emptyMap();
            }

            try {
                // 处理数据
                PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);
                clcSiteDataService.handleSiteData(propertyDTO, dataBO.getData(), null);
            } catch (Exception e) {
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                return ImmutableMap.of("error", e.getMessage());
            }

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

        /**
         * 处理 card data
         *
         * @param appId     第三方应用注册时被分配的id标识（w-connect内部请求时为0）
         * @param signature 签名
         * @param property  资产标识（w-connect内部请求时才需要）
         * @param tenant    租户标识（w-connect内部请求时才需要）
         * @param dataBO    card data
         * @param response  响应信息
         * @author yyt
         * @date 2023/7/27
         */
        @PostMapping("/card-data.json")
        public Map handleCardData(@RequestHeader Integer appId, @RequestHeader String signature, String property, String tenant,
                                  @Validated(General.class) @RequestBody DataBO<CardDataBO> dataBO, HttpServletResponse response) {
            if (appId != 0) {
                response.setStatus(HttpServletResponse.SC_FORBIDDEN);
                return Collections.emptyMap();
            }

            try {
                // 处理数据
                PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);
                clcCardDataService.handleCardData(propertyDTO, dataBO.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();
        }

        /**
         * 处理 guest in house 数据
         *
         * @param appId     第三方应用注册时被分配的id标识（w-connect内部请求时为0）
         * @param signature 签名
         * @param property  资产标识（w-connect内部请求时才需要）
         * @param tenant    租户标识（w-connect内部请求时才需要）
         * @param dataBO    guest in house 数据
         * @param response  响应信息
         * @author yyt
         * @date 2023/7/28
         */
        @PostMapping("/guest-in-house.json")
        public Map handleGuestInHouse(@RequestHeader Integer appId, @RequestHeader String signature, String property, String tenant,
                                  @Validated(General.class) @RequestBody DataBO<GuestInHouseBO> dataBO, HttpServletResponse response) {

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

            try {
                // 处理数据
                PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);
                clcGuestInhService.handleGuestInHouseData(propertyDTO, BaseConstant.ZERO, dataBO.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();
        }

        /**
         * 处理 crew fax 数据
         *
         * @param appId     第三方应用注册时被分配的id标识（w-connect内部请求时为0）
         * @param signature 签名
         * @param property  资产标识（w-connect内部请求时才需要）
         * @param tenant    租户标识（w-connect内部请求时才需要）
         * @param dataBO    crew fax 数据
         * @param response  响应信息
         * @author yyt
         * @date 2023/7/28
         */
        @PostMapping("/crew-fax.json")
        public Map handleCrewFax(@RequestHeader Integer appId, @RequestHeader String signature, String property, String tenant,
                                  @Validated(General.class) @RequestBody DataBO<CrewFaxBO> dataBO, HttpServletResponse response) {
            if (appId != 0) {
                response.setStatus(HttpServletResponse.SC_FORBIDDEN);
                return Collections.emptyMap();
            }

            try {
                // 处理数据
                PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);
                clcSiteCrewFaxService.handleCrewFax(propertyDTO, dataBO.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();
        }

        /**
         * 保存爬取日志
         *
         * @param appId     第三方应用注册时被分配的id标识（w-connect内部请求时为0）
         * @param signature 签名
         * @param property  资产标识（w-connect内部请求时才需要）
         * @param tenant    租户标识（w-connect内部请求时才需要）
         * @param crawLogBO 爬取日志
         * @param response  响应信息
         * @author yyt
         * @date 2023/7/31
         */
        @PostMapping("/crawl-log.json")
        public Map saveCrawlLog(@RequestHeader Integer appId, @RequestHeader String signature, String property, String tenant,
                                @Validated(Addition.class) @RequestBody CrawLogBO crawLogBO, HttpServletResponse response) {
            if (appId != 0) {
                response.setStatus(HttpServletResponse.SC_FORBIDDEN);
                return Collections.emptyMap();
            }

            try {
                // 保存日志
                PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);
                clcMissingReportService.saveLog(propertyDTO, crawLogBO);

            } catch (Exception e) {
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                return ImmutableMap.of("error", e.getMessage());
            }

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

    }