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

import java.util.Arrays;
import java.util.List;

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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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.StudentAttendanceDto;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.dal.page.CourseScheduleQueryInfo;
import com.ym.mec.biz.dal.page.StudentAttendanceQueryInfo;
import com.ym.mec.biz.service.ClassGroupService;
import com.ym.mec.biz.service.StudentAttendanceService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.exception.BizException;

@RequestMapping("studentAttendance")
@Api(tags = "学生考勤服务")
@RestController
public class StudentAttendanceController extends BaseController {

    @Autowired
    private StudentAttendanceService studentAttendanceService;
    @Autowired
    private ClassGroupService classGroupService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private EmployeeDao employeeDao;

    //修复逻辑:是连堂课，有签退记录，某个月,遍历有学员签到记录的每节课
    @GetMapping("/repairStudentAttendance")
    public void repairStudentAttendance(Integer month) {
        if(month == null){
            throw new BizException("参数校验失败");
        }
        studentAttendanceService.repairStudentAttendance(month);
    }

    @ApiOperation(value = "点名完成")
    @PostMapping("/addStudentAttendances")
    public Object addStudentAttendances(@RequestBody StudentAttendanceDto studentAttendanceDto){
        studentAttendanceService.addStudentAttendances(studentAttendanceDto);
        return succeed();
    }

    @ApiOperation(value = "获取学生个人签到信息")
    @GetMapping("/queryStudentPersonalAttendances")
    public Object queryStudentPersonalAttendances(@RequestBody StudentAttendanceQueryInfo queryInfo){
        return succeed(studentAttendanceService.getStudentPersonalAttendances(queryInfo));
    }

    @ApiOperation(value = "获取某节课学生签到列表")
    @GetMapping("/findStudentAttendance")
    @PreAuthorize("@pcs.hasPermissions('studentAttendance/findStudentAttendance')")
    public Object findStudentAttendance(StudentAttendanceQueryInfo queryInfo){
    	SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        if(!sysUser.getIsSuperAdmin()){
            Employee employee = employeeDao.get(sysUser.getId());
            if (StringUtils.isEmpty(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(studentAttendanceService.findStudentAttendance(queryInfo));
    }

    @ApiOperation(value = "查询课程学生列表")
    @GetMapping("/findAttendanceStudentByCourseWithPage")
    @PreAuthorize("@pcs.hasPermissions('studentAttendance/findAttendanceStudentByCourseWithPage')")
    public Object findAttendanceStudentByCourseWithPage(CourseScheduleQueryInfo queryInfo){
        return succeed(classGroupService.findAttendanceStudentByCourseWithPage(queryInfo));
    }

    @ApiOperation(value = "更新学生签到记录")
    @PostMapping("/updateStudentAttendances")
    @PreAuthorize("@pcs.hasPermissions('studentAttendance/updateStudentAttendances')")
    public Object updateStudentAttendances(@RequestBody StudentAttendanceDto studentAttendanceInfo){
        studentAttendanceService.updateStudentAttendances(studentAttendanceInfo);
        return succeed();
    }

}
