# HkInventoryItemConfigController.java

    package cloud.wconnect.workperformance.controller.hkinventory;

    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.ListPagedDTO;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.workperformance.entity.hkinventory.HkInventoryCategoryConfig;
    import cloud.wconnect.workperformance.entity.hkinventory.HkInventoryItemConfig;
    import cloud.wconnect.workperformance.entity.hkinventory.HkInventoryRoomTypeConfig;
    import cloud.wconnect.workperformance.pojo.dto.hkinventory.HkInventoryItemConfigDTO;
    import cloud.wconnect.workperformance.pojo.query.hkinventory.HkInventoryItemConfigQuery;
    import cloud.wconnect.workperformance.pojo.vo.hkinventory.HkInventoryItemConfigVO;
    import cloud.wconnect.workperformance.service.hkinventory.IHkInventoryCategoryConfigService;
    import cloud.wconnect.workperformance.service.hkinventory.IHkInventoryItemConfigService;
    import cloud.wconnect.workperformance.service.hkinventory.IHkInventoryRoomTypeConfigService;
    import com.google.common.collect.ImmutableMap;
    import jakarta.validation.Valid;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

    import java.util.Collections;
    import java.util.Map;

    /**
     * 客房库存管理配置-item配置控制层
     *
     * @author lichen
     * @date 2025/4/22
     */
    @RestController
    @RequestMapping("/hk-inventory/item-config")
    public class HkInventoryItemConfigController {

        @Autowired
        private SessionContext sessionContext;
        @Autowired
        private IHkInventoryItemConfigService itemConfigService;
        @Autowired
        private IHkInventoryCategoryConfigService categoryConfigService;
        @Autowired
        private IHkInventoryRoomTypeConfigService roomTypeConfigService;
        @Autowired
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 查询item配置列表
         *
         * @param property                   资产标识
         * @param hkInventoryItemConfigQuery item查询对象
         * @return cloud.wconnect.commonlib.pojo.dto.ListPagedDTO<cloud.wconnect.workperformance.pojo.vo.hkinventory.HkInventoryItemConfigVO>
         * @author lichen
         * @date 2025/4/22
         */
        @ApiId(1425)
        @GetMapping
        public ListPagedDTO<HkInventoryItemConfigVO> list(String property, @Valid HkInventoryItemConfigQuery hkInventoryItemConfigQuery) {
            int propertyId = sessionContext.getPropertyId(property);
            return itemConfigService.pageByCategoryIdAndRoomTypeId(propertyId, hkInventoryItemConfigQuery);
        }

        /**
         * 保存item配置
         *
         * @param property                 资产标识
         * @param itemId                   item id
         * @param hkInventoryItemConfigDTO 保存的item配置DTO
         * @return java.util.Map<java.lang.String, java.lang.Integer>
         * @author lichen
         * @date 2025/4/22
         */
        @ApiId(1426)
        @PostMapping("/{itemId:\\d+}")
        public Map<String, Integer> save(String property, @PathVariable Integer itemId, @RequestBody @Valid HkInventoryItemConfigDTO hkInventoryItemConfigDTO) {
            int propertyId = sessionContext.getPropertyId(property);
            Integer tenantId = sessionContext.getTenantSymbolBO().getId();
            int loginUserId = sessionContext.getLoginUserId();
            if (itemId > 0) {
                //校验是否存在
                HkInventoryItemConfig oriItemConfig = itemConfigService.checkExist(itemId);
                //校验是否归属于资产
                itemConfigService.checkBelongProperty(oriItemConfig, propertyId);
                //校验不可修改的字段
                checkUnmodifiableFields(oriItemConfig, hkInventoryItemConfigDTO);
                //校验名称重复
                itemConfigService.checkNameRepeat(propertyId, hkInventoryItemConfigDTO.getItemName(), hkInventoryItemConfigDTO.getCategoryId(), hkInventoryItemConfigDTO.getRoomTypeId(), itemId);
                //修改
                itemConfigService.update(propertyId, oriItemConfig, hkInventoryItemConfigDTO, loginUserId);
                return ImmutableMap.of("id", itemId);
            } else {
                //校验房型是否存在
                HkInventoryRoomTypeConfig roomTypeConfig = roomTypeConfigService.checkExist(hkInventoryItemConfigDTO.getRoomTypeId());
                //校验房型是否归属于资产
                roomTypeConfigService.checkBelongProperty(roomTypeConfig, propertyId);

                //校验分类是否存在
                HkInventoryCategoryConfig categoryConfig = categoryConfigService.checkExist(hkInventoryItemConfigDTO.getCategoryId());
                //校验分类是否属于当前资产
                categoryConfigService.checkBelong(categoryConfig, propertyId);
                //校验分类配置是否包含指定房型
                categoryConfigService.checkContainRoomType(categoryConfig, hkInventoryItemConfigDTO.getRoomTypeId());

                //校验名称重复
                itemConfigService.checkNameRepeat(propertyId, hkInventoryItemConfigDTO.getItemName(), hkInventoryItemConfigDTO.getCategoryId(), hkInventoryItemConfigDTO.getRoomTypeId(), itemId);
                return ImmutableMap.of("id", itemConfigService.insert(tenantId, propertyId, hkInventoryItemConfigDTO, loginUserId));
            }
        }

        /**
         * 删除item配置
         *
         * @param property 资产标识
         * @param itemId   item id
         * @return java.util.Map<java.lang.String, java.lang.String>
         * @author lichen
         * @date 2025/4/22
         */
        @ApiId(1427)
        @DeleteMapping("/{itemId:\\d+}")
        public Map<String, String> delete(String property, @PathVariable Integer itemId) {
            int propertyId = sessionContext.getPropertyId(property);
            //校验是否存在
            HkInventoryItemConfig itemConfig = itemConfigService.checkExist(itemId);
            //校验是否归属于资产
            itemConfigService.checkBelongProperty(itemConfig, propertyId);
            //删除
            itemConfigService.delete(propertyId, itemConfig);
            return Collections.emptyMap();
        }

        /**
         * 校验不可修改的字段
         *
         * @param oriItemConfig            修改前的item config
         * @param hkInventoryItemConfigDTO 修改后的DTO
         * @return void
         * @author lichen
         * @date 2025/4/23
         */
        private void checkUnmodifiableFields(HkInventoryItemConfig oriItemConfig, HkInventoryItemConfigDTO hkInventoryItemConfigDTO) {
            //校验分类id不可修改
            if (!oriItemConfig.getCategoryId().equals(hkInventoryItemConfigDTO.getCategoryId())) {
                throw new BadRequestException(CommonErrorI18nConstant.CANNOT_MODIFIED,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("categoryId"),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "categoryId cannot be modified");
            }
            //校验房型id不可修改
            if (!oriItemConfig.getRoomTypeId().equals(hkInventoryItemConfigDTO.getRoomTypeId())) {
                throw new BadRequestException(CommonErrorI18nConstant.CANNOT_MODIFIED,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("roomTypeId"),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "roomTypeId cannot be modified");
            }
        }

    }