ClassDictServiceImpl.java 12.1 KB
package com.subsidy.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.subsidy.common.ResponseData;
import com.subsidy.common.constant.CourseNotification;
import com.subsidy.common.exception.HttpException;
import com.subsidy.dto.classDict.AddClassDTO;
import com.subsidy.dto.classDict.AddMemberToClassDTO;
import com.subsidy.dto.classDict.GetClassBaseInfoDTO;
import com.subsidy.dto.classDict.GetSpareMembersDTO;
import com.subsidy.jobs.CourseNotificationJob;
import com.subsidy.mapper.AnsweringQuestionMapper;
import com.subsidy.mapper.ClassDictMapper;
import com.subsidy.mapper.ClassMemberMappingMapper;
import com.subsidy.mapper.ClassNoticeMapper;
import com.subsidy.mapper.CompanyDictMapper;
import com.subsidy.mapper.CourseDictMapper;
import com.subsidy.mapper.RoleAdministerMappingMapper;
import com.subsidy.mapper.VodDictMapper;
import com.subsidy.mapper.VodPlayHistoryMapper;
import com.subsidy.model.AnsweringQuestionDO;
import com.subsidy.model.ClassDictDO;
import com.subsidy.model.ClassMemberMappingDO;
import com.subsidy.model.ClassNoticeDO;
import com.subsidy.model.CompanyDictDO;
import com.subsidy.model.CourseDictDO;
import com.subsidy.model.FieldDictDO;
import com.subsidy.model.MemberDO;
import com.subsidy.model.RoleAdministerMappingDO;
import com.subsidy.model.VodDictDO;
import com.subsidy.service.ClassDictService;
import com.subsidy.util.ConstantUtils;
import com.subsidy.util.DateFormatUtil;
import com.subsidy.util.QuartzUtil;
import com.subsidy.util.SMSUtils;
import com.subsidy.util.websocket.WebSocketUtil;
import com.subsidy.vo.classdict.ClassSettingsVO;
import com.subsidy.vo.classdict.GetAllClassesVO;
import com.subsidy.vo.classdict.GetClassBaseInfoVO;
import com.subsidy.vo.classdict.SystemSettings;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 班级id 服务实现类
 * </p>
 *
 * @author DengMin
 * @since 2021-10-12
 */
@Service
public class ClassDictServiceImpl extends ServiceImpl<ClassDictMapper, ClassDictDO> implements ClassDictService {

    @Autowired
    private ClassMemberMappingMapper classMemberMappingMapper;

    @Autowired
    private CourseDictMapper courseDictMapper;

    @Autowired
    private CompanyDictMapper companyDictMapper;

    @Autowired
    private ClassNoticeMapper classNoticeMapper;

    @Autowired
    private AnsweringQuestionMapper answeringQuestionMapper;

    @Autowired
    private QuartzUtil quartzUtil;

    @Autowired
    private VodDictMapper vodDictMapper;

    @Autowired
    private VodPlayHistoryMapper vodPlayHistoryMapper;

    public List<GetAllClassesVO> getAllClasses(ClassDictDO classDictDO) throws Exception {
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");

        List<GetAllClassesVO> getAllClassesVOS = this.baseMapper.getAllClasses(classDictDO);
        for (GetAllClassesVO getAllClassesVO : getAllClassesVOS) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(fmt.parse(getAllClassesVO.getEndDate()));
            calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + 1);
            if (new Date().before(fmt.parse(getAllClassesVO.getStartDate()))) {
                getAllClassesVO.setOpenStatus("1"); //待开始
            } else if (new Date().after(calendar.getTime())) {
                getAllClassesVO.setOpenStatus("2");
            } else {
                getAllClassesVO.setOpenStatus("0");
            }
        }
        return getAllClassesVOS;
    }

    @Transactional
    public String deleteClasses(ClassDictDO classDictDO) {
        this.baseMapper.deleteById(classDictDO.getId());

        classMemberMappingMapper.delete(new QueryWrapper<ClassMemberMappingDO>()
                .lambda()
                .eq(ClassMemberMappingDO::getClassId, classDictDO.getId()));
        return ConstantUtils.DELETE_SUCCESS;
    }

    @Transactional(rollbackFor = Exception.class)
    public Long addClass(AddClassDTO addClassDTO) {

        //新建一个班级
        ClassDictDO classDictDO = new ClassDictDO();
        BeanUtils.copyProperties(addClassDTO, classDictDO);
        this.baseMapper.insert(classDictDO);

        //班级里添加成员
        List<Long> memberIds = addClassDTO.getMemberIds();
        for (Long lg : memberIds) {
            ClassMemberMappingDO classMemberMappingDO = new ClassMemberMappingDO();
            classMemberMappingDO.setMemberId(lg);
            classMemberMappingDO.setClassId(classDictDO.getId());
            classMemberMappingMapper.insert(classMemberMappingDO);
        }

        //添加通知
        if (null != addClassDTO.getClassNoticeDOS()) {
            List<ClassNoticeDO> classNoticeDOS = addClassDTO.getClassNoticeDOS();
            for (ClassNoticeDO classNoticeDO : classNoticeDOS) {
                if (DateFormatUtil.parse(classNoticeDO.getNoticeTime(), "yyyy-MM-dd HH").before(new Date())) {
                    throw new HttpException(70001);
                }
                classNoticeDO.setClassId(classDictDO.getId());
                classNoticeDO.setStatus(CourseNotification.UNSENT);
                classNoticeMapper.insert(classNoticeDO);
                Map<String, Object> params = new HashMap<>();
                params.put("classId", classNoticeDO.getClassId());
                params.put("id", classNoticeDO.getId());
                String name = classDictDO.getClassName() + "-" + classNoticeDO.getNoticeType() + "-" + classNoticeDO.getNoticeTime();
                quartzUtil.addSimpleJob(CourseNotificationJob.class, DateFormatUtil.parse(classNoticeDO.getNoticeTime(), "yyyy-MM-dd HH:mm:ss"), params, name, "CourseNotificationJob");

            }
        }
        return classDictDO.getId();
    }

    public String updateClass(ClassDictDO classDictDO) {
        this.baseMapper.updateById(classDictDO);

        //班级所在公司
        SystemSettings systemSettings = this.baseMapper.getCompanySettings(classDictDO.getId());

        List<ClassMemberMappingDO> list = classMemberMappingMapper.selectList(new QueryWrapper<ClassMemberMappingDO>()
                .lambda()
                .eq(ClassMemberMappingDO::getClassId, classDictDO.getId()));
        if(CollectionUtils.isNotEmpty(list)) {
            for (ClassMemberMappingDO classMemberMappingDO : list) {
                WebSocketSession webSocketSession = WebSocketUtil.webSocketMap.get(classMemberMappingDO.getMemberId());
                if(null != webSocketSession && webSocketSession.isOpen()) {
                    try {
                        //班级的配置
                        List<ClassSettingsVO> classSettings = this.baseMapper.getClassSettings(classMemberMappingDO.getMemberId());
                        systemSettings.setClassSettingsVOS(classSettings);
                        String data = JSONObject.toJSONString(ResponseData.generateCreatedResponse(0, systemSettings), SerializerFeature.WriteMapNullValue);
                        webSocketSession.sendMessage(new TextMessage(data));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return ConstantUtils.SET_SUCCESS;
    }

    public ClassDictDO classDetail(ClassDictDO classDictDO) {
        return this.baseMapper.selectById(classDictDO.getId());
    }

    public String removeMember(ClassMemberMappingDO classMemberMappingDO) {
        classMemberMappingMapper.delete(new QueryWrapper<ClassMemberMappingDO>()
                .lambda()
                .eq(ClassMemberMappingDO::getClassId, classMemberMappingDO.getClassId())
                .eq(ClassMemberMappingDO::getMemberId, classMemberMappingDO.getMemberId()));
        //签到记录

        //做题记录

        //答疑记录也删除
        answeringQuestionMapper.delete(new QueryWrapper<AnsweringQuestionDO>()
                .lambda()
                .eq(AnsweringQuestionDO::getAskId, classMemberMappingDO.getMemberId())
                .eq(AnsweringQuestionDO::getClassId, classMemberMappingDO.getClassId()));
        return ConstantUtils.DELETE_SUCCESS;
    }

    public List<MemberDO> getSpareMembers(GetSpareMembersDTO getSpareMembersDTO) {
        return this.baseMapper.getSpareMembers(getSpareMembersDTO);
    }

    public String addMemberToClass(AddMemberToClassDTO addMemberToClassDTO) {

        List<Long> memberIds = addMemberToClassDTO.getMemberIds();

        for (Long lg : memberIds) {
            ClassMemberMappingDO classMemberMappingDO = new ClassMemberMappingDO();
            classMemberMappingDO.setClassId(addMemberToClassDTO.getClassId());
            classMemberMappingDO.setMemberId(lg);
            classMemberMappingMapper.insert(classMemberMappingDO);
        }
        return ConstantUtils.ADD_SUCCESS;
    }

    public String batchSms(ClassDictDO classDictDO) {

        ClassDictDO classDictDO1 = this.baseMapper.selectById(classDictDO.getId());
        CourseDictDO courseDictDO = courseDictMapper.selectById(classDictDO1.getCourseId());
        CompanyDictDO companyDictDO = companyDictMapper.selectById(classDictDO1.getCompanyId());

        //查找这个班级里的学员的手机号
        List<MemberDO> memberDOS = this.baseMapper.classMembers(classDictDO.getId());

        StringBuilder telephone = new StringBuilder();
        String param = "{\"company\":\"" + companyDictDO.getCompanyName() + "\",\"course\":\"" + courseDictDO.getCourseName() + "\",\"startDate\":\"" + classDictDO1.getStartDate() + "\",\"endDate\":\"" + classDictDO1.getEndDate() + "\"}";
        if (memberDOS.size() > 0) {
            for (int i = 0; i < memberDOS.size(); i++) {
                telephone.append(memberDOS.get(i).getTelephone());
                if (i != memberDOS.size() - 1) {
                    telephone.append(",");
                }
            }
            SMSUtils.sendNoticeSMS("SMS_228137810", telephone.toString(), param);
        }
        return ConstantUtils.SUCCESS_SEND_OUT;
    }

    public GetClassBaseInfoVO getClassBaseInfo(GetClassBaseInfoDTO getClassBaseInfoDTO) {

        GetClassBaseInfoVO getClassBaseInfoVO = this.baseMapper.getClassBaseInfo(getClassBaseInfoDTO.getId());
        //查看课程完成状态
        //查看某个课程都有哪些视频
        List<VodDictDO> vodDictDOS = vodDictMapper.getCourseVods(getClassBaseInfoVO.getCourseId(),null);
        int completeNum = 0 ;
        //查看每个视频的状态
        for (VodDictDO vodDictDO : vodDictDOS){
            int i = vodPlayHistoryMapper.completeVodOrNot(getClassBaseInfoDTO.getUserId(),vodDictDO.getId(),getClassBaseInfoDTO.getId());
            completeNum += i;
        }
        if (completeNum == vodDictDOS.size()){
            getClassBaseInfoVO.setTestFlag(true);
        }else {
            getClassBaseInfoVO.setTestFlag(false);
        }
        return getClassBaseInfoVO;
    }

    public String email(ClassMemberMappingDO classMemberMappingDO){

        ClassMemberMappingDO classMemberMappingDO1 = classMemberMappingMapper.selectOne(new QueryWrapper<ClassMemberMappingDO>()
        .lambda()
        .eq(ClassMemberMappingDO::getMemberId,classMemberMappingDO.getMemberId())
        .eq(ClassMemberMappingDO::getClassId,classMemberMappingDO.getClassId()));

        if (null != classMemberMappingDO1){
            classMemberMappingDO1.setEmail(classMemberMappingDO.getEmail());
            classMemberMappingDO1.setEmailStatus(classMemberMappingDO.getEmailStatus());
            classMemberMappingMapper.updateById(classMemberMappingDO1);
        }
        return ConstantUtils.SUCCESS_UPDATE;
    }

}