123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656 |
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>service: Go Coverage Report</title>
- <style>
- body {
- background: black;
- color: rgb(80, 80, 80);
- }
- body, pre, #legend span {
- font-family: Menlo, monospace;
- font-weight: bold;
- }
- #topbar {
- background: black;
- position: fixed;
- top: 0; left: 0; right: 0;
- height: 42px;
- border-bottom: 1px solid rgb(80, 80, 80);
- }
- #content {
- margin-top: 50px;
- }
- #nav, #legend {
- float: left;
- margin-left: 10px;
- }
- #legend {
- margin-top: 12px;
- }
- #nav {
- margin-top: 10px;
- }
- #legend span {
- margin: 0 5px;
- }
- .cov0 { color: rgb(192, 0, 0) }
- .cov1 { color: rgb(128, 128, 128) }
- .cov2 { color: rgb(116, 140, 131) }
- .cov3 { color: rgb(104, 152, 134) }
- .cov4 { color: rgb(92, 164, 137) }
- .cov5 { color: rgb(80, 176, 140) }
- .cov6 { color: rgb(68, 188, 143) }
- .cov7 { color: rgb(56, 200, 146) }
- .cov8 { color: rgb(44, 212, 149) }
- .cov9 { color: rgb(32, 224, 152) }
- .cov10 { color: rgb(20, 236, 155) }
- </style>
- </head>
- <body>
- <div id="topbar">
- <div id="nav">
- <select id="files">
-
- <option value="file0">bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/service/message_mail_box.go (100.0%)</option>
-
- </select>
- </div>
- <div id="legend">
- <span>not tracked</span>
-
- <span class="cov0">not covered</span>
- <span class="cov8">covered</span>
-
- </div>
- </div>
- <div id="content">
-
- <pre class="file" id="file0" style="display: none">package service
- import (
- quitl "app.yhyue.com/moapp/jybase/common"
- util "bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/entity"
- "bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/rpc/messagecenter/messagecenter"
- "database/sql"
- "fmt"
- "log"
- "time"
- )
- type MessaggeService struct{}
- //未读消息查询
- func (b MessaggeService) Count(newUserId, userType, entUserId int64) (count int, last map[string]interface{}, err error) <span class="cov8" title="1">{
- v := make([]interface{}, 0)
- sqlStr := ""
- if userType == 1 </span><span class="cov8" title="1">{
- sqlStr = fmt.Sprintf("select count(b.id) from %s a "+
- "LEFT JOIN %s b ON a.id=b.own_id "+
- "where b.type=5 "+
- "AND a.customer_service_id=%d "+
- "AND b.isread=0 "+
- "AND own_type = 1 "+
- "order by create_time", util.SOCIALIZE_CHAT_SESSION, util.SOCIALIZE_MESSAGE_MAILBOX, entUserId)
- }</span> else<span class="cov8" title="1"> {
- sqlStr = fmt.Sprintf("select count(b.id) from %s b "+
- "where b.send_user_id != %d "+
- "AND b.own_id=%d "+
- "AND b.isread=0 "+
- "AND own_type = 2 "+
- "order by create_time", util.SOCIALIZE_MESSAGE_MAILBOX, newUserId, newUserId)
- }</span>
- <span class="cov8" title="1">log.Println(sqlStr, v)
- rs := util.Mysql.CountBySql(sqlStr, v...)
- log.Println(rs)
- count = int(rs)
- //最后一条信息查询
- sqlStr=fmt.Sprintf("SELECT c.* FROM %s b "+
- "LEFT join %s c on b.messag_id=c.id "+
- "WHERE b.send_user_id != %d "+
- "AND b.own_id = %d "+
- "AND b.isread=0 "+
- "AND own_type = 2 "+
- "ORDER BY create_time DESC "+
- "LIMIT 0,1",util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_MESSAGE,
- newUserId, newUserId)
- lastData := util.Mysql.SelectBySql(sqlStr)
- if len(*lastData) > 0 </span><span class="cov8" title="1">{
- last = (*lastData)[0]
- }</span>
- <span class="cov8" title="1">return</span>
- }
- //用户列表查询
- func (b MessaggeService) UserList(in *messagecenter.UserReq) (data *[]map[string]interface{}, err error) <span class="cov8" title="1">{
- sqlStr := ""
- if in.UserType == 2 </span><span class="cov8" title="1">{
- //用户最后一次信息查询
- userSql := fmt.Sprintf("SELECT MAX( c.id ) as messageId FROM socialize_message_mailbox c "+
- "WHERE c.own_id = %d "+
- "AND c.type = 2 "+
- "AND c.own_type = 2 "+
- "GROUP BY ( CASE WHEN c.send_user_id > c.receive_user_id "+
- "THEN CONCAT( c.send_user_id, c.receive_user_id ) "+
- "WHEN c.send_user_id < c.receive_user_id "+
- "THEN CONCAT( c.receive_user_id, c.send_user_id ) END ) ", in.NewUserId,
- )
- data = util.Mysql.SelectBySql(userSql)
- userMessageId := util.Inhandle(data)
- //客服最后一次信息查询
- customerSql := fmt.Sprintf("SELECT max( c.id ) as messageId FROM %s c "+
- "LEFT JOIN %s d ON IF ( c.send_user_type = 1, d.id = c.send_user_id, d.id = c.receive_user_id ) "+
- "WHERE c.own_id = %d "+
- "AND ( c.type = 4 OR c.type = 5 or c.type=6 or c.type=7 ) "+
- "AND c.own_type = 2 "+
- "AND d.user_id=c.own_id "+
- "GROUP BY d.ent_id ", util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_CHAT_SESSION, in.NewUserId)
- data = util.Mysql.SelectBySql(customerSql)
- customerMessageId := util.Inhandle(data)
- //用户的列表
- sqlStr = fmt.Sprintf("SELECT IF ( b.nickname = '', CONCAT(SUBSTR( b.phone, 1, 3 ),'****',SUBSTR( b.phone, 8, 11)), b.nickname ) AS name, b.id, e.title, b.headimg, e.type, e.link, e.content, 2 AS userType, a.create_time, a.type AS itemType, "+
- "( SELECT count( h.id ) FROM %s h "+
- "WHERE h.type = 2 "+
- "AND h.own_id = %d "+
- "AND h.own_type = 2 "+
- "AND h.send_user_id=b.id "+
- "AND h.isread = 0 ) AS number FROM %s a "+
- "LEFT JOIN %s b ON b.id = a.receive_user_id or b.id = a.send_user_id "+
- "LEFT JOIN %s e ON e.id = a.messag_id "+
- "WHERE a.id IN ( %s ) AND b.id != %d "+
- "UNION ALL "+
- "SELECT f.nickname AS name, b.ent_id AS id, e.title,f.headimage AS headimg, e.type, e.link, e.content, 1 AS userType, a.create_time, a.type AS itemType, "+
- "( SELECT count( h.id ) FROM %s h WHERE ( h.type = 4 OR h.type = 5 ) AND h.own_id = %d AND h.own_type = 2 AND h.send_user_type = 1 AND h.receive_user_id = %d AND h.isread = 0 ) AS number "+
- "FROM %s a "+
- "LEFT JOIN %s b ON IF ( a.send_user_type = 1, b.id = a.send_user_id, b.id = a.receive_user_id ) "+
- "LEFT JOIN %s e ON e.id = a.messag_id "+
- "LEFT join %s f on f.ent_id=b.ent_id "+
- "WHERE a.id IN ( %s ) "+
- "ORDER BY create_time DESC",
- util.SOCIALIZE_MESSAGE_MAILBOX,
- in.NewUserId,
- util.SOCIALIZE_MESSAGE_MAILBOX,
- util.BASE_USER,
- util.SOCIALIZE_MESSAGE,
- userMessageId,
- in.NewUserId,
- util.SOCIALIZE_MESSAGE_MAILBOX,
- in.NewUserId,
- in.NewUserId,
- util.SOCIALIZE_MESSAGE_MAILBOX,
- util.SOCIALIZE_CHAT_SESSION,
- util.SOCIALIZE_MESSAGE,
- util.SOCIALIZE_TENANT_ROBOT,
- customerMessageId)
- }</span> else<span class="cov8" title="1"> {
- phoneSql := ""
- if in.Phone != "" </span><span class="cov8" title="1">{
- phoneSql = "AND b.phone like '% " + in.Phone + "%'"
- }</span>
- <span class="cov8" title="1">startTimeSql := ""
- if in.StartTime != "" </span><span class="cov8" title="1">{
- startTimeSql = "AND DATE_FORMAT(c.create_time,'%Y-%m-%d') >= ' " + in.StartTime + "' "
- }</span>
- <span class="cov8" title="1">endTimeSql := ""
- if in.EndTime != "" </span><span class="cov8" title="1">{
- endTimeSql = "AND DATE_FORMAT(c.create_time,'%Y-%m-%d') <= ' " + in.EndTime + "' "
- }</span>
- //先获取每个用户最后聊天记录
- <span class="cov8" title="1">userSql := fmt.Sprintf("SELECT MAX( c.id ) as messageId FROM %s c "+
- "LEFT JOIN %s d ON c.own_type=1 AND c.own_id=d.id "+
- "WHERE c.own_type = 1 "+
- "AND d.customer_service_id = %d "+
- "AND ( c.type = 4 OR c.type = 5 or c.type=6 or c.type=7 ) "+
- "AND d.customer_service_id !=0 %s %s "+
- "GROUP BY ( CASE WHEN c.send_user_type =2 THEN CONCAT( c.send_user_id ) WHEN c.send_user_type =1 THEN CONCAT( c.receive_user_id ) END ) ",
- util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_CHAT_SESSION,
- in.EntUserId, startTimeSql, endTimeSql)
- data = util.Mysql.SelectBySql(userSql)
- if data != nil </span><span class="cov8" title="1">{
- customerMessageId := util.Inhandle(data)
- //客服的用户列表
- sqlStr = fmt.Sprintf("SELECT IF ( b.nickname = '', CONCAT(SUBSTR( b.phone, 1, 3 ),'****',SUBSTR( b.phone, 8, 11)), b.nickname ) AS name, b.id, e.title, b.headimg, e.type, e.link, e.content, a.create_time, "+
- "( SELECT count( h.id ) FROM %s h "+
- "LEFT JOIN %s i ON h.own_type = 1 AND h.own_id = i.id "+
- "WHERE h.own_type = 1 "+
- "AND i.ent_id=f.ent_id "+
- "AND i.user_id=f.user_id "+
- "AND h.isread = 0 "+
- "AND i.customer_service_id= %d "+
- "FROM %s a "+
- "LEFT JOIN %s b ON if (a.send_user_type=1 ,a.receive_user_id=b.id,a.send_user_id=b.id) "+
- "LEFT JOIN %s e ON e.id = a.messag_id "+
- "LEFT JOIN %s f ON a.own_type=1 AND a.own_id=f.id "+
- "WHERE a.id IN ( %s) %s ORDER BY a.create_time DESC",
- util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_CHAT_SESSION,
- in.EntUserId, util.SOCIALIZE_MESSAGE_MAILBOX, util.BASE_USER, util.SOCIALIZE_MESSAGE, util.SOCIALIZE_CHAT_SESSION, customerMessageId, phoneSql)
- }</span>
- }
- <span class="cov8" title="1">if sqlStr != "" </span><span class="cov8" title="1">{
- log.Println(sqlStr)
- data = util.Mysql.SelectBySql(sqlStr)
- }</span>
- <span class="cov8" title="1">return</span>
- }
- //消息保存
- func (b MessaggeService) SaveMessage(in *messagecenter.MessageEntity) (fool bool, errorMsg string, content string, messageId, nowInt int64) <span class="cov8" title="1">{
- //先插入信息表
- //判断会话标识是否属于本人
- nowForm := time.Now().Local()
- if in.ItemType != 2 </span><span class="cov8" title="1">{
- userId := int64(0)
- sessionId := int64(0)
- switch in.ItemType </span>{
- case 4, 5:<span class="cov8" title="1">
- if in.OwnType == 1 </span><span class="cov8" title="1">{
- sessionId = in.ReceiveId
- userId = in.NewUserId
- }</span> else<span class="cov8" title="1"> {
- sessionId = in.SendId
- userId = in.ReceiveId
- if in.ItemType == 4 </span><span class="cov8" title="1">{
- userId = in.NewUserId
- }</span>
- }
- <span class="cov8" title="1">break</span>
- case 6:<span class="cov8" title="1">
- if in.OwnType == 1 </span><span class="cov8" title="1">{
- sessionId = in.ReceiveId
- userId = in.NewUserId
- }</span> else<span class="cov8" title="1"> if in.OwnType == 2 </span><span class="cov8" title="1">{
- sessionId = in.SendId
- userId = in.ReceiveId
- }</span> else<span class="cov8" title="1"> {
- sessionId = in.ReceiveId
- userId = in.NewUserId
- }</span>
- <span class="cov8" title="1">break</span>
- }
- //查找会话信息
- <span class="cov8" title="1">chatJson := util.Mysql.FindOne(util.SOCIALIZE_CHAT_SESSION, map[string]interface{}{"id": sessionId}, "user_id", "")
- if chatJson == nil </span><span class="cov8" title="1">{
- return false, "会话标识不存在", "", 0, nowForm.Unix()
- }</span> else<span class="cov8" title="1"> {
- if userId != quitl.Int64All((*chatJson)["user_id"]) </span><span class="cov8" title="1">{
- return false, "会话标识不属于此用户", "", 0, nowForm.Unix()
- }</span>
- }
- }
- <span class="cov8" title="1">fool = util.Mysql.ExecTx("聊天信息保存", func(tx *sql.Tx) bool </span><span class="cov8" title="1">{
- //先插入信息表
- message := map[string]interface{}{
- "appid": in.Appid,
- "title": in.Title,
- "content": in.Content,
- "item": in.Item,
- "type": in.Type,
- "link": in.Link,
- "create_time": nowForm.Format(util.Date_Full_Layout),
- "create_person": in.SendId,
- }
- ok := util.Mysql.InsertByTx(tx, util.SOCIALIZE_MESSAGE, message)
- receiveOk := int64(0)
- messageId = ok
- //在插入邮箱表socialize_message_mailbox
- messageMailBox := map[string]interface{}{
- "appid": in.Appid,
- "messag_id": ok,
- "type": in.ItemType,
- "create_time": nowForm.Format(util.Date_Full_Layout),
- "isread": 0,
- "send_isdel": 0,
- "receive_isdel": 0,
- "iswithdraw": 0,
- }
- //系统信息处理
- if in.ItemType == 6 </span><span class="cov8" title="1">{
- messageMailBox = map[string]interface{}{
- "appid": in.Appid,
- "messag_id": ok,
- "type": in.ItemType,
- "create_time": nowForm.Format(util.Date_Full_Layout),
- "read_time": nowForm.Format(util.Date_Full_Layout),
- "isread": 1,
- "send_isdel": 0,
- "receive_isdel": 0,
- "iswithdraw": 0,
- }
- if in.OwnType == 1 </span><span class="cov8" title="1">{
- //用户接受系统信息
- messageMailBox["own_type"] = 2
- messageMailBox["send_user_type"] = 1
- messageMailBox["receive_user_type"] = 2
- messageMailBox["own_id"] = in.NewUserId
- messageMailBox["send_user_id"] = in.ReceiveId
- messageMailBox["receive_user_id"] = in.NewUserId
- }</span> else<span class="cov8" title="1"> if in.OwnType == 2 </span><span class="cov8" title="1">{
- //客服接受系统信息
- messageMailBox["own_type"] = 1
- messageMailBox["send_user_type"] = 2
- messageMailBox["receive_user_type"] = 1
- messageMailBox["own_id"] = in.SendId
- messageMailBox["send_user_id"] = in.ReceiveId
- messageMailBox["receive_user_id"] = in.SendId
- }</span> else<span class="cov8" title="1"> {
- //客服接受系统信息
- messageMailBox["own_type"] = 1
- messageMailBox["send_user_type"] = 2
- messageMailBox["receive_user_type"] = 1
- messageMailBox["own_id"] = in.ReceiveId
- messageMailBox["send_user_id"] = in.NewUserId
- messageMailBox["receive_user_id"] = in.ReceiveId
- }</span>
- <span class="cov8" title="1">receiveOk = util.Mysql.InsertByTx(tx, util.SOCIALIZE_MESSAGE_MAILBOX, messageMailBox)
- return ok > 1 && receiveOk > 1</span>
- }
- <span class="cov8" title="1">if in.ItemType == 4 || in.ItemType == 5 </span><span class="cov8" title="1">{
- //客服或者机器人聊天
- if in.OwnType == 1 </span><span class="cov8" title="1">{
- // (用户发送)客服接受
- messageMailBox["own_type"] = 1
- messageMailBox["send_user_type"] = 2
- messageMailBox["receive_user_type"] = 1
- messageMailBox["own_id"] = in.ReceiveId
- messageMailBox["send_user_id"] = in.NewUserId
- messageMailBox["receive_user_id"] = in.ReceiveId
- }</span> else<span class="cov8" title="1"> {
- //客服发送(用户接收信息)
- messageMailBox["own_type"] = 2
- messageMailBox["send_user_type"] = 1
- messageMailBox["receive_user_type"] = 2
- messageMailBox["own_id"] = in.ReceiveId
- messageMailBox["send_user_id"] = in.SendId
- messageMailBox["receive_user_id"] = in.ReceiveId
- if in.ItemType == 4 </span><span class="cov8" title="1">{
- messageMailBox["receive_user_id"] = in.NewUserId
- messageMailBox["own_id"] = in.NewUserId
- messageMailBox["isread"] = 1
- messageMailBox["read_time"] = nowForm.Format(util.Date_Full_Layout)
- }</span>
- }
- } else<span class="cov8" title="1"> {
- messageMailBox["own_type"] = 2
- messageMailBox["send_user_type"] = 2
- messageMailBox["receive_user_type"] = 2
- messageMailBox["own_id"] = in.ReceiveId
- messageMailBox["send_user_id"] = in.NewUserId
- messageMailBox["receive_user_id"] = in.ReceiveId
- }</span>
- <span class="cov8" title="1">receiveOk = util.Mysql.InsertByTx(tx, util.SOCIALIZE_MESSAGE_MAILBOX, messageMailBox)
- messageMailBox = map[string]interface{}{
- "appid": in.Appid,
- "messag_id": ok,
- "type": in.ItemType,
- "create_time": nowForm.Format(util.Date_Full_Layout),
- "read_time": nowForm.Format(util.Date_Full_Layout),
- "isread": 1,
- "send_isdel": 0,
- "receive_isdel": 0,
- "iswithdraw": 0,
- }
- if in.ItemType == 4 || in.ItemType == 5 </span><span class="cov8" title="1">{
- //客服或者机器人聊天
- if in.OwnType == 1 </span><span class="cov8" title="1">{
- //用户发送(用户接受)
- messageMailBox["own_type"] = 2
- messageMailBox["send_user_type"] = 2
- messageMailBox["receive_user_type"] = 1
- messageMailBox["own_id"] = in.NewUserId
- messageMailBox["send_user_id"] = in.NewUserId
- messageMailBox["receive_user_id"] = in.ReceiveId
- }</span> else<span class="cov8" title="1"> {
- //客服发送信息(用户接受)
- messageMailBox["own_type"] = 1
- messageMailBox["send_user_type"] = 1
- messageMailBox["receive_user_type"] = 2
- messageMailBox["own_id"] = in.SendId
- messageMailBox["send_user_id"] = in.SendId
- messageMailBox["receive_user_id"] = in.ReceiveId
- if in.ItemType == 4 </span><span class="cov8" title="1">{
- messageMailBox["receive_user_id"] = in.NewUserId
- }</span>
- }
- } else<span class="cov8" title="1"> {
- messageMailBox["own_type"] = 2
- messageMailBox["send_user_type"] = 2
- messageMailBox["receive_user_type"] = 2
- messageMailBox["own_id"] = in.NewUserId
- messageMailBox["send_user_id"] = in.NewUserId
- messageMailBox["receive_user_id"] = in.ReceiveId
- }</span>
- <span class="cov8" title="1">receiveOk = util.Mysql.InsertByTx(tx, util.SOCIALIZE_MESSAGE_MAILBOX, messageMailBox)
- return ok > 1 && receiveOk > 1</span>
- })
- <span class="cov8" title="1">return fool, "", in.Content, messageId, nowForm.Unix()</span>
- }
- //历史信息查询
- func (b MessaggeService) FindMessage(in *messagecenter.MessageReq) *[]map[string]interface{} <span class="cov8" title="1">{
- sqlStr := ""
- lastStr := ""
- if in.LastId > 0 </span><span class="cov8" title="1">{
- if in.Sort == "asc" </span><span class="cov8" title="1">{
- lastStr = fmt.Sprintf("AND a.messag_id > %d ", in.LastId)
- }</span> else<span class="cov8" title="1"> {
- lastStr = fmt.Sprintf("AND a.messag_id < %d ", in.LastId)
- }</span>
- }
- <span class="cov8" title="1">switch in.MsgType </span>{
- case 2:<span class="cov8" title="1"> //点对点聊天
- sqlStr = fmt.Sprintf("SELECT a.messag_id as messageId,b.* ,if(a.own_id = a.send_user_id,1,2) as fool "+
- "FROM %s a "+
- "LEFT JOIN %s b on a.messag_id=b.id "+
- "LEFT JOIN %s c on c.id=a.send_user_id "+
- "LEFT JOIN %s d on d.id=a.receive_user_id "+
- "where a.own_id= %d "+
- "AND ((a.send_user_id= %d AND a.receive_user_id= %d) or (a.send_user_id= %d AND a.receive_user_id= %d)) "+
- "AND a.type=2 %s "+
- "ORDER BY a.create_time desc ,a.id asc "+
- "limit 0 , %d ",
- util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_MESSAGE, util.BASE_USER, util.BASE_USER,
- in.NewUserId, in.NewUserId, in.SendId, in.SendId, in.NewUserId, lastStr, in.PageSize)
- break</span>
- case 4, 5, 6, 7:<span class="cov8" title="1"> //客服聊天
- if in.UserType == 1 </span><span class="cov8" title="1">{
- //客服聊天记录查看
- sqlStr = fmt.Sprintf("SELECT a.messag_id as messageId,b.*, IF ( a.own_id = a.send_user_id, 1, 2 ) AS fool, a.send_user_type, a.type AS itemType, '' AS robotName, '' AS robotImg, c.customer_service_name AS setName , c.user_id "+
- "FROM %s a "+
- "LEFT JOIN %s b ON a.messag_id = b.id "+
- "LEFT JOIN %s c ON a.own_type = 1 AND a.own_id=c.id "+
- "WHERE a.own_type = 1 "+
- "AND (a.type = 5 or a.type=4 or a.type=6 or a.type=7) "+
- "AND c.ent_id = %d "+
- "AND c.user_id = %d %s "+
- "ORDER BY a.create_time desc ,a.id asc "+
- "limit 0 , %d ",
- util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_MESSAGE, util.SOCIALIZE_CHAT_SESSION,
- in.EntId, in.SendId, lastStr, in.PageSize)
- }</span> else<span class="cov8" title="1"> {
- //用户聊天记录查看
- sqlStr = fmt.Sprintf("SELECT a.messag_id as messageId, b.*, IF ( a.own_id = a.send_user_id, 1, 2 ) AS fool , a.send_user_type, a.type as itemType, d.nickname as robotName, d.headimage as robotImg, c.customer_service_name as setName "+
- "FROM %s a "+
- "LEFT JOIN %s b ON a.messag_id = b.id "+
- "LEFT JOIN %s c ON IF ( a.send_user_type = 1, c.id = a.send_user_id, c.id = a.receive_user_id ) AND c.ent_id = %d AND c.user_id = %d "+
- "LEFT JOIN %s d on c.ent_id=d.ent_id "+
- "WHERE a.own_type = 2 "+
- "AND a.own_id = %d "+
- "AND c.ent_id = %d "+
- "AND c.user_id = %d "+
- "AND ( a.type = 4 OR a.type = 5 or a.type=6 or a.type=7) %s "+
- "ORDER BY a.create_time desc ,a.id asc "+
- "limit 0 , %d ",
- util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_MESSAGE, util.SOCIALIZE_CHAT_SESSION, in.SendId, in.NewUserId, util.SOCIALIZE_TENANT_ROBOT,
- in.NewUserId, in.SendId, in.NewUserId, lastStr, in.PageSize)
- }</span>
- <span class="cov8" title="1">break</span>
- }
- <span class="cov8" title="1">log.Println(sqlStr)
- data := util.Mysql.SelectBySql(sqlStr)
- //自己头像处理
- if in.UserType == 2 </span><span class="cov8" title="1">{
- userData := util.Mysql.FindOne(util.BASE_USER, map[string]interface{}{"id": in.NewUserId}, "headimg", "")
- if userData != nil </span><span class="cov8" title="1">{
- for key := range *data </span><span class="cov8" title="1">{
- (*data)[key]["ownImg"] = (*userData)["headimg"]
- }</span>
- }
- }
- <span class="cov8" title="1">go func() </span><span class="cov8" title="1">{
- updateMap := map[string]interface{}{}
- if len(*data) > 0 </span><span class="cov8" title="1">{
- //未读信息修改
- switch in.MsgType </span>{
- case 2:<span class="cov8" title="1"> //点对点聊天
- updateMap = map[string]interface{}{
- "own_type": 2,
- "own_id": in.NewUserId,
- "type": 2,
- "isread": 0,
- }
- util.Mysql.Update(util.SOCIALIZE_MESSAGE_MAILBOX, updateMap, map[string]interface{}{"isread": 1, "read_time": time.Now().Local().Format(util.Date_Full_Layout)})
- break</span>
- case 4, 5:<span class="cov8" title="1">
- sqlStr := ""
- if in.UserType == 1 </span><span class="cov8" title="1">{
- sqlStr = fmt.Sprintf("UPDATE %s a SET a.isread = 1, a.read_time = now( ) "+
- "WHERE a.own_type = 1 "+
- "AND a.type IN ( 4,5,6,7) "+
- "AND a.isread = 0 "+
- "AND a.own_id IN ( SELECT b.id FROM %s b WHERE b.customer_service_id=%d AND b.user_id=%d )",
- util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_CHAT_SESSION, in.EntUserId, in.SendId)
- }</span> else<span class="cov8" title="1"> {
- sqlStr = fmt.Sprintf("UPDATE %s a SET a.isread = 1, a.read_time = now( ) "+
- "WHERE a.own_type = 2 "+
- "AND a.type IN ( 4,5,6,7 ) "+
- "AND a.isread = 0 "+
- "AND a.own_id =%d ", util.SOCIALIZE_MESSAGE_MAILBOX, in.NewUserId)
- }</span>
- <span class="cov8" title="1">util.Mysql.UpdateOrDeleteBySql(sqlStr)
- break</span>
- }
- }
- }()
- <span class="cov8" title="1">return data</span>
- }
- //创建会话
- func (b MessaggeService) CreateChatSession(in *messagecenter.ChatSessionReq) (fool bool, sessionId int64) <span class="cov8" title="1">{
- fool = util.Mysql.ExecTx("会话新建", func(tx *sql.Tx) bool </span><span class="cov8" title="1">{
- customerserviceName := in.CustomerserviceName
- if in.CustomerServiceId != 0 </span><span class="cov8" title="1">{
- sqlStr:=fmt.Sprintf("select customer_service_name from %s "+
- "where ent_id= %d "+
- "AND customer_service_id= %d ",
- util.SOCIALIZE_CHAT_SESSION, in.EntId, in.CustomerServiceId)
- customerList := util.Mysql.SelectBySql(sqlStr)
- if len(*customerList) > 0 </span><span class="cov8" title="1">{
- customerserviceName = quitl.InterfaceToStr((*customerList)[0]["customer_service_name"])
- }</span>
- }
- <span class="cov8" title="1">chatMession := map[string]interface{}{
- "handle_status": 0,
- "start_time": time.Now().Local().Format(util.Date_Full_Layout),
- "appid": in.AppId,
- "ent_id": in.EntId,
- "customer_service_id": in.CustomerServiceId,
- "user_id": in.UserId,
- "customer_service_name": customerserviceName,
- }
- sessionId = util.Mysql.InsertByTx(tx, util.SOCIALIZE_CHAT_SESSION, chatMession)
- return sessionId > 0</span>
- })
- <span class="cov8" title="1">return fool, sessionId</span>
- }
- //结束会话
- func (b MessaggeService) CloseChatSession(in *messagecenter.CloseSessionReq) bool <span class="cov8" title="1">{
- fool := util.Mysql.ExecTx("关闭会话", func(tx *sql.Tx) bool </span><span class="cov8" title="1">{
- updateMap := map[string]interface{}{
- "id": in.SessionId,
- }
- fool := util.Mysql.Update(util.SOCIALIZE_CHAT_SESSION, updateMap, map[string]interface{}{"start_time": time.Now().Local().Format(util.Date_Full_Layout)})
- return fool
- }</span>)
- <span class="cov8" title="1">return fool</span>
- }
- //创建会话并保存信息
- func (b *MessaggeService) SaveAutoReplyMsg(userType, entId, entUserId, userId int64, content, appId, nowFormat string) (bool, int64) <span class="cov8" title="1">{
- messageId := int64(0)
- return util.Mysql.ExecTx("保存自动回复消息", func(tx *sql.Tx) bool </span><span class="cov8" title="1">{
- entUserName := ""
- if entUserId > 0 </span><span class="cov8" title="1">{
- list := util.Mysql.SelectBySql(`select ? from socialize_tenant_seat where appid=? AND ent_id=? AND customer_service_id=?`, util.SOCIALIZE_CHAT_SESSION, appId, entId, entUserId)
- if list != nil && len(*list) > 0 </span><span class="cov8" title="1">{
- entUserName, _ = (*list)[0]["customer_service_name"].(string)
- }</span>
- }
- <span class="cov8" title="1">messageId = util.Mysql.InsertBySqlByTx(tx, `insert into ? (appid,content,item,type,create_time,create_person) values (?,?,?,?,?,?)`, util.SOCIALIZE_MESSAGE, appId, content, 8, 1, nowFormat, "admin")
- sessionId := util.Mysql.InsertBySqlByTx(tx, `insert into ? (appid,type,ent_id,customer_service_id,customer_service_name,user_id,start_time,end_time) values (?,?,?,?,?,?,?,?)`, util.SOCIALIZE_CHAT_SESSION, appId, 1, entId, entUserId, entUserName, userId, nowFormat, nowFormat)
- ok := false
- if userType == 0 </span><span class="cov8" title="1">{
- ok1 := util.Mysql.InsertBySqlByTx(tx, `insert into ? (appid,messag_id,type,send_user_id,send_user_type,receive_user_id,receive_user_type,own_id,own_type,create_time,isread,read_time) values (?,?,?,?,?,?,?,?,?,?,?,?)`, util.SOCIALIZE_MESSAGE_MAILBOX, appId, messageId, 7, sessionId, 1, userId, 2, sessionId, 1, nowFormat, 1, nowFormat) > 0
- ok2 := util.Mysql.InsertBySqlByTx(tx, `insert into ? (appid,messag_id,type,send_user_id,send_user_type,receive_user_id,receive_user_type,own_id,own_type,create_time) values (?,?,?,?,?,?,?,?,?,?)`, util.SOCIALIZE_MESSAGE_MAILBOX, appId, messageId, 7, sessionId, 1, userId, 2, userId, 2, nowFormat) > 0
- ok = ok1 && ok2
- }</span> else<span class="cov8" title="1"> if userType == 1 </span><span class="cov8" title="1">{
- ok = util.Mysql.InsertBySqlByTx(tx, `insert into ? (appid,messag_id,type,send_user_id,send_user_type,receive_user_id,receive_user_type,own_id,own_type,create_time,isread,read_time) values (?,?,?,?,?,?,?,?,?,?,?,?)`, util.SOCIALIZE_MESSAGE_MAILBOX, appId, messageId, 7, sessionId, 1, userId, 2, sessionId, 1, nowFormat, 1, nowFormat) > 0
- }</span> else<span class="cov8" title="1"> if userType == 2 </span><span class="cov8" title="1">{
- ok = util.Mysql.InsertBySqlByTx(tx, `insert into ? (appid,messag_id,type,send_user_id,send_user_type,receive_user_id,receive_user_type,own_id,own_type,create_time) values (?,?,?,?,?,?,?,?,?,?)`, util.SOCIALIZE_MESSAGE_MAILBOX, appId, messageId, 7, sessionId, 1, userId, 2, userId, 2, nowFormat) > 0
- }</span>
- <span class="cov8" title="1">return messageId > 0 && sessionId > 0 && ok</span>
- }), messageId
- }
- //修改未读状态
- func (b MessaggeService) UpdateReadById(in *messagecenter.ReadStateReq) bool <span class="cov8" title="1">{
- fool := util.Mysql.ExecTx("已读状态修改", func(tx *sql.Tx) bool </span><span class="cov8" title="1">{
- updateMap := map[string]interface{}{
- "messag_id": in.MessageId,
- "isread": 0,
- }
- fool := util.Mysql.Update(util.SOCIALIZE_MESSAGE_MAILBOX, updateMap, map[string]interface{}{"read_time": time.Now().Local().Format(util.Date_Full_Layout), "isread": 1})
- return fool
- }</span>)
- <span class="cov8" title="1">return fool</span>
- }
- </pre>
-
- </div>
- </body>
- <script>
- (function() {
- var files = document.getElementById('files');
- var visible;
- files.addEventListener('change', onChange, false);
- function select(part) {
- if (visible)
- visible.style.display = 'none';
- visible = document.getElementById(part);
- if (!visible)
- return;
- files.value = part;
- visible.style.display = 'block';
- location.hash = part;
- }
- function onChange() {
- select(files.value);
- window.scrollTo(0, 0);
- }
- if (location.hash != "") {
- select(location.hash.substr(1));
- }
- if (!visible) {
- select("file0");
- }
- })();
- </script>
- </html>
|