package com.nantian.cloud.jljc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.DictRegion;
import com.nantian.cloud.jljc.service.DictRegionService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.security.annotation.Inner;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


/**
 * 行政区编码字典
 *
 * @author
 * @date 2020-08-13
 */
@Slf4j
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/dictRegion")
@Api(value="dictRegion",tags = "行政区编码字典")
public class DictRegionController {
	private final DictRegionService dictRegionService;


	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param dictRegion 字典对象
	 * @return
	 */
	@GetMapping("/page")
	@ApiOperation(value = "分页获取行政区编码字典", notes = "分页获取行政区编码字典")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
			@ApiImplicitParam(name = "dictRegion", value = "行政区编码字典对象", required = false, dataType = "json", paramType = "path")
	})
	public R getDictRegionPage(Page page, DictRegion dictRegion) {
		dictRegion.setDelFlag("1");  //筛选未删除项
		return R.ok(dictRegionService.page(page, Wrappers.query(dictRegion)));
	}


	/**
	 * 信息
	 *
	 * @param dictRegionId
	 * @return R
	 */
	@GetMapping("/{dictRegionId}")
	@ApiOperation(value = "根据id获取行政区编码字典", notes = "根据id获取行政区编码字典")
	@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
	public R getById(@PathVariable("dictRegionId") String dictRegionId) {
		return R.ok(dictRegionService.getById(dictRegionId));
	}

	/**
	 * 获取树形结构
	 * @return
	 */
	@GetMapping("/tree")
	@ApiOperation(value = "获取行政区划树形结构", notes = "获取行政区划树形结构")
	public R getDictRegionTree() {
		return R.ok(dictRegionService.getDictRegionTreee());
	}

	/**
	 * 系统外获取树形结构
	 * @return
	 */
	@Inner(value = false)
	@GetMapping("/treeOut")
	@ApiOperation(value = "系统外获取行政区划树形结构", notes = "系统外获取行政区划树形结构")
	public R getDictRegionTreeOut() {
		return R.ok(dictRegionService.getDictRegionTreee());
	}

	/**
	 * 获取树形结构（数据权限）
	 * @return
	 */
	@GetMapping("/treeScope")
	@ApiOperation(value = "获取行政区划树形结构（数据权限）", notes = "获取行政区划树形结构（数据权限）")
	public R getDictRegionTreeScope(String regionCode,String version) {
		return R.ok(dictRegionService.getDictRegionTreeeScope(regionCode,version));
	}

	/**
	 * 获取树形结构（数据权限，名称显示为地区名称_地区编码）
	 */
	@GetMapping("/treeDataScope")
	@ApiOperation(value = "获取行政区划树形结构（数据权限）", notes = "获取行政区划树形结构（数据权限）")
	public R getDictRegionTreeeDataScope(String regionCode,String version) {
		return R.ok(dictRegionService.getDictRegionTreeDataScope(regionCode,version));
	}

	/**
	 * 获取州市列表
	 */
	@GetMapping("/getCity")
	@ApiOperation(value = "获取州市列表", notes = "获取州市列表")
	public R getCity() {
		return R.ok(dictRegionService.getCityRegion());
	}

	/**
	 * 获取区县级编码
	 */
	@GetMapping("/getCountyCode")
	@ApiOperation(value = "获取区县级编码", notes = "获取区县级编码")
	public R getCountyCode(String name) {
		QueryWrapper<DictRegion> q = new QueryWrapper<>();
		q.like(StringUtils.isNotEmpty(name),"id",name);
		q.ne("right(pid,4)","0000");
		return R.ok(dictRegionService.getOne(q));
	}

	/**
	 * 保存
	 *
	 * @param dictRegion
	 * @return R
	 */
	@PostMapping
	@SysLog("新增行政区编码字典")
	@ApiOperation(value = "新增行政区编码字典", notes = "新增行政区编码字典")
	@ApiImplicitParam(name = "dictRegion", value = "行政区编码字典对象", required = true, dataType = "json", paramType = "path")
	public R save(@RequestBody DictRegion dictRegion) {
		return R.ok(dictRegionService.save(dictRegion));
	}

	/**
	 * 修改
	 *
	 * @param dictRegion
	 * @return R
	 */
	@PutMapping
	@SysLog("修改行政区编码字典")
	@ApiOperation(value = "修改行政区编码字典", notes = "修改行政区编码字典")
	@ApiImplicitParam(name = "dictRegion", value = "行政区编码字典对象", required = true, dataType = "json", paramType = "path")
	public R updateById(@RequestBody DictRegion dictRegion) {
		return R.ok(dictRegionService.updateById(dictRegion));
	}

	/**
	 * 删除
	 *
	 * @param dictRegionId
	 * @return R
	 */
	@DeleteMapping("/{dictRegionId}")
	@SysLog("删除行政区编码字典")
	@ApiOperation(value = "删除行政区编码字典", notes = "删除行政区编码字典")
	@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
	public R removeById(@PathVariable String dictRegionId) {
		return R.ok(dictRegionService.removeById(dictRegionId));
	}

}
