package com.fs.company.controller.company;

import com.fs.common.annotation.Log;
import com.fs.common.constant.UserConstants;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanyPost;
import com.fs.company.service.ICompanyPostService;
import com.fs.framework.security.LoginUser;
import com.fs.framework.security.SecurityUtils;
import com.fs.framework.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 岗位信息操作处理
 *

 */
@RestController
@RequestMapping("/company/companyPost")
public class CompanyPostController extends BaseController
{
    @Autowired
    private ICompanyPostService postService;
    @Autowired
    private TokenService tokenService;
    /**
     * 获取岗位列表
     */
    @PreAuthorize("@ss.hasPermi('company:post:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyPost post)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        post.setCompanyId(loginUser.getCompany().getCompanyId());
        startPage();
        List<CompanyPost> list = postService.selectCompanyPostList(post);
        return getDataTable(list);
    }

    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('company:post:export')")
    @GetMapping("/export")
    public AjaxResult export(CompanyPost post)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        post.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanyPost> list = postService.selectCompanyPostList(post);
        ExcelUtil<CompanyPost> util = new ExcelUtil<CompanyPost>(CompanyPost.class);
        return util.exportExcel(list, "岗位数据");
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:post:query')")
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable Long postId)
    {
        return AjaxResult.success(postService.selectCompanyPostById(postId));
    }

    /**
     * 新增岗位
     */
    @PreAuthorize("@ss.hasPermi('company:post:add')")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CompanyPost post)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        post.setCompanyId(loginUser.getCompany().getCompanyId());
       if (postService.checkPostCodeUnique(post)>0)
        {
            return AjaxResult.error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setCreateBy(SecurityUtils.getUsername());
        return toAjax(postService.insertCompanyPost(post));
    }

    /**
     * 修改岗位
     */
    @PreAuthorize("@ss.hasPermi('company:post:edit')")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CompanyPost post)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        post.setCompanyId(loginUser.getCompany().getCompanyId());
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post)))
        {
            return AjaxResult.error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(postService.updateCompanyPost(post));
    }

    /**
     * 删除岗位
     */
    @PreAuthorize("@ss.hasPermi('company:post:remove')")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public AjaxResult remove(@PathVariable Long[] postIds)
    {

        return toAjax(postService.deleteCompanyPostByIds(postIds));
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        CompanyPost postMap=new CompanyPost();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        postMap.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanyPost> posts = postService.selectCompanyPostList(postMap);
        return AjaxResult.success(posts);
    }
}
