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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.enums.MessageSendMode;
import com.ym.mec.education.base.BaseResponse;
import com.ym.mec.education.entity.SysMessage;
import com.ym.mec.education.entity.Teacher;
import com.ym.mec.education.entity.TeacherAttendance;
import com.ym.mec.education.enums.JobTypeEnum;
import com.ym.mec.education.enums.SignStatusEnum;
import com.ym.mec.education.mapper.SysMessageMapper;
import com.ym.mec.education.resp.MessageResp;
import com.ym.mec.education.service.ISysMessageService;
import com.ym.mec.education.service.ITeacherAttendanceService;
import com.ym.mec.education.service.ITeacherService;
import org.snaker.engine.IQueryService;
import org.snaker.engine.access.QueryFilter;
import org.snaker.engine.entity.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 * 消息表 服务实现类
 * </p>
 *
 * @author lemeng
 * @since 2019-09-25
 */
@Service("ISysMessageService")
public class SysMessageServiceImpl extends ServiceImpl<SysMessageMapper, SysMessage> implements ISysMessageService {

    @Resource
    private SysUserFeignService userFeign;
    @Autowired
    private IQueryService taskQueryService;
    @Autowired
    private ITeacherAttendanceService teacherAttendanceService;
    @Autowired
    private ITeacherService teacherService;

    @Override
    public BaseResponse getInfo() {
        SysUser currentUser = userFeign.queryUserInfo();
        if (Objects.isNull(currentUser)) {
            return BaseResponse.noDataExists();
        }
        MessageResp messageResp = new MessageResp();
        messageResp.setUserId(currentUser.getId()).setUserName(currentUser.getRealName()).setMobileNo(currentUser.getPhone());
        //职务
        Teacher teacher = teacherService.getById(currentUser.getId());
        Optional.ofNullable(teacher.getJobType()).ifPresent(jobType ->
            messageResp.setJobType(JobTypeEnum.getMsgByCode(jobType)));
        //活动中的任务
        QueryFilter queryFilter = new QueryFilter();
        queryFilter.setOperator(currentUser.getId().toString());
        List<Task> activeTasks = taskQueryService.getActiveTasks(queryFilter);
        messageResp.setApprovalCount(activeTasks.size());
        //老师考勤异常
        QueryWrapper<TeacherAttendance> teacherAttendanceQueryWrapper = new QueryWrapper<>();
        teacherAttendanceQueryWrapper.lambda().eq(TeacherAttendance::getTeacherId, currentUser.getId())
            .eq(TeacherAttendance::getSignInStatus, SignStatusEnum.EXCEPTION.getCode());
        int exceptionCount = teacherAttendanceService.count(teacherAttendanceQueryWrapper);
        messageResp.setAttendanceCount(exceptionCount);
        //未知状态消息
        QueryWrapper<SysMessage> messageQueryWrapper = new QueryWrapper<>();
        messageQueryWrapper.lambda().eq(SysMessage::getReceiver, currentUser.getId())
            .eq(SysMessage::getReadStatus, 0).eq(SysMessage::getType, MessageSendMode.PUSH.getCode());
        int messageCount = count(messageQueryWrapper);
        messageResp.setOtherStatusCount(messageCount);
        messageResp.setTotalCount(activeTasks.size() + exceptionCount + messageCount);
        return BaseResponse.success(messageResp);
    }
}
