package com.ruoyi.hezhi.controller;

import cn.hutool.core.util.ObjectUtil;
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.exception.ServiceException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hezhi.domain.TbStudent;
import com.ruoyi.hezhi.domain.dto.ImportStudentDTO;
import com.ruoyi.hezhi.service.ITbStudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 学员Controller
 *
 * @author ruoyi
 * @date 2024-10-21
 */
@RestController
@RequestMapping("/hezhi/student")
public class TbStudentController extends BaseController
{
    @Autowired
    private ITbStudentService tbStudentService;

    /**
     * 查询学员列表
     */
    @PreAuthorize("@ss.hasPermi('hezhi:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbStudent tbStudent)
    {
        startPage();
        List<TbStudent> list = tbStudentService.selectTbStudentList(tbStudent);
        return getDataTable(list);
    }

    /**
     * 导出学员列表
     */
    @PreAuthorize("@ss.hasPermi('hezhi:student:export')")
    @Log(title = "学员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbStudent tbStudent)
    {
        List<TbStudent> list = tbStudentService.selectTbStudentList(tbStudent);
        ExcelUtil<TbStudent> util = new ExcelUtil<TbStudent>(TbStudent.class);
        util.exportExcel(response, list, "学员数据");
    }

    @Log(title = "学员列表", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<ImportStudentDTO> util = new ExcelUtil<ImportStudentDTO>(ImportStudentDTO.class);
        List<ImportStudentDTO> userList = util.importExcel(file.getInputStream());
        String message = tbStudentService.importUser(userList);
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        List<ImportStudentDTO> list = new ArrayList<>();
        ExcelUtil<ImportStudentDTO> util = new ExcelUtil<ImportStudentDTO>(ImportStudentDTO.class);
        util.exportExcel(response, list, "学员数据");
    }

    /**
     * 获取学员详细信息
     */
    @PreAuthorize("@ss.hasPermi('hezhi:student:query')")
    @GetMapping(value = "/{studentId}")
    public AjaxResult getInfo(@PathVariable("studentId") Long studentId)
    {
        return success(tbStudentService.selectTbStudentByStudentId(studentId));
    }

    /**
     * 新增学员
     */
    @PreAuthorize("@ss.hasPermi('hezhi:student:add')")
    @Log(title = "学员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbStudent tbStudent)
    {
        try{
            return toAjax(tbStudentService.insertTbStudent(tbStudent));
        } catch (ServiceException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改学员
     */
    @PreAuthorize("@ss.hasPermi('hezhi:student:edit')")
    @Log(title = "学员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbStudent tbStudent)
    {
        return toAjax(tbStudentService.updateTbStudent(tbStudent));
    }

    /**
     * 删除学员
     */
    @PreAuthorize("@ss.hasPermi('hezhi:student:remove')")
    @Log(title = "学员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{studentIds}")
    public AjaxResult remove(@PathVariable Long[] studentIds)
    {
        return toAjax(tbStudentService.deleteTbStudentByStudentIds(studentIds));
    }
}
