# ItticketPriorityServiceImpl.java

    package cloud.wconnect.it.service.itrequest.impl;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.it.entity.itrequest.ItticketPriority;
    import cloud.wconnect.it.mapper.itrequest.ItticketPriorityMapper;
    import cloud.wconnect.it.pojo.dto.itrequest.ItticketPriorityDTO;
    import cloud.wconnect.it.pojo.query.itrequest.PriorityQuery;
    import cloud.wconnect.it.service.itrequest.IItticketPriorityService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.apache.commons.lang3.StringUtils;
    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-10-10
     */
    @Service
    @DS("it_manage")
    public class ItticketPriorityServiceImpl extends ServiceImpl<ItticketPriorityMapper, ItticketPriority> implements IItticketPriorityService {

        @Autowired
        private I18nMessageUtils i18nMessageUtils;
        @Autowired
        private BeanMapper beanMapper;

        /**
         * 校验重复
         *
         * @param propertyId 资产 id
         * @param name       名称
         * @param id         排除自身
         * @author yyt
         * @date 2023/10/10
         */
        @Override
        public void checkRepeat(Integer propertyId, String name, Integer id) {
            long count = count(new QueryWrapper<ItticketPriority>()
                    .lambda().eq(ItticketPriority::getPropertyId, propertyId)
                    .eq(ItticketPriority::getName, name)
                    .ne(Objects.nonNull(id), ItticketPriority::getId, id));
            if (count > 0) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_EXISTED,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "Failed to save existing data");
            }
        }

        /**
         * 校验存在
         *
         * @param id 数据 id
         * @return Priority
         * @author yyt
         * @date 2023/10/10
         */
        @Override
        public ItticketPriority checkExist(int id) {
            ItticketPriority priority = getById(id);
            if (Objects.isNull(priority)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the itticketPriority is not found");
            }
            return priority;
        }

        /**
         * 分页查询 priority
         *
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return priority
         * @author yyt
         * @date 2025/1/15
         **/
        @Override
        public Page<ItticketPriority> listPage(Integer propertyId, PriorityQuery query) {
            return page(new Page<>(query.getPage(), query.getSize()), new QueryWrapper<ItticketPriority>().lambda()
                    .eq(ItticketPriority::getPropertyId, propertyId)
                    .nested(StringUtils.isNotEmpty(query.getKeyWord()),
                            sql ->
                                    sql.like(ItticketPriority::getName, query.getKeyWord())
                                            .or().like(ItticketPriority::getShortDescription, query.getKeyWord())
                                            .or().like(ItticketPriority::getLongDescription, query.getKeyWord()))
                    .orderByAsc(ItticketPriority::getName));
        }

        /**
         * 查询 priority
         *
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return priority
         * @author yyt
         * @date 2025/1/15
         **/
        @Override
        public List<ItticketPriority> list(Integer propertyId, PriorityQuery query) {
            return list(new QueryWrapper<ItticketPriority>().lambda()
                    .eq(ItticketPriority::getPropertyId, propertyId)
                    .eq(ItticketPriority::getName, query.getKeyWord()));
        }

        /**
         * 删除 priority
         *
         * @param id priority id
         * @author yyt
         * @date 2025/1/15
         **/
        @Override
        public void delete(int id) {
            removeById(id);
        }

        /**
         * 获取 priority 列表
         *
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return priority
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public ListPagedDTO<ItticketPriorityDTO> listPriorityDTO(int propertyId, PriorityQuery query) {
            long total;
            List<ItticketPriority> list;
            if (Objects.nonNull(query.getPage()) && Objects.nonNull(query.getSize())) {
                Page<ItticketPriority> page = listPage(propertyId, query);
                total = page.getTotal();
                list = page.getRecords();
            } else {
                if (StringUtils.isEmpty(query.getKeyWord())) {
                    throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                            i18nMessageUtils.getErrorParamMapNotNeedTranslate("keyWord"),
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the keyWord must not be empty");
                }
                list = list(propertyId, query);
                total = list.size();
            }

            return new ListPagedDTO<>(total, beanMapper.mapList(list, ItticketPriorityDTO.class));
        }

        /**
         * 保存 priority
         *
         * @param propertyId  资产 id
         * @param priorityDTO priority 信息
         * @param userId      操作人
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public void save(int propertyId, ItticketPriorityDTO priorityDTO, int userId) {
            save(new ItticketPriority().setPropertyId(propertyId)
                    .setName(priorityDTO.getName())
                    .setShortDescription(priorityDTO.getShortDescription())
                    .setLongDescription(priorityDTO.getLongDescription())
                    .setUpdateBy(userId)
                    .setUpdateTime(DateTimeUtils.nowTimestamp()));
        }

        /**
         * 更新 priority
         *
         * @param id          priority id
         * @param priorityDTO priority 信息
         * @param userId      操作人
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public void update(int id, ItticketPriorityDTO priorityDTO, int userId) {
            updateById(new ItticketPriority().setId(id)
                    .setName(priorityDTO.getName())
                    .setShortDescription(priorityDTO.getShortDescription())
                    .setLongDescription(priorityDTO.getLongDescription())
                    .setUpdateBy(userId)
                    .setUpdateTime(DateTimeUtils.nowTimestamp()));
        }

    }