package com.ruoyi.web.controller.back;

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

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
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.voluntary.domain.BusiClasses;
import com.ruoyi.voluntary.service.IBusiClassesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 班级Controller
 * 
 * @author voluntary
 * @date 2022-11-20
 */
@RestController
@RequestMapping("/voluntary/busiClasses")
public class BackBusiClassesController extends BaseController {
    @Autowired
    private IBusiClassesService busiClassesService;

    
    /**
     * 查询班级列表
     */
    @PreAuthorize("@ss.hasPermi('voluntary:busiClasses:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusiClasses busiClasses) {
        startPage();
        List<BusiClasses> list = busiClassesService.selectBusiClassesList(busiClasses);
        return getDataTable(list);
    }

    /**
     * 导出班级列表
     */
    @PreAuthorize("@ss.hasPermi('voluntary:busiClasses:export')")
    @Log(title = "班级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusiClasses busiClasses) {
        List<BusiClasses> list = busiClassesService.selectBusiClassesList(busiClasses);
        ExcelUtil<BusiClasses> util = new ExcelUtil<BusiClasses>(BusiClasses.class);
        util.exportExcel(response, list, "班级数据");
    }

    /**
     * 获取班级详细信息
     */
    @PreAuthorize("@ss.hasPermi('voluntary:busiClasses:query')")
    @GetMapping(value = "/{classId}")
    public AjaxResult getInfo(@PathVariable("classId") Long classId) {
        return success(busiClassesService.selectBusiClassesByClassId(classId));
    }

    /**
     * 新增班级
     */
    @PreAuthorize("@ss.hasPermi('voluntary:busiClasses:add')")
    @Log(title = "班级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusiClasses busiClasses) {
        if (StringUtils.isNotEmpty(busiClasses.getName())) {
            // 根据班级名称，批量新增
            String[] classNames = Convert.toStrArray(busiClasses.getName());
            for (int i = 0; i < classNames.length; i++) {
                busiClasses.setName(classNames[i]);
                busiClassesService.insertBusiClasses(busiClasses);
            }
            return toAjax(classNames.length);
        }
        return AjaxResult.error("班级名称不能为空！");
    }

    /**
     * 修改班级
     */
    @PreAuthorize("@ss.hasPermi('voluntary:busiClasses:edit')")
    @Log(title = "班级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusiClasses busiClasses) {
        return toAjax(busiClassesService.updateBusiClasses(busiClasses));
    }

    /**
     * 删除班级
     */
    @PreAuthorize("@ss.hasPermi('voluntary:busiClasses:remove')")
    @Log(title = "班级", businessType = BusinessType.DELETE)
    @DeleteMapping("/{classIds}")
    public AjaxResult remove(@PathVariable Long[] classIds) {
        return toAjax(busiClassesService.deleteBusiClassesByClassIds(classIds));
    }
}
