package com.xinelu.manage.controller.agencycategory;

import com.xinelu.common.annotation.Log;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.enums.BusinessType;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.manage.domain.agencycategory.AgencyCategory;
import com.xinelu.manage.service.agencycategory.IAgencyCategoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 机构类别Controller
 *
 * @author xinelu
 * @date 2024-02-26
 */
@RestController
@RequestMapping("/system/agencyCategory")
public class AgencyCategoryController extends BaseController {
    @Resource
    private IAgencyCategoryService agencyCategoryService;

    /**
     * 查询机构类别列表
     */
    @PreAuthorize("@ss.hasPermi('system:agencyCategory:list')")
    @GetMapping("/list")
    public TableDataInfo list(AgencyCategory agencyCategory) {
        startPage();
        List<AgencyCategory> list = agencyCategoryService.selectAgencyCategoryList(agencyCategory);
        return getDataTable(list);
    }

    /**
     * 导出机构类别列表
     */
    @PreAuthorize("@ss.hasPermi('system:agencyCategory:export')")
    @Log(title = "机构类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AgencyCategory agencyCategory) {
        List<AgencyCategory> list = agencyCategoryService.selectAgencyCategoryList(agencyCategory);
        ExcelUtil<AgencyCategory> util = new ExcelUtil<AgencyCategory>(AgencyCategory.class);
        util.exportExcel(response, list, "机构类别数据");
    }

    /**
     * 获取机构类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:agencyCategory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(agencyCategoryService.selectAgencyCategoryById(id));
    }

    /**
     * 新增机构类别
     */
    @PreAuthorize("@ss.hasPermi('system:agencyCategory:add')")
    @Log(title = "机构类别", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody AgencyCategory agencyCategory) {
        if (Objects.isNull(agencyCategory)) {
            return AjaxResult.success();
        }
        return toAjax(agencyCategoryService.insertAgencyCategory(agencyCategory));
    }

    /**
     * 修改机构类别
     */
    @PreAuthorize("@ss.hasPermi('system:agencyCategory:edit')")
    @Log(title = "机构类别", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody AgencyCategory agencyCategory) {
        return toAjax(agencyCategoryService.updateAgencyCategory(agencyCategory));
    }

    /**
     * 删除机构类别
     */
    @PreAuthorize("@ss.hasPermi('system:agencyCategory:remove')")
    @Log(title = "机构类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(agencyCategoryService.deleteAgencyCategoryByIds(ids));
    }

    /**
     * 查询机构类别信息列表 - 树图
     */
    @GetMapping("/agencyCategoryList")
    public AjaxResult agencyCategoryList() {
        return AjaxResult.success(agencyCategoryService.agencyCategoryList());
    }
}
