/*
 *      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.controller;

import io.swagger.annotations.*;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
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.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.guosen.region.entity.BussinessRegion;
import org.springblade.modules.guosen.region.vo.BussinessRegionVO;
import org.springblade.modules.guosen.region.wrapper.BussinessRegionWrapper;
import org.springblade.modules.guosen.region.service.IBussinessRegionService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.List;

/**
 *  控制器
 *
 * @author BladeX
 * @since 2021-09-23
 */
@RestController
@AllArgsConstructor
@RequestMapping("cyzh-region/region")
@Api(value = "", tags = "接口")
public class BussinessRegionController extends BladeController {

	private final IBussinessRegionService regionService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入region")
	public R<BussinessRegionVO> detail(BussinessRegion region) {
		BussinessRegion detail = regionService.getOne(Condition.getQueryWrapper(region));
		return R.data(BussinessRegionWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入region")
	public R<IPage<BussinessRegionVO>> list(BussinessRegion region, Query query) {
		IPage<BussinessRegion> pages = regionService.page(Condition.getPage(query), Condition.getQueryWrapper(region));
		return R.data(BussinessRegionWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入region")
	public R<IPage<BussinessRegionVO>> page(BussinessRegionVO region, Query query) {
		IPage<BussinessRegionVO> pages = regionService.selectRegionPage(Condition.getPage(query), region);
		return R.data(pages);
	}

	/**
	 * 新增 
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入region")
	public R save(@Valid @RequestBody BussinessRegion region) {
		return R.status(regionService.save(region));
	}

	/**
	 * 修改 
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入region")
	public R update(@Valid @RequestBody BussinessRegion region) {
		return R.status(regionService.updateById(region));
	}

	/**
	 * 新增或修改 
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入region")
	public R submit(@Valid @RequestBody BussinessRegion region) {
		return R.status(regionService.saveOrUpdate(region));
	}


	/**
	 * 获取省列表
	 */
	@GetMapping("/getProvinceList")
	@ApiOperationSupport(order = 8)
	public R<List<ProvinceVO>> getProvinceList() {
		List<ProvinceVO> provinceList = regionService.getProvinceList();
		return R.data(provinceList);
	}

	/**
	 * 获取市列表
	 */
	@GetMapping("/getCityList")
	@ApiOperationSupport(order = 9)
	public R<List<CityVO>> getCityList(@RequestParam(value = "provinceCode") String provinceCode) {
		List<CityVO> cityList = regionService.getCityList(provinceCode);
		return R.data(cityList);
	}

	/**
	 * 获取市列表
	 */
	@GetMapping("/getDistrictList")
	@ApiOperationSupport(order = 10)
	public R<List<DistrictVO>> getDistrictList(@RequestParam(value = "cityCode") String cityCode) {
		List<DistrictVO> districtList = regionService.getDistrictList(cityCode);
		return R.data(districtList);
	}

	
}
