package com.srs.web.controller.survey;

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.LeaveRes;
import com.srs.survey.domain.dto.ReturnRes;
import com.srs.survey.domain.dto.SubmitRes;
import com.srs.survey.domain.vo.SurItineraryStatisticsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
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;

    @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("/leaveAudit")
    @ApiOperation("辅导员离校审核")
    public AjaxResult leaveAudit(@RequestBody SurItinerary param){
        return surItineraryService.leaveAudit(param);
    }

    @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);
    }

    @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: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));
    }

    /**
     * 修改学生假期返校
     */
    @PreAuthorize("@ss.hasPermi('survey:itinerary:edit')")
    @Log(title = "学生假期返校", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改学生假期返校")
    public AjaxResult edit(@RequestBody SurItinerary surItinerary)
    {
        return toAjax(surItineraryService.updateSurItinerary(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);
    }
}
