jobutil.go 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951
  1. package main
  2. import (
  3. "app.yhyue.com/moapp/jybase/mail"
  4. "app.yhyue.com/moapp/jybase/redis"
  5. "fmt"
  6. "github.com/tealeg/xlsx"
  7. "log"
  8. "math"
  9. "strings"
  10. "time"
  11. "github.com/gogf/gf/v2/util/gconv"
  12. "app.yhyue.com/moapp/jybase/common"
  13. "app.yhyue.com/moapp/jybase/date"
  14. "app.yhyue.com/moapp/jybase/mongodb"
  15. )
  16. // 电销工单生成
  17. func rderAcceptance() {
  18. sql := fmt.Sprintf(`select * from order_acceptance where is_clue=2 and is_delete=1 order by propose_time `)
  19. //sql := fmt.Sprintf(`select * from order_acceptance where is_clue=2 and is_delete=1 and id = 333 order by propose_time `)
  20. data := WorkOrder.SelectBySql(sql)
  21. if data != nil && len(*data) > 0 {
  22. for _, v := range *data {
  23. acceptance_no := gconv.String(v["acceptance_no"])
  24. childrenArr := WorkOrder.Find("order_acceptance_children", map[string]interface{}{
  25. "acceptance_no": acceptance_no,
  26. }, "", "", -1, -1)
  27. if childrenArr != nil && len(*childrenArr) > 0 {
  28. company := ""
  29. phone := ""
  30. demand := ""
  31. name := ""
  32. product := ""
  33. for _, v1 := range *childrenArr {
  34. if gconv.String(v1["field_name"]) == "公司名称" {
  35. company = gconv.String(v1["field_value"])
  36. }
  37. if gconv.String(v1["field_name"]) == "联系方式num" {
  38. phone = gconv.String(v1["field_value"])
  39. }
  40. if gconv.String(v1["field_name"]) == "客户姓名" {
  41. name = gconv.String(v1["field_value"])
  42. }
  43. if gconv.String(v1["field_name"]) == "客户需求" {
  44. demand = gconv.String(v1["field_value"])
  45. }
  46. if gconv.String(v1["field_name"]) == "咨询产品" {
  47. product = gconv.String(v1["field_value"])
  48. }
  49. }
  50. if !WorkDataHandle(company, phone, demand, name, product, v) {
  51. log.Println("工单创建失败")
  52. }
  53. }
  54. }
  55. }
  56. }
  57. func WorkDataHandle(company, phone, demand, name, product string, acceptanceData map[string]interface{}) bool {
  58. uId := ""
  59. query := map[string]interface{}{}
  60. contactsData := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where phone = ? and is_delete = 1", phone)
  61. if contactsData != nil && len(*contactsData) > 0 {
  62. if common.ObjToString((*contactsData)[0]["baseinfo_id"]) != "" {
  63. uId = common.ObjToString((*contactsData)[0]["baseinfo_id"])
  64. query["uid"] = uId
  65. }
  66. }
  67. if uId == "" {
  68. log.Println("用户信息不存在")
  69. return false
  70. }
  71. cluename := company
  72. if cluename == "" {
  73. cluename = phone //没有线索名,手机号代替
  74. }
  75. ok, data, saleData := false, map[string]interface{}{}, []map[string]interface{}{}
  76. isGroup, isCommerce := GetCompanyType(cluename, uId)
  77. uCount, _ := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", ""), true //查当前线索是否已存在
  78. if uCount != nil && len(*uCount) > 0 {
  79. isUpdate := gconv.Int64((*uCount)["is_artificially_modified"])
  80. if isUpdate == 1 {
  81. cluename = ""
  82. }
  83. clueId := gconv.Int64((*uCount)["id"])
  84. positionId := gconv.Int64((*uCount)["position_id"])
  85. trailstatus := gconv.String((*uCount)["trailstatus"])
  86. IS_TRANSFER := gconv.Int64((*uCount)["IS_TRANSFER"])
  87. if IS_TRANSFER == 1 {
  88. //客成处理
  89. //客成
  90. //生成客成数据
  91. customerMap := TiDb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{
  92. "clue_id": clueId,
  93. }, "position_id,name", "")
  94. if customerMap != nil && len(*customerMap) > 0 {
  95. //UpdateClue(*uCount, saleData, "", "", uId, "5", "169", "新增线索", "主动咨询客服留资客户", company, name, gconv.String((*uCount)["name"]), phone, "", "", "", "", "", "", gconv.String((*uCount)["seat_number"]), "", gconv.Int64((*uCount)["position_id"]), "", "", "", []string{}, "", isGroup, isCommerce, false, demand)
  96. WorkUpdateClue(*uCount, saleData, "", "", uId, "5", "169", "新增线索", "主动咨询客服留资客户", cluename, name, gconv.String((*uCount)["name"]), phone, "", "", "", "", "", "", gconv.String((*uCount)["seat_number"]), "", gconv.Int64((*uCount)["position_id"]), "", "", "", []string{}, "", isGroup, isCommerce, false, demand)
  97. data = map[string]interface{}{
  98. "type": "kc",
  99. "position_id": (*customerMap)["position_id"],
  100. "name": (*customerMap)["name"],
  101. }
  102. } else {
  103. log.Println("查询不到客成数据", clueId, uId)
  104. return false
  105. }
  106. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  107. "clue_id": clueId,
  108. "position_id": (*customerMap)["position_id"],
  109. "change_type": "加入任务车",
  110. "new_value": "咨询客服转客成",
  111. "createtime": time.Now().Format(date.Date_Full_Layout),
  112. "BCPCID": common.GetRandom(32),
  113. "operator_id": -1,
  114. })
  115. } else {
  116. if positionId == 0 {
  117. //线索变更
  118. ok, data, saleData = FindPosition(0, "", gconv.String(acceptanceData["creator_time"]))
  119. if !ok {
  120. log.Println(positionId, "用户查询失败")
  121. return false
  122. }
  123. WorkUpdateClue(*uCount, saleData, "", "", uId, "5", "169", "新增线索", "主动咨询客服留资客户", cluename, name, gconv.String(data["name"]), phone, "", "", "", "", "", "", gconv.String(data["seat_number"]), "", gconv.Int64(data["position_id"]), "", "", "", []string{}, "", isGroup, isCommerce, true, demand)
  124. } else {
  125. ok, data, saleData = FindPosition(positionId, trailstatus, gconv.String(acceptanceData["creator_time"]))
  126. if !ok {
  127. log.Println(positionId, "用户查询失败")
  128. return false
  129. }
  130. if gconv.Int64(data["position_id"]) == positionId {
  131. WorkUpdateClue(*uCount, saleData, "", "", uId, "5", "169", "新增线索", "主动咨询客服留资客户", cluename, name, gconv.String(data["name"]), phone, "", "", "", "", "", "", gconv.String(data["seat_number"]), "", gconv.Int64(data["position_id"]), "", "", "", []string{}, "", isGroup, isCommerce, false, demand)
  132. } else {
  133. WorkUpdateClue(*uCount, saleData, "", "", uId, "5", "169", "新增线索", "主动咨询客服留资客户", cluename, name, gconv.String(data["name"]), phone, "", "", "", "", "", "", gconv.String(data["seat_number"]), "", gconv.Int64(data["position_id"]), "", "", "", []string{}, "", isGroup, isCommerce, true, demand)
  134. }
  135. }
  136. }
  137. } else {
  138. //新增线索
  139. ok, data, _ = FindPosition(0, "", gconv.String(acceptanceData["creator_time"]))
  140. if !ok {
  141. return false
  142. }
  143. SaveClue("", "", uId, "5", "169", "新增线索", "主动咨询客服留资客户", cluename, name, gconv.String(data["name"]), phone, "", "", "", "", "", "", gconv.String(data["seat_number"]), gconv.Int64(data["position_id"]), "", "", "", []string{}, "", isGroup, isCommerce, false, demand)
  144. }
  145. //工单生成
  146. ok1 := AddOrderWork(acceptanceData, data, product, phone, company)
  147. log.Println("工单创建", ok1, gconv.String(acceptanceData["acceptance_no"]))
  148. return true
  149. }
  150. func AddOrderWork(acceptanceData map[string]interface{}, userData map[string]interface{}, product, phone, company string) bool {
  151. nowTime := time.Now().Format(date.Date_Full_Layout)
  152. work_order_no := fmt.Sprintf("GD%s%s", time.Now().Format(date.Date_yyyyMMdd), FindNumber("gd"))
  153. productArr := []string{}
  154. for _, v := range strings.Split(product, ",") {
  155. switch ProductMap[v] {
  156. case "dk":
  157. continue
  158. default:
  159. productArr = append(productArr, v)
  160. }
  161. }
  162. two_type := "dx"
  163. if gconv.String(userData["type"]) == "kc" {
  164. two_type = "kc"
  165. }
  166. personMap := GetPerson(gconv.String(userData["position_id"]))
  167. if personMap == nil || len(personMap) == 0 {
  168. return false
  169. }
  170. orderWorkMap := map[string]interface{}{
  171. "work_order_no": work_order_no,
  172. "acceptance_no": gconv.String(acceptanceData["acceptance_no"]),
  173. "type": strings.Join(productArr, ","),
  174. "status": common.If(gconv.Int64(userData["orderStatus"]) == 0, 2, 1),
  175. "initiator_name": gconv.String(acceptanceData["creator_name"]),
  176. "initiator_position_id": gconv.String(acceptanceData["initiator_position_id"]),
  177. "current_name": common.If(gconv.Int64(userData["orderStatus"]) == 0, nil, userData["name"]),
  178. "current_position_id": common.If(gconv.Int64(userData["orderStatus"]) == 0, nil, userData["position_id"]),
  179. "history_name": common.If(gconv.Int64(userData["orderStatus"]) == 0, userData["name"], nil),
  180. "history_postion_id": common.If(gconv.Int64(userData["orderStatus"]) == 0, userData["position_id"], nil),
  181. "is_delete": 1,
  182. "creator_name": gconv.String(acceptanceData["creator_name"]),
  183. "creator_position_id": gconv.String(acceptanceData["creator_position_id"]),
  184. "creator_time": nowTime,
  185. "two_type": two_type,
  186. "department_no": gconv.String(personMap["deptId"]),
  187. "department_name": gconv.String(personMap["deptName"]),
  188. "update_time": common.If(gconv.Int64(userData["orderStatus"]) == 0, nowTime, nil),
  189. }
  190. ok3 := WorkOrder.Insert("order_work", orderWorkMap)
  191. if ok3 > 0 {
  192. status := 2
  193. if WorkOrder.Count("order_work", map[string]interface{}{
  194. "status": 1,
  195. "acceptance_no": gconv.String(acceptanceData["acceptance_no"]),
  196. }) > 0 {
  197. status = 1
  198. }
  199. WorkOrder.Update("order_acceptance", map[string]interface{}{
  200. "id": acceptanceData["id"],
  201. }, map[string]interface{}{
  202. "is_clue": 3,
  203. "status": status,
  204. "over_time": common.If(status == 1, nil, nowTime),
  205. })
  206. //日志添加
  207. approvalRecordMap := map[string]interface{}{
  208. "work_order_no": work_order_no,
  209. "status": common.If(gconv.Int64(userData["orderStatus"]) == 0, 3, 1),
  210. "new_status": common.If(gconv.Int64(userData["orderStatus"]) == 0, 2, nil),
  211. "handle_name": userData["name"],
  212. "handle_position_id": userData["position_id"],
  213. "handle_dept_id": gconv.String(personMap["deptId"]),
  214. "handle_dept_name": gconv.String(personMap["deptName"]),
  215. "creator_name": gconv.String(acceptanceData["creator_name"]),
  216. "creator_position_id": gconv.String(acceptanceData["initiator_position_id"]),
  217. "is_delete": 1,
  218. "creator_time": nowTime,
  219. "handle_time": common.If(gconv.Int64(userData["orderStatus"]) == 0, nowTime, nil),
  220. "update_name": common.If(gconv.Int64(userData["orderStatus"]) == 0, userData["name"], nil),
  221. "update_position_id": common.If(gconv.Int64(userData["orderStatus"]) == 0, userData["position_id"], nil),
  222. "update_time": common.If(gconv.Int64(userData["orderStatus"]) == 0, nowTime, nil),
  223. }
  224. WorkOrder.Insert("approval_record", approvalRecordMap)
  225. WorkMail(personMap,
  226. strings.Join(productArr, ","),
  227. gconv.Int64(common.If(gconv.Int64(userData["orderStatus"]) == 0, 2, 1)),
  228. gconv.String(personMap["name"]),
  229. gconv.String(acceptanceData["creator_name"]),
  230. nowTime,
  231. work_order_no,
  232. phone,
  233. company)
  234. return true
  235. }
  236. return false
  237. }
  238. // 本级以及上级管理员查询
  239. func GetPerson(positionId string) map[string]interface{} {
  240. person := map[string]interface{}{}
  241. positionArrMap := Base.SelectBySql(fmt.Sprintf(`select a.phone,b.id,b.ent_id from
  242. base_user a
  243. INNER JOIN base_position b
  244. on b.id=%s and b.user_id=a.id and b.type=1`,
  245. positionId))
  246. if positionArrMap == nil || len(*positionArrMap) == 0 {
  247. return map[string]interface{}{}
  248. }
  249. phone := gconv.String((*positionArrMap)[0]["phone"])
  250. entId := gconv.String((*positionArrMap)[0]["ent_id"])
  251. entUserArrMap := Mysql.SelectBySql(fmt.Sprintf(`SELECT
  252. a.name as name,a.mail as mail,b.dept_id as deptId,a.phone ,c.name as deptName
  253. FROM
  254. entniche_user a
  255. INNER JOIN entniche_department_user b ON a.ent_id =%s
  256. AND a.phone IN %s
  257. and a.id=b.user_id
  258. inner join entniche_department c on b.dept_id=c.id
  259. `, entId, fmt.Sprintf("(%s)", phone)))
  260. //商机管理员
  261. if entUserArrMap == nil || len(*entUserArrMap) == 0 {
  262. return map[string]interface{}{}
  263. }
  264. deptId := gconv.Int64((*entUserArrMap)[0]["deptId"])
  265. person = map[string]interface{}{
  266. "name": gconv.String((*entUserArrMap)[0]["name"]),
  267. "mail": gconv.String((*entUserArrMap)[0]["mail"]),
  268. "phone": gconv.String((*entUserArrMap)[0]["phone"]),
  269. "deptId": gconv.String((*entUserArrMap)[0]["deptId"]),
  270. "deptName": gconv.String((*entUserArrMap)[0]["deptName"]),
  271. }
  272. //本部门管理员查询
  273. depthMap := Mysql.SelectBySql(`SELECT
  274. c.name as name,c.mail as mail
  275. FROM
  276. entniche_department_user a
  277. INNER JOIN entniche_user_role b ON a.dept_id = ?
  278. AND a.user_id = b.user_id
  279. AND b.role_id !=""
  280. INNER JOIN entniche_user c ON a.user_id = c.id`, deptId)
  281. if depthMap != nil && len(*depthMap) > 0 {
  282. person["deptPersonName"] = gconv.String((*depthMap)[0]["name"])
  283. person["deptPersonMail"] = gconv.String((*depthMap)[0]["mail"])
  284. }
  285. //商机管理员查询
  286. superiorDepthMap := Mysql.SelectBySql(`SELECT
  287. c.*
  288. FROM
  289. entniche_department d
  290. INNER JOIN entniche_department_user a ON d.id = ?
  291. AND d.pid = a.dept_id
  292. INNER JOIN entniche_user_role b ON a.user_id = b.user_id
  293. AND b.role_id !=""
  294. INNER JOIN entniche_user c ON a.user_id = c.id`, deptId)
  295. if superiorDepthMap != nil && len(*superiorDepthMap) > 0 {
  296. person["superiorDepthPersonName"] = gconv.String((*superiorDepthMap)[0]["name"])
  297. person["superiorDepthPersonMail"] = gconv.String((*superiorDepthMap)[0]["mail"])
  298. }
  299. return person
  300. }
  301. // 编号查询
  302. func FindNumber(moudle string) string {
  303. today := time.Now().Format("2006-01-02")
  304. yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  305. key := fmt.Sprintf("%s_%s", today, moudle)
  306. yesterdayKey := fmt.Sprintf("%s_%s", yesterday, moudle)
  307. if ok, _ := redis.Exists("newother", yesterdayKey); ok {
  308. //删除之前数据
  309. redis.Del("newother", yesterdayKey)
  310. }
  311. count := redis.Incr("newother", key)
  312. log.Println("编号获取", moudle, fmt.Sprintf("%04d", count))
  313. return fmt.Sprintf("%04d", count)
  314. }
  315. // 人员查询
  316. func GetAllocation(proportion1, proportion3 float64, deptCount1, deptCount3 int64, administrators1, administrators3 map[string]interface{}, creatorTime string) map[string]interface{} {
  317. log.Println("分配比例查询", proportion1, proportion3, deptCount1, deptCount3)
  318. if deptCount1 == 0 {
  319. return administrators1
  320. }
  321. if deptCount3 == 0 {
  322. return administrators3
  323. }
  324. nowAllocationRatio := math.Round(gconv.Float64(deptCount1 / deptCount3))
  325. if cfg.AllocationRatio == 0 {
  326. //重新计算
  327. cfg.AllocationRatio = math.Round(proportion1 / proportion3)
  328. cfg.AllocationTime = creatorTime
  329. common.WriteSysConfig(&cfg)
  330. log.Println("新增计算比例", cfg.AllocationRatio)
  331. return administrators1
  332. } else {
  333. if nowAllocationRatio == cfg.AllocationRatio {
  334. //重新计算
  335. cfg.AllocationRatio = math.Round(proportion1 / proportion3)
  336. cfg.AllocationTime = creatorTime
  337. common.WriteSysConfig(&cfg)
  338. return administrators1
  339. }
  340. }
  341. if nowAllocationRatio > cfg.AllocationRatio {
  342. return administrators3
  343. } else {
  344. return administrators1
  345. }
  346. //达到比例也重新计算
  347. }
  348. func FindPosition(positionId int64, trailstatus, creatorTime string) (bool, map[string]interface{}, []map[string]interface{}) {
  349. allData := []map[string]interface{}{}
  350. //查询两个部门高级管理员
  351. //查询两个部门所有人员标识
  352. //电销三部
  353. userData1 := TiDb.SelectBySql(`SELECT
  354. a.position_id,
  355. a.name,
  356. b.seat_number,
  357. b.role_id,
  358. a.bi_pcode,
  359. a.dept_name
  360. FROM
  361. dwd_d_crm_department_level_succbi a
  362. INNER JOIN dwd_f_crm_personnel_management b ON a.resign = 0 and b.resign = 0
  363. AND a.dept_name LIKE "销售一部%"
  364. AND a.position_id = b.position_id`)
  365. userData3 := TiDb.SelectBySql(`SELECT
  366. a.position_id,
  367. a.name,
  368. b.seat_number,
  369. b.role_id,
  370. a.bi_pcode,
  371. a.dept_name
  372. FROM
  373. dwd_d_crm_department_level_succbi a
  374. INNER JOIN dwd_f_crm_personnel_management b ON a.resign = 0 and b.resign = 0
  375. AND a.resign = 0
  376. AND a.dept_name LIKE "%销售三部"
  377. AND a.position_id = b.position_id`)
  378. if userData3 == nil || len(*userData3) == 0 || userData1 == nil || len(*userData1) == 0 {
  379. return false, map[string]interface{}{}, allData
  380. }
  381. //一部管理员信息
  382. administrators1 := map[string]interface{}{}
  383. //三部管理员信息
  384. administrators3 := map[string]interface{}{}
  385. //返回当前人信息
  386. administrators := map[string]interface{}{}
  387. //电销一部
  388. deptNameMap1 := map[string]interface{}{}
  389. deptNameMap3 := map[string]interface{}{}
  390. proportion1 := float64(0)
  391. proportion3 := float64(0)
  392. for _, v := range *userData3 {
  393. v["type"] = 3
  394. id := gconv.Int64(v["position_id"])
  395. roleId := gconv.Int64(v["role_id"])
  396. if id == positionId {
  397. administrators = v
  398. }
  399. if roleId == 8 {
  400. v["orderStatus"] = 2
  401. administrators3 = v
  402. } else if roleId == 3 {
  403. proportion3 += 0.5
  404. } else {
  405. proportion3 += 1
  406. }
  407. deptNameMap3[gconv.String(v["bi_pcode"])] = true
  408. allData = append(allData, v)
  409. }
  410. for _, v := range *userData1 {
  411. v["type"] = 1
  412. id := gconv.Int64(v["position_id"])
  413. roleId := gconv.Int64(v["role_id"])
  414. if id == positionId {
  415. administrators = v
  416. }
  417. if roleId == 8 {
  418. v["orderStatus"] = 2
  419. administrators1 = v
  420. } else if roleId == 3 {
  421. proportion1 += 0.5
  422. } else {
  423. proportion1 += 1
  424. }
  425. deptNameMap1[gconv.String(v["bi_pcode"])] = true
  426. allData = append(allData, v)
  427. }
  428. if positionId != 0 {
  429. if administrators == nil || len(administrators) == 0 {
  430. return false, map[string]interface{}{}, allData
  431. }
  432. positiontype := gconv.Int64(administrators["type"])
  433. if trailstatus == "01" {
  434. switch positiontype {
  435. case 1:
  436. //找他上级
  437. administrators = administrators1
  438. case 3:
  439. //找他上级
  440. administrators = administrators3
  441. }
  442. return true, administrators, allData
  443. } else {
  444. return true, administrators, allData
  445. }
  446. }
  447. //按照比例分配选择一部还是三部
  448. //一部查询
  449. deptCount1 := CalculateProportion(deptNameMap1)
  450. //三部查询
  451. deptCount3 := CalculateProportion(deptNameMap3)
  452. return true, GetAllocation(proportion1, proportion3, deptCount1, deptCount3, administrators1, administrators3, creatorTime), allData
  453. log.Println("电销信息获取失败", positionId)
  454. return false, map[string]interface{}{}, allData
  455. }
  456. func CalculateProportion(deptNameMap map[string]interface{}) int64 {
  457. deptNameStr := ""
  458. for k := range deptNameMap {
  459. if deptNameStr == "" {
  460. deptNameStr = k
  461. } else {
  462. deptNameStr = fmt.Sprintf("%s,%s", deptNameStr, k)
  463. }
  464. }
  465. if deptNameStr == "" {
  466. return int64(0)
  467. }
  468. count := int64(0)
  469. sql := fmt.Sprintf(`SELECT
  470. sum(1) as count
  471. FROM
  472. order_work a
  473. INNER JOIN approval_record b ON a.creator_time >= "%s"
  474. AND FIND_IN_SET( a.department_no , "%s")
  475. AND a.work_order_no = b.work_order_no
  476. and (b.new_status!= 1 or b.new_status is null )
  477. `, cfg.AllocationTime, deptNameStr)
  478. data := WorkOrder.SelectBySql(sql)
  479. if data != nil && len(*data) > 0 {
  480. count = gconv.Int64((*data)[0]["count"])
  481. }
  482. return count
  483. }
  484. // 未支付订单 30分钟一次
  485. func orders() {
  486. //一个小时未支付进入线索 A
  487. log.Println("未支付订单定时任务开始")
  488. lastOrderId := cfg.LastOrderId
  489. selectTimeStart := time.Unix(time.Now().Unix()-7200, 0).Format(date.Date_Full_Layout)
  490. selectTimeEnd := time.Unix(time.Now().Unix()-3600, 0).Format(date.Date_Full_Layout)
  491. sql := fmt.Sprintf(`select * from dataexport_order where create_time <= "%s" and create_time >= "%s" and id > %s`, selectTimeEnd, selectTimeStart, fmt.Sprint(lastOrderId))
  492. //sql := fmt.Sprintf(`select * from dataexport_order where id=260265`)
  493. data := Mysql.SelectBySql(sql)
  494. if data != nil && *data != nil && len(*data) > 0 {
  495. for _, v := range *data {
  496. order_status := common.IntAll(v["order_status"])
  497. is_backstage_order := common.IntAll(v["is_backstage_order"])
  498. product_type_str1 := `"大会员","VIP订阅","数据流量包","历史数据"`
  499. product_type := common.ObjToString(v["product_type"])
  500. if order_status == 0 && is_backstage_order == 0 && strings.Contains(product_type_str1, product_type) {
  501. ok1, ok2, _ := FormatData(v, "orders")
  502. if !ok1 {
  503. common.WriteSysConfig(&cfg)
  504. log.Println("线索卡点", "orders", v, selectTimeEnd, selectTimeStart)
  505. } else {
  506. if !ok2 {
  507. log.Println("用户分配已达上限", "orders", v, selectTimeEnd, selectTimeStart)
  508. common.WriteSysConfig(&cfg)
  509. }
  510. }
  511. }
  512. cfg.LastOrderId = common.IntAll(v["id"])
  513. }
  514. }
  515. common.WriteSysConfig(&cfg)
  516. log.Println("未支付订单定时任务结束")
  517. }
  518. func readClue() {
  519. log.Println("读表进线索定时任务开始")
  520. lastReadClueTime := cfg.LastReadClueTime
  521. sql := fmt.Sprintf(`select * from clue_info where updatetime > "%s" order by updatetime asc`, fmt.Sprint(lastReadClueTime))
  522. data := TiDb.SelectBySql(sql)
  523. if data != nil && *data != nil && len(*data) > 0 {
  524. for _, v := range *data {
  525. ok1, ok2, _ := FormatData(v, "readClue")
  526. if !ok1 {
  527. common.WriteSysConfig(&cfg)
  528. log.Println("线索卡点", "readClue", v, lastReadClueTime)
  529. } else {
  530. if !ok2 {
  531. log.Println("用户分配已达上限", "readClue", v, lastReadClueTime)
  532. common.WriteSysConfig(&cfg)
  533. }
  534. }
  535. cfg.LastReadClueTime = common.ObjToString(v["updatetime"])
  536. }
  537. }
  538. common.WriteSysConfig(&cfg)
  539. log.Println("读表进线索定时任务结束")
  540. }
  541. // 新注册用户 5分钟一次
  542. func users() {
  543. //判断节假日
  544. runOk := getRunOk()
  545. if !runOk {
  546. log.Println("不是工作日,任务暂停")
  547. return
  548. }
  549. //新用户注册后5分钟内进入线索 C
  550. log.Println("新注册用户定时任务开始")
  551. selectTimeEnd := cfg.LastUserId
  552. sql := fmt.Sprintf(`select * from dwd_f_userbase_baseinfo where createtime > "%s" and (s_platform != 'xcx' or s_platform is NULL) and source = "0101" and status != 2 order by createtime asc`, selectTimeEnd)
  553. //sql := fmt.Sprintf(`select * from dwd_f_userbase_baseinfo where id= 2259277`)
  554. data := TiDb.SelectBySql(sql)
  555. abhList := []map[string]interface{}{}
  556. if data != nil && *data != nil && len(*data) > 0 {
  557. for k, v := range *data {
  558. //判断用户是否有小程序切使用过剑鱼其他产品
  559. createtime := common.ObjToString(v["createtime"])
  560. ok1, ok2, ok3 := FormatData(v, "users")
  561. if !ok3 {
  562. cfg.LastUserId = createtime
  563. //安博会数据
  564. abhList = append(abhList, v)
  565. continue
  566. }
  567. if !ok1 {
  568. log.Println("线索卡点", "users", v, selectTimeEnd)
  569. } else {
  570. if !ok2 {
  571. log.Println("用户分配已达上限", "users", v, selectTimeEnd)
  572. }
  573. }
  574. if k == len(*data)-1 {
  575. cfg.LastUserId = createtime
  576. }
  577. }
  578. if len(abhList) > 0 {
  579. //安博会发邮件高翔
  580. ABHEmail("user", abhList)
  581. }
  582. }
  583. common.WriteSysConfig(&cfg)
  584. selectXcxTimeEnd := cfg.LastXcxUserId
  585. xcxSql := fmt.Sprintf(`SELECT DISTINCT
  586. aa.mgo_id as userId,
  587. cc.code as industry ,
  588. aa.phone,
  589. IF
  590. ( aa.phone_time IS NULL, aa.create_time, aa.phone_time ) as time
  591. FROM
  592. debris_product.miniprogram_user aa
  593. INNER JOIN debris_product.miniprogram bb ON (
  594. aa.phone <> ''
  595. AND
  596. IF
  597. ( aa.phone_time IS NULL, aa.create_time, aa.phone_time ) > '%s'
  598. AND aa.miniprogram_code = bb.CODE
  599. )
  600. INNER JOIN debris_product.industry cc ON ( bb.industry_code = cc.CODE )
  601. WHERE
  602. NOT EXISTS (
  603. SELECT
  604. 1
  605. FROM
  606. debris_product.miniprogram_user a
  607. INNER JOIN debris_product.miniprogram b ON (
  608. aa.unionid = a.unionid
  609. AND aa.miniprogram_code = a.miniprogram_code
  610. AND aa.id != a.id
  611. AND
  612. IF
  613. ( a.phone_time IS NULL, a.create_time, a.phone_time ) <= '%s'
  614. AND a.miniprogram_code = b.CODE
  615. )
  616. INNER JOIN debris_product.miniprogram_user c ON ( a.unionid = c.unionid AND b.CODE = c.miniprogram_code ))
  617. ORDER BY time
  618. `, selectXcxTimeEnd, selectXcxTimeEnd)
  619. xcxData := debrisProductMysql.SelectBySql(xcxSql)
  620. log.Println("11122", selectXcxTimeEnd, xcxData)
  621. if xcxData != nil && *xcxData != nil && len(*xcxData) > 0 {
  622. afList := []map[string]interface{}{}
  623. for k, v := range *xcxData {
  624. //判断用户是否有小程序切使用过剑鱼其他产品
  625. //判断uid
  626. uId := ""
  627. for i := 0; i < 10; i++ {
  628. phone := common.ObjToString(v["phone"])
  629. contactsData := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where phone = ? and is_delete = 1", phone)
  630. if contactsData != nil && len(*contactsData) > 0 {
  631. if common.ObjToString((*contactsData)[0]["baseinfo_id"]) != "" {
  632. uId = common.ObjToString((*contactsData)[0]["baseinfo_id"])
  633. }
  634. }
  635. if uId == "" {
  636. time.Sleep(1 * time.Minute)
  637. } else {
  638. break
  639. }
  640. }
  641. updatetime := common.ObjToString(v["time"])
  642. if uId == "" {
  643. cfg.LastXcxUserId = updatetime
  644. continue
  645. }
  646. ok1, ok2, _ := FormatData(v, "xcxusers")
  647. /*if !ok3 {
  648. //首次使用"安防"小程序新注册用户
  649. afList = append(afList, v)
  650. cfg.LastXcxUserId = updatetime
  651. continue
  652. }*/
  653. if !ok1 {
  654. common.WriteSysConfig(&cfg)
  655. log.Println("小程序用户分配线索卡点", "xcxusers", v, selectXcxTimeEnd)
  656. } else {
  657. if !ok2 {
  658. log.Println("小程序用户分配已达上限", "xcxusers", v, selectXcxTimeEnd)
  659. common.WriteSysConfig(&cfg)
  660. }
  661. }
  662. if k == len(*xcxData)-1 {
  663. cfg.LastXcxUserId = updatetime
  664. }
  665. }
  666. if len(afList) > 0 {
  667. //发邮件给张文福
  668. AFEmail(afList)
  669. }
  670. }
  671. common.WriteSysConfig(&cfg)
  672. log.Println("新注册用户定时任务结束")
  673. }
  674. // 留资 5分钟一次
  675. func saleLeads() {
  676. //判断节假日
  677. runOk := getRunOk()
  678. if !runOk {
  679. log.Println("不是工作日,任务暂停")
  680. return
  681. }
  682. //留资后5分钟内进入线索
  683. //分为免费留资和付费留资 付费B 免费C
  684. log.Println("用户留资定时任务开始")
  685. session := Mgo.GetMgoConn()
  686. lastId := cfg.LastId
  687. abhList := []map[string]interface{}{}
  688. defer func() {
  689. Mgo.DestoryMongoConn(session)
  690. }()
  691. query := map[string]interface{}{}
  692. if lastId != "" {
  693. query["_id"] = map[string]interface{}{"$gt": mongodb.StringTOBsonId(lastId)}
  694. }
  695. log.Println("query :", query)
  696. iter := session.DB(db.Mgo.DbName).C("saleLeads").Find(&query).Sort("_id").Iter()
  697. thisData := map[string]interface{}{}
  698. for {
  699. if !iter.Next(&thisData) {
  700. break
  701. }
  702. sourceCode := common.ObjToString(thisData["source"])
  703. if sourceCode == "" {
  704. log.Println("留资没有source", thisData)
  705. continue
  706. }
  707. //
  708. filterArr := []string{"-pc", "-app", "-wx", "-h5"}
  709. sourceMap := map[string]string{}
  710. saleSource := TiDb.SelectBySql(`SELECT source,name FROM d_saleleads_code WHERE (department LIKE '%大客户%' or department LIKE '%市场组%' or department LIKE '%咨询组%' or department LIKE '%运营部%') AND is_delete = 1`)
  711. if saleSource != nil && len(*saleSource) > 0 {
  712. for _, v := range *saleSource {
  713. source := common.ObjToString(v["source"])
  714. name := common.ObjToString(v["name"])
  715. for _, s := range filterArr {
  716. name = strings.ReplaceAll(name, s, "")
  717. }
  718. sourceMap[source] = name
  719. }
  720. }
  721. if sourceMap[sourceCode] != "" {
  722. continue
  723. }
  724. ok1, ok2, ok3 := FormatData(thisData, "saleLeads")
  725. if !ok3 {
  726. cfg.LastId = mongodb.BsonIdToSId(thisData["_id"])
  727. //安博会数据
  728. abhList = append(abhList, thisData)
  729. continue
  730. }
  731. if !ok1 {
  732. log.Println("线索卡点", "saleLeads", thisData, lastId)
  733. } else {
  734. if !ok2 {
  735. log.Println("用户分配已达上限", "saleLeads", thisData, lastId)
  736. }
  737. }
  738. cfg.LastId = mongodb.BsonIdToSId(thisData["_id"])
  739. }
  740. if len(abhList) > 0 {
  741. //安博会发邮件高翔
  742. ABHEmail("saleLeads", abhList)
  743. }
  744. common.WriteSysConfig(&cfg)
  745. log.Println("用户留资定时任务结束")
  746. }
  747. func userbase() {
  748. log.Println("userbase定时任务开始")
  749. timeUnix := time.Now().Unix()
  750. selectTimeStart := time.Unix(timeUnix-1800, 0).Format("2006-01-02 15:04:05")
  751. selectTimeEnd := time.Unix(timeUnix, 0).Format("2006-01-02 15:04:05")
  752. sql := fmt.Sprintf(`SELECT
  753. a.phone,b.uid,a.baseinfo_id,b.l_registedate,a.createtime,b.id,b.userid,b.rsource
  754. FROM
  755. dwd_f_userbase_contacts a
  756. INNER JOIN dwd_f_userbase_baseinfo b ON
  757. b.status= 1
  758. and (b.s_platform != 'xcx' or b.s_platform is NULL)
  759. AND a.phone = b.phone and a.baseinfo_id= b.uid and a.source ="0101"
  760. and a.phone is not NULL
  761. and a.createtime >="%s"
  762. and a.createtime <"%s"
  763. and a.is_delete=1
  764. ORDER BY a.createtime `, selectTimeStart, selectTimeEnd)
  765. data := TiDb.SelectBySql(sql)
  766. if data != nil && *data != nil && len(*data) > 0 {
  767. for _, v := range *data {
  768. phone := common.ObjToString(v["phone"])
  769. uId := common.ObjToString(v["uid"])
  770. userId := common.ObjToString(v["userid"])
  771. registedate := common.ObjToString(v["l_registedate"])
  772. name := common.ObjToString(v["name"])
  773. nowTime := time.Now().Format(date.Date_Full_Layout)
  774. layout := "2006-01-02 15:04:05"
  775. createtime := gconv.String(v["createtime"])
  776. createtimeInt64, _ := time.Parse(layout, createtime)
  777. registedateInt64, _ := time.Parse(layout, registedate)
  778. count := TiDb.CountBySql("select count(1) as count from dwd_f_crm_clue_info where uid = ?", uId)
  779. if createtimeInt64.Unix()-registedateInt64.Unix() < int64(db.RegTimes)*86400 {
  780. if count == 0 {
  781. //注册少于7天、走新增
  782. data := TiDb.FindOne("dwd_f_userbase_baseinfo", map[string]interface{}{
  783. "id": gconv.Int64(v["id"]),
  784. }, "", "")
  785. if data != nil && len(*data) > 0 {
  786. ok1, ok2, _ := FormatData(v, "users")
  787. if !ok1 {
  788. log.Println("线索卡点", "userbase uid", v, uId)
  789. } else {
  790. if !ok2 {
  791. log.Println("用户分配已达上限", "userbase uid", v, uId)
  792. }
  793. }
  794. }
  795. }
  796. continue
  797. }
  798. registedates, _ := time.Parse(date.Date_Full_Layout, registedate)
  799. log.Println("userbase uid 线索数量 ", count)
  800. log.Println("userbase uid 注册时间 ", registedates)
  801. if count == 0 {
  802. clueId := int64(0)
  803. sql := fmt.Sprintf(`select * from freeClubSign where mogUserId="%s" and sub_again_date > "%s" `, userId, selectTimeEnd)
  804. data := BiService.SelectBySql(sql)
  805. if len(*data) > 0 && userId != "" {
  806. clueId = TiDb.Insert("dwd_f_crm_clue_info", map[string]interface{}{
  807. "userid": userId,
  808. "uid": uId,
  809. "is_assign": 0,
  810. "comeintime": nowTime,
  811. "createtime": nowTime,
  812. "updatetime": nowTime,
  813. "cluename": phone,
  814. "top_cluetype": "532",
  815. "sub_cluetype": "670",
  816. "trailstatus": "01",
  817. "name": name,
  818. "phone": phone,
  819. "comeintime_open": nowTime,
  820. "comeinsource_open": 1,
  821. "FREEZE_TIME": nowTime,
  822. })
  823. } else {
  824. clueId = TiDb.Insert("dwd_f_crm_clue_info", map[string]interface{}{
  825. "userid": userId,
  826. "uid": uId,
  827. "is_assign": 0,
  828. "comeintime": nowTime,
  829. "createtime": nowTime,
  830. "updatetime": nowTime,
  831. "cluename": phone,
  832. "top_cluetype": "532",
  833. "sub_cluetype": "475",
  834. "trailstatus": "01",
  835. "name": name,
  836. "phone": phone,
  837. "comeintime_open": nowTime,
  838. "comeinsource_open": 1,
  839. "FREEZE_TIME": nowTime,
  840. })
  841. }
  842. if clueId > 0 {
  843. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  844. "clue_id": clueId,
  845. "position_id": -1,
  846. "change_type": "创建线索",
  847. "new_value": "系统自动创建",
  848. "createtime": nowTime,
  849. "BCPCID": common.GetRandom(32),
  850. "operator_id": -1,
  851. })
  852. }
  853. }
  854. }
  855. }
  856. log.Println("userbase定时任务结束")
  857. }
  858. func getRunOk() bool {
  859. currentTime, runOk := time.Now(), false
  860. if currentTime.Weekday() == time.Sunday {
  861. isok := false
  862. for k, v := range DateMap {
  863. if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
  864. isok = true
  865. }
  866. }
  867. if isok {
  868. runOk = true
  869. }
  870. } else {
  871. isok := true
  872. for k, v := range DateMap {
  873. if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
  874. isok = false
  875. }
  876. }
  877. if isok {
  878. runOk = true
  879. }
  880. }
  881. return runOk
  882. }
  883. func getAreaCode(userId string) (code string) {
  884. followData := Base.Find("follow_project_monitor", map[string]interface{}{"s_userid": userId}, "", "", -1, -1)
  885. sidArr := []string{}
  886. if followData != nil && len(*followData) > 0 {
  887. for _, v := range *followData {
  888. infoId := common.ObjToString(v["s_id"])
  889. sidArr = append(sidArr, infoId)
  890. }
  891. }
  892. if len(sidArr) > 0 {
  893. query := `{"query": {"bool": {"must": [{"terms": {"_id": ["%s"]}}],"must_not": [],"should": []}}}`
  894. query = fmt.Sprintf(query, strings.Join(sidArr, `","`))
  895. biddingData := Es.Get("bidding", "bidding", query)
  896. if biddingData != nil && len(*biddingData) > 0 {
  897. codeMap := map[string]string{}
  898. codeArr := []string{}
  899. for _, v := range *biddingData {
  900. area := common.ObjToString(v["area"])
  901. address := common.ObjToString(v["city"])
  902. if address == "" {
  903. address = area
  904. }
  905. codeMap[address] = AreaCode[address]
  906. }
  907. if len(codeMap) > 0 {
  908. for _, v := range codeMap {
  909. codeArr = append(codeArr, v)
  910. }
  911. }
  912. if len(codeArr) > 0 {
  913. code = strings.Join(codeArr, ",")
  914. }
  915. }
  916. }
  917. log.Println("code ", code)
  918. return
  919. }
  920. func getClueType(item string, data map[string]interface{}, sourceCode string, sourceId int64) (pcode, code, level, topname, subname string) {
  921. if item == "orders" {
  922. productType := common.ObjToString(data["product_type"])
  923. pcode = "1"
  924. level = "A"
  925. topname = "提交订单未支付"
  926. if productType == "VIP订阅" {
  927. code = "6"
  928. subname = "超级订阅"
  929. } else if productType == "大会员" {
  930. code = "7"
  931. subname = "大会员"
  932. } else if productType == "数据流量包" {
  933. code = "8"
  934. subname = "数据流量包"
  935. } else if productType == "历史数据" {
  936. code = "9"
  937. subname = "数据自助导出"
  938. }
  939. } else if item == "users" && sourceCode != "bidCreditReportPreview" && sourceCode != "bidCreditReportFile" {
  940. pcode = "4"
  941. code = "154"
  942. level = "C"
  943. topname = "新增注册"
  944. subname = "新增注册用户"
  945. } else if item == "users" && (sourceCode == "bidCreditReportPreview" || sourceCode == "bidCreditReportFile") {
  946. pcode = "4"
  947. code = "767"
  948. level = "B"
  949. topname = "新增注册"
  950. subname = "信用报告引流用户"
  951. } else if item == "message" {
  952. pcode = "532"
  953. code = "477"
  954. level = "B"
  955. topname = "其他"
  956. subname = "机器人客服主动咨询"
  957. } else if item == "readClue" {
  958. level = "A"
  959. topname = "超级订阅临期用户"
  960. pcode = "614"
  961. if sourceId == 1 {
  962. code = "615"
  963. subname = "60天后到期"
  964. } else if sourceId == 2 {
  965. code = "616"
  966. subname = "45天后到期"
  967. } else if sourceId == 3 {
  968. code = "617"
  969. subname = "15天后到期"
  970. } else {
  971. code = "618"
  972. subname = "7天后到期"
  973. }
  974. } else if item == "allocation" {
  975. pcode = "532"
  976. code = "671"
  977. level = "D"
  978. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": pcode}, "", "")
  979. if pcodeData != nil && len(*pcodeData) > 0 {
  980. topname = common.ObjToString((*pcodeData)["name"])
  981. }
  982. pcodeData = TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": code}, "", "")
  983. if pcodeData != nil && len(*pcodeData) > 0 {
  984. subname = common.ObjToString((*pcodeData)["name"])
  985. }
  986. } else if item == "rebind" {
  987. pcode = "532"
  988. code = "670"
  989. level = "D"
  990. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": pcode}, "", "")
  991. if pcodeData != nil && len(*pcodeData) > 0 {
  992. topname = common.ObjToString((*pcodeData)["name"])
  993. }
  994. pcodeData = TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": code}, "", "")
  995. if pcodeData != nil && len(*pcodeData) > 0 {
  996. subname = common.ObjToString((*pcodeData)["name"])
  997. }
  998. } else if item == "invite" {
  999. codeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": sourceCode}, "", "")
  1000. if codeData != nil && len(*codeData) > 0 {
  1001. pcode = common.ObjToString((*codeData)["pcode"])
  1002. code = common.ObjToString((*codeData)["code"])
  1003. level = common.ObjToString((*codeData)["clue_level"])
  1004. subname = common.ObjToString((*codeData)["name"])
  1005. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": pcode}, "", "")
  1006. if pcodeData != nil && len(*pcodeData) > 0 {
  1007. topname = common.ObjToString((*pcodeData)["name"])
  1008. }
  1009. }
  1010. } else {
  1011. if sourceCode != "" {
  1012. codeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"source": sourceCode}, "", "")
  1013. if codeData != nil && len(*codeData) > 0 {
  1014. pcode = common.ObjToString((*codeData)["pcode"])
  1015. code = common.ObjToString((*codeData)["code"])
  1016. level = common.ObjToString((*codeData)["clue_level"])
  1017. subname = common.ObjToString((*codeData)["name"])
  1018. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": pcode}, "", "")
  1019. if pcodeData != nil && len(*pcodeData) > 0 {
  1020. topname = common.ObjToString((*pcodeData)["name"])
  1021. }
  1022. }
  1023. }
  1024. }
  1025. return
  1026. }
  1027. // 获取自动分配的人
  1028. func autoDraw(mode, cluename, phone string, isGroup, isCommerce int) (positionId int64, seatNumber, saleName string, saleData []map[string]interface{}, isOk, isFreeze bool, noticePositionId int64) {
  1029. isOk = false
  1030. isFreeze = false
  1031. if TiDb.Count("dwd_f_crm_clue_info", map[string]interface{}{"phone": phone, "is_assign": 1}) == 0 { //线索没销售进入,有销售走分配次数最少的逻辑
  1032. if isGroup == 0 && isCommerce == 1 && cluename != "" { //非集团在工商库线索名不为空
  1033. //cdata := TiDb.Find("dwd_f_crm_clue_info", map[string]interface{}{"cluename": cluename, "is_assign": 1}, "", "", -1, -1)
  1034. cdata := TiDb.SelectBySql(`select position_id,MAX(trail_time) as trail_time,max(last_ring_time) as last_ring_time,seatNumber,count(id) as count from dwd_f_crm_clue_info where cluename=? and is_assign =1 GROUP BY position_id `, cluename)
  1035. if cdata != nil && len(*cdata) > 0 { //找到了公司有人在跟进
  1036. isOk = true
  1037. pdata := TiDb.SelectBySql(`select * from dwd_f_crm_personnel_management where seat_number is not null and seat_number != ""`)
  1038. if pdata == nil {
  1039. positionId = 0
  1040. seatNumber = ""
  1041. saleName = ""
  1042. return
  1043. }
  1044. saleData = *pdata
  1045. cdataNew := []map[string]interface{}{}
  1046. if len(*cdata) > 1 {
  1047. //可能有多个人跟进
  1048. personMap := map[int64]bool{}
  1049. upperLimitPersonMap := map[int64]bool{}
  1050. upperLimitCdataNew := []map[string]interface{}{}
  1051. upperLimitCount := 0
  1052. isFull := false
  1053. for _, m := range *cdata {
  1054. positionid1 := gconv.Int64(m["position_id"])
  1055. for _, v := range *pdata {
  1056. resign := common.IntAll(v["resign"])
  1057. assign_type := common.IntAll(v["assign_type"])
  1058. role_id := common.IntAll(v["role_id"])
  1059. if positionid1 == common.Int64All(v["position_id"]) {
  1060. if resign == 0 && (assign_type == 1 || role_id == 8) {
  1061. if !FindUpperLimit(gconv.String(positionid1), mode, true) {
  1062. personMap[positionid1] = true
  1063. m["saleName"] = common.ObjToString(v["name"])
  1064. cdataNew = append(cdataNew, m)
  1065. } else {
  1066. upperLimitCount++
  1067. upperLimitPersonMap[positionid1] = true
  1068. m["saleName"] = common.ObjToString(v["name"])
  1069. upperLimitCdataNew = append(upperLimitCdataNew, m)
  1070. }
  1071. }
  1072. }
  1073. }
  1074. }
  1075. if len(cdataNew) == 0 && len(upperLimitCdataNew) > 0 {
  1076. //没有达上限的人数为0 全部都是达上限的
  1077. personMap = upperLimitPersonMap
  1078. cdataNew = upperLimitCdataNew
  1079. isFull = true
  1080. }
  1081. //查询是否都有没有离职
  1082. if len(personMap) != 0 && len(cdataNew) > 0 {
  1083. layout := "2006-01-02 15:04:05"
  1084. //有人没有离职
  1085. data := map[string]interface{}{}
  1086. trailTime := int64(0)
  1087. for _, m := range cdataNew {
  1088. currentTime := int64(0)
  1089. if gconv.String(m["trail_time"]) == "" {
  1090. continue
  1091. }
  1092. t, _ := time.Parse(layout, gconv.String(m["trail_time"]))
  1093. currentTime = t.Unix()
  1094. if currentTime > trailTime {
  1095. trailTime = currentTime
  1096. data = m
  1097. }
  1098. }
  1099. if trailTime == 0 {
  1100. //需要查看通话记录
  1101. lastRingTime := int64(0)
  1102. for _, m := range cdataNew {
  1103. currentTime := int64(0)
  1104. if gconv.String(m["last_ring_time"]) == "" {
  1105. continue
  1106. }
  1107. t, _ := time.Parse(layout, gconv.String(m["last_ring_time"]))
  1108. currentTime = t.Unix()
  1109. if currentTime > lastRingTime {
  1110. lastRingTime = currentTime
  1111. data = m
  1112. }
  1113. }
  1114. if lastRingTime != 0 {
  1115. positionId = common.Int64All(data["position_id"])
  1116. noticePositionId = positionId
  1117. seatNumber = common.ObjToString(data["seatNumber"])
  1118. saleName = common.ObjToString(data["saleName"])
  1119. if isFull {
  1120. isFreeze = true
  1121. noticePositionId = positionId
  1122. positionId = 0
  1123. seatNumber = ""
  1124. saleName = ""
  1125. }
  1126. return
  1127. } else {
  1128. count := 0
  1129. //线索数量判断
  1130. for i, v := range cdataNew {
  1131. if i == 0 {
  1132. count = gconv.Int(v["count"])
  1133. data = v
  1134. } else {
  1135. if count < gconv.Int(v["count"]) {
  1136. count = gconv.Int(v["count"])
  1137. data = v
  1138. }
  1139. }
  1140. }
  1141. positionId = common.Int64All(data["position_id"])
  1142. noticePositionId = positionId
  1143. saleName = common.ObjToString(data["saleName"])
  1144. seatNumber = common.ObjToString(data["seatNumber"])
  1145. if isFull {
  1146. isFreeze = true
  1147. noticePositionId = positionId
  1148. positionId = 0
  1149. seatNumber = ""
  1150. saleName = ""
  1151. }
  1152. return
  1153. }
  1154. } else {
  1155. positionId = common.Int64All(data["position_id"])
  1156. noticePositionId = positionId
  1157. seatNumber = common.ObjToString(data["seatNumber"])
  1158. saleName = common.ObjToString(data["saleName"])
  1159. if isFull {
  1160. isFreeze = true
  1161. noticePositionId = positionId
  1162. positionId = 0
  1163. seatNumber = ""
  1164. saleName = ""
  1165. }
  1166. return
  1167. }
  1168. }
  1169. } else {
  1170. //只有一人跟进
  1171. //(1)该销售人员未离职,则继续分配给该销售人员(保持现状);
  1172. //该销售人员已离职,则随机分配
  1173. positionId = common.Int64All((*cdata)[0]["position_id"])
  1174. noticePositionId = positionId
  1175. seatNumber = common.ObjToString((*cdata)[0]["seatNumber"])
  1176. for _, v := range *pdata {
  1177. resign := common.IntAll(v["resign"])
  1178. assign_type := common.IntAll(v["assign_type"])
  1179. role_id := common.IntAll(v["role_id"])
  1180. if positionId == common.Int64All(v["position_id"]) {
  1181. if resign == 0 && (assign_type == 1 || role_id == 8) {
  1182. if FindUpperLimit(gconv.String(positionId), mode, true) {
  1183. isFreeze = true
  1184. positionId = 0
  1185. seatNumber = ""
  1186. saleName = ""
  1187. break
  1188. } else {
  1189. saleName = common.ObjToString(v["name"])
  1190. return
  1191. }
  1192. }
  1193. }
  1194. }
  1195. }
  1196. }
  1197. }
  1198. }
  1199. positionId = 0
  1200. seatNumber = ""
  1201. saleName = ""
  1202. query := `select * from dwd_f_crm_personnel_management where assign_type = 1 and`
  1203. if mode == "A" {
  1204. query += ` assign_level like "%A%"`
  1205. } else if mode == "B" {
  1206. query += ` assign_level like "%B%"`
  1207. } else if mode == "D" {
  1208. query += ` assign_level like "%D%"`
  1209. } else if mode == "C" {
  1210. query += ` assign_level like "%C%"`
  1211. } else if mode == "S" {
  1212. query += ` assign_level like "%S%"`
  1213. }
  1214. data := TiDb.SelectBySql(query)
  1215. if data != nil && len(*data) > 0 {
  1216. saleData = *data
  1217. sql := "select * from dwd_f_crm_clue_autodraw_record where clue_level = ?"
  1218. countData := TiDb.SelectBySql(sql, mode)
  1219. if countData != nil && len(*countData) > 0 {
  1220. for _, v := range *data {
  1221. isOk := false //判断是否有新员工
  1222. for _, vv := range *countData {
  1223. if common.Int64All(v["position_id"]) == common.Int64All(vv["position_id"]) {
  1224. if common.IntAll(v["resign"]) == 0 {
  1225. vv["status"] = 1
  1226. } else {
  1227. vv["status"] = 2
  1228. }
  1229. isOk = true
  1230. }
  1231. }
  1232. if !isOk { //有新员工直接分给新员工
  1233. positionId = common.Int64All(v["position_id"])
  1234. saleName = common.ObjToString(v["name"])
  1235. log.Println("新员工, ", positionId, saleName)
  1236. rData := TiDb.FindOne("dwd_f_crm_clue_autodraw_record", map[string]interface{}{"clue_level": mode}, "", "count desc")
  1237. TiDb.Insert("dwd_f_crm_clue_autodraw_record", map[string]interface{}{
  1238. "position_id": positionId,
  1239. "clue_level": mode,
  1240. "count": common.Int64All((*rData)["count"]),
  1241. })
  1242. break
  1243. }
  1244. }
  1245. if positionId == 0 {
  1246. res := int64(0)
  1247. countres := 0
  1248. for _, v := range *countData {
  1249. if common.IntAll(v["status"]) == 1 {
  1250. if FindUpperLimit(gconv.String(v["position_id"]), mode, false) {
  1251. continue
  1252. }
  1253. if countres == 0 {
  1254. res = common.Int64All(v["count"])
  1255. positionId = common.Int64All(v["position_id"])
  1256. } else {
  1257. if common.Int64All(v["count"]) <= res {
  1258. res = common.Int64All(v["count"])
  1259. positionId = common.Int64All(v["position_id"])
  1260. }
  1261. }
  1262. countres++
  1263. }
  1264. }
  1265. log.Println(444, res)
  1266. }
  1267. } else {
  1268. for _, kv := range *data {
  1269. positionId1 := gconv.String(kv["position_id"])
  1270. if !FindUpperLimit(positionId1, "", false) {
  1271. positionId = common.Int64All(kv["position_id"])
  1272. saleName = common.ObjToString(kv["name"])
  1273. rData := TiDb.FindOne("dwd_f_crm_clue_autodraw_record", map[string]interface{}{"clue_level": mode}, "", "count desc")
  1274. if rData != nil && len(*rData) > 0 {
  1275. TiDb.Insert("dwd_f_crm_clue_autodraw_record", map[string]interface{}{
  1276. "position_id": positionId,
  1277. "clue_level": mode,
  1278. "count": common.Int64All((*rData)["count"]),
  1279. })
  1280. } else {
  1281. TiDb.Insert("dwd_f_crm_clue_autodraw_record", map[string]interface{}{
  1282. "position_id": positionId,
  1283. "clue_level": mode,
  1284. "count": 0,
  1285. })
  1286. }
  1287. break
  1288. }
  1289. }
  1290. }
  1291. for _, v := range *data {
  1292. if positionId == common.Int64All(v["position_id"]) {
  1293. seatNumber = common.ObjToString(v["seat_number"])
  1294. saleName = common.ObjToString(v["name"])
  1295. }
  1296. }
  1297. }
  1298. return
  1299. }
  1300. func getPositionId(phone string) (positionId int64) {
  1301. userData, ok := Mgo.FindOne("user", map[string]interface{}{"s_phone": phone})
  1302. if ok && userData != nil && len(*userData) > 0 {
  1303. userId := common.Int64All((*userData)["base_user_id"])
  1304. positionData := Base.FindOne("base_position", map[string]interface{}{"type": 1, "ent_id": db.EntId, "user_id": userId}, "", "") //TODO ent_id
  1305. if positionData != nil && len(*positionData) > 0 {
  1306. positionId = common.Int64All((*positionData)["id"])
  1307. }
  1308. }
  1309. return
  1310. }
  1311. func GetCompanyType(companyName string, uId string) (int, int) {
  1312. isGroup, isCommerce := 0, 0
  1313. uCount := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", "")
  1314. if uCount != nil && len(*uCount) > 0 {
  1315. isUpdate := gconv.Int64((*uCount)["is_artificially_modified"])
  1316. if isUpdate == 1 {
  1317. companyName = gconv.String((*uCount)["cluename"])
  1318. }
  1319. if companyName == "" {
  1320. companyName = gconv.String((*uCount)["cluename"])
  1321. }
  1322. }
  1323. if companyName == "" {
  1324. return isGroup, isCommerce
  1325. }
  1326. //是否是集团
  1327. if c := TiDb.CountBySql(`select count(1) from group_company_name where company_name=?`, companyName); c > 0 {
  1328. isGroup = 1
  1329. }
  1330. //是否在工商库
  1331. if companyName != "" {
  1332. if c := MgoQyxy.Count("qyxy_std", map[string]interface{}{"company_name": companyName, "company_type": map[string]interface{}{"$ne": "个体工商户"}}); c > 0 {
  1333. isCommerce = 1
  1334. }
  1335. }
  1336. return isGroup, isCommerce
  1337. }
  1338. // 查询是否达上限
  1339. func FindUpperLimit(positionId string, level string, isAdd bool) bool {
  1340. if positionId == "" {
  1341. return false
  1342. }
  1343. isFull := TiDb.CountBySql(`select count(1) from dwd_f_crm_clue_info where position_id=? and is_assign=1 and is_transfer != 1 `, positionId) >= db.AllocationCap
  1344. if isFull && isAdd && level != "" {
  1345. TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_autodraw_record set count = count + 1 where position_id = ? and clue_level = ?`, positionId, level)
  1346. }
  1347. return isFull
  1348. }
  1349. func getSeatNumberPositionId(seatNumber string) (positionId int64) {
  1350. saleData := TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{"seat_number": seatNumber}, "", "")
  1351. if saleData != nil && len(*saleData) > 0 {
  1352. positionId = common.Int64All((*saleData)["position_id"])
  1353. }
  1354. return
  1355. }
  1356. // 重新关注用户处理
  1357. func rebind() {
  1358. log.Println("重新关注用户处理开始")
  1359. //判断节假日
  1360. runOk := getRunOk()
  1361. if !runOk {
  1362. log.Println("不是工作日,任务暂停")
  1363. return
  1364. }
  1365. rebindTimeEnd := cfg.RebindTime
  1366. sql := fmt.Sprintf(`select * from freeClubSign where sub_again_date > "%s" order by sub_again_date asc`, rebindTimeEnd)
  1367. data := BiService.SelectBySql(sql)
  1368. if data != nil && *data != nil && len(*data) > 0 {
  1369. for _, v := range *data {
  1370. rebindTimeEnd = common.ObjToString(v["sub_again_date"])
  1371. registedates, _ := time.Parse(date.Date_Full_Layout, gconv.String(v["register_time"]))
  1372. if time.Now().Unix()-registedates.Unix() > int64(db.RegTimes)*86400 {
  1373. ok1, ok2, _ := FormatData(v, "rebind")
  1374. if !ok1 {
  1375. log.Println("线索卡点", "allocation", v, rebindTimeEnd)
  1376. } else {
  1377. if !ok2 {
  1378. log.Println("用户分配已达上限", "allocation", v, rebindTimeEnd)
  1379. }
  1380. }
  1381. }
  1382. }
  1383. cfg.RebindTime = rebindTimeEnd
  1384. common.WriteSysConfig(&cfg)
  1385. }
  1386. log.Println("重新关注用户处理结束")
  1387. }
  1388. // 活跃用户处理
  1389. func activeUsers() {
  1390. log.Println("活跃用户处理开始")
  1391. //判断节假日
  1392. runOk := getRunOk()
  1393. if !runOk {
  1394. log.Println("不是工作日,任务暂停")
  1395. return
  1396. }
  1397. //活跃用户查询
  1398. activeTimeEnd := cfg.ActiveTime
  1399. sql := fmt.Sprintf(`select * from freeClubSign where act_again_date > "%s" order by act_again_date asc`, activeTimeEnd)
  1400. data := BiService.SelectBySql(sql)
  1401. if data != nil && *data != nil && len(*data) > 0 {
  1402. for _, v := range *data {
  1403. activeTimeEnd = common.ObjToString(v["act_again_date"])
  1404. ok1, ok2, _ := FormatData(v, "allocation")
  1405. log.Println(v, "allocation", ok1, ok2)
  1406. if !ok1 {
  1407. log.Println("线索卡点", "allocation", v, activeTimeEnd)
  1408. } else {
  1409. if !ok2 {
  1410. log.Println("用户分配已达上限", "allocation", v, activeTimeEnd)
  1411. }
  1412. }
  1413. }
  1414. cfg.ActiveTime = activeTimeEnd
  1415. common.WriteSysConfig(&cfg)
  1416. }
  1417. log.Println("活跃用户处理结束")
  1418. }
  1419. // 安博会邮件
  1420. /*func ABHEmail(source string, data []map[string]interface{}) {
  1421. fileName, detailName, email, dir := "", "", "", ""
  1422. var err error
  1423. switch source {
  1424. case "user":
  1425. xlsxArr := []string{"姓名", "联系方式", "邮箱", "公司名称", "咨询需求", "销售线索来源", "具体来源", "留资时间"}
  1426. batch := FindBatch("abh")
  1427. fileName, detailName = "安博会用户注册销售线索 "+time.Now().Format(date.Date_Short_Layout)+"-"+batch, ""
  1428. xf := xlsx.NewFile()
  1429. style := xlsx.NewStyle()
  1430. style.Font.Size = 12
  1431. style.Font.Bold = true
  1432. style.Alignment.Vertical = "center"
  1433. style.Alignment.Horizontal = "center"
  1434. detailName = "今日新增安博会销售线索,请查收附件,及时跟进。"
  1435. sh, _ := xf.AddSheet("线索数据")
  1436. row1 := sh.AddRow()
  1437. for _, x := range xlsxArr {
  1438. cell := row1.AddCell()
  1439. cell.SetString(x)
  1440. cell.SetStyle(style)
  1441. }
  1442. for _, v := range data {
  1443. row := sh.AddRow()
  1444. row.AddCell().SetString(common.ObjToString(v["name"]))
  1445. row.AddCell().SetString(common.ObjToString(v["phone"]))
  1446. row.AddCell().SetString(common.ObjToString(v["email"]))
  1447. row.AddCell().SetString(common.ObjToString(v["company_name"]))
  1448. row.AddCell().SetString("")
  1449. row.AddCell().SetString("")
  1450. row.AddCell().SetString("安博会用户注册")
  1451. row.AddCell().SetString(gconv.Time(gconv.Int64(v["createtime"])).Format(date.Date_Full_Layout))
  1452. //增加客户需求
  1453. }
  1454. dir = "./xlsx/abh/" + fileName + ".xlsx"
  1455. err = xf.Save(dir)
  1456. case "saleLeads":
  1457. xlsxArr := []string{"用户昵称", "姓名", "公司名称", "职位", "联系人电话", "购买条数", "用户邮箱", "已支付订单信息", "未支付订单类型", "销售线索来源", "具体来源", "数据需求"}
  1458. batch := FindBatch("abh")
  1459. fileName, detailName = time.Now().Format(date.Date_Short_Layout)+"-"+batch+"安博会电销销售线索", ""
  1460. xf := xlsx.NewFile()
  1461. style := xlsx.NewStyle()
  1462. style.Font.Size = 12
  1463. style.Font.Bold = true
  1464. style.Alignment.Vertical = "center"
  1465. style.Alignment.Horizontal = "center"
  1466. detailName = "今日新增安博会销售线索,请查收附件,及时跟进。"
  1467. sh, _ := xf.AddSheet("线索数据")
  1468. row1 := sh.AddRow()
  1469. for _, x := range xlsxArr {
  1470. cell := row1.AddCell()
  1471. cell.SetString(x)
  1472. cell.SetStyle(style)
  1473. }
  1474. filterArr := []string{"-pc", "-app", "-wx", "-h5"}
  1475. sourceMap := map[string]string{} //根据留资维表:tidb/Jianyu_subjectdb/d_saleleads_code
  1476. saleSource := TiDb.SelectBySql(`SELECT source,name FROM d_saleleads_code WHERE is_delete = 1`)
  1477. if saleSource != nil && len(*saleSource) > 0 {
  1478. for _, v := range *saleSource {
  1479. source := common.ObjToString(v["source"])
  1480. name := common.ObjToString(v["name"])
  1481. for _, s := range filterArr {
  1482. name = strings.ReplaceAll(name, s, "")
  1483. }
  1484. sourceMap[source] = name
  1485. }
  1486. }
  1487. for _, v := range data {
  1488. sources := common.ObjToString(v["source"])
  1489. interest := common.ObjToString(v["interest"])
  1490. row := sh.AddRow()
  1491. row.AddCell().SetString(common.ObjToString(v["usernickname"]))
  1492. row.AddCell().SetString(common.ObjToString(v["name"]))
  1493. row.AddCell().SetString(common.ObjToString(v["company"]))
  1494. row.AddCell().SetString(common.ObjToString(v["position"]))
  1495. row.AddCell().SetString(common.ObjToString(v["phone"]))
  1496. row.AddCell().SetValue(0)
  1497. row.AddCell().SetString(common.ObjToString(v["email"]))
  1498. row.AddCell().SetString("")
  1499. row.AddCell().SetString("")
  1500. row.AddCell().SetString(sourceMap[sources])
  1501. row.AddCell().SetString(interest)
  1502. row.AddCell().SetString(common.ObjToString(v["data_requirement"]))
  1503. //增加客户需求
  1504. }
  1505. dir = "./xlsx/abh/" + fileName + ".xlsx"
  1506. err = xf.Save(dir)
  1507. case "big":
  1508. xlsxArr := []string{"用户昵称", "姓名", "公司名称", "职位", "联系人电话", "购买条数", "用户邮箱", "已支付订单信息", "未支付订单类型", "销售线索来源", "具体来源", "数据需求"}
  1509. batch := FindBatch("abh")
  1510. fileName, detailName = time.Now().Format(date.Date_Short_Layout)+"-"+batch+"安博会大客户销售线索", ""
  1511. xf := xlsx.NewFile()
  1512. style := xlsx.NewStyle()
  1513. style.Font.Size = 12
  1514. style.Font.Bold = true
  1515. style.Alignment.Vertical = "center"
  1516. style.Alignment.Horizontal = "center"
  1517. detailName = "今日新增安博会销售线索,请查收附件,及时跟进。"
  1518. sh, _ := xf.AddSheet("线索数据")
  1519. row1 := sh.AddRow()
  1520. for _, x := range xlsxArr {
  1521. cell := row1.AddCell()
  1522. cell.SetString(x)
  1523. cell.SetStyle(style)
  1524. }
  1525. for _, v := range data {
  1526. row := sh.AddRow()
  1527. row.AddCell().SetString(common.ObjToString(v["usernickname"]))
  1528. row.AddCell().SetString(common.ObjToString(v["username"]))
  1529. row.AddCell().SetString(common.ObjToString(v["company"]))
  1530. row.AddCell().SetString(common.ObjToString(v["job"]))
  1531. row.AddCell().SetString(common.ObjToString(v["phone"]))
  1532. row.AddCell().SetValue(common.IntAll(v["data_count"]))
  1533. row.AddCell().SetString(common.ObjToString(v["email"]))
  1534. row.AddCell().SetString(common.ObjToString(v["payorderinfo"]))
  1535. row.AddCell().SetString(common.ObjToString(v["unpayorderinfo"]))
  1536. row.AddCell().SetString(common.ObjToString(v["source"]))
  1537. row.AddCell().SetString(common.ObjToString(v["interest"]))
  1538. row.AddCell().SetString(common.ObjToString(v["data_requirement"]))
  1539. //增加客户需求
  1540. }
  1541. dir = "./xlsx/abh/" + fileName + ".xlsx"
  1542. err = xf.Save(dir)
  1543. case "advisory":
  1544. xlsxArr := []string{"姓名", "联系方式", "邮箱", "公司名称", "咨询需求", "销售线索来源", "具体来源", "留资时间"}
  1545. //排序:首先按照“联系方式”排序,即同1个用户的留资放在一起,其次按照留资时间正序排序。
  1546. batch := FindBatch("abh")
  1547. fileName, detailName = "安博会咨询服务销售线索 "+time.Now().Format(date.Date_Short_Layout)+"-"+batch, ""
  1548. xf := xlsx.NewFile()
  1549. style := xlsx.NewStyle()
  1550. style.Font.Size = 12
  1551. style.Font.Bold = true
  1552. style.Alignment.Vertical = "center"
  1553. style.Alignment.Horizontal = "center"
  1554. detailName = "今日新增安博会销售线索,请查收附件,及时跟进。"
  1555. sh, _ := xf.AddSheet("线索数据")
  1556. row1 := sh.AddRow()
  1557. for _, x := range xlsxArr {
  1558. cell := row1.AddCell()
  1559. cell.SetString(x)
  1560. cell.SetStyle(style)
  1561. }
  1562. for _, v := range data {
  1563. row := sh.AddRow()
  1564. row.AddCell().SetString(common.ObjToString(v["username"]))
  1565. row.AddCell().SetString(common.ObjToString(v["phone"]))
  1566. row.AddCell().SetString(common.ObjToString(v["email"]))
  1567. row.AddCell().SetString(common.ObjToString(v["company"]))
  1568. row.AddCell().SetString(common.ObjToString(v["data_requirement"]))
  1569. row.AddCell().SetString(common.ObjToString(v["source"]))
  1570. row.AddCell().SetString(common.ObjToString(v["interest"]))
  1571. row.AddCell().SetString(gconv.Time(gconv.Int64(v["createtime"])).Format(date.Date_Full_Layout))
  1572. }
  1573. dir = "./xlsx/abh/" + fileName + ".xlsx"
  1574. err = xf.Save(dir)
  1575. case "market":
  1576. xlsxArr := []string{"姓名", "联系人电话", "公司名称", "职位", "部门", "销售线索来源", "留资时间"}
  1577. batch := FindBatch("abh")
  1578. fileName, detailName = "安博会商务合作销售线索 "+time.Now().Format(date.Date_Short_Layout)+"-"+batch, ""
  1579. xf := xlsx.NewFile()
  1580. style := xlsx.NewStyle()
  1581. style.Font.Size = 12
  1582. style.Font.Bold = true
  1583. style.Alignment.Vertical = "center"
  1584. style.Alignment.Horizontal = "center"
  1585. detailName = "今日新增安博会销售线索,请查收附件,及时跟进。"
  1586. sh, _ := xf.AddSheet("线索数据")
  1587. row1 := sh.AddRow()
  1588. for _, x := range xlsxArr {
  1589. cell := row1.AddCell()
  1590. cell.SetString(x)
  1591. cell.SetStyle(style)
  1592. }
  1593. for _, v := range data {
  1594. row := sh.AddRow()
  1595. row.AddCell().SetString(common.ObjToString(v["username"]))
  1596. row.AddCell().SetString(common.ObjToString(v["phone"]))
  1597. row.AddCell().SetString(common.ObjToString(v["company"]))
  1598. row.AddCell().SetString(common.ObjToString(v["job"]))
  1599. row.AddCell().SetString(common.ObjToString(v["branch"]))
  1600. row.AddCell().SetString(common.ObjToString(v["source"]))
  1601. row.AddCell().SetString(gconv.Time(gconv.Int64(v["createtime"])).Format(date.Date_Full_Layout))
  1602. //增加客户需求
  1603. }
  1604. dir = "./xlsx/abh/" + fileName + ".xlsx"
  1605. err = xf.Save(dir)
  1606. }
  1607. email = db.AbhEmail
  1608. if err != nil {
  1609. log.Println("xls error", err, dir)
  1610. } else {
  1611. gmail := &mail.GmailAuth{
  1612. SmtpHost: "smtp.exmail.qq.com",
  1613. SmtpPort: 465,
  1614. User: "public03@topnet.net.cn",
  1615. Pwd: "ue9Rg9Sf4CVtdm5a",
  1616. }
  1617. status := mail.GSendMail_q("剑鱼标讯", email, "", "", fileName, detailName, dir, fileName+".xlsx", gmail)
  1618. if status {
  1619. log.Println("send mail success", fileName, email)
  1620. }
  1621. }
  1622. }*/
  1623. // 安防邮件
  1624. func AFEmail(data []map[string]interface{}) {
  1625. xlsxArr := []string{"姓名", "联系方式", "邮箱", "公司名称", "咨询需求", "销售线索来源", "具体来源", "留资时间"}
  1626. batch := FindBatch("af")
  1627. fileName, detailName := "安防销售线索 "+time.Now().Format(date.Date_Short_Layout)+"-"+batch, ""
  1628. xf := xlsx.NewFile()
  1629. style := xlsx.NewStyle()
  1630. style.Font.Size = 12
  1631. style.Font.Bold = true
  1632. style.Alignment.Vertical = "center"
  1633. style.Alignment.Horizontal = "center"
  1634. detailName = "今日新增安防销售线索,请查收附件,及时跟进。"
  1635. sh, _ := xf.AddSheet("线索数据")
  1636. row1 := sh.AddRow()
  1637. for _, x := range xlsxArr {
  1638. cell := row1.AddCell()
  1639. cell.SetString(x)
  1640. cell.SetStyle(style)
  1641. }
  1642. for _, v := range data {
  1643. row := sh.AddRow()
  1644. row.AddCell().SetString(common.ObjToString(v["name"]))
  1645. row.AddCell().SetString(common.ObjToString(v["phone"]))
  1646. row.AddCell().SetString(common.ObjToString(v["email"]))
  1647. row.AddCell().SetString(common.ObjToString(v["company_name"]))
  1648. row.AddCell().SetString("")
  1649. row.AddCell().SetString("")
  1650. row.AddCell().SetString("首次使用安防新注册")
  1651. row.AddCell().SetString(gconv.String(v["time"]))
  1652. //增加客户需求
  1653. }
  1654. email := db.AfEmail
  1655. dir := "./xlsx/af/" + fileName + ".xlsx"
  1656. err := xf.Save(dir)
  1657. if err != nil {
  1658. log.Println("xls error", err, dir)
  1659. } else {
  1660. gmail := &mail.GmailAuth{
  1661. SmtpHost: db.Mail.SmtpHost,
  1662. SmtpPort: db.Mail.SmtpPort,
  1663. User: db.Mail.User,
  1664. Pwd: db.Mail.Pwd,
  1665. }
  1666. status := mail.GSendMail_q("剑鱼标讯", email, "", "", fileName, detailName, dir, "", gmail)
  1667. if status {
  1668. log.Println("send mail success", fileName, email)
  1669. }
  1670. }
  1671. }
  1672. func ABHEmail(source string, data []map[string]interface{}) {
  1673. var fileName, detailName, dir string
  1674. var err error
  1675. xlsxArr, detailName := getExcelHeaderAndDetail(source)
  1676. batch := FindBatch("abh")
  1677. fileName = fmt.Sprintf("%s %s-%s", getFileNamePrefix(source), time.Now().Format(date.Date_Short_Layout), batch)
  1678. xf := xlsx.NewFile()
  1679. style := createExcelStyle()
  1680. sh, _ := xf.AddSheet("线索数据")
  1681. addHeaderRow(sh, xlsxArr, style)
  1682. switch source {
  1683. case "user":
  1684. err = populateUserData(sh, data)
  1685. case "saleLeads", "big":
  1686. err = populateSaleLeadData(sh, data)
  1687. case "advisory":
  1688. err = populateAdvisoryData(sh, data)
  1689. case "market", "selection":
  1690. err = populateMarketData(sh, data)
  1691. }
  1692. if err == nil {
  1693. dir = fmt.Sprintf("./xlsx/abh/%s.xlsx", fileName)
  1694. err = xf.Save(dir)
  1695. }
  1696. sendEmailIfSuccessful(err, fileName, detailName, dir)
  1697. }
  1698. func getExcelHeaderAndDetail(source string) ([]string, string) {
  1699. switch source {
  1700. case "user":
  1701. return []string{"姓名", "联系方式", "邮箱", "公司名称", "咨询需求", "销售线索来源", "具体来源", "留资时间"}, "今日新增安博会销售线索,请查收附件,及时跟进。"
  1702. case "saleLeads":
  1703. return []string{"用户昵称", "姓名", "公司名称", "职位", "联系人电话", "购买条数", "用户邮箱", "已支付订单信息", "未支付订单类型", "销售线索来源", "具体来源", "数据需求"}, "今日新增安博会销售线索,请查收附件,及时跟进。"
  1704. case "big":
  1705. return []string{"用户昵称", "姓名", "公司名称", "职位", "联系人电话", "购买条数", "用户邮箱", "已支付订单信息", "未支付订单类型", "销售线索来源", "具体来源", "数据需求"}, "今日新增安博会销售线索,请查收附件,及时跟进。"
  1706. case "advisory":
  1707. return []string{"姓名", "联系方式", "邮箱", "公司名称", "咨询需求", "销售线索来源", "具体来源", "留资时间"}, "今日新增安博会销售线索,请查收附件,及时跟进。"
  1708. case "market":
  1709. return []string{"姓名", "联系人电话", "公司名称", "职位", "部门", "销售线索来源", "留资时间"}, "今日新增安博会销售线索,请查收附件,及时跟进。"
  1710. case "selection":
  1711. return []string{"姓名", "联系人电话", "公司名称", "职位", "部门", "销售线索来源", "留资时间"}, "今日新增安博会销售线索,请查收附件,及时跟进。"
  1712. default:
  1713. return nil, ""
  1714. }
  1715. }
  1716. func getFileNamePrefix(source string) string {
  1717. switch source {
  1718. case "user":
  1719. return "安博会用户注册销售线索"
  1720. case "saleLeads":
  1721. return "安博会电销销售线索"
  1722. case "big":
  1723. return "安博会大客户销售线索"
  1724. case "advisory":
  1725. return "安博会咨询服务销售线索"
  1726. case "market":
  1727. return "安博会商务合作销售线索"
  1728. default:
  1729. return "未知线索"
  1730. }
  1731. }
  1732. func createExcelStyle() *xlsx.Style {
  1733. style := xlsx.NewStyle()
  1734. style.Font.Size = 12
  1735. style.Font.Bold = true
  1736. style.Alignment.Vertical = "center"
  1737. style.Alignment.Horizontal = "center"
  1738. return style
  1739. }
  1740. func addHeaderRow(sh *xlsx.Sheet, headers []string, style *xlsx.Style) {
  1741. row := sh.AddRow()
  1742. for _, header := range headers {
  1743. cell := row.AddCell()
  1744. cell.SetString(header)
  1745. cell.SetStyle(style)
  1746. }
  1747. }
  1748. func populateUserData(sh *xlsx.Sheet, data []map[string]interface{}) error {
  1749. for _, v := range data {
  1750. row := sh.AddRow()
  1751. row.AddCell().SetString(common.ObjToString(v["name"]))
  1752. row.AddCell().SetString(common.ObjToString(v["phone"]))
  1753. row.AddCell().SetString(common.ObjToString(v["email"]))
  1754. row.AddCell().SetString(common.ObjToString(v["company_name"]))
  1755. row.AddCell().SetString("")
  1756. row.AddCell().SetString("")
  1757. row.AddCell().SetString("安博会用户注册")
  1758. row.AddCell().SetString(gconv.Time(gconv.Int64(v["createtime"])).Format(date.Date_Full_Layout))
  1759. }
  1760. return nil
  1761. }
  1762. func populateSaleLeadData(sh *xlsx.Sheet, data []map[string]interface{}) error {
  1763. sourceMap := buildSourceMap("运营部")
  1764. for _, v := range data {
  1765. row := sh.AddRow()
  1766. row.AddCell().SetString(common.ObjToString(v["usernickname"]))
  1767. row.AddCell().SetString(common.ObjToString(v["name"]))
  1768. row.AddCell().SetString(common.ObjToString(v["company"]))
  1769. row.AddCell().SetString(common.ObjToString(v["position"]))
  1770. row.AddCell().SetString(common.ObjToString(v["phone"]))
  1771. row.AddCell().SetValue(0)
  1772. row.AddCell().SetString(common.ObjToString(v["email"]))
  1773. row.AddCell().SetString("")
  1774. row.AddCell().SetString("")
  1775. row.AddCell().SetString(sourceMap[common.ObjToString(v["source"])])
  1776. row.AddCell().SetString(common.ObjToString(v["interest"]))
  1777. row.AddCell().SetString(common.ObjToString(v["data_requirement"]))
  1778. }
  1779. return nil
  1780. }
  1781. func populateAdvisoryData(sh *xlsx.Sheet, data []map[string]interface{}) error {
  1782. for _, v := range data {
  1783. row := sh.AddRow()
  1784. row.AddCell().SetString(common.ObjToString(v["username"]))
  1785. row.AddCell().SetString(common.ObjToString(v["phone"]))
  1786. row.AddCell().SetString(common.ObjToString(v["email"]))
  1787. row.AddCell().SetString(common.ObjToString(v["company"]))
  1788. row.AddCell().SetString(common.ObjToString(v["data_requirement"]))
  1789. row.AddCell().SetString(common.ObjToString(v["source"]))
  1790. row.AddCell().SetString(common.ObjToString(v["interest"]))
  1791. row.AddCell().SetString(gconv.Time(gconv.Int64(v["createtime"])).Format(date.Date_Full_Layout))
  1792. }
  1793. return nil
  1794. }
  1795. func populateMarketData(sh *xlsx.Sheet, data []map[string]interface{}) error {
  1796. for _, v := range data {
  1797. row := sh.AddRow()
  1798. row.AddCell().SetString(common.ObjToString(v["username"]))
  1799. row.AddCell().SetString(common.ObjToString(v["phone"]))
  1800. row.AddCell().SetString(common.ObjToString(v["company"]))
  1801. row.AddCell().SetString(common.ObjToString(v["job"]))
  1802. row.AddCell().SetString(common.ObjToString(v["branch"]))
  1803. row.AddCell().SetString(common.ObjToString(v["source"]))
  1804. row.AddCell().SetString(gconv.Time(gconv.Int64(v["createtime"])).Format(date.Date_Full_Layout))
  1805. }
  1806. return nil
  1807. }
  1808. func sendEmailIfSuccessful(err error, fileName, detailName, dir string) {
  1809. if err != nil {
  1810. log.Println("xls error", err, dir)
  1811. return
  1812. }
  1813. email := db.AbhEmail
  1814. gmail := &mail.GmailAuth{
  1815. SmtpHost: db.Mail.SmtpHost,
  1816. SmtpPort: db.Mail.SmtpPort,
  1817. User: db.Mail.User,
  1818. Pwd: db.Mail.Pwd,
  1819. }
  1820. status := mail.GSendMail_q("剑鱼标讯", email, "", "", fileName, detailName, dir, fileName+".xlsx", gmail)
  1821. if status {
  1822. log.Println("send mail success", fileName, email)
  1823. }
  1824. }
  1825. // 邀请用户处理
  1826. func inviteUser() {
  1827. log.Println("邀请用户处理开始")
  1828. //判断节假日
  1829. runOk := getRunOk()
  1830. if !runOk {
  1831. log.Println("不是工作日,任务暂停")
  1832. return
  1833. }
  1834. rebindTimeEnd := cfg.InviteTime
  1835. sql := fmt.Sprintf(`select * from user_statistics where createTime > "%s" order by createTime asc`, rebindTimeEnd)
  1836. //sql := fmt.Sprintf(`select * from user_statistics where createTime = "%s" order by createTime asc`, rebindTimeEnd)
  1837. data := BiService.SelectBySql(sql)
  1838. if data != nil && *data != nil && len(*data) > 0 {
  1839. for _, v := range *data {
  1840. rebindTimeEnd = common.ObjToString(v["createTime"])
  1841. event := gconv.Int64(v["event"])
  1842. switch event {
  1843. case 1:
  1844. v["sourceCode"] = "736"
  1845. case 2:
  1846. v["sourceCode"] = "737"
  1847. case 3:
  1848. v["sourceCode"] = "738"
  1849. }
  1850. v["user_id"] = gconv.String(v["userId"])
  1851. ok1, ok2, _ := FormatData(v, "invite")
  1852. if !ok1 {
  1853. log.Println("线索卡点", "invite", v, rebindTimeEnd)
  1854. } else {
  1855. if !ok2 {
  1856. log.Println("用户分配已达上限", "invite", v, rebindTimeEnd)
  1857. }
  1858. }
  1859. }
  1860. cfg.InviteTime = rebindTimeEnd
  1861. common.WriteSysConfig(&cfg)
  1862. }
  1863. log.Println("邀请用户处理结束")
  1864. }
  1865. // 开年活动
  1866. func nextYearActivity() {
  1867. log.Println("开年活动处理开始")
  1868. //判断节假日
  1869. runOk := getRunOk()
  1870. if !runOk {
  1871. log.Println("不是工作日,任务暂停")
  1872. return
  1873. }
  1874. activityTimeEnd := cfg.ActivityTime
  1875. sql := fmt.Sprintf(`SELECT
  1876. a.*
  1877. FROM
  1878. jyactivities.lottery_user_account a
  1879. INNER JOIN jyactivities.lottery_prize_info b on a.create_time > "%s" and a.prize_id=b.id
  1880. INNER JOIN jyactivities.prize_base_info c on c.name like "%s" and b.pb_id=c.id
  1881. ORDER BY
  1882. a.create_time ASC
  1883. `, activityTimeEnd, "%超级订阅%")
  1884. data := Mysql.SelectBySql(sql)
  1885. if data != nil && *data != nil && len(*data) > 0 {
  1886. for _, v := range *data {
  1887. activityTimeEnd = common.ObjToString(v["create_time"])
  1888. v["sourceCode"] = gconv.Int64(v["active_id"])
  1889. v["user_id"] = gconv.String(v["mgo_user_id"])
  1890. ok1, ok2, _ := FormatData(v, "activity")
  1891. if !ok1 {
  1892. log.Println("线索卡点", "activity", v, activityTimeEnd)
  1893. } else {
  1894. if !ok2 {
  1895. log.Println("开年活动分配已达上限", "activity", v, activityTimeEnd)
  1896. }
  1897. }
  1898. }
  1899. cfg.ActivityTime = activityTimeEnd
  1900. common.WriteSysConfig(&cfg)
  1901. }
  1902. log.Println("开年活动处理结束")
  1903. }