package cn.iocoder.yudao.module.portal.service.city;

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 cn.iocoder.yudao.module.portal.controller.admin.city.vo.*;
import cn.iocoder.yudao.module.portal.dal.dataobject.city.CityDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.portal.dal.mysql.city.CityMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.portal.enums.ErrorCodeConstants.*;

/**
 * 城市 Service 实现类
 *
 * @author 篮智科技
 */
@Service
@Validated
public class CityServiceImpl implements CityService {

    @Resource
    private CityMapper cityMapper;

    @Override
    public Long createCity(CitySaveReqVO createReqVO) {
        // 插入
        CityDO city = BeanUtils.toBean(createReqVO, CityDO.class);
        cityMapper.insert(city);
        // 返回
        return city.getId();
    }

    @Override
    public void updateCity(CitySaveReqVO updateReqVO) {
        // 校验存在
        validateCityExists(updateReqVO.getId());
        // 更新
        CityDO updateObj = BeanUtils.toBean(updateReqVO, CityDO.class);
        cityMapper.updateById(updateObj);
    }

    @Override
    public void deleteCity(Long id) {
        // 校验存在
        validateCityExists(id);
        // 删除
        cityMapper.deleteById(id);
    }

    private void validateCityExists(Long id) {
        if (cityMapper.selectById(id) == null) {
            throw exception(CITY_NOT_EXISTS);
        }
    }

    @Override
    public CityDO getCity(Long id) {
        return cityMapper.selectById(id);
    }

    @Override
    public PageResult<CityDO> getCityPage(CityPageReqVO pageReqVO) {
        return cityMapper.selectPage(pageReqVO);
    }

}