package com.ytpm.middle.controller;

import com.ytpm.general.Result;
import com.ytpm.general.ResultTable;
import com.ytpm.middle.param.MiddleDeptListParam;
import com.ytpm.middle.param.MiddleDeptParam;
import com.ytpm.middle.service.DeptService;
import com.ytpm.middle.view.MiddleDeptVO;
import com.ytpm.middle.view.MiddleUserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

@RestController
@RequestMapping("/dept")
@Api(tags = "部门管理")
public class DeptController {

    @Resource
    private DeptService deptService;

    @ApiOperation("查询部门列表")
    @PostMapping("/queryList")
    public ResultTable<MiddleDeptVO> queryList(@RequestBody MiddleDeptListParam param) {
        return deptService.queryList(param);
    }

    @ApiOperation("新增部门")
    @PostMapping("/addOne")
    public Result<String> addOne(@RequestBody MiddleDeptParam param, @ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo) {
        param.setDefaultParam(userInfo.getUserId());
        return deptService.addOne(param);
    }

    @ApiOperation("修改部门")
    @PostMapping("/updateOne")
    public Result<String> updateOne(@RequestBody MiddleDeptParam param,@ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo) {
        param.setUpdateParam(userInfo.getUserId());
        return deptService.updateOne(param);
    }

    @ApiImplicitParam(name = "deptId", value = "部门ID", required = true)
    @ApiOperation("删除部门")
    @GetMapping("/deleteOne")
    public Result<String> deleteOne(@RequestParam(name = "deptId")Integer deptId,@ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo) {
        return deptService.deleteOne(deptId,userInfo.getUserId());
    }

}
