package com.bonus.system.controller;

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

import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.log.enums.BusinessType;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.system.domain.SysPost;
import com.bonus.system.service.ISysPostService;

/**
 * 岗位信息操作处理
 * 
 * @author bonus
 */
@RestController
@RequestMapping("/post")
@Slf4j
public class SysPostController extends BaseController
{
    @Autowired
    private ISysPostService postService;

    /**
     * 获取岗位列表
     */
    @RequiresPermissions("system:post:list")
    @GetMapping("/list")
    @SysLog(title = "岗位管理", businessType = OperaType.QUERY,logType = 0,module = "系统管理->岗位管理")
    public TableDataInfo list(SysPost post) {
       try{
           startPage();
           List<SysPost> list = postService.selectPostList(post);
           return getDataTable(list);
       }catch (Exception e){
           log.error(e.toString(),e);
       }
       return  getDataTableError(new ArrayList<>());
    }

    @RequiresPermissions("system:post:export")
    @PostMapping("/export")
    @SysLog(title = "岗位管理", businessType = OperaType.EXPORT,logType = 0,module = "系统管理->岗位管理")
    public void export(HttpServletResponse response, SysPost post) {
         try{
             List<SysPost> list = postService.selectPostList(post);
             ExcelUtil<SysPost> util = new ExcelUtil<SysPost>(SysPost.class);
             util.exportExcel(response, list, "岗位数据");
         }catch (Exception e){
             log.error(e.toString(),e);
         }
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @RequiresPermissions("system:post:query")
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable Long postId) {
        try{
            return success(postService.selectPostById(postId));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 新增岗位
     */
    @RequiresPermissions("system:post:add")
    @PostMapping
    @SysLog(title = "岗位管理", businessType = OperaType.INSERT,logType = 0,module = "系统管理->岗位管理",details ="新增岗位" )
    public AjaxResult add(@Validated @RequestBody SysPost post) {
        try{
            if (!postService.checkPostNameUnique(post)) {
                return error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
            } else if (!postService.checkPostCodeUnique(post)) {
                return error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
            }
            post.setCreateBy(SecurityUtils.getUsername());
            return toAjax(postService.insertPost(post));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 修改岗位
     */
    @RequiresPermissions("system:post:edit")
    @PutMapping
    @SysLog(title = "岗位管理", businessType = OperaType.UPDATE,logType = 0,module = "系统管理->岗位管理")
    public AjaxResult edit(@Validated @RequestBody SysPost post) {
        try{
            if (!postService.checkPostNameUnique(post)) {
                return error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
            } else if (!postService.checkPostCodeUnique(post)) {
                return error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
            }
            post.setUpdateBy(SecurityUtils.getUsername());
            return toAjax(postService.updatePost(post));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 删除岗位
     */
    @RequiresPermissions("system:post:remove")
    @DeleteMapping("/{postIds}")
    @SysLog(title = "岗位管理", businessType = OperaType.DELETE,logType = 0,module = "系统管理->岗位管理")
    public AjaxResult remove(@PathVariable Long[] postIds) {
        try{
            return toAjax(postService.deletePostByIds(postIds));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect() {
        try{
            List<SysPost> posts = postService.selectPostAll();
            return success(posts);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");

    }
}
