package com.ssj.service.sys.attendance;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.sys.attendance.Attendance;
import com.ssj.bean.weixin.libmy.domain.TbLibManager;
import com.ssj.framework.weixin.util.DateUtil;
import com.ssj.dao.sys.attendance.AttendanceDao;
import com.ssj.framework.basic.utils.DateHelper;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.weixin.library.service.IBookManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Mount Liu
 * @date 2019/6/4
 */
@Service(version = "1.0")
@Transactional
public class AttendanceServiceImpl extends BaseServiceImpl<Attendance, String> implements AttendanceService {

    @Autowired
    private AttendanceDao attendanceDao;

    @Autowired
    private IBookManagerService managerService;

    @Override
    public PagingAndSortingRepository<Attendance, String> getDao() {
        return attendanceDao;
    }

    @Override
    public Attendance findByUserIdAndDate(String userId, Date oneDay) {
        String day = DateUtil.format_yyyyMMdd(oneDay);
        return attendanceDao.findTopByUserIdAndCreateTimeLike(userId, day);
    }

    @Override
    public void takeDownTodayCorrectTeacher() {
        Date now = new Date();
        String period = DateHelper.getWeekOfDate(now, 1);
        //所有该天应该出勤的老师（不包含封号老师）
        List<TbLibManager> list = managerService.findCorrectTeacherByPeriod(period);
        if (list != null && list.size() > 0) {
            List<Attendance> attendanceList = new ArrayList<>();
            for (TbLibManager teacher : list) {
                Attendance attendance = new Attendance();
                attendance.setCreateTime(now);
                attendance.setUserId(teacher.getUserId());
                attendance.setType(teacher.getType());
                attendanceList.add(attendance);
            }
            this.save(attendanceList);
        }
    }
}
