# EngineeringRequestItemServiceImpl.java

    package cloud.wconnect.frontoffice.service.engineeringrequest.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.entity.engineeringrequest.EngineeringRequestItem;
    import cloud.wconnect.frontoffice.mapper.engineeringrequest.EngineeringRequestItemMapper;
    import cloud.wconnect.frontoffice.pojo.dto.engineeringrequest.ItemDTO;
    import cloud.wconnect.frontoffice.pojo.dto.engineeringrequest.SelectOptionQuery;
    import cloud.wconnect.frontoffice.service.engineeringrequest.IEngineeringRequestItemService;
    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.stereotype.Service;

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

    /**
     * <p>
     *  服务实现类
     * </p>
     *
     * @author yyt
     * @since 2023-05-10
     */
    @Service
    @DS("logbook")
    public class EngineeringRequestItemServiceImpl extends ServiceImpl<EngineeringRequestItemMapper, EngineeringRequestItem> implements IEngineeringRequestItemService {

        /**
         * 校验存在
         *
         * @param locationId location id
         * @param areaId     area id
         * @param id         itemId
         * @return item
         * @author yyt
         * @date 2023/5/11
         */
        @Override
        public EngineeringRequestItem checkExist(Integer locationId, Integer areaId, Integer id) {
            EngineeringRequestItem item = getOne(new QueryWrapper<EngineeringRequestItem>().lambda()
                    .eq(EngineeringRequestItem::getLocationId, locationId)
                    .eq(EngineeringRequestItem::getAreaId, areaId)
                    .eq(EngineeringRequestItem::getId, id));
            if (Objects.isNull(item)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the item is not found;id:" + id);
            }
            return item;
        }

        /**
         * 获取 item 列表
         *
         * @param propertyId 资产
         * @param locationId location id
         * @param areaId     area id
         * @return item 列表
         * @author yyt
         * @date 2023/5/12
         */
        @Override
        public List<EngineeringRequestItem> list(Integer propertyId, int locationId, int areaId) {
            return list(new QueryWrapper<EngineeringRequestItem>().lambda().eq(EngineeringRequestItem::getPropertyId, propertyId)
                    .eq(EngineeringRequestItem::getLocationId, locationId).eq(EngineeringRequestItem::getAreaId, areaId));
        }

        /**
         * 校验存在
         *
         * @param propertyId 资产
         * @param locationId location id
         * @param areaId     area id
         * @param name       名称
         * @param groupName  分组名称
         * @param id         排除自身
         * @author yyt
         * @date 2023/5/12
         */
        @Override
        public void checkExist(Integer propertyId, int locationId, int areaId, String groupName, String name, Integer id) {
            EngineeringRequestItem item = getOne(new QueryWrapper<EngineeringRequestItem>().lambda()
                    .eq(EngineeringRequestItem::getPropertyId, propertyId)
                    .eq(EngineeringRequestItem::getLocationId, locationId)
                    .eq(EngineeringRequestItem::getAreaId, areaId)
                    .eq(EngineeringRequestItem::getGroupName, groupName)
                    .eq(EngineeringRequestItem::getName, name)
                    .ne(Objects.nonNull(id), EngineeringRequestItem::getId, id));
            if (Objects.nonNull(item)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_EXISTED,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the item already exist;propertyId:" + propertyId +
                        ",locationId:" + locationId + ",areaId:" + areaId + ",groupName:" + groupName + ",name:" + name);
            }
        }

        /**
         * 删除
         *
         * @param item Engineering Request Item
         * @author yyt
         * @date 2023/5/12
         */
        @Override
        public void delete(EngineeringRequestItem item) {
            removeById(item.getId());
        }

        /**
         * 查询 item
         *
         * @param query 查询条件
         * @return item
         * @author yyt
         * @date 2024/2/8
         **/
        @Override
        public Page<EngineeringRequestItem> page(SelectOptionQuery query) {
            return baseMapper.selectPage(new Page<>(query.getPage(), query.getSize()), query);
        }

        /**
         * 更新 item 信息
         *
         * @param id      item id
         * @param itemDTO item 信息
         * @param userId  操作人
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public void update(int id, ItemDTO itemDTO, int userId) {
            updateById(new EngineeringRequestItem().setId(id)
                    .setGroupName(itemDTO.getGroupName())
                    .setName(itemDTO.getName())
                    .setUpdateBy(userId)
                    .setUpdateTime(DateTimeUtils.nowTimestamp()));
        }

        /**
         * 保存 item 信息
         *
         * @param propertyId 资产
         * @param lid        location id
         * @param aid        area id
         * @param itemDTO    item 信息
         * @param userId     操作人
         * @author yyt
         * @date 2026/1/19
         */
        @Override
        public void save(Integer propertyId, int lid, int aid, ItemDTO itemDTO, int userId) {
            save(new EngineeringRequestItem().setPropertyId(propertyId)
                    .setLocationId(lid)
                    .setAreaId(aid)
                    .setGroupName(itemDTO.getGroupName())
                    .setName(itemDTO.getName())
                    .setUpdateBy(userId)
                    .setUpdateTime(DateTimeUtils.nowTimestamp()));
        }

        /**
         * 根据指定条件查询 item
         *
         * @param propertyId 资产 id
         * @param lid        location id
         * @param aid        area id
         * @param query      查询条件
         * @return item
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public List<EngineeringRequestItem> listByCondition(Integer propertyId, int lid, int aid, SelectOptionQuery query) {
            return list(new QueryWrapper<EngineeringRequestItem>().lambda()
                    .eq(EngineeringRequestItem::getPropertyId, propertyId)
                    .eq(EngineeringRequestItem::getLocationId, lid)
                    .eq(EngineeringRequestItem::getAreaId, aid)
                    .eq(StringUtils.isNotEmpty(query.getGroupName()), EngineeringRequestItem::getGroupName, query.getGroupName())
                    .eq(StringUtils.isNotEmpty(query.getName()), EngineeringRequestItem::getName, query.getName()));
        }

    }