# EngineeringRequestAreaServiceImpl.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.mapper.engineeringrequest.EngineeringRequestAreaMapper;
    import cloud.wconnect.frontoffice.pojo.dto.engineeringrequest.AreaDTO;
    import cloud.wconnect.frontoffice.pojo.dto.engineeringrequest.SelectOptionQuery;
    import cloud.wconnect.frontoffice.service.engineeringrequest.IEngineeringRequestAreaService;
    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 jakarta.annotation.Nullable;
    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-05-10
     */
    @Service
    @DS("logbook")
    public class EngineeringRequestAreaServiceImpl extends ServiceImpl<EngineeringRequestAreaMapper, EngineeringRequestArea> implements IEngineeringRequestAreaService {

        @Autowired
        private IEngineeringRequestItemService engineeringRequestItemService;

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

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

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

        /**
         * 删除
         *
         * @param area Engineering Request Area
         * @author yyt
         * @date 2023/5/12
         */
        @Override
        public void delete(EngineeringRequestArea area) {
            removeById(area.getId());

            engineeringRequestItemService.remove(new QueryWrapper<EngineeringRequestItem>().lambda()
                    .eq(EngineeringRequestItem::getPropertyId, area.getPropertyId())
                    .eq(EngineeringRequestItem::getAreaId, area.getId()));
        }

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

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

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

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

    }