package com.ruoyi.cms.controller.cms;


import com.ruoyi.cms.util.RoleUtils;
import com.ruoyi.common.core.domain.entity.Company;
import com.ruoyi.cms.service.ICompanyService;
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.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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 lishundong
 * @date 2024-09-04
 */
@RestController
@RequestMapping("/cms/company")
@Api(tags = "后台：公司管理")
public class CompanyController extends BaseController
{
    @Autowired
    private ICompanyService companyService;

    /**
     * 查询公司列表
     */
    @ApiOperation("查询公司列表")
//    @PreAuthorize("@ss.hasPermi('cms:company:list')")
    @GetMapping("/list")
    public TableDataInfo list(Company company)
    {
        if (RoleUtils.isCompanyAdmin()) {
            company.setCode(RoleUtils.getCurrentUseridCard());
        }
        startPage();
        List<Company> list = companyService.selectCompanyList(company);
        return getDataTable(list);
    }

    /**
     * 导出公司列表
     */
    @ApiOperation("导出公司列表")
//    @PreAuthorize("@ss.hasPermi('app:company:export')")
    @Log(title = "公司", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Company company)
    {
        List<Company> list = companyService.selectCompanyList(company);
        ExcelUtil<Company> util = new ExcelUtil<Company>(Company.class);
        util.exportExcel(response, list, "公司数据");
    }

    /**
     * 获取公司详细信息
     */
    @ApiOperation("获取公司详细信息")
//    @PreAuthorize("@ss.hasPermi('app:company:query')")
    @GetMapping(value = "/{companyId}")
    public AjaxResult getInfo(@PathVariable("companyId") Long companyId)
    {
        return success(companyService.selectCompanyByCompanyId(companyId));
    }

    /**
     * 新增公司
     */
    @ApiOperation("新增公司")
//    @PreAuthorize("@ss.hasPermi('app:company:add')")
    @Log(title = "公司", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Company company)
    {
        return toAjax(companyService.insertCompany(company));
    }

    /**
     * 修改公司
     */
    @ApiOperation("修改公司")
//    @PreAuthorize("@ss.hasPermi('app:company:edit')")
    @Log(title = "公司", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Company company)
    {
        return toAjax(companyService.updateCompany(company));
    }

    /**
     * 删除公司
     */
    @ApiOperation("删除公司")
//    @PreAuthorize("@ss.hasPermi('app:company:remove')")
    @Log(title = "公司", businessType = BusinessType.DELETE)
    @DeleteMapping("/{companyIds}")
    public AjaxResult remove(@PathVariable Long[] companyIds)
    {
        return toAjax(companyService.deleteCompanyByCompanyIds(companyIds));
    }

    @ApiOperation("企业资质审核列表")
    @PreAuthorize("@ss.hasPermi('app:company:approval:list')")
    @GetMapping("/approval/list")
    public TableDataInfo approvalList(Company company)
    {
        startPage();
        List<Company> list = companyService.approvalList(company);
        return getDataTable(list);
    }
    @ApiOperation("企业资质修改")
//    @PreAuthorize("@ss.hasPermi('app:company:approval:edit')")
    @GetMapping("/approval/edit")
    public AjaxResult approvalEdit(Company company)
    {
        startPage();
        List<Company> list = companyService.approvalList(company);
        return AjaxResult.success();
    }

    @ApiOperation("查询公司列表")
//    @PreAuthorize("@ss.hasPermi('cms:company:list')")
    @GetMapping("/listPage")
    public TableDataInfo listPage(Company company)
    {
        startPage();
        List<Company> list = companyService.selectCompanyList(company);
        return getDataTable(list);
    }
}
