tag.go 14 KB

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