package com.inspect.system.controller;

import com.inspect.base.core.utils.poi.ExcelUtil;
import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.base.core.web.page.TableDataInfo;
import com.inspect.common.log.annotation.Log;
import com.inspect.common.log.enums.BizType;
import com.inspect.common.security.annotation.RequiresPermissions;
import com.inspect.common.security.utils.SecurityUtils;
import com.inspect.system.domain.SysPost;
import com.inspect.system.service.ISysPostService;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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;

@RestController
@RequestMapping({"/post"})
public class SysPostController extends BaseController {
    private final ISysPostService postService;

    public SysPostController(ISysPostService postService) {
        this.postService = postService;
    }

    @RequiresPermissions({"system:post:list"})
    @GetMapping({"/list"})
    public TableDataInfo list(SysPost post) {
        this.startPage();
        List<SysPost> list = this.postService.selectPostList(post);
        return this.getDataTable(list);
    }

    @Log(
            title = "岗位管理",
            businessType = BizType.EXPORT
    )
    @RequiresPermissions({"system:post:export"})
    @PostMapping({"/export"})
    public void export(HttpServletResponse response, SysPost post) {
        List<SysPost> list = this.postService.selectPostList(post);
        ExcelUtil<SysPost> util = new ExcelUtil<>(SysPost.class);
        util.exportExcel(response, list, "岗位数据");
    }

    @RequiresPermissions({"system:post:query"})
    @GetMapping({"/{postId}"})
    public AjaxResult getInfo(@PathVariable Long postId) {
        return AjaxResult.success(this.postService.selectPostById(postId));
    }

    @RequiresPermissions({"system:post:add"})
    @Log(
            title = "岗位管理",
            businessType = BizType.INSERT
    )
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysPost post) {
        if ("1".equals(this.postService.checkPostNameUnique(post))) {
            return AjaxResult.error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if ("1".equals(this.postService.checkPostCodeUnique(post))) {
            return AjaxResult.error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        } else {
            post.setCreateBy(SecurityUtils.getUsername());
            return this.toAjax(this.postService.insertPost(post));
        }
    }

    @RequiresPermissions({"system:post:edit"})
    @Log(
            title = "岗位管理",
            businessType = BizType.UPDATE
    )
    @PostMapping({"/edit"})
    public AjaxResult edit(@Validated @RequestBody SysPost post) {
        if ("1".equals(this.postService.checkPostNameUnique(post))) {
            return AjaxResult.error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if ("1".equals(this.postService.checkPostCodeUnique(post))) {
            return AjaxResult.error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        } else {
            post.setUpdateBy(SecurityUtils.getUsername());
            return this.toAjax(this.postService.updatePost(post));
        }
    }

    @RequiresPermissions({"system:post:remove"})
    @Log(
            title = "岗位管理",
            businessType = BizType.DELETE
    )
    @PostMapping({"/remove/{postIds}"})
    public AjaxResult remove(@PathVariable Long[] postIds) {
        return this.toAjax(this.postService.deletePostByIds(postIds));
    }

    @GetMapping({"/optionselect"})
    public AjaxResult optionSelect() {
        List<SysPost> posts = this.postService.selectPostAll();
        return AjaxResult.success(posts);
    }
}
