package com.govmade.modules.system.controller;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.govmade.common.utils.PageTreeUtils;
import com.govmade.common.utils.R;
import com.govmade.common.validator.Assert;
import com.govmade.modules.basic.controller.AbstractController;
import com.govmade.modules.system.entity.AreaEntity;
import com.govmade.modules.system.service.AreaService;

/**
 * 系统管理 - 行政区划设置
 * 
 * @author 刘弈臻
 * @date 2018年8月9日
 */

@RestController
@RequestMapping("/system/area")
public class AreaController extends AbstractController{
     
	@Autowired
	private AreaService areaService;
	
	/**
	 * 所有区划数据树结构
	 */
	@GetMapping("/listTree")
	public R listTree(@RequestParam Map<String, Object> params) {
		PageTreeUtils page = areaService.queryPage(params);
		return R.ok().put("page", page);
	}
	
	/**
	 * 保存或更新区划
	 */
	@PostMapping("/save")
	public R save(AreaEntity area) {

		if (null == area.getId()) {
			if (this.areaService.checkArea(area.getName()) > 0) {
				return R.error(area.getName() + " 已存在!");
			}
			
		}
		areaService.save(area);
		return R.ok();
	}
	
	/**
	 * 批量删除区划
	 * 
	 * @param ids
	 * @return
	 */
	@DeleteMapping("/delete")
	public R deleteArea(@RequestBody Set<Long> ids) {
		Assert.isNull(ids, "删除项不能为空");

		areaService.deleteBatch(ids);
		return R.ok();
	}
	
}