package com.nantian.cloud.jly.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jly.entity.DictRegion;
import com.nantian.cloud.jly.service.DictRegionService;
import com.nantian.cloud.jly.vo.TreeVo;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.security.annotation.Inner;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


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


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


	/**
	 * 信息
	 *
	 * @param dictRegionId
	 * @return R
	 */
	@Inner(value = false)
	@ApiOperation(value = "通过ID查询行政区编码字典")
	@ApiImplicitParam(name ="dictRegionId",value = "行政区编码字典ID",required = true,dataType = "int",paramType = "path")
	@GetMapping("/{dictRegionId}")
	public R getById(@PathVariable("dictRegionId") String dictRegionId) {
		return R.ok(dictRegionService.getById(dictRegionId));
	}

	/**
	 * 保存
	 *
	 * @param dictRegion
	 * @return R
	 */
	@ApiOperation(value = "新增行政区编码字典")
	@ApiImplicitParam(name ="DictRegion",value = "行政区编码字典",required = true,dataType = "json",paramType = "path")
	@PostMapping
	public R save(@RequestBody DictRegion dictRegion) {
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			dictRegion.setUserid(u.getId());
		}
		if (u.getDeptId() != null) {
			dictRegion.setDeptId(u.getDeptId());
		}
		dictRegion.setDelFlag("1");
		dictRegion.setUpdateTime(new Date());
		return R.ok(dictRegionService.save(dictRegion));
	}

	/**
	 * 修改
	 *
	 * @param dictRegion
	 * @return R
	 */
	@ApiOperation(value = "修改行政区编码字典")
	@ApiImplicitParam(name ="DictRegion",value = "行政区编码字典",required = true,dataType = "json",paramType = "path")
	@PutMapping
	public R updateById(@RequestBody DictRegion dictRegion) {
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			dictRegion.setUserid(u.getId());
		}
		dictRegion.setUpdateTime(new Date());
		return R.ok(dictRegionService.updateById(dictRegion));
	}

	/**
	 * 删除
	 *
	 * @param dictRegionId
	 * @return R
	 */
	@ApiOperation(value = "删除行政区编码字典")
	@ApiImplicitParam(name ="dictRegionId",value = "dictRegionId",required = true,dataType = "string",paramType = "path")
	@DeleteMapping("/{dictRegionId}")
	public R removeById(@PathVariable("dictRegionId") String dictRegionId) {
		DictRegion dictRegion = dictRegionService.getById(dictRegionId);
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			dictRegion.setUserid(u.getId());
		}
		dictRegion.setDelFlag("4");
		dictRegion.setUpdateTime(new Date());
		return R.ok(dictRegionService.updateById(dictRegion));
	}

	/**
	 * 获取树形结构
	 * @return
	 */
	@GetMapping("/tree")
	public R getDictRegionTree(String id) {
		return R.ok(dictRegionService.getDictRegionTree(id));
	}

	/**
	 * 获取添加市属树形结构
	 */
	@GetMapping("/trees")
	@ApiOperation(value = "获取行政区划树形结构", notes = "获取行政区划树形结构")
	public R getDictRegionTrees(String id) {
		List<TreeVo> dictRegionTrees = dictRegionService.getDictRegionTree(id);
		// 获取第一级孩子数组
		TreeVo treeVo = dictRegionTrees.get(0);
		List<TreeVo> children = treeVo.getChildren();
		for (int i = 0; i < children.size(); i++) {
			TreeVo treeVo1 = children.get(i);
			String value = treeVo1.getValue();
			List<TreeVo> children1 = treeVo1.getChildren();
			TreeVo treeVo2 = new TreeVo();
			treeVo2.setValue(value);
			treeVo2.setLabel(treeVo1.getLabel().substring(0,2)+"市属");
			if(!children1.contains(treeVo2)){
				//children1.add(0,treeVo2);
			}
		}
		return R.ok(dictRegionTrees);
	}
}
