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.core.page.TableDataInfo;
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.HsPostConfig;
import com.willalp.system.service.IHsPostConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 岗位配置Controller
 *
 * @author willalp
 * @date 2022-09-21
 */
@RestController
@RequestMapping("/system/postConfig")
public class HsPostConfigController extends BaseController {
    @Resource
    private IHsPostConfigService hsPostConfigService;

    /**
     * 查询岗位配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:postConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsPostConfig hsPostConfig) {
        startPage();
        List<HsPostConfig> list = hsPostConfigService.selectHsPostConfigList(hsPostConfig);
        return getDataTable(list);
    }

    /**
     * 查询岗位配置列表
     */
    @GetMapping("/pcTreeList")
    public AjaxResult pcTreeList() {
        return AjaxResult.success(hsPostConfigService.getTreePost(null));
    }

    /**
     * 导出岗位配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:postConfig:export')")
    @Log(title = "岗位配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsPostConfig hsPostConfig) {
        List<HsPostConfig> list = hsPostConfigService.selectHsPostConfigList(hsPostConfig);
        ExcelUtil<HsPostConfig> util = new ExcelUtil<HsPostConfig>(HsPostConfig.class);
        return util.exportExcel(list, "岗位配置数据");
    }

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

    /**
     * 新增岗位配置
     */
    @PreAuthorize("@ss.hasPermi('system:postConfig:add')")
    @Log(title = "岗位配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsPostConfig hsPostConfig) {
        hsPostConfig.setCreateBy(SecurityUtils.getUsername());
        hsPostConfig.setCreateTime(DateUtils.getNowDate());
        hsPostConfig.setOrgCode("G00000007");
        return toAjax(hsPostConfigService.save(hsPostConfig));
    }

    /**
     * 修改岗位配置
     */
    @PreAuthorize("@ss.hasPermi('system:postConfig:edit')")
    @Log(title = "岗位配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsPostConfig hsPostConfig) {
        hsPostConfig.setUpdateBy(SecurityUtils.getUsername());
        hsPostConfig.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsPostConfigService.updateById(hsPostConfig));
    }

    /**
     * 删除岗位配置
     */
    @PreAuthorize("@ss.hasPermi('system:postConfig:remove')")
    @Log(title = "岗位配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(hsPostConfigService.removeByIds(Arrays.asList(ids)));
    }
}
