package com.steerinfo.ems.emswebsocketlog.service.impl;

import com.steerinfo.ems.Utils.DateUtils;
import com.steerinfo.ems.emswebsocketlog.mapper.EmsWebsocketLogMapper;
import com.steerinfo.ems.emswebsocketlog.model.EmsWebsocketLog;
import com.steerinfo.ems.emswebsocketlog.service.IEmsWebsocketLogService;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.websocketclient.model.MessageType;
import com.steerinfo.websocketclient.service.IAsyncWebSocketService;
import com.steerinfo.websocketclient.service.ISyncWebSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * EmsWebsocketLog服务实现：
 * @author generator
 * @version 1.0-SNAPSHORT 2021-10-15 03:24
 * 类描述
 * 修订历史：
 * 日期：2021-10-15
 * 作者：generator
 * 参考：
 * 描述：EmsWebsocketLog服务实现
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@Service(value = "emsWebsocketLogService")
public class EmsWebsocketLogServiceImpl extends BaseServiceImpl<EmsWebsocketLog, String> implements IEmsWebsocketLogService {

    @Autowired
    private EmsWebsocketLogMapper emsWebsocketLogMapper;

    @Override
    protected IBaseMapper<EmsWebsocketLog, String> getMapper() {
        return emsWebsocketLogMapper;
    }

    /**
     * 异步发送消息
     */
    @Autowired
    IAsyncWebSocketService asyncWebSocketService;
    /**
     * 同步发送消息
     */
    @Autowired
    ISyncWebSocketService syncWebSocketService;

    @Override
    public void pushMessageByDataException(List<EmsWebsocketLog> websocketLogs) {
        if (websocketLogs != null && websocketLogs.size() > 0) {
            //这里state必须为2，因为2是接收人，0是未读，1是已读
            Map<String, Object> map = new HashMap<>(2);
            map.put("state", "2");
            map.put("title", websocketLogs.get(0).getTitle());
            //查出所有符合条件的接收角色
            List<EmsWebsocketLog> emsWebsocketLogs = getMapper().selectByParameters(map);
            if (emsWebsocketLogs != null && emsWebsocketLogs.size() > 0) {
                String[] userIds;
                String tips;
                List<MessageType> messageTypeList = new ArrayList<>();
                List<EmsWebsocketLog> models = new ArrayList<>();
                //应发送的消息数量=消息个数*接收角色条数*接收角色下的所有用户。三层循环；一：循环消息列表
                //二：循环角色列表
                for(int i = 0 ;i<emsWebsocketLogs.size();i++){
                for (EmsWebsocketLog websocketLog : websocketLogs) {
                    //语音消息尽量选择自定义，否则会播放失败
                    switch (websocketLog.getTitle()){
                        case "计控处": tips="您好，计量点数据存在异常,请注意查看。";break;
                        default: tips=websocketLogs.get(0).getRole(); break;
                    }
                        //用户转为数组
                        userIds = emsWebsocketLogs.get(i).getRelation().split(",");
                        //三：循环用户列表
                        for (String userId : userIds) {
                            EmsWebsocketLog model = new EmsWebsocketLog();
                            model.setState("0");
                            model.setRelation(userId);
                            model.setRole(emsWebsocketLogs.get(i).getRole());
                            model.setTitle(websocketLog.getTips());
                            model.setMessage(websocketLog.getMessage());
                            model.setTips(tips);
                            model.setCreateman("系统");
                            model.setCreatetime(DateUtils.dateStr(new Date(), "yyyy-MM-dd HH:mm:ss"));
                            models.add(model);
                            MessageType messageType = new MessageType();
                            messageType.setType("top-right");
                            messageType.setPayload(model);
                            messageType.setUserId(userId);
                            messageType.setOffline(false);
                            messageType.setContentId(userId+DateUtils.dateStr(new Date(), "yyyy-MM-dd HH:mm:ss"));
                            messageTypeList.add(messageType);

                        }
                    }
                }
            if (models != null && models.size() > 0) {
                try {
                    //getMapper().batchInsert(models);
                    this.batchAdd(models);
                    asyncWebSocketService.sendMessagesAsync(messageTypeList);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            }
        }
    }
}
