message_mail_box.go 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  1. package service
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "log"
  6. "strconv"
  7. "strings"
  8. "sync"
  9. "time"
  10. quitl "app.yhyue.com/moapp/jybase/common"
  11. "app.yhyue.com/moapp/jybase/date"
  12. "app.yhyue.com/moapp/jybase/encrypt"
  13. util "bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/entity"
  14. IC "bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/rpc/messagecenter/init"
  15. "bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/rpc/messagecenter/messagecenter"
  16. "github.com/gogf/gf/v2/util/gconv"
  17. )
  18. type MessaggeService struct{}
  19. var rwLock = new(sync.RWMutex)
  20. // Count 未读消息查询
  21. func (b MessaggeService) Count(newUserId, positionId int64) (last map[string]interface{}, count int) {
  22. //导航右上角未读消息总量 1v1聊天+群组消息+客服消息
  23. querySql := fmt.Sprintf("SELECT b.*,(SELECT SUM( a.unread) FROM %s a "+
  24. "LEFT JOIN %s b ON a.message_id = b.id "+
  25. "WHERE a.unread > 0 "+
  26. "AND ( a.my_position_id = %d OR a.user_id = %d )) AS unread "+
  27. "FROM %s a "+
  28. "LEFT JOIN %s b ON a.message_id = b.id "+
  29. "WHERE a.unread > 0 "+
  30. "AND ( a.my_position_id = %d OR a.user_id = %d ) "+
  31. "ORDER BY a.TIMESTAMP DESC LIMIT 0,1", util.SOCIALIZE_SUMMARY, util.SOCIALIZE_MESSAGE, positionId, newUserId,
  32. util.SOCIALIZE_SUMMARY, util.SOCIALIZE_MESSAGE, positionId, newUserId)
  33. log.Println("查询sql", querySql)
  34. data := IC.BaseMysql.SelectBySql(querySql)
  35. if data != nil && len(*data) > 0 {
  36. return (*data)[0], quitl.IntAll((*data)[0]["unread"])
  37. } else {
  38. return nil, 0
  39. }
  40. }
  41. // 用户列表查询
  42. func (b MessaggeService) UserList(in *messagecenter.UserReq) (data *[]map[string]interface{}, count int64, err error) {
  43. sqlStr := ""
  44. tm := time.Now()
  45. if in.UserType == 2 {
  46. var (
  47. allSql, positionIdArr, oneSql, oneNameSql, serviceNameSql string
  48. sqlArr []string
  49. )
  50. GroupNameSql := `(SELECT *,"" as groupMember FROM socialize_chat_group where isdismiss = 0)` //无搜索群
  51. if in.NameSearch != "" {
  52. serviceNameSql = " AND b.nickname like '%" + in.NameSearch + "%'"
  53. GroupNameSql = `(SELECT id,name,"" as groupMember FROM socialize_chat_group WHERE isdismiss = 0 AND name like '%` + in.NameSearch + `%')` //默认搜素群名称
  54. positionIdArr = NameToPositionIdp(in.NameSearch, in.EntId)
  55. if positionIdArr != "" {
  56. oneNameSql = " AND find_in_set(b.id,'" + positionIdArr + "')" //1v1搜索成员id
  57. //存在满足成员名时 重新定义搜索sql 并兼容群名称不满足但成员名满足时也展示 并展示部分满足群员名
  58. nameSearch := " or b.name like '%" + in.NameSearch + "%')"
  59. name := fmt.Sprintf(" AND (c.position_id IN ( %s ) %s", positionIdArr, nameSearch)
  60. GroupNameSql = fmt.Sprintf(` (
  61. SELECT
  62. a.chat_group_id as id,
  63. GROUP_CONCAT( distinct c.position_id ) as groupMember,
  64. GROUP_CONCAT( distinct b.name ) as name
  65. FROM
  66. socialize_chat_group_person a
  67. INNER JOIN socialize_chat_group b ON a.position_id=%d AND a.chat_group_id = b.id AND b.isdismiss = 0
  68. INNER JOIN socialize_chat_group_person c ON a.chat_group_id = c.chat_group_id %s
  69. GROUP BY
  70. a.chat_group_id)`, in.PositionId, name) //群搜索
  71. }
  72. }
  73. log.Println("用户列表展示", in.NameSearch, in.EntId, oneNameSql)
  74. //1v1 无搜索时 或搜索有结果时
  75. if in.NameSearch == "" || oneNameSql != "" {
  76. oneSql = fmt.Sprintf(`(SELECT
  77. a.your_position_id AS id,
  78. 2 AS userType,
  79. c.nickname AS name,
  80. c.headimg,
  81. d.content,
  82. d.type,
  83. d.create_time,
  84. a.unread as number,
  85. a.timestamp,
  86. c.phone,
  87. "" as groupMember
  88. FROM
  89. socialize_summary a
  90. INNER JOIN base_position b ON ( a.my_position_id = %d %s AND a.your_position_id = b.id)
  91. INNER JOIN base_user c ON ( b.user_id = c.id )
  92. LEFT JOIN socialize_message d ON ( a.message_id = d.id )
  93. ORDER BY a.timestamp DESC LIMIT 0,500
  94. )`, in.PositionId, oneNameSql)
  95. }
  96. //客服列表
  97. serviceSql := fmt.Sprintf(` (
  98. SELECT
  99. a.ent_id AS id,
  100. 1 AS userType,
  101. b.nickname AS name,
  102. b.headimage as headimg,
  103. c.content,
  104. c.type,
  105. c.create_time,
  106. a.unread as number,
  107. a.timestamp,
  108. "" as phone,
  109. "" as groupMember
  110. FROM
  111. socialize_summary a
  112. INNER JOIN socialize_tenant_robot b ON ( a.user_id = %d AND a.ent_id = b.ent_id %s )
  113. LEFT JOIN socialize_message c ON ( a.message_id = c.id )
  114. ORDER BY a.timestamp DESC LIMIT 0,500
  115. ) `, in.NewUserId, serviceNameSql)
  116. //群列表
  117. groupSql := fmt.Sprintf(` (
  118. SELECT
  119. b.id,
  120. 3 AS userType,
  121. b.name,
  122. '' AS headimg,
  123. d.content,
  124. d.type,
  125. d.create_time,
  126. a.unread as number,
  127. a.timestamp,
  128. "" as phone,
  129. b.groupMember
  130. FROM
  131. socialize_summary a
  132. INNER JOIN %s b ON ( a.my_position_id = %d AND a.chat_group_id = b.id)
  133. LEFT JOIN socialize_message d ON ( a.message_id = d.id )
  134. ORDER BY a.timestamp DESC LIMIT 0,500
  135. ) `, GroupNameSql, in.PositionId)
  136. if oneSql != "" {
  137. sqlArr = append(sqlArr, oneSql)
  138. }
  139. if groupSql != "" {
  140. sqlArr = append(sqlArr, groupSql)
  141. }
  142. switch in.QueryType {
  143. case 1: //分享列表
  144. if len(sqlArr) > 1 {
  145. allSql = strings.Join(sqlArr, " UNION ALL ")
  146. } else {
  147. allSql = sqlArr[0]
  148. }
  149. default:
  150. //历史会话列表
  151. if serviceSql != "" {
  152. sqlArr = append(sqlArr, serviceSql)
  153. }
  154. allSql = strings.Join(sqlArr, " UNION ALL ")
  155. }
  156. sqlStr = fmt.Sprintf(`SELECT * FROM(
  157. %s
  158. ) a ORDER BY a.timestamp DESC LIMIT 0,500`, allSql)
  159. } else {
  160. var timeSql, phoneSql, filtrationSql string
  161. if in.StartTime != "" {
  162. timeSql += fmt.Sprintf(" AND a.timestamp >= '%s 00:00:00'", in.StartTime)
  163. }
  164. if in.EndTime != "" {
  165. timeSql += fmt.Sprintf(" AND a.timestamp <= '%s 23:59:59'", in.EndTime)
  166. }
  167. if in.Phone != "" {
  168. phoneSql = " AND b.phone like '%" + in.Phone + "%'"
  169. }
  170. if in.FiltrationId != "" {
  171. var ids []string
  172. for _, v := range strings.Split(in.FiltrationId, ",") {
  173. ids = append(ids, encrypt.SE.Decode4Hex(v))
  174. }
  175. filtrationSql += fmt.Sprintf(" AND b.id not in (%s)", strings.Join(ids, ","))
  176. }
  177. aiSql := fmt.Sprintf(`(SELECT
  178. a.user_id,
  179. a.message_id,
  180. a.timestamp,
  181. b.nickname,
  182. 0 as unread,
  183. b.headimg,
  184. b.phone,
  185. 0 as userType
  186. FROM
  187. socialize_summary a
  188. INNER JOIN base_user b ON (a.user_id = b.id AND a.ent_id = %d AND a.customer_service_access = 0)
  189. %s %s %s)`, in.EntId, timeSql, phoneSql, filtrationSql)
  190. serviceSql := fmt.Sprintf(`(SELECT
  191. a.user_id,
  192. c.message_id,
  193. c.timestamp,
  194. b.nickname,
  195. a.unread,
  196. b.headimg,
  197. b.phone,
  198. 0 as userType
  199. FROM
  200. socialize_customer_service_user a
  201. INNER JOIN base_user b ON ( a.customer_service_id = %d AND a.user_id = b.id %s %s)
  202. INNER JOIN socialize_summary c ON ( a.ent_id = c.ent_id AND a.user_id = c.user_id %s))`, in.EntUserId, filtrationSql, phoneSql,
  203. strings.ReplaceAll(timeSql, "a.", "c."))
  204. var restrictionSql string
  205. switch in.IsArtificial {
  206. case 1:
  207. restrictionSql = serviceSql
  208. case 2:
  209. restrictionSql = aiSql
  210. default:
  211. restrictionSql = aiSql + " UNION ALL " + serviceSql
  212. }
  213. if in.Page <= 0 {
  214. in.Page = 1
  215. }
  216. if in.Size <= 0 || in.Size > 100 {
  217. in.Size = 50
  218. }
  219. sqlStr = fmt.Sprintf(`SELECT
  220. (
  221. CASE
  222. WHEN SUBSTR( a.nickname, 1, 3 ) = 'JY_' THEN
  223. CONCAT( SUBSTR( a.phone, 1, 3 ), '****', SUBSTR( a.phone, 8, 11 ) )
  224. WHEN a.nickname = ''
  225. OR a.nickname IS NULL THEN
  226. CONCAT( SUBSTR( a.phone, 1, 3 ), '****', SUBSTR( a.phone, 8, 11 ) ) ELSE a.nickname
  227. END
  228. ) AS name,
  229. c.content,
  230. c.title,
  231. c.type,
  232. c.link,
  233. c.create_time,
  234. a.message_id,
  235. a.timestamp,
  236. a.unread as number,
  237. a.user_id as id,
  238. a.headimg,
  239. a.phone,
  240. a.userType
  241. FROM
  242. (%s) a
  243. LEFT JOIN socialize_message c ON ( a.message_id = c.id )
  244. ORDER BY
  245. a.timestamp DESC`, restrictionSql)
  246. }
  247. var dataSize []map[string]interface{}
  248. if sqlStr != "" {
  249. log.Println("查询列表sql:", sqlStr)
  250. data = IC.BaseMysql.SelectBySql(sqlStr)
  251. if data != nil && len(*data) > 0 {
  252. count = quitl.Int64All(len(*data))
  253. log.Println("查询列表耗时2:", time.Since(tm), in.QueryType, count)
  254. switch in.UserType { //客服查询结果分页
  255. case 1:
  256. if in.Page*in.Size >= count {
  257. dataSize = (*data)[(in.Page-1)*in.Size:]
  258. } else {
  259. dataSize = (*data)[(in.Page-1)*in.Size : in.Page*in.Size]
  260. }
  261. return &dataSize, count, err
  262. case 2: //用户查询结果
  263. phoneMap, _, positionMap := EntPerson(in.EntId, true) //获取企业架构人员名称
  264. log.Println("查询列表耗时3:", time.Since(tm))
  265. for _, v := range *data {
  266. if quitl.IntAll(v["userType"]) == 2 {
  267. v["name"] = phoneMap[quitl.InterfaceToStr(v["phone"])]
  268. } else if quitl.IntAll(v["userType"]) == 3 {
  269. var names []string
  270. if quitl.InterfaceToStr(v["groupMember"]) != "" {
  271. for _, id := range strings.Split(quitl.InterfaceToStr(v["groupMember"]), ",") {
  272. if len(names) > 4 {
  273. break
  274. }
  275. //防止匹配的是群名称 去除不匹配的群成员
  276. if strings.Contains(positionMap[quitl.IntAll(id)], in.NameSearch) {
  277. names = append(names, positionMap[quitl.IntAll(id)])
  278. }
  279. }
  280. }
  281. v["groupMember"] = names
  282. }
  283. }
  284. }
  285. }
  286. }
  287. return
  288. }
  289. // 客服会话列表
  290. func (b MessaggeService) ConversationList(in *messagecenter.ConversationReq) (data *[]map[string]interface{}, count int64, err error) {
  291. sqlStr := ""
  292. tm := time.Now()
  293. if in.UserType == 1 && in.FiltrationId != "" {
  294. var ids []string
  295. for _, v := range strings.Split(in.FiltrationId, ",") {
  296. ids = append(ids, encrypt.SE.Decode4Hex(v))
  297. }
  298. filtrationSql := fmt.Sprintf(" AND b.id in (%s)", strings.Join(ids, ","))
  299. aiSql := fmt.Sprintf(`(SELECT
  300. a.user_id,
  301. a.message_id,
  302. a.timestamp,
  303. b.nickname,
  304. 0 as unread,
  305. b.headimg,
  306. b.phone
  307. FROM
  308. socialize_summary a
  309. INNER JOIN base_user b ON (a.user_id = b.id AND a.ent_id = %d AND a.customer_service_access = 0
  310. %s ))`, in.EntId, filtrationSql)
  311. serviceSql := fmt.Sprintf(`(SELECT
  312. a.user_id,
  313. c.message_id,
  314. c.timestamp,
  315. b.nickname,
  316. a.unread,
  317. b.headimg,
  318. b.phone
  319. FROM
  320. socialize_customer_service_user a
  321. INNER JOIN base_user b ON ( a.customer_service_id = %d AND a.user_id = b.id %s)
  322. INNER JOIN socialize_summary c ON ( a.ent_id = c.ent_id AND a.user_id = c.user_id))`, in.EntUserId, filtrationSql)
  323. restrictionSql := aiSql + " UNION ALL " + serviceSql
  324. sqlStr = fmt.Sprintf(`SELECT
  325. (
  326. CASE
  327. WHEN SUBSTR( a.nickname, 1, 3 ) = 'JY_' THEN
  328. CONCAT( SUBSTR( a.phone, 1, 3 ), '****', SUBSTR( a.phone, 8, 11 ) )
  329. WHEN a.nickname = ''
  330. OR a.nickname IS NULL THEN
  331. CONCAT( SUBSTR( a.phone, 1, 3 ), '****', SUBSTR( a.phone, 8, 11 ) ) ELSE a.nickname
  332. END
  333. ) AS name,
  334. c.content,
  335. c.title,
  336. c.type,
  337. c.link,
  338. c.create_time,
  339. a.message_id,
  340. a.timestamp,
  341. a.unread as number,
  342. a.user_id as id,
  343. a.headimg,
  344. a.phone
  345. FROM
  346. (%s) a
  347. LEFT JOIN socialize_message c ON ( a.message_id = c.id )
  348. ORDER BY
  349. a.timestamp DESC`, restrictionSql)
  350. }
  351. if sqlStr != "" {
  352. log.Println("ConversationList查询列表sql:", sqlStr)
  353. data = IC.BaseMysql.SelectBySql(sqlStr)
  354. log.Println("查询耗时2:", time.Since(tm), count)
  355. }
  356. return
  357. }
  358. // 消息保存
  359. func (b MessaggeService) SaveMessage(in *messagecenter.MessageEntity) (fool bool, errorMsg string, content string, messageId, nowInt int64) {
  360. //先插入信息表
  361. //判断会话标识是否属于本人
  362. var customer_service_id, userid, entid, message_id int64
  363. nowForm := time.Now().Local()
  364. create_time := nowForm.Format(util.Date_Full_Layout)
  365. userId := int64(0)
  366. sessionId := int64(0)
  367. switch in.ItemType {
  368. case 4, 5, 8:
  369. if in.OwnType == 1 {
  370. sessionId = in.ReceiveId
  371. userId = in.NewUserId
  372. } else {
  373. sessionId = in.SendId
  374. userId = in.ReceiveId
  375. if in.ItemType == 4 || in.ItemType == 8 {
  376. userId = in.NewUserId
  377. }
  378. }
  379. break
  380. case 6:
  381. if in.OwnType == 1 {
  382. sessionId = in.ReceiveId
  383. userId = in.NewUserId
  384. } else if in.OwnType == 2 {
  385. sessionId = in.SendId
  386. userId = in.ReceiveId
  387. } else {
  388. sessionId = in.ReceiveId
  389. userId = in.NewUserId
  390. }
  391. break
  392. }
  393. //查找会话信息
  394. chatJson := IC.BaseMysql.FindOne(util.SOCIALIZE_CHAT_SESSION, map[string]interface{}{"id": sessionId}, "user_id,ent_id,customer_service_id", "")
  395. if chatJson == nil {
  396. return false, "会话标识不存在", "", 0, nowForm.Unix()
  397. } else {
  398. if userId != quitl.Int64All((*chatJson)["user_id"]) {
  399. return false, "会话标识不属于此用户", "", 0, nowForm.Unix()
  400. }
  401. }
  402. customer_service_id = quitl.Int64All((*chatJson)["customer_service_id"])
  403. entid = quitl.Int64All((*chatJson)["ent_id"])
  404. userid = userId
  405. userType := int64(1)
  406. //客服相关信息保存
  407. fool = IC.BaseMysql.ExecTx("聊天信息保存", func(tx *sql.Tx) bool {
  408. //先插入信息表
  409. message := map[string]interface{}{
  410. "appid": in.Appid,
  411. "title": in.Title,
  412. "content": in.Content,
  413. "item": in.Item,
  414. "type": in.Type,
  415. "link": in.Link,
  416. "create_time": create_time,
  417. "create_person": in.SendId,
  418. }
  419. data := map[string]interface{}{
  420. "item": in.Item,
  421. "type": in.Type,
  422. "create_time": create_time,
  423. "content": in.Content,
  424. }
  425. ok := IC.BaseMysql.InsertByTx(tx, util.SOCIALIZE_MESSAGE, message)
  426. receiveOk := int64(0)
  427. messageId, message_id = ok, ok
  428. data["id"] = ok
  429. //在插入邮箱表socialize_message_mailbox
  430. messageMailBox := map[string]interface{}{
  431. "appid": in.Appid,
  432. "messag_id": ok,
  433. "type": in.ItemType,
  434. "create_time": nowForm.Format(util.Date_Full_Layout),
  435. "isread": 0,
  436. "send_isdel": 0,
  437. "receive_isdel": 0,
  438. "iswithdraw": 0,
  439. }
  440. //系统信息处理
  441. if in.ItemType == 6 {
  442. messageMailBox = map[string]interface{}{
  443. "appid": in.Appid,
  444. "messag_id": ok,
  445. "type": in.ItemType,
  446. "create_time": nowForm.Format(util.Date_Full_Layout),
  447. "read_time": nowForm.Format(util.Date_Full_Layout),
  448. "isread": 1,
  449. "send_isdel": 0,
  450. "receive_isdel": 0,
  451. "iswithdraw": 0,
  452. }
  453. if in.OwnType == 1 {
  454. //用户接受系统信息
  455. messageMailBox["own_type"] = 2
  456. messageMailBox["send_user_type"] = 1
  457. messageMailBox["receive_user_type"] = 2
  458. messageMailBox["own_id"] = in.NewUserId
  459. messageMailBox["send_user_id"] = in.ReceiveId
  460. messageMailBox["receive_user_id"] = in.NewUserId
  461. } else if in.OwnType == 2 {
  462. //客服接受系统信息
  463. messageMailBox["own_type"] = 1
  464. messageMailBox["send_user_type"] = 2
  465. messageMailBox["receive_user_type"] = 1
  466. messageMailBox["own_id"] = in.SendId
  467. messageMailBox["send_user_id"] = in.ReceiveId
  468. messageMailBox["receive_user_id"] = in.SendId
  469. } else {
  470. //客服接受系统信息
  471. messageMailBox["own_type"] = 1
  472. messageMailBox["send_user_type"] = 2
  473. messageMailBox["receive_user_type"] = 1
  474. messageMailBox["own_id"] = in.ReceiveId
  475. messageMailBox["send_user_id"] = in.NewUserId
  476. messageMailBox["receive_user_id"] = in.ReceiveId
  477. }
  478. receiveOk = IC.BaseMysql.InsertByTx(tx, util.SOCIALIZE_MESSAGE_MAILBOX, messageMailBox)
  479. return ok > 1 && receiveOk > 1
  480. }
  481. if in.ItemType == 4 || in.ItemType == 8 || in.ItemType == 5 {
  482. //客服或者机器人聊天
  483. if in.OwnType == 1 {
  484. // (用户发送)客服接受
  485. messageMailBox["own_type"] = 1
  486. messageMailBox["send_user_type"] = 2
  487. messageMailBox["receive_user_type"] = 1
  488. messageMailBox["own_id"] = in.ReceiveId
  489. messageMailBox["send_user_id"] = in.NewUserId
  490. messageMailBox["receive_user_id"] = in.ReceiveId
  491. userType = 1
  492. userId = in.ReceiveId
  493. if in.ItemType == 4 {
  494. messageMailBox["isread"] = 1
  495. }
  496. } else {
  497. //客服发送(用户接收信息)
  498. messageMailBox["own_type"] = 2
  499. messageMailBox["send_user_type"] = 1
  500. messageMailBox["receive_user_type"] = 2
  501. messageMailBox["own_id"] = in.ReceiveId
  502. messageMailBox["send_user_id"] = in.SendId
  503. messageMailBox["receive_user_id"] = in.ReceiveId
  504. userId = in.ReceiveId
  505. if in.ItemType == 4 || in.ItemType == 8 {
  506. messageMailBox["receive_user_id"] = in.NewUserId
  507. userId = in.NewUserId
  508. messageMailBox["own_id"] = in.NewUserId
  509. messageMailBox["isread"] = 1
  510. messageMailBox["read_time"] = nowForm.Format(util.Date_Full_Layout)
  511. }
  512. userType = 2
  513. }
  514. } else {
  515. messageMailBox["own_type"] = 2
  516. messageMailBox["send_user_type"] = 2
  517. messageMailBox["receive_user_type"] = 2
  518. messageMailBox["own_id"] = in.ReceiveId
  519. messageMailBox["send_user_id"] = in.NewUserId
  520. messageMailBox["receive_user_id"] = in.ReceiveId
  521. userType = 2
  522. userId = in.ReceiveId
  523. }
  524. receiveOk = IC.BaseMysql.InsertByTx(tx, util.SOCIALIZE_MESSAGE_MAILBOX, messageMailBox)
  525. messageMailBox = map[string]interface{}{
  526. "appid": in.Appid,
  527. "type": in.ItemType,
  528. "create_time": nowForm.Format(util.Date_Full_Layout),
  529. "messag_id": ok,
  530. "read_time": nowForm.Format(util.Date_Full_Layout),
  531. "isread": 1,
  532. "send_isdel": 0,
  533. "receive_isdel": 0,
  534. "iswithdraw": 0,
  535. }
  536. if in.ItemType == 4 || in.ItemType == 8 || in.ItemType == 5 {
  537. //客服或者机器人聊天
  538. if in.OwnType == 1 {
  539. //用户发送(用户接受)
  540. messageMailBox["own_type"] = 2
  541. messageMailBox["send_user_type"] = 2
  542. messageMailBox["receive_user_type"] = 1
  543. messageMailBox["own_id"] = in.NewUserId
  544. messageMailBox["send_user_id"] = in.NewUserId
  545. messageMailBox["receive_user_id"] = in.ReceiveId
  546. } else {
  547. //客服发送信息(用户接受)
  548. messageMailBox["own_type"] = 1
  549. messageMailBox["send_user_type"] = 1
  550. messageMailBox["receive_user_type"] = 2
  551. messageMailBox["own_id"] = in.SendId
  552. messageMailBox["send_user_id"] = in.SendId
  553. messageMailBox["receive_user_id"] = in.ReceiveId
  554. if in.ItemType == 4 || in.ItemType == 8 {
  555. messageMailBox["receive_user_id"] = in.NewUserId
  556. }
  557. }
  558. } else {
  559. messageMailBox["own_type"] = 2
  560. messageMailBox["send_user_type"] = 2
  561. messageMailBox["receive_user_type"] = 2
  562. messageMailBox["own_id"] = in.NewUserId
  563. messageMailBox["send_user_id"] = in.NewUserId
  564. messageMailBox["receive_user_id"] = in.ReceiveId
  565. }
  566. receiveOk = IC.BaseMysql.InsertByTx(tx, util.SOCIALIZE_MESSAGE_MAILBOX, messageMailBox)
  567. return ok > 1 && receiveOk > 1
  568. })
  569. if fool {
  570. go UserSynchronousList(customer_service_id, userid, entid, message_id, create_time, quitl.Int64All(quitl.If(userType == 1, 2, 1)), in.ItemType)
  571. }
  572. return fool, "", in.Content, messageId, nowForm.Unix()
  573. }
  574. // 客服 用户聊天消息列表同步
  575. // sendUserType 发送人类型 1客服 2用户
  576. func UserSynchronousList(customerServiceId, userId, entId, messageId int64, createTime string, sendUserType, itemType int64) {
  577. log.Printf("同步最后消息参数customerServiceId:%d,userId:%d,entId%d,messageId:%d", customerServiceId, userId, entId, messageId)
  578. nowForm := time.Now().Local()
  579. create_time := nowForm.Format(util.Date_Full_Layout)
  580. //查看汇总表
  581. IC.BaseMysql.ExecTx("更新汇总表", func(tx *sql.Tx) bool {
  582. data := IC.BaseMysql.SelectBySqlByTx(tx, "select customer_service_access from socialize_summary where ent_id=? and user_id=? for update ", entId, userId)
  583. //data := FindOne(util.SOCIALIZE_SUMMARY, map[string]interface{}{"user_id": userId, "ent_id": entId}, "customer_service_access", "")
  584. customer_service_access := 0
  585. if data != nil && len(*data) > 0 {
  586. if quitl.IntAll((*data)[0]["customer_service_access"]) == 0 && customerServiceId > 0 {
  587. customer_service_access = 1
  588. } else {
  589. customer_service_access = quitl.IntAll((*data)[0]["customer_service_access"])
  590. }
  591. log.Println(customer_service_access, "customer_service_access打印", (*data)[0]["customer_service_access"], customerServiceId, quitl.IntAll((*data)[0]["customer_service_access"]) == 0 && customerServiceId > 0)
  592. //更新汇总表
  593. ok := int64(0)
  594. if sendUserType == 1 {
  595. if itemType == 4 {
  596. //机器人消息保存处理
  597. ok = IC.BaseMysql.UpdateOrDeleteBySqlByTx(tx, fmt.Sprintf("UPDATE socialize_summary SET message_id = %d ,timestamp = '%s' WHERE user_id = %d and ent_id= %d", messageId, create_time, userId, entId))
  598. } else {
  599. ok = IC.BaseMysql.UpdateOrDeleteBySqlByTx(tx, fmt.Sprintf("UPDATE socialize_summary SET message_id = %d ,timestamp = '%s', unread = unread+1, customer_service_access = %d WHERE user_id = %d and ent_id= %d", messageId, create_time, customer_service_access, userId, entId))
  600. }
  601. } else {
  602. ok = IC.BaseMysql.UpdateOrDeleteBySqlByTx(tx, fmt.Sprintf("UPDATE socialize_summary SET message_id = %d ,timestamp = '%s', customer_service_access = %d WHERE user_id = %d and ent_id= %d", messageId, create_time, customer_service_access, userId, entId))
  603. }
  604. if ok > 0 && customerServiceId > 0 {
  605. //判断客服用户表是否存在
  606. if IC.BaseMysql.Count(util.Socialize_customer_service_user, map[string]interface{}{"user_id": userId, "ent_id": entId, "customer_service_id": customerServiceId}) > 0 {
  607. if sendUserType == 2 {
  608. IC.BaseMysql.UpdateOrDeleteBySqlByTx(tx, fmt.Sprintf("UPDATE socialize_customer_service_user SET unread = unread+1 WHERE user_id = %d and customer_service_id= %d", userId, customerServiceId))
  609. }
  610. } else {
  611. IC.BaseMysql.Insert(util.SOCIALIZE_CUSTOMER_SERVICE_USER, map[string]interface{}{
  612. "user_id": userId,
  613. "ent_id": entId,
  614. "customer_service_id": customerServiceId,
  615. "unread": quitl.If(sendUserType == 1, 0, 1),
  616. })
  617. }
  618. }
  619. } else {
  620. //新增汇总表
  621. ok := IC.BaseMysql.InsertByTx(tx, util.SOCIALIZE_SUMMARY, map[string]interface{}{
  622. "user_id": userId,
  623. "ent_id": entId,
  624. "customer_service_access": quitl.If(customerServiceId > 0, 1, 0),
  625. "timestamp": create_time,
  626. "unread": quitl.If(sendUserType == 1, 0, 1),
  627. "message_id": messageId,
  628. })
  629. //新增客服用户表
  630. if ok > 0 && customerServiceId > 0 {
  631. IC.BaseMysql.InsertByTx(tx, util.SOCIALIZE_CUSTOMER_SERVICE_USER, map[string]interface{}{
  632. "user_id": userId,
  633. "ent_id": entId,
  634. "customer_service_id": customerServiceId,
  635. "unread": quitl.If(sendUserType == 1, 0, 1),
  636. })
  637. } else {
  638. return false
  639. }
  640. }
  641. return true
  642. })
  643. }
  644. // 历史信息查询
  645. func (b MessaggeService) FindMessage(in *messagecenter.MessageReq) *[]map[string]interface{} {
  646. //log.Println(in.PositionId, in.NewUserId, in.EntId, in.ChatGroupId)
  647. sqlStr := ""
  648. lastStr := ""
  649. positionStr := ""
  650. if in.LastId > 0 {
  651. if in.Sort == "asc" {
  652. lastStr = fmt.Sprintf("AND a.messag_id > %d ", in.LastId)
  653. } else {
  654. lastStr = fmt.Sprintf("AND a.messag_id < %d ", in.LastId)
  655. }
  656. }
  657. switch in.MsgType {
  658. case 2: //点对点聊天
  659. sqlStr = fmt.Sprintf("SELECT a.messag_id as messageId,a.send_user_type,a.type AS itemType,b.* ,if(a.own_id = a.send_user_id,1,2) as fool, "+
  660. "IF ( a.own_id = a.send_user_id, 0, a.send_user_id ) AS send_position_id "+
  661. "FROM %s a "+
  662. "LEFT JOIN %s b on a.messag_id=b.id "+
  663. "LEFT JOIN %s c on c.id=a.send_user_id "+
  664. "LEFT JOIN %s d on d.id=a.receive_user_id "+
  665. "where a.own_id= %d and a.iswithdraw = 0 "+
  666. "AND ((a.send_user_id= %d AND a.receive_user_id= %d) or (a.send_user_id= %d AND a.receive_user_id= %d)) "+
  667. "AND a.type IN ( 2, 6 ) %s "+
  668. "AND a.chat_group_id IS null "+
  669. "AND a.send_user_type = a.receive_user_type "+
  670. "ORDER BY a.id desc "+
  671. "limit 0 , %d ",
  672. util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_MESSAGE, util.BASE_USER, util.BASE_USER,
  673. in.PositionId, in.PositionId, in.SendId, in.SendId, in.PositionId, lastStr, in.PageSize)
  674. break
  675. case 3: //群聊天
  676. sqlStr = fmt.Sprintf("SELECT a.messag_id AS messageId,b.*,a.send_user_type,a.type AS itemType,"+
  677. "IF ( a.own_id = a.send_user_id, 1, 2 ) AS fool,"+
  678. "IF ( a.own_id = a.send_user_id, 0, a.send_user_id ) AS send_position_id,"+
  679. "a.send_user_type,a.type AS itemType FROM %s a "+
  680. "LEFT JOIN %s b ON a.messag_id = b.id "+
  681. "WHERE a.own_type = 2 and a.iswithdraw = 0 AND a.own_id = %d "+
  682. "AND a.chat_group_id = %d AND a.type IN ( 3, 6 ) %s "+
  683. "ORDER BY a.id DESC "+
  684. "LIMIT 0, %d",
  685. util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_MESSAGE, in.PositionId,
  686. in.ChatGroupId, lastStr, in.PageSize)
  687. case 4, 5, 6, 7: //客服聊天
  688. //查询用户所有的职位id
  689. positionStr = GetUserAllPosition(in.NewUserId)
  690. if in.UserType == 1 {
  691. //客服聊天记录查看
  692. 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 "+
  693. "FROM %s a "+
  694. "LEFT JOIN %s b ON a.messag_id = b.id "+
  695. "LEFT JOIN %s c ON a.own_type = 1 AND a.own_id=c.id "+
  696. "WHERE a.own_type = 1 and a.iswithdraw = 0 "+
  697. "AND (a.type = 5 or a.type=4 or a.type=6 or a.type=7 or a.type=8 ) "+
  698. "AND c.ent_id = %d "+
  699. "AND c.user_id = %d %s "+
  700. "AND a.send_user_type != a.receive_user_type "+
  701. "ORDER BY a.id desc "+
  702. "limit 0 , %d ",
  703. util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_MESSAGE, util.SOCIALIZE_CHAT_SESSION,
  704. in.EntId, in.SendId, lastStr, in.PageSize)
  705. } else {
  706. //用户聊天记录查看
  707. sqlStr = fmt.Sprintf("SELECT a.messag_id as messageId,e.appraise as appraise, 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 "+
  708. "FROM %s a "+
  709. "LEFT JOIN %s b ON a.messag_id = b.id "+
  710. "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 "+
  711. "LEFT JOIN %s d on c.ent_id=d.ent_id "+
  712. "LEFT JOIN %s e on e.messag_id=b.id "+
  713. "WHERE a.own_type = 2 and a.iswithdraw = 0 "+
  714. "AND a.own_id = %d "+
  715. "AND c.ent_id = %d "+
  716. "AND c.user_id = %d "+
  717. "AND a.send_user_type != a.receive_user_type "+
  718. "AND ( a.type = 4 OR a.type = 5 or a.type=6 or a.type=7 or a.type=8) %s "+
  719. "ORDER BY a.id desc "+
  720. "limit 0 , %d ",
  721. util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_MESSAGE, util.SOCIALIZE_CHAT_SESSION, in.SendId, in.NewUserId, util.SOCIALIZE_TENANT_ROBOT, util.SOCIALIZE_APPRAISE,
  722. in.NewUserId, in.SendId, in.NewUserId, lastStr, in.PageSize)
  723. }
  724. break
  725. }
  726. log.Println(sqlStr)
  727. data := IC.BaseMysql.SelectBySql(sqlStr)
  728. //查询非自己发送消息得发送人名字
  729. //自己头像处理
  730. if in.UserType == 2 {
  731. userData := IC.BaseMysql.FindOne(util.BASE_USER, map[string]interface{}{"id": in.NewUserId}, "headimg", "")
  732. if userData != nil {
  733. for key := range *data {
  734. (*data)[key]["ownImg"] = (*userData)["headimg"]
  735. }
  736. }
  737. }
  738. if (in.MsgType == 3 || in.MsgType == 2) && data != nil && len(*data) > 0 {
  739. _, _, positionData := EntPerson(in.EntId, true)
  740. for _, v := range *data {
  741. positionId := quitl.IntAll(v["send_position_id"])
  742. if positionId != 0 {
  743. v["userName"] = positionData[positionId]
  744. //log.Println(v["userName"], positionData[positionId])
  745. }
  746. }
  747. }
  748. go func() {
  749. updateMap := map[string]interface{}{}
  750. if len(*data) > 0 && data != nil {
  751. //if true {
  752. //未读信息修改
  753. switch in.MsgType {
  754. case 2: //点对点聊天
  755. updateMap = map[string]interface{}{
  756. "own_type": 2,
  757. "own_id": in.PositionId,
  758. "send_user_id": in.SendId,
  759. "type": 2,
  760. "isread": 0,
  761. }
  762. IC.BaseMysql.Update(util.SOCIALIZE_MESSAGE_MAILBOX, updateMap, map[string]interface{}{"isread": 1, "read_time": time.Now().Local().Format(util.Date_Full_Layout)})
  763. //更新socialize_summary表未读消息数量
  764. updateQuery := map[string]interface{}{
  765. "my_position_id": in.PositionId,
  766. "your_position_id": in.SendId,
  767. }
  768. IC.BaseMysql.Update(util.SOCIALIZE_SUMMARY, updateQuery, map[string]interface{}{"unread": 0})
  769. break
  770. case 3: //群聊天
  771. sqlStr = fmt.Sprintf("UPDATE %s a SET a.isread = 1, a.read_time = now( ) "+
  772. "WHERE a.own_type = 1 "+
  773. "AND a.type IN (3,6) "+
  774. "AND a.isread = 0 "+
  775. "AND a.own_id = %d AND a.chat_group_id = %d",
  776. util.SOCIALIZE_MESSAGE_MAILBOX, in.SendId, in.ChatGroupId)
  777. IC.BaseMysql.UpdateOrDeleteBySql(sqlStr)
  778. //更新socialize_summary表未读消息数量
  779. updateQuery := map[string]interface{}{
  780. "my_position_id": in.PositionId,
  781. "chat_group_id": in.ChatGroupId,
  782. }
  783. IC.BaseMysql.Update(util.SOCIALIZE_SUMMARY, updateQuery, map[string]interface{}{"unread": 0})
  784. case 4, 5:
  785. sqlStr := ""
  786. unreadSql := ""
  787. if in.UserType == 1 { //1客服
  788. sqlStr = fmt.Sprintf("UPDATE %s a SET a.isread = 1, a.read_time = now( ) "+
  789. "WHERE a.own_type = 1 and a.iswithdraw = 0 "+
  790. "AND a.type IN ( 4,5,6,7) "+
  791. "AND a.isread = 0 "+
  792. "AND a.own_id IN (SELECT id FROM %s WHERE user_id = %d)",
  793. util.SOCIALIZE_MESSAGE_MAILBOX, util.BASE_POSITION, in.NewUserId)
  794. unreadSql = fmt.Sprintf("UPDATE %s SET unread = 0 WHERE user_id = %d AND customer_service_id = %d", util.Socialize_customer_service_user, in.SendId, in.EntUserId)
  795. } else { //2用户
  796. sqlStr = fmt.Sprintf("UPDATE %s a SET a.isread = 1, a.read_time = now( ) "+
  797. "WHERE a.own_type = 2 and a.iswithdraw = 0 "+
  798. "AND a.type IN ( 4,5,6,7 ) "+
  799. "AND a.isread = 0 "+
  800. "AND a.own_id in (%s) ", util.SOCIALIZE_MESSAGE_MAILBOX, positionStr)
  801. unreadSql = fmt.Sprintf("UPDATE %s SET unread = 0 WHERE user_id = %d AND ent_id = %d", util.SOCIALIZE_SUMMARY, in.NewUserId, in.SendId)
  802. }
  803. IC.BaseMysql.UpdateOrDeleteBySql(sqlStr)
  804. IC.BaseMysql.UpdateOrDeleteBySql(unreadSql)
  805. break
  806. }
  807. //redis缓存处理
  808. //b.Count(in.NewUserId, in.UserType, in.EntUserId, true)
  809. }
  810. }()
  811. return data
  812. }
  813. // 创建会话
  814. func (b MessaggeService) CreateChatSession(in *messagecenter.ChatSessionReq) (fool bool, sessionId int64) {
  815. fool = IC.BaseMysql.ExecTx("会话新建", func(tx *sql.Tx) bool {
  816. customerserviceName := in.CustomerserviceName
  817. if in.CustomerServiceId != 0 {
  818. sqlStr := fmt.Sprintf("select customer_service_name from %s "+
  819. "where ent_id= %d "+
  820. "AND customer_service_id= %d ",
  821. util.SOCIALIZE_CHAT_SESSION, in.EntId, in.CustomerServiceId)
  822. customerList := IC.BaseMysql.SelectBySql(sqlStr)
  823. if len(*customerList) > 0 {
  824. customerserviceName = quitl.InterfaceToStr((*customerList)[0]["customer_service_name"])
  825. }
  826. }
  827. //查询企业是否存在
  828. count := IC.BaseMysql.Count(util.SOCIALIZE_TENANT_ROBOT, map[string]interface{}{
  829. "ent_id": in.EntId,
  830. })
  831. if count < 1 {
  832. return false
  833. }
  834. chatMession := map[string]interface{}{
  835. "handle_status": 0,
  836. "start_time": time.Now().Local().Format(util.Date_Full_Layout),
  837. "appid": in.AppId,
  838. "ent_id": in.EntId,
  839. "customer_service_id": in.CustomerServiceId,
  840. "user_id": in.UserId,
  841. "customer_service_name": customerserviceName,
  842. }
  843. sessionId = IC.BaseMysql.InsertByTx(tx, util.SOCIALIZE_CHAT_SESSION, chatMession)
  844. return sessionId > 0
  845. })
  846. return
  847. }
  848. // 结束会话
  849. func (b MessaggeService) CloseChatSession(in *messagecenter.CloseSessionReq) bool {
  850. fool := IC.BaseMysql.ExecTx("关闭会话", func(tx *sql.Tx) bool {
  851. updateMap := map[string]interface{}{
  852. "id": in.SessionId,
  853. }
  854. fool := IC.BaseMysql.Update(util.SOCIALIZE_CHAT_SESSION, updateMap, map[string]interface{}{"start_time": time.Now().Local().Format(util.Date_Full_Layout)})
  855. return fool
  856. })
  857. return fool
  858. }
  859. // 创建会话并保存信息
  860. func (b *MessaggeService) SaveAutoReplyMsg(userType, entId, entUserId, userId int64, content, appId, nowFormat string) (bool, int64) {
  861. var customer_service_id, userid, entid, message_id int64
  862. messageId := int64(0)
  863. ok1 := IC.BaseMysql.ExecTx("保存自动回复消息", func(tx *sql.Tx) bool {
  864. entUserName := ""
  865. if entUserId > 0 {
  866. list := IC.BaseMysql.SelectBySql(`select ? from socialize_tenant_seat where appid=? AND ent_id=? AND customer_service_id=?`, util.SOCIALIZE_CHAT_SESSION, appId, entId, entUserId)
  867. if list != nil && len(*list) > 0 {
  868. entUserName, _ = (*list)[0]["customer_service_name"].(string)
  869. }
  870. }
  871. messageId = IC.BaseMysql.InsertBySqlByTx(tx, `insert into socialize_message (appid,content,item,type,create_time,create_person) values (?,?,?,?,?,?)`, appId, content, 8, 1, nowFormat, "admin")
  872. sessionId := IC.BaseMysql.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)
  873. ok := false
  874. if userType == 0 {
  875. //客服给用户发送,归属客服已读
  876. ok1 := IC.BaseMysql.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
  877. //客服给用户发送,归属用户未读
  878. ok2 := IC.BaseMysql.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
  879. ok = ok1 && ok2
  880. userType = 1
  881. } else if userType == 1 {
  882. // 客服给用户发送 客服接受已读系统信息
  883. ok = IC.BaseMysql.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
  884. userType = 2
  885. } else if userType == 2 {
  886. //客服给用户发送消息 用户接受未读读系统信息
  887. ok = IC.BaseMysql.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
  888. userType = 1
  889. }
  890. message_id = messageId
  891. return messageId > 0 && sessionId > 0 && ok
  892. })
  893. if ok1 {
  894. customer_service_id = entUserId
  895. userid = userId
  896. entid = entId
  897. go UserSynchronousList(customer_service_id, userid, entid, message_id, nowFormat, userType, 6)
  898. }
  899. return ok1, messageId
  900. }
  901. // 修改未读状态
  902. func (b MessaggeService) UpdateReadById(in *messagecenter.ReadStateReq) {
  903. updateMap := map[string]interface{}{}
  904. sqlStr := ""
  905. log.Println("UpdateReadById", in.PositionId, in.NewUserId, in.SendId, in.EntUserId, in.UserType)
  906. switch in.UserType {
  907. case 2: //点对点聊天
  908. updateMap = map[string]interface{}{
  909. "own_type": 2,
  910. "own_id": in.PositionId,
  911. "send_user_id": in.SendId,
  912. "type": 2,
  913. "isread": 0,
  914. }
  915. IC.BaseMysql.Update(util.SOCIALIZE_MESSAGE_MAILBOX, updateMap, map[string]interface{}{"isread": 1, "read_time": time.Now().Local().Format(util.Date_Full_Layout)})
  916. //更新socialize_summary表未读消息数量
  917. updateQuery := map[string]interface{}{
  918. "my_position_id": in.PositionId,
  919. "your_position_id": in.SendId,
  920. }
  921. IC.BaseMysql.Update(util.SOCIALIZE_SUMMARY, updateQuery, map[string]interface{}{"unread": 0})
  922. case 3: //群聊天
  923. sqlStr = fmt.Sprintf("UPDATE %s a SET a.isread = 1, a.read_time = now( ) "+
  924. "WHERE a.own_type = 1 "+
  925. "AND a.type IN (3,6) "+
  926. "AND a.isread = 0 "+
  927. "AND a.own_id = %d AND a.chat_group_id = %d",
  928. util.SOCIALIZE_MESSAGE_MAILBOX, in.PositionId, in.SendId)
  929. IC.BaseMysql.UpdateOrDeleteBySql(sqlStr)
  930. //更新socialize_summary表未读消息数量
  931. updateQuery := map[string]interface{}{
  932. "my_position_id": in.PositionId,
  933. "chat_group_id": in.SendId,
  934. }
  935. IC.BaseMysql.Update(util.SOCIALIZE_SUMMARY, updateQuery, map[string]interface{}{"unread": 0})
  936. case 1: // 客服聊天
  937. unreadSql := ""
  938. positionStr := GetUserAllPosition(in.NewUserId)
  939. if in.SendType == 1 { //1客服
  940. sqlStr = fmt.Sprintf("UPDATE %s a SET a.isread = 1, a.read_time = now( ) "+
  941. "WHERE a.own_type = 1 and a.iswithdraw = 0 "+
  942. "AND a.type IN ( 4,5,6,7) "+
  943. "AND a.isread = 0 "+
  944. "AND a.own_id IN (SELECT id FROM %s WHERE user_id = %d)",
  945. util.SOCIALIZE_MESSAGE_MAILBOX, util.BASE_POSITION, in.NewUserId)
  946. unreadSql = fmt.Sprintf("UPDATE %s SET unread = 0 WHERE user_id = %d AND customer_service_id = %d", util.Socialize_customer_service_user, in.SendId, in.EntUserId)
  947. } else { //2用户
  948. sqlStr = fmt.Sprintf("UPDATE %s a SET a.isread = 1, a.read_time = now( ) "+
  949. "WHERE a.own_type = 2 and a.iswithdraw = 0 "+
  950. "AND a.type IN ( 4,5,6,7 ) "+
  951. "AND a.isread = 0 "+
  952. "AND a.own_id in (%s) ", util.SOCIALIZE_MESSAGE_MAILBOX, positionStr)
  953. unreadSql = fmt.Sprintf("UPDATE %s SET unread = 0 WHERE user_id = %d AND ent_id = %d", util.SOCIALIZE_SUMMARY, in.NewUserId, in.SendId)
  954. }
  955. IC.BaseMysql.UpdateOrDeleteBySql(sqlStr)
  956. IC.BaseMysql.UpdateOrDeleteBySql(unreadSql)
  957. }
  958. //if true {
  959. //未读信息修改
  960. //redis缓存处理
  961. //b.Count(in.NewUserId, in.UserType, in.EntUserId, true)
  962. }
  963. // WithdrawMessage 撤回消息
  964. func (b MessaggeService) WithdrawMessage(in *messagecenter.ReadWithdrawReq) bool {
  965. messageId := encrypt.SE.Decode4Hex(in.MessageId)
  966. msg := IC.BaseMysql.FindOne(util.SOCIALIZE_MESSAGE, map[string]interface{}{"id": messageId}, "create_time", "")
  967. if msg == nil || len(*msg) <= 0 {
  968. log.Println("查询消息id失败")
  969. return false
  970. }
  971. createTime, _ := time.Parse(util.Date_Full_Layout, quitl.InterfaceToStr((*msg)["create_time"]))
  972. if createTime.Unix()+60*2 < time.Now().Unix() {
  973. log.Println("消息已超过2分钟,撤回失败")
  974. return false
  975. }
  976. nowForm := time.Now().Local()
  977. m := IC.BaseMysql.Update(util.SOCIALIZE_MESSAGE_MAILBOX,
  978. map[string]interface{}{"messag_id": messageId}, map[string]interface{}{"iswithdraw": 1, "withdraw_time": nowForm.Format(util.Date_Full_Layout)})
  979. if m {
  980. go SynchronousInfo(in.SenderId, in.RecipientId, in.ConversationType, quitl.Int64All(messageId), in.UserType, in.EntId, in.ChatGroupId)
  981. }
  982. return m
  983. }
  984. // 撤回消息同步信息
  985. func SynchronousInfo(sender, recipient, conversationType, messageId, userType, entId, chatGroupId int64) {
  986. switch conversationType {
  987. case 1: //1v1用户聊天
  988. if IC.BaseMysql.Count(util.SOCIALIZE_MESSAGE_MAILBOX, map[string]interface{}{"messag_id": messageId, "own_id": recipient, "isread": 0}) > 0 {
  989. IC.BaseMysql.SelectBySql(fmt.Sprintf("update %s set unread=CASE WHEN unread > 0 and my_position_id = %d and your_position_id = %d THEN unread-1 ELSE 0 END;", util.Socialize_summary, recipient, sender))
  990. }
  991. case 2: //用户与客服
  992. if IC.BaseMysql.Count(util.SOCIALIZE_MESSAGE_MAILBOX, map[string]interface{}{"messag_id": messageId, "own_id": recipient, "isread": 0}) > 0 {
  993. if userType == 2 { //发送人是用户
  994. //接收人是会话标识 查询客服id
  995. data := IC.BaseMysql.FindOne(util.SOCIALIZE_CHAT_SESSION, map[string]interface{}{"id": recipient}, "", "")
  996. if data != nil && len(*data) > 0 {
  997. IC.BaseMysql.SelectBySql(fmt.Sprintf("update %s set unread=CASE WHEN unread > 0 and user_id = %d and customer_service_id = %d and entId = %d THEN unread-1 ELSE 0 END;", util.Socialize_customer_service_user, sender, quitl.IntAll((*data)["customer_service_id"]), entId))
  998. }
  999. } else { //发送人是客服
  1000. IC.BaseMysql.SelectBySql(fmt.Sprintf("update %s set unread=CASE WHEN unread > 0 and user_id = %d and ent_id = %d THEN unread-1 ELSE 0 END;", util.Socialize_summary, recipient, entId))
  1001. }
  1002. }
  1003. case 3: //一对群
  1004. data := IC.BaseMysql.Find(util.SOCIALIZE_MESSAGE_MAILBOX, map[string]interface{}{"messag_id": messageId, "chat_group_id": chatGroupId, "isread": 0}, "own_id", "", -1, -1)
  1005. if data != nil && len(*data) > 0 {
  1006. var ownIds []string
  1007. for _, v := range *data {
  1008. ownIds = append(ownIds, quitl.InterfaceToStr(v["own_id"]))
  1009. }
  1010. IC.BaseMysql.SelectBySql(fmt.Sprintf("update %s set unread=CASE WHEN unread > 0 and chat_group_id = %d and my_position_id in (%s) THEN unread-1 ELSE 0 END;", util.Socialize_summary, chatGroupId, strings.Join(ownIds, ",")))
  1011. }
  1012. }
  1013. }
  1014. // AppraiseMessage 消息评价
  1015. func (b MessaggeService) AppraiseMessage(in *messagecenter.AppraiseReq) error {
  1016. messageId := encrypt.SE.Decode4Hex(in.MessageId)
  1017. //查询此条消息是否是当前用户的
  1018. if IC.BaseMysql.Count(util.SOCIALIZE_MESSAGE_MAILBOX, map[string]interface{}{
  1019. "messag_id": messageId,
  1020. "receive_user_id": in.NewUserId,
  1021. "receive_user_type": 2,
  1022. "type": 8,
  1023. }) == 0 {
  1024. return fmt.Errorf("未查询到信息")
  1025. }
  1026. if IC.BaseMysql.Count(util.SOCIALIZE_APPRAISE, map[string]interface{}{
  1027. "appid": in.Appid,
  1028. "messag_id": messageId,
  1029. }) > 0 {
  1030. return fmt.Errorf("请勿重复评价")
  1031. }
  1032. //插入评价
  1033. if IC.BaseMysql.Insert(util.SOCIALIZE_APPRAISE, map[string]interface{}{
  1034. "appid": in.Appid,
  1035. "messag_id": messageId,
  1036. "create_time": time.Now().Local().Format(util.Date_Full_Layout),
  1037. "appraise": in.Appraise,
  1038. }) > 0 {
  1039. return nil
  1040. }
  1041. return fmt.Errorf("评价消息异常")
  1042. }
  1043. // 聊天
  1044. // 包含 1v1 ,群聊,群发
  1045. /*
  1046. 类型;1:站内信消息 2:点对点消息 3:群消息 4:机器人消息 5:客服消息 6系统信息 7:客服自动回复 8:评价 9:其它系统消息 (除客服以外的) 10:撤回(自己看到的) 11:撤回(其他人看到的)
  1047. 群撤回自己看到的和用户看到的不一样
  1048. */
  1049. func (this *MessaggeService) Chat(in *messagecenter.MessageEntity) (fool bool, errorMsg string, content string, messageId, nowInt int64) {
  1050. now := time.Now()
  1051. nowTime := now.Format(date.Date_Full_Layout)
  1052. messageId = int64(0)
  1053. fool = IC.BaseMysql.ExecTx("消息存储", func(tx *sql.Tx) bool {
  1054. isGroup := len(in.GroupIds) > 0
  1055. isOneToOne := len(in.ReceiverIds) > 0
  1056. isWithdrawByMyself := in.ItemType == 10
  1057. isWithdrawByOthers := in.ItemType == 11
  1058. createperson := strconv.Itoa(int(in.SendId))
  1059. messageId = MessageAdd(tx, in.Appid, in.Title, in.Content, createperson, in.Link, in.Item, in.Type)
  1060. //是否客服介入
  1061. isCustomerServiceAccess := 0
  1062. //除客服消息以外的系统消息
  1063. if in.ItemType == 9 || in.ItemType == 10 || in.ItemType == 11 {
  1064. in.ItemType = 6
  1065. }
  1066. //群聊
  1067. if isGroup {
  1068. //修改类型为群聊
  1069. if in.ItemType != 6 {
  1070. in.ItemType = 3
  1071. }
  1072. fieids := []string{"appid", "messag_id", "type", "send_user_id", "send_user_type", "receive_user_id", "receive_user_type", "own_type", "own_id", "create_time", "chat_group_id", "isread"}
  1073. for _, v := range in.GroupIds {
  1074. args := []interface{}{}
  1075. groupUser := GetUserByGroupId(tx, v, in.SendId)
  1076. //发送人自己
  1077. if !isWithdrawByOthers {
  1078. args = append(args, in.Appid, messageId, in.ItemType, in.SendId, 2, in.SendId, 2, 2, in.SendId, nowTime, v, 1)
  1079. //最后一次聊天
  1080. SocializeSummaryAddOrUpdate(tx, v, in.SendId, 0, messageId, isCustomerServiceAccess, nowTime, false, in.ItemType)
  1081. }
  1082. for _, vv := range groupUser {
  1083. if !isWithdrawByMyself {
  1084. log.Println("获取到群组下员工:", vv)
  1085. //接收人其他用户
  1086. args = append(args, in.Appid, messageId, in.ItemType, in.SendId, 2, vv, 2, 2, vv, nowTime, v, 0)
  1087. //最后一次聊天
  1088. SocializeSummaryAddOrUpdate(tx, v, 0, vv, messageId, isCustomerServiceAccess, nowTime, false, in.ItemType)
  1089. }
  1090. }
  1091. MessageMailBoxAdd(tx, fieids, args)
  1092. }
  1093. }
  1094. //1v1
  1095. if isOneToOne {
  1096. //修改类型为个人
  1097. if in.ItemType != 6 {
  1098. in.ItemType = 2
  1099. }
  1100. fieids := []string{"appid", "messag_id", "type", "send_user_id", "send_user_type", "receive_user_id", "receive_user_type", "own_type", "own_id", "create_time", "isread"}
  1101. args := []interface{}{}
  1102. for _, v := range in.ReceiverIds {
  1103. if !isWithdrawByOthers {
  1104. args = append(args, in.Appid, messageId, in.ItemType, in.SendId, 2, v, 2, 2, in.SendId, nowTime, 1)
  1105. //最后一次聊天
  1106. SocializeSummaryAddOrUpdate(tx, 0, in.SendId, v, messageId, isCustomerServiceAccess, nowTime, true, in.ItemType)
  1107. }
  1108. if !isWithdrawByMyself {
  1109. args = append(args, in.Appid, messageId, in.ItemType, in.SendId, 2, v, 2, 2, v, nowTime, 0)
  1110. //最后一次聊天
  1111. SocializeSummaryAddOrUpdate(tx, 0, v, in.SendId, messageId, isCustomerServiceAccess, nowTime, false, in.ItemType)
  1112. }
  1113. }
  1114. MessageMailBoxAdd(tx, fieids, args)
  1115. }
  1116. return true
  1117. })
  1118. return fool, "", in.Content, messageId, now.Unix()
  1119. }
  1120. // 消息存储
  1121. func MessageAdd(tx *sql.Tx, appid, title, content, createperson, link string, item, messageType int64) int64 {
  1122. nowTime := time.Now()
  1123. message := map[string]interface{}{
  1124. "appid": appid,
  1125. "title": title,
  1126. "content": content,
  1127. "item": item,
  1128. "type": messageType,
  1129. "link": link,
  1130. "create_time": nowTime.Format(date.Date_Full_Layout),
  1131. "create_person": createperson, //系统消息时,创建人是群聊id或接收人id
  1132. }
  1133. return IC.BaseMysql.InsertByTx(tx, util.SOCIALIZE_MESSAGE, message)
  1134. }
  1135. // 消息信息箱存储
  1136. // types 2:点对点 3:群消息
  1137. func MessageMailBoxAdd(tx *sql.Tx, fieids []string, args []interface{}) (int64, int64) {
  1138. length, lastId := IC.BaseMysql.InsertBatchByTx(tx, util.SOCIALIZE_MESSAGE_MAILBOX, fieids, args)
  1139. log.Println("MessageMailBoxAdd length:", length, "MessageMailBoxAdd lastId:", lastId)
  1140. return length, lastId
  1141. }
  1142. // 获取除发送人以外的群成员
  1143. func GetUserByGroupId(tx *sql.Tx, groupId, sendId int64) []int64 {
  1144. arr := []int64{}
  1145. data := IC.BaseMysql.SelectBySqlByTx(tx, "select position_id from "+util.SOCIALIZE_CHAT_GROUP_PERSON+" where status = 1 and chat_group_id = ? AND position_id != ? ", groupId, sendId)
  1146. if data == nil || len(*data) <= 0 {
  1147. return arr
  1148. }
  1149. for _, v := range *data {
  1150. position_id := quitl.Int64All(v["position_id"])
  1151. if position_id == 0 {
  1152. continue
  1153. }
  1154. arr = append(arr, position_id)
  1155. }
  1156. return arr
  1157. }
  1158. // 最后一次聊天存储
  1159. // 1v1是双方的
  1160. // 群聊是发送人和其他人的
  1161. // send :true发送人 false接收人 用于取分1对1 身份
  1162. func SocializeSummaryAddOrUpdate(tx *sql.Tx, groupId, myPositionId, yourPositionId, messageId int64, isCustomerServiceAccess int, timestamp string, send bool, itemType int64) bool {
  1163. //判断是否存在
  1164. if groupId > 0 {
  1165. isSend := myPositionId != 0
  1166. if isSend {
  1167. if IC.BaseMysql.CountBySql(fmt.Sprintf(`select count(1) from %s where chat_group_id =? and my_position_id =?`, util.SOCIALIZE_SUMMARY), groupId, myPositionId) > 0 {
  1168. //存在更新
  1169. return IC.BaseMysql.UpdateByTx(tx, util.SOCIALIZE_SUMMARY, map[string]interface{}{
  1170. "chat_group_id": groupId,
  1171. "my_position_id": myPositionId,
  1172. }, map[string]interface{}{
  1173. "message_id": messageId,
  1174. "timestamp": timestamp,
  1175. })
  1176. } else {
  1177. //新增
  1178. return IC.BaseMysql.InsertByTx(tx, util.SOCIALIZE_SUMMARY, map[string]interface{}{
  1179. "chat_group_id": groupId,
  1180. "my_position_id": myPositionId,
  1181. "message_id": messageId,
  1182. "timestamp": timestamp,
  1183. "unread": 0,
  1184. }) > 0
  1185. }
  1186. } else {
  1187. if r := IC.BaseMysql.SelectBySql(fmt.Sprintf(`select unread from %s where chat_group_id =? and my_position_id =?`, util.SOCIALIZE_SUMMARY), groupId, yourPositionId); r != nil && len(*r) > 0 {
  1188. updateMap := map[string]interface{}{
  1189. "message_id": messageId,
  1190. "timestamp": timestamp,
  1191. }
  1192. if itemType != 6 {
  1193. updateMap["unread"] = gconv.Int((*r)[0]["unread"]) + 1
  1194. }
  1195. //存在更新
  1196. return IC.BaseMysql.UpdateByTx(tx, util.SOCIALIZE_SUMMARY, map[string]interface{}{
  1197. "chat_group_id": groupId,
  1198. "my_position_id": yourPositionId,
  1199. }, updateMap)
  1200. } else {
  1201. //新增
  1202. saveMap := map[string]interface{}{
  1203. "chat_group_id": groupId,
  1204. "my_position_id": yourPositionId,
  1205. "message_id": messageId,
  1206. "timestamp": timestamp,
  1207. }
  1208. if itemType != 6 {
  1209. saveMap["unread"] = 1
  1210. }
  1211. return IC.BaseMysql.InsertByTx(tx, util.SOCIALIZE_SUMMARY, saveMap) > 0
  1212. }
  1213. }
  1214. } else {
  1215. if yourPositionId > 0 {
  1216. ok_my := true
  1217. ok_your := true
  1218. //发送方
  1219. if r := IC.BaseMysql.SelectBySql(fmt.Sprintf(`select id,unread from %s where my_position_id =? and your_position_id =? limit 1`, util.SOCIALIZE_SUMMARY), myPositionId, yourPositionId); r != nil && len(*r) > 0 { //存在更新
  1220. updateMap := map[string]interface{}{
  1221. "message_id": messageId,
  1222. "timestamp": timestamp,
  1223. }
  1224. if !send && itemType != 6 {
  1225. updateMap["unread"] = gconv.Int((*r)[0]["unread"]) + 1
  1226. }
  1227. ok_my = IC.BaseMysql.UpdateByTx(tx, util.SOCIALIZE_SUMMARY, map[string]interface{}{
  1228. "my_position_id": myPositionId,
  1229. "your_position_id": yourPositionId,
  1230. }, updateMap)
  1231. } else {
  1232. //新增
  1233. saveMap := map[string]interface{}{
  1234. "my_position_id": myPositionId,
  1235. "your_position_id": yourPositionId,
  1236. "message_id": messageId,
  1237. "timestamp": timestamp,
  1238. "unread": 0,
  1239. "customer_service_access": isCustomerServiceAccess,
  1240. }
  1241. if !send && itemType != 6 {
  1242. saveMap["unread"] = 1
  1243. }
  1244. ok_my = IC.BaseMysql.InsertByTx(tx, util.SOCIALIZE_SUMMARY, saveMap) > 0
  1245. }
  1246. return ok_my && ok_your
  1247. }
  1248. }
  1249. return true
  1250. }
  1251. // 群组人员未读消息更新
  1252. func GroupUserUnReadUpdate(tx *sql.Tx, ids []int64) bool {
  1253. whs := []string{}
  1254. for i := 0; i < len(ids); i++ {
  1255. whs = append(whs, "?")
  1256. }
  1257. wh := strings.Join(whs, ",")
  1258. interfaces := gconv.Interfaces(ids)
  1259. count := IC.BaseMysql.UpdateOrDeleteBySql(`UPDATE `+util.SOCIALIZE_CHAT_GROUP_PERSON+` SET unread = unread + 1 WHERE id in (`+wh+`)`, interfaces...)
  1260. if count > 0 {
  1261. return true
  1262. }
  1263. return true
  1264. }
  1265. // GetUserAllPosition 用户下所有职位id
  1266. func GetUserAllPosition(baseUserId int64) (positionStr string) {
  1267. //查询用户所有的职位id
  1268. sqlPosition := fmt.Sprintf("SELECT id FROM %s WHERE user_id = %d", util.BASE_POSITION, baseUserId)
  1269. positionArr := IC.BaseMysql.SelectBySql(sqlPosition)
  1270. if positionArr != nil && len(*positionArr) > 0 {
  1271. for k, val := range *positionArr {
  1272. if k < len(*positionArr)-1 {
  1273. positionStr += strconv.Itoa(quitl.IntAll(val["id"])) + ","
  1274. } else {
  1275. positionStr += strconv.Itoa(quitl.IntAll(val["id"]))
  1276. }
  1277. }
  1278. }
  1279. return positionStr
  1280. }
  1281. // 1v1首次建立会话 创建汇总表
  1282. func OneCreatingSession(in *messagecenter.OneUserConversationReq) string {
  1283. if in.MyPositionId < 0 || in.YouPositionId < 0 {
  1284. return "参数异常"
  1285. }
  1286. if IC.BaseMysql.Count(util.SOCIALIZE_SUMMARY, map[string]interface{}{"my_position_id": in.MyPositionId, "your_position_id": in.YouPositionId}) > 0 {
  1287. return ""
  1288. }
  1289. if IC.BaseMysql.Insert(util.SOCIALIZE_SUMMARY, map[string]interface{}{
  1290. "my_position_id": in.MyPositionId,
  1291. "your_position_id": in.YouPositionId,
  1292. "message_id": 0,
  1293. "timestamp": time.Now().Format(date.Date_Full_Layout),
  1294. "unread": 0,
  1295. "customer_service_access": 0,
  1296. }) > 0 {
  1297. return ""
  1298. }
  1299. return "创建汇总表数据失败"
  1300. }
  1301. func GetSenderName(in *messagecenter.GetSenderNameReq) string {
  1302. var name string
  1303. switch in.SType {
  1304. case 1: //客服
  1305. data1 := IC.BaseMysql.FindOne("socialize_chat_session", map[string]interface{}{"id": in.Id}, "customer_service_id", "")
  1306. if data1 != nil && len(*data1) > 0 {
  1307. data := IC.BaseMysql.FindOne("socialize_tenant_seat", map[string]interface{}{"customer_service_id": quitl.IntAll((*data1)["customer_service_id"])}, "customer_service_name", "")
  1308. if data != nil && len(*data) > 0 {
  1309. name = fmt.Sprintf("%s(客服)", quitl.InterfaceToStr((*data)["customer_service_name"]))
  1310. }
  1311. }
  1312. case 2: //用户
  1313. position := IC.BaseMysql.SelectBySql(fmt.Sprintf(`SELECT a.ent_id,b.phone FROM base_position a INNER JOIN base_user b on a.id =%d AND a.user_id = b.id `, in.Id))
  1314. if position != nil && len(*position) > 0 {
  1315. phone := (*position)[0]["phone"]
  1316. entId := (*position)[0]["ent_id"]
  1317. nameMap := IC.MainMysql.SelectBySql(fmt.Sprintf(`SELECT a.phone,IF(a.name = "我" AND b.role_id = 1 ,"%s",a.name) AS name,b.role_id FROM %s a
  1318. LEFT JOIN entniche_user_role b on a.id = b.user_id where a.phone = %s and a.ent_id = %d `, util.UserRoleOne, util.ENTNICHE_USER, phone, entId))
  1319. if nameMap != nil && len(*nameMap) > 0 {
  1320. name = fmt.Sprintf("%s(用户)", (*nameMap)[0]["name"])
  1321. }
  1322. }
  1323. case 3: //群
  1324. data := IC.BaseMysql.FindOne(util.SOCIALIZE_CHAT_GROUP, map[string]interface{}{"id": in.Id}, "name", "")
  1325. if data != nil {
  1326. name = fmt.Sprintf("%s(群)", quitl.InterfaceToStr((*data)["name"]))
  1327. }
  1328. }
  1329. return name
  1330. }