# ScheduleOpenApiController.java

    package cloud.wconnect.workperformance.controller.schedule.openapi;

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.controller.BaseInternalController;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.service.PropertyInternalService;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.workperformance.pojo.bo.schedule.ScheduleBO;
    import cloud.wconnect.workperformance.pojo.bo.schedule.ScheduleDepartmentEmployeeBO;
    import cloud.wconnect.workperformance.pojo.bo.schedule.ScheduleHistoryAndForecastBO;
    import cloud.wconnect.workperformance.service.schedule.IScheduleDepartmentEmployeeService;
    import cloud.wconnect.workperformance.service.schedule.IScheduleHistoryAndForecastService;
    import cloud.wconnect.workperformance.service.schedule.IScheduleInfoService;
    import com.google.common.collect.ImmutableMap;
    import com.google.common.collect.Lists;
    import org.apache.commons.collections.CollectionUtils;
    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.format.DateTimeFormatter;
    import java.util.Collections;
    import java.util.List;
    import java.util.Map;

    /**
     * 控制层：Schedule Open Api
     *
     * @author yyt
     * 2023/7/5
     */
    @Validated
    @RestController
    @RequestMapping("/openapi/v1/schedule")
    public class ScheduleOpenApiController extends BaseInternalController {

        @Autowired
        private IScheduleHistoryAndForecastService scheduleHistoryAndForecastService;
        @Autowired
        private PropertyInternalService propertyInternalService;
        @Autowired
        private IScheduleDepartmentEmployeeService scheduleDepartmentEmployeeService;
        @Autowired
        private IScheduleInfoService scheduleInfoService;
        @Autowired
        private I18nMessageUtils i18nMessageUtils;

        public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        /**
         * 处理运营数据
         *
         * @param appId                  第三方应用注册时被分配的id标识（w-connect内部请求时为0）
         * @param sign                   签名
         * @param property               资产标识（w-connect内部请求时才需要）
         * @param tenant                 租户标识（w-connect内部请求时才需要）
         * @param historyAndForecastList history and forecast 数据
         * @param response               响应信息
         * @author yyt
         * @date 2023/7/5
         */
        @PostMapping("/operating-info.json")
        public Map handleHistoryAndForecastData(@RequestParam Integer appId, @RequestParam String sign, String property,
                                                String tenant, @RequestBody List<@Valid ScheduleHistoryAndForecastBO> historyAndForecastList,
                                                HttpServletResponse response) {
            if (CollectionUtils.isEmpty(historyAndForecastList)) {
                throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("historyAndForecastList"),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the historyAndForecastList must not be empty");
            }

            // 校验日期格式
            historyAndForecastList.forEach(item -> checkDateFormat(item.getTargetDate()));

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

            try {
                // 处理数据
                PropertyDTO propertyDTO = propertyInternalService.getProperty(Lists.newArrayList(property), tenant).get(0);
                scheduleHistoryAndForecastService.handleHistoryAndForecastData(propertyDTO, historyAndForecastList);
            } 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 targetDate 日期
         * @author yyt
         * @date 2023/7/10
         */
        private void checkDateFormat(String targetDate) {
            try {
                LocalDate.parse(targetDate, DATE_TIME_FORMATTER);
            } catch (Exception e) {
                throw new BadRequestException(CommonErrorI18nConstant.FORMAT_ERROR,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("targetDate"),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the date format error");
            }
        }

        /**
         * 处理员工信息
         *
         * @param appId               第三方应用注册时被分配的id标识（w-connect内部请求时为0）
         * @param sign                签名
         * @param property            资产标识（w-connect内部请求时才需要）
         * @param tenant              租户标识（w-connect内部请求时才需要）
         * @param departmentEmployees 员工信息
         * @param response            响应信息
         * @author yyt
         * @date 2023/7/8
         */
        @PostMapping("/employees.json")
        public Map handleEmployeeData(@RequestParam Integer appId, @RequestParam String sign, String property,
                                      String tenant, @RequestBody List<@Valid ScheduleDepartmentEmployeeBO> departmentEmployees,
                                      HttpServletResponse response) {
            if (CollectionUtils.isEmpty(departmentEmployees)) {
                throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("departmentEmployees"),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the departmentEmployees must not be empty");
            }

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

            try {
                // 处理数据
                Integer propertyId = propertyInternalService.getProperty(Lists.newArrayList(property), tenant).get(0).getId();
                scheduleDepartmentEmployeeService.handleInsertDepartmentAndEmployee(propertyId, departmentEmployees, BaseConstant.ZERO);
            } 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 sign      签名
         * @param property  资产标识（w-connect内部请求时才需要）
         * @param tenant    租户标识（w-connect内部请求时才需要）
         * @param schedules 排班信息
         * @param response  响应信息
         * @author yyt
         * @date 2023/7/8
         */
        @PostMapping("/records.json")
        public Map handleScheudleData(@RequestParam Integer appId, @RequestParam String sign, String property,
                                      String tenant, @RequestBody List<@Valid ScheduleBO> schedules,
                                      HttpServletResponse response) {
            if (CollectionUtils.isEmpty(schedules)) {
                throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("schedules"),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the schedules must not be empty");
            }

            // 校验日期格式
            schedules.forEach(item -> checkDateFormat(item.getTargetDate()));

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

            try {
                // 处理数据
                PropertyDTO propertyDTO = propertyInternalService.getProperty(Lists.newArrayList(property), tenant).get(0);
                scheduleInfoService.handleInsertSchedule(propertyDTO, schedules, BaseConstant.ZERO);
            } catch (Exception e) {
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                return ImmutableMap.of("error", e.getMessage());
            }

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

    }