package com.ym.mec.web.controller.education;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dto.TeacherServeDto;
import com.ym.mec.biz.dal.page.*;
import com.ym.mec.biz.service.*;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.tenant.TenantContextHolder;
import com.ym.mec.util.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.List;

/**
 * @Author Joburgess
 * @Date 2020/4/10
 */
@RequestMapping("eduExercisesSituation")
@Api(tags = "课外训练统计服务")
@RestController
public class EduStudentExtracurricularExercisesSituationController extends BaseController {

    @Autowired
    private StudentExtracurricularExercisesSituationService studentExtracurricularExercisesSituationService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private ExtracurricularExercisesMessageService extracurricularExercisesMessageService;
    @Autowired
    private StudentCourseHomeworkReplyService studentCourseHomeworkReplyService;
    @Autowired
    private StudentServeService studentServeService;
    @Autowired
    private OrganizationService organizationService;

    @ApiOperation(value = "课外训练-教学")
    @GetMapping("/findStudentExtracurricularExercisesSituations")
    public HttpResponseResult findStudentExtracurricularExercisesSituations(StudentExercisesSituationQueryInfo queryInfo){
        queryInfo.setOrganId(organizationService.getEmployeeOrgan(queryInfo.getOrganId()));
        return succeed(studentExtracurricularExercisesSituationService.findStudentExtracurricularExercisesSituations(queryInfo));
    }

    @ApiOperation(value = "服务指标详情")
    @GetMapping("/findServiceStudentDetail")
    public HttpResponseResult findServiceStudentDetail(StudentServiceDetailQueryInfo queryInfo) {
        return succeed(studentExtracurricularExercisesSituationService.findServiceStudentDetail(queryInfo));
    }

    @ApiOperation(value = "教师服务指标明细")
    @GetMapping("/queryTeacherServeInfo")
    public HttpResponseResult queryTeacherServeInfo(TeacherServeQueryInfo queryInfo) {
        queryInfo.setOrganId(organizationService.getEmployeeOrgan(queryInfo.getOrganId()));
        return succeed(studentExtracurricularExercisesSituationService.queryTeacherServeInfo(queryInfo));
    }

    @ApiOperation(value = "回复查询")
    @GetMapping("/findStudentHomeworkComments")
    public Object findStudentHomeworkComments(StudentCourseHomeworkReplyQueryInfo queryInfo){
        if(!queryInfo.isExtra()){
            return succeed(studentCourseHomeworkReplyService.queryPage(queryInfo));
        }else{
            return succeed(extracurricularExercisesMessageService.findStudentExtraExerciseComments(queryInfo));
        }
    }

    @GetMapping("/exercisesSituationStatistics")
    public HttpResponseResult exercisesSituationStatistics(String startDay, String monday){
    	Integer tenantId = TenantContextHolder.getTenantId();
        if(StringUtils.isNotBlank(monday)){
            studentServeService.exercisesSituationStatistics(monday, tenantId);
        }
        if(StringUtils.isNotBlank(startDay)){
            LocalDate date=LocalDate.now();
            if(StringUtils.isNotBlank(startDay)){
                date= LocalDate.parse(startDay, DateUtil.dateFormatter);
            }
            while (date.isBefore(LocalDate.now())){
                date = date.with(DateUtil.weekFields.dayOfWeek(), DayOfWeek.MONDAY.getValue());
                studentServeService.exercisesSituationStatistics(date.format(DateUtil.dateFormatter), tenantId);
                date = date.with(DateUtil.weekFields.dayOfWeek(), DayOfWeek.SUNDAY.getValue()).plusDays(1);
            }
        }
        return succeed();
    }

    @ApiOperation(value = "教师作业布置提醒")
    @PostMapping("/teacherServeRemindPush")
    public HttpResponseResult teacherServeRemindPush(@RequestBody List<TeacherServeDto> teacherServes) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        studentExtracurricularExercisesSituationService.teacherServeRemindPush(teacherServes, sysUser.getId(), sysUser.getRealName());
        return succeed();
    }

    @ApiOperation(value = "教师服务课后作业")
    @GetMapping("/queryTeacherServeHomeworkDetail")
    public HttpResponseResult queryTeacherServeHomeworkDetail(TeacherServeHomeworkQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        return succeed(studentExtracurricularExercisesSituationService.queryTeacherServeHomeworkDetail(queryInfo));
    }

    @ApiOperation(value = "教师服务课外作业")
    @GetMapping("/queryTeacherServeExtraDetail")
    public HttpResponseResult queryTeacherServeExtraDetail(TeacherServeHomeworkQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        return succeed(studentExtracurricularExercisesSituationService.queryTeacherServeExtraDetail(queryInfo));
    }

    @ApiOperation(value = "乐团作业情况")
    @GetMapping("/musicGroupHomeworkStat")
    public HttpResponseResult musicGroupHomeworkStat(String musicGroupId, String startDay, String endDay) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        return succeed(studentServeService.musicGroupHomeworkStat(musicGroupId, null, startDay, endDay));
    }

}
