package com.elephant.framework.galaxy.aroad.module.system.controller;


import com.elephant.framework.galaxy.aroad.module.common.annotation.SysLog;
import com.elephant.framework.galaxy.aroad.module.common.controller.BaseController;
import com.elephant.framework.galaxy.aroad.module.system.entity.Dept;
import com.elephant.framework.galaxy.aroad.module.system.service.DeptService;
import com.elephant.framework.galaxy.aroad.response.R;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 部门管理 前端控制器
 * </p>
 *
 * @author wangjian
 * @since 2019-02-21
 */
@RestController
@AllArgsConstructor
@RequestMapping("/admin/dept")
public class DeptController extends BaseController {
    private final DeptService deptService;


    /**
     * 通过ID查询
     *
     * @param id ID
     * @return SysDept
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        return new R(deptService.getById(id));
    }

    /**
     * 返回树形菜单集合
     *
     * @return 树形菜单
     */
    @GetMapping(value = "/tree")
    public R getTree() {
        return new R(deptService.selectTree());
    }

    /**
     * 返回当前用户树形菜单集合
     *
     * @return 树形菜单
     */
    @GetMapping(value = "/user-tree")
    public R getUserTree() {
        return new R(deptService.getUserTree());
    }

    /**
     * 添加
     *
     * @param dept 实体
     * @return success/false
     */
    @SysLog("添加部门")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sys_dept_add')")
    public R save(@Valid @RequestBody Dept dept) {
        return new R(deptService.saveDept(dept));
    }

    /**
     * 删除
     *
     * @param id ID
     * @return success/false
     */
    @SysLog("删除部门")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('sys_dept_del')")
    public R removeById(@PathVariable Integer id) {
        return new R(deptService.removeDeptById(id));
    }

    /**
     * 编辑
     *
     * @param dept 实体
     * @return success/false
     */
    @SysLog("编辑部门")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sys_dept_edit')")
    public R update(@Valid @RequestBody Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        return new R(deptService.updateDeptById(dept));
    }

    /** 
    * @Description: 判断是否是省厅
    * @Param: [deptId] 
    * @return: com.elephant.framework.galaxy.aroad.response.R 
    * @Author: songwenjie 
    * @Date: 2019/5/9 
    */
    @GetMapping("/isProvince/{deptId}")
    public R isProvince(@PathVariable Integer deptId){
        return new R(deptService.isProvince(deptId));
    }
}

