package com.yc.ship.module.ota.service.otaDistributorRegion;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.yc.ship.module.ota.controller.admin.otaDistributorRegion.vo.*;
import com.yc.ship.module.ota.dal.dataobject.otaDistributorRegion.OtaDistributorRegionDO;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.util.object.BeanUtils;

import com.yc.ship.module.ota.dal.mysql.otaDistributorRegion.OtaDistributorRegionMapper;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.ota.enums.ErrorCodeConstants.*;

/**
 * 旅行社与代理区域关联 Service 实现类
 *
 * @author lsq
 */
@Service
@Validated
public class OtaDistributorRegionServiceImpl implements OtaDistributorRegionService {

    @Resource
    private OtaDistributorRegionMapper distributorRegionMapper;

    @Override
    public Long createDistributorRegion(OtaDistributorRegionSaveReqVO createReqVO) {
        // 插入
        OtaDistributorRegionDO distributorRegion = BeanUtils.toBean(createReqVO, OtaDistributorRegionDO.class);
        distributorRegionMapper.insert(distributorRegion);
        // 返回
        return distributorRegion.getId();
    }

    @Override
    public void updateDistributorRegion(OtaDistributorRegionSaveReqVO updateReqVO) {
        // 校验存在
        validateDistributorRegionExists(updateReqVO.getId());
        // 更新
        OtaDistributorRegionDO updateObj = BeanUtils.toBean(updateReqVO, OtaDistributorRegionDO.class);
        distributorRegionMapper.updateById(updateObj);
    }

    @Override
    public void deleteDistributorRegion(String id) {
        // 校验存在
        validateDistributorRegionExists(id);
        // 删除
        distributorRegionMapper.deleteById(id);
    }

    private void validateDistributorRegionExists(String id) {
        if (distributorRegionMapper.selectById(id) == null) {
            throw exception(500,"不存在");
        }
    }

    @Override
    public OtaDistributorRegionDO getDistributorRegion(String id) {
        return distributorRegionMapper.selectById(id);
    }

    @Override
    public PageResult<OtaDistributorRegionDO> getDistributorRegionPage(OtaDistributorRegionPageReqVO pageReqVO) {
        return distributorRegionMapper.selectPage(pageReqVO);
    }

}