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.entity.Employee;
import com.ym.mec.biz.dal.page.StudentCourseHomeworkReplyQueryInfo;
import com.ym.mec.biz.dal.page.StudentExercisesSituationQueryInfo;
import com.ym.mec.biz.dal.page.StudentServiceDetailQueryInfo;
import com.ym.mec.biz.service.ExtracurricularExercisesMessageService;
import com.ym.mec.biz.service.StudentCourseHomeworkReplyService;
import com.ym.mec.biz.service.StudentExtracurricularExercisesSituationService;
import com.ym.mec.biz.service.StudentServeService;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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;

    @ApiOperation(value = "课外训练-教学")
    @GetMapping("/findStudentExtracurricularExercisesSituations")
    @PreAuthorize("@pcs.hasPermissions('exercisesSituation/findStudentExtracurricularExercisesSituations')")
    public HttpResponseResult findStudentExtracurricularExercisesSituations(StudentExercisesSituationQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        if(!sysUser.getIsSuperAdmin()){
            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("/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();
    }

}
