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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.subsidy.common.exception.HttpException;
import com.subsidy.dto.classDict.AddClassDTO;
import com.subsidy.dto.classDict.AddMemberToClassDTO;
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.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.MemberDO;
import com.subsidy.model.RoleAdministerMappingDO;
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.vo.classdict.GetAllClassesVO;
import com.subsidy.vo.classdict.GetClassBaseInfoVO;
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 java.text.DateFormat;
import java.text.SimpleDateFormat;
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;

    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){

            if (new Date().before(fmt.parse(getAllClassesVO.getStartDate()))){
                getAllClassesVO.setOpenStatus("1"); //待开始
            }else if (new Date().after(fmt.parse(getAllClassesVO.getEndDate()))){
                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;
    }

    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());
        }

        //添加通知
        if (null != addClassDTO.getClassNoticeDOS()){
            List<ClassNoticeDO> classNoticeDOS = addClassDTO.getClassNoticeDOS();
            for (ClassNoticeDO classNoticeDO : classNoticeDOS) {
                if(DateFormatUtil.parse(classNoticeDO.getNoticeTime(),"yyyy-MM-dd").before(new Date())) {
                    throw new HttpException(70001);
                }
                classNoticeDO.setClassId(classDictDO.getId());
                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") , params, name, "CourseNotificationJob");

            }

        }

        return classDictDO.getId();
    }

    public String updateClass(ClassDictDO classDictDO) {
        this.baseMapper.updateById(classDictDO);
        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(ClassDictDO classDictDO) {
        return this.baseMapper.getClassBaseInfo(classDictDO.getId());
    }

}