package com.duowenlvshi.duowenlszp.model.Dialogue.server;

import com.duowenlvshi.duowenlszp.model.Dialogue.entity.Dialogue;
import com.duowenlvshi.duowenlszp.app.util.AllPush;
import com.duowenlvshi.duowenlszp.app.util.UUIDUtil;
import com.duowenlvshi.duowenlszp.model.Dialogue.dao.DialogueMapper;

import com.duowenlvshi.duowenlszp.model.Job.server.JobServer;
import com.duowenlvshi.duowenlszp.model.Push.server.PushServer;
import com.duowenlvshi.duowenlszp.model.lawyer.server.LawyerServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yang fang yu
 * @since 2019/8/26 17:15
 */
@Service
public class DialogueServer {
    @Autowired(required = false)
    DialogueMapper dialogueMapper;
    @Autowired
    AllPush allPush;
    @Autowired
    JobServer jobServer;
    @Autowired
    LawyerServer lawyerServer;
    @Autowired
    PushServer pushServer;

    public int save(Dialogue dialogue){
        int i;
        dialogue.setDialogueId(UUIDUtil.GetUUID());
        dialogue.setIsvalid("1");
        int j = dialogueMapper.selectNewTopic(dialogue.getTopic());//查询聊天室是否存在
        if(j==0){//聊天室不存在 设置聊天室为新增
            dialogue.setNewTopic("1");
            //Map map = jobServer.selectByIdPush(dialogue.getJobId());
            Map<String,String> pushMap =new HashMap<>();
            Map nameMap = lawyerServer.selectName(dialogue.getLawyerIda());//查询推送岗位所需信息
            String name =nameMap.get("real_name")+"";
            String logo =nameMap.get("logo")+"";
            pushMap.put("Title","求职: "+name+"律师");//设置安卓内容
            pushMap.put("Text",name+"律师已经在线回复您了,快去看看吧!");
            pushMap.put("Alert",name+"律师已经在线回复您了,快去看看吧!");//设置ios内容
            pushMap.put("NcOrTrans","1");//设置华为透传还是消息栏 1消息栏 2透传
            pushMap.put("type","39");//设置跳转参数
            pushMap.put("lawyerId",dialogue.getLawyerIdb());//把被推送者lawyerId放入推送map中
            pushMap.put("topic",dialogue.getTopic());
            pushMap.put("jobId",dialogue.getJobId());
            pushMap.put("identityB",dialogue.getIdentityB());
            pushMap.put("lawyerIdb",dialogue.getLawyerIda());
            pushMap.put("realName",name);
            pushMap.put("logo",logo);
            Map extraFieldMap = new HashMap();//设置消息盒子信息
            extraFieldMap.put("topic",pushMap.get("topic"));
            extraFieldMap.put("jobId",pushMap.get("jobId"));
            extraFieldMap.put("identityB",pushMap.get("identityB"));
            extraFieldMap.put("lawyerIdb",pushMap.get("lawyerIdb"));
            extraFieldMap.put("realName",name);
            extraFieldMap.put("logo",logo);
            allPush.AllPush(pushMap,extraFieldMap);//推送
        }
        i =dialogueMapper.insertSelective(dialogue);
        return i;
    }

    public List selectByIdAorIdB(Map map){

        return dialogueMapper.selectByIdAorIdB(map);
    }

    public List<Map> selectByIdList(Map map){
        return dialogueMapper.selectByIdList(map);
    }


    public Integer selectByIdListCount(Map map){
        return dialogueMapper.selectByIdListCount(map);
    }
    public Integer selectDialogueCount(String topic){
        return dialogueMapper.selectDialogueCount(topic);
    }
    public Integer selectNewTopic(String topic){
        return dialogueMapper.selectNewTopic(topic);
    }
    public Integer updateNewTopic(Map map){
        return dialogueMapper.updateNewTopic(map);
    }

    public int updateRead(Map map){
        return dialogueMapper.updateRead(map);
    }
}
