everything.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. orderUserId := gconv.String(v["user_id"])
  425. query := map[string]interface{}{}
  426. if !mongodb.IsObjectIdHex(orderUserId) {
  427. userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"position_id": orderUserId}, "", "")
  428. if userMapping != nil && len(*userMapping) > 0 {
  429. uid := common.ObjToString((*userMapping)["uid"])
  430. query["uid"] = uid
  431. }
  432. } else {
  433. query["userid"] = orderUserId
  434. }
  435. userData := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  436. username, usernickname, userid, uid, source, payorderinfo, unpayorderinfo := "", "", "", "", "", "", ""
  437. if userData != nil {
  438. username = common.ObjToString((*userData)["name"])
  439. usernickname = common.ObjToString((*userData)["nickname"])
  440. uid = common.ObjToString((*userData)["uid"])
  441. userid = common.ObjToString((*userData)["userid"])
  442. bt := common.ObjToString((*userData)["belong_to"])
  443. if strings.HasPrefix(bt, "03") || bt == "0102" {
  444. continue
  445. }
  446. }
  447. if product_type == "历史数据" {
  448. if order_status == 0 {
  449. source = "线上历史数据导出未支付订单"
  450. unpayorderinfo = "历史数据"
  451. } else {
  452. source = "线上历史数据导出已支付订单"
  453. payorderinfo = "历史数据" + fmt.Sprint(common.IntAll(v["pay_money"])/100)
  454. }
  455. } else {
  456. if order_status == 0 {
  457. source = "线上数据流量包未支付订单"
  458. unpayorderinfo = "数据流量包"
  459. } else {
  460. source = "线上数据流量包已支付订单"
  461. payorderinfo = "数据流量包" + fmt.Sprint(common.IntAll(v["pay_money"])/100)
  462. }
  463. }
  464. key := fmt.Sprintf("%s_%s", source, phone)
  465. if _, ok := data[key]; ok {
  466. continue
  467. }
  468. data[key] = true
  469. dataArr = append(dataArr, map[string]interface{}{
  470. "createTime": nowTime,
  471. "lastUpdateTime": nowTime,
  472. "phone": phone,
  473. "username": username,
  474. "usernickname": usernickname,
  475. "uid": uid,
  476. "userid": userid,
  477. "company": v["company_name"],
  478. "email": v["user_mail"],
  479. "belongTo": "大客户",
  480. "source": source,
  481. "data_count": v["data_count"],
  482. "unpayorderinfo": unpayorderinfo,
  483. "payorderinfo": payorderinfo, //增加客户需求
  484. })
  485. bigOrderTime = gconv.Time(gconv.String(v["create_time"])).Format(date.Date_Full_Layout)
  486. }
  487. cfg.BigOrderTime = bigOrderTime
  488. }
  489. xlsxArr := []string{"用户昵称", "姓名", "公司名称", "职位", "联系人电话", "购买条数", "用户邮箱", "已支付订单信息", "未支付订单类型", "销售线索来源", "具体来源", "数据需求"}
  490. if len(dataArr) > 0 {
  491. keyContent := fmt.Sprintf("data_%s_%s", time.Now().Format("2006-01-02"), "dk")
  492. redis.Put("newother", keyContent, data, 86400)
  493. bigData := TiDb.SelectBySql("select * from dwd_f_crm_clue_big_autodraw_record")
  494. if bigData != nil && len(*bigData) > 0 {
  495. cdata := *bigData
  496. bigArr := map[string][]map[string]interface{}{}
  497. for _, v := range dataArr {
  498. mincount := common.IntAll(cdata[0]["count"])
  499. minindex := 0
  500. minemail := ""
  501. for kk, vv := range cdata {
  502. vcount := common.IntAll(vv["count"])
  503. vemail := common.ObjToString(vv["email"])
  504. if vcount <= mincount {
  505. minindex = kk
  506. mincount = vcount
  507. minemail = vemail
  508. }
  509. }
  510. bigArr[minemail] = append(bigArr[minemail], v)
  511. cdata[minindex]["count"] = mincount + 1
  512. name := common.ObjToString(cdata[minindex]["name"])
  513. TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_big_autodraw_record set count = count + 1 where name = ?`, name)
  514. }
  515. for k, vb := range bigArr {
  516. batch := FindBatch("dk")
  517. fileName, detailName := time.Now().Format(date.Date_Short_Layout)+"-"+batch+"大客户销售线索", ""
  518. xf := xlsx.NewFile()
  519. style := xlsx.NewStyle()
  520. style.Font.Size = 12
  521. style.Font.Bold = true
  522. style.Alignment.Vertical = "center"
  523. style.Alignment.Horizontal = "center"
  524. detailName = time.Now().Format(date.Date_Short_Layout) + "数据详情请查看附件"
  525. sh, _ := xf.AddSheet("线索数据")
  526. row1 := sh.AddRow()
  527. for _, x := range xlsxArr {
  528. cell := row1.AddCell()
  529. cell.SetString(x)
  530. cell.SetStyle(style)
  531. }
  532. for _, v := range vb {
  533. row := sh.AddRow()
  534. row.AddCell().SetString(common.ObjToString(v["usernickname"]))
  535. row.AddCell().SetString(common.ObjToString(v["username"]))
  536. row.AddCell().SetString(common.ObjToString(v["company"]))
  537. row.AddCell().SetString(common.ObjToString(v["job"]))
  538. row.AddCell().SetString(common.ObjToString(v["phone"]))
  539. row.AddCell().SetValue(common.IntAll(v["data_count"]))
  540. row.AddCell().SetString(common.ObjToString(v["email"]))
  541. row.AddCell().SetString(common.ObjToString(v["payorderinfo"]))
  542. row.AddCell().SetString(common.ObjToString(v["unpayorderinfo"]))
  543. row.AddCell().SetString(common.ObjToString(v["source"]))
  544. row.AddCell().SetString(common.ObjToString(v["interest"]))
  545. row.AddCell().SetString(common.ObjToString(v["data_requirement"]))
  546. //增加客户需求
  547. }
  548. email := k
  549. //email = "wanghao@jianyu360.com"
  550. dir := "./xlsx/" + fileName + ".xlsx"
  551. err := xf.Save(dir)
  552. if err != nil {
  553. log.Println("xls error", err, dir)
  554. } else {
  555. gmail := &mail.GmailAuth{
  556. SmtpHost: "smtp.exmail.qq.com",
  557. SmtpPort: 465,
  558. User: "public03@topnet.net.cn",
  559. Pwd: "ue9Rg9Sf4CVtdm5a",
  560. }
  561. status := mail.GSendMail_q("剑鱼标讯", email, "", "", fileName, detailName, dir, fileName+".xlsx", gmail)
  562. if status {
  563. log.Println("send mail success", fileName, email)
  564. }
  565. }
  566. }
  567. }
  568. }
  569. common.WriteSysConfig(&cfg)
  570. log.Println("大客户线索定时任务结束")
  571. }
  572. // 市场部线索
  573. func marketCustomer() {
  574. runOk := getRunOk()
  575. if !runOk {
  576. log.Println("不是工作日,任务暂停")
  577. return
  578. }
  579. log.Println("市场部线索定时任务开始")
  580. dataArr := []map[string]interface{}{}
  581. filterArr := []string{"-pc", "-app", "-wx", "-h5"}
  582. sourceMap := map[string]string{} //根据留资维表:tidb/Jianyu_subjectdb/d_saleleads_code
  583. saleSource := TiDb.SelectBySql(`SELECT source,name FROM d_saleleads_code WHERE department LIKE '%市场组%' AND is_delete = 1`)
  584. if saleSource != nil && len(*saleSource) > 0 {
  585. for _, v := range *saleSource {
  586. source := common.ObjToString(v["source"])
  587. name := common.ObjToString(v["name"])
  588. for _, s := range filterArr {
  589. name = strings.ReplaceAll(name, s, "")
  590. }
  591. sourceMap[source] = name
  592. }
  593. }
  594. nowTime := time.Now().Format(date.Date_Full_Layout)
  595. marketSaleTime := cfg.MarketSaleTime
  596. if marketSaleTime == 0 {
  597. marketSaleTime = time.Now().Unix()
  598. }
  599. saleleadsData, ok := Mgo.Find("saleLeads", map[string]interface{}{"createtime": map[string]interface{}{"$gte": marketSaleTime}}, "phone,createtime", nil, false, -1, -1)
  600. if ok && saleleadsData != nil && len(*saleleadsData) > 0 {
  601. data := FindBatchData("sc")
  602. for _, v := range *saleleadsData {
  603. sources := common.ObjToString(v["source"])
  604. userid := common.ObjToString(v["userid"])
  605. uid := ""
  606. if !mongodb.IsObjectIdHex(userid) {
  607. userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"position_id": userid}, "", "")
  608. if userMapping != nil && len(*userMapping) > 0 {
  609. userid = common.ObjToString((*userMapping)["userid"])
  610. uid = common.ObjToString((*userMapping)["uid"])
  611. }
  612. }
  613. if sourceMap[sources] != "" {
  614. company := common.ObjToString(v["company"])
  615. phone := common.ObjToString(v["phone"])
  616. job := common.ObjToString(v["position"])
  617. username := common.ObjToString(v["name"])
  618. email := common.ObjToString(v["mail"])
  619. interest := common.ObjToString(v["interest"])
  620. data_requirement := common.ObjToString(v["data_requirement"])
  621. source := sourceMap[sources]
  622. belongTo, usernickname := "市场组", ""
  623. userData := TiDb.FindOne("dwd_f_userbase_baseinfo", map[string]interface{}{"uid": uid}, "", "")
  624. if userData != nil {
  625. usernickname = common.ObjToString((*userData)["nickname"])
  626. bt := common.ObjToString((*userData)["belong_to"])
  627. if strings.HasPrefix(bt, "03") || bt == "0102" {
  628. continue
  629. }
  630. }
  631. key := fmt.Sprintf("%s_%s", source, phone)
  632. if _, ok := data[key]; ok {
  633. continue
  634. }
  635. data[key] = true
  636. dataArr = append(dataArr, map[string]interface{}{
  637. "createTime": nowTime,
  638. "lastUpdateTime": nowTime,
  639. "createtime": v["createtime"],
  640. "uid": uid,
  641. "userid": userid,
  642. "username": username,
  643. "usernickname": usernickname,
  644. "company": company,
  645. "job": job,
  646. "phone": phone,
  647. "email": email,
  648. "source": source,
  649. "belongTo": belongTo,
  650. "interest": interest,
  651. "data_requirement": data_requirement,
  652. "branch": v["branch"],
  653. })
  654. }
  655. marketSaleTime = gconv.Int64(v["createtime"])
  656. }
  657. keyContent := fmt.Sprintf("data_%s_%s", time.Now().Format("2006-01-02"), "sc")
  658. redis.Put("newother", keyContent, data, 86400)
  659. }
  660. cfg.MarketSaleTime = marketSaleTime
  661. xlsxArr := []string{"姓名", "联系人电话", "公司名称", "职位", "部门", "销售线索来源", "留资时间"}
  662. if len(dataArr) > 0 {
  663. //排序:首先按照“联系方式”排序,即同1个用户的留资放在一起,其次按照留资时间正序排序。
  664. batch := FindBatch("sc")
  665. fileName, detailName := "商务合作销售线索 "+time.Now().Format(date.Date_Short_Layout)+"-"+batch, ""
  666. xf := xlsx.NewFile()
  667. style := xlsx.NewStyle()
  668. style.Font.Size = 12
  669. style.Font.Bold = true
  670. style.Alignment.Vertical = "center"
  671. style.Alignment.Horizontal = "center"
  672. detailName = "今日新增商务合作销售线索,请查收附件,及时跟进。"
  673. sh, _ := xf.AddSheet("线索数据")
  674. row1 := sh.AddRow()
  675. for _, x := range xlsxArr {
  676. cell := row1.AddCell()
  677. cell.SetString(x)
  678. cell.SetStyle(style)
  679. }
  680. for _, v := range dataArr {
  681. row := sh.AddRow()
  682. row.AddCell().SetString(common.ObjToString(v["username"]))
  683. row.AddCell().SetString(common.ObjToString(v["phone"]))
  684. row.AddCell().SetString(common.ObjToString(v["company"]))
  685. row.AddCell().SetString(common.ObjToString(v["job"]))
  686. row.AddCell().SetString(common.ObjToString(v["branch"]))
  687. row.AddCell().SetString(common.ObjToString(v["source"]))
  688. row.AddCell().SetString(gconv.Time(gconv.Int64(v["createtime"])).Format(date.Date_Full_Layout))
  689. //增加客户需求
  690. }
  691. email := db.MarketSaleMail
  692. dir := "./xlsx/" + fileName + ".xlsx"
  693. err := xf.Save(dir)
  694. if err != nil {
  695. log.Println("xls error", err, dir)
  696. } else {
  697. gmail := &mail.GmailAuth{
  698. SmtpHost: "smtp.exmail.qq.com",
  699. SmtpPort: 465,
  700. User: "public03@topnet.net.cn",
  701. Pwd: "ue9Rg9Sf4CVtdm5a",
  702. }
  703. status := mail.GSendMail_q("剑鱼标讯", email, "", "", fileName, detailName, dir, fileName+".xlsx", gmail)
  704. if status {
  705. log.Println("send mail success", fileName, email)
  706. }
  707. }
  708. }
  709. common.WriteSysConfig(&cfg)
  710. log.Println("大客户线索定时任务结束")
  711. }
  712. func eventReg() {
  713. lastEventRegTime := cfg.LastEventRegTime
  714. sql := fmt.Sprintf(`select * from exhibition_sign_up where is_del = 0 and update_time > "%s" order by update_time asc`, lastEventRegTime)
  715. log.Println("活动报名表定时任务开始", sql)
  716. data := Jyactivities.SelectBySql(sql)
  717. if data != nil && *data != nil && len(*data) > 0 {
  718. for _, v := range *data {
  719. ok1, ok2 := FormatData(v, "eventReg")
  720. if !ok1 {
  721. common.WriteSysConfig(&cfg)
  722. log.Println("线索卡点", "eventReg", v, lastEventRegTime)
  723. break
  724. } else {
  725. if !ok2 {
  726. log.Println("用户分配已达上限", "eventReg", v, lastEventRegTime)
  727. common.WriteSysConfig(&cfg)
  728. break
  729. }
  730. }
  731. cfg.LastEventRegTime = common.ObjToString(v["update_time"])
  732. }
  733. }
  734. common.WriteSysConfig(&cfg)
  735. log.Println("活动报名表定时任务结束")
  736. }
  737. func saveHlyj(belong_to, item, phone, name, sourceName, cluename, position, nowTime string, isGroup, isCommerce int) {
  738. if strings.HasPrefix(belong_to, "02") && item == "eventReg" {
  739. saveMap := map[string]interface{}{
  740. "unique_id": phone,
  741. "phone": phone,
  742. "username": name,
  743. "source": sourceName,
  744. "status999": "status5",
  745. "company": cluename,
  746. "job": position,
  747. "belongTo": "市场部",
  748. "createTime": nowTime,
  749. "lastUpdateTime": nowTime,
  750. }
  751. token := getToken()
  752. updateData := map[string]interface{}{
  753. "dbType": "0001",
  754. "customerList": []map[string]interface{}{saveMap},
  755. }
  756. dataByte, _ := json.Marshal(&updateData)
  757. url := `https://a1.7x24cc.com/commonInte?flag=1007&account=N000000029739&accessToken=` + token + `&json=` + url.QueryEscape(string(dataByte))
  758. bs, err := doGet(url)
  759. if err != nil {
  760. log.Println("调用接口失败")
  761. } else {
  762. resMap := common.StringToMap(string(bs))
  763. if resMap["success"] != nil && resMap["success"].(bool) {
  764. saveMap["company_nature"] = isGroup
  765. saveMap["company_verification"] = isCommerce
  766. TiDbData.Insert("customer", saveMap)
  767. } else {
  768. log.Println("新增线索失败")
  769. }
  770. }
  771. }
  772. }
  773. func FindBatch(moudle string) string {
  774. now := time.Now().Format("2006-01-02")
  775. key := fmt.Sprintf("batch_%s_%s", now, moudle)
  776. yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  777. yesterdayKey := fmt.Sprintf("batch_%s_%s", yesterday, moudle)
  778. if ok, _ := redis.Exists("newother", yesterdayKey); ok {
  779. //删除之前数据
  780. redis.Del("newother", yesterdayKey)
  781. }
  782. batch := redis.Incr("newother", key)
  783. return fmt.Sprintf("%04d", batch)
  784. }
  785. func FindBatchData(moudle string) map[string]interface{} {
  786. now := time.Now().Format("2006-01-02")
  787. //数据提取
  788. keyContent := fmt.Sprintf("data_%s_%s", now, moudle)
  789. data := redis.Get("newother", keyContent)
  790. if data == nil {
  791. return make(map[string]interface{})
  792. } else {
  793. return gconv.Map(data)
  794. }
  795. }