package com.lz.modules.app.controller;

import java.util.Arrays;
import java.util.List;

import com.lz.modules.app.dto.DepartmentsDto;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lz.modules.app.entity.DepartmentsEntity;
import com.lz.modules.app.service.DepartmentsService;
import com.lz.common.utils.R;


/**
 * 部门表
 *
 * @author fumeiai
 * @email fumeiai@linzikg.com
 * @date 2020-04-30 16:59:50
 */
@RestController
@RequestMapping("staff/structure")
@Api(tags = "部门相关")
public class DepartmentsController {
    @Autowired
    private DepartmentsService departmentsService;

            /**
             * 列表
             */
    @PostMapping("/list")
    //    @RequiresPermissions("staff:structure:list")
    public List<DepartmentsDto> list() {
        List<DepartmentsDto> departmentList = departmentsService.getDepartmentTree(0);

        return departmentList;
    }

    @GetMapping("/deplist")
    //    @RequiresPermissions("staff:structure:list")
    @ApiOperation("获取全部有效部门树列表")
    @ApiResponses({@ApiResponse(code = 200, message = "成功", response = DepartmentsDto.class)})
    public R deplist(@RequestParam @ApiParam(value = "请求类型0 请求部门不包含人员列表，1同时返回人员列表") int type) {
        List<DepartmentsDto> departmentList = departmentsService.getDepartmentTree(type);

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



    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("staff:structure::info")
    public R info(@PathVariable("id") Long id) {
        DepartmentsEntity departments = departmentsService.getById(id);
        return R.ok().put("departments", departments);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("staff:structure:save")
    public R save(@RequestBody DepartmentsEntity departments) {
        departmentsService.save(departments);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("staff:structure:update")
    public R update(@RequestBody DepartmentsEntity departments) {
        departmentsService.updateById(departments);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("staff:structure:delete")
    public R delete(@RequestBody Long[] ids) {
        departmentsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
