# ItticketPriorityController.java

    package cloud.wconnect.it.controller.itrequest;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.exception.ForbiddenException;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.it.entity.itrequest.ItticketPriority;
    import cloud.wconnect.it.pojo.dto.itrequest.ItticketPriorityDTO;
    import cloud.wconnect.it.pojo.query.itrequest.PriorityQuery;
    import cloud.wconnect.it.service.itrequest.IItticketPriorityService;
    import cloud.wconnect.it.service.itrequest.IItticketService;
    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>
     *  前端控制器
     * </p>
     *
     * @author yyt
     * @since 2023-10-10
     */
    @RestController
    @RequestMapping("/it-request/priority")
    public class ItticketPriorityController {

        @Autowired
        private IItticketPriorityService itticketPriorityService;
        @Autowired
        private IItticketService itticketService;
        @Autowired
        private SessionContext sessionContext;

        /**
         * Priority 列表
         *
         * @param property 资产标识
         * @param query    搜索条件
         * @return Priority 列表
         * @author yyt
         * @date 2023/10/10
         */
        @ApiId(823)
        @GetMapping
        public ListPagedDTO<ItticketPriorityDTO> listPriority(@RequestParam String property, @Valid PriorityQuery query) {
            int propertyId = sessionContext.getPropertyId(property);
            return itticketPriorityService.listPriorityDTO(propertyId, query);
        }

        /**
         * 新增、编辑 Priority
         *
         * @param id          category id
         * @param property    资产标识
         * @param priorityDTO Priority 信息
         * @return {}
         * @author yyt
         * @date 2023/10/10
         */
        @ApiId(824)
        @PostMapping("/{id:\\d+}")
        public Map savePriority(@PathVariable("id") int id, @RequestParam String property,
                                @Valid @RequestBody ItticketPriorityDTO priorityDTO) {
            int propertyId = sessionContext.getPropertyId(property);
            int userId = sessionContext.getLoginUserId();
            if (id == 0) {
                itticketPriorityService.checkRepeat(propertyId, priorityDTO.getName(), null);

                itticketPriorityService.save(propertyId, priorityDTO, userId);
            } else {
                ItticketPriority priority = itticketPriorityService.checkExist(id);

                checkPropertySame(priority, propertyId);

                itticketPriorityService.checkRepeat(priority.getPropertyId(), priorityDTO.getName(), id);

                itticketPriorityService.update(id, priorityDTO, userId);
            }

            return Collections.emptyMap();
        }

        /**
         * 校验 itticket priority property 与 参数 property 一致
         *
         * @param priority        itticket priority
         * @param paramPropertyId 参数 property
         * @author yyt
         * @date 2023/10/10
         */
        private void checkPropertySame(ItticketPriority priority, int paramPropertyId) {
            if (priority.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 itticketPriority;propertyId:" + paramPropertyId + ",itticketPriorityPropertyId:" +
                        priority.getPropertyId() + ",itticketPriorityId:" + priority.getId());
            }
        }

        /**
         * 删除 Priority
         *
         * @param id       category id
         * @param property 资产标识
         * @return {}
         * @author yyt
         * @date 2023/10/10
         */
        @ApiId(825)
        @DeleteMapping("/{id:\\d+}")
        public Map deletePriority(@PathVariable("id") int id, @RequestParam String property) {
            ItticketPriority priority = itticketPriorityService.checkExist(id);
            int propertyId = sessionContext.getPropertyId(property);
            checkPropertySame(priority, propertyId);

            // 检查是否被占用
            itticketService.checkPriorityUsed(id);

            itticketPriorityService.delete(id);

            return Collections.emptyMap();
        }

    }