# LogbookCategoryController.java

    package cloud.wconnect.frontoffice.controller.logbook;

    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.frontoffice.entity.logbook.LogbookCategory;
    import cloud.wconnect.frontoffice.pojo.dto.logbook.LogbookCategoryDTO;
    import cloud.wconnect.frontoffice.service.logbook.ILogbookCategoryService;
    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;

    /**
     * 控制层：Logbook Category
     *
     * @author yyt
     * 2023/3/23
     */
    @RestController
    @RequestMapping("/logbook/category")
    public class LogbookCategoryController {

        @Autowired
        private ILogbookCategoryService logbookCategoryService;
        @Autowired
        private BeanMapper beanMapper;
        @Autowired
        private SessionContext sessionContext;

        /**
         * 获取 category 列表
         *
         * @param property 资产标识
         * @return category 列表
         * @author yyt
         * @date 2023/3/23
         */
        @ApiId(154)
        @GetMapping
        public List<LogbookCategoryDTO> listCategory(@RequestParam String property) {
            List<LogbookCategory> logbookCategories = logbookCategoryService.list(sessionContext.getPropertyId(property));
            return beanMapper.mapList(logbookCategories, LogbookCategoryDTO.class);
        }

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

                logbookCategoryService.saveCategory(propertyId, logbookCategoryDTO.getName(), sessionContext.getLoginUserId());
            } else {
                LogbookCategory logbookCategory = logbookCategoryService.checkExist(id);

                checkPropertySame(logbookCategory, propertyId);

                logbookCategoryService.checkExist(logbookCategory.getPropertyId(), logbookCategoryDTO.getName(), id);

                logbookCategoryService.updateCategory(id, logbookCategoryDTO.getName(), sessionContext.getLoginUserId());
            }

            return Collections.emptyMap();
        }

        /**
         * 校验 logbook category property 与 参数 property 一致
         *
         * @param logbookCategory logbook category
         * @param paramPropertyId 参数 property
         * @author yyt
         * @date 2023/3/23
         */
        private void checkPropertySame(LogbookCategory logbookCategory, int paramPropertyId) {
            if (logbookCategory.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 logbookCategory;propertyId:" + paramPropertyId + ",logbookCategoryPropertyId:" +
                        logbookCategory.getPropertyId() + ",logbookCategoryId:" + logbookCategory.getId());
            }
        }

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

            checkPropertySame(logbookCategory, sessionContext.getPropertyId(property));

            logbookCategoryService.deleteCategory(id);

            return Collections.emptyMap();
        }

    }