/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.guosen.region.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.C;
import org.springblade.modules.guosen.region.entity.BussinessRegion;
import org.springblade.modules.guosen.region.vo.CityVO;
import org.springblade.modules.guosen.region.vo.DistrictVO;
import org.springblade.modules.guosen.region.vo.ProvinceVO;
import org.springblade.modules.guosen.region.vo.BussinessRegionVO;
import org.springblade.modules.guosen.region.mapper.BussinessRegionMapper;
import org.springblade.modules.guosen.region.service.IBussinessRegionService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;
import java.util.stream.Collectors;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2021-09-23
 */
@Service
public class BussinessRegionServiceImpl extends ServiceImpl<BussinessRegionMapper, BussinessRegion> implements IBussinessRegionService {

	@Override
	public IPage<BussinessRegionVO> selectRegionPage(IPage<BussinessRegionVO> page, BussinessRegionVO region) {
		return page.setRecords(baseMapper.selectRegionPage(page, region));
	}

	@Override
	public List<ProvinceVO> getProvinceList() {
		List<BussinessRegion> list = this.list();
		List<ProvinceVO> provinceVOList = list.stream().map(region -> {
			ProvinceVO provinceVO = new ProvinceVO();
			provinceVO.setProvinceCode(region.getProvinceCode());
			provinceVO.setProvinceName(region.getProvinceName());
			return provinceVO;
		}).distinct().collect(Collectors.toList());
		return provinceVOList;
	}

	@Override
	public List<CityVO> getCityList(String provinceCode) {
		List<BussinessRegion> list = this.list();
		List<CityVO> cityVOList = list.stream().filter(region -> region.getProvinceCode().equals(provinceCode)).map(region -> {
			CityVO cityVO = new CityVO();
			cityVO.setCityCode(region.getCityCode());
			cityVO.setCityName(region.getCityName());
			return cityVO;
		}).distinct().collect(Collectors.toList());
		return cityVOList;
	}

	@Override
	public List<DistrictVO> getDistrictList(String cityCode) {
		List<BussinessRegion> list = this.list();
		List<DistrictVO> districtVOList = list.stream().filter(region -> region.getCityCode().equals(cityCode)).map(region -> {
			DistrictVO districtVO = new DistrictVO();
			districtVO.setDistrictCode(region.getDistrictCode());
			districtVO.setDistrictName(region.getDistrictName());
			return districtVO;
		}).distinct().collect(Collectors.toList());
		return districtVOList;
	}

}
