package com.lutao.cloud.admin.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lutao.cloud.admin.api.entity.SysDept;
import com.lutao.cloud.admin.api.entity.SysDeptRelation;
import com.lutao.cloud.admin.service.SysDeptRelationService;
import com.lutao.cloud.admin.service.SysDeptService;
import com.lutao.cloud.common.core.util.MessageInfo;
import com.lutao.cloud.common.log.annotation.SysLog;
import com.lutao.cloud.common.security.annotation.Inner;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 部门管理 前端控制器
 * </p>
 *

 * @since 2018-01-20
 */
@Api(value = "/dept", tags ={"部门管理"})
@RestController
@AllArgsConstructor
@RequestMapping("/dept")
public class SysDeptController{

	private final SysDeptRelationService relationService;

	private final SysDeptService sysDeptService;

	/**
	 * 通过ID查询
	 * @param id ID
	 * @return SysDept
	 */
	@ApiOperation(value = "通过ID查询", notes = "通过ID查询", httpMethod = "GET")
	@GetMapping("/{id}")
	@Inner
	public MessageInfo<SysDept> getById(@PathVariable Integer id){
		return MessageInfo.ok(sysDeptService.getById(id));
	}

	/**
	 * 返回树形菜单集合
	 * @return 树形菜单
	 */
	@ApiOperation(value = "返回树形菜单集合", notes = "返回树形菜单集合", httpMethod = "GET")
	@GetMapping(value = "/tree")
	public MessageInfo<List<Tree<Integer>>> getTree(){
		return MessageInfo.ok(sysDeptService.selectTree());
	}

	/**
	 * 添加
	 * @param sysDept 实体
	 * @return success/false
	 */
	@ApiOperation(value = "添加", notes = "添加", httpMethod = "POST")
	@SysLog("添加部门")
	@PostMapping
	@Inner
//	@PreAuthorize("@pms.hasPermission('sys_dept_add')")
	public MessageInfo<Boolean> save(@Valid @RequestBody SysDept sysDept){
		return sysDeptService.saveDept(sysDept);
	}

	/**
	 * 删除
	 * @param id ID
	 * @return success/false
	 */
	@ApiOperation(value = "删除", notes = "删除", httpMethod = "DELETE")
	@SysLog("删除部门")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_dept_del')")
	public MessageInfo<Boolean> removeById(@PathVariable Integer id){
		return MessageInfo.ok(sysDeptService.removeDeptById(id));
	}

	/**
	 * 编辑
	 * @param sysDept 实体
	 * @return success/false
	 */
	@ApiOperation(value = "编辑", notes = "编辑", httpMethod = "PUT")
	@SysLog("编辑部门")
	@PutMapping
	@Inner
//	@PreAuthorize("@pms.hasPermission('sys_dept_edit')")
	public MessageInfo<Boolean> update(@Valid @RequestBody SysDept sysDept){
		sysDept.setUpdateTime(LocalDateTime.now());
		return sysDeptService.updateDeptById(sysDept);
	}

	/**
	 * 查收子级列表
	 * @return 返回子级
	 */
	@ApiOperation(value = "查收子级列表", notes = "查收子级列表", httpMethod = "GET")
	@GetMapping(value = "/getDescendantList/{deptId}")
	public MessageInfo<List<SysDeptRelation>> getDescendantList(@PathVariable Integer deptId){
		return MessageInfo.ok(
				relationService.list(Wrappers.<SysDeptRelation>lambdaQuery().eq(SysDeptRelation::getAncestor, deptId)));
	}

}
