package com.srs.web.controller.routine;

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

import com.srs.common.utils.poi.ExcelExp;
import com.srs.common.utils.poi.ExcelUtilManySheet;
import com.srs.routine.domain.SysDisMate;
import com.srs.routine.domain.dto.SysDisBasicDao;
import com.srs.routine.domain.dto.SysDisMateDao;
import com.srs.teacher.domain.SysPerformance;
import com.srs.teacher.domain.SysTeacherThesis;
import com.srs.teacher.domain.dto.SysPerformanceDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
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 io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;

import com.srs.routine.domain.SysDisBasic;
import com.srs.routine.service.ISysDisBasicService;
import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.common.enums.BusinessType;
import com.srs.common.annotation.Log;
import com.srs.common.core.page.TableDataInfo;

/**
 * 退伍复学申请Controller
 *
 * @author srs
 * @date 2025-11-06
 */
@RestController
@RequestMapping("/routine/basic")
@Api(value = "退伍复学申请管理", tags = "退伍复学申请管理")
public class SysDisBasicController extends BaseController {
    @Autowired
    private ISysDisBasicService sysDisBasicService;

    @Value("${srs.url}")
    private String baseUrl;

    /**
     * 查询退伍复学申请列表
     */
@PreAuthorize("@ss.hasPermi('routine:basic:list')")
@GetMapping("/list")
@ApiOperation("查询退伍复学申请列表")
    public TableDataInfo list(SysDisBasic sysDisBasic)
    {
        startPage();
        List<SysDisBasic> list = sysDisBasicService.selectSysDisBasicList(sysDisBasic);
        return getDataTable(list);
    }

    /**
     * 导出退伍复学申请列表
     */
    @PreAuthorize("@ss.hasPermi('routine:basic:export')")
    @Log(title = "退伍复学申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出退伍复学申请列表")
    public void export(HttpServletResponse response, SysDisBasic sysDisBasic) throws IOException {
        List<SysDisBasic> list = sysDisBasicService.selectSysDisBasicList(sysDisBasic);
//      基础材料
        List<SysDisBasicDao> sysDisBasicDaos = new ArrayList<>();
//        转专业
        List<SysDisMateDao> sysDisMateDaos = new ArrayList<>();

//        基础材料
        for (SysDisBasic item : list){
            SysDisBasicDao dao = new SysDisBasicDao();
            dao.setStId(item.getStId());
            dao.setStName(item.getStName());
            dao.setCollegeInfo(item.getCollegeInfo());
            dao.setGradeInfo(item.getGradeInfo());
            dao.setMajorInfo(item.getMajorInfo());
//            dao.setMajors(item.getMajors());
            dao.setClassInfo(item.getClassInfo());
//            dao.setCollege(item.getCollege());
            dao.setSex(item.getSex());
            dao.setNations(item.getNations());
//            dao.setStClass(item.getStClass());
//            dao.setMajors(item.getMajors());
//            dao.setGrade(item.getGrade());
//            dao.setCollege(item.getCollege());
            dao.setReasons(item.getReasons());
            dao.setConversion(item.getConversion());


            sysDisBasicDaos.add(dao);
        }

//        转专业材料
        for(SysDisBasic item :list){
            SysDisMateDao dao1 = new SysDisMateDao();
            dao1.setStId(item.getStId());
            dao1.setStName(item.getStName());
            dao1.setCollege(item.getCollege());
            for (SysDisMate mate : item.getMaList()){
                dao1.setOldgrade(mate.getOldgrade());
                dao1.setOldmajor(mate.getOldmajor());
                dao1.setProof(baseUrl.substring(0,baseUrl.length()-1) + mate.getProof());
                dao1.setIdcard(baseUrl.substring(0,baseUrl.length()-1) + mate.getIdcard());
                dao1.setMaterial(baseUrl.substring(0,baseUrl.length()-1) + mate.getMaterial());
            }
            sysDisMateDaos.add(dao1);
        }

        ExcelExp e1 = new ExcelExp("基础材料", sysDisBasicDaos, SysDisBasicDao.class);
        ExcelExp e2 = new ExcelExp("转专业材料", sysDisMateDaos, SysDisMateDao.class);
        List<ExcelExp> mysheet = new ArrayList<ExcelExp>();
        mysheet.add(e1);
        mysheet.add(e2);
        ExcelUtilManySheet<List<SysDisBasic>> util2 = new ExcelUtilManySheet<List<SysDisBasic>>(mysheet);
        util2.exportExcelManySheet(response, mysheet);

//        ExcelUtil<SysDisBasic> util = new ExcelUtil<SysDisBasic>(SysDisBasic.class);
//        util.exportExcel(response, list, "退伍复学申请数据");

    }

    /**
     * 获取退伍复学申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('routine:basic:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取退伍复学申请详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysDisBasicService.selectSysDisBasicById(id));
    }

//    根据学号查询信息
    @PreAuthorize("@ss.hasPermi('routine:basic:query')")
    @GetMapping(value = "/name/{stName}")
    public AjaxResult getStNameInfo(@PathVariable("stName") String stName)
    {
        return success(sysDisBasicService.selectSysDisBasicByStName(stName));
    }

//    根据流程ID查询流程信息
//    selectSysDisBasicByProcessInstanceId
    @PreAuthorize("@ss.hasPermi('routine:basic:query')")
    @GetMapping(value = "/processId/{processId}")
    public AjaxResult getProcessId(@PathVariable("processId") String processId)
    {
        return success(sysDisBasicService.selectSysDisBasicByProcessInstanceId(processId));
    }

    /**
     * 新增退伍复学申请
     */
    @PreAuthorize("@ss.hasPermi('routine:basic:add')")
    @Log(title = "退伍复学申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增退伍复学申请")
    public AjaxResult add(@RequestBody SysDisBasic sysDisBasic)
    {
        return toAjax(sysDisBasicService.insertSysDisBasic(sysDisBasic));
    }

    /**
     * 修改退伍复学申请
     */
    @PreAuthorize("@ss.hasPermi('routine:basic:edit')")
    @Log(title = "退伍复学申请", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改退伍复学申请")
    public AjaxResult edit(@RequestBody SysDisBasic sysDisBasic)
    {
        return toAjax(sysDisBasicService.updateSysDisBasic(sysDisBasic));
    }

    /**
     * 删除退伍复学申请
     */
    @PreAuthorize("@ss.hasPermi('routine:basic:remove')")
    @Log(title = "退伍复学申请", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    @ApiOperation("删除退伍复学申请")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysDisBasicService.deleteSysDisBasicByIds(ids));
    }
}
