package com.xinyilu.web.controller.studentInfo;

import com.xinyilu.common.annotation.Log;
import com.xinyilu.common.core.controller.BaseController;
import com.xinyilu.common.core.domain.AjaxResult;
import com.xinyilu.common.core.page.TableDataInfo;
import com.xinyilu.common.custominterface.Insert;
import com.xinyilu.common.custominterface.Update;
import com.xinyilu.common.enums.BusinessType;
import com.xinyilu.common.utils.poi.ExcelUtil;
import com.xinyilu.system.domain.dto.StudentInfoDTO;
import com.xinyilu.system.domain.vo.StudentInforImportlVO;
import com.xinyilu.system.service.studentInfo.IStudentInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

/**
 * 学生信息Controller
 *
 * @author jihan
 * @date 2022-08-01
 */
@RestController
@RequestMapping("/system/studentInfo")
public class StudentInfoController extends BaseController {

    @Autowired
    private IStudentInfoService studentInfoService;

    /**
     * Excel文件格式
     */
    private static final String XLS = "xls";

    /**
     * Excel文件格式
     */
    private static final String XLSX = "xlsx";

    /**
     * 查询学生信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:studentInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(StudentInfoDTO studentInfo) {
        return studentInfoService.selectStudentInfoList(studentInfo);
    }

    /**
     * 获取学生信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:studentInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(studentInfoService.selectStudentInfoById(id));
    }

    /**
     * 新增学生信息
     */
    @PreAuthorize("@ss.hasPermi('system:studentInfo:add')")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated(Insert.class) @RequestBody StudentInfoDTO studentInfo) {
        return studentInfoService.insertStudentInfo(studentInfo, getUsername());
    }

    /**
     * 修改学生信息
     */
    @PreAuthorize("@ss.hasPermi('system:studentInfo:edit')")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated(Update.class) @RequestBody StudentInfoDTO studentInfo) {
        return studentInfoService.updateStudentInfo(studentInfo, getUsername());
    }

    /**
     * 删除学生信息
     */
    @PreAuthorize("@ss.hasPermi('system:studentInfo:remove')")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return studentInfoService.deleteStudentInfoByIds(ids);
    }


    /**
     * 导入学生信息集合
     */
    @Log(title = "护理站项目信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file,
                                 @RequestParam(value = "kindergartenId") Long kindergartenId,
                                 @RequestParam(value = "classId") Long classId) throws Exception {
        if (Objects.isNull(file)) {
            return AjaxResult.error("请选择上传Excel文件！");
        }
        //判断文件名是否存在
        if (StringUtils.isBlank(file.getOriginalFilename())) {
            AjaxResult.error("请选择上传Excel文件！");
        }
        //判断文件是否是excel文件
        if (!file.getOriginalFilename().endsWith(XLS) && !file.getOriginalFilename().endsWith(XLSX)) {
            return AjaxResult.error("请选择上传正确格式的Excel文件！");
        }
        ExcelUtil<StudentInforImportlVO> util = new ExcelUtil<>(StudentInforImportlVO.class);
        List<StudentInforImportlVO> list = util.importExcel(file.getInputStream());
        return studentInfoService.importData(list, kindergartenId, classId);
    }
}
