package com.ym.mec.web.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.EmployeeDao;
import com.ym.mec.biz.dal.dto.TeacherServeDto;
import com.ym.mec.biz.dal.entity.Employee;
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.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.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

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

    @Autowired
    private StudentExtracurricularExercisesSituationService studentExtracurricularExercisesSituationService;

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private ExtracurricularExercisesMessageService extracurricularExercisesMessageService;

    @Autowired
    private StudentCourseHomeworkReplyService studentCourseHomeworkReplyService;

    @Autowired
    private StudentServeService studentServeService;

    @Autowired
    private EmployeeDao employeeDao;

    @Autowired
    private EmployeeService employeeService;

    @ApiOperation(value = "课外训练-教学")
    @GetMapping("/findStudentExtracurricularExercisesSituations")
    @PreAuthorize("@pcs.hasPermissions('exercisesSituation/findStudentExtracurricularExercisesSituations')")
    public HttpResponseResult findStudentExtracurricularExercisesSituations(StudentExercisesSituationQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        Employee employee = employeeDao.get(sysUser.getId());
        if (StringUtils.isEmpty(queryInfo.getOrganIdList())) {
            queryInfo.setOrganIdList(employee.getOrganIdList());
        }else if(StringUtils.isEmpty(employee.getOrganIdList())){
            return failed("用户所在分部异常");
        }else {
            List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
            if(!list.containsAll(Arrays.asList(queryInfo.getOrganIdList().split(",")))){
                return failed("非法请求");
            }
        }
        return succeed(studentExtracurricularExercisesSituationService.findStudentExtracurricularExercisesSituations(queryInfo));
    }

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

    @ApiOperation(value = "教师服务指标明细")
    @GetMapping("/queryTeacherServeInfo")
    @PreAuthorize("@pcs.hasPermissions('exercisesSituation/queryTeacherServeInfo')")
    public HttpResponseResult queryTeacherServeInfo(TeacherServeQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        Employee employee = employeeService.get(sysUser.getId());
        if (StringUtils.isBlank(queryInfo.getOrganId())) {
            queryInfo.setOrganId(employee.getOrganIdList());
        }else if(StringUtils.isEmpty(employee.getOrganIdList())){
            return failed("用户所在分部异常");
        }else {
            List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
            if(!list.containsAll(Arrays.asList(queryInfo.getOrganId().split(",")))){
                return failed("非法请求");
            }
        }
        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){
        if(StringUtils.isNotBlank(monday)){
            studentServeService.exercisesSituationStatistics(monday);
        }
        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));
                date = date.with(DateUtil.weekFields.dayOfWeek(), DayOfWeek.SUNDAY.getValue()).plusDays(1);
            }
        }
        return succeed();
    }

    @ApiOperation(value = "教师作业布置提醒")
    @PostMapping("/teacherServeRemindPush")
    @PreAuthorize("@pcs.hasPermissions('exercisesSituation/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")
    @PreAuthorize("@pcs.hasPermissions('exercisesSituation/queryTeacherServeHomeworkDetail')")
    public HttpResponseResult queryTeacherServeHomeworkDetail(TeacherServeHomeworkQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        return succeed(studentExtracurricularExercisesSituationService.queryTeacherServeHomeworkDetail(queryInfo));
    }

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

}
