# ItInventoryController.java

    package cloud.wconnect.it.controller.itinventory;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseConstant;
    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.ListPagedDTO;
    import cloud.wconnect.it.entity.itinventory.ItInventory;
    import cloud.wconnect.it.pojo.dto.itinventory.ItInventoryDTO;
    import cloud.wconnect.it.pojo.query.itinventory.ItInventoryQuery;
    import cloud.wconnect.it.service.itinventory.IItInventoryService;
    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;

    /**
     * <p>
     * IT Inventory 前端控制器
     * </p>
     *
     * @author yyt
     * @since 2024-03-02
     */
    @RestController
    @RequestMapping("/it-inventory")
    public class ItInventoryController {

        @Autowired
        private IItInventoryService itInventoryService;
        @Autowired
        private SessionContext sessionContext;
        @Autowired
        private BeanMapper beanMapper;

        /**
         * IT Inventory 列表
         *
         * @param query 查询条件
         * @return IT Inventory 列表
         * @author yyt
         * @date 2024/3/2
         **/
        @ApiId(1030)
        @GetMapping
        public ListPagedDTO<ItInventoryDTO> list(String property, @Valid ItInventoryQuery query) {
            Integer propertyId = sessionContext.getPropertyId(property);
            return itInventoryService.listPage(propertyId, query);
        }

        /**
         * 新增、编辑 IT Inventory
         *
         * @param property 资产标识
         * @param id inventory id
         * @param itInventoryDTO inventory 信息
         * @return {}
         * @author yyt
         * @date 2024/3/2
         **/
        @ApiId(1031)
        @PostMapping("/{id:\\d+}")
        public Map save(String property, @PathVariable long id, @Valid @RequestBody ItInventoryDTO itInventoryDTO) {
            Integer propertyId = sessionContext.getPropertyId(property);
            int userId = sessionContext.getLoginUserId();

            if (id == BaseConstant.ZERO) {
                itInventoryService.save(propertyId, itInventoryDTO, userId);
            } else {
                ItInventory existItinventory = itInventoryService.checkExist(id);
                checkBelong(existItinventory, propertyId);
                checkNotDelete(existItinventory);

                itInventoryService.update(id, itInventoryDTO, userId);
            }
            return Collections.emptyMap();
        }

        /**
         * 删除 IT Inventory
         *
         * @param property 资产标识
         * @param id       inventory id
         * @return {}
         * @author yyt
         * @date 2024/3/2
         **/
        @ApiId(1032)
        @DeleteMapping("/{id:\\d+}")
        public Map delete(String property, @PathVariable long id) {
            Integer propertyId = sessionContext.getPropertyId(property);
            ItInventory existItinventory = itInventoryService.checkExist(id);
            checkBelong(existItinventory, propertyId);
            checkNotDelete(existItinventory);

            itInventoryService.delete(id);

            return Collections.emptyMap();
        }

        /**
         * 检查是否未删除
         *
         * @param itinventory it inventory
         * @author yyt
         * @date 2024/3/2
         **/
        private void checkNotDelete(ItInventory itinventory) {
            if (itinventory.getIsDelete()) {
                throw new BadRequestException(CommonErrorI18nConstant.DELETED_DATA_CANNOT_HANDLE,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "The deleted data does not support any operations");
            }
        }

        /**
         * 校验数据归属
         *
         * @param itInventory inventory
         * @param propertyId     资产 id
         * @author yyt
         * @date 2024/3/2
         */
        private void checkBelong(ItInventory itInventory, Integer propertyId) {
            if (!itInventory.getPropertyId().equals(propertyId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(), "the property id parameter is different from " +
                        "the property id of the itInventory;propertyId:" + propertyId + ",itInventoryPropertyId:" +
                        itInventory.getPropertyId() + ",itInventoryId:" + itInventory.getId());
            }
        }

    }