coverage.html 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>service: Go Coverage Report</title>
  6. <style>
  7. body {
  8. background: black;
  9. color: rgb(80, 80, 80);
  10. }
  11. body, pre, #legend span {
  12. font-family: Menlo, monospace;
  13. font-weight: bold;
  14. }
  15. #topbar {
  16. background: black;
  17. position: fixed;
  18. top: 0; left: 0; right: 0;
  19. height: 42px;
  20. border-bottom: 1px solid rgb(80, 80, 80);
  21. }
  22. #content {
  23. margin-top: 50px;
  24. }
  25. #nav, #legend {
  26. float: left;
  27. margin-left: 10px;
  28. }
  29. #legend {
  30. margin-top: 12px;
  31. }
  32. #nav {
  33. margin-top: 10px;
  34. }
  35. #legend span {
  36. margin: 0 5px;
  37. }
  38. .cov0 { color: rgb(192, 0, 0) }
  39. .cov1 { color: rgb(128, 128, 128) }
  40. .cov2 { color: rgb(116, 140, 131) }
  41. .cov3 { color: rgb(104, 152, 134) }
  42. .cov4 { color: rgb(92, 164, 137) }
  43. .cov5 { color: rgb(80, 176, 140) }
  44. .cov6 { color: rgb(68, 188, 143) }
  45. .cov7 { color: rgb(56, 200, 146) }
  46. .cov8 { color: rgb(44, 212, 149) }
  47. .cov9 { color: rgb(32, 224, 152) }
  48. .cov10 { color: rgb(20, 236, 155) }
  49. </style>
  50. </head>
  51. <body>
  52. <div id="topbar">
  53. <div id="nav">
  54. <select id="files">
  55. <option value="file0">bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/service/message_mail_box.go (100.0%)</option>
  56. </select>
  57. </div>
  58. <div id="legend">
  59. <span>not tracked</span>
  60. <span class="cov0">not covered</span>
  61. <span class="cov8">covered</span>
  62. </div>
  63. </div>
  64. <div id="content">
  65. <pre class="file" id="file0" style="display: none">package service
  66. import (
  67. quitl "app.yhyue.com/moapp/jybase/common"
  68. util "bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/entity"
  69. "bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/rpc/messagecenter/messagecenter"
  70. "database/sql"
  71. "fmt"
  72. "log"
  73. "time"
  74. )
  75. type MessaggeService struct{}
  76. //未读消息查询
  77. func (b MessaggeService) Count(newUserId, userType, entUserId int64) (count int, last map[string]interface{}, err error) <span class="cov8" title="1">{
  78. v := make([]interface{}, 0)
  79. sqlStr := ""
  80. if userType == 1 </span><span class="cov8" title="1">{
  81. sqlStr = fmt.Sprintf("select count(b.id) from %s a "+
  82. "LEFT JOIN %s b ON a.id=b.own_id "+
  83. "where b.type=5 "+
  84. "AND a.customer_service_id=%d "+
  85. "AND b.isread=0 "+
  86. "AND own_type = 1 "+
  87. "order by create_time", util.SOCIALIZE_CHAT_SESSION, util.SOCIALIZE_MESSAGE_MAILBOX, entUserId)
  88. }</span> else<span class="cov8" title="1"> {
  89. sqlStr = fmt.Sprintf("select count(b.id) from %s b "+
  90. "where b.send_user_id != %d "+
  91. "AND b.own_id=%d "+
  92. "AND b.isread=0 "+
  93. "AND own_type = 2 "+
  94. "order by create_time", util.SOCIALIZE_MESSAGE_MAILBOX, newUserId, newUserId)
  95. }</span>
  96. <span class="cov8" title="1">log.Println(sqlStr, v)
  97. rs := util.Mysql.CountBySql(sqlStr, v...)
  98. log.Println(rs)
  99. count = int(rs)
  100. //最后一条信息查询
  101. sqlStr=fmt.Sprintf("SELECT c.* FROM %s b "+
  102. "LEFT join %s c on b.messag_id=c.id "+
  103. "WHERE b.send_user_id != %d "+
  104. "AND b.own_id = %d "+
  105. "AND b.isread=0 "+
  106. "AND own_type = 2 "+
  107. "ORDER BY create_time DESC "+
  108. "LIMIT 0,1",util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_MESSAGE,
  109. newUserId, newUserId)
  110. lastData := util.Mysql.SelectBySql(sqlStr)
  111. if len(*lastData) &gt; 0 </span><span class="cov8" title="1">{
  112. last = (*lastData)[0]
  113. }</span>
  114. <span class="cov8" title="1">return</span>
  115. }
  116. //用户列表查询
  117. func (b MessaggeService) UserList(in *messagecenter.UserReq) (data *[]map[string]interface{}, err error) <span class="cov8" title="1">{
  118. sqlStr := ""
  119. if in.UserType == 2 </span><span class="cov8" title="1">{
  120. //用户最后一次信息查询
  121. userSql := fmt.Sprintf("SELECT MAX( c.id ) as messageId FROM socialize_message_mailbox c "+
  122. "WHERE c.own_id = %d "+
  123. "AND c.type = 2 "+
  124. "AND c.own_type = 2 "+
  125. "GROUP BY ( CASE WHEN c.send_user_id &gt; c.receive_user_id "+
  126. "THEN CONCAT( c.send_user_id, c.receive_user_id ) "+
  127. "WHEN c.send_user_id &lt; c.receive_user_id "+
  128. "THEN CONCAT( c.receive_user_id, c.send_user_id ) END ) ", in.NewUserId,
  129. )
  130. data = util.Mysql.SelectBySql(userSql)
  131. userMessageId := util.Inhandle(data)
  132. //客服最后一次信息查询
  133. customerSql := fmt.Sprintf("SELECT max( c.id ) as messageId FROM %s c "+
  134. "LEFT JOIN %s d ON IF ( c.send_user_type = 1, d.id = c.send_user_id, d.id = c.receive_user_id ) "+
  135. "WHERE c.own_id = %d "+
  136. "AND ( c.type = 4 OR c.type = 5 or c.type=6 or c.type=7 ) "+
  137. "AND c.own_type = 2 "+
  138. "AND d.user_id=c.own_id "+
  139. "GROUP BY d.ent_id ", util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_CHAT_SESSION, in.NewUserId)
  140. data = util.Mysql.SelectBySql(customerSql)
  141. customerMessageId := util.Inhandle(data)
  142. //用户的列表
  143. 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, "+
  144. "( SELECT count( h.id ) FROM %s h "+
  145. "WHERE h.type = 2 "+
  146. "AND h.own_id = %d "+
  147. "AND h.own_type = 2 "+
  148. "AND h.send_user_id=b.id "+
  149. "AND h.isread = 0 ) AS number FROM %s a "+
  150. "LEFT JOIN %s b ON b.id = a.receive_user_id or b.id = a.send_user_id "+
  151. "LEFT JOIN %s e ON e.id = a.messag_id "+
  152. "WHERE a.id IN ( %s ) AND b.id != %d "+
  153. "UNION ALL "+
  154. "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, "+
  155. "( 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 "+
  156. "FROM %s a "+
  157. "LEFT JOIN %s b ON IF ( a.send_user_type = 1, b.id = a.send_user_id, b.id = a.receive_user_id ) "+
  158. "LEFT JOIN %s e ON e.id = a.messag_id "+
  159. "LEFT join %s f on f.ent_id=b.ent_id "+
  160. "WHERE a.id IN ( %s ) "+
  161. "ORDER BY create_time DESC",
  162. util.SOCIALIZE_MESSAGE_MAILBOX,
  163. in.NewUserId,
  164. util.SOCIALIZE_MESSAGE_MAILBOX,
  165. util.BASE_USER,
  166. util.SOCIALIZE_MESSAGE,
  167. userMessageId,
  168. in.NewUserId,
  169. util.SOCIALIZE_MESSAGE_MAILBOX,
  170. in.NewUserId,
  171. in.NewUserId,
  172. util.SOCIALIZE_MESSAGE_MAILBOX,
  173. util.SOCIALIZE_CHAT_SESSION,
  174. util.SOCIALIZE_MESSAGE,
  175. util.SOCIALIZE_TENANT_ROBOT,
  176. customerMessageId)
  177. }</span> else<span class="cov8" title="1"> {
  178. phoneSql := ""
  179. if in.Phone != "" </span><span class="cov8" title="1">{
  180. phoneSql = "AND b.phone like '% " + in.Phone + "%'"
  181. }</span>
  182. <span class="cov8" title="1">startTimeSql := ""
  183. if in.StartTime != "" </span><span class="cov8" title="1">{
  184. startTimeSql = "AND DATE_FORMAT(c.create_time,'%Y-%m-%d') &gt;= ' " + in.StartTime + "' "
  185. }</span>
  186. <span class="cov8" title="1">endTimeSql := ""
  187. if in.EndTime != "" </span><span class="cov8" title="1">{
  188. endTimeSql = "AND DATE_FORMAT(c.create_time,'%Y-%m-%d') &lt;= ' " + in.EndTime + "' "
  189. }</span>
  190. //先获取每个用户最后聊天记录
  191. <span class="cov8" title="1">userSql := fmt.Sprintf("SELECT MAX( c.id ) as messageId FROM %s c "+
  192. "LEFT JOIN %s d ON c.own_type=1 AND c.own_id=d.id "+
  193. "WHERE c.own_type = 1 "+
  194. "AND d.customer_service_id = %d "+
  195. "AND ( c.type = 4 OR c.type = 5 or c.type=6 or c.type=7 ) "+
  196. "AND d.customer_service_id !=0 %s %s "+
  197. "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 ) ",
  198. util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_CHAT_SESSION,
  199. in.EntUserId, startTimeSql, endTimeSql)
  200. data = util.Mysql.SelectBySql(userSql)
  201. if data != nil </span><span class="cov8" title="1">{
  202. customerMessageId := util.Inhandle(data)
  203. //客服的用户列表
  204. 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, "+
  205. "( SELECT count( h.id ) FROM %s h "+
  206. "LEFT JOIN %s i ON h.own_type = 1 AND h.own_id = i.id "+
  207. "WHERE h.own_type = 1 "+
  208. "AND i.ent_id=f.ent_id "+
  209. "AND i.user_id=f.user_id "+
  210. "AND h.isread = 0 "+
  211. "AND i.customer_service_id= %d "+
  212. "FROM %s a "+
  213. "LEFT JOIN %s b ON if (a.send_user_type=1 ,a.receive_user_id=b.id,a.send_user_id=b.id) "+
  214. "LEFT JOIN %s e ON e.id = a.messag_id "+
  215. "LEFT JOIN %s f ON a.own_type=1 AND a.own_id=f.id "+
  216. "WHERE a.id IN ( %s) %s ORDER BY a.create_time DESC",
  217. util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_CHAT_SESSION,
  218. in.EntUserId, util.SOCIALIZE_MESSAGE_MAILBOX, util.BASE_USER, util.SOCIALIZE_MESSAGE, util.SOCIALIZE_CHAT_SESSION, customerMessageId, phoneSql)
  219. }</span>
  220. }
  221. <span class="cov8" title="1">if sqlStr != "" </span><span class="cov8" title="1">{
  222. log.Println(sqlStr)
  223. data = util.Mysql.SelectBySql(sqlStr)
  224. }</span>
  225. <span class="cov8" title="1">return</span>
  226. }
  227. //消息保存
  228. func (b MessaggeService) SaveMessage(in *messagecenter.MessageEntity) (fool bool, errorMsg string, content string, messageId, nowInt int64) <span class="cov8" title="1">{
  229. //先插入信息表
  230. //判断会话标识是否属于本人
  231. nowForm := time.Now().Local()
  232. if in.ItemType != 2 </span><span class="cov8" title="1">{
  233. userId := int64(0)
  234. sessionId := int64(0)
  235. switch in.ItemType </span>{
  236. case 4, 5:<span class="cov8" title="1">
  237. if in.OwnType == 1 </span><span class="cov8" title="1">{
  238. sessionId = in.ReceiveId
  239. userId = in.NewUserId
  240. }</span> else<span class="cov8" title="1"> {
  241. sessionId = in.SendId
  242. userId = in.ReceiveId
  243. if in.ItemType == 4 </span><span class="cov8" title="1">{
  244. userId = in.NewUserId
  245. }</span>
  246. }
  247. <span class="cov8" title="1">break</span>
  248. case 6:<span class="cov8" title="1">
  249. if in.OwnType == 1 </span><span class="cov8" title="1">{
  250. sessionId = in.ReceiveId
  251. userId = in.NewUserId
  252. }</span> else<span class="cov8" title="1"> if in.OwnType == 2 </span><span class="cov8" title="1">{
  253. sessionId = in.SendId
  254. userId = in.ReceiveId
  255. }</span> else<span class="cov8" title="1"> {
  256. sessionId = in.ReceiveId
  257. userId = in.NewUserId
  258. }</span>
  259. <span class="cov8" title="1">break</span>
  260. }
  261. //查找会话信息
  262. <span class="cov8" title="1">chatJson := util.Mysql.FindOne(util.SOCIALIZE_CHAT_SESSION, map[string]interface{}{"id": sessionId}, "user_id", "")
  263. if chatJson == nil </span><span class="cov8" title="1">{
  264. return false, "会话标识不存在", "", 0, nowForm.Unix()
  265. }</span> else<span class="cov8" title="1"> {
  266. if userId != quitl.Int64All((*chatJson)["user_id"]) </span><span class="cov8" title="1">{
  267. return false, "会话标识不属于此用户", "", 0, nowForm.Unix()
  268. }</span>
  269. }
  270. }
  271. <span class="cov8" title="1">fool = util.Mysql.ExecTx("聊天信息保存", func(tx *sql.Tx) bool </span><span class="cov8" title="1">{
  272. //先插入信息表
  273. message := map[string]interface{}{
  274. "appid": in.Appid,
  275. "title": in.Title,
  276. "content": in.Content,
  277. "item": in.Item,
  278. "type": in.Type,
  279. "link": in.Link,
  280. "create_time": nowForm.Format(util.Date_Full_Layout),
  281. "create_person": in.SendId,
  282. }
  283. ok := util.Mysql.InsertByTx(tx, util.SOCIALIZE_MESSAGE, message)
  284. receiveOk := int64(0)
  285. messageId = ok
  286. //在插入邮箱表socialize_message_mailbox
  287. messageMailBox := map[string]interface{}{
  288. "appid": in.Appid,
  289. "messag_id": ok,
  290. "type": in.ItemType,
  291. "create_time": nowForm.Format(util.Date_Full_Layout),
  292. "isread": 0,
  293. "send_isdel": 0,
  294. "receive_isdel": 0,
  295. "iswithdraw": 0,
  296. }
  297. //系统信息处理
  298. if in.ItemType == 6 </span><span class="cov8" title="1">{
  299. messageMailBox = map[string]interface{}{
  300. "appid": in.Appid,
  301. "messag_id": ok,
  302. "type": in.ItemType,
  303. "create_time": nowForm.Format(util.Date_Full_Layout),
  304. "read_time": nowForm.Format(util.Date_Full_Layout),
  305. "isread": 1,
  306. "send_isdel": 0,
  307. "receive_isdel": 0,
  308. "iswithdraw": 0,
  309. }
  310. if in.OwnType == 1 </span><span class="cov8" title="1">{
  311. //用户接受系统信息
  312. messageMailBox["own_type"] = 2
  313. messageMailBox["send_user_type"] = 1
  314. messageMailBox["receive_user_type"] = 2
  315. messageMailBox["own_id"] = in.NewUserId
  316. messageMailBox["send_user_id"] = in.ReceiveId
  317. messageMailBox["receive_user_id"] = in.NewUserId
  318. }</span> else<span class="cov8" title="1"> if in.OwnType == 2 </span><span class="cov8" title="1">{
  319. //客服接受系统信息
  320. messageMailBox["own_type"] = 1
  321. messageMailBox["send_user_type"] = 2
  322. messageMailBox["receive_user_type"] = 1
  323. messageMailBox["own_id"] = in.SendId
  324. messageMailBox["send_user_id"] = in.ReceiveId
  325. messageMailBox["receive_user_id"] = in.SendId
  326. }</span> else<span class="cov8" title="1"> {
  327. //客服接受系统信息
  328. messageMailBox["own_type"] = 1
  329. messageMailBox["send_user_type"] = 2
  330. messageMailBox["receive_user_type"] = 1
  331. messageMailBox["own_id"] = in.ReceiveId
  332. messageMailBox["send_user_id"] = in.NewUserId
  333. messageMailBox["receive_user_id"] = in.ReceiveId
  334. }</span>
  335. <span class="cov8" title="1">receiveOk = util.Mysql.InsertByTx(tx, util.SOCIALIZE_MESSAGE_MAILBOX, messageMailBox)
  336. return ok &gt; 1 &amp;&amp; receiveOk &gt; 1</span>
  337. }
  338. <span class="cov8" title="1">if in.ItemType == 4 || in.ItemType == 5 </span><span class="cov8" title="1">{
  339. //客服或者机器人聊天
  340. if in.OwnType == 1 </span><span class="cov8" title="1">{
  341. // (用户发送)客服接受
  342. messageMailBox["own_type"] = 1
  343. messageMailBox["send_user_type"] = 2
  344. messageMailBox["receive_user_type"] = 1
  345. messageMailBox["own_id"] = in.ReceiveId
  346. messageMailBox["send_user_id"] = in.NewUserId
  347. messageMailBox["receive_user_id"] = in.ReceiveId
  348. }</span> else<span class="cov8" title="1"> {
  349. //客服发送(用户接收信息)
  350. messageMailBox["own_type"] = 2
  351. messageMailBox["send_user_type"] = 1
  352. messageMailBox["receive_user_type"] = 2
  353. messageMailBox["own_id"] = in.ReceiveId
  354. messageMailBox["send_user_id"] = in.SendId
  355. messageMailBox["receive_user_id"] = in.ReceiveId
  356. if in.ItemType == 4 </span><span class="cov8" title="1">{
  357. messageMailBox["receive_user_id"] = in.NewUserId
  358. messageMailBox["own_id"] = in.NewUserId
  359. messageMailBox["isread"] = 1
  360. messageMailBox["read_time"] = nowForm.Format(util.Date_Full_Layout)
  361. }</span>
  362. }
  363. } else<span class="cov8" title="1"> {
  364. messageMailBox["own_type"] = 2
  365. messageMailBox["send_user_type"] = 2
  366. messageMailBox["receive_user_type"] = 2
  367. messageMailBox["own_id"] = in.ReceiveId
  368. messageMailBox["send_user_id"] = in.NewUserId
  369. messageMailBox["receive_user_id"] = in.ReceiveId
  370. }</span>
  371. <span class="cov8" title="1">receiveOk = util.Mysql.InsertByTx(tx, util.SOCIALIZE_MESSAGE_MAILBOX, messageMailBox)
  372. messageMailBox = map[string]interface{}{
  373. "appid": in.Appid,
  374. "messag_id": ok,
  375. "type": in.ItemType,
  376. "create_time": nowForm.Format(util.Date_Full_Layout),
  377. "read_time": nowForm.Format(util.Date_Full_Layout),
  378. "isread": 1,
  379. "send_isdel": 0,
  380. "receive_isdel": 0,
  381. "iswithdraw": 0,
  382. }
  383. if in.ItemType == 4 || in.ItemType == 5 </span><span class="cov8" title="1">{
  384. //客服或者机器人聊天
  385. if in.OwnType == 1 </span><span class="cov8" title="1">{
  386. //用户发送(用户接受)
  387. messageMailBox["own_type"] = 2
  388. messageMailBox["send_user_type"] = 2
  389. messageMailBox["receive_user_type"] = 1
  390. messageMailBox["own_id"] = in.NewUserId
  391. messageMailBox["send_user_id"] = in.NewUserId
  392. messageMailBox["receive_user_id"] = in.ReceiveId
  393. }</span> else<span class="cov8" title="1"> {
  394. //客服发送信息(用户接受)
  395. messageMailBox["own_type"] = 1
  396. messageMailBox["send_user_type"] = 1
  397. messageMailBox["receive_user_type"] = 2
  398. messageMailBox["own_id"] = in.SendId
  399. messageMailBox["send_user_id"] = in.SendId
  400. messageMailBox["receive_user_id"] = in.ReceiveId
  401. if in.ItemType == 4 </span><span class="cov8" title="1">{
  402. messageMailBox["receive_user_id"] = in.NewUserId
  403. }</span>
  404. }
  405. } else<span class="cov8" title="1"> {
  406. messageMailBox["own_type"] = 2
  407. messageMailBox["send_user_type"] = 2
  408. messageMailBox["receive_user_type"] = 2
  409. messageMailBox["own_id"] = in.NewUserId
  410. messageMailBox["send_user_id"] = in.NewUserId
  411. messageMailBox["receive_user_id"] = in.ReceiveId
  412. }</span>
  413. <span class="cov8" title="1">receiveOk = util.Mysql.InsertByTx(tx, util.SOCIALIZE_MESSAGE_MAILBOX, messageMailBox)
  414. return ok &gt; 1 &amp;&amp; receiveOk &gt; 1</span>
  415. })
  416. <span class="cov8" title="1">return fool, "", in.Content, messageId, nowForm.Unix()</span>
  417. }
  418. //历史信息查询
  419. func (b MessaggeService) FindMessage(in *messagecenter.MessageReq) *[]map[string]interface{} <span class="cov8" title="1">{
  420. sqlStr := ""
  421. lastStr := ""
  422. if in.LastId &gt; 0 </span><span class="cov8" title="1">{
  423. if in.Sort == "asc" </span><span class="cov8" title="1">{
  424. lastStr = fmt.Sprintf("AND a.messag_id &gt; %d ", in.LastId)
  425. }</span> else<span class="cov8" title="1"> {
  426. lastStr = fmt.Sprintf("AND a.messag_id &lt; %d ", in.LastId)
  427. }</span>
  428. }
  429. <span class="cov8" title="1">switch in.MsgType </span>{
  430. case 2:<span class="cov8" title="1"> //点对点聊天
  431. sqlStr = fmt.Sprintf("SELECT a.messag_id as messageId,b.* ,if(a.own_id = a.send_user_id,1,2) as fool "+
  432. "FROM %s a "+
  433. "LEFT JOIN %s b on a.messag_id=b.id "+
  434. "LEFT JOIN %s c on c.id=a.send_user_id "+
  435. "LEFT JOIN %s d on d.id=a.receive_user_id "+
  436. "where a.own_id= %d "+
  437. "AND ((a.send_user_id= %d AND a.receive_user_id= %d) or (a.send_user_id= %d AND a.receive_user_id= %d)) "+
  438. "AND a.type=2 %s "+
  439. "ORDER BY a.create_time desc ,a.id asc "+
  440. "limit 0 , %d ",
  441. util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_MESSAGE, util.BASE_USER, util.BASE_USER,
  442. in.NewUserId, in.NewUserId, in.SendId, in.SendId, in.NewUserId, lastStr, in.PageSize)
  443. break</span>
  444. case 4, 5, 6, 7:<span class="cov8" title="1"> //客服聊天
  445. if in.UserType == 1 </span><span class="cov8" title="1">{
  446. //客服聊天记录查看
  447. 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 "+
  448. "FROM %s a "+
  449. "LEFT JOIN %s b ON a.messag_id = b.id "+
  450. "LEFT JOIN %s c ON a.own_type = 1 AND a.own_id=c.id "+
  451. "WHERE a.own_type = 1 "+
  452. "AND (a.type = 5 or a.type=4 or a.type=6 or a.type=7) "+
  453. "AND c.ent_id = %d "+
  454. "AND c.user_id = %d %s "+
  455. "ORDER BY a.create_time desc ,a.id asc "+
  456. "limit 0 , %d ",
  457. util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_MESSAGE, util.SOCIALIZE_CHAT_SESSION,
  458. in.EntId, in.SendId, lastStr, in.PageSize)
  459. }</span> else<span class="cov8" title="1"> {
  460. //用户聊天记录查看
  461. 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 "+
  462. "FROM %s a "+
  463. "LEFT JOIN %s b ON a.messag_id = b.id "+
  464. "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 "+
  465. "LEFT JOIN %s d on c.ent_id=d.ent_id "+
  466. "WHERE a.own_type = 2 "+
  467. "AND a.own_id = %d "+
  468. "AND c.ent_id = %d "+
  469. "AND c.user_id = %d "+
  470. "AND ( a.type = 4 OR a.type = 5 or a.type=6 or a.type=7) %s "+
  471. "ORDER BY a.create_time desc ,a.id asc "+
  472. "limit 0 , %d ",
  473. util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_MESSAGE, util.SOCIALIZE_CHAT_SESSION, in.SendId, in.NewUserId, util.SOCIALIZE_TENANT_ROBOT,
  474. in.NewUserId, in.SendId, in.NewUserId, lastStr, in.PageSize)
  475. }</span>
  476. <span class="cov8" title="1">break</span>
  477. }
  478. <span class="cov8" title="1">log.Println(sqlStr)
  479. data := util.Mysql.SelectBySql(sqlStr)
  480. //自己头像处理
  481. if in.UserType == 2 </span><span class="cov8" title="1">{
  482. userData := util.Mysql.FindOne(util.BASE_USER, map[string]interface{}{"id": in.NewUserId}, "headimg", "")
  483. if userData != nil </span><span class="cov8" title="1">{
  484. for key := range *data </span><span class="cov8" title="1">{
  485. (*data)[key]["ownImg"] = (*userData)["headimg"]
  486. }</span>
  487. }
  488. }
  489. <span class="cov8" title="1">go func() </span><span class="cov8" title="1">{
  490. updateMap := map[string]interface{}{}
  491. if len(*data) &gt; 0 </span><span class="cov8" title="1">{
  492. //未读信息修改
  493. switch in.MsgType </span>{
  494. case 2:<span class="cov8" title="1"> //点对点聊天
  495. updateMap = map[string]interface{}{
  496. "own_type": 2,
  497. "own_id": in.NewUserId,
  498. "type": 2,
  499. "isread": 0,
  500. }
  501. util.Mysql.Update(util.SOCIALIZE_MESSAGE_MAILBOX, updateMap, map[string]interface{}{"isread": 1, "read_time": time.Now().Local().Format(util.Date_Full_Layout)})
  502. break</span>
  503. case 4, 5:<span class="cov8" title="1">
  504. sqlStr := ""
  505. if in.UserType == 1 </span><span class="cov8" title="1">{
  506. sqlStr = fmt.Sprintf("UPDATE %s a SET a.isread = 1, a.read_time = now( ) "+
  507. "WHERE a.own_type = 1 "+
  508. "AND a.type IN ( 4,5,6,7) "+
  509. "AND a.isread = 0 "+
  510. "AND a.own_id IN ( SELECT b.id FROM %s b WHERE b.customer_service_id=%d AND b.user_id=%d )",
  511. util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_CHAT_SESSION, in.EntUserId, in.SendId)
  512. }</span> else<span class="cov8" title="1"> {
  513. sqlStr = fmt.Sprintf("UPDATE %s a SET a.isread = 1, a.read_time = now( ) "+
  514. "WHERE a.own_type = 2 "+
  515. "AND a.type IN ( 4,5,6,7 ) "+
  516. "AND a.isread = 0 "+
  517. "AND a.own_id =%d ", util.SOCIALIZE_MESSAGE_MAILBOX, in.NewUserId)
  518. }</span>
  519. <span class="cov8" title="1">util.Mysql.UpdateOrDeleteBySql(sqlStr)
  520. break</span>
  521. }
  522. }
  523. }()
  524. <span class="cov8" title="1">return data</span>
  525. }
  526. //创建会话
  527. func (b MessaggeService) CreateChatSession(in *messagecenter.ChatSessionReq) (fool bool, sessionId int64) <span class="cov8" title="1">{
  528. fool = util.Mysql.ExecTx("会话新建", func(tx *sql.Tx) bool </span><span class="cov8" title="1">{
  529. customerserviceName := in.CustomerserviceName
  530. if in.CustomerServiceId != 0 </span><span class="cov8" title="1">{
  531. sqlStr:=fmt.Sprintf("select customer_service_name from %s "+
  532. "where ent_id= %d "+
  533. "AND customer_service_id= %d ",
  534. util.SOCIALIZE_CHAT_SESSION, in.EntId, in.CustomerServiceId)
  535. customerList := util.Mysql.SelectBySql(sqlStr)
  536. if len(*customerList) &gt; 0 </span><span class="cov8" title="1">{
  537. customerserviceName = quitl.InterfaceToStr((*customerList)[0]["customer_service_name"])
  538. }</span>
  539. }
  540. <span class="cov8" title="1">chatMession := map[string]interface{}{
  541. "handle_status": 0,
  542. "start_time": time.Now().Local().Format(util.Date_Full_Layout),
  543. "appid": in.AppId,
  544. "ent_id": in.EntId,
  545. "customer_service_id": in.CustomerServiceId,
  546. "user_id": in.UserId,
  547. "customer_service_name": customerserviceName,
  548. }
  549. sessionId = util.Mysql.InsertByTx(tx, util.SOCIALIZE_CHAT_SESSION, chatMession)
  550. return sessionId &gt; 0</span>
  551. })
  552. <span class="cov8" title="1">return fool, sessionId</span>
  553. }
  554. //结束会话
  555. func (b MessaggeService) CloseChatSession(in *messagecenter.CloseSessionReq) bool <span class="cov8" title="1">{
  556. fool := util.Mysql.ExecTx("关闭会话", func(tx *sql.Tx) bool </span><span class="cov8" title="1">{
  557. updateMap := map[string]interface{}{
  558. "id": in.SessionId,
  559. }
  560. fool := util.Mysql.Update(util.SOCIALIZE_CHAT_SESSION, updateMap, map[string]interface{}{"start_time": time.Now().Local().Format(util.Date_Full_Layout)})
  561. return fool
  562. }</span>)
  563. <span class="cov8" title="1">return fool</span>
  564. }
  565. //创建会话并保存信息
  566. func (b *MessaggeService) SaveAutoReplyMsg(userType, entId, entUserId, userId int64, content, appId, nowFormat string) (bool, int64) <span class="cov8" title="1">{
  567. messageId := int64(0)
  568. return util.Mysql.ExecTx("保存自动回复消息", func(tx *sql.Tx) bool </span><span class="cov8" title="1">{
  569. entUserName := ""
  570. if entUserId &gt; 0 </span><span class="cov8" title="1">{
  571. 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)
  572. if list != nil &amp;&amp; len(*list) &gt; 0 </span><span class="cov8" title="1">{
  573. entUserName, _ = (*list)[0]["customer_service_name"].(string)
  574. }</span>
  575. }
  576. <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")
  577. 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)
  578. ok := false
  579. if userType == 0 </span><span class="cov8" title="1">{
  580. 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) &gt; 0
  581. 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) &gt; 0
  582. ok = ok1 &amp;&amp; ok2
  583. }</span> else<span class="cov8" title="1"> if userType == 1 </span><span class="cov8" title="1">{
  584. 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) &gt; 0
  585. }</span> else<span class="cov8" title="1"> if userType == 2 </span><span class="cov8" title="1">{
  586. 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) &gt; 0
  587. }</span>
  588. <span class="cov8" title="1">return messageId &gt; 0 &amp;&amp; sessionId &gt; 0 &amp;&amp; ok</span>
  589. }), messageId
  590. }
  591. //修改未读状态
  592. func (b MessaggeService) UpdateReadById(in *messagecenter.ReadStateReq) bool <span class="cov8" title="1">{
  593. fool := util.Mysql.ExecTx("已读状态修改", func(tx *sql.Tx) bool </span><span class="cov8" title="1">{
  594. updateMap := map[string]interface{}{
  595. "messag_id": in.MessageId,
  596. "isread": 0,
  597. }
  598. fool := util.Mysql.Update(util.SOCIALIZE_MESSAGE_MAILBOX, updateMap, map[string]interface{}{"read_time": time.Now().Local().Format(util.Date_Full_Layout), "isread": 1})
  599. return fool
  600. }</span>)
  601. <span class="cov8" title="1">return fool</span>
  602. }
  603. </pre>
  604. </div>
  605. </body>
  606. <script>
  607. (function() {
  608. var files = document.getElementById('files');
  609. var visible;
  610. files.addEventListener('change', onChange, false);
  611. function select(part) {
  612. if (visible)
  613. visible.style.display = 'none';
  614. visible = document.getElementById(part);
  615. if (!visible)
  616. return;
  617. files.value = part;
  618. visible.style.display = 'block';
  619. location.hash = part;
  620. }
  621. function onChange() {
  622. select(files.value);
  623. window.scrollTo(0, 0);
  624. }
  625. if (location.hash != "") {
  626. select(location.hash.substr(1));
  627. }
  628. if (!visible) {
  629. select("file0");
  630. }
  631. })();
  632. </script>
  633. </html>