jobutil.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "strings"
  6. "time"
  7. "app.yhyue.com/moapp/jybase/common"
  8. "app.yhyue.com/moapp/jybase/date"
  9. )
  10. func userbase() {
  11. log.Println("userbase定时任务开始")
  12. selectTimeEnd := time.Unix(time.Now().Unix()-1800, 0).Format("2006-01-02 15:04:05")
  13. sql := fmt.Sprintf(`select * from dwd_f_userbase_baseinfo where updatetime > "%s" and source != "0105" and source != "0104"`, selectTimeEnd)
  14. data := TiDb.SelectBySql(sql)
  15. if data != nil && *data != nil && len(*data) > 0 {
  16. for _, v := range *data {
  17. phone := common.ObjToString(v["phone"])
  18. uId := common.ObjToString(v["uid"])
  19. userId := common.ObjToString(v["userid"])
  20. registedate := common.ObjToString(v["l_registedate"])
  21. name := common.ObjToString(v["name"])
  22. nowTime := time.Now().Format(date.Date_Full_Layout)
  23. source := common.ObjToString(v["source"])
  24. if phone != "" {
  25. contactsData := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where phone = ? and is_delete = 1", phone)
  26. if contactsData == nil || len(*contactsData) == 0 {
  27. contactsData2 := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where baseinfo_id = ? and is_delete = 1", uId)
  28. if contactsData2 != nil && len(*contactsData2) > 0 {
  29. log.Println("userbase uid不为空 新增通讯录", uId)
  30. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  31. "status": 1,
  32. "is_delete": 1,
  33. "createtime": nowTime,
  34. "updatetime": nowTime,
  35. "phone": phone,
  36. "baseinfo_id": uId,
  37. "SOURCE": source,
  38. })
  39. } else {
  40. registedates, _ := time.Parse(date.Date_Full_Layout, registedate)
  41. count := TiDb.CountBySql("select count(1) as count from dwd_f_crm_clue_info where uid = ?", uId)
  42. log.Println("userbase uid 线索数量 ", count)
  43. log.Println("userbase uid 注册时间 ", registedates)
  44. if time.Now().Unix()-registedates.Unix() > int64(cfg.RegTimes)*86400 {
  45. if count == 0 {
  46. // TiDb.Insert("dwd_f_crm_open_sea", map[string]interface{}{
  47. // "clue_id": clueId,
  48. // "comeintime": nowTime,
  49. // "comeinsource": 2,
  50. // })
  51. clueId := TiDb.Insert("dwd_f_crm_clue_info", map[string]interface{}{
  52. "userid": userId,
  53. "uid": uId,
  54. "is_assign": 0,
  55. "comeintime": nowTime,
  56. "createtime": nowTime,
  57. "updatetime": nowTime,
  58. "cluename": phone,
  59. "top_cluetype": "474",
  60. "sub_cluetype": "475",
  61. "trailstatus": "01",
  62. "name": name,
  63. "phone": phone,
  64. "comeintime_open": nowTime,
  65. "comeinsource_open": 1,
  66. })
  67. if clueId > 0 {
  68. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  69. "status": 1,
  70. "is_delete": 1,
  71. "createtime": nowTime,
  72. "updatetime": nowTime,
  73. "phone": phone,
  74. "baseinfo_id": uId,
  75. "SOURCE": source,
  76. })
  77. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  78. "clue_id": clueId,
  79. "position_id": -1,
  80. "change_type": "创建线索",
  81. "new_value": "系统自动创建",
  82. "createtime": nowTime,
  83. "BCPCID": common.GetRandom(32),
  84. "operator_id": -1,
  85. })
  86. }
  87. } else {
  88. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  89. "status": 1,
  90. "is_delete": 1,
  91. "createtime": nowTime,
  92. "updatetime": nowTime,
  93. "phone": phone,
  94. "baseinfo_id": uId,
  95. "SOURCE": source,
  96. })
  97. }
  98. } else {
  99. if count == 0 {
  100. FormatData(v, "users")
  101. } else {
  102. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  103. "status": 1,
  104. "is_delete": 1,
  105. "createtime": nowTime,
  106. "updatetime": nowTime,
  107. "phone": phone,
  108. "baseinfo_id": uId,
  109. "SOURCE": source,
  110. })
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. log.Println("userbase定时任务结束")
  119. }
  120. func getRunOk() bool {
  121. currentTime, runOk := time.Now(), false
  122. if currentTime.Weekday() == time.Sunday {
  123. isok := false
  124. for k, v := range DateMap {
  125. if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
  126. isok = true
  127. }
  128. }
  129. if isok {
  130. runOk = true
  131. }
  132. } else {
  133. isok := true
  134. for k, v := range DateMap {
  135. if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
  136. isok = false
  137. }
  138. }
  139. if isok {
  140. runOk = true
  141. }
  142. }
  143. return runOk
  144. }
  145. func getAreaCode(userId string) (code string) {
  146. followData := Base.Find("follow_project_monitor", map[string]interface{}{"s_userid": userId}, "", "", -1, -1)
  147. sidArr := []string{}
  148. if followData != nil && len(*followData) > 0 {
  149. for _, v := range *followData {
  150. infoId := common.ObjToString(v["s_id"])
  151. sidArr = append(sidArr, infoId)
  152. }
  153. }
  154. if len(sidArr) > 0 {
  155. query := `{"query": {"bool": {"must": [{"terms": {"_id": ["%s"]}}],"must_not": [],"should": []}}}`
  156. query = fmt.Sprintf(query, strings.Join(sidArr, `","`))
  157. biddingData := Es.Get("bidding", "bidding", query)
  158. if biddingData != nil && len(*biddingData) > 0 {
  159. codeMap := map[string]string{}
  160. codeArr := []string{}
  161. for _, v := range *biddingData {
  162. area := common.ObjToString(v["area"])
  163. address := common.ObjToString(v["city"])
  164. if address == "" {
  165. address = area
  166. }
  167. codeMap[address] = AreaCode[address]
  168. }
  169. if len(codeMap) > 0 {
  170. for _, v := range codeMap {
  171. codeArr = append(codeArr, v)
  172. }
  173. }
  174. if len(codeArr) > 0 {
  175. code = strings.Join(codeArr, ",")
  176. }
  177. }
  178. }
  179. log.Println("code ", code)
  180. return
  181. }
  182. func getClueType(item string, data map[string]interface{}, sourceCode string) (pcode, code, level, topname, subname string) {
  183. if item == "orders" {
  184. productType := common.ObjToString(data["product_type"])
  185. pcode = "1"
  186. level = "A"
  187. topname = "提交订单未支付"
  188. if productType == "VIP订阅" {
  189. code = "6"
  190. subname = "超级订阅"
  191. } else if productType == "大会员" {
  192. code = "7"
  193. subname = "大会员"
  194. } else if productType == "数据流量包" {
  195. code = "8"
  196. subname = "数据流量包"
  197. } else if productType == "历史数据" {
  198. code = "9"
  199. subname = "数据自助导出"
  200. }
  201. } else if item == "users" {
  202. pcode = "4"
  203. code = "154"
  204. level = "C"
  205. topname = "新增注册"
  206. subname = "新增注册用户"
  207. } else if item == "message" {
  208. pcode = "532"
  209. code = "477"
  210. level = "B"
  211. topname = "其他"
  212. subname = "机器人客服主动咨询"
  213. } else {
  214. if sourceCode != "" {
  215. codeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"source": sourceCode}, "", "")
  216. if codeData != nil && len(*codeData) > 0 {
  217. pcode = common.ObjToString((*codeData)["pcode"])
  218. code = common.ObjToString((*codeData)["code"])
  219. level = common.ObjToString((*codeData)["clue_level"])
  220. subname = common.ObjToString((*codeData)["name"])
  221. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": pcode}, "", "")
  222. if pcodeData != nil && len(*pcodeData) > 0 {
  223. topname = common.ObjToString((*pcodeData)["name"])
  224. }
  225. }
  226. }
  227. }
  228. return
  229. }
  230. // 获取自动分配的人
  231. func autoDraw(mode, cluename string, isGroup, isCommerce int) (positionId int64, seatNumber, saleName string, saleData []map[string]interface{}, isOk bool) {
  232. isOk = false
  233. if isGroup == 0 && isCommerce == 1 {
  234. cdata := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"cluename": cluename, "is_assign": 1}, "", "")
  235. if cdata != nil && len(*cdata) > 0 {
  236. isOk = true
  237. positionId = common.Int64All((*cdata)["position_id"])
  238. seatNumber = common.ObjToString((*cdata)["seatNumber"])
  239. log.Println("positionId seatNumber ", positionId, seatNumber)
  240. if positionId > 0 {
  241. pdata := TiDb.SelectBySql(`select * from dwd_f_crm_personnel_management where seat_number is not null and seat_number != ""`)
  242. if pdata != nil {
  243. saleData = *pdata
  244. for _, v := range *pdata {
  245. resign := common.IntAll(v["resign"])
  246. if seatNumber == common.ObjToString(v["seat_number"]) {
  247. if resign == 1 { //离职分配
  248. sdata := TiDb.SelectBySql(`SELECT b.name,b.position_id,b.seat_number from dwd_d_crm_department_level_succbi a INNER JOIN dwd_f_crm_personnel_management b on a.position_id = b.position_id where a.bi_pcode = (SELECT bi_pcode from dwd_d_crm_department_level_succbi where position_id = ?) and b.role_id = 3`, positionId)
  249. if sdata != nil && len(*sdata) > 0 {
  250. positionId = common.Int64All((*sdata)[0]["position_id"])
  251. seatNumber = common.ObjToString((*sdata)[0]["seat_number"])
  252. saleName = common.ObjToString((*sdata)[0]["name"])
  253. }
  254. } else {
  255. saleName = common.ObjToString(v["name"])
  256. }
  257. }
  258. }
  259. }
  260. return
  261. }
  262. }
  263. }
  264. query := `select * from dwd_f_crm_personnel_management where assign_type = 1 and`
  265. if mode == "A" {
  266. query += ` assign_level like "%A%"`
  267. } else if mode == "B" {
  268. query += ` assign_level like "%B%"`
  269. } else {
  270. query += ` assign_level like "%C%"`
  271. }
  272. data := TiDb.SelectBySql(query)
  273. if data != nil && len(*data) > 0 {
  274. saleData = *data
  275. sql := "select * from dwd_f_crm_clue_autodraw_record where clue_level = ?"
  276. countData := TiDb.SelectBySql(sql, mode)
  277. if countData != nil && len(*countData) > 0 {
  278. for _, v := range *data {
  279. isOk := false //判断是否有新员工
  280. for _, vv := range *countData {
  281. if common.ObjToString(v["seat_number"]) == common.ObjToString(vv["seatNumber"]) {
  282. if common.IntAll(v["resign"]) == 0 {
  283. vv["status"] = 1
  284. } else {
  285. vv["status"] = 2
  286. }
  287. isOk = true
  288. }
  289. }
  290. if !isOk { //有新员工直接分给新员工
  291. seatNumber = common.ObjToString(v["seat_number"])
  292. saleName = common.ObjToString(v["name"])
  293. rData := TiDb.FindOne("dwd_f_crm_clue_autodraw_record", map[string]interface{}{"clue_level": mode}, "", "count desc")
  294. TiDb.Insert("dwd_f_crm_clue_autodraw_record", map[string]interface{}{
  295. "seatNumber": seatNumber,
  296. "clue_level": mode,
  297. "count": common.Int64All((*rData)["count"]),
  298. })
  299. break
  300. }
  301. }
  302. if seatNumber == "" {
  303. res := int64(0)
  304. countres := 0
  305. for _, v := range *countData {
  306. if common.IntAll(v["status"]) == 1 {
  307. if countres == 0 {
  308. res = common.Int64All(v["count"])
  309. seatNumber = common.ObjToString(v["seatNumber"])
  310. } else {
  311. if common.Int64All(v["count"]) <= res {
  312. res = common.Int64All(v["count"])
  313. seatNumber = common.ObjToString(v["seatNumber"])
  314. }
  315. }
  316. countres++
  317. }
  318. }
  319. }
  320. } else {
  321. seatNumber = common.ObjToString((*data)[0]["seat_number"])
  322. saleName = common.ObjToString((*data)[0]["name"])
  323. rData := TiDb.FindOne("dwd_f_crm_clue_autodraw_record", map[string]interface{}{"clue_level": mode}, "", "count desc")
  324. if rData != nil && len(*rData) > 0 {
  325. TiDb.Insert("dwd_f_crm_clue_autodraw_record", map[string]interface{}{
  326. "seatNumber": seatNumber,
  327. "clue_level": mode,
  328. "count": common.Int64All((*rData)["count"]),
  329. })
  330. } else {
  331. TiDb.Insert("dwd_f_crm_clue_autodraw_record", map[string]interface{}{
  332. "seatNumber": seatNumber,
  333. "clue_level": mode,
  334. "count": 0,
  335. })
  336. }
  337. }
  338. for _, v := range *data {
  339. if seatNumber == common.ObjToString(v["seat_number"]) {
  340. positionId = common.Int64All(v["position_id"])
  341. saleName = common.ObjToString(v["name"])
  342. }
  343. }
  344. }
  345. return
  346. }
  347. func getPositionId(phone string) (positionId int64) {
  348. userData, ok := Mgo.FindOne("user", map[string]interface{}{"s_phone": phone})
  349. if ok && userData != nil && len(*userData) > 0 {
  350. userId := common.Int64All((*userData)["base_user_id"])
  351. positionData := Base.FindOne("base_position", map[string]interface{}{"type": 1, "ent_id": 25917, "user_id": userId}, "", "") //TODO ent_id
  352. if positionData != nil && len(*positionData) > 0 {
  353. positionId = common.Int64All((*positionData)["id"])
  354. }
  355. }
  356. return
  357. }
  358. func GetCompanyType(companyName string) (int, int) {
  359. //是否是集团
  360. isGroup, isCommerce := 0, 0
  361. if c := TiDb.CountBySql(`select count(1) from group_company_name where company_name=?`, companyName); c > 0 {
  362. isGroup = 1
  363. }
  364. //是否在工商库
  365. if c := MgoQyxy.Count("qyxy_std", map[string]interface{}{"company_name": companyName}); c > 0 {
  366. isCommerce = 1
  367. }
  368. return isGroup, isCommerce
  369. }