message_mail_box.go 22 KB

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