message_mail_box.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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/init"
  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. func (b MessaggeService) Table() string {
  14. return "socialize_message_mailbox"
  15. }
  16. func (b MessaggeService) Count(newUserId, userType, entUserId int64) (count int, last map[string]interface{}, err error) {
  17. v := make([]interface{}, 0)
  18. sqlStr := ""
  19. if userType == 1 {
  20. sqlStr = "select count(b.id) from socialize_chat_session a left join socialize_message_mailbox b ON a.id=b.own_id where b.type=5 and a.customer_service_id=? and b.isread=0 and own_type = 1 order by create_time"
  21. v = append(v, entUserId)
  22. } else {
  23. sqlStr = "select count(b.id) from socialize_message_mailbox b where b.send_user_id !=? and b.own_id=? and b.isread=0 and own_type = 2 order by create_time"
  24. v = append(v, newUserId, newUserId)
  25. }
  26. fmt.Println(sqlStr, v)
  27. rs := Mysql.CountBySql(sqlStr, v...)
  28. log.Println(rs)
  29. count = int(rs)
  30. //最后一条信息查询
  31. lastData := Mysql.SelectBySql("SELECT c.* FROM socialize_message_mailbox b left join socialize_message c on b.messag_id=c.id WHERE b.send_user_id != ? AND b.own_id = ? and b.isread=0 AND own_type = 2 ORDER BY create_time DESC LIMIT 0,1", newUserId, newUserId)
  32. if len(*lastData) > 0 {
  33. last = (*lastData)[0]
  34. }
  35. return
  36. }
  37. func (b MessaggeService) UserList(in *messagecenter.UserReq) (data *[]map[string]interface{}, err error) {
  38. v := make([]interface{}, 0)
  39. sqlStr := ""
  40. if in.UserType == 2 {
  41. //用户最后一次信息查询
  42. userSql := "SELECT MAX( c.id ) as messageId FROM socialize_message_mailbox c WHERE c.own_id = ? AND c.type = 2 AND c.own_type = 2 GROUP BY ( CASE WHEN c.send_user_id > c.receive_user_id THEN CONCAT( c.send_user_id, c.receive_user_id ) WHEN c.send_user_id < c.receive_user_id THEN CONCAT( c.receive_user_id, c.send_user_id ) END ) "
  43. data = Mysql.SelectBySql(userSql, in.NewUserId)
  44. userMessageId := Inhandle(data)
  45. //客服最后一次信息查询
  46. customerSql := " SELECT max( c.id ) as messageId FROM socialize_message_mailbox c LEFT JOIN socialize_chat_session d ON IF ( c.send_user_type = 1, d.id = c.send_user_id, d.id = c.receive_user_id ) WHERE c.own_id = ? AND ( c.type = 4 OR c.type = 5 ) AND c.own_type = 2 and d.user_id=c.own_id GROUP BY d.ent_id "
  47. data = Mysql.SelectBySql(customerSql, in.NewUserId)
  48. customerMessageId := Inhandle(data)
  49. //用户的列表
  50. sqlStr = "SELECT IF ( b.nickname = '', CONCAT(SUBSTR( b.phone, 1, 3 ),'****',SUBSTR( b.phone, 8, 11)), b.nickname ) AS name, b.id, e.title, b.headimg, e.type, e.link, e.content, 2 AS userType, a.create_time, a.type AS itemType, ( SELECT count( h.id ) FROM socialize_message_mailbox h WHERE h.type = 2 AND h.own_id = ? AND h.own_type = 2 AND h.receive_user_id = ? and h.send_user_id=b.id AND h.isread = 0 ) AS number FROM socialize_message_mailbox a LEFT JOIN base_user b ON b.id = a.receive_user_id or b.id = a.send_user_id LEFT JOIN socialize_message e ON e.id = a.messag_id WHERE a.id IN ( " + userMessageId + " ) AND b.id != ? UNION ALL SELECT f.nickname AS name, b.ent_id AS id, e.title,f.headimage AS headimg, e.type, e.link, e.content, 1 AS userType, a.create_time, a.type AS itemType, ( SELECT count( h.id ) FROM socialize_message_mailbox h WHERE ( h.type = 4 OR h.type = 5 ) AND h.own_id = ? AND h.own_type = 2 AND h.send_user_type = 1 and h.receive_user_id = ? AND h.isread = 0 ) AS number FROM socialize_message_mailbox a LEFT JOIN socialize_chat_session b ON IF ( a.send_user_type = 1, b.id = a.send_user_id, b.id = a.receive_user_id ) LEFT JOIN socialize_message e ON e.id = a.messag_id left join socialize_tenant_robot f on f.ent_id=b.ent_id WHERE a.id IN ( " + customerMessageId + " ) ORDER BY create_time DESC"
  51. v = append(v, in.NewUserId, in.NewUserId, in.NewUserId, in.NewUserId, in.NewUserId)
  52. } else {
  53. phoneSql := ""
  54. if in.Phone != "" {
  55. phoneSql = "and b.phone='" + in.Phone + "'"
  56. }
  57. startTimeSql := ""
  58. if in.StartTime != "" {
  59. startTimeSql = " and DATE_FORMAT(c.create_time,'%Y-%m-%d') >= '" + in.StartTime + "' "
  60. }
  61. endTimeSql := ""
  62. if in.EndTime != "" {
  63. endTimeSql = " and DATE_FORMAT(c.create_time,'%Y-%m-%d') <= '" + in.EndTime + "' "
  64. }
  65. //先获取每个用户最后聊天记录
  66. userSql := " SELECT MAX( c.id ) as messageId FROM socialize_message_mailbox c LEFT JOIN socialize_chat_session d ON IF ( c.send_user_type = 1, d.id = c.send_user_id, d.id = c.receive_user_id ) WHERE c.own_type = 1 AND d.customer_service_id = ? and ( c.type = 4 OR c.type = 5 ) AND d.customer_service_id !=0 " + startTimeSql + endTimeSql + " 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 ) "
  67. v = append(v, in.EntUserId)
  68. log.Println(userSql, v)
  69. data = Mysql.SelectBySql(userSql, v...)
  70. if data != nil {
  71. customerMessageId := Inhandle(data)
  72. //客服的用户列表
  73. sqlStr = "SELECT IF ( b.nickname = '', CONCAT(SUBSTR( b.phone, 1, 3 ),'****',SUBSTR( b.phone, 8, 11)), b.nickname ) AS name, b.id, e.title, b.headimg, e.type, e.link, e.content, a.create_time, ( SELECT count( h.id ) FROM socialize_message_mailbox h WHERE ( h.type = 4 OR h.type = 5 ) AND h.own_type = 1 AND IF ( h.send_user_type = 1, h.send_user_id = f.id, h.receive_user_id = f.id ) AND h.isread = 0 ) AS number FROM socialize_message_mailbox a LEFT JOIN base_user b ON IF ( a.send_user_type = 1, b.id = a.receive_user_id, b.id = a.send_user_id ) LEFT JOIN socialize_message e ON e.id = a.messag_id LEFT JOIN socialize_chat_session f ON IF ( a.send_user_type = 1, f.id = a.send_user_id, f.id = a.receive_user_id ) WHERE a.id IN (" + customerMessageId + ")" + phoneSql + " ORDER BY a.create_time DESC"
  74. fmt.Println(in.EntUserId)
  75. v = make([]interface{}, 0)
  76. }
  77. }
  78. if sqlStr != "" {
  79. fmt.Println(sqlStr, v)
  80. data = Mysql.SelectBySql(sqlStr, v...)
  81. }
  82. return
  83. }
  84. func (b MessaggeService) SaveMessage(in *messagecenter.MessageEntity) (errorCode int64, errorMsg string) {
  85. //先插入信息表
  86. //判断会话标识是否属于本人
  87. if in.ItemType != 2 {
  88. userId := int64(0)
  89. sessionId := int64(0)
  90. switch in.ItemType {
  91. case 4, 5:
  92. if in.OwnType == 1 {
  93. sessionId = in.ReceiveId
  94. userId = in.NewUserId
  95. } else {
  96. sessionId = in.SendId
  97. userId = in.ReceiveId
  98. if in.ItemType == 4 {
  99. userId = in.NewUserId
  100. }
  101. }
  102. break
  103. case 6:
  104. if in.OwnType == 1 {
  105. sessionId = in.ReceiveId
  106. userId = in.NewUserId
  107. } else {
  108. sessionId = in.SendId
  109. userId = in.NewUserId
  110. }
  111. break
  112. }
  113. //查找会话信息
  114. chatJson := Mysql.FindOne("socialize_chat_session", map[string]interface{}{"id": sessionId}, "user_id", "")
  115. if chatJson == nil {
  116. return 1, "会话标识不存在"
  117. } else {
  118. if userId != quitl.Int64All((*chatJson)["user_id"]) {
  119. return 1, "会话标识不属于此用户"
  120. }
  121. }
  122. }
  123. fool := Mysql.ExecTx("聊天信息保存", func(tx *sql.Tx) bool {
  124. //先插入信息表
  125. message := map[string]interface{}{
  126. "appid": in.Appid,
  127. "title": in.Title,
  128. "content": in.Content,
  129. "item": in.ItemType,
  130. "type": in.Type,
  131. "link": in.Link,
  132. "create_time": time.Now().Local().Format(util.Date_Full_Layout),
  133. "create_person": in.SendId,
  134. }
  135. ok := Mysql.InsertByTx(tx, "socialize_message", message)
  136. if ok <= 0 {
  137. return false
  138. }
  139. //在插入邮箱表socialize_message_mailbox
  140. messageMailBox := map[string]interface{}{
  141. "appid": in.Appid,
  142. "messag_id": ok,
  143. "type": in.ItemType,
  144. "create_time": time.Now().Local().Format(util.Date_Full_Layout),
  145. "isread": 0,
  146. "send_isdel": 0,
  147. "receive_isdel": 0,
  148. "iswithdraw": 0,
  149. }
  150. //系统信息处理
  151. if in.ItemType == 6 {
  152. messageMailBox = map[string]interface{}{
  153. "appid": in.Appid,
  154. "messag_id": ok,
  155. "type": in.ItemType,
  156. "create_time": time.Now().Local().Format(util.Date_Full_Layout),
  157. "read_time": time.Now().Local().Format(util.Date_Full_Layout),
  158. "isread": 1,
  159. "send_isdel": 0,
  160. "receive_isdel": 0,
  161. "iswithdraw": 0,
  162. }
  163. if in.OwnType == 1 {
  164. //用户接受系统信息
  165. messageMailBox["own_type"] = 2
  166. messageMailBox["send_user_type"] = 1
  167. messageMailBox["receive_user_type"] = 2
  168. messageMailBox["own_id"] = in.NewUserId
  169. messageMailBox["send_user_id"] = in.ReceiveId
  170. messageMailBox["receive_user_id"] = in.NewUserId
  171. } else {
  172. //客服接受系统信息
  173. messageMailBox["own_type"] = 1
  174. messageMailBox["send_user_type"] = 2
  175. messageMailBox["receive_user_type"] = 1
  176. messageMailBox["own_id"] = in.SendId
  177. messageMailBox["send_user_id"] = in.NewUserId
  178. messageMailBox["receive_user_id"] = in.SendId
  179. }
  180. receiveOk := Mysql.InsertByTx(tx, "socialize_message_mailbox", messageMailBox)
  181. if receiveOk == 0 {
  182. return false
  183. } else {
  184. return true
  185. }
  186. }
  187. if in.ItemType == 4 || in.ItemType == 5 {
  188. //客服或者机器人聊天
  189. if in.OwnType == 1 {
  190. // (用户发送)客服接受
  191. messageMailBox["own_type"] = 1
  192. messageMailBox["send_user_type"] = 2
  193. messageMailBox["receive_user_type"] = 1
  194. messageMailBox["own_id"] = in.ReceiveId
  195. messageMailBox["send_user_id"] = in.NewUserId
  196. messageMailBox["receive_user_id"] = in.ReceiveId
  197. } else {
  198. //客服发送(用户接收信息)
  199. messageMailBox["own_type"] = 2
  200. messageMailBox["send_user_type"] = 1
  201. messageMailBox["receive_user_type"] = 2
  202. messageMailBox["own_id"] = in.ReceiveId
  203. messageMailBox["send_user_id"] = in.SendId
  204. messageMailBox["receive_user_id"] = in.ReceiveId
  205. if in.ItemType == 4 {
  206. messageMailBox["receive_user_id"] = in.NewUserId
  207. messageMailBox["own_id"] = in.NewUserId
  208. }
  209. }
  210. } else {
  211. messageMailBox["own_type"] = 2
  212. messageMailBox["send_user_type"] = 2
  213. messageMailBox["receive_user_type"] = 2
  214. messageMailBox["own_id"] = in.ReceiveId
  215. messageMailBox["send_user_id"] = in.NewUserId
  216. messageMailBox["receive_user_id"] = in.ReceiveId
  217. }
  218. sendOk := Mysql.InsertByTx(tx, "socialize_message_mailbox", messageMailBox)
  219. if sendOk == 0 {
  220. return false
  221. }
  222. messageMailBox = map[string]interface{}{
  223. "appid": in.Appid,
  224. "messag_id": ok,
  225. "type": in.ItemType,
  226. "create_time": time.Now().Local().Format(util.Date_Full_Layout),
  227. "read_time": time.Now().Local().Format(util.Date_Full_Layout),
  228. "isread": 1,
  229. "send_isdel": 0,
  230. "receive_isdel": 0,
  231. "iswithdraw": 0,
  232. }
  233. if in.ItemType == 4 || in.ItemType == 5 {
  234. //客服或者机器人聊天
  235. if in.OwnType == 1 {
  236. //用户发送(用户接受)
  237. messageMailBox["own_type"] = 2
  238. messageMailBox["send_user_type"] = 2
  239. messageMailBox["receive_user_type"] = 1
  240. messageMailBox["own_id"] = in.NewUserId
  241. messageMailBox["send_user_id"] = in.NewUserId
  242. messageMailBox["receive_user_id"] = in.ReceiveId
  243. } else {
  244. //客服发送信息(用户接受)
  245. messageMailBox["own_type"] = 1
  246. messageMailBox["send_user_type"] = 1
  247. messageMailBox["receive_user_type"] = 2
  248. messageMailBox["own_id"] = in.SendId
  249. messageMailBox["send_user_id"] = in.SendId
  250. messageMailBox["receive_user_id"] = in.ReceiveId
  251. if in.ItemType == 4 {
  252. messageMailBox["receive_user_id"] = in.NewUserId
  253. }
  254. }
  255. } else {
  256. messageMailBox["own_type"] = 2
  257. messageMailBox["send_user_type"] = 2
  258. messageMailBox["receive_user_type"] = 2
  259. messageMailBox["own_id"] = in.NewUserId
  260. messageMailBox["send_user_id"] = in.NewUserId
  261. messageMailBox["receive_user_id"] = in.ReceiveId
  262. }
  263. receiveOk := Mysql.InsertByTx(tx, "socialize_message_mailbox", messageMailBox)
  264. if receiveOk == 0 {
  265. return false
  266. }
  267. return true
  268. })
  269. if fool {
  270. return 0, ""
  271. } else {
  272. return 1, "保存失败"
  273. }
  274. }
  275. func (b MessaggeService) FindMessage(in *messagecenter.MessageReq) (*[]map[string]interface{}, int64) {
  276. strat := time.Now().Unix()
  277. sqlStr := ""
  278. countSql := ""
  279. lastStr := " "
  280. if in.LastId > 0 {
  281. lastStr = " and a.messag_id < " + quitl.InterfaceToStr(in.LastId)
  282. }
  283. switch in.MsgType {
  284. case 2: //点对点聊天
  285. sqlStr = "SELECT a.messag_id as messageId,b.* ,if(a.own_id = a.send_user_id,1,2) as fool FROM socialize_message_mailbox a LEFT JOIN socialize_message b on a.messag_id=b.id left join base_user c on c.id=a.send_user_id left join base_user d on d.id=a.receive_user_id where a.own_id= " + quitl.InterfaceToStr(in.NewUserId) + " and ((a.send_user_id= " + quitl.InterfaceToStr(in.NewUserId) + " and a.receive_user_id= " + quitl.InterfaceToStr(in.SendId) + ") or (a.send_user_id= " + quitl.InterfaceToStr(in.SendId) + " and a.receive_user_id= " + quitl.InterfaceToStr(in.NewUserId) + ")) and a.type=2 " + lastStr + " ORDER BY a.create_time desc ,a.id asc limit 0 , " + quitl.InterfaceToStr(in.PageSize)
  286. countSql = "SELECT count(b.id) FROM socialize_message_mailbox a LEFT JOIN socialize_message b on a.messag_id=b.id left join base_user c on c.id=a.send_user_id left join base_user d on d.id=a.receive_user_id where a.own_id=" + quitl.InterfaceToStr(in.NewUserId) + " and ((a.send_user_id=" + quitl.InterfaceToStr(in.NewUserId) + " and a.receive_user_id=" + quitl.InterfaceToStr(in.SendId) + ") or (a.send_user_id=" + quitl.InterfaceToStr(in.SendId) + " and a.receive_user_id=" + quitl.InterfaceToStr(in.NewUserId) + ")) and a.type=2 " + lastStr
  287. break
  288. case 4, 5, 6, 7: //客服聊天
  289. if in.UserType == 1 {
  290. //客服聊天记录查看
  291. sqlStr = "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 , c.user_id FROM socialize_message_mailbox a LEFT JOIN socialize_message b ON a.messag_id = b.id LEFT JOIN socialize_chat_session c ON IF ( a.send_user_type = 1, c.id = a.send_user_id, c.id = a.receive_user_id ) LEFT JOIN socialize_tenant_robot d ON c.ent_id = d.ent_id WHERE a.own_type = 1 AND c.ent_id = " + quitl.InterfaceToStr(in.EntId) + " AND c.user_id = " + quitl.InterfaceToStr(in.SendId) + " AND (a.type = 5 or a.type=4 or a.type=6 or a.type=7) " + lastStr + " ORDER BY a.create_time desc ,a.id asc limit 0 , " + quitl.InterfaceToStr(in.PageSize)
  292. countSql = "SELECT count(b.id) FROM socialize_message_mailbox a LEFT JOIN socialize_message b ON a.messag_id = b.id LEFT JOIN socialize_chat_session c ON IF ( a.send_user_type = 1, c.id = a.send_user_id, c.id = a.receive_user_id ) LEFT JOIN socialize_tenant_robot d ON c.ent_id = d.ent_id WHERE a.own_type = 1 AND c.ent_id = " + quitl.InterfaceToStr(in.EntId) + " AND c.user_id = " + quitl.InterfaceToStr(in.SendId) + " AND (a.type = 5 or a.type=4 or a.type = 5 or a.type=4 or a.type=6 or a.type=7) " + lastStr
  293. } else {
  294. //用户聊天记录查看
  295. sqlStr = "SELECT a.messag_id as messageId, b.*, IF ( a.own_id = a.send_user_id, 1, 2 ) AS fool , a.send_user_type, a.type as itemType, d.nickname as robotName, d.headimage as robotImg, c.customer_service_name as setName FROM socialize_message_mailbox a LEFT JOIN socialize_message b ON a.messag_id = b.id LEFT JOIN socialize_chat_session c ON IF ( a.send_user_type = 1, c.id = a.send_user_id, c.id = a.receive_user_id ) left join socialize_tenant_robot d on c.ent_id=d.ent_id WHERE a.own_type = 2 AND c.ent_id = " + quitl.InterfaceToStr(in.SendId) + " AND a.own_id = " + quitl.InterfaceToStr(in.NewUserId) + " AND ( a.type = 4 OR a.type = 5 or a.type=6 or a.type=7) " + lastStr + " ORDER BY a.create_time desc ,a.id asc limit 0 , " + quitl.InterfaceToStr(in.PageSize)
  296. countSql = "SELECT count(a.id) FROM socialize_message_mailbox a LEFT JOIN socialize_message b ON a.messag_id = b.id LEFT JOIN socialize_chat_session c ON IF ( a.send_user_type = 1, c.id = a.send_user_id, c.id = a.receive_user_id ) left join socialize_tenant_robot d on c.ent_id=d.ent_id WHERE a.own_type = 2 AND c.ent_id =" + quitl.InterfaceToStr(in.SendId) + " AND a.own_id =" + quitl.InterfaceToStr(in.NewUserId) + " AND ( a.type = 4 OR a.type = 5 or a.type=6 or a.type=7) " + lastStr
  297. }
  298. break
  299. }
  300. fmt.Println(sqlStr)
  301. fmt.Println(countSql)
  302. data := Mysql.SelectBySql(sqlStr)
  303. stop1 := time.Now().Unix()
  304. log.Println("sqlStr耗时", stop1-strat)
  305. count := Mysql.CountBySql(countSql)
  306. stop2 := time.Now().Unix()
  307. log.Println("countSql耗时", stop2-stop1)
  308. //自己头像处理
  309. if in.UserType == 2 && count > 0 {
  310. userData := Mysql.FindOne("base_user", map[string]interface{}{"id": in.NewUserId}, "headimg", "")
  311. if userData != nil {
  312. for key := range *data {
  313. (*data)[key]["ownImg"] = (*userData)["headimg"]
  314. }
  315. }
  316. }
  317. go func() {
  318. updateMap := map[string]interface{}{}
  319. if len(*data) > 0 {
  320. //未读信息修改
  321. switch in.MsgType {
  322. case 2: //点对点聊天
  323. updateMap = map[string]interface{}{
  324. "own_type": 2,
  325. "own_id": in.NewUserId,
  326. "type": 2,
  327. "isread": 0,
  328. }
  329. Mysql.Update("socialize_message_mailbox", updateMap, map[string]interface{}{"isread": 1, "read_time": time.Now().Local().Format(util.Date_Full_Layout)})
  330. break
  331. case 4, 5:
  332. if in.UserType == 1 {
  333. Mysql.UpdateOrDeleteBySql("UPDATE socialize_message_mailbox a SET a.isread = 1, a.read_time = now( ) WHERE a.own_type = 1 AND a.type IN ( 4, 5,6,7) AND a.isread = 0 AND a.own_id IN ( SELECT b.id FROM socialize_chat_session b WHERE b.customer_service_id=? and b.user_id=? )", in.EntUserId, in.SendId)
  334. } else {
  335. Mysql.UpdateOrDeleteBySql("UPDATE socialize_message_mailbox a SET a.isread = 1, a.read_time = now( ) WHERE a.own_type = 2 AND a.type IN ( 4, 5,6,7 ) AND a.isread = 0 AND a.own_id =?", in.NewUserId)
  336. }
  337. break
  338. }
  339. }
  340. }()
  341. stop := time.Now().Unix()
  342. log.Println("耗时", stop-strat)
  343. log.Println("耗时", stop-stop2)
  344. return data, count
  345. }
  346. func (b MessaggeService) CreateChatSession(in *messagecenter.ChatSessionReq) (errorCode, sessionId int64) {
  347. fool := Mysql.ExecTx("会话新建", func(tx *sql.Tx) bool {
  348. customerserviceName := in.CustomerserviceName
  349. if in.CustomerServiceId != 0 {
  350. customerList := Mysql.SelectBySql("select customer_service_name from socialize_tenant_seat where ent_id=" + quitl.InterfaceToStr(in.EntId) + " and customer_service_id=" + quitl.InterfaceToStr(in.CustomerServiceId))
  351. if len(*customerList) > 0 {
  352. customerserviceName = quitl.InterfaceToStr((*customerList)[0]["customer_service_name"])
  353. }
  354. }
  355. chatMession := map[string]interface{}{
  356. "handle_status": 0,
  357. "start_time": time.Now().Local().Format(util.Date_Full_Layout),
  358. "appid": in.AppId,
  359. "ent_id": in.EntId,
  360. "customer_service_id": in.CustomerServiceId,
  361. "user_id": in.UserId,
  362. "customer_service_name": customerserviceName,
  363. }
  364. sessionId = Mysql.InsertByTx(tx, "socialize_chat_session", chatMession)
  365. return sessionId > 0
  366. })
  367. if fool {
  368. return 0, sessionId
  369. } else {
  370. return 1, sessionId
  371. }
  372. }
  373. func (b MessaggeService) CloseChatSession(in *messagecenter.CloseSessionReq) (errorCode int64) {
  374. fool := Mysql.ExecTx("关闭会话", func(tx *sql.Tx) bool {
  375. updateMap := map[string]interface{}{
  376. "id": in.SessionId,
  377. }
  378. fool := Mysql.Update("socialize_chat_session", updateMap, map[string]interface{}{"start_time": time.Now().Local().Format(util.Date_Full_Layout)})
  379. return fool
  380. })
  381. if fool {
  382. return 0
  383. } else {
  384. return 1
  385. }
  386. }
  387. //in数据处理
  388. func Inhandle(data *[]map[string]interface{}) (messId string) {
  389. if len(*data) == 0 {
  390. messId = "''"
  391. return
  392. }
  393. for k, m := range *data {
  394. if k > 0 {
  395. messId += `,`
  396. }
  397. messId += "'" + quitl.InterfaceToStr(m["messageId"]) + "'"
  398. }
  399. return
  400. }
  401. func (m *MessaggeService) SaveAutoReplyMsg(userType, entId, entUserId, userId int64, content, appId, nowFormat string) bool {
  402. return Mysql.ExecTx("保存自动回复消息", func(tx *sql.Tx) bool {
  403. entUserName := ""
  404. if entUserId > 0 {
  405. list := Mysql.SelectBySql(`select customer_service_name from socialize_tenant_seat where appid=? and ent_id=? and customer_service_id=?`, appId, entId, entUserId)
  406. if list != nil && len(*list) > 0 {
  407. entUserName, _ = (*list)[0]["customer_service_name"].(string)
  408. }
  409. }
  410. messageId := Mysql.InsertBySqlByTx(tx, `insert into socialize_message (appid,content,item,type,create_time,create_person) values (?,?,?,?,?,?)`, appId, content, 8, 1, nowFormat, "admin")
  411. sessionId := 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)
  412. ok := false
  413. if userType == 1 {
  414. if 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, sessionId, 1, nowFormat) > 0 {
  415. ok = true
  416. }
  417. } else if userType == 2 {
  418. if 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 {
  419. ok = true
  420. }
  421. }
  422. return messageId > 0 && sessionId > 0 && ok
  423. })
  424. }