message_mail_box.go 26 KB

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