# MonthlyReportPropertySettingController.java

    package cloud.wconnect.workperformance.controller.monthlyreport;

    import cloud.wconnect.commonlib.annotation.ApiId;
    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.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.workperformance.cons.monthlyreport.AreaEnum;
    import cloud.wconnect.workperformance.entity.monthlyreport.PropertyMonthlyReportSet;
    import cloud.wconnect.workperformance.service.monthlyreport.IPropertyMonthlyReportSetService;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.google.common.collect.Sets;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

    import java.util.*;

    /**
     * 资产配置区域控制层
     *
     * @author lichen
     * @date 2023/9/5
     */
    @RestController
    @RequestMapping("/monthly-report/property-setting")
    public class MonthlyReportPropertySettingController {

        @Autowired
        private SessionContext sessionContext;
        @Autowired
        private IPropertyMonthlyReportSetService propertyMonthlyReportSetService;
        @Autowired
        private ObjectMapper objectMapper;
        @Autowired
        private PropertyI18nRedisService propertyI18nRedisService;
        @Autowired
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 配置资产区域
         *
         * @param property 资产标识
         * @param areaIds  区域id集合
         * @return java.util.Map
         * @author lichen
         * @date 2023/9/5
         */
        @ApiId(624)
        @PutMapping
        public Map savePropertySetting(String property, @RequestBody List<Integer> areaIds) {
            if (CollectionUtils.isEmpty(areaIds)) {
                throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("areaId"),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The areaIds cannot be empty");
            }
            if (areaIds.size() != Sets.newHashSet(areaIds).size()) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_CANNOT_REPEAT,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("areaId"),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The areaIds cannot be repeat");
            }

            List<Integer> allAreaIds = Arrays.stream(AreaEnum.values()).map(AreaEnum::getAreaId).toList();
            if (!allAreaIds.containsAll(areaIds)) {
                throw new BadRequestException(CommonErrorI18nConstant.MUST_IN_LIST,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("areaId", allAreaIds),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The areaIds is invalid");
            }
            int propertyId = sessionContext.getPropertyId(property);
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);
            int loginUserId = sessionContext.getLoginUserId();
            //保存资产配置区域
            propertyMonthlyReportSetService.savePropertySetting(propertyDTO, areaIds, loginUserId);
            return Collections.emptyMap();
        }

        /**
         * 查询资产配置区域
         *
         * @param property 资产标识
         * @return java.util.List<java.lang.Integer>
         * @author lichen
         * @date 2023/9/5
         */
        @ApiId(625)
        @GetMapping
        public List<Integer> getPropertySetting(String property) {
            //资产id
            int propertyId = sessionContext.getPropertyId(property);
            PropertyMonthlyReportSet propertyMonthlyReportSet = propertyMonthlyReportSetService.getByPropertyId(propertyId);
            if (Objects.isNull(propertyMonthlyReportSet)) return Collections.emptyList();
            return JsonUtils.convertStringToList(objectMapper, propertyMonthlyReportSet.getAreaIds(), Integer.class);
        }

    }