# EngineeringRequestLocationServiceImpl.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.EngineeringRequestArea;
    import cloud.wconnect.frontoffice.entity.engineeringrequest.EngineeringRequestItem;
    import cloud.wconnect.frontoffice.entity.engineeringrequest.EngineeringRequestLocation;
    import cloud.wconnect.frontoffice.mapper.engineeringrequest.EngineeringRequestLocationMapper;
    import cloud.wconnect.frontoffice.pojo.dto.engineeringrequest.LocationDTO;
    import cloud.wconnect.frontoffice.pojo.dto.engineeringrequest.SelectOptionQuery;
    import cloud.wconnect.frontoffice.service.engineeringrequest.IEngineeringRequestAreaService;
    import cloud.wconnect.frontoffice.service.engineeringrequest.IEngineeringRequestItemService;
    import cloud.wconnect.frontoffice.service.engineeringrequest.IEngineeringRequestLocationService;
    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 org.springframework.transaction.annotation.Transactional;

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

    /**
     * <p>
     *  服务实现类
     * </p>
     *
     * @author yyt
     * @since 2023-05-10
     */
    @Service
    @DS("logbook")
    public class EngineeringRequestLocationServiceImpl extends ServiceImpl<EngineeringRequestLocationMapper, EngineeringRequestLocation> implements IEngineeringRequestLocationService {

        @Autowired
        private IEngineeringRequestAreaService engineeringRequestAreaService;
        @Autowired
        private IEngineeringRequestItemService engineeringRequestItemService;

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

        /**
         * 获取 location 列表
         *
         * @param propertyId 资产
         * @return location 列表
         * @author yyt
         * @date 2023/5/12
         */
        @Override
        public List<EngineeringRequestLocation> listByPropertyId(Integer propertyId) {
            return list(new QueryWrapper<EngineeringRequestLocation>().lambda().eq(EngineeringRequestLocation::getPropertyId, propertyId));
        }

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

        /**
         * 删除
         *
         * @param location location
         * @author yyt
         * @date 2023/5/12
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void delete(EngineeringRequestLocation location) {
            removeById(location.getId());

            engineeringRequestAreaService.remove(new QueryWrapper<EngineeringRequestArea>().lambda()
                    .eq(EngineeringRequestArea::getPropertyId, location.getPropertyId())
                    .eq(EngineeringRequestArea::getLocationId, location.getId()));
            engineeringRequestItemService.remove(new QueryWrapper<EngineeringRequestItem>().lambda()
                    .eq(EngineeringRequestItem::getPropertyId, location.getPropertyId())
                    .eq(EngineeringRequestItem::getLocationId, location.getId()));
        }

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

        /**
         * 查询 location
         *
         * @param propertyId 资产 id
         * @param query      搜索条件
         * @return location
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public List<EngineeringRequestLocation> listByCondition(Integer propertyId, SelectOptionQuery query) {
            return list(new QueryWrapper<EngineeringRequestLocation>().lambda()
                    .eq(EngineeringRequestLocation::getPropertyId, propertyId)
                    .eq(StringUtils.isNotEmpty(query.getName()), EngineeringRequestLocation::getName, query.getName()));
        }

        /**
         * 保存  location
         *
         * @param propertyId  资产 id
         * @param locationDTO location
         * @param userId      操作人
         * @author yyt
         * @date 2026/1/19
         */
        @Override
        public void save(Integer propertyId, LocationDTO locationDTO, int userId) {
            save(new EngineeringRequestLocation().setPropertyId(propertyId)
                    .setName(locationDTO.getName())
                    .setUpdateBy(userId)
                    .setUpdateTime(DateTimeUtils.nowTimestamp()));
        }

        /**
         * 更新 location
         *
         * @param id          location id
         * @param locationDTO location
         * @param userId      操作人
         * @author yyt
         * @date 2026/1/19
         */
        @Override
        public void update(int id, LocationDTO locationDTO, int userId) {
            updateById(new EngineeringRequestLocation().setId(id)
                    .setName(locationDTO.getName())
                    .setUpdateBy(userId)
                    .setUpdateTime(DateTimeUtils.nowTimestamp()));
        }

    }