# LogbookCategoryServiceImpl.java

    package cloud.wconnect.frontoffice.service.logbook.impl;

    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.frontoffice.cons.logbook.LogbookCons;
    import cloud.wconnect.frontoffice.entity.logbook.Logbook;
    import cloud.wconnect.frontoffice.entity.logbook.LogbookCategory;
    import cloud.wconnect.frontoffice.entity.logbook.LogbookNotification;
    import cloud.wconnect.frontoffice.mapper.logbook.LogbookCategoryMapper;
    import cloud.wconnect.frontoffice.service.logbook.ILogbookCategoryService;
    import cloud.wconnect.frontoffice.service.logbook.ILogbookNotificationService;
    import cloud.wconnect.frontoffice.service.logbook.ILogbookService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import java.util.List;
    import java.util.Objects;

    /**
     * <p>
     *  服务实现类
     * </p>
     *
     * @author chenx
     * @since 2023-03-20
     */
    @Service
    @DS("logbook")
    public class LogbookCategoryServiceImpl extends ServiceImpl<LogbookCategoryMapper, LogbookCategory> implements ILogbookCategoryService {

        @Autowired
        private ILogbookService logbookService;
        @Autowired
        private ILogbookNotificationService logbookNotificationService;

        /**
         * 校验存在
         *
         * @param id 主键 id
         * @author yyt
         * @date 2023/3/21
         */
        @Override
        public LogbookCategory checkExist(Integer id) {
            LogbookCategory category = getById(id);
            if (category == null) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the logbookCategory is not found;id:" + id);
            }
            return category;
        }

        /**
         * 校验名称存在
         *
         * @param propertyId 资产 id
         * @param name       category name
         * @param id         排除自身
         * @author yyt
         * @date 2023/3/23
         */
        @Override
        public void checkExist(Integer propertyId, String name, Integer id) {
            LogbookCategory category = getOne(new QueryWrapper<LogbookCategory>().lambda()
                    .eq(LogbookCategory::getPropertyId, propertyId)
                    .eq(LogbookCategory::getName, name)
                    .ne(Objects.nonNull(id), LogbookCategory::getId, id));
            if (Objects.nonNull(category)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_EXISTED,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the logbookCategory is exist;propertyId:" + propertyId +
                        ",name:" + name);
            }
        }

        /**
         * 新增 category
         *
         * @param propertyId 资产 id
         * @param name       category name
         * @param userId     操作人
         * @author yyt
         * @date 2023/3/23
         */
        @Override
        public void saveCategory(int propertyId, String name, int userId) {
            save(new LogbookCategory().setPropertyId(propertyId).setName(name).setUpdateBy(userId)
                    .setUpdateTime(DateTimeUtils.nowTimestamp()));
        }

        /**
         * 修改 category
         *
         * @param id     category id
         * @param name   category name
         * @param userId 操作人
         * @author yyt
         * @date 2023/3/23
         */
        @Override
        public void updateCategory(int id, String name, int userId) {
            updateById(new LogbookCategory().setId(id).setName(name).setUpdateBy(userId).setUpdateTime(DateTimeUtils.nowTimestamp()));
        }

        /**
         * 删除 category
         *
         * @param id category id
         * @author yyt
         * @date 2023/3/23
         */
        @Override
        public void deleteCategory(int id) {
            // 校验 category 是否被 logbook 占用
            Logbook logbook = logbookService.getOne(new QueryWrapper<Logbook>().lambda().eq(Logbook::getCategory, id)
                    .select(Logbook::getId).last("limit 1"));
            if (Objects.nonNull(logbook)) {
                throw new BadRequestException(LogbookCons.LOGBOOK_CATEGORY_USED_BY_LOGBOOK,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "Sorry, the category is used by logbook;id:" + id);
            }

            // 校验 category 是否被 logbook notification 占用
            LogbookNotification logbookNotification = logbookNotificationService.getOne(new QueryWrapper<LogbookNotification>()
                    .lambda().eq(LogbookNotification::getCategory, id)
                    .select(LogbookNotification::getId).last("limit 1"));
            if (Objects.nonNull(logbookNotification)) {
                throw new BadRequestException(LogbookCons.LOGBOOK_CATEGORY_USED_BY_LOGBOOK_NOTIFICATION,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "Sorry, the category is used by logbook notification;id:" + id);
            }

            removeById(id);
        }

        /**
         * 查询 category
         *
         * @param propertyId 资产 id
         * @return category
         * @author yyt
         * @date 2025/1/15
         **/
        @Override
        public List<LogbookCategory> list(Integer propertyId) {
            return list(new QueryWrapper<LogbookCategory>().lambda().eq(LogbookCategory::getPropertyId, propertyId));
        }

    }