package com.xhkjedu.slive.service.liveplay;

import com.alibaba.fastjson.JSON;
import com.xhkjedu.slive.mapper.liveplay.CourseSectionChatroomMapper;
import com.xhkjedu.slive.mapper.liveplay.LCourseSectionWsMapper;
import com.xhkjedu.slive.model.liveplay.LCourseSectionChatroom;
import com.xhkjedu.slive.model.liveplay.LCourseSectionWs;
import com.xhkjedu.slive.vo.liveplay.MessageBodyVo;
import com.xhkjedu.utils.N_Utils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class LCourseSectionWsService {
    @Resource
    private LCourseSectionWsMapper lCourseSectionWsMapper;
    @Resource
    private CourseSectionChatroomMapper courseSectionChatroomMapper;

    //添加
    public void save(LCourseSectionWs ws) {
        ws.setCreatetime(N_Utils.getSecondTimestamp());
        MessageBodyVo mb = JSON.parseObject(ws.getMsg(), MessageBodyVo.class);
        String d = mb.getD();
        ws.setDevice(d);
        lCourseSectionWsMapper.insertSelective(ws);
        if (ws.getCinteract().equals(2000)) {//发消息
            LCourseSectionChatroom chatroom = JSON.parseObject(JSON.toJSONString(mb.getB()), LCourseSectionChatroom.class);
            chatroom.setSectionid(ws.getSectionid());
            chatroom.setUsertype(mb.getU());
            chatroom.setDevice(d);
            try {
                courseSectionChatroomMapper.insertSelective(chatroom);
            } catch (DuplicateKeyException e) {
            }
        }
    }

    //获取列表
    public List<Map> listAll(LCourseSectionWs ws) {
        return lCourseSectionWsMapper.listAll(ws);
    }
}
