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.SysJobEntity;
import io.renren.modules.sys.service.SysJobService;
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 15:35
 */
@RestController
@RequestMapping("sys/job")
public class SysJobController {
    
    @Autowired
    private SysJobService sysJobService;

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


    /**
     * 岗位信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("sys:job:info")
    public R info(@PathVariable("id") Long id){
        SysJobEntity job = sysJobService.getById(id);

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

    /**
     * 保存岗位
     */
    @SysLog("保存岗位")
    @PostMapping("/save")
    @RequiresPermissions("sys:job:save")
    public R save(@RequestBody @Valid SysJobEntity jobEntity){
        ValidatorUtils.validateEntity(jobEntity);
        sysJobService.save(jobEntity);
        return R.ok();
    }

    /**
     * 修改岗位
     */
    @SysLog("修改岗位")
    @PostMapping("/update")
    @RequiresPermissions("sys:job:update")
    public R update(@RequestBody @Valid SysJobEntity jobEntity){
        ValidatorUtils.validateEntity(jobEntity);
        sysJobService.updateById(jobEntity);
        return R.ok();
    }

    /**
     * 删除岗位
     */
    @SysLog("删除岗位")
    @PostMapping("/delete")
    @RequiresPermissions("sys:job:delete")
    public R delete(@RequestBody Long[] ids){
        sysJobService.removeByIds(CollUtil.toList(ids));
        return R.ok();
    }

    /**
     * 查询所有岗位
     */
    @GetMapping("/findAll")
    @RequiresPermissions("sys:job:list")
    public R findAll(){
        List<SysJobEntity> jobList = sysJobService.list();
        return R.ok().put("jobList",jobList);
    }
}
