# MonthlyReportAreaTypeConfigController.java

    package cloud.wconnect.workperformance.controller.monthlyreport;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.exception.ForbiddenException;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.workperformance.cons.monthlyreport.AreaEnum;
    import cloud.wconnect.workperformance.cons.monthlyreport.MonthlyReportFieldI18nConstant;
    import cloud.wconnect.workperformance.entity.monthlyreport.AreaTypeConfig;
    import cloud.wconnect.workperformance.pojo.dto.monthlyreport.areatypeconfig.AreaTypeConfigDTO;
    import cloud.wconnect.workperformance.service.monthlyreport.IAreaTypeConfigService;
    import com.google.common.collect.ImmutableMap;
    import com.google.common.collect.Lists;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

    import jakarta.validation.Valid;

    import java.text.MessageFormat;
    import java.util.*;

    /**
     * 区域类型配置控制层
     *
     * @author lichen
     * @date 2023/9/5
     */
    @RestController
    @RequestMapping("/monthly-report/area-type-config")
    public class MonthlyReportAreaTypeConfigController {

        @Autowired
        private SessionContext sessionContext;
        @Autowired
        private IAreaTypeConfigService areaTypeConfigService;
        @Autowired
        private BeanMapper beanMapper;
        @Autowired
        private PropertyI18nRedisService propertyI18nRedisService;
        @Autowired
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 查询区域类型
         *
         * @param property 资产标识
         * @param areaId   区域id
         * @return java.util.List<cloud.wconnect.workperformance.pojo.dto.monthlyreport.areatypeconfig.AreaTypeConfigDTO>
         * @author lichen
         * @date 2023/9/5
         */
        @ApiId(626)
        @GetMapping
        public List<AreaTypeConfigDTO> listAreaTypeConfig(String property, @RequestParam int areaId) {
            int propertyId = sessionContext.getPropertyId(property);
            //校验区域id是否合法
            AreaEnum.verifyValidity(areaId);

            List<AreaTypeConfig> areaTypeConfigList = areaTypeConfigService.listByAreaId(propertyId, Lists.newArrayList(areaId));
            return beanMapper.mapList(areaTypeConfigList, AreaTypeConfigDTO.class);
        }

        /**
         * 保存区域类型
         *
         * @param property          资产标识
         * @param areaTypeConfigDTO 区域类型dto
         * @return java.util.Map
         * @author lichen
         * @date 2023/9/5
         */
        @ApiId(627)
        @PostMapping
        public Map save(String property, @RequestBody @Valid AreaTypeConfigDTO areaTypeConfigDTO) {
            //对name进行去除空格处理
            String name = areaTypeConfigDTO.getName().trim();
            areaTypeConfigDTO.setName(name);

            //Room Revenue,Profit,Forecast 区域此值是必传项
            List<Integer> areaIds = Lists.newArrayList(AreaEnum.ROOM_REVENUE.getAreaId(), AreaEnum.PROFIT.getAreaId(), AreaEnum.FORECAST.getAreaId());
            if (areaIds.contains(areaTypeConfigDTO.getAreaId()) && Objects.isNull(areaTypeConfigDTO.getContentType())) {
                throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                        i18nMessageUtils.getErrorParamMapNeedTranslate(MonthlyReportFieldI18nConstant.DATA_FORMAT),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The contentType cannot be null, when areaId between in [1:Room Revenue 2:Profit 4:Forecast]");
            }
            if (!areaIds.contains(areaTypeConfigDTO.getAreaId()) && Objects.nonNull(areaTypeConfigDTO.getContentType())) {
                throw new BadRequestException(CommonErrorI18nConstant.MUST_EMPTY,
                        i18nMessageUtils.getErrorParamMapNeedTranslate(MonthlyReportFieldI18nConstant.DATA_FORMAT),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The contentType must be null, when areaId between not in [1:Room Revenue 2:Profit 4:Forecast]");
            }
            //Room Revenue,Forecast 区域,type是必传项
            if (Lists.newArrayList(AreaEnum.ROOM_REVENUE.getAreaId(), AreaEnum.FORECAST.getAreaId()).contains(areaTypeConfigDTO.getAreaId())
                    && Objects.isNull(areaTypeConfigDTO.getContentType())) {
                throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("type"),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The type cannot be null, when areaId between in [1:Room Revenue 4:Forecast]");
            }

            int propertyId = sessionContext.getPropertyId(property);
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);
            int loginUserId = sessionContext.getLoginUserId();

            if (areaTypeConfigDTO.getId() > 0) {//修改
                AreaTypeConfig oriAreaTypeConfig = areaTypeConfigService.checkExist(areaTypeConfigDTO.getId());
                checkBelong(oriAreaTypeConfig, propertyId);
                if (!oriAreaTypeConfig.getAreaId().equals(areaTypeConfigDTO.getAreaId())) {
                    throw new BadRequestException(CommonErrorI18nConstant.CANNOT_MODIFIED,
                            i18nMessageUtils.getErrorParamMapNotNeedTranslate("areaId"),
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                            "The areaId cannot be modified");
                }
                //校验分类名称是否重复
                areaTypeConfigService.checkNameRepeat(propertyId, areaTypeConfigDTO.getAreaId(), areaTypeConfigDTO.getName(), areaTypeConfigDTO.getId());
                //校验type是否重复
                areaTypeConfigService.checkTypeRepeat(propertyId, areaTypeConfigDTO.getAreaId(), areaTypeConfigDTO.getType(), areaTypeConfigDTO.getId());
                areaTypeConfigService.update(oriAreaTypeConfig, areaTypeConfigDTO, propertyDTO);
                return ImmutableMap.of("id", areaTypeConfigDTO.getId());
            } else {//新增
                //校验分类名称是否重复
                areaTypeConfigService.checkNameRepeat(propertyId, areaTypeConfigDTO.getAreaId(), areaTypeConfigDTO.getName(), null);
                //校验type是否重复
                areaTypeConfigService.checkTypeRepeat(propertyId, areaTypeConfigDTO.getAreaId(), areaTypeConfigDTO.getType(), null);
                return areaTypeConfigService.save(areaTypeConfigDTO, propertyDTO, loginUserId);
            }
        }

        /**
         * 删除区域类型
         *
         * @param property 资产标识
         * @param id       分类id
         * @return java.util.Map
         * @author lichen
         * @date 2023/9/6
         */
        @ApiId(628)
        @DeleteMapping("/{id}")
        public Map delete(String property, @PathVariable Integer id) {
            int propertyId = sessionContext.getPropertyId(property);
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);
            AreaTypeConfig oriAreaTypeConfig = areaTypeConfigService.checkExist(id);
            checkBelong(oriAreaTypeConfig, propertyId);

            areaTypeConfigService.remove(oriAreaTypeConfig, propertyDTO);
            return Collections.emptyMap();
        }

        /**
         * 校验分类数据是否归属于当前资产
         *
         * @param oriAreaTypeConfig 分类数据
         * @param propertyId        资产id
         * @return void
         * @author lichen
         * @date 2023/9/6
         */
        private void checkBelong(AreaTypeConfig oriAreaTypeConfig, Integer propertyId) {
            if (!oriAreaTypeConfig.getPropertyId().equals(propertyId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(), BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        MessageFormat.format("The category not belong to current property, categoryId:{0}, categoryBelongPropertyId:{1}, currentPropertyId:{2}",
                                oriAreaTypeConfig.getId(), oriAreaTypeConfig.getPropertyId(), propertyId));
            }
        }
    }