package com.zd.system.controller;

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

import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
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.zd.common.core.constant.UserConstants;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.utils.poi.ExcelUtil;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.system.domain.SysPost;
import com.zd.system.service.ISysPostService;

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

    /**
     * 获取职位列表 "system:post:list"
     */
    @PreAuthorize(hasPermi = PerPrefix.SYSTEM_POST+ PerFun.LIST)
    @GetMapping("/list")
    public TableDataInfo list(SysPost post) {
        startPage();
        List<SysPost> list = postService.selectPostList(post);
        return getDataTable(list);
    }

    //"system:post:export"
    @Log(title = "职位管理", businessType = BusinessType.EXPORT)
    @PreAuthorize(hasPermi = PerPrefix.SYSTEM_POST+ PerFun.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPost post) throws IOException {
        List<SysPost> list = postService.selectPostList(post);
        ExcelUtil<SysPost> util = new ExcelUtil<SysPost>(SysPost.class);
        util.exportExcel(response, list, "职位数据");
    }

    /**
     * 根据职位编号获取详细信息 "system:post:query"
     */
    @PreAuthorize(hasPermi = PerPrefix.SYSTEM_POST+ PerFun.QUERY)
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable Long postId) {
        return AjaxResult.success(postService.selectPostById(postId));
    }

    /**
     * 新增职位 "system:post:add"
     */
    @PreAuthorize(hasPermi = PerPrefix.SYSTEM_POST+ PerFun.ADD)
    @Log(title = "职位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysPost post) {
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            return AjaxResult.error("新增职位'" + post.getPostName() + "'失败，职位名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            return AjaxResult.error("新增职位'" + post.getPostName() + "'失败，职位编码已存在");
        }
        post.setCreateBy(SecurityUtils.getUsername());
        return toAjax(postService.insertPost(post));
    }

    /**
     * 修改职位 "system:post:edit"
     */
    @PreAuthorize(hasPermi = PerPrefix.SYSTEM_POST+ PerFun.EDIT)
    @Log(title = "职位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysPost post) {
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            return AjaxResult.error("修改职位'" + post.getPostName() + "'失败，职位名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            return AjaxResult.error("修改职位'" + post.getPostName() + "'失败，职位编码已存在");
        }
        post.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(postService.updatePost(post));
    }

    /**
     * 删除职位 "system:post:remove"
     */
    @PreAuthorize(hasPermi = PerPrefix.SYSTEM_POST+ PerFun.REMOVE)
    @Log(title = "职位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public AjaxResult remove(@PathVariable Long[] postIds) {
        return toAjax(postService.deletePostByIds(postIds));
    }

    /**
     * 获取职位选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect(SysPost post) {
        List<SysPost> posts = postService.selectPostAll(post);
        return AjaxResult.success(posts);
    }
}
