package com.willalp.web.controller.system;

import java.util.Arrays;
import java.util.List;

import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.system.domain.SysDeptPost;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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.system.domain.SysDeptJob;
import com.willalp.system.service.ISysDeptJobService;
import com.willalp.common.utils.poi.ExcelUtil;

/**
 * 职位Controller
 *
 * @author songyu
 * @date 2022-02-14
 */
@RestController
@RequestMapping("/system/deptJob")
public class SysDeptJobController extends BaseController
{
    @Autowired
    private ISysDeptJobService sysDeptJobService;

    /**
     * 查询职位列表
     */
    @PreAuthorize("@ss.hasPermi('system:deptJob:list')")
    @GetMapping("/list")
    public AjaxResult list(SysDeptJob sysDeptJob)
    {
        List<SysDeptJob> list = sysDeptJobService.selectSysDeptJobList(sysDeptJob);
        return AjaxResult.success(list);
    }

    /**
     * 导出职位列表
     */
    @PreAuthorize("@ss.hasPermi('system:deptJob:export')")
    @Log(title = "职位", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysDeptJob sysDeptJob)
    {
        List<SysDeptJob> list = sysDeptJobService.selectSysDeptJobList(sysDeptJob);
        ExcelUtil<SysDeptJob> util = new ExcelUtil<SysDeptJob>(SysDeptJob.class);
        return util.exportExcel(list, "职位数据");
    }

    /**
     * 获取职位详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:deptJob:query')")
    @GetMapping(value = "/{jobId}")
    public AjaxResult getInfo(@PathVariable("jobId") Long jobId)
    {
        return AjaxResult.success(sysDeptJobService.getById(jobId));
    }

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

    /**
     * 修改职位
     */
    @PreAuthorize("@ss.hasPermi('system:deptJob:edit')")
    @Log(title = "职位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDeptJob sysDeptJob)
    {
        sysDeptJob.setUpdateBy(SecurityUtils.getUsername());
        sysDeptJob.setUpdateTime(DateUtils.getNowDate());
        return toAjax(sysDeptJobService.updateSysDeptJob(sysDeptJob));
    }

    /**
     * 删除职位
     */
    @PreAuthorize("@ss.hasPermi('system:deptJob:remove')")
    @Log(title = "职位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{jobIds}")
    public AjaxResult remove(@PathVariable Long jobIds)
    {
        return toAjax(sysDeptJobService.deleteSysDeptJobByJobId(jobIds));
    }
}
