package com.ym.mec.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.ym.mec.education.base.PageResponse;
import com.ym.mec.education.entity.ClassGroup;
import com.ym.mec.education.entity.CourseSchedule;
import com.ym.mec.education.entity.StudentAttendance;
import com.ym.mec.education.mapper.StudentAttendanceMapper;
import com.ym.mec.education.req.StudentAttendanceReq;
import com.ym.mec.education.resp.StudentAttendanceResq;
import com.ym.mec.education.service.IClassGroupService;
import com.ym.mec.education.service.ICourseScheduleService;
import com.ym.mec.education.service.IStudentAttendanceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 学生考勤表 服务实现类
 * </p>
 *
 * @author lemeng
 * @since 2019-09-25
 */
@Service("iStudentAttendanceService")
public class StudentAttendanceServiceImpl extends ServiceImpl<StudentAttendanceMapper, StudentAttendance> implements IStudentAttendanceService {

    @Autowired
    private ICourseScheduleService courseScheduleService;
    @Autowired
    private IClassGroupService groupService;

    @Override
    public PageResponse getPage(StudentAttendanceReq studentAttendanceReq) {
        Page<StudentAttendance> pageParam = new Page(studentAttendanceReq.getPageNo(), studentAttendanceReq.getPageSize());
        QueryWrapper<StudentAttendance> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Objects.nonNull(studentAttendanceReq.getMusicGroupId()),
                StudentAttendance::getMusicGroupId, studentAttendanceReq.getMusicGroupId())
                .eq(Objects.nonNull(studentAttendanceReq.getClassGroupId()),
                        StudentAttendance::getClassGroupId, studentAttendanceReq.getClassGroupId());
        IPage<StudentAttendance> page = page(pageParam, queryWrapper);
        IPage<StudentAttendanceResq> pageResult = new Page<>();
        BeanUtils.copyProperties(page, pageResult);
        List<StudentAttendanceResq> list = Lists.newArrayList();
        QueryWrapper<StudentAttendance> leaveWrapper = new QueryWrapper<>();
        QueryWrapper<StudentAttendance> normalWrapper = new QueryWrapper<>();
        //总人数
        Integer totalCount = count();
        //请假
        leaveWrapper.lambda().eq(true, StudentAttendance::getMusicGroupId, studentAttendanceReq.getMusicGroupId())
                .eq(true, StudentAttendance::getClassGroupId, studentAttendanceReq.getClassGroupId())
                .eq(true, StudentAttendance::getStatus, "LEAVE");
        Integer leaveCount = count(leaveWrapper);
        //正常
        normalWrapper.lambda().eq(true, StudentAttendance::getMusicGroupId, studentAttendanceReq.getMusicGroupId())
                .eq(true, StudentAttendance::getClassGroupId, studentAttendanceReq.getClassGroupId())
                .eq(true, StudentAttendance::getStatus, "NORMAL");
        Integer normalCount = count(normalWrapper);
        page.getRecords().forEach(item ->{
            StudentAttendanceResq studentAttendanceResq = new StudentAttendanceResq();
            QueryWrapper<CourseSchedule> courseScheduleQueryWrapper = new QueryWrapper<>();
            courseScheduleQueryWrapper.lambda().eq(true, CourseSchedule::getClassGroupId, studentAttendanceReq.getClassGroupId());
            CourseSchedule courseSchedule = courseScheduleService.getOne(courseScheduleQueryWrapper);
            ClassGroup classGroup = groupService.getById(studentAttendanceReq.getClassGroupId());
            studentAttendanceResq.setClassDate(courseSchedule.getClassDate())
                    .setClassTime(courseSchedule.getStartClassTime() + "-" + courseSchedule.getEndClassTime())
                    .setClassGroupName(classGroup.getName())
                    .setLeaveNum(leaveCount).setAttendanceRate(normalCount + "/" + totalCount);
            list.add(studentAttendanceResq);
        });
        pageResult.setRecords(list);
        return PageResponse.success(pageResult);
    }
}
