package com.govmade.modules.system.controller;

import java.util.Map;

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.PageUtils;
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.DepartEntity;
import com.govmade.modules.system.service.DepartService;
import com.govmade.modules.system.service.UserService;

/**
 * 系统管理 - 部门设置
 * 
 * @author 刘弈臻
 * @date 2018年8月8日
 */
@RestController
@RequestMapping("/system/depart")
public class DepartController extends AbstractController{
  
	@Autowired
	private DepartService departService;
	
	@Autowired
	private UserService userService;
	
	/**
	 * 部门列表
	 */
	@GetMapping("/list")
	public R list(@RequestParam Map<String, Object> params) {
		
		PageUtils page = departService.queryPage(params);
		return R.ok().put("page", page);
	}
	
	/**
	 * 保存或更新部门
	 */
	@PostMapping("/save")
	public R save(DepartEntity depart) {

		if (null == depart.getId()) {
			if (this.departService.checkDepart(depart.getName()) > 0) {
				return R.error(depart.getName() + " 已存在!");
			}
			
		}
		departService.save(depart);
		return R.ok();
	}
	

	/**
	 * 删除部门
	 * 
	 * @param ids
	 * @return
	 */

	@DeleteMapping("/delete")
	public R deleteDepart(@RequestBody DepartEntity depart) {
		Assert.isNull(depart.getId(), "删除项不能为空");
        if(this.userService.userCount(depart.getId())>0) {
           return R.error("选中部门下有用户!!");
        }
		departService.deleteDepart(depart);
		return R.ok();
	}
	
}