package io.renren.modules.sys.controller;

import cn.hutool.core.collection.CollUtil;
import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.SysDepartmentEntity;
import io.renren.modules.sys.service.SysDepartmentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门管理Controller
 * </p>
 *
 * @author yinhao
 * @date 2021/6/9 14:21
 */
@RestController
@RequestMapping("sys/department")
public class SysDepartmentController extends AbstractController {

    @Autowired
    private SysDepartmentService sysDepartmentService;

    /**
     * 所有部门列表
     */
    @GetMapping("/list")
    @RequiresPermissions("sys:department:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysDepartmentService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 部门信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("sys:department:info")
    public R info(@PathVariable("id") Long id){
        SysDepartmentEntity department = sysDepartmentService.getById(id);

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

    /**
     * 保存部门
     */
    @SysLog("保存部门")
    @PostMapping("/save")
    @RequiresPermissions("sys:department:save")
    public R save(@RequestBody @Valid SysDepartmentEntity departmentEntity){
        ValidatorUtils.validateEntity(departmentEntity);
        sysDepartmentService.save(departmentEntity);
        return R.ok();
    }

    /**
     * 修改部门
     */
    @SysLog("修改部门")
    @PostMapping("/update")
    @RequiresPermissions("sys:department:update")
    public R update(@RequestBody @Valid SysDepartmentEntity departmentEntity){
        //ValidatorUtils.validateEntity(departmentEntity);
        sysDepartmentService.updateById(departmentEntity);
        return R.ok();
    }

    /**
     * 删除部门
     */
    @SysLog("删除部门")
    @PostMapping("/delete")
    @RequiresPermissions("sys:department:delete")
    public R delete(@RequestBody Long[] ids){
        sysDepartmentService.removeByIds(CollUtil.toList(ids));
        return R.ok();
    }

    /**
     * 查询所有部门
     */
    @GetMapping("/findAll")
    @RequiresPermissions("sys:department:list")
    public R findAll(){
        List<SysDepartmentEntity> departmentList = sysDepartmentService.list();
        return R.ok().put("departmentList",departmentList);
    }


}
