tag.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "regexp"
  6. "strings"
  7. "time"
  8. "app.yhyue.com/moapp/jybase/common"
  9. )
  10. func tagAllSync() {
  11. //dwd_f_userbase_baseinfo -->l_registedate 注册日期 -->userid
  12. //dwd_f_userbase_visit_info -->date 访问时间 -->userid
  13. //dwd_f_userbase_search_info -->search_area去重 jianyu_subjectdb_test.d_area_code -->userid
  14. //dwd_f_userbase_search_info -->search_word去重 -->userid
  15. //dwd_f_userbase_subscribe_info -->subscribe_areas去重 jianyu_subjectdb_test.d_area_code -->userid
  16. //dwd_f_userbase_subscribe_info -->subscribe_keywords去重 -->userid
  17. //dwd_f_userbase_order_info -->product_type去重 -->userid
  18. //dwd_f_userbase_order_info -->product_type=VIP订阅 vip_endtime 一周内到期、一月内到期 -->userid 暂时不要了
  19. //dwd_f_userbase_event_info -->createtime查昨天有没有数据, 有数据-昨日浏览过 没数据-昨日未浏览 -->userid
  20. log.Println("用户标签定时任务开始")
  21. // TiDb.ExecBySql(`SET session max_execution_time=86400`)
  22. count := 0
  23. TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
  24. for _, v := range *l {
  25. count++
  26. FormatTag(v, count)
  27. }
  28. return true
  29. }, `select l_registedate,userid,uid,base_user_id from dwd_f_userbase_baseinfo order by updatetime desc`)
  30. log.Println("用户标签定时任务结束")
  31. }
  32. func FormatTag(data map[string]interface{}, count int) {
  33. registedate := common.ObjToString(data["l_registedate"])
  34. userId := common.ObjToString(data["userid"])
  35. uId := common.ObjToString(data["uid"])
  36. base_user_id := common.Int64All(data["base_user_id"])
  37. log.Println("第", count, "条:", uId, registedate, userId)
  38. if registedate == "" || uId == "" || userId == "" {
  39. log.Println("缺少信息 ", registedate, uId, userId)
  40. return
  41. }
  42. date, search_areass, search_wordss, product_types, subscribe_keywords, subscribe_areas := "", "", "", "", "", ""
  43. //访问数据
  44. visitData := TiDb.FindOne("dwd_f_userbase_visit_info", map[string]interface{}{"userid": userId}, "", "date desc")
  45. if visitData != nil && len(*visitData) > 0 {
  46. date = common.ObjToString((*visitData)["date"])
  47. }
  48. //搜索数据
  49. searchData := TiDb.Find("dwd_f_userbase_search_info", map[string]interface{}{"userid": userId}, "", "", -1, -1)
  50. if searchData != nil && len(*searchData) > 0 {
  51. search_areas, search_words := "", ""
  52. for k, v := range *searchData {
  53. search_area := common.ObjToString(v["search_area"])
  54. search_word := common.ObjToString(v["search_word"])
  55. if k == len(*searchData)-1 && search_area != "" {
  56. search_areas += search_area
  57. } else if search_area != "" {
  58. search_areas += search_area + ","
  59. }
  60. if k == len(*searchData)-1 && search_word != "" {
  61. search_words += search_word
  62. } else if search_word != "" {
  63. search_words += search_word + ","
  64. }
  65. }
  66. search_areas_arr, search_words_arr := []string{}, []string{}
  67. for _, v := range strings.Split(search_areas, ",") {
  68. isOk := false
  69. for _, vv := range search_areas_arr {
  70. if vv == v {
  71. isOk = true
  72. }
  73. }
  74. if !isOk && v != "" {
  75. search_areas_arr = append(search_areas_arr, v)
  76. }
  77. }
  78. for _, v := range strings.Split(search_words, ",") {
  79. isOk := false
  80. for _, vv := range search_words_arr {
  81. if vv == v {
  82. isOk = true
  83. }
  84. }
  85. if !isOk && v != "" {
  86. search_words_arr = append(search_words_arr, v)
  87. }
  88. }
  89. search_areass = strings.Join(search_areas_arr, ",")
  90. search_wordss = strings.Join(search_words_arr, ",")
  91. }
  92. //订单购买的服务
  93. orderData := TiDb.Find("dwd_f_userbase_order_info", map[string]interface{}{"userid": userId, "order_status": 1, "delete_status": 0}, "", "", -1, -1)
  94. if orderData != nil && len(*orderData) > 0 {
  95. product_type_arr, product_type_arrs := []string{}, []string{}
  96. for _, v := range *orderData {
  97. product_type := common.ObjToString(v["product_type"])
  98. if product_type != "" {
  99. product_type_arr = append(product_type_arr, product_type)
  100. }
  101. }
  102. for _, v := range product_type_arr {
  103. isOk := false
  104. for _, vv := range product_type_arrs {
  105. if vv == v {
  106. isOk = true
  107. }
  108. }
  109. if !isOk {
  110. product_type_arrs = append(product_type_arrs, v)
  111. }
  112. }
  113. product_types = strings.Join(product_type_arrs, ",")
  114. }
  115. //订阅数据
  116. subscribeData := TiDb.FindOne("dwd_f_userbase_subscribe_info", map[string]interface{}{"userid": userId}, "", "updatetime desc")
  117. if subscribeData != nil && len(*subscribeData) > 0 {
  118. subscribe_keywords = common.ObjToString((*subscribeData)["subscribe_keywords"])
  119. subscribe_areas = common.ObjToString((*subscribeData)["subscribe_areas"])
  120. }
  121. //会员介绍页面
  122. start := time.Now().AddDate(0, 0, -1).Format("2006-01-02") + " 00:00:00"
  123. end := time.Now().Format("2006-01-02") + " 00:00:00"
  124. eventStr := "昨日未浏览"
  125. eventCount := TiDb.SelectBySql(`select * from dwd_f_userbase_event_info where userid = "` + userId + `" and eventtype = "会员介绍页面" and createtime >= "` + start + `" and createtime <= "` + end + `"`)
  126. if eventCount != nil && len(*eventCount) > 0 {
  127. eventStr = "昨日浏览过"
  128. }
  129. nowTime := time.Now().Format("2006-01-02 15:04:05")
  130. //聊天记录标签
  131. keyStr := ""
  132. phoneRegexp := regexp.MustCompile(`^1[0-9]{10}$`)
  133. keyMap := map[string]bool{}
  134. match := "会员,费,数据,价格,合作,大会员,超级订阅,功能,账号,如何,订阅,充值,试用,报价,买,意向,超前项目,附件,拟招标,个人办,vip,套餐,权益"
  135. sqls := fmt.Sprintf(`select b.content from socialize_message_mailbox a LEFT JOIN socialize_message b on b.id = a.messag_id where a.type in (4,5,7) and a.own_type = 2 and a.own_id = %d and b.title = "文本" and a.send_user_type = 2 and a.create_time >= "%s"`, base_user_id, time.Now().AddDate(0, 0, -30).Format("2006-01-02 15:04:05"))
  136. mData := Base.SelectBySql(sqls)
  137. if mData != nil && *mData != nil && len(*mData) > 0 {
  138. for _, vv := range *mData {
  139. content := common.ObjToString(vv["content"])
  140. if phoneRegexp.MatchString(content) {
  141. keyMap[content] = true
  142. }
  143. for _, vvv := range strings.Split(match, ",") {
  144. if strings.Contains(strings.ToUpper(content), strings.ToUpper(vvv)) {
  145. keyMap[vvv] = true
  146. break
  147. }
  148. }
  149. }
  150. }
  151. if len(keyMap) > 0 {
  152. keyArr := []string{}
  153. for k, _ := range keyMap {
  154. keyArr = append(keyArr, k)
  155. }
  156. keyStr = strings.Join(keyArr, ",")
  157. }
  158. if TiDb.Count("dwd_f_crm_attribute_label", map[string]interface{}{"uid": uId}) > 0 {
  159. TiDb.Update("dwd_f_crm_attribute_label", map[string]interface{}{"uid": uId}, map[string]interface{}{
  160. "last_login_time": common.If(date != "", date, nil),
  161. "search_areas": search_areass,
  162. "search_words": search_wordss,
  163. "subscribe_areas": subscribe_areas,
  164. "subscribe_keywords": subscribe_keywords,
  165. "product_type": product_types,
  166. "updatetime": nowTime,
  167. "members_info": eventStr,
  168. "messagekey": common.If(keyStr == "", nil, keyStr),
  169. })
  170. } else {
  171. TiDb.Insert("dwd_f_crm_attribute_label", map[string]interface{}{
  172. "uid": uId,
  173. "registedate": registedate,
  174. "last_login_time": common.If(date != "", date, nil),
  175. "search_areas": search_areass,
  176. "search_words": search_wordss,
  177. "subscribe_areas": subscribe_areas,
  178. "subscribe_keywords": subscribe_keywords,
  179. "product_type": product_types,
  180. "updatetime": nowTime,
  181. "members_info": eventStr,
  182. "messagekey": common.If(keyStr == "", nil, keyStr),
  183. })
  184. }
  185. }
  186. func tagAddSync() {
  187. log.Println("注册日期、订单增量定时任务开始")
  188. userData := TiDb.SelectBySql(`select l_registedate,createtime,uid,userid from dwd_f_userbase_baseinfo where createtime >= "` + cfg.LastUserTime + `" order by createtime asc`)
  189. if userData != nil && len(*userData) > 0 {
  190. for k, v := range *userData {
  191. nowTime := time.Now().Format("2006-01-02 15:04:05")
  192. registedate := common.ObjToString(v["l_registedate"])
  193. createtime := common.ObjToString(v["createtime"])
  194. userId := common.ObjToString(v["userid"])
  195. uId := common.ObjToString(v["uid"])
  196. if registedate == "" || uId == "" || userId == "" {
  197. log.Println("缺少信息", uId, userId)
  198. continue
  199. } else {
  200. log.Println("新注册", uId, userId)
  201. }
  202. if k == len(*userData)-1 {
  203. cfg.LastUserTime = createtime
  204. }
  205. if TiDb.Count("dwd_f_crm_attribute_label", map[string]interface{}{"uid": uId}) > 0 {
  206. TiDb.Update("dwd_f_crm_attribute_label", map[string]interface{}{"uid": uId}, map[string]interface{}{
  207. "registedate": registedate,
  208. "updatetime": nowTime,
  209. })
  210. } else {
  211. TiDb.Insert("dwd_f_crm_attribute_label", map[string]interface{}{
  212. "uid": uId,
  213. "registedate": registedate,
  214. "updatetime": nowTime,
  215. })
  216. }
  217. }
  218. }
  219. log.Println("注册日期定时任务结束")
  220. orderData := TiDb.SelectBySql(`select product_type,autoUpdate,uid from dwd_f_userbase_order_info where order_status = 1 and autoUpdate >= "` + cfg.LastOrderTime + `" order by autoUpdate desc`)
  221. if orderData != nil && len(*orderData) > 0 {
  222. for k, order := range *orderData {
  223. nowTime := time.Now().Format("2006-01-02 15:04:05")
  224. autoUpdate := common.ObjToString(order["autoUpdate"])
  225. uId := common.ObjToString(order["uid"])
  226. if uId == "" {
  227. log.Println("缺少信息")
  228. continue
  229. }
  230. product_types := ""
  231. if k == 0 {
  232. cfg.LastOrderTime = autoUpdate
  233. }
  234. orderDatas := TiDb.Find("dwd_f_userbase_order_info", map[string]interface{}{"uid": uId, "order_status": 1, "delete_status": 0}, "", "", -1, -1)
  235. if orderDatas != nil && len(*orderDatas) > 0 {
  236. product_type_arr, product_type_arrs := []string{}, []string{}
  237. for _, v := range *orderDatas {
  238. product_type := common.ObjToString(v["product_type"])
  239. log.Println("product_type ", product_type)
  240. if product_type != "" {
  241. product_type_arr = append(product_type_arr, product_type)
  242. }
  243. }
  244. log.Println("product_type_arr ", product_type_arr)
  245. for _, v := range product_type_arr {
  246. isOk := false
  247. for _, vv := range product_type_arrs {
  248. if vv == v {
  249. isOk = true
  250. }
  251. }
  252. if !isOk {
  253. product_type_arrs = append(product_type_arrs, v)
  254. }
  255. }
  256. product_types = strings.Join(product_type_arrs, ",")
  257. }
  258. log.Println("product_types ", uId, product_types)
  259. if TiDb.Count("dwd_f_crm_attribute_label", map[string]interface{}{"uid": uId}) > 0 {
  260. TiDb.Update("dwd_f_crm_attribute_label", map[string]interface{}{"uid": uId}, map[string]interface{}{
  261. "product_type": product_types,
  262. "updatetime": nowTime,
  263. })
  264. } else {
  265. TiDb.Insert("dwd_f_crm_attribute_label", map[string]interface{}{
  266. "uid": uId,
  267. "product_type": product_types,
  268. "updatetime": nowTime,
  269. })
  270. }
  271. }
  272. }
  273. common.WriteSysConfig(&cfg)
  274. log.Println("注册日期、订单增量定时任务结束")
  275. }
  276. // 在线客服聊天记录30分钟一次
  277. func messageSync() {
  278. log.Println("在线客服聊天记录定时任务开始")
  279. phoneRegexp := regexp.MustCompile(`^1[0-9]{10}$`)
  280. sql := fmt.Sprintf(`select * from socialize_message where title = "文本" and create_time > "%s"`, cfg.LastMessageTime)
  281. data := Base.SelectBySql(sql)
  282. if data != nil && *data != nil && len(*data) > 0 {
  283. for _, v := range *data {
  284. isOk := false
  285. content := common.ObjToString(v["content"])
  286. match := "会员,费,数据,价格,合作,大会员,超级订阅,功能,账号,如何,订阅,充值,试用,报价,买,意向,超前项目,附件,拟招标,个人办,vip,套餐,权益"
  287. for _, vv := range strings.Split(match, ",") {
  288. if strings.Contains(strings.ToUpper(content), strings.ToUpper(vv)) || phoneRegexp.MatchString(content) {
  289. isOk = true
  290. }
  291. }
  292. if isOk {
  293. messag_id := common.Int64All(v["id"])
  294. mData := Base.FindOne("socialize_message_mailbox", map[string]interface{}{"messag_id": messag_id, "own_type": 2}, "", "")
  295. if mData != nil && len(*mData) > 0 {
  296. own_id := common.Int64All((*mData)["own_id"])
  297. if own_id > 0 {
  298. ok1, _ := FormatData(*mData, "message")
  299. if !ok1 {
  300. break
  301. }
  302. }
  303. }
  304. }
  305. cfg.LastMessageTime = common.ObjToString(v["create_time"])
  306. }
  307. }
  308. common.WriteSysConfig(&cfg)
  309. log.Println("在线客服聊天记录定时任务结束")
  310. }