package com.ruoyi.hezhi.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.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hezhi.domain.TbCareerClass;
import com.ruoyi.hezhi.service.ITbCareerClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 职业分类Controller
 *
 * @author ruoyi
 * @date 2024-10-10
 */
@RestController
@RequestMapping("/hezhi/careerClass")
public class TbCareerClassController extends BaseController
{
    @Autowired
    private ITbCareerClassService tbCareerClassService;

    /**
     * 查询职业分类列表
     */
    @PreAuthorize("@ss.hasPermi('hezhi:careerClass:list')")
    @GetMapping("/list")
    public AjaxResult list(TbCareerClass tbCareerClass)
    {
        List<TbCareerClass> list = tbCareerClassService.selectTbCareerClassList(tbCareerClass);
        return success(list);
    }

    /**
     * 导出职业分类列表
     */
    @PreAuthorize("@ss.hasPermi('hezhi:careerClass:export')")
    @Log(title = "职业分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbCareerClass tbCareerClass)
    {
        List<TbCareerClass> list = tbCareerClassService.selectTbCareerClassList(tbCareerClass);
        ExcelUtil<TbCareerClass> util = new ExcelUtil<TbCareerClass>(TbCareerClass.class);
        util.exportExcel(response, list, "职业分类数据");
    }

    /**
     * 获取职业分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('hezhi:careerClass:query')")
    @GetMapping(value = "/{careerId}")
    public AjaxResult getInfo(@PathVariable("careerId") Long careerId)
    {
        return success(tbCareerClassService.selectTbCareerClassByCareerId(careerId));
    }

    /**
     * 新增职业分类
     */
    @PreAuthorize("@ss.hasPermi('hezhi:careerClass:add')")
    @Log(title = "职业分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbCareerClass tbCareerClass)
    {
        if (tbCareerClass.getPid() == 0) {
            tbCareerClass.setLevel(0);
        } else {
            TbCareerClass tbCareerClassUp = tbCareerClassService.selectTbCareerClassByCareerId(tbCareerClass.getPid());
            if (tbCareerClassUp != null) {
                tbCareerClass.setLevel(tbCareerClassUp.getLevel() + 1);
            }
        }
        tbCareerClass.setType(0);
        return toAjax(tbCareerClassService.insertTbCareerClass(tbCareerClass));
    }

    /**
     * 修改职业分类
     */
    @PreAuthorize("@ss.hasPermi('hezhi:careerClass:edit')")
    @Log(title = "职业分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbCareerClass tbCareerClass)
    {
        return toAjax(tbCareerClassService.updateTbCareerClass(tbCareerClass));
    }

    /**
     * 删除职业分类
     */
    @PreAuthorize("@ss.hasPermi('hezhi:careerClass:remove')")
    @Log(title = "职业分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{careerIds}")
    public AjaxResult remove(@PathVariable Long[] careerIds)
    {
        return toAjax(tbCareerClassService.deleteTbCareerClassByCareerIds(careerIds));
    }
}
