# ItticketCategoryServiceImpl.java

    package cloud.wconnect.it.service.itrequest.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.it.cons.itrequest.ItRequestConstant;
    import cloud.wconnect.it.entity.itrequest.Itticket;
    import cloud.wconnect.it.entity.itrequest.ItticketCategory;
    import cloud.wconnect.it.mapper.itrequest.ItticketCategoryMapper;
    import cloud.wconnect.it.service.itrequest.IItticketCategoryService;
    import cloud.wconnect.it.service.itrequest.IItticketService;
    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 yyt
     * @since 2023-03-25
     */
    @DS("it_manage")
    @Service
    public class ItticketCategoryServiceImpl extends ServiceImpl<ItticketCategoryMapper, ItticketCategory> implements IItticketCategoryService {

        @Autowired
        private IItticketService itticketService;

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

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

        /**
         * 新增 category
         *
         * @param propertyId 资产 id
         * @param name       category name
         * @param userId     操作人
         * @author yyt
         * @date 2023/3/28
         */
        @Override
        public void saveCategory(int propertyId, String name, Integer userId) {
            save(new ItticketCategory().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/28
         */
        @Override
        public void updateCategory(int id, String name, Integer userId) {
            updateById(new ItticketCategory().setId(id).setName(name).setUpdateBy(userId)
                    .setUpdateTime(DateTimeUtils.nowTimestamp()));
        }

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

            removeById(id);
        }

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

    }