package com.zcloud.modules.oa.controller;

import com.zcloud.common.utils.PageUtils;
import com.zcloud.common.utils.R;
import com.zcloud.modules.oa.entity.OaDepartmentEntity;
import com.zcloud.modules.oa.service.OaDepartmentService;
import com.zcloud.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Map;


/**
 * 部门管理
 *
 */
@RestController
@RequestMapping("/oa/department")
@Api(tags="部门管理")
public class OaDepartmentController extends AbstractController {
	@Autowired
	private OaDepartmentService oaDepartmentService;

	/**
	 * 保存
	 */
	@PostMapping("/save")
	//@RequiresPermission("oaDepartment:save")
	@ApiOperation(value = "保存部门")
	public R save(@RequestBody OaDepartmentEntity department){
		department.setCreateor(getUserId());
		department.setOperator(getUserId());
		boolean result = oaDepartmentService.saveDepartment(department);
		if (!result)
			return R.error(200,"部门名重复!");
		return R.ok();
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	//@RequiresPermission("oaDepartment:update")
	@ApiOperation(value = "修改部门")
	public R update(@RequestBody OaDepartmentEntity department){
		department.setOperator(getUserId());
		oaDepartmentService.updateById(department);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping("/delete")
	//@RequiresPermission("oaDepartment:delete")
	@ApiOperation(value = "删除部门")
	public R delete(@RequestBody OaDepartmentEntity department){
		//判断是否有下级部门
		List<OaDepartmentEntity> departmentList = oaDepartmentService.queryListParentId(department.getDepartmentId(), department.getCorpinfoId());
		if(departmentList.size() > 0){
			return R.error(200,"请先删除下级部门");
		}

		oaDepartmentService.delete(department.getDepartmentId());

		return R.ok();
	}

	/**
	 * 分页部门列表
	 */
	@PostMapping("/listPage")
	//@RequiresPermission("oaDepartment:list")
	@ApiOperation(value = "分页查询部门列表")
	public R listPage(@RequestBody Map<String, Object> params){

		PageUtils page = oaDepartmentService.queryPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 所有部门列表
	 */
	@PostMapping("/list")
	//@RequiresPermission("oaDepartment:list")
	@ApiOperation(value = "查询部门列表")
	public R list(@RequestBody OaDepartmentEntity department){
		List<OaDepartmentEntity> departmentList = oaDepartmentService.queryListParentId(department.getParentId(),department.getCorpinfoId());

		//查询完成 对此list直接排序
		Collections.sort(departmentList);

		return R.ok().put("departmentList", departmentList);
	}

	/**
	 * 部门信息
	 */
	@PostMapping("/info")
	//@RequiresPermission("oaDepartment:info")
	@ApiOperation(value = "查看部门详情")
	public R info(@RequestBody OaDepartmentEntity department){
		department = oaDepartmentService.getById(department.getDepartmentId());
		return R.ok().put("department", department);
	}

	/**
	 * 分页部门列表
	 */
	@PostMapping("/getTree")
	@ApiOperation(value = "查询部门树形列表")
	public R getTree(@RequestBody OaDepartmentEntity department){

		List<OaDepartmentEntity> list = oaDepartmentService.getDeptTree(department.getParentId(),department.getCorpinfoId());

		return R.ok().put("deptTree", list);
	}
}

