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

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.otaRegion.vo.*;
import com.yc.ship.module.ota.dal.dataobject.otaRegion.OtaRegionDO;
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.otaRegion.OtaRegionMapper;

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 OtaRegionServiceImpl implements OtaRegionService {

    @Resource
    private OtaRegionMapper regionMapper;

    @Override
    public String createRegion(OtaRegionSaveReqVO createReqVO) {
        // 插入
        OtaRegionDO region = BeanUtils.toBean(createReqVO, OtaRegionDO.class);
        regionMapper.insert(region);
        // 返回
        return region.getId();
    }

    @Override
    public void updateRegion(OtaRegionSaveReqVO updateReqVO) {
        // 校验存在
        validateRegionExists(updateReqVO.getId());
        // 更新
        OtaRegionDO updateObj = BeanUtils.toBean(updateReqVO, OtaRegionDO.class);
        regionMapper.updateById(updateObj);
    }

    @Override
    public void deleteRegion(String id) {
        // 校验存在
        validateRegionExists(id);
        // 删除
        regionMapper.deleteById(id);
    }

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

    @Override
    public OtaRegionDO getRegion(String id) {
        return regionMapper.selectById(id);
    }

    @Override
    public PageResult<OtaRegionDO> getRegionPage(OtaRegionPageReqVO pageReqVO) {
        return regionMapper.selectPage(pageReqVO);
    }

}