package com.app.controller.platform;

import com.app.annotation.platform.OperationLog;
import com.app.dto.PageResult;
import com.app.dto.Result;
import com.app.dto.SearchDto;
import com.app.dto.platform.ResponseNodeDto;
import com.app.dto.platform.SearchJobDto;
import com.app.entity.platform.system.Job;
import com.app.service.platform.JobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 功能描述：岗位管理
 *
 * @Auther: 梁展鹏
 * @Date: 2018/7/24 11:38
 */
@Api(tags = "岗位管理")
@Slf4j
@RestController("PlatformJobController")
@RequestMapping("/platform/job")
public class JobController {

    @Resource
    private JobService jobService;

    /**
     * 查询【岗位】列表
     *
     * @param params 搜索dto
     * @return 响应结果
     */
    @ApiOperation(value = "查询[岗位管理]列表")
    @PostMapping("/page")
    public ResponseEntity<Result<PageResult<Job>>> page(@Valid @RequestBody SearchJobDto params) {
        return new ResponseEntity(new Result<>().ok(jobService.page(params)), HttpStatus.OK);
    }

    /**
     * 保存/更新[岗位]
     *
     */
    @OperationLog("更新 - 岗位")
    @ApiOperation(value = "保存/更新[岗位管理]")
    @PostMapping("/save-or-update")
    public ResponseEntity<Result> saveOrUpdate(@Valid @RequestBody Job job) {
        if(jobService.saveOrUpdate(job)!=null){
            return new ResponseEntity(new Result().ok(), HttpStatus.OK);
        }
        return new ResponseEntity(new Result().serverError("岗位或已存在"), HttpStatus.OK);
    }

    /**
     * 删除【岗位】
     */
    @OperationLog("替换删除状态 - 岗位")
    @ApiOperation(value = "替换[岗位管理]删除状态")
    @PostMapping("/handle-is-del")
    public ResponseEntity<Result> handleIsDel(@Valid @RequestBody SearchDto searchdto) {
        jobService.handleIsDel(searchdto.getId());
        return new ResponseEntity(new Result().ok(), HttpStatus.OK);
    }

    /**
     * 查询【岗位】节点树
     *
     */
    @ApiOperation(value = "查询【岗位】节点树")
    @PostMapping("/nodes")
    public ResponseEntity<Result<List<ResponseNodeDto>>> nodes() {
        return new ResponseEntity(new Result<>().ok(jobService.getAllNodes()), HttpStatus.OK);
    }

}
