# TaAirlineServiceImpl.java

    package cloud.wconnect.finance.service.ta.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.exception.ForbiddenException;
    import cloud.wconnect.finance.entity.ta.TaAirline;
    import cloud.wconnect.finance.mapper.ta.TaAirlineMapper;
    import cloud.wconnect.finance.pojo.dto.ta.TaAirlineDTO;
    import cloud.wconnect.finance.service.ta.ITaAirlineService;
    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.stereotype.Service;

    import jakarta.annotation.Resource;
    import java.text.MessageFormat;
    import java.util.List;
    import java.util.Objects;

    /**
     * <p>
     * 航空公司数据表 服务实现类
     * </p>
     *
     * @author zc
     * @since 2023-07-07
     */
    @Service
    @DS("finance")
    public class TaAirlineServiceImpl extends ServiceImpl<TaAirlineMapper, TaAirline> implements ITaAirlineService {
        @Resource
        private BeanMapper beanMapper;

        /**
         * 获取 Airline 集合
         *
         * @param propertyId 资产 id
         * @return TaAirlineDTO 集合
         * @author zc
         * @date 2023/7/8
         */
        @Override
        public List<TaAirlineDTO> list(int propertyId) {
            List<TaAirline> airlineList = list(new QueryWrapper<TaAirline>().lambda()
                    .eq(TaAirline::getPropertyId, propertyId)
                    .orderByAsc(TaAirline::getAirline));

            return beanMapper.mapList(airlineList, TaAirlineDTO.class);
        }

        /**
         * 新增 Airline
         *
         * @param propertyId   资产 id
         * @param taAirlineDTO Airline 数据
         * @return id
         * @author zc
         * @date 2023/7/8
         */
        @Override
        public int insert(int propertyId, TaAirlineDTO taAirlineDTO) {
            // 校验指定 Airline 数据是否已存在
            checkExistByAirline(propertyId, taAirlineDTO);

            TaAirline taAirline = beanMapper.map(taAirlineDTO, TaAirline.class,
                    dto -> dto.setPropertyId(propertyId));

            save(taAirline);

            return taAirline.getId();
        }

        /**
         * 更新 Airline
         *
         * @param propertyId      资产 id
         * @param originTaAirline 原 Airline 数据
         * @param taAirlineDTO    Airline 数据
         * @author zc
         * @date 2023/7/8
         */
        @Override
        public void update(int propertyId, TaAirline originTaAirline, TaAirlineDTO taAirlineDTO) {
            // 校验指定 Airline 数据是否已存在
            checkExistByAirline(propertyId, taAirlineDTO);

            updateById(TaAirline.builder()
                    .id(taAirlineDTO.getId())
                    .airline(taAirlineDTO.getAirline())
                    .build());
        }

        /**
         * 校验数据是否存在
         *
         * @param id Airline 数据 id
         * @return TaAirline
         * @author zc
         * @date 2023/7/8
         */
        @Override
        public TaAirline checkExist(int id) {
            TaAirline taAirline = getById(id);
            if (Objects.isNull(taAirline)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The taAirline is not found; taAirlineId: " + id);
            }
            return taAirline;
        }

        /**
         * 校验数据归属
         *
         * @param propertyId 资产 id
         * @param taAirline  Airline 数据
         * @author zc
         * @date 2023/7/8
         */
        @Override
        public void checkBelong(int propertyId, TaAirline taAirline) {
            if (!taAirline.getPropertyId().equals(propertyId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        MessageFormat.format("The taAirline does not belong to this property; propertyId:{0}; taAirlineId:{1}",
                                propertyId, taAirline.getId()));
            }
        }

        /**
         * 校验指定 Airline 数据是否已存在
         *
         * @param propertyId   资产 id
         * @param taAirlineDTO Airline 数据
         * @author zc
         * @date 2023/7/8
         */
        private void checkExistByAirline(int propertyId, TaAirlineDTO taAirlineDTO) {
            long count = count(new QueryWrapper<TaAirline>().lambda()
                    .eq(TaAirline::getPropertyId, propertyId)
                    .ne(Objects.nonNull(taAirlineDTO.getId()), TaAirline::getId, taAirlineDTO.getId())
                    .eq(TaAirline::getAirline, taAirlineDTO.getAirline()));
            if (count > 0) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_EXISTED,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The airline has existed in current property; airline: " + taAirlineDTO.getAirline());
            }
        }
    }