package com.pangu.student.controller;

import com.alibaba.excel.EasyExcel;
import com.pangu.common.core.controller.BaseController;
import com.pangu.common.core.domain.AjaxResult;
import com.pangu.common.core.page.TableDataInfo;
import com.pangu.student.domain.dto.StudentDTO;
import com.pangu.student.domain.dto.StudentImportDTO;
import com.pangu.student.domain.vo.ImportResultVO;
import com.pangu.student.domain.vo.StudentVO;
import com.pangu.student.service.IStudentService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 学生管理Controller
 * 
 * @author pangu
 */
@RestController
@RequestMapping("/api/student")
@RequiredArgsConstructor
public class StudentController extends BaseController {

    private final IStudentService studentService;

    /**
     * 查询学生列表
     */
    @GetMapping("/list")
    public TableDataInfo list(StudentDTO studentDTO) {
        return studentService.selectStudentList(studentDTO);
    }

    /**
     * 获取学生详情
     */
    @GetMapping("/{studentId}")
    public AjaxResult getInfo(@PathVariable Long studentId) {
        StudentVO studentVO = studentService.getStudentById(studentId);
        return success(studentVO);
    }

    /**
     * 新增学生
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody StudentDTO studentDTO) {
        return toAjax(studentService.insertStudent(studentDTO));
    }

    /**
     * 修改学生
     */
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody StudentDTO studentDTO) {
        return toAjax(studentService.updateStudent(studentDTO));
    }

    /**
     * 删除学生
     */
    @DeleteMapping("/{studentId}")
    public AjaxResult remove(@PathVariable Long studentId) {
        return toAjax(studentService.deleteStudent(studentId));
    }
    
    /**
     * 绑定会员
     */
    @PostMapping("/bindMember")
    public AjaxResult bindMember(@RequestBody StudentDTO studentDTO) {
        studentService.bindMember(studentDTO.getStudentId(), studentDTO.getMemberId());
        return success("绑定成功");
    }

    /**
     * 批量导入学生
     */
    @PostMapping("/import")
    public AjaxResult importData(@RequestParam("file") MultipartFile file) {
        ImportResultVO result = studentService.importStudents(file);
        return success(result);
    }

    /**
     * 下载导入模板
     */
    @GetMapping("/template")
    public void downloadTemplate(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("学生导入模板", StandardCharsets.UTF_8).replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        
        // 创建模板数据
        List<StudentImportDTO> templateData = new ArrayList<>();
        StudentImportDTO example = new StudentImportDTO();
        example.setStudentName("张小明");
        example.setStudentNo("STU20260001");
        example.setMemberPhone("13812345678");
        example.setRegionPath("湖北省-武汉市-武昌区");
        example.setSchoolName("武汉市第一中学");
        example.setGradeName("七年级");
        example.setClassName("1班");
        example.setGender("男");
        example.setBirthday("2015-03");
        templateData.add(example);
        
        EasyExcel.write(response.getOutputStream(), StudentImportDTO.class)
            .sheet("学生信息")
            .doWrite(templateData);
    }

    /**
     * 检查学号是否唯一
     */
    @GetMapping("/checkStudentNo")
    public AjaxResult checkStudentNo(String studentNo, Long studentId) {
        boolean unique = studentService.checkStudentNoUnique(studentNo, studentId);
        return success(unique);
    }
}
