package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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.system.domain.ContractTable;
import com.ruoyi.system.service.IContractTableService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * B类Controller
 * 
 * @author ruoyi
 * @date 2022-11-25
 */
@RestController
@RequestMapping("/system/contract")
public class ContractTableController extends BaseController
{
    @Autowired
    private IContractTableService contractTableService;

    /**
     * 查询B类列表
     */
    @PreAuthorize("@ss.hasPermi('system:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(ContractTable contractTable)
    {
        startPage();
        List<ContractTable> list = contractTableService.selectContractTableList(contractTable);
        return getDataTable(list);
    }

    /**
     * 导出B类列表
     */
    @PreAuthorize("@ss.hasPermi('system:contract:export')")
    @Log(title = "B类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ContractTable contractTable)
    {
        List<ContractTable> list = contractTableService.selectContractTableList(contractTable);
        ExcelUtil<ContractTable> util = new ExcelUtil<ContractTable>(ContractTable.class);
        util.exportExcel(response, list, "B类数据");
    }

    /**
     * 获取B类详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(contractTableService.selectContractTableById(id));
    }

    /**
     * 新增B类
     */
    @PreAuthorize("@ss.hasPermi('system:contract:add')")
    @Log(title = "B类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ContractTable contractTable)
    {
        return toAjax(contractTableService.insertContractTable(contractTable));
    }

    /**
     * 修改B类
     */
    @PreAuthorize("@ss.hasPermi('system:contract:edit')")
    @Log(title = "B类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ContractTable contractTable)
    {
        return toAjax(contractTableService.updateContractTable(contractTable));
    }

    /**
     * 删除B类
     */
    @PreAuthorize("@ss.hasPermi('system:contract:remove')")
    @Log(title = "B类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(contractTableService.deleteContractTableByIds(ids));
    }

    @Log(title = "B类证书", businessType = BusinessType.IMPORT) // todo
    @PreAuthorize("@ss.hasPermi('collegeManage:studentBase:import')") // todo
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ContractTable> util = new ExcelUtil<>(ContractTable.class); // todo
        List<ContractTable> stuList = util.importExcel(file.getInputStream()); // todo
        String operName = getUsername();
        String message = contractTableService.importCon(stuList, updateSupport, operName); // todo
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<ContractTable> util = new ExcelUtil<>(ContractTable.class); // todo
        util.importTemplateExcel(response, "B类证书信息");
    }

}
