package com.ebts.web.controller.system;

import com.ebts.common.annotation.Log;
import com.ebts.common.constant.UserConstants;
import com.ebts.common.core.controller.BaseController;
import com.ebts.common.core.entity.Result;
import com.ebts.common.enums.BusinessType;
import com.ebts.common.utils.SecurityUtils;
import com.ebts.common.utils.poi.ExcelUtil;
import com.ebts.system.entity.Post;
import com.ebts.system.service.PostService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 岗位信息操作处理
 *
 * @author binlin
 */
@RestController
@RequestMapping("/system/post")
public class PostController extends BaseController {
    @Resource
    private PostService postService;

    /**
     * 获取岗位列表
     */
    @PreAuthorize("@ebts.hasPermi('system:post:list')")
    @GetMapping("/list")
    public Serializable list(Post post) {
        startPage();
        List<Post> list = postService.selectPostList(post);
        return getDataTable(list);
    }

    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ebts.hasPermi('system:post:export')")
    @GetMapping("/export")
    public Result<Post> export(Post post) {
        List<Post> list = postService.selectPostList(post);
        ExcelUtil<Post> util = new ExcelUtil<Post>(Post.class);
        return util.exportExcel(list, "岗位数据");
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @PreAuthorize("@ebts.hasPermi('system:post:query')")
    @GetMapping(value = "/{postId}")
    public Result<Post> getInfo(@PathVariable Long postId) {
        return Result.ok(postService.selectPostById(postId));
    }

    /**
     * 新增岗位
     */
    @PreAuthorize("@ebts.hasPermi('system:post:add')")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<Integer> add(@Validated @RequestBody Post post) {
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            return Result.error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            return Result.error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setCreateBy(SecurityUtils.getUserId());
        return toResult(postService.insertPost(post));
    }

    /**
     * 修改岗位
     */
    @PreAuthorize("@ebts.hasPermi('system:post:edit')")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<Integer> edit(@Validated @RequestBody Post post) {
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            return Result.error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            return Result.error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setUpdateBy(SecurityUtils.getUserId());
        return toResult(postService.updatePost(post));
    }

    /**
     * 删除岗位
     */
    @PreAuthorize("@ebts.hasPermi('system:post:remove')")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public Result<Integer> remove(@PathVariable Long[] postIds) {
        if (postIds.length == 0){
            return Result.error("postId不能为空");
        }
        return toResult(postService.deletePostByIds(postIds));
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public Result<List<Post>> optionselect() {
        List<Post> posts = postService.selectPostAll();
        return Result.ok(posts);
    }
}
