package com.willalp.web.controller.system;

import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.enums.BusinessType;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.system.domain.SysDeptPost;
import com.willalp.system.service.ISysDeptPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 岗位设置2Controller
 *
 * @author songyu
 * @date 2022-02-11
 */
@RestController
@RequestMapping("/system/deptPost")
public class SysDeptPostController extends BaseController
{
    @Autowired
    private ISysDeptPostService sysDeptPostService;

    /**
     * 查询岗位设置2列表
     */
    @PreAuthorize("@ss.hasPermi('system:deptPost:list')")
    @GetMapping("/list")
    public AjaxResult list(SysDeptPost sysDeptPost)
    {
        sysDeptPost.setParentId(null);
        List<SysDeptPost> list = sysDeptPostService.selectSysDeptPostList(sysDeptPost);
        return AjaxResult.success(list);
    }

    /**
     * 导出岗位设置2列表
     */
    @PreAuthorize("@ss.hasPermi('system:deptPost:export')")
    @Log(title = "岗位设置2", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysDeptPost sysDeptPost)
    {
        List<SysDeptPost> list = sysDeptPostService.selectSysDeptPostList(sysDeptPost);
        ExcelUtil<SysDeptPost> util = new ExcelUtil<SysDeptPost>(SysDeptPost.class);
        return util.exportExcel(list, "岗位设置2数据");
    }

    /**
     * 获取岗位设置2详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:deptPost:query')")
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable("postId") String postId)
    {
        return AjaxResult.success(sysDeptPostService.getById(postId));
    }

    /**
     * 新增岗位设置2
     */
    @PreAuthorize("@ss.hasPermi('system:deptPost:add')")
    @Log(title = "岗位设置2", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDeptPost sysDeptPost)
    {
        sysDeptPost.setCreateBy(SecurityUtils.getUsername());
        sysDeptPost.setCreateTime(DateUtils.getNowDate());
        if (sysDeptPost.getParentId() != null && sysDeptPost.getParentId() != 0) {
            SysDeptPost parent = sysDeptPostService.selectById(sysDeptPost.getParentId());
            sysDeptPost.setAncestors(parent.getAncestors() + "," + sysDeptPost.getParentId());
        } else {
            sysDeptPost.setParentId(null);
            sysDeptPost.setAncestors("0");
        }
        return toAjax(sysDeptPostService.save(sysDeptPost));
    }

    /**
     * 修改岗位设置2
     */
    @PreAuthorize("@ss.hasPermi('system:deptPost:edit')")
    @Log(title = "岗位设置2", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDeptPost sysDeptPost)
    {
        sysDeptPost.setUpdateBy(SecurityUtils.getUsername());
        sysDeptPost.setUpdateTime(DateUtils.getNowDate());
        return toAjax(sysDeptPostService.updateSysDeptPost(sysDeptPost));
    }

    /**
     * 删除岗位设置2
     */
    @PreAuthorize("@ss.hasPermi('system:deptPost:remove')")
    @Log(title = "岗位设置2", businessType = BusinessType.DELETE)
	@DeleteMapping("/{postIds}")
    public AjaxResult remove(@PathVariable Long postIds)
    {
        return toAjax(sysDeptPostService.deleteSysDeptPostByPostId(postIds));
    }
}
