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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.CourseScheduleTeacherSalary;
import com.ym.mec.education.entity.TeacherAttendance;
import com.ym.mec.education.enums.SignStatusEnum;
import com.ym.mec.education.mapper.TeacherAttendanceMapper;
import com.ym.mec.education.req.TeacherLeaveRecordReq;
import com.ym.mec.education.resp.TeacherAttendanceResp;
import com.ym.mec.education.service.IClassGroupService;
import com.ym.mec.education.service.ICourseScheduleService;
import com.ym.mec.education.service.ICourseScheduleTeacherSalaryService;
import com.ym.mec.education.service.ITeacherAttendanceService;
import com.ym.mec.education.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 老师签到表 服务实现类
 * </p>
 *
 * @author lemeng
 * @since 2019-09-25
 */
@Service("ITeacherAttendanceService")
public class TeacherAttendanceServiceImpl extends ServiceImpl<TeacherAttendanceMapper, TeacherAttendance> implements ITeacherAttendanceService {

    @Autowired
    private ICourseScheduleService courseScheduleService;
    @Autowired
    private IClassGroupService classGroupService;
    @Autowired
    private ICourseScheduleTeacherSalaryService teacherSalaryService;

    @Override
    public PageResponse getPage(TeacherLeaveRecordReq teacherLeaveRecordReq) {
        if (Objects.isNull(teacherLeaveRecordReq.getTeacherId())) {
            return PageResponse.errorParam();
        }
        Page<TeacherAttendance> teacherAttendancePage = new Page<TeacherAttendance>(teacherLeaveRecordReq.getPageNo(), teacherLeaveRecordReq.getPageSize());
        Page<TeacherAttendanceResp> pageResult = new Page();
        List<TeacherAttendanceResp> list = Lists.newArrayList();
        Page<TeacherAttendance> page = baseMapper.selectClassRecordPage(teacherAttendancePage, teacherLeaveRecordReq);
        BeanUtils.copyProperties(page, pageResult);
        if (!CollectionUtils.isEmpty(page.getRecords())) {
            page.getRecords().forEach(item -> {
                TeacherAttendanceResp teacherAttendanceResp = new TeacherAttendanceResp();
                teacherAttendanceResp.setTeacherAttendanceStatus(SignStatusEnum.getMsgByCode(item.getSignInStatus()));
                CourseSchedule courseSchedule = courseScheduleService.getById(item.getCourseScheduleId());
                if (Objects.nonNull(courseSchedule)) {
                    if (Objects.nonNull(courseSchedule.getClassDate())) {
                        teacherAttendanceResp.setClassDate(DateUtil.date2String(courseSchedule.getClassDate(), DateUtil.DATE_FORMAT_EXTRA)
                                + " " + DateUtil.time2String(courseSchedule.getStartClassTime(), DateUtil.TIME_FORMAT_MIN))
                                .setClassWeek(DateUtil.date2Week(courseSchedule.getClassDate()));
                    }
                    if (StringUtils.isNotBlank(courseSchedule.getName())) {
                        teacherAttendanceResp.setCourseName(courseSchedule.getName());
                    }
                    if (StringUtils.isNotBlank(courseSchedule.getType())) {
                        teacherAttendanceResp.setCourseType(CourseSchedule.CourseScheduleType.getMsgByCode(courseSchedule.getType()));
                    }
                    ClassGroup classGroup = classGroupService.getById(courseSchedule.getClassGroupId());
                    if (Objects.nonNull(classGroup) && Objects.nonNull(classGroup.getCurrentClassTimes()) && Objects.nonNull(classGroup.getCurrentClassTimes())) {
                        teacherAttendanceResp.setCourseProgress(classGroup.getCurrentClassTimes() + "/" + classGroup.getTotalClassTimes());
                    }
                    //课酬
                    QueryWrapper<CourseScheduleTeacherSalary> courseScheduleTeacherSalaryQueryWrapper = new QueryWrapper();
                    courseScheduleTeacherSalaryQueryWrapper.lambda().eq(CourseScheduleTeacherSalary::getCourseScheduleId, item.getCourseScheduleId())
                            .eq(CourseScheduleTeacherSalary::getUserId, item.getTeacherId());
                    CourseScheduleTeacherSalary teacherSalary = teacherSalaryService.getOne(courseScheduleTeacherSalaryQueryWrapper);
                    if (Objects.nonNull(teacherSalary) && Objects.nonNull(teacherSalary.getSalary())) {
                        teacherAttendanceResp.setTeacherSalary(teacherSalary.getSalary());
                    }
                }
                list.add(teacherAttendanceResp);
            });
            pageResult.setRecords(list);
        }
        return PageResponse.success(pageResult);
    }
}
