tag.go 13 KB

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