package com.srs.web.controller.survey;

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

import com.srs.comprehensive.domain.ViewStuInfo;
import com.srs.survey.domain.dto.LeaveExport;
import com.srs.survey.domain.dto.LeaveRes;
import com.srs.survey.domain.dto.ReturnRes;
import com.srs.survey.domain.dto.XwReturnRes;
import com.srs.survey.domain.vo.SurItineraryStatisticsVo;
import com.srs.survey.service.ISurSurveyService;
import com.srs.system.domain.SysDeptMap;
import com.srs.system.service.ISysDeptMapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;

import com.srs.survey.domain.SurItinerary;
import com.srs.survey.service.ISurItineraryService;
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-03-14
 */
@RestController
@RequestMapping("/survey/itinerary")
@Api(value = "学生假期返校管理", tags = "学生假期返校管理")
public class SurItineraryController extends BaseController {
    @Autowired
    private ISurItineraryService surItineraryService;
    @Autowired
    private ISurSurveyService surSurveyService;

    @PreAuthorize("@ss.hasPermi('survey:itinerary:listXg')")
    @Log(title = "学工导出学生假期填写记录", businessType = BusinessType.EXPORT)
    @PostMapping("/xgExport")
    @ApiOperation("学工导出学生假期填写记录")
    public void xgExport(HttpServletResponse response, SurItinerary param)
    {
        List<SurItinerary> list = surItineraryService.listView(param);
        ExcelUtil<SurItinerary> util = new ExcelUtil<SurItinerary>(SurItinerary.class);
        util.exportExcel(response, list, "学生假期返校数据");
    }


    @PreAuthorize("@ss.hasPermi('survey:itinerary:listFdyAll')")
    @Log(title = "辅导员导出学生假期填写记录", businessType = BusinessType.EXPORT)
    @PostMapping("/fdyExport")
    @ApiOperation("辅导员导出学生假期填写记录")
    public void fdyExport(HttpServletResponse response, SurItinerary param)
    {
        param.tNo = getUsername();
        List<SurItinerary> list = surItineraryService.listView(param);
        ExcelUtil<SurItinerary> util = new ExcelUtil<SurItinerary>(SurItinerary.class);
        util.exportExcel(response, list, "学生假期返校数据");
    }

    @PreAuthorize("@ss.hasPermi('survey:itinerary:listFdyAll')")
    @GetMapping("/listFdyAll")
    @ApiOperation("辅导员查询待审核")
    public TableDataInfo listFdyAll(SurItinerary param){
        param.tNo = getUsername();
        startPage();
        List<SurItinerary> list = surItineraryService.listView(param);
        return getDataTable(list);
    }


    @GetMapping("/countFdyClassWillLeave/{surveyId}")
    @ApiOperation("统计预计离校")
    public AjaxResult countFdyClassWillLeave(@PathVariable Long surveyId) {
        List<Map<String, Object>> list = surItineraryService.countFdyClassWillLeave(surveyId,getUsername());
        return AjaxResult.success(list);
    }

    @GetMapping("/listFdyStuReturn")
    @ApiOperation("列出辅导员学生返校填写情况")
    public Map<String,Object> listFdyStuReturn(SurItinerary param){
        return surItineraryService.listFdyStuReturn(param);
    }

    @PostMapping("/sendFdyStuReturnMsg")
    @ApiOperation("辅导员向未填写返校的学生发送企业微信消息")
    public AjaxResult sendFdyStuReturnMsg(@RequestBody SurItinerary param){
        return surItineraryService.sendFdyStuReturnMsg(param);
    }


    @PostMapping("/sendFdyStuLeaveMsg")
    @ApiOperation("辅导员向未填写离校的学生发送企业微信消息")
    public AjaxResult sendFdyStuLeaveMsg(@RequestBody SurItinerary param){
        return surItineraryService.sendFdyStuLeaveMsg(param);
    }

    @GetMapping("/listFdyStuLeave")
    @ApiOperation("列出辅导员学生离校填写情况")
    public Map<String,Object> listFdyStuLeave(SurItinerary param){
        return surItineraryService.listFdyStuLeave(param);
    }



    @PreAuthorize("@ss.hasPermi('survey:itinerary:listXg')")
    @GetMapping("/listXg")
    @ApiOperation("学工查询")
    public TableDataInfo listXg(SurItinerary param){
        startPage();
        List<SurItinerary> list = surItineraryService.listView(param);
        return getDataTable(list);
    }


    @PreAuthorize("@ss.hasPermi('survey:survey:list')")
    @GetMapping("/countWillLeave/{surveyId}")
    @ApiOperation("统计预计离校")
    public AjaxResult countWillLeave(@PathVariable Long surveyId) {
        List<Map<String, Object>> list = surItineraryService.countWillLeave(surveyId);
        return AjaxResult.success(list);
    }


    @PreAuthorize("@ss.hasPermi('survey:survey:list')")
    @GetMapping("/listSubmitRes/{surveyId}")
    @ApiOperation("列出学生假期离校结果")
    public AjaxResult listSubmitRes(@PathVariable Long surveyId) {
        List<Map<String, Object>> list = surItineraryService.listSubmitRes(surveyId);
        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('survey:survey:list')")
    @GetMapping("/listLeaveRes/{surveyId}")
    @ApiOperation("列出学生假期离校结果")
    public AjaxResult listLeaveRes(@PathVariable Long surveyId) {
        List<LeaveRes> list = surItineraryService.listLeaveRes(surveyId);
        return AjaxResult.success(list);
    }


    @PostMapping("/exportLeaveRes/{surveyId}")
    @ApiOperation("导出学生假期返校结果")
    public void exportLeaveRes(HttpServletResponse response,@PathVariable Long surveyId)
    {
        List<LeaveRes> list = surItineraryService.listLeaveRes(surveyId);
        ExcelUtil<LeaveRes> util = new ExcelUtil<LeaveRes>(LeaveRes.class);
        util.exportExcel(response, list, "学生假期返校数据");
    }

    @PostMapping("/exportReturnRes/{surveyId}")
    @ApiOperation("导出学生假期返校结果")
    public void exportReturnRes(HttpServletResponse response,@PathVariable Long surveyId)
    {
        List<ReturnRes> list = surItineraryService.listReturnRes(surveyId);
        ExcelUtil<ReturnRes> util = new ExcelUtil<ReturnRes>(ReturnRes.class);
        util.exportExcel(response, list, "学生假期返校数据");
    }

    @PreAuthorize("@ss.hasPermi('survey:survey:list')")
    @GetMapping("/listReturnRes1/{surveyId}")
    @ApiOperation("列出学生假期离校结果")
    public AjaxResult listReturnRes1(@PathVariable Long surveyId) {
        List<Map<String, Object>> list = surItineraryService.listReturnRes1(surveyId);
        return AjaxResult.success(list);
    }



    @GetMapping("/getStuCampus")
    @ApiOperation("获取学生所在校区")
    public AjaxResult getStuCampus(){
        return surItineraryService.getStuCampus();
    }



    @PostMapping("/updateStuHomeGpsAddr")
    @ApiOperation("更新学生到家详细地址")
    public AjaxResult updateStuHomeGpsAddr(@RequestBody SurItinerary param){
        return surItineraryService.updateStuHomeGpsAddr(param);
    }


    @GetMapping("/listOwnReturn")
    @ApiOperation("学生查询")
    public AjaxResult listOwnReturn(SurItinerary param) {
        param.stuNo = getUsername();
        startPage();
        List<SurItinerary> list = surItineraryService.listView(param);
        return success(list);
    }


    @PostMapping("/stuSchoolUpdate")
    @ApiOperation("学生返校更新")
    public AjaxResult stuSchoolUpdate(@RequestBody SurItinerary param){
        return surItineraryService.stuSchoolUpdate(param);
    }

    @PreAuthorize("@ss.hasPermi('survey:itinerary:returnAudit')")
    @PostMapping("/manyReturnAudit")
    @ApiOperation("辅导员批量返校审核通过")
    public AjaxResult manyReturnAudit(@RequestBody Long[] ids){
        return surItineraryService.manyReturnAudit(ids);
    }

    @PreAuthorize("@ss.hasPermi('survey:itinerary:returnAudit')")
    @PostMapping("/returnAudit")
    @ApiOperation("辅导员返校审核")
    public AjaxResult returnAudit(@RequestBody SurItinerary param){
        return surItineraryService.returnAudit(param);
    }

    @PostMapping("/stuWillReturnUpdate")
    @ApiOperation("学生计划返校更新")
    public AjaxResult stuWillReturnUpdate(@RequestBody SurItinerary param){
        return surItineraryService.stuWillReturnUpdate(param);
    }

    @PostMapping("/boolStuOwnReturn/{id}")
    @ApiOperation("判断返校记录是否是学生自己的")
    public AjaxResult boolStuOwnReturn(@PathVariable Long id) {
        return surItineraryService.boolStuOwnReturn(id);
    }

    @PreAuthorize("@ss.hasPermi('survey:itinerary:leaveAudit')")
    @PostMapping("/manyLeaveAudit")
    @ApiOperation("辅导员批量离校审核通过")
    public AjaxResult manyLeaveAudit(@RequestBody Long[] ids){
        return surItineraryService.manyLeaveAudit(ids);
    }

    @PreAuthorize("@ss.hasPermi('survey:itinerary:leaveAudit')")
    @PostMapping("/manyLeaveReject")
    @ApiOperation("辅导员批量离校审核驳回")
    public AjaxResult manyLeaveReject(@RequestBody Long[] ids){
        return surItineraryService.manyLeaveReject(ids);
    }

    @PreAuthorize("@ss.hasPermi('survey:itinerary:leaveAudit')")
    @PostMapping("/leaveAudit")
    @ApiOperation("辅导员离校审核")
    public AjaxResult leaveAudit(@RequestBody SurItinerary param){
        return surItineraryService.leaveAudit(param);
    }

    @Autowired
    ISysDeptMapService _deptMap;

    @PreAuthorize("@ss.hasPermi('survey:itinerary:listXw')")
    @GetMapping("/listXwReturnRes1/{surveyId}")
    @ApiOperation("学务列出学生返校情况")
    public AjaxResult listXwReturnRes1(@PathVariable Long surveyId) {
        Long oldId = getDeptId();
        SysDeptMap dept = _deptMap.getOldDeptByNewId(oldId);
        Long deptId = null;
        if (dept != null) {
            deptId = dept.getOldDeptId();
        }
        List<Map<String, Object>> list = surItineraryService.listXwReturnRes1(surveyId,deptId);
        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('survey:itinerary:listXw')")
    @GetMapping("/listXwSubmitRes/{surveyId}")
    @ApiOperation("学务列出到家人数")
    public AjaxResult listXwSubmitRes(@PathVariable Long surveyId) {
        Long oldId = getDeptId();
        SysDeptMap dept = _deptMap.getOldDeptByNewId(oldId);
        Long deptId = null;
        if (dept != null) {
            deptId = dept.getOldDeptId();
        }
        List<Map<String, Object>> list = surItineraryService.listXwSubmitRes(surveyId,deptId);
        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('survey:itinerary:listXw')")
    @GetMapping("/countXwWillLeave/{surveyId}")
    @ApiOperation("学院统计预计离校")
    public AjaxResult countXwWillLeave(@PathVariable Long surveyId) {
        Long oldId = getDeptId();
        SysDeptMap dept = _deptMap.getOldDeptByNewId(oldId);
        Long deptId = null;
        if (dept != null) {
            deptId = dept.getOldDeptId();
        }
        List<Map<String, Object>> list = surItineraryService.countXwWillLeave(surveyId,deptId);
        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('survey:itinerary:listXw')")
    @PostMapping("/exportXwReturnRes")
    @ApiOperation("学务导出学生假期返校结果")
    public void exportXwReturnRes(HttpServletResponse response, SurItinerary param)
    {
        Long oldId = getDeptId();
        SysDeptMap dept = _deptMap.getOldDeptByNewId(oldId);
        Long deptId = null;
        if (dept != null) {
            deptId = dept.getOldDeptId();
        }
        param.setDeptId(deptId);
        List<XwReturnRes> list = surItineraryService.listXwReturnRes(param);
        ExcelUtil<XwReturnRes> util = new ExcelUtil<XwReturnRes>(XwReturnRes.class);
        util.exportExcel(response, list, "学生假期返校结果");
    }

    @PreAuthorize("@ss.hasPermi('survey:itinerary:listXw')")
    @Log(title = "学务导出学生假期填写记录", businessType = BusinessType.EXPORT)
    @PostMapping("/xwExport")
    @ApiOperation("学务导出学生假期填写记录")
    public void xwExport(HttpServletResponse response, SurItinerary param)
    {
        Long oldId = getDeptId();
        SysDeptMap dept = _deptMap.getOldDeptByNewId(oldId);
        Long deptId = null;
        if (dept != null) {
            deptId = dept.getOldDeptId();
        }
        param.setDeptId(deptId);
        List<SurItinerary> list = surItineraryService.listView(param);
        ExcelUtil<SurItinerary> util = new ExcelUtil<SurItinerary>(SurItinerary.class);
        util.exportExcel(response, list, "学生假期返校数据");
    }

    @PreAuthorize("@ss.hasPermi('survey:itinerary:listXw')")
    @GetMapping("/listXw")
    @ApiOperation("学院数据")
    public TableDataInfo listXw(SurItinerary param){
        Long oldId = getDeptId();
        SysDeptMap dept = _deptMap.getOldDeptByNewId(oldId);
        Long deptId = null;
        if (dept != null) {
            deptId = dept.getOldDeptId();
        }
        param.setDeptId(deptId);
        startPage();
        List<SurItinerary> list = surItineraryService.listView(param);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('survey:itinerary:listFdyReturn')")
    @GetMapping("/listFdyReturn")
    @ApiOperation("辅导员查询待审核")
    public TableDataInfo listFdyReturn(SurItinerary param){
        param.tNo = getUsername();
        param.status = "1";
        startPage();
        List<SurItinerary> list = surItineraryService.listView(param);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('survey:itinerary:listFdy')")
    @GetMapping("/listFdy")
    @ApiOperation("辅导员查询待审核")
    public TableDataInfo listFdy(SurItinerary param){
        param.tNo = getUsername();
        param.leaveStatus = "1";
        startPage();
        List<SurItinerary> list = surItineraryService.listView(param);
        return getDataTable(list);
    }

    @PostMapping("/stuHomeUpdate")
    @ApiOperation("学生到家更新")
    public AjaxResult stuHomeUpdate(@RequestBody SurItinerary param){
        return surItineraryService.stuHomeUpdate(param);
    }


    @PostMapping("/stuLeaveApply")
    @ApiOperation("学生申请离校")
    public AjaxResult stuLeaveApply(@RequestBody SurItinerary param) {
        param.leaveStatus = "1";
        return surItineraryService.stuLeaveApply(param);
    }


    @GetMapping("/stuGetRecordBySvId/{svId}")
    @ApiOperation("学生通过项目id获取记录")
    public AjaxResult stuGetRecordBySvId(@PathVariable Long svId) {
        return surItineraryService.stuGetRecordBySvId(svId);
    }


    /**
     * 查询学生假期返校列表
     */
    @PreAuthorize("@ss.hasPermi('survey:itinerary:list')")
    @GetMapping("/list")
    @ApiOperation("查询学生假期返校列表")
    public TableDataInfo list(SurItinerary surItinerary)
    {
        startPage();
        List<SurItinerary> list = surItineraryService.selectSurItineraryList(surItinerary);
        return getDataTable(list);
    }

    /**
     * 导出学生假期返校列表
     */
    @PreAuthorize("@ss.hasPermi('survey:itinerary:export')")
    @Log(title = "学生假期返校", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出学生假期返校列表")
    public void export(HttpServletResponse response, SurItinerary surItinerary)
    {
        List<SurItinerary> list = surItineraryService.selectSurItineraryList(surItinerary);
        ExcelUtil<SurItinerary> util = new ExcelUtil<SurItinerary>(SurItinerary.class);
        util.exportExcel(response, list, "学生假期返校数据");
    }
    /**
     * 导出学生假期离校
     */
//    @PreAuthorize("@ss.hasPermi('survey:itinerary:LeavExport')")
    @Log(title = "学生假期离校填写", businessType = BusinessType.EXPORT)
    @PostMapping("/LeavExport")
    @ApiOperation("导出学生假期离校填写列表")
    public void LeavExport(HttpServletResponse response, SurItinerary param)
    {
        param.pageSize=100000L;
        param.tNo=getUsername();
        param.isLeave="1";
        Map<String,Object> result=surItineraryService.listFdyStuLeave(param);
        ExcelUtil<LeaveExport> util = new ExcelUtil<LeaveExport>(LeaveExport.class);
        List<LeaveExport> listExport=new ArrayList<>();
        List<ViewStuInfo> rows = (List<ViewStuInfo>) result.get("rows");
        for (ViewStuInfo student : rows) {
            LeaveExport entity=new LeaveExport();
            // 逐条取出每个学生的数据
            Long surveyId=param.getSurveyId();
            String surveyName =surveyId==null?"":surSurveyService.selectSurSurveyBySurveyId(surveyId).getSurveyName();
            String className =student.getClassName();
            String stuNo =student.getStuNo();
            String stuName =student.getStuName();
            String status =student.getStatus();
            String editStatus =student.getEditStatus();
            entity.setSurveyName(surveyName);
            entity.setClassName(className);
            entity.setStuNo(stuNo);
            entity.setName(stuName);
            if(status.equals("01")) {
                entity.setStatus("在校");
            }
            entity.setEditStatus(editStatus);
            listExport.add(entity);
        }
        util.exportExcel(response, listExport, "学生假期离校填写数据");
    }

    /**
     * 获取学生假期返校详细信息
     */
    @PreAuthorize("@ss.hasPermi('survey:itinerary:query')")
    @GetMapping(value = "/{returnSchoolId}")
    @ApiOperation("获取学生假期返校详细信息")
    public AjaxResult getInfo(@PathVariable("returnSchoolId") Long returnSchoolId)
    {
        return success(surItineraryService.selectSurItineraryByReturnSchoolId(returnSchoolId));
    }

    /**
     * 新增学生假期返校
     */
    @PreAuthorize("@ss.hasPermi('survey:itinerary:add')")
    @Log(title = "学生假期返校", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增学生假期返校")
    public AjaxResult add(@RequestBody SurItinerary surItinerary)
    {
        return toAjax(surItineraryService.insertSurItinerary(surItinerary));
    }

    /**
     * 修改学生假期返校
     */
    @Log(title = "学生假期返校", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改学生假期返校")
    public AjaxResult edit(@RequestBody SurItinerary surItinerary)
    {
        return toAjax(surItineraryService.updateById(surItinerary));
    }

    /**
     * 审核学生假期返校
     */
    @GetMapping("/auditItinerary/{returnSchoolId}")
    @ApiOperation("审核学生假期返校")
    public AjaxResult auditItinerary(@PathVariable("returnSchoolId") Long returnSchoolId)
    {
        return toAjax(surItineraryService.auditItinerary(returnSchoolId));
    }
    /**
     * 批量审核学生假期返校
     */
    @GetMapping("/batchAuditItinerary/{returnSchoolIds}")
    @ApiOperation("审核学生假期返校")
    public AjaxResult batchAuditItinerary(@PathVariable Long[] returnSchoolIds)
    {
        return toAjax(surItineraryService.batchAuditItinerary(returnSchoolIds));
    }

    @GetMapping("/statistics/{surveyId}")
    public AjaxResult getItineraryStatistics(@PathVariable Long surveyId) {
        List<SurItineraryStatisticsVo> statistics = surItineraryService.getItineraryStatistics(surveyId);
        return AjaxResult.success(statistics);
    }
}
