everything.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. package main
  2. import (
  3. "app.yhyue.com/moapp/jybase/redis"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/gogf/gf/util/gconv"
  7. "log"
  8. "net/url"
  9. "strings"
  10. "time"
  11. "app.yhyue.com/moapp/jybase/mail"
  12. "github.com/tealeg/xlsx"
  13. "app.yhyue.com/moapp/jybase/date"
  14. dates "app.yhyue.com/moapp/jybase/date"
  15. "app.yhyue.com/moapp/jybase/mongodb"
  16. "app.yhyue.com/moapp/jybase/common"
  17. )
  18. func everythingSync() {
  19. lastEverythingTime := cfg.LastEverythingTime
  20. nowTime := time.Now().Format(dates.Date_Full_Layout)
  21. sql := fmt.Sprintf(`select * from user_source where channel_code = "JyChCoopA" and create_time > "%s" order by create_time asc`, lastEverythingTime)
  22. log.Println("三方渠道数据定时任务开始", sql)
  23. data := ThirdParty.SelectBySql(sql)
  24. if data != nil && *data != nil && len(*data) > 0 {
  25. for _, v := range *data {
  26. user_id := common.ObjToString(v["user_id"])
  27. position_id := common.Int64All(v["position_id"])
  28. phone := common.ObjToString(v["phone"])
  29. state := common.IntAll(v["state"])
  30. user_mold, is_assign, is_transfer, last_login_time, registe_time, mailbox, source, clueId, order_type := 4, 0, 0, "", "", "", "", int64(0), ""
  31. owner, sales_lead_phone, sales_ent_name, sales_position, sales_dep, data_request, unique_id, belongTo, sales_leads_source := "", "", "", "", "", "", "", "", ""
  32. cData := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"phone": phone}, "", "")
  33. if cData != nil {
  34. is_assign = common.IntAll((*cData)["is_assign"])
  35. is_transfer = common.IntAll((*cData)["is_transfer"])
  36. clueId = common.Int64All((*cData)["id"])
  37. }
  38. //
  39. udata := TiDb.FindOne("dwd_f_userbase_baseinfo", map[string]interface{}{"userid": user_id}, "", "")
  40. if udata != nil {
  41. userSource := common.ObjToString((*udata)["source"])
  42. if userSource == "0102" {
  43. continue
  44. }
  45. }
  46. //
  47. /*cuData := TiDbData.FindOne("customer", map[string]interface{}{"phone": phone}, "", "")
  48. if cuData != nil {
  49. owner = common.ObjToString((*cuData)["owner"])
  50. unique_id = common.ObjToString((*cuData)["unique_id"])
  51. belongTo = common.ObjToString((*cuData)["belongTo"])
  52. }*/
  53. vData := TiDb.FindOne("dwd_f_userbase_visit_info", map[string]interface{}{"userid": user_id}, "", "")
  54. if vData != nil {
  55. last_login_time = common.ObjToString((*vData)["date"])
  56. }
  57. dData := Mysql.Find("dataexport_order", map[string]interface{}{"user_id": user_id}, "", "", -1, -1)
  58. if dData != nil && len(*dData) > 0 {
  59. pMap := map[string]string{}
  60. pArr := []string{}
  61. for _, v := range *dData {
  62. product_type := common.ObjToString(v["product_type"])
  63. pMap[product_type] = "1"
  64. }
  65. for k := range pMap {
  66. pArr = append(pArr, k)
  67. }
  68. if len(pArr) > 0 {
  69. order_type = strings.Join(pArr, ",")
  70. }
  71. }
  72. uData, ok := Mgo.FindOne("user", map[string]interface{}{"_id": mongodb.StringTOBsonId(user_id)})
  73. if ok && uData != nil {
  74. l_registedate := common.Int64All((*uData)["l_registedate"])
  75. registe_time = time.Unix(l_registedate, 0).Format(dates.Date_Full_Layout)
  76. }
  77. sData, oks := Mgo.Find("saleLeads", map[string]interface{}{"userid": user_id}, `{"_id":-1}`, nil, false, 0, 1)
  78. if oks && sData != nil {
  79. if len(*sData) > 0 {
  80. sDatas := (*sData)[0]
  81. sales_lead_phone = common.ObjToString(sDatas["phone"])
  82. sales_position = common.ObjToString(sDatas["position"])
  83. sales_ent_name = common.ObjToString(sDatas["company"])
  84. sales_dep = common.ObjToString(sDatas["branch"])
  85. if sales_dep == "" {
  86. sales_dep = common.ObjToString(sDatas["department"])
  87. }
  88. data_request = common.ObjToString(sDatas["data_requirement"])
  89. mailbox = common.ObjToString(sDatas["mail"])
  90. source = common.ObjToString(sDatas["interest"])
  91. }
  92. }
  93. if state == 1 {
  94. sales_leads_source = "一切都好登录"
  95. if is_assign == 1 || is_transfer == 1 || owner != "" {
  96. user_mold = 1
  97. } else {
  98. user_mold = 2
  99. }
  100. } else if state == 2 {
  101. sales_leads_source = "一切都好注册"
  102. if is_assign == 1 || is_transfer == 1 || owner != "" {
  103. user_mold = 3
  104. } else {
  105. user_mold = 4
  106. }
  107. }
  108. if owner == "" && unique_id != "" && belongTo == "市场部" {
  109. token := getToken()
  110. if token != "" {
  111. sss := url.QueryEscape("合作渠道一切都好")
  112. urls := `https://a1.7x24cc.com/commonInte?flag=1008&account=N000000029739&accessToken=` + token + `&cusObj={"unique_id":"` + unique_id + `","empNo":"8049","owner":"8049","source":"` + sss + `"}&dbType=0001`
  113. bs, err := doGet(urls)
  114. if err != nil {
  115. log.Println("调用接口失败", unique_id, err)
  116. }
  117. resMap := common.StringToMap(string(bs))
  118. if resMap["success"] != nil && resMap["success"].(bool) {
  119. /*ok := TiDbData.Update("customer", map[string]interface{}{"unique_id": unique_id}, map[string]interface{}{"empNo": "8049", "owner": "8049", "source": "合作渠道一切都好"})
  120. if ok {
  121. log.Println("更新成功", unique_id)
  122. } else {
  123. log.Println("更新失败", unique_id)
  124. }*/
  125. } else {
  126. log.Println("调用接口失败!!", unique_id)
  127. }
  128. }
  129. }
  130. if ThirdParty.Count("user_channel_info", map[string]interface{}{"user_id": user_id}) > 0 {
  131. ThirdParty.Update("user_channel_info", map[string]interface{}{"user_id": user_id}, map[string]interface{}{
  132. "user_mold": user_mold,
  133. "last_login_time": common.If(last_login_time != "", last_login_time, nil),
  134. "sales_lead_phone": sales_lead_phone,
  135. "sales_ent_name": sales_ent_name,
  136. "sales_position": sales_position,
  137. "sales_dep": sales_dep,
  138. "sales_leads_source": sales_leads_source,
  139. "mailbox": mailbox,
  140. "order_type": order_type,
  141. "data_request": data_request,
  142. "source": source,
  143. "update_time": nowTime,
  144. })
  145. } else {
  146. ThirdParty.Insert("user_channel_info", map[string]interface{}{
  147. "user_id": user_id,
  148. "registe_time": registe_time,
  149. "phone": phone,
  150. "user_mold": user_mold,
  151. "last_login_time": common.If(last_login_time != "", last_login_time, nil),
  152. "sales_lead_phone": sales_lead_phone,
  153. "sales_ent_name": sales_ent_name,
  154. "sales_position": sales_position,
  155. "sales_dep": sales_dep,
  156. "sales_leads_source": sales_leads_source,
  157. "order_type": order_type,
  158. "mailbox": mailbox,
  159. "data_request": data_request,
  160. "source": source,
  161. "update_time": nowTime,
  162. })
  163. }
  164. if clueId > 0 && is_assign != 1 && is_transfer != 1 {
  165. TiDb.Update("dwd_f_userbase_baseinfo", map[string]interface{}{"userid": user_id}, map[string]interface{}{"belong_to": "0301"})
  166. TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"phone": phone}, map[string]interface{}{"is_assign": -1})
  167. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  168. "clue_id": clueId,
  169. "position_id": common.If(position_id > 0, position_id, -1),
  170. "change_type": "退出公海",
  171. "new_value": "通过合作渠道一切就好" + fmt.Sprint(common.If(user_mold == 1 || user_mold == 2, "登录", "注册")),
  172. "createtime": nowTime,
  173. "BCPCID": common.GetRandom(32),
  174. "operator_id": -1,
  175. })
  176. }
  177. }
  178. cfg.LastEverythingTime = common.ObjToString((*data)[len(*data)-1]["create_time"])
  179. }
  180. common.WriteSysConfig(&cfg)
  181. log.Println("三方渠道数据定时任务结束")
  182. }
  183. func saveEverything(user_id, phone, item, sourceName, sourceCode string) bool {
  184. nowTime, isOk := time.Now().Format(dates.Date_Full_Layout), false
  185. user_mold, is_assign, is_transfer, last_login_time, registe_time, mailbox, source, order_type, sales_leads_source := 4, 0, 0, "", "", "", "", "", ""
  186. owner, sales_lead_phone, sales_ent_name, sales_position, sales_dep, data_request, unique_id, belongTo, clueId, position_id := "", "", "", "", "", "", "", "", int64(0), int64(0)
  187. cData := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"phone": phone}, "", "")
  188. if cData != nil {
  189. is_assign = common.IntAll((*cData)["is_assign"])
  190. is_transfer = common.IntAll((*cData)["is_transfer"])
  191. clueId = common.Int64All((*cData)["is_transfer"])
  192. position_id = common.Int64All((*cData)["position_id"])
  193. }
  194. /*cuData := TiDbData.FindOne("customer", map[string]interface{}{"phone": phone}, "", "")
  195. if cuData != nil {
  196. unique_id = common.ObjToString((*cuData)["unique_id"])
  197. owner = common.ObjToString((*cuData)["owner"])
  198. }*/
  199. dData := Mysql.Find("dataexport_order", map[string]interface{}{"user_id": user_id}, "", "", -1, -1)
  200. if dData != nil && len(*dData) > 0 {
  201. pMap := map[string]string{}
  202. pArr := []string{}
  203. for _, v := range *dData {
  204. product_type := common.ObjToString(v["product_type"])
  205. pMap[product_type] = "1"
  206. }
  207. for k := range pMap {
  208. pArr = append(pArr, k)
  209. }
  210. if len(pArr) > 0 {
  211. order_type = strings.Join(pArr, ",")
  212. }
  213. }
  214. vData := TiDb.FindOne("dwd_f_userbase_visit_info", map[string]interface{}{"userid": user_id}, "", "")
  215. if vData != nil {
  216. last_login_time = common.ObjToString((*vData)["date"])
  217. }
  218. uData, ok := Mgo.FindOne("user", map[string]interface{}{"_id": mongodb.StringTOBsonId(user_id)})
  219. if ok && uData != nil {
  220. l_registedate := common.Int64All((*uData)["l_registedate"])
  221. registe_time = time.Unix(l_registedate, 0).Format(dates.Date_Full_Layout)
  222. }
  223. if item == "orders" {
  224. sales_leads_source = "订单未支付"
  225. } else if item == "users" || item == "xcxusers" {
  226. return false
  227. } else if item == "saleLeads" {
  228. sales_leads_source = sourceName
  229. } else {
  230. sales_leads_source = "用户留资"
  231. }
  232. query := map[string]interface{}{"userid": user_id}
  233. if sourceCode != "" {
  234. query["source"] = sourceCode
  235. }
  236. sData, oks := Mgo.Find("saleLeads", query, `{"_id":-1}`, nil, false, 0, 1)
  237. if oks && sData != nil {
  238. if len(*sData) > 0 {
  239. sDatas := (*sData)[0]
  240. sales_lead_phone = common.ObjToString(sDatas["phone"])
  241. sales_position = common.ObjToString(sDatas["position"])
  242. sales_ent_name = common.ObjToString(sDatas["company"])
  243. sales_dep = common.ObjToString(sDatas["branch"])
  244. if sales_dep == "" {
  245. sales_dep = common.ObjToString(sDatas["department"])
  246. }
  247. data_request = common.ObjToString(sDatas["data_requirement"])
  248. mailbox = common.ObjToString(sDatas["mail"])
  249. source = common.ObjToString(sDatas["interest"])
  250. }
  251. }
  252. if is_assign == 1 || is_transfer == 1 || owner != "" {
  253. user_mold = 1
  254. } else {
  255. user_mold = 2
  256. }
  257. if owner == "" && unique_id != "" && belongTo == "市场部" {
  258. token := getToken()
  259. if token != "" {
  260. sss := url.QueryEscape("合作渠道一切都好")
  261. urls := `https://a1.7x24cc.com/commonInte?flag=1008&account=N000000029739&accessToken=` + token + `&cusObj={"unique_id":"` + unique_id + `","empNo":"8049","owner":"8049","source":"` + sss + `"}&dbType=0001`
  262. bs, err := doGet(urls)
  263. if err != nil {
  264. log.Println("调用接口失败", unique_id, err)
  265. }
  266. resMap := common.StringToMap(string(bs))
  267. if resMap["success"] != nil && resMap["success"].(bool) {
  268. ok := TiDbData.Update("customer", map[string]interface{}{"unique_id": unique_id}, map[string]interface{}{"empNo": "8049", "owner": "8049", "source": "合作渠道一切都好"})
  269. if ok {
  270. log.Println("更新成功", unique_id)
  271. } else {
  272. log.Println("更新失败", unique_id)
  273. }
  274. } else {
  275. log.Println("调用接口失败!!", unique_id)
  276. }
  277. }
  278. }
  279. if ThirdParty.Count("user_channel_info", map[string]interface{}{"user_id": user_id}) > 0 {
  280. ThirdParty.Update("user_channel_info", map[string]interface{}{"user_id": user_id}, map[string]interface{}{
  281. "user_mold": user_mold,
  282. "last_login_time": common.If(last_login_time != "", last_login_time, nil),
  283. "sales_lead_phone": sales_lead_phone,
  284. "sales_ent_name": sales_ent_name,
  285. "sales_position": sales_position,
  286. "sales_dep": sales_dep,
  287. "sales_leads_source": sales_leads_source,
  288. "mailbox": mailbox,
  289. "data_request": data_request,
  290. "source": source,
  291. "order_type": order_type,
  292. "update_time": nowTime,
  293. })
  294. } else {
  295. ThirdParty.Insert("user_channel_info", map[string]interface{}{
  296. "user_id": user_id,
  297. "registe_time": registe_time,
  298. "phone": phone,
  299. "user_mold": user_mold,
  300. "last_login_time": common.If(last_login_time != "", last_login_time, nil),
  301. "sales_lead_phone": sales_lead_phone,
  302. "sales_ent_name": sales_ent_name,
  303. "sales_position": sales_position,
  304. "sales_dep": sales_dep,
  305. "sales_leads_source": sales_leads_source,
  306. "mailbox": mailbox,
  307. "data_request": data_request,
  308. "order_type": order_type,
  309. "source": source,
  310. "update_time": nowTime,
  311. })
  312. }
  313. if clueId > 0 && is_assign != 1 && is_transfer != 1 {
  314. TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"phone": phone}, map[string]interface{}{"is_assign": -1})
  315. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  316. "clue_id": clueId,
  317. "position_id": common.If(position_id > 0, position_id, -1),
  318. "change_type": "退出公海",
  319. "new_value": "通过合作渠道一切就好登录",
  320. "createtime": nowTime,
  321. "BCPCID": common.GetRandom(32),
  322. "operator_id": -1,
  323. })
  324. } else if is_assign == 1 || is_transfer == 1 {
  325. isOk = true
  326. }
  327. return isOk
  328. }
  329. // 大客户线索-发邮件
  330. func bigCustomer() {
  331. runOk := getRunOk()
  332. if !runOk {
  333. log.Println("不是工作日,任务暂停")
  334. return
  335. }
  336. log.Println("大客户线索定时任务开始")
  337. dataArr := []map[string]interface{}{}
  338. filterArr := []string{"-pc", "-app", "-wx", "-h5"}
  339. sourceMap := map[string]string{} //根据留资维表:tidb/Jianyu_subjectdb/d_saleleads_code
  340. saleSource := TiDb.SelectBySql(`SELECT source,name FROM d_saleleads_code WHERE department LIKE '%大客户%' AND is_delete = 1`)
  341. if saleSource != nil && len(*saleSource) > 0 {
  342. for _, v := range *saleSource {
  343. source := common.ObjToString(v["source"])
  344. name := common.ObjToString(v["name"])
  345. for _, s := range filterArr {
  346. name = strings.ReplaceAll(name, s, "")
  347. }
  348. sourceMap[source] = name
  349. }
  350. }
  351. nowTime := time.Now().Format(date.Date_Full_Layout)
  352. bigSaleTime := cfg.BigSaleTime
  353. bigOrderTime := cfg.BigOrderTime
  354. if bigSaleTime == 0 {
  355. bigSaleTime = time.Now().Unix()
  356. }
  357. if bigOrderTime == "" {
  358. bigOrderTime = nowTime
  359. }
  360. data := FindBatchData("dk")
  361. saleleadsData, ok := Mgo.Find("saleLeads", map[string]interface{}{"createtime": map[string]interface{}{"$gte": bigSaleTime}}, "createtime", nil, false, -1, -1)
  362. if ok && saleleadsData != nil && len(*saleleadsData) > 0 {
  363. for _, v := range *saleleadsData {
  364. sources := common.ObjToString(v["source"])
  365. userid := common.ObjToString(v["userid"])
  366. uid := ""
  367. if !mongodb.IsObjectIdHex(userid) {
  368. userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"position_id": userid}, "", "")
  369. if userMapping != nil && len(*userMapping) > 0 {
  370. userid = common.ObjToString((*userMapping)["userid"])
  371. uid = common.ObjToString((*userMapping)["uid"])
  372. }
  373. }
  374. if sourceMap[sources] != "" {
  375. source := sourceMap[sources]
  376. phone := common.ObjToString(v["phone"])
  377. key := fmt.Sprintf("%s_%s", source, phone)
  378. if _, ok := data[key]; ok {
  379. continue
  380. }
  381. data[key] = true
  382. company := common.ObjToString(v["company"])
  383. job := common.ObjToString(v["position"])
  384. username := common.ObjToString(v["name"])
  385. email := common.ObjToString(v["mail"])
  386. interest := common.ObjToString(v["interest"])
  387. data_requirement := common.ObjToString(v["data_requirement"])
  388. belongTo, usernickname := "大客户", ""
  389. userData := TiDb.FindOne("dwd_f_userbase_baseinfo", map[string]interface{}{"uid": uid}, "", "")
  390. if userData != nil {
  391. usernickname = common.ObjToString((*userData)["nickname"])
  392. bt := common.ObjToString((*userData)["belong_to"])
  393. if strings.HasPrefix(bt, "03") || bt == "0102" {
  394. continue
  395. }
  396. }
  397. dataArr = append(dataArr, map[string]interface{}{
  398. "createTime": nowTime,
  399. "lastUpdateTime": nowTime,
  400. "uid": uid,
  401. "userid": userid,
  402. "username": username,
  403. "usernickname": usernickname,
  404. "company": company,
  405. "job": job,
  406. "phone": phone,
  407. "email": email,
  408. "source": source,
  409. "belongTo": belongTo,
  410. "interest": interest,
  411. "data_requirement": data_requirement,
  412. })
  413. }
  414. bigSaleTime = gconv.Int64(v["createtime"])
  415. }
  416. }
  417. cfg.BigSaleTime = bigSaleTime
  418. orderData := Mysql.SelectBySql(`SELECT * FROM dataexport_order WHERE create_time > "` + bigOrderTime + `" AND (product_type = '历史数据' or product_type = '数据流量包') AND order_status IN (0,1) AND salesperson IS NULL AND is_backstage_order = 0`)
  419. if orderData != nil && len(*orderData) > 0 {
  420. for _, v := range *orderData {
  421. phone := common.ObjToString(v["user_phone"])
  422. order_status := common.IntAll(v["order_status"])
  423. product_type := common.ObjToString(v["product_type"])
  424. userData := TiDb.FindOne("dwd_f_userbase_baseinfo", map[string]interface{}{"phone": phone}, "", "")
  425. username, usernickname, userid, uid, source, payorderinfo, unpayorderinfo := "", "", "", "", "", "", ""
  426. if userData != nil {
  427. username = common.ObjToString((*userData)["name"])
  428. usernickname = common.ObjToString((*userData)["nickname"])
  429. uid = common.ObjToString((*userData)["uid"])
  430. userid = common.ObjToString((*userData)["userid"])
  431. bt := common.ObjToString((*userData)["belong_to"])
  432. if strings.HasPrefix(bt, "03") || bt == "0102" {
  433. continue
  434. }
  435. }
  436. if product_type == "历史数据" {
  437. if order_status == 0 {
  438. source = "线上历史数据导出未支付订单"
  439. unpayorderinfo = "历史数据"
  440. } else {
  441. source = "线上历史数据导出已支付订单"
  442. payorderinfo = "历史数据" + fmt.Sprint(common.IntAll(v["pay_money"])/100)
  443. }
  444. } else {
  445. if order_status == 0 {
  446. source = "线上数据流量包未支付订单"
  447. unpayorderinfo = "数据流量包"
  448. } else {
  449. source = "线上数据流量包已支付订单"
  450. payorderinfo = "数据流量包" + fmt.Sprint(common.IntAll(v["pay_money"])/100)
  451. }
  452. }
  453. key := fmt.Sprintf("%s_%s", source, phone)
  454. if _, ok := data[key]; ok {
  455. continue
  456. }
  457. data[key] = true
  458. dataArr = append(dataArr, map[string]interface{}{
  459. "createTime": nowTime,
  460. "lastUpdateTime": nowTime,
  461. "phone": phone,
  462. "username": username,
  463. "usernickname": usernickname,
  464. "uid": uid,
  465. "userid": userid,
  466. "company": v["company_name"],
  467. "email": v["user_mail"],
  468. "belongTo": "大客户",
  469. "source": source,
  470. "data_count": v["data_count"],
  471. "unpayorderinfo": unpayorderinfo,
  472. "payorderinfo": payorderinfo, //增加客户需求
  473. })
  474. bigOrderTime = gconv.Time(gconv.String(v["create_time"])).Format(date.Date_Full_Layout)
  475. }
  476. cfg.BigOrderTime = bigOrderTime
  477. }
  478. xlsxArr := []string{"用户昵称", "姓名", "公司名称", "职位", "联系人电话", "购买条数", "用户邮箱", "已支付订单信息", "未支付订单类型", "销售线索来源", "具体来源", "数据需求"}
  479. if len(dataArr) > 0 {
  480. keyContent := fmt.Sprintf("data_%s_%s", time.Now().Format("2006-01-02"), "dk")
  481. redis.Put("newother", keyContent, data, 86400)
  482. bigData := TiDb.SelectBySql("select * from dwd_f_crm_clue_big_autodraw_record")
  483. if bigData != nil && len(*bigData) > 0 {
  484. cdata := *bigData
  485. bigArr := map[string][]map[string]interface{}{}
  486. for _, v := range dataArr {
  487. mincount := common.IntAll(cdata[0]["count"])
  488. minindex := 0
  489. minemail := ""
  490. for kk, vv := range cdata {
  491. vcount := common.IntAll(vv["count"])
  492. vemail := common.ObjToString(vv["email"])
  493. if vcount <= mincount {
  494. minindex = kk
  495. mincount = vcount
  496. minemail = vemail
  497. }
  498. }
  499. bigArr[minemail] = append(bigArr[minemail], v)
  500. cdata[minindex]["count"] = mincount + 1
  501. name := common.ObjToString(cdata[minindex]["name"])
  502. TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_big_autodraw_record set count = count + 1 where name = ?`, name)
  503. }
  504. for k, vb := range bigArr {
  505. batch := FindBatch("dk")
  506. fileName, detailName := time.Now().Format(date.Date_Short_Layout)+"-"+batch+"大客户销售线索", ""
  507. xf := xlsx.NewFile()
  508. style := xlsx.NewStyle()
  509. style.Font.Size = 12
  510. style.Font.Bold = true
  511. style.Alignment.Vertical = "center"
  512. style.Alignment.Horizontal = "center"
  513. detailName = time.Now().Format(date.Date_Short_Layout) + "数据详情请查看附件"
  514. sh, _ := xf.AddSheet("线索数据")
  515. row1 := sh.AddRow()
  516. for _, x := range xlsxArr {
  517. cell := row1.AddCell()
  518. cell.SetString(x)
  519. cell.SetStyle(style)
  520. }
  521. for _, v := range vb {
  522. row := sh.AddRow()
  523. row.AddCell().SetString(common.ObjToString(v["usernickname"]))
  524. row.AddCell().SetString(common.ObjToString(v["username"]))
  525. row.AddCell().SetString(common.ObjToString(v["company"]))
  526. row.AddCell().SetString(common.ObjToString(v["job"]))
  527. row.AddCell().SetString(common.ObjToString(v["phone"]))
  528. row.AddCell().SetValue(common.IntAll(v["data_count"]))
  529. row.AddCell().SetString(common.ObjToString(v["email"]))
  530. row.AddCell().SetString(common.ObjToString(v["payorderinfo"]))
  531. row.AddCell().SetString(common.ObjToString(v["unpayorderinfo"]))
  532. row.AddCell().SetString(common.ObjToString(v["source"]))
  533. row.AddCell().SetString(common.ObjToString(v["interest"]))
  534. row.AddCell().SetString(common.ObjToString(v["data_requirement"]))
  535. //增加客户需求
  536. }
  537. email := k
  538. //email = "wanghao@jianyu360.com"
  539. dir := "./xlsx/" + fileName + ".xlsx"
  540. err := xf.Save(dir)
  541. if err != nil {
  542. log.Println("xls error", err, dir)
  543. } else {
  544. gmail := &mail.GmailAuth{
  545. SmtpHost: "smtp.exmail.qq.com",
  546. SmtpPort: 465,
  547. User: "public03@topnet.net.cn",
  548. Pwd: "ue9Rg9Sf4CVtdm5a",
  549. }
  550. status := mail.GSendMail_q("剑鱼标讯", email, "", "", fileName, detailName, dir, fileName+".xlsx", gmail)
  551. if status {
  552. log.Println("send mail success", fileName, email)
  553. }
  554. }
  555. }
  556. }
  557. }
  558. common.WriteSysConfig(&cfg)
  559. log.Println("大客户线索定时任务结束")
  560. }
  561. // 市场部线索
  562. func marketCustomer() {
  563. runOk := getRunOk()
  564. if !runOk {
  565. log.Println("不是工作日,任务暂停")
  566. return
  567. }
  568. log.Println("市场部线索定时任务开始")
  569. dataArr := []map[string]interface{}{}
  570. filterArr := []string{"-pc", "-app", "-wx", "-h5"}
  571. sourceMap := map[string]string{} //根据留资维表:tidb/Jianyu_subjectdb/d_saleleads_code
  572. saleSource := TiDb.SelectBySql(`SELECT source,name FROM d_saleleads_code WHERE department LIKE '%市场组%' AND is_delete = 1`)
  573. if saleSource != nil && len(*saleSource) > 0 {
  574. for _, v := range *saleSource {
  575. source := common.ObjToString(v["source"])
  576. name := common.ObjToString(v["name"])
  577. for _, s := range filterArr {
  578. name = strings.ReplaceAll(name, s, "")
  579. }
  580. sourceMap[source] = name
  581. }
  582. }
  583. nowTime := time.Now().Format(date.Date_Full_Layout)
  584. marketSaleTime := cfg.MarketSaleTime
  585. if marketSaleTime == 0 {
  586. marketSaleTime = time.Now().Unix()
  587. }
  588. saleleadsData, ok := Mgo.Find("saleLeads", map[string]interface{}{"createtime": map[string]interface{}{"$gte": marketSaleTime}}, "phone,createtime", nil, false, -1, -1)
  589. if ok && saleleadsData != nil && len(*saleleadsData) > 0 {
  590. data := FindBatchData("sc")
  591. for _, v := range *saleleadsData {
  592. sources := common.ObjToString(v["source"])
  593. userid := common.ObjToString(v["userid"])
  594. uid := ""
  595. if !mongodb.IsObjectIdHex(userid) {
  596. userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"position_id": userid}, "", "")
  597. if userMapping != nil && len(*userMapping) > 0 {
  598. userid = common.ObjToString((*userMapping)["userid"])
  599. uid = common.ObjToString((*userMapping)["uid"])
  600. }
  601. }
  602. if sourceMap[sources] != "" {
  603. company := common.ObjToString(v["company"])
  604. phone := common.ObjToString(v["phone"])
  605. job := common.ObjToString(v["position"])
  606. username := common.ObjToString(v["name"])
  607. email := common.ObjToString(v["mail"])
  608. interest := common.ObjToString(v["interest"])
  609. data_requirement := common.ObjToString(v["data_requirement"])
  610. source := sourceMap[sources]
  611. belongTo, usernickname := "市场组", ""
  612. userData := TiDb.FindOne("dwd_f_userbase_baseinfo", map[string]interface{}{"uid": uid}, "", "")
  613. if userData != nil {
  614. usernickname = common.ObjToString((*userData)["nickname"])
  615. bt := common.ObjToString((*userData)["belong_to"])
  616. if strings.HasPrefix(bt, "03") || bt == "0102" {
  617. continue
  618. }
  619. }
  620. key := fmt.Sprintf("%s_%s", source, phone)
  621. if _, ok := data[key]; ok {
  622. continue
  623. }
  624. data[key] = true
  625. dataArr = append(dataArr, map[string]interface{}{
  626. "createTime": nowTime,
  627. "lastUpdateTime": nowTime,
  628. "createtime": v["createtime"],
  629. "uid": uid,
  630. "userid": userid,
  631. "username": username,
  632. "usernickname": usernickname,
  633. "company": company,
  634. "job": job,
  635. "phone": phone,
  636. "email": email,
  637. "source": source,
  638. "belongTo": belongTo,
  639. "interest": interest,
  640. "data_requirement": data_requirement,
  641. "branch": v["branch"],
  642. })
  643. }
  644. marketSaleTime = gconv.Int64(v["createtime"])
  645. }
  646. keyContent := fmt.Sprintf("data_%s_%s", time.Now().Format("2006-01-02"), "sc")
  647. redis.Put("newother", keyContent, data, 86400)
  648. }
  649. cfg.MarketSaleTime = marketSaleTime
  650. xlsxArr := []string{"姓名", "联系人电话", "公司名称", "职位", "部门", "销售线索来源", "留资时间"}
  651. if len(dataArr) > 0 {
  652. //排序:首先按照“联系方式”排序,即同1个用户的留资放在一起,其次按照留资时间正序排序。
  653. batch := FindBatch("sc")
  654. fileName, detailName := "商务合作销售线索 "+time.Now().Format(date.Date_Short_Layout)+"-"+batch, ""
  655. xf := xlsx.NewFile()
  656. style := xlsx.NewStyle()
  657. style.Font.Size = 12
  658. style.Font.Bold = true
  659. style.Alignment.Vertical = "center"
  660. style.Alignment.Horizontal = "center"
  661. detailName = "今日新增商务合作销售线索,请查收附件,及时跟进。"
  662. sh, _ := xf.AddSheet("线索数据")
  663. row1 := sh.AddRow()
  664. for _, x := range xlsxArr {
  665. cell := row1.AddCell()
  666. cell.SetString(x)
  667. cell.SetStyle(style)
  668. }
  669. for _, v := range dataArr {
  670. row := sh.AddRow()
  671. row.AddCell().SetString(common.ObjToString(v["username"]))
  672. row.AddCell().SetString(common.ObjToString(v["phone"]))
  673. row.AddCell().SetString(common.ObjToString(v["company"]))
  674. row.AddCell().SetString(common.ObjToString(v["job"]))
  675. row.AddCell().SetString(common.ObjToString(v["branch"]))
  676. row.AddCell().SetString(common.ObjToString(v["source"]))
  677. row.AddCell().SetString(gconv.Time(gconv.Int64(v["createtime"])).Format(date.Date_Full_Layout))
  678. //增加客户需求
  679. }
  680. email := db.MarketSaleMail
  681. dir := "./xlsx/" + fileName + ".xlsx"
  682. err := xf.Save(dir)
  683. if err != nil {
  684. log.Println("xls error", err, dir)
  685. } else {
  686. gmail := &mail.GmailAuth{
  687. SmtpHost: "smtp.exmail.qq.com",
  688. SmtpPort: 465,
  689. User: "public03@topnet.net.cn",
  690. Pwd: "ue9Rg9Sf4CVtdm5a",
  691. }
  692. status := mail.GSendMail_q("剑鱼标讯", email, "", "", fileName, detailName, dir, fileName+".xlsx", gmail)
  693. if status {
  694. log.Println("send mail success", fileName, email)
  695. }
  696. }
  697. }
  698. common.WriteSysConfig(&cfg)
  699. log.Println("大客户线索定时任务结束")
  700. }
  701. func eventReg() {
  702. lastEventRegTime := cfg.LastEventRegTime
  703. sql := fmt.Sprintf(`select * from exhibition_sign_up where is_del = 0 and update_time > "%s" order by update_time asc`, lastEventRegTime)
  704. log.Println("活动报名表定时任务开始", sql)
  705. data := Jyactivities.SelectBySql(sql)
  706. if data != nil && *data != nil && len(*data) > 0 {
  707. for _, v := range *data {
  708. ok1, ok2 := FormatData(v, "eventReg")
  709. if !ok1 {
  710. common.WriteSysConfig(&cfg)
  711. log.Println("线索卡点", "eventReg", v, lastEventRegTime)
  712. break
  713. } else {
  714. if !ok2 {
  715. log.Println("用户分配已达上限", "eventReg", v, lastEventRegTime)
  716. common.WriteSysConfig(&cfg)
  717. break
  718. }
  719. }
  720. cfg.LastEventRegTime = common.ObjToString(v["update_time"])
  721. }
  722. }
  723. common.WriteSysConfig(&cfg)
  724. log.Println("活动报名表定时任务结束")
  725. }
  726. func saveHlyj(belong_to, item, phone, name, sourceName, cluename, position, nowTime string, isGroup, isCommerce int) {
  727. if strings.HasPrefix(belong_to, "02") && item == "eventReg" {
  728. saveMap := map[string]interface{}{
  729. "unique_id": phone,
  730. "phone": phone,
  731. "username": name,
  732. "source": sourceName,
  733. "status999": "status5",
  734. "company": cluename,
  735. "job": position,
  736. "belongTo": "市场部",
  737. "createTime": nowTime,
  738. "lastUpdateTime": nowTime,
  739. }
  740. token := getToken()
  741. updateData := map[string]interface{}{
  742. "dbType": "0001",
  743. "customerList": []map[string]interface{}{saveMap},
  744. }
  745. dataByte, _ := json.Marshal(&updateData)
  746. url := `https://a1.7x24cc.com/commonInte?flag=1007&account=N000000029739&accessToken=` + token + `&json=` + url.QueryEscape(string(dataByte))
  747. bs, err := doGet(url)
  748. if err != nil {
  749. log.Println("调用接口失败")
  750. } else {
  751. resMap := common.StringToMap(string(bs))
  752. if resMap["success"] != nil && resMap["success"].(bool) {
  753. saveMap["company_nature"] = isGroup
  754. saveMap["company_verification"] = isCommerce
  755. TiDbData.Insert("customer", saveMap)
  756. } else {
  757. log.Println("新增线索失败")
  758. }
  759. }
  760. }
  761. }
  762. func FindBatch(moudle string) string {
  763. now := time.Now().Format("2006-01-02")
  764. key := fmt.Sprintf("batch_%s_%s", now, moudle)
  765. yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  766. yesterdayKey := fmt.Sprintf("batch_%s_%s", yesterday, moudle)
  767. if ok, _ := redis.Exists("newother", yesterdayKey); ok {
  768. //删除之前数据
  769. redis.Del("newother", yesterdayKey)
  770. }
  771. batch := redis.Incr("newother", key)
  772. return fmt.Sprintf("%04d", batch)
  773. }
  774. func FindBatchData(moudle string) map[string]interface{} {
  775. now := time.Now().Format("2006-01-02")
  776. //数据提取
  777. keyContent := fmt.Sprintf("data_%s_%s", now, moudle)
  778. data := redis.Get("newother", keyContent)
  779. if data == nil {
  780. return make(map[string]interface{})
  781. } else {
  782. return gconv.Map(data)
  783. }
  784. }