package com.ruoyi.web.work.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.work.domain.Enterprise;
import com.ruoyi.web.work.domain.Position;
import com.ruoyi.web.work.service.IEnterpriseService;
import com.ruoyi.web.work.service.IPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 职位管理
 *
 * @author lsw
 * @date 2024-06-07
 */
@RestController
@RequestMapping("/work/position")
public class PositionController extends BaseController {
    @Autowired
    private IPositionService positionService;

    @Autowired
    private IEnterpriseService enterpriseService;

    @PreAuthorize("@ss.hasPermi('work:position:list')")
    @GetMapping("/list")
    public TableDataInfo list(Position position) {
        startPage();
        List<Position> list = positionService.selectList(position);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('work:position:query')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Long id) {
        Position position = positionService.getById(id);
        Enterprise enterprise = enterpriseService.getById(position.getUserId());
        position.setEnterpriseName(enterprise.getName());
        return AjaxResult.success(position);
    }

    @PreAuthorize("@ss.hasPermi('work:position:edit')")
    @Log(title = "职位管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Position position) {
        return toAjax(positionService.updateById(position));
    }

    @PreAuthorize("@ss.hasPermi('work:position:remove')")
    @Log(title = "职位管理", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(positionService.removeByIds(Arrays.asList(ids)));
    }
}
