package com.chushang.system.controller;

import com.chushang.common.core.exception.utils.AssertUtil;
import com.chushang.common.core.web.AjaxResult;
import com.chushang.common.excel.utils.ExcelUtils;
import com.chushang.common.log.annotation.SysLog;
import com.chushang.common.log.enums.BusinessType;
import com.chushang.common.mybatis.page.CommonParam;
import com.chushang.common.mybatis.utils.PageResult;
import com.chushang.datascope.entity.DataScopeEntity;
import com.chushang.security.annotation.RequiresPermissions;
import com.chushang.security.entity.vo.LoginUser;
import com.chushang.security.utils.SecurityUtils;
import com.chushang.system.entity.po.SysPost;
import com.chushang.system.entity.po.SysUser;
import com.chushang.system.service.ISysPostService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @auther: zhao
 * @date: 2024/5/9 15:54
 */
@RestController
@RequestMapping("/post")
public class PostController {

    @Resource
    ISysPostService postService;

    /**
     * 获取岗位列表
     */
    @RequiresPermissions("system:post:list")
    @GetMapping("/list")
    public AjaxResult list(SysPost post)
    {
        CommonParam commonParam = CommonParam.buildPageRequest();
        PageResult pageResult = postService.pagePostList(post, commonParam);
        return AjaxResult.success(pageResult);
    }

    @SysLog(value = "岗位", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:post:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPost post)
    {
        CommonParam commonParam = CommonParam.buildAllRequest();
        List<SysPost> list = postService.allPostList(post, commonParam);
        ExcelUtils.exportList(response, SysPost.class,list, "岗位数据");
    }

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

    /**
     * 新增岗位
     */
    @RequiresPermissions("system:post:add")
    @SysLog(value = "岗位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysPost post)
    {
        SysPost postName = new SysPost();
        postName.setPostName(post.getPostName());
        postName.setPostCode(null);
        AssertUtil.invalidate(postService.checkPostUnique(postName), "新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        postName.setPostCode(post.getPostCode());
        postName.setPostName(null);
        AssertUtil.invalidate(postService.checkPostUnique(postName), "新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        post.setCreateBy(SecurityUtils.getUsername());

        LoginUser<SysUser, DataScopeEntity> loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        Long deptId = sysUser.getDeptId();

        post.setDeptId(deptId);
        postService.saveOrUpdate(post);
        return AjaxResult.success();
    }

    /**
     * 修改岗位
     */
    @RequiresPermissions("system:post:edit")
    @SysLog(value = "岗位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysPost post)
    {
        SysPost postName = new SysPost();
        postName.setPostName(post.getPostName());
        postName.setPostId(post.getPostId());
        postName.setPostCode(null);
        AssertUtil.invalidate(postService.checkPostUnique(postName), "修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        postName.setPostCode(post.getPostCode());
        postName.setPostName(null);
        AssertUtil.invalidate(postService.checkPostUnique(postName), "修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        post.setCreateBy(SecurityUtils.getUsername());
        postService.saveOrUpdate(post);
        return AjaxResult.success();
    }

    /**
     * 删除岗位
     */
    @RequiresPermissions("system:post:remove")
    @SysLog(value = "岗位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public AjaxResult remove(@PathVariable Long[] postIds)
    {
        postService.removeBatchByIds(Arrays.asList(postIds));
        return AjaxResult.success();
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/option/select")
    public AjaxResult optionSelect()
    {
        CommonParam commonParam = CommonParam.buildAllRequest();
        List<SysPost> list = postService.allPostList(new SysPost(), commonParam);
        return AjaxResult.success(list);
    }

}
