# ItticketCategoryController.java

    package cloud.wconnect.it.controller.itrequest;

    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.exception.ForbiddenException;
    import cloud.wconnect.it.entity.itrequest.ItticketCategory;
    import cloud.wconnect.it.pojo.dto.itrequest.ItticketCategoryDTO;
    import cloud.wconnect.it.service.itrequest.IItticketCategoryService;
    import jakarta.validation.Valid;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

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

    /**
     * <p>
     * 控制层: IT Request Category
     * </p>
     *
     * @author yyt
     * @since 2023-03-25
     */
    @RestController
    @RequestMapping("/it-request/category")
    public class ItticketCategoryController {

        @Autowired
        private IItticketCategoryService itticketCategoryService;
        @Autowired
        private BeanMapper beanMapper;
        @Autowired
        private SessionContext sessionContext;

        /**
         * 获取 category 列表
         *
         * @param property 资产标识
         * @return category 列表
         * @author yyt
         * @date 2023/3/28
         */
        @ApiId(179)
        @GetMapping
        public List<ItticketCategoryDTO> listCategory(@RequestParam String property) {
            List<ItticketCategory> categories = itticketCategoryService.list(sessionContext.getPropertyId(property));
            return beanMapper.mapList(categories, ItticketCategoryDTO.class);
        }

        /**
         * 新增/编辑 category
         *
         * @param id          category id
         * @param property    资产标识
         * @param categoryDTO category 信息
         * @return {}
         * @author yyt
         * @date 2023/3/28
         */
        @ApiId(180)
        @PostMapping("/{id:\\d+}")
        public Map saveCategory(@PathVariable("id") int id, @RequestParam String property,
                                @Valid @RequestBody ItticketCategoryDTO categoryDTO) {
            int propertyId = sessionContext.getPropertyId(property);
            if (id == 0) {
                itticketCategoryService.checkExist(propertyId, categoryDTO.getName(), null);

                itticketCategoryService.saveCategory(propertyId, categoryDTO.getName(), sessionContext.getLoginUserId());
            } else {
                ItticketCategory category = itticketCategoryService.checkExist(id);

                checkPropertySame(category, propertyId);

                itticketCategoryService.checkExist(category.getPropertyId(), categoryDTO.getName(), id);

                itticketCategoryService.updateCategory(id, categoryDTO.getName(), sessionContext.getLoginUserId());
            }

            return Collections.emptyMap();
        }

        /**
         * 校验 itticket category property 与 参数 property 一致
         *
         * @param category        itticket category
         * @param paramPropertyId 参数 property
         * @author yyt
         * @date 2023/3/28
         */
        private void checkPropertySame(ItticketCategory category, int paramPropertyId) {
            if (category.getPropertyId() != paramPropertyId) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(), "the property id parameter is different from " +
                        "the property id of the itticketCategory;propertyId:" + paramPropertyId + ",itticketCategoryPropertyId:" +
                        category.getPropertyId() + ",itticketCategoryId:" + category.getId());
            }
        }

        /**
         * 删除 category
         *
         * @param id       category id
         * @param property 资产标识
         * @return {}
         * @author yyt
         * @date 2023/3/28
         */
        @ApiId(181)
        @DeleteMapping("/{id:\\d+}")
        public Map deleteCategory(@PathVariable("id") int id, @RequestParam String property) {
            ItticketCategory category = itticketCategoryService.checkExist(id);

            int propertyId = sessionContext.getPropertyId(property);
            checkPropertySame(category, propertyId);

            itticketCategoryService.deleteCategory(id);

            return Collections.emptyMap();
        }

    }