package com.yonge.cooleshow.biz.dal.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.biz.dal.dao.CourseScheduleStudentPaymentDao;
import com.yonge.cooleshow.biz.dal.dto.ImNetworkCustomMessage;
import com.yonge.cooleshow.biz.dal.dto.ImNetworkRoomMusicSheetDownloadData;
import com.yonge.cooleshow.biz.dal.entity.CourseScheduleStudentPayment;
import com.yonge.cooleshow.biz.dal.enums.CourseScheduleEnum;
import com.yonge.cooleshow.biz.dal.service.CourseScheduleStudentPaymentService;
import com.yonge.cooleshow.biz.dal.vo.LiveCourseInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 课程学生缴费表(CourseScheduleStudentPayment)表服务实现类
 *
 * @author hgw
 * @since 2022-03-18 15:29:12
 */
@Service("courseScheduleStudentPaymentService")
public class CourseScheduleStudentPaymentServiceImpl extends ServiceImpl<CourseScheduleStudentPaymentDao, CourseScheduleStudentPayment> implements CourseScheduleStudentPaymentService {

    private final static Logger log = LoggerFactory.getLogger(CourseScheduleStudentPaymentServiceImpl.class);

    @Override
    public CourseScheduleStudentPaymentDao getDao() {
        return this.baseMapper;
    }

    /**
     * 根据课程组id查询学员信息
     *
     * @param param 参数
     *              <p> groupId    课程组id
     *              <p> orderState 订单状态
     *              <p> userId     学员id
     */
    @Override
    public List<LiveCourseInfoVo.CourseBuyStudentVo> queryStudentInfoByGroupId(Map<String, Object> param) {
        return baseMapper.queryStudentInfoByGroupId(param);
    }

    @Override
    public List<CourseScheduleStudentPayment> getByCourseId(Long courseId) {
        List<CourseScheduleEnum> courseScheduleEnumList = new ArrayList<>();
        courseScheduleEnumList.add(CourseScheduleEnum.PIANO_ROOM_CLASS);
        courseScheduleEnumList.add(CourseScheduleEnum.PRACTICE);
        List<CourseScheduleStudentPayment> list = this.lambdaQuery()
                .eq(CourseScheduleStudentPayment::getCourseId, courseId)
                .in(CourseScheduleStudentPayment::getCourseType,courseScheduleEnumList)
                .isNotNull(CourseScheduleStudentPayment::getUserId)
                .list();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list;
    }

    @Override
    public List<String> getOrderNoByGroupId(Long courseGroupId) {

        return baseMapper.selectOrderNoByGroupId(courseGroupId);

    }

    @Override
    public ImNetworkCustomMessage getMemberMidi(long courseScheduleId, Long userId) {
        String midi = baseMapper.getMidiByCourseIdAndUserId(courseScheduleId, userId);
        if(StringUtils.isNotEmpty(midi)){
            return JSONObject.parseObject(midi, ImNetworkCustomMessage.class);
        }
        return new ImNetworkCustomMessage();
    }

    @Override
    public ImNetworkRoomMusicSheetDownloadData getMemberExamSong(long courseScheduleId, Long userId) {
        String musicSheet = baseMapper.getMusicSheetByCourseIdAndUserId(courseScheduleId, userId);
        if(StringUtils.isNotEmpty(musicSheet)){
            return JSONObject.parseObject(musicSheet, ImNetworkRoomMusicSheetDownloadData.class);
        }
        return new ImNetworkRoomMusicSheetDownloadData();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void adjustPlayMidiAndMusicSheet(Long scheduleId, Long userId, String content, String musicSheetJson) {
        baseMapper.adjustPlayMidiAndMusicSheet(scheduleId,userId,content,musicSheetJson);
    }

}

