message_mail_box.go 22 KB

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