1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603 |
- package main
- import (
- "app.yhyue.com/moapp/jybase/mail"
- "database/sql"
- "encoding/json"
- "fmt"
- "log"
- "strings"
- "time"
- "github.com/gogf/gf/v2/util/gconv"
- "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/date"
- "app.yhyue.com/moapp/jybase/mongodb"
- )
- var (
- CodeLock = map[int64]string{
- 1: "已锁定",
- 0: "/",
- 2: "未锁定",
- 3: "无需锁定",
- }
- )
- func kcSync() {
- log.Println("客户成功系统移交定时任务开始")
- sql := `select * from dwd_f_userbase_order_info where (payable_money > 0 or (payable_money=0 and ( filter like "%分期付款补充权益%" or filter like "%原订单不支持开通多项权益%" or filter like "%权益码兑换%")))
- and data_spec!="dhy4" and order_status=1 and (refund_status!=1 or refund_status is null ) and vip_starttime<"2099-01-01"
- and vip_endtime > "` + time.Now().Format(date.Date_Full_Layout) + `" and autoUpdate > "` + cfg.LastkcTime + `" order by autoUpdate asc`
- //sql := `select * from dwd_f_userbase_order_info where order_code="173943424276" `
- data := TiDb.SelectBySql(sql)
- if data != nil && *data != nil && len(*data) > 0 {
- arr := []string{}
- for _, v := range *data {
- product_type := common.ObjToString(v["product_type"])
- vip_starttime := common.ObjToString(v["vip_starttime"])
- vip_endtime := common.ObjToString(v["vip_endtime"])
- order_change := gconv.Int64(v["order_change"])
- if (product_type == "大会员" || product_type == "企业商机管理") && ((order_change != 0) || (order_change == 0 && TimeStrcount(vip_starttime, vip_endtime) > 95)) {
- orderCode := gconv.String(v["order_code"])
- arr = append(arr, orderCode)
- status := kcJob(v)
- if status == 0 {
- break
- }
- }
- cfg.LastkcTime = common.ObjToString(v["autoUpdate"])
- }
- log.Println(strings.Join(arr, "`,`"))
- }
- common.WriteSysConfig(&cfg)
- log.Println("客户成功系统移交定时任务结束")
- }
- func kcJob(data map[string]interface{}) int {
- nowTime := time.Now().Format(date.Date_Full_Layout)
- uId, entId, clueId, saveMap, status := common.ObjToString(data["uid"]), fmt.Sprint(data["ent_id"]), int64(0), map[string]interface{}{}, 1
- clueData := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", "")
- orderCode := gconv.String(data["order_code"])
- saleDep, orderPositionId, salesperson := FindSaleRecord(orderCode)
- log.Println(data["order_code"], saleDep, orderPositionId, salesperson)
- if clueData == nil || len(*clueData) == 0 {
- if saleDep == "销售部" || saleDep == "市场部" {
- return 1
- }
- //创建线索
- //原始订单获取
- position_id := int64(0)
- orderData := Mysql.FindOne("dataexport_order", map[string]interface{}{
- "order_code": orderCode,
- }, "", "")
- if orderData == nil {
- log.Println("原始订单查询不到:", orderCode)
- }
- userId, cluename, phone := common.ObjToString((*orderData)["user_id"]), "", ""
- seatNumber := ""
- if saleDep == "客户成功组" {
- //新增线索
- position_id = 0
- } else if saleDep == "销售部" {
- log.Println("该订单数据销售部业绩,不会创建线索")
- return 0
- } else {
- position_id = orderPositionId
- //其他信息
- seatNumber = "0000"
- }
- cluename = common.ObjToString(data["company_name"])
- phone = common.ObjToString(data["phone"])
- if cluename == "" {
- cluename = phone
- }
- trailstatus := "08"
- isGroup, isCommerce := GetCompanyType(cluename, uId) //判断是否集团公司、工商库
- clueId = TiDb.Insert("dwd_f_crm_clue_info", map[string]interface{}{
- "userid": userId,
- "uid": uId,
- "is_assign": common.If(position_id > 0, 1, 0),
- "createtime": nowTime,
- "updatetime": nowTime,
- "cluename": cluename,
- "seatNumber": seatNumber,
- "position_id": position_id,
- "top_cluetype": "4",
- "sub_cluetype": "154",
- "trailstatus": trailstatus,
- "name": phone,
- "phone": phone,
- "is_task": 0,
- "taskstatus": 0,
- "company_nature": isGroup,
- "company_verification": isCommerce,
- "FREEZE_TIME": nowTime,
- "label": common.If(position_id > 0, 1, nil),
- })
- if clueId > 0 {
- AddChangeRecord(nil, clueId, gconv.Int64(common.If(position_id > 0, position_id, -1)), "", "创建线索", "系统自动创建", "", nowTime, "", -1)
- clueData = TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", "")
- if clueData == nil || len(*clueData) == 0 {
- return 0
- }
- } else {
- return 1
- }
- }
- clueId = common.Int64All((*clueData)["id"])
- starttime := common.ObjToString(data["vip_starttime"])
- endtime := common.ObjToString(data["vip_endtime"])
- buy_subject := common.IntAll(data["buy_subject"])
- product_type := common.ObjToString(data["product_type"])
- data_spec := common.ObjToString(data["data_spec"])
- userName := common.ObjToString((*clueData)["name"])
- product, company_name := 0, common.ObjToString(data["company_name"])
- productMap := map[string]int{
- "dhy6": 1,
- "dhy7": 2,
- "dhy3": 4,
- "dhy1": 5,
- "dhy2": 6,
- "dhy5": 7,
- "企业商机管理": 8,
- }
- if buy_subject == 1 || buy_subject == 0 {
- entId = common.ObjToString(data["userid"])
- }
- if product_type == "企业商机管理" {
- product = productMap[product_type]
- powerData := TiDb.FindOne("dwd_f_data_equity_info", map[string]interface{}{"uid": uId, "product_type": "商机管理"}, "", "comeintime desc")
- if powerData != nil {
- starttime = common.ObjToString((*powerData)["starttime"])
- endtime = common.ObjToString((*powerData)["endtime"])
- } else {
- log.Println("客成移交权限未查到--", uId)
- return 0
- }
- } else {
- if data_spec == "dhy6" {
- product = 1
- filter := common.ObjToString(data["filter"])
- filterMap := map[string]interface{}{}
- json.Unmarshal([]byte(filter), &filterMap)
- if len(filterMap) > 0 {
- areaCount := common.IntAll(filterMap["areaCount"])
- if areaCount == 1 {
- product = 3
- }
- }
- } else {
- product = productMap[data_spec]
- }
- }
- //同一公司名称(以客户详情-组织机构-公司名称)下的线索需分配给同1人
- saveMap = map[string]interface{}{
- "clue_id": clueId,
- "transfertime": nowTime,
- "service_starttime": starttime,
- "service_endtime": endtime,
- "ent_id": entId,
- "is_task": 1,
- "tasktime": nowTime,
- "taskstatus": 0,
- "tasksource": "1",
- "is_admin": 1,
- "product_access": product,
- "buy_subject": buy_subject,
- "relationship_building_way": 1,
- "inventory_way": 1,
- "training_way": 1,
- "is_pre_sales_training": 0,
- "service_stage": 1,
- "company_name": company_name,
- }
- if TiDb.Count("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}) > 0 {
- csmdata := TiDb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, "", "")
- if csmdata != nil && len(*csmdata) > 0 {
- id := gconv.Int64((*csmdata)["id"])
- log.Println(1111111, id, orderCode)
- //'已移交销售,0移交客成 1移交销售',
- is_transfer := gconv.Int64((*csmdata)["is_transfer"])
- customerPositionId, customerName := cAutoDraw(0, orderPositionId, salesperson, saleDep, true, csmdata)
- log.Println("移交客成positionId", customerPositionId, customerName, saleDep, orderPositionId, salesperson, true)
- oldName := gconv.String((*csmdata)["name"])
- clueId := common.Int64All((*csmdata)["id"])
- updateMap := map[string]interface{}{
- "is_transfer": 0,
- "is_renewal_protection": 0,
- "product_access": product,
- "buy_subject": buy_subject,
- "transfertime": nowTime,
- "service_starttime": starttime,
- "service_endtime": endtime,
- "ent_id": entId,
- "company_name": company_name,
- "name": customerName,
- "position_id": customerPositionId,
- }
- if is_transfer == 1 || customerPositionId != gconv.Int64((*csmdata)["position_id"]) {
- //重新进去
- updateMap["relationship_building_way"] = 1
- updateMap["tasksource"] = "1"
- updateMap["inventory_way"] = 1
- updateMap["training_way"] = 1
- updateMap["is_pre_sales_training"] = 0
- updateMap["service_stage"] = 1
- updateMap["is_task"] = 1
- }
- TiDb.Update("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, updateMap)
- AddChangeRecord(nil, clueId, customerPositionId, "", "成交客户移交", "移交至客户成功组", "", nowTime, "", -1)
- if customerPositionId > 0 && (is_transfer == 1 || customerPositionId != gconv.Int64((*csmdata)["position_id"])) {
- AddChangeRecord(nil, clueId, customerPositionId, "position_id", "客户成功经理", customerName, gconv.String(common.If(oldName != customerName, oldName, common.If(oldName == "", "/", oldName))), nowTime, "", -1)
- AddChangeRecord(nil, clueId, customerPositionId, "", "加入任务车", "未建联", "", nowTime, "", -1)
- }
- if is_transfer == 1 || customerPositionId != gconv.Int64((*csmdata)["position_id"]) {
- if customerName == db.KeCheng.Admin {
- TiDb.UpdateOrDeleteBySql(`update dwd_f_csm_customer_autodraw_record set count = count + 2 where name = ?`, customerName)
- } else {
- TiDb.UpdateOrDeleteBySql(`update dwd_f_csm_customer_autodraw_record set count = count + 1 where name = ?`, customerName)
- }
- }
- KcSend(orderCode, customerName)
- TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime, "name": userName})
- }
- } else {
- entIds := common.IntAll(data["ent_id"])
- customerPositionId, customerName := cAutoDraw(entIds, orderPositionId, salesperson, saleDep, false, nil)
- log.Println("移交客成positionId", customerPositionId, customerName, saleDep, orderPositionId, salesperson, false)
- cId, ok, updateId1, updateId2, updateId3 := int64(0), false, int64(0), int64(0), int64(0)
- if TiDb.ExecTx("保存客户", func(tx *sql.Tx) bool {
- saveMap["position_id"] = customerPositionId
- saveMap["name"] = customerName
- cId = TiDb.InsertByTx(tx, "dwd_f_csm_customer_info", saveMap)
- ok = TiDb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime, "name": userName})
- if customerPositionId > 0 {
- updateId1 = AddChangeRecord(tx, clueId, customerPositionId, "", "加入任务车", "未建联", "", nowTime, "", -1)
- updateId3 = AddChangeRecord(tx, clueId, customerPositionId, "position_id", "客户成功经理", customerName, "/", nowTime, "", -1)
- }
- KcSend(orderCode, customerName)
- updateId2 := AddChangeRecord(tx, clueId, customerPositionId, "", "成交客户移交", "移交至客户成功组", "", nowTime, "", -1)
- return cId > -1 && ok && updateId1 > -1 && updateId2 > -1 && updateId3 > -1
- }) {
- //TiDb.UpdateOrDeleteBySql(`update dwd_f_csm_customer_autodraw_record set count = count + 1 where name = ?`, name)
- if customerName == db.KeCheng.Admin {
- TiDb.UpdateOrDeleteBySql(`update dwd_f_csm_customer_autodraw_record set count = count + 2 where name = ?`, customerName)
- } else {
- TiDb.UpdateOrDeleteBySql(`update dwd_f_csm_customer_autodraw_record set count = count + 1 where name = ?`, customerName)
- }
- log.Println("保存客户成功")
- } else {
- log.Println("保存客户失败!!!", clueId, cId, ok, updateId1, updateId2, updateId3, " 用户信息 ", customerName, customerPositionId, uId)
- }
- }
- return status
- }
- func cAutoDraw(entId int, orderPositionId int64, salesperson, saleDep string, isExist bool, csmdata *map[string]interface{}) (positionId int64, name string) {
- query := `SELECT name,position_id FROM dwd_d_crm_department_level_succbi WHERE bi_pcode = (SELECT bi_code FROM dwd_d_crm_department_level_succbi WHERE name = "客户成功部" and resign=0 and ent_id=?) and resign = 0 `
- data := TiDb.SelectBySql(query, db.EntId)
- if data == nil || len(*data) == 0 {
- return
- }
- positionId, name = findKcOldPerson(entId, orderPositionId, salesperson, saleDep, isExist, csmdata, data)
- if positionId == 0 {
- positionId, name = findKcNewPerson(entId, data)
- }
- return
- }
- func findKcOldPerson(entId int, orderPositionId int64, salesperson, saleDep string, isExist bool, csmdata *map[string]interface{}, data *[]map[string]interface{}) (positionId int64, name string) {
- //判断是否需要新增客成数据
- if isExist {
- //用户存在客成数据
- positionid := common.Int64All((*csmdata)["position_id"])
- if positionid != 0 {
- //以前没有,需要找一个新的
- for _, v := range *data {
- deptPositionId := gconv.Int64(v["position_id"])
- if deptPositionId == positionid {
- positionId = positionid
- name = common.ObjToString((*csmdata)["name"])
- return
- }
- }
- }
- //原始用户已离职 查看订单是否有合适的订单
- if saleDep == "客户成功组" {
- positionId = orderPositionId
- name = salesperson
- return
- }
- }
- //客成数据不存在的时候
- //查找企业有没有其他信息
- if entId > 0 {
- entdata := TiDb.Find("dwd_f_csm_customer_info", map[string]interface{}{"ent_id": entId}, "", "", -1, -1)
- if entdata != nil && len(*entdata) > 0 {
- for _, v := range *entdata {
- positionid := common.Int64All(v["position_id"])
- name = common.ObjToString(v["name"])
- for _, v := range *data {
- deptPositionId := gconv.Int64(v["position_id"])
- if deptPositionId == positionId {
- positionId = positionid
- name = common.ObjToString((*csmdata)["name"])
- return
- }
- }
- }
- }
- //原始用户已离职 或者没有查找相关企业客成信息 以订单信息为主
- if saleDep == "客户成功组" {
- positionId = orderPositionId
- name = salesperson
- }
- return
- //没有相同企业的客成信息
- } else {
- //没有人 分配给自己
- if saleDep == "客户成功组" {
- positionId = orderPositionId
- name = salesperson
- return
- }
- return
- }
- }
- func findKcNewPerson(entId int, data *[]map[string]interface{}) (positionId int64, name string) {
- if entId > 0 {
- entdata := TiDb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{"ent_id": entId}, "", "")
- if entdata != nil && len(*entdata) > 0 {
- positionId = common.Int64All((*entdata)["position_id"])
- name = common.ObjToString((*entdata)["name"])
- return
- }
- }
- sql := `select a.name,a.count from dwd_f_csm_customer_autodraw_record a INNER JOIN dwd_d_crm_department_level_succbi b on b.ent_id=? and (a.name = b.name) and b.resign = 0`
- countData := TiDb.SelectBySql(sql, db.EntId)
- if countData != nil && len(*countData) > 0 {
- for _, v := range *data {
- //判断是否有新员工
- isOk := false
- for _, vv := range *countData {
- if common.ObjToString(v["name"]) == common.ObjToString(vv["name"]) {
- isOk = true
- }
- }
- //有新员工直接分给新员工
- if !isOk {
- name = common.ObjToString(v["name"])
- rData := TiDb.FindOne("dwd_f_csm_customer_autodraw_record", map[string]interface{}{}, "", "count desc")
- TiDb.Insert("dwd_f_csm_customer_autodraw_record", map[string]interface{}{
- "name": name,
- "count": common.Int64All((*rData)["count"]),
- })
- break
- }
- }
- res := int64(0)
- countres := 0
- for _, v := range *countData {
- if countres == 0 {
- res = common.Int64All(v["count"])
- name = common.ObjToString(v["name"])
- } else {
- if common.Int64All(v["count"]) <= res {
- res = common.Int64All(v["count"])
- name = common.ObjToString(v["name"])
- }
- }
- countres++
- }
- }
- for _, v := range *data {
- if name == common.ObjToString(v["name"]) {
- positionId = common.Int64All(v["position_id"])
- }
- }
- return
- }
- func kcAuto() {
- log.Println("客户成功系统自动进入任务车定时任务开始")
- nowTime := time.Now().Format(date.Date_Full_Layout)
- sql := `select * from dwd_f_csm_customer_info where ((relationship_building_way != 1 and inventory_way = 1) or (inventory_way != 1 and training_way = 1)) and is_task = 0`
- data := TiDb.SelectBySql(sql)
- if data != nil && *data != nil && len(*data) > 0 {
- for _, v := range *data {
- relationship_building_way := common.IntAll(v["relationship_building_way"])
- inventory_way := common.IntAll(v["inventory_way"])
- training_way := common.IntAll(v["training_way"])
- positionId := common.Int64All(v["position_id"])
- clueId := common.Int64All(v["clue_id"])
- tasksource, tasksources := common.ObjToString(v["tasksource"]), ""
- taskstatus := common.IntAll(v["taskstatus"])
- if relationship_building_way != 1 && inventory_way == 1 {
- tasksources = "2"
- AddChangeRecord(nil, clueId, positionId, "", "加入任务车", "未盘点", "", nowTime, "", -1)
- } else if inventory_way != 1 && training_way == 1 {
- tasksources = "3"
- AddChangeRecord(nil, clueId, positionId, "", "加入任务车", "未培训", "", nowTime, "", -1)
- }
- if taskstatus == 1 {
- tasksource = tasksources
- } else {
- if tasksource != "" {
- if !strings.Contains(tasksource, tasksources) {
- tasksource += "," + tasksources
- }
- } else {
- tasksource = tasksources
- }
- }
- TiDb.Update("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, map[string]interface{}{
- "tasksource": tasksource,
- "tasktime": nowTime,
- "is_task": 1,
- "taskstatus": 0,
- })
- }
- }
- log.Println("客户成功系统进入任务车定时任务结束")
- }
- func ordersClue() {
- log.Println("后台订单线索定时任务开始")
- lastOrderClueId := cfg.LastOrderClueId
- nowTime := time.Now().Format(date.Date_Full_Layout)
- sql := fmt.Sprintf(`select order_code,order_status,is_backstage_order,pay_money,saleDep,salesperson,user_phone,create_person,saleMoney,user_id,product_type,company_name,autoUpdate from dataexport_order where autoUpdate >= "%s" order by autoUpdate asc`, lastOrderClueId)
- data := Mysql.SelectBySql(sql)
- if data != nil && *data != nil && len(*data) > 0 {
- for _, v := range *data {
- order_status := common.IntAll(v["order_status"])
- is_backstage_order := common.IntAll(v["is_backstage_order"])
- pay_money := common.IntAll(v["pay_money"])
- orderCode := gconv.String(v["order_code"])
- saleDep, _, salesperson := FindSaleRecord(orderCode)
- user_phone := common.ObjToString(v["user_phone"])
- createperson := common.ObjToString(v["create_person"])
- userId, uId, cluename, seatNumber, positionId, trailstatus, clueId := common.ObjToString(v["user_id"]), "", "", "", int64(0), "", int64(0)
- product_type_str2 := `"大会员","企业商机管理","VIP订阅"`
- product_type := common.ObjToString(v["product_type"])
- if order_status == 1 && is_backstage_order == 1 && saleDep != "" && !strings.HasPrefix(user_phone, "9") && strings.Contains(product_type_str2, product_type) {
- //销售部
- query := map[string]interface{}{}
- source := ""
- if product_type == "企业商机管理" {
- query["phone"] = user_phone
- userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
- if userInfo != nil && len(*userInfo) > 0 {
- uId = common.ObjToString((*userInfo)["uid"])
- userId = common.ObjToString((*userInfo)["userid"])
- source = common.ObjToString((*userInfo)["source"])
- } else {
- log.Println("后台订单--未查询到 ", user_phone)
- break
- }
- } else {
- if !mongodb.IsObjectIdHex(userId) {
- userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"position_id": userId}, "", "")
- if userMapping != nil && len(*userMapping) > 0 {
- userId = common.ObjToString((*userMapping)["userid"])
- } else {
- log.Println("后台订单--未查询到 ", user_phone)
- break
- }
- }
- query["userid"] = userId
- userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
- if userInfo != nil && len(*userInfo) > 0 {
- uId = common.ObjToString((*userInfo)["uid"])
- source = common.ObjToString((*userInfo)["source"])
- } else {
- log.Println("后台订单--未查询到 ", query, user_phone)
- break
- }
- }
- is_assign := 1
- mailContent := ""
- cluename = common.ObjToString(v["company_name"])
- mailContent = fmt.Sprintf("%s(%s)", cluename, user_phone)
- if cluename == "" {
- cluename = user_phone
- mailContent = user_phone
- }
- if pay_money > 0 {
- trailstatus = "08"
- } else {
- trailstatus = "01"
- }
- isGroup, isCommerce := GetCompanyType(cluename, uId) //判断是否集团公司、工商库
- clueData := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", "")
- if clueData == nil || len(*clueData) == 0 {
- log.Println("saleDep", saleDep)
- if saleDep == "销售部" {
- //撞单
- log.Println("销售部订单", saleDep)
- if strings.Contains(salesperson, ",") {
- log.Println("销售部撞单 ", salesperson)
- if salesperson == "" {
- salesperson = createperson
- }
- }
- log.Println("销售部订单salesperson", salesperson)
- saleData := TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{"name": salesperson}, "", "")
- if saleData != nil {
- seatNumber = common.ObjToString((*saleData)["seat_number"])
- positionId = common.Int64All((*saleData)["position_id"])
- }
- if IsFreeze(source, pay_money, gconv.String(positionId), trailstatus) {
- //线索需要冻结
- is_assign = -3
- }
- clueId = TiDb.Insert("dwd_f_crm_clue_info", map[string]interface{}{
- "userid": userId,
- "uid": uId,
- "seatNumber": seatNumber,
- "position_id": positionId,
- "is_assign": is_assign,
- "comeintime": nowTime,
- "createtime": nowTime,
- "updatetime": nowTime,
- "cluename": cluename,
- "top_cluetype": "4",
- "sub_cluetype": "154",
- "trailstatus": trailstatus,
- "name": user_phone,
- "phone": user_phone,
- "comeinsource_private": 2,
- "is_task": 0,
- "taskstatus": 0,
- "company_nature": isGroup,
- "company_verification": isCommerce,
- "FREEZE_TIME": nowTime,
- })
- } else if saleDep == "市场部" {
- log.Println("市场部订单", saleDep)
- clueId = TiDb.Insert("dwd_f_crm_clue_info", map[string]interface{}{
- "userid": userId,
- "uid": uId,
- "is_assign": -2,
- "createtime": nowTime,
- "updatetime": nowTime,
- "cluename": cluename,
- "top_cluetype": "4",
- "sub_cluetype": "154",
- "trailstatus": trailstatus,
- "name": user_phone,
- "phone": user_phone,
- "is_task": 0,
- "taskstatus": 0,
- "company_nature": isGroup,
- "company_verification": isCommerce,
- "FREEZE_TIME": nowTime,
- })
- }
- if clueId > 0 {
- AddChangeRecord(nil, clueId, gconv.Int64(common.If(positionId > 0, positionId, -1)), "", "创建线索", "系统自动创建", "", nowTime, "", -1)
- if is_assign == -3 {
- //新增冻结记录
- AddChangeRecord(nil, clueId, positionId, "", "线索冻结", "销售人员私海已达上限", "", nowTime, "", -1)
- //移交失败发送消息
- if IsFreeze(source, pay_money, gconv.String(positionId), trailstatus) {
- //发送邮件信息
- OrderCreateFail(positionId, mailContent)
- }
- }
- }
- }
- }
- cfg.LastOrderClueId = common.ObjToString(v["autoUpdate"])
- }
- }
- common.WriteSysConfig(&cfg)
- log.Println("后台订单线索定时任务结束")
- }
- func refundAuto() {
- log.Println("自动移交销售定时任务开始")
- findNowTime := time.Now().AddDate(0, 0, -db.ExpirationPeriod).Format(date.Date_Full_Layout)
- nowTime := time.Now().Format(date.Date_Full_Layout)
- mailData := map[string][]map[string]interface{}{}
- salesEmailData := map[int64][]map[string]interface{}{}
- TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
- for _, v := range *l {
- saleId, cluename, company_nature, company_verification, uid, phone, userName := int64(0), "", 0, 0, "", "", ""
- clueId := common.Int64All(v["clue_id"])
- company_name := gconv.String(v["company_name"])
- name := common.ObjToString(v["name"])
- kcposition_id := common.Int64All(v["position_id"])
- isRenewalProtection := common.IntAll(v["is_renewal_protection"])
- clueData := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, "name,phone,company_nature,company_verification,cluename,userid,position_id,uid", "")
- if clueData != nil && len(*clueData) > 0 {
- saleId = common.Int64All((*clueData)["position_id"])
- cluename = common.ObjToString((*clueData)["cluename"])
- userName = gconv.String((*clueData)["name"])
- company_nature = common.IntAll((*clueData)["company_nature"])
- company_verification = common.IntAll((*clueData)["company_verification"])
- uid = common.ObjToString((*clueData)["uid"])
- phone = common.ObjToString((*clueData)["phone"])
- }
- //查询即将到期数据
- if isRenewalProtection == 0 {
- orderData := TiDb.SelectBySql(`select * from dwd_f_userbase_order_info where uid=? and (product_type = "企业商机管理" or product_type = "大会员") and order_status = 1 and refund_status!=1 ORDER BY vip_endtime desc `, uid)
- if orderData != nil && len(*orderData) > 0 {
- endDate := time.Now().AddDate(0, 0, -db.ExpirationPeriod+3).Unix()
- startDate := time.Now().AddDate(0, 0, -db.ExpirationPeriod+2).Unix()
- vipEndDate, _ := time.ParseInLocation(time.DateTime, gconv.String((*orderData)[0]["vip_endtime"]), time.Local)
- log.Println(vipEndDate.Unix() > startDate, vipEndDate.Unix() < endDate)
- log.Println(vipEndDate.Unix(), startDate, endDate)
- if vipEndDate.Unix() > startDate && vipEndDate.Unix() < endDate {
- //即将到期
- mailData[name] = append(mailData[name], map[string]interface{}{
- "company_name": company_name,
- "phone": phone,
- "userName": userName,
- "remrk": "3天后即将移交",
- "reason": "成交客户续费失败",
- })
- /*salesEmailData[saleId] = append(salesEmailData[saleId], map[string]interface{}{
- "company_name": company_name,
- "phone": phone,
- "userName": userName,
- "remrk": "1天后即将移交",
- "reason": "成交客户续费失败",
- "cluename": cluename,
- })*/
- continue
- }
- }
- }
- isFull := FindUpperLimit(gconv.String(saleId), "", false)
- isAllRefund := false
- myOrders := TiDb.SelectBySql(`select refund_status from dwd_f_userbase_order_info where uid=? and (product_type = "企业商机管理" or product_type = "大会员") and order_status = 1 `, uid)
- if myOrders != nil {
- refundCount := 0
- for _, v := range *myOrders {
- if common.IntAll(v["refund_status"]) == 1 {
- refundCount++
- }
- }
- if len(*myOrders) > 0 && len(*myOrders) == refundCount {
- isAllRefund = true
- }
- }
- if isAllRefund {
- //全退款了
- TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,is_assign=0,trailstatus="01",position_id=null,seatNumber=null,updatetime=?,comeintime_open=?,comeinsource_open=1,level_open=3,next_trail_time=null,is_task=null,tasktime=null,taskstatus=null,comeinsource_private=null,tasksource=null,is_transfer=0 WHERE id = ?`, nowTime, nowTime, clueId)
- AddChangeRecord(nil, clueId, gconv.Int64(common.If(kcposition_id > 0, kcposition_id, -1)), "", "退回公海", "成交客户申请退款", "", nowTime, "", -1)
- AddChangeRecord(nil, clueId, gconv.Int64(common.If(kcposition_id > 0, kcposition_id, -1)), "trailstatus", "基本信息变更", "商机线索", "成交客户", nowTime, "", -1)
- AddChangeRecord(nil, clueId, gconv.Int64(common.If(kcposition_id > 0, kcposition_id, -1)), "position_id", "客户成功经理", "/", name, nowTime, "", -1)
- AddChangeRecord(nil, clueId, gconv.Int64(common.If(kcposition_id > 0, kcposition_id, -1)), "", "移交销售", "成交客户申请退款", "", nowTime, "", -1)
- mailData[name] = append(mailData[name], map[string]interface{}{
- "company_name": company_name,
- "phone": phone,
- "userName": userName,
- "remrk": "已移交",
- "reason": "成交客户申请退款",
- })
- TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_csm_customer_info SET is_transfer=1 WHERE clue_id = ?`, clueId)
- } else {
- order1 := TiDb.SelectBySql(`select id from dwd_f_userbase_order_info where uid=? and (product_type = "企业商机管理" or product_type = "大会员") and vip_endtime > ? and order_status = 1 `, uid, findNowTime)
- if order1 == nil || len(*order1) == 0 {
- isOk := false
- order3 := TiDb.SelectBySql(`select vip_endtime from dwd_f_userbase_order_info where uid=? and (product_type = "企业商机管理" or product_type = "大会员") and vip_endtime < ? and order_status = 1 ORDER BY vip_endtime desc limit 1 `, uid, findNowTime)
- if order3 != nil && len(*order3) > 0 {
- //获取最后一个订单信息
- vv := (*order3)[0]
- if isRenewalProtection == 1 {
- vip_endtime := common.ObjToString(vv["vip_endtime"])
- vip_endtimes, _ := time.ParseInLocation(date.Date_Full_Layout, vip_endtime, time.Local)
- //查询申请保护时间
- renewalTime := time.Now()
- timeData := TiDb.SelectBySql(`select max(createtime) as createtime from dwd_f_crm_clue_change_record where clue_id = ? and change_type="申请续费保护"`, clueId)
- if timeData != nil && len(*timeData) > 0 {
- if gconv.String(gconv.String((*timeData)[0]["createtime"])) != "" {
- renewalTime, _ = time.ParseInLocation(date.Date_Short_Layout, gconv.String((*timeData)[0]["createtime"]), time.Local)
- }
- }
- renewalInt := renewalTime.Unix()
- log.Println("申请保护期时间", clueId, renewalInt)
- log.Println("申请保护期时间222", time.Now().Unix()-vip_endtimes.Unix(), (db.HandoverCycle-1)*86400, (db.HandoverCycle)*86400)
- //查看服务到期时间
- if renewalInt != 0 && renewalInt < db.CustomerTime {
- //三个月
- if time.Now().Unix()-vip_endtimes.Unix() < 3*30*86400 {
- isOk = true
- }
- } else if (db.HandoverCycle-5)*86400 < time.Now().Unix()-vip_endtimes.Unix() && time.Now().Unix()-vip_endtimes.Unix() < (db.HandoverCycle-4)*86400 {
- //提前一天提示
- mailData[name] = append(mailData[name], map[string]interface{}{
- "company_name": company_name,
- "phone": phone,
- "userName": userName,
- "remrk": "3天后即将移交",
- "reason": "成交客户续费失败",
- })
- isOk = true
- } else if time.Now().Unix()-vip_endtimes.Unix() < db.HandoverCycle*86400 {
- isOk = true
- }
- }
- }
- if !isOk {
- //全到期了
- if saleId > 0 {
- saleData := TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{"position_id": saleId, "resign": 0}, "", "")
- if saleData != nil && len(*saleData) > 0 {
- if FindUpperLimit(gconv.String(saleId), "", false) {
- TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0,is_assign=-3,FREEZE_TIME=? WHERE id = ?`, nowTime, nowTime, nowTime, clueId)
- } else {
- TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0 WHERE id = ?`, nowTime, nowTime, clueId)
- }
- } else {
- if company_nature == 0 && company_verification == 1 && cluename != "" {
- adata := TiDb.Find("dwd_f_crm_clue_info", map[string]interface{}{"cluename": cluename, "is_assign": 1}, "seatNumber,position_id", "", -1, -1)
- if adata != nil && len(*adata) > 0 {
- isOks := false
- for _, vv := range *adata {
- position_ids := common.Int64All(vv["position_id"])
- seatNumber := common.ObjToString(vv["seatNumber"])
- saleDatas := TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{"position_id": position_ids, "resign": 0}, "", "")
- if saleDatas != nil && len(*saleDatas) > 0 {
- saleId = position_ids
- if isFull {
- TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",position_id=?,seatNumber=?,is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0,is_assign=-3,FREEZE_TIME=? WHERE id = ?`, nowTime, saleId, seatNumber, nowTime, nowTime, clueId)
- } else {
- TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",position_id=?,seatNumber=?,is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0 WHERE id = ?`, nowTime, saleId, seatNumber, nowTime, clueId)
- }
- isOks = true
- break
- }
- }
- if !isOks {
- 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`, saleId)
- if sdata != nil && len(*sdata) > 0 {
- saleId = common.Int64All((*sdata)[0]["position_id"])
- seatNumber := common.ObjToString((*sdata)[0]["seat_number"])
- if isFull {
- TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",position_id=?,seatNumber=?,is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0,is_assign=-3,FREEZE_TIME=? WHERE id = ?`, nowTime, saleId, seatNumber, nowTime, nowTime, clueId)
- } else {
- TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",position_id=?,seatNumber=?,is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0 WHERE id = ?`, nowTime, saleId, seatNumber, nowTime, clueId)
- }
- }
- }
- }
- } else {
- 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`, saleId)
- if sdata != nil && len(*sdata) > 0 {
- saleId = common.Int64All((*sdata)[0]["position_id"])
- seatNumber := common.ObjToString((*sdata)[0]["seat_number"])
- if isFull {
- TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",position_id=?,seatNumber=?,is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0,is_assign=-3,FREEZE_TIME=? WHERE id = ?`, nowTime, saleId, seatNumber, nowTime, nowTime, clueId)
- } else {
- TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",position_id=?,seatNumber=?,is_task=1,tasktime=?,top_cluetype="532",sub_cluetype="537",taskstatus=0,tasksource="其他-成交客户续约失败",is_transfer=0 WHERE id = ?`, nowTime, saleId, seatNumber, nowTime, clueId)
- }
- }
- }
- }
- } else {
- if isFull {
- TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",top_cluetype="532",sub_cluetype="537",is_transfer=0,is_assign=-3,FREEZE_TIME=? WHERE id = ?`, nowTime, nowTime, clueId)
- } else {
- TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,updatetime=?,trailstatus="01",top_cluetype="532",sub_cluetype="537",is_transfer=0 WHERE id = ?`, nowTime, clueId)
- }
- }
- if kcposition_id > 0 {
- mailData[name] = append(mailData[name], map[string]interface{}{
- "company_name": company_name,
- "phone": phone,
- "userName": userName,
- "remrk": "已移交",
- "reason": "成交客户续费失败",
- })
- }
- if saleId > 0 {
- salesEmailData[saleId] = append(salesEmailData[saleId], map[string]interface{}{
- "company_name": company_name,
- "phone": phone,
- "userName": userName,
- "remrk": "已移交",
- "reason": "成交客户续费失败",
- "cluename": cluename,
- })
- }
- AddChangeRecord(nil, clueId, gconv.Int64(common.If(kcposition_id > 0, kcposition_id, -1)), "trailstatus", "基本信息变更", "商机线索", "", nowTime, "", -1)
- AddChangeRecord(nil, clueId, saleId, "trailstatus", "加入任务车", "线索自动分配-其他-成交客户续约失败", "", nowTime, "", -1)
- AddChangeRecord(nil, clueId, gconv.Int64(common.If(kcposition_id > 0, kcposition_id, -1)), "position_id", "客户成功经理", "/", name, nowTime, "", -1)
- AddChangeRecord(nil, clueId, saleId, "", "移交销售", "成交客户续费失败,到期自动移交", "", nowTime, "", -1)
- TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_csm_customer_info SET is_transfer=1 WHERE clue_id = ?`, clueId)
- if isFull {
- //新增冻结记录
- AddChangeRecord(nil, clueId, saleId, "", "线索冻结", "销售人员私海已达上限", "", nowTime, "", -1)
- //发送邮件
- mailContent := ""
- if cluename != "" {
- if phone != "" {
- if cluename == phone {
- mailContent = phone
- } else {
- mailContent = fmt.Sprintf("%s(%s)", cluename, phone)
- }
- } else {
- mailContent = cluename
- }
- } else {
- mailContent = phone
- }
- HandOverFail(saleId, mailContent)
- }
- }
- }
- }
- }
- return true
- }, `select ent_id,clue_id,position_id,name,is_renewal_protection,company_name from dwd_f_csm_customer_info where is_transfer = 0`)
- //}, `select ent_id,clue_id,position_id,name,is_renewal_protection,company_name from dwd_f_csm_customer_info where clue_id =1651434`)
- //移交电销提醒
- for i, v := range mailData {
- ExitKcSend(i, v)
- }
- //移交电销销售提醒
- for i, v := range salesEmailData {
- ExitKcSaleSend(i, v)
- }
- log.Println("自动移交销售定时任务结束")
- }
- // 查找一部三部人员信息 返回人员信息 以及管理员信息
- func FindDeptAdmin(positionId int64) (map[string]interface{}, map[string]interface{}, map[string]interface{}) {
- //先判断那个部门的
- userData1 := TiDb.SelectBySql(`SELECT
- a.position_id,
- a.name,
- b.seat_number,
- b.role_id,
- a.bi_pcode,
- a.dept_name,
- a.resign
- FROM
- (SELECT *
- FROM dwd_d_crm_department_level_succbi u1
- WHERE id = (
- SELECT MAX(id)
- FROM dwd_d_crm_department_level_succbi u2
- WHERE u1.position_id = u2.position_id
- )
- ORDER BY id desc) a
- INNER JOIN (SELECT *
- FROM dwd_f_crm_personnel_management u1
- WHERE id = (
- SELECT MAX(id)
- FROM dwd_f_crm_personnel_management u2
- WHERE u1.position_id = u2.position_id
- )
- ORDER BY id desc) b ON a.dept_name LIKE "%电销部%"
- AND a.position_id = b.position_id`)
- userData3 := TiDb.SelectBySql(`SELECT
- a.position_id,
- a.name,
- b.seat_number,
- b.role_id,
- a.bi_pcode,
- a.dept_name,
- a.resign
- FROM
- (SELECT *
- FROM dwd_d_crm_department_level_succbi u1
- WHERE id = (
- SELECT MAX(id)
- FROM dwd_d_crm_department_level_succbi u2
- WHERE u1.position_id = u2.position_id
- )
- ORDER BY id desc) a
- INNER JOIN (SELECT *
- FROM dwd_f_crm_personnel_management u1
- WHERE id = (
- SELECT MAX(id)
- FROM dwd_f_crm_personnel_management u2
- WHERE u1.position_id = u2.position_id
- )
- ORDER BY id desc) b ON
- a.dept_name LIKE "%销售三部"
- AND a.position_id = b.position_id`)
- if userData3 == nil || len(*userData3) == 0 || userData1 == nil || len(*userData1) == 0 {
- log.Println("找不到一部三部员工信息")
- return nil, nil, nil
- }
- //一部高级管理员信息
- seniorAdmin1 := map[string]interface{}{}
- //三部高级管理员信息
- seniorAdmin3 := map[string]interface{}{}
- //一部管理员信息
- admin1 := map[string]map[string]interface{}{}
- //返回当前人信息
- admin := map[string]interface{}{}
- for _, v := range *userData3 {
- v["type"] = 3
- id := gconv.Int64(v["position_id"])
- roleId := gconv.Int64(v["role_id"])
- resign := gconv.Int64(v["resign"])
- if id == positionId && resign == 0 {
- admin = v
- }
- if roleId == 8 {
- v["orderStatus"] = 2
- seniorAdmin3 = v
- }
- }
- for _, v := range *userData1 {
- v["type"] = 1
- id := gconv.Int64(v["position_id"])
- roleId := gconv.Int64(v["role_id"])
- dept_name := gconv.String(v["dept_name"])
- resign := gconv.Int64(v["resign"])
- if id == positionId && resign == 0 {
- admin = v
- }
- if roleId == 8 {
- v["orderStatus"] = 2
- seniorAdmin1 = v
- } else if roleId == 3 {
- admin1[dept_name] = v
- }
- }
- //查看当前人属于哪个部门
- if admin == nil {
- //当前人不属于电销部的
- admin = *(TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{
- "position_id": positionId,
- "resign": 1,
- }, "name", ""))
- }
- positiontype := gconv.Int64(admin["type"])
- deptName := gconv.String(admin["dept_name"])
- switch positiontype {
- case 1:
- roleId := gconv.Int64(admin["role_id"])
- if roleId == 3 {
- return admin, nil, seniorAdmin1
- }
- return admin, admin1[deptName], seniorAdmin1
- case 3:
- return admin, nil, seniorAdmin3
- default:
- return admin, nil, nil
- }
- }
- // 邮箱获取
- func GetMail(personName string) (string, string) {
- data := Mysql.SelectBySql(`select name,mail,img from entniche_user where ent_id =?`, db.EntId)
- if data == nil || len(*data) == 0 {
- return "", ""
- }
- for _, m := range *data {
- name := gconv.String(m["name"])
- if name == personName {
- //
- return gconv.String(m["mail"]), gconv.String(m["img"])
- }
- }
- return "", ""
- }
- // 退客成进销售 销售收邮件
- func ExitKcSaleSend(positionId int64, infoList []map[string]interface{}) {
- to := SaleMail(positionId)
- //退出客成 即将退出客成信息 销售信息编辑
- gmail := &mail.GmailAuth{
- SmtpHost: db.Mail.SmtpHost,
- SmtpPort: db.Mail.SmtpPort,
- User: db.Mail.User,
- Pwd: db.Mail.Pwd,
- }
- //正文拼接
- startStr := `<html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, initial-scale=1.0">
- <style>table{width:100%;border-collapse:collapse}th,td{border:1px solid#000;padding:8px;text-align:left}th{background-color:#f2f2f2}</style></head>
- <body><p>以下客户已从或即将从客成系统退出,并移交销售跟进,请收悉,客户明细如下:</p>
- <table><thead><tr><th>序号</th><th>线索名称</th><th>联系人</th><th>姓名</th><th>移交状态</th></tr></thead><tbody>`
- for i, v := range infoList {
- startStr += fmt.Sprintf(`<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>`, i+1, gconv.String(v["cluename"]), gconv.String(v["phone"]), gconv.String(v["userName"]), gconv.String(v["remrk"]))
- }
- endStr := `</tbody></table><p></body></html>`
- startStr += endStr
- if to != "" {
- status := mail.GSendMail_q("剑鱼标讯", to, "", "", "成交客户续费失败移交销售通知", startStr, "", "", gmail)
- if status {
- log.Println("客成发邮件 send mail success", startStr, to)
- }
- }
- }
- // 客成人员变更发邮件
- func CustomerChange() {
- personMap := map[int64]string{}
- personData := TiDb.SelectBySql(`select name,position_id from dwd_d_crm_department_level_succbi where ent_id=? and resign=0 and position_id>0`, db.EntId)
- if personData != nil && len(*personData) > 0 {
- for _, v := range *personData {
- personMap[gconv.Int64(v["position_id"])] = gconv.String(v["name"])
- }
- }
- startTime := cfg.LastKcChangeTime
- if startTime == "" {
- startTime = time.Now().Format(time.DateTime)
- }
- customMap1 := map[string][]map[string]interface{}{}
- customMap2 := map[string][]map[string]interface{}{}
- saleMap := map[int64][]map[string]interface{}{}
- saleNobodyCustomMapMap := map[int64][]map[string]interface{}{}
- TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
- for _, v := range *l {
- startTime = gconv.String(v["createtime"])
- data := CustomerChangeHandle(v)
- oldPerson := gconv.String(data["oldPerson"])
- newPerson := gconv.String(data["newPerson"])
- positionId := gconv.Int64(data["positionId"])
- operatorId := gconv.Int64(data["operatorId"])
- if oldPerson == "/" {
- data["oldPerson"] = "-"
- }
- if positionId > 0 {
- data["positionName"] = personMap[positionId]
- }
- if operatorId > 0 {
- //手动分配 客成收邮件
- data["operatorName"] = personMap[operatorId]
- if newPerson != "" && newPerson != "/" {
- customMap2[newPerson] = append(customMap2[newPerson], data)
- }
- if oldPerson != "" && oldPerson != "/" {
- customMap1[oldPerson] = append(customMap1[oldPerson], data)
- }
- } else {
- //自动分配 销售收邮件
- if (oldPerson == "" || oldPerson == "/") && newPerson != "" && newPerson != "/" && positionId > 0 {
- //之前没有可成人员
- saleNobodyCustomMapMap[positionId] = append(saleNobodyCustomMapMap[positionId], data)
- } else if newPerson != "" && newPerson != "/" && oldPerson != "" && oldPerson != "/" && positionId > 0 {
- //之前有客成
- saleMap[positionId] = append(saleMap[positionId], data)
- }
- }
- }
- return true
- }, `select operator_id, clue_id,position_id,old_value,new_value,createtime from dwd_f_crm_clue_change_record where createtime>? and change_type="客户成功经理" order by createtime`, startTime)
- if len(customMap1) > 0 {
- ChangeSendKc(customMap1, 1)
- }
- if len(customMap2) > 0 {
- ChangeSendKc(customMap2, 2)
- }
- if len(saleMap) > 0 {
- ChangeSendSale(saleMap, 2)
- }
- if len(saleNobodyCustomMapMap) > 0 {
- ChangeSendSale(saleNobodyCustomMapMap, 1)
- }
- log.Println("11111", startTime)
- cfg.LastKcChangeTime = startTime
- common.WriteSysConfig(&cfg)
- }
- // 客户成功经理变更邮件提醒客成人员
- func ChangeSendKc(data map[string][]map[string]interface{}, personType int64) {
- for name, v := range data {
- to, _ := GetMail(name)
- //正文拼接
- startStr := `<html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, initial-scale=1.0"><style>table{width:100%;border-collapse:collapse}th,td{border:1px solid#000;padding:8px;text-align:left}th{background-color:#f2f2f2}</style></head><body><p></p>
- <table><thead><tr><th>序号</th><th>公司名称</th><th>联系人</th><th>姓名</th><th>变更前客户经理</th><th>变更后客户经理</th><th>操作人</th></tr></thead><tbody>`
- for i, v := range v {
- startStr += fmt.Sprintf(`<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>`, i+1, gconv.String(v["companyName"]), gconv.String(v["phone"]), gconv.String(v["userName"]), gconv.String(v["oldPerson"]), gconv.String(v["newPerson"]), gconv.String(v["operatorName"]))
- }
- endStr := `</tbody></table><p></body></html>`
- startStr += endStr
- if to != "" {
- status := mail.GSendMail_q("剑鱼标讯", to, "", "", gconv.String(common.If(personType == 2, "您有新的客户,请及时跟进处理", "您的客户已转至其他客户成功经理负责")), startStr, "", "", &Gmail)
- if status {
- log.Println("客成发邮件 send mail success", startStr, to, "")
- }
- }
- }
- }
- func ChangeSendSale(data map[int64][]map[string]interface{}, personType int64) {
- if personType == 1 {
- //客户成功经理从空变成有人
- for _, v := range data {
- for _, v1 := range v {
- positionId := gconv.Int64(v1["positionId"])
- to := SaleMail(positionId)
- _, img := GetMail(gconv.String(v1["newPerson"]))
- //抄送人查询
- startStr := `<style> *,body,html{margin:10px;font-family:tahoma,arial,'Hiragino Sans GB','Microsoft YaHei',宋体,ans-serif;font-size:16px;}p{margin:15px;font-size:18px;}table{background-color: rgb(244, 244, 249);padding:5px 15px;border:solid 1px #ddd;margin: 20px 0px 20px 50px;vertical-align:top;display:inline-block;}.tit{width:120px;}td{padding: 5px;}.clear{clear: both;}</style>
- <p>销售人员"%s"成交的客户已成功移交至客成,线索名称:%s,姓名:%s,手机号:%s,负责客户成功经理:%s</p>
- <p>请引导客户添加客户成功经理企业微信:</p><div class='clear'>%s</div>`
- endStr := fmt.Sprintf(startStr, gconv.String(v1["positionName"]), gconv.String(v1["clueName"]), gconv.String(v1["userName"]), gconv.String(v1["phone"]), gconv.String(v1["newPerson"]), common.If(img != "", fmt.Sprintf(`<img width="300" height="450" src="%s">`, img), ""))
- if to != "" {
- status := mail.GSendMail_q("剑鱼标讯", to, "", "", "成交客户已成功移交客成", endStr, "", "", &Gmail)
- if status {
- log.Println("客成发邮件 send mail success", endStr, to, "")
- }
- }
- }
- }
- return
- }
- //客户成功经理从A变成了B
- for positionId, v := range data {
- to := SaleMail(positionId)
- //正文拼接
- startStr := `<html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, initial-scale=1.0"><style>table{width:100%;border-collapse:collapse}th,td{border:1px solid#000;padding:8px;text-align:left}th{background-color:#f2f2f2}</style></head><body><p>客户明细如下:
- </p><table><thead><tr><th>序号</th><th>线索名称</th><th>联系人</th><th>姓名</th><th>变更前客户经理</th><th>变更后客户经理</th><th>销售人员</th></tr></thead><tbody>`
- for i, v := range v {
- startStr += fmt.Sprintf(`<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>`,
- i+1, gconv.String(v["clueName"]), gconv.String(v["phone"]), gconv.String(v["userName"]), gconv.String(v["oldPerson"]), gconv.String(v["newPerson"]), gconv.String(v["positionName"]))
- }
- endStr := `</tbody></table><p></body></html>`
- startStr += endStr
- if to != "" {
- status := mail.GSendMail_q("剑鱼标讯", to, "", "", "成交客户负责客户成功经理变更", startStr, "", "", &Gmail)
- if status {
- log.Println("客成发邮件 send mail success", startStr, to, "")
- }
- }
- }
- }
- // 客成人员变更基本信息获取
- func CustomerChangeHandle(data map[string]interface{}) map[string]interface{} {
- clueId := gconv.Int64(data["clue_id"])
- oldPerson := gconv.String(data["old_value"])
- newPerson := gconv.String(data["new_value"])
- operatorId := gconv.Int64(data["operator_id"])
- companyName := ""
- phone := ""
- userName := ""
- clueName := ""
- clueData := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{
- "id": clueId,
- }, "", "")
- if clueData == nil || len(*clueData) == 0 {
- return nil
- }
- phone = common.ObjToString((*clueData)["phone"])
- userName = gconv.String((*clueData)["name"])
- positionId := gconv.Int64((*clueData)["position_id"])
- clueName = gconv.String((*clueData)["cluename"])
- customData := TiDb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{
- "clue_id": clueId,
- }, "", "")
- if customData != nil || len(*customData) != 0 {
- companyName = gconv.String((*customData)["company_name"])
- }
- return map[string]interface{}{
- "oldPerson": oldPerson,
- "newPerson": newPerson,
- "operatorId": operatorId,
- "phone": phone,
- "userName": userName,
- "positionId": positionId,
- "clueName": clueName,
- "companyName": companyName,
- }
- }
- func refundAutoHistory() {
- data := TiDb.Find("dwd_f_crm_clue_autodraw_record", nil, "", "", -1, -1)
- if data != nil {
- for _, v := range *data {
- seatNumber := common.ObjToString(v["seatNumber"])
- adata := TiDb.Find("dwd_f_crm_personnel_management", map[string]interface{}{"seat_number": seatNumber}, "", "", -1, -1)
- if adata != nil {
- for _, vv := range *adata {
- if common.IntAll(vv["resign"]) == 0 {
- position_id := common.Int64All(vv["position_id"])
- TiDb.Update("dwd_f_crm_clue_autodraw_record", map[string]interface{}{"seatNumber": seatNumber}, map[string]interface{}{"position_id": position_id})
- break
- }
- }
- }
- }
- }
- }
- func IsFreeze(source string, pay_money int, positionId, trailstatus string) bool {
- log.Println(source, pay_money == 0, FindUpperLimit(gconv.String(
- positionId), "", false), trailstatus)
- if source == "0104" && pay_money == 0 && FindUpperLimit(gconv.String(
- positionId), "", false) {
- return true
- } else if source == "0104" && pay_money > 0 && trailstatus == "08" && FindUpperLimit(gconv.String(
- positionId), "", false) {
- return true
- }
- return false
- }
- func FindSaleRecord(orderCode string) (dept string, positionId int64, position string) {
- recordList := Mysql.SelectBySql(`
- SELECT
- a.saler_dept,
- a.saler_name,
- b.phone
- FROM
- order_sale_record a
- INNER JOIN cadmin.admin_user b
- on
- a.ordercode = ? and a.saler_Id = b.id
- ORDER BY
- a.state,
- a.money
- `, orderCode)
- if recordList != nil && len(*recordList) > 0 {
- saler_dept := gconv.String((*recordList)[0]["saler_dept"])
- if strings.Contains(saler_dept, "销售") {
- saler_dept = "销售部"
- } else if strings.Contains(saler_dept, "市场") {
- saler_dept = "市场部"
- } else if strings.Contains(saler_dept, "客户成功") {
- saler_dept = "客户成功组"
- } else {
- saler_dept = ""
- }
- positionid := PhoneToPositionId(gconv.String((*recordList)[0]["phone"]))
- if positionid > 0 {
- return saler_dept, positionid, gconv.String((*recordList)[0]["saler_name"])
- }
- }
- return "", int64(0), ""
- }
- func PhoneToPositionId(phone string) int64 {
- positionId := int64(0)
- data := Base.SelectBySql(`SELECT
- b.id
- FROM
- base_user a
- INNER JOIN base_position b ON a.phone = ?
- AND a.id = b.user_id
- AND b.ent_id = 25917`, phone)
- if data != nil && len(*data) > 0 {
- positionId = gconv.Int64((*data)[0]["id"])
- }
- return positionId
- }
- func kcClue(userId, uId, seatNumber, cluename, top_cluetype, sub_cluetype,
- name, phone, position, sourceCode, industry, follow_project_area,
- role, item, subname, topname, remark, demand, department, departments, saleName, source string,
- positionId int64, isGroup, isCommerce int,
- keywords []string) bool {
- clueId, uodateId1, uodateId2, uodateId3, uodateId4, uodateId5 := int64(0), int64(0), int64(0), int64(0), int64(0), int64(0)
- nowTime := time.Now().Format("2006-01-02 15:04:05")
- nowTimes := time.Unix(time.Now().Unix()+3600*12, 0).Format("2006-01-02 15:04:05")
- if TiDb.ExecTx("保存线索", func(tx *sql.Tx) bool {
- clueId = TiDb.InsertByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{
- "userid": userId,
- "uid": uId,
- "seatNumber": seatNumber,
- "position_id": positionId,
- "is_assign": common.If(positionId > 0, 1, 0),
- "comeintime": nowTime,
- "createtime": nowTime,
- "updatetime": nowTime,
- "cluename": cluename,
- "top_cluetype": top_cluetype,
- "sub_cluetype": sub_cluetype,
- "trailstatus": "01",
- "name": name,
- "phone": phone,
- "position": position,
- "department": common.If(sourceCode == "app_xzcyh", departments, department),
- "industry": industry,
- "follow_project_area": follow_project_area,
- "role": role,
- "comeinsource_private": 2,
- "is_task": 1,
- "task_time": nowTime,
- "tasktime": common.If(item == "users", nowTimes, nowTime),
- "taskstatus": 0,
- "tasksource": "线索自动分配" + "-" + topname + "-" + subname,
- "business_scope": common.If(sourceCode == "app_xzcyh", keywords, nil),
- "company_nature": isGroup,
- "company_verification": isCommerce,
- "remark": remark,
- "customer_demand": demand,
- "FREEZE_TIME": nowTime,
- "label": 1,
- "labelChangeTime": time.Now().Format("2006-01-02"),
- })
- uodateId1 = AddChangeRecord(tx, clueId, positionId, "", "创建线索", "系统自动创建", "", nowTime, "", -1)
- uodateId2 = AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", saleName, "/", nowTime, "", -1)
- uodateId3 = AddChangeRecord(tx, clueId, positionId, "trailstatus", "基本信息变更", "新增", "商机线索", nowTime, "", -1)
- uodateId4 = AddChangeRecord(tx, clueId, positionId, "top_cluetype", "基本信息变更", topname, "/", nowTime, "", -1)
- uodateId5 = AddChangeRecord(tx, clueId, positionId, "sub_cluetype", "基本信息变更", subname, "/", nowTime, "", -1)
- AddLockeStatusChange(tx, clueId, positionId, 0, 0, isGroup, isCommerce, cluename, nowTime, "add")
- return clueId > -1 && uodateId1 > -1 && uodateId2 > -1 && uodateId3 > -1 && uodateId4 > -1 && uodateId5 > -1
- }) {
- log.Println("线索分配成功")
- if TiDb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
- TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
- "status": 1,
- "is_delete": 1,
- "createtime": nowTime,
- "updatetime": nowTime,
- "phone": phone,
- "baseinfo_id": uId,
- "SOURCE": source,
- })
- }
- return true
- }
- return false
- }
- type OrderInfo struct {
- Id int64
- UserId string
- CompanyName string
- ProductType string
- OrderCode string
- PayMoney float64
- ReturnMoney float64
- UserPhone string
- StartEnd string
- DisKcName string
- UserName string
- SeriveList []string
- SaleName string
- }
- // 客成发送邮箱
- func KcSend(orderCode, personName string) {
- log.Println("客成发邮件", orderCode)
- bigmemberService := map[int64]string{}
- combo := map[int64]string{}
- Mysql.SelectByBath(1, func(l *[]map[string]interface{}) bool {
- bigmemberService[common.Int64All((*l)[0]["id"])] = common.ObjToString((*l)[0]["s_name"])
- return true
- }, `select id,s_name from jianyu.bigmember_service`)
- Mysql.SelectByBath(1, func(l *[]map[string]interface{}) bool {
- combo[common.Int64All((*l)[0]["id"])] = common.ObjToString((*l)[0]["s_name"])
- return true
- }, `select id,s_name from jianyu.bigmember_combo`)
- orderInfo := KcOrderFormat(orderCode, bigmemberService, combo)
- if orderInfo != nil {
- tableAppend := ""
- if len(orderInfo.SeriveList) > 0 {
- tableAppend = fmt.Sprintf(db.KeCheng.Mail.ServiceList, strings.Join(orderInfo.SeriveList, ","))
- }
- if orderInfo.ReturnMoney > 0 {
- tableAppend += fmt.Sprintf(db.KeCheng.Mail.ReturnMoney, orderInfo.ReturnMoney)
- }
- table := ""
- orderInfo.DisKcName = personName
- deptData := Mysql.SelectBySql("select b.mail from jianyu.entniche_department_user a inner join jianyu.entniche_user b on a.dept_id=? and a.user_id=b.id and b.name=? ", db.KeCheng.DeptId, personName)
- adminData := Mysql.SelectBySql(` select c.mail from entniche_department_user a INNER JOIN entniche_user_role b on a.dept_id=59005 and a.user_id = b.user_id and b.role_id=2 INNER JOIN entniche_user c on a.user_id=c.id`)
- table += fmt.Sprintf(db.KeCheng.Mail.Table, orderInfo.ProductType, orderInfo.CompanyName, orderInfo.UserPhone, orderInfo.UserName, orderInfo.OrderCode, orderInfo.PayMoney, orderInfo.StartEnd, orderInfo.DisKcName, orderInfo.SaleName, tableAppend)
- adminMailStr := ""
- if adminData != nil && len(*adminData) > 0 {
- adminMailStr = gconv.String((*adminData)[0]["mail"])
- }
- //发送邮件
- if personName == "" {
- if adminData != nil && len(*adminData) > 0 {
- //发送给管理员
- if adminMailStr != "" {
- gmail := &mail.GmailAuth{
- SmtpHost: db.Mail.SmtpHost,
- SmtpPort: db.Mail.SmtpPort,
- User: db.Mail.User,
- Pwd: db.Mail.Pwd,
- }
- status := mail.GSendMail_q("剑鱼标讯", adminMailStr, "", "", db.KeCheng.Title, fmt.Sprintf(db.KeCheng.Mail.Content, table), "", "", gmail)
- if status {
- log.Println("客成发邮件 send mail success", table, adminMailStr)
- }
- }
- }
- return
- }
- //当事人 管理员发送
- if deptData != nil && len(*deptData) > 0 {
- mailStr := gconv.String((*deptData)[0]["mail"])
- if mailStr != "" || adminMailStr != "" {
- if mailStr == adminMailStr {
- adminMailStr = ""
- }
- gmail := &mail.GmailAuth{
- SmtpHost: db.Mail.SmtpHost,
- SmtpPort: db.Mail.SmtpPort,
- User: db.Mail.User,
- Pwd: db.Mail.Pwd,
- }
- status := true
- if mailStr == "" {
- status = mail.GSendMail_q("剑鱼标讯", adminMailStr, "", "", db.KeCheng.Title, fmt.Sprintf(db.KeCheng.Mail.Content, table), "", "", gmail)
- } else {
- status = mail.GSendMail_q("剑鱼标讯", mailStr, adminMailStr, "", db.KeCheng.Title, fmt.Sprintf(db.KeCheng.Mail.Content, table), "", "", gmail)
- }
- if status {
- log.Println("客成发邮件 send mail success", table, mailStr, adminMailStr)
- }
- }
- }
- }
- }
- func ExitKcSend(personName string, infoList []map[string]interface{}) {
- //退出客成 即将退出客成信息
- deptData := Mysql.SelectBySql("select b.mail from jianyu.entniche_department_user a inner join jianyu.entniche_user b on a.dept_id=? and a.user_id=b.id and b.name=? ", db.KeCheng.DeptId, personName)
- adminData := Mysql.SelectBySql(` select c.mail from entniche_department_user a INNER JOIN entniche_user_role b on a.dept_id=59005 and a.user_id = b.user_id and b.role_id=2 INNER JOIN entniche_user c on a.user_id=c.id`)
- if personName == "" {
- //没有客成人员
- adminMailStr := ""
- if adminData != nil && len(*adminData) > 0 {
- adminMailStr = gconv.String((*adminData)[0]["mail"])
- }
- if adminMailStr != "" {
- gmail := &mail.GmailAuth{
- SmtpHost: db.Mail.SmtpHost,
- SmtpPort: db.Mail.SmtpPort,
- User: db.Mail.User,
- Pwd: db.Mail.Pwd,
- }
- //正文拼接
- startStr := `<html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, initial-scale=1.0"><style>table{width:100%;border-collapse:collapse}th,td{border:1px solid#000;padding:8px;text-align:left}th{background-color:#f2f2f2}</style></head><body><p>以下客户已从或即将从客成系统退出,并移交销售跟进,请收悉,客户明细如下:</p><table><thead><tr><th>序号</th><th>公司名称</th><th>联系人</th><th>姓名</th><th>移交状态</th><th>移交销售原因</th></tr></thead><tbody>`
- for i, v := range infoList {
- startStr += fmt.Sprintf(`<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>`, i+1, gconv.String(v["company_name"]), gconv.String(v["phone"]), gconv.String(v["userName"]), gconv.String(v["remrk"]), gconv.String(v["reason"]))
- }
- endStr := `</tbody></table><p></body></html>`
- startStr += endStr
- status := mail.GSendMail_q("剑鱼标讯", adminMailStr, "", "", "客户退出客成系统通知", startStr, "", "", gmail)
- if status {
- log.Println("客成发邮件 send mail success", startStr, adminMailStr)
- }
- }
- return
- }
- if deptData != nil && len(*deptData) > 0 {
- mailStr := gconv.String((*deptData)[0]["mail"])
- adminMailStr := ""
- if adminData != nil && len(*adminData) > 0 {
- adminMailStr = gconv.String((*adminData)[0]["mail"])
- }
- if mailStr != "" || adminMailStr != "" {
- if mailStr == adminMailStr {
- adminMailStr = ""
- }
- gmail := &mail.GmailAuth{
- SmtpHost: db.Mail.SmtpHost,
- SmtpPort: db.Mail.SmtpPort,
- User: db.Mail.User,
- Pwd: db.Mail.Pwd,
- }
- //正文拼接
- startStr := `<html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, initial-scale=1.0"><style>table{width:100%;border-collapse:collapse}th,td{border:1px solid#000;padding:8px;text-align:left}th{background-color:#f2f2f2}</style></head><body><p>以下客户已从或即将从客成系统退出,并移交销售跟进,请收悉,客户明细如下:</p><table><thead><tr><th>序号</th><th>公司名称</th><th>联系人</th><th>姓名</th><th>移交状态</th><th>移交销售原因</th></tr></thead><tbody>`
- for i, v := range infoList {
- startStr += fmt.Sprintf(`<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>`, i+1, gconv.String(v["company_name"]), gconv.String(v["phone"]), gconv.String(v["userName"]), gconv.String(v["remrk"]), gconv.String(v["reason"]))
- }
- endStr := `</tbody></table><p></body></html>`
- startStr += endStr
- status := true
- if mailStr == "" {
- status = mail.GSendMail_q("剑鱼标讯", adminMailStr, "", "", "客户退出客成系统通知", startStr, "", "", gmail)
- } else {
- status = mail.GSendMail_q("剑鱼标讯", mailStr, adminMailStr, "", "客户退出客成系统通知", startStr, "", "", gmail)
- }
- if status {
- log.Println("客成发邮件 send mail success", startStr, mailStr)
- }
- }
- }
- }
- // 客成邮件 订单信息初始化
- func KcOrderFormat(orderCode string, bigmemberService, combo map[int64]string) *OrderInfo {
- orderData := Mysql.SelectBySql(`SELECT a.id,a.order_code,a.pay_money,a.user_phone,a.vip_starttime,a.vip_endtime,a.product_type,a.filter,a.user_id,a.ent_id,a.company_name,a.create_person,SUM(b.return_money) AS return_money
- FROM jianyu.dataexport_order a
- LEFT JOIN return_money_record b ON (a.order_code=b.order_code)
- WHERE a.order_code=? AND a.order_status=1 AND (a.product_type='大会员' OR a.product_type='大会员-子账号' or a.product_type = "企业商机管理")
- GROUP BY a.id ORDER BY a.autoUpdate`, orderCode)
- data := &OrderInfo{}
- if orderData != nil && len(*orderData) > 0 {
- id := common.Int64All((*orderData)[0]["id"])
- user_phone := common.ObjToString((*orderData)[0]["user_phone"])
- user_id := common.ObjToString((*orderData)[0]["user_id"])
- product_type := common.ObjToString((*orderData)[0]["product_type"])
- serviceList := []string{}
- if filter := common.ObjToString((*orderData)[0]["filter"]); filter != "" {
- filterMap := map[string]interface{}{}
- json.Unmarshal([]byte(filter), &filterMap)
- if level := common.Int64All(filterMap["level"]); level == 5 {
- product_type += "自定义版"
- for _, serversId := range strings.Split(common.ObjToString(filterMap["serversId"]), ",") {
- if serviceName := bigmemberService[common.Int64All(serversId)]; serviceName != "" {
- serviceList = append(serviceList, serviceName)
- }
- }
- } else {
- product_type += combo[common.Int64All(filterMap["comboId"])]
- }
- }
- //联系人姓名
- sqlStr := `SELECT
- a.phone,
- b.NAME AS bname
- FROM
- dwd_f_userbase_contacts a
- INNER JOIN dwd_f_crm_clue_info b ON ( a.phone = ? AND a.baseinfo_id = b.uid )
- LEFT JOIN dwd_f_csm_customer_info c ON ( b.id = c.clue_id)`
- userName := ""
- if user_phone != "" {
- userData := TiDb.SelectBySql(sqlStr, user_phone)
- if userData != nil && len(*userData) > 0 {
- userName = gconv.String((*userData)[0]["bname"])
- }
- }
- data = &OrderInfo{
- Id: id,
- CompanyName: common.ObjToString((*orderData)[0]["company_name"]),
- UserId: user_id,
- ProductType: product_type,
- OrderCode: orderCode,
- PayMoney: common.Float64All((*orderData)[0]["pay_money"]) / 100,
- ReturnMoney: common.Float64All((*orderData)[0]["return_money"]) / 100,
- UserPhone: user_phone,
- StartEnd: fmt.Sprintf("%s--%s", strings.Split(common.ObjToString((*orderData)[0]["vip_starttime"]), " ")[0], strings.Split(common.ObjToString((*orderData)[0]["vip_endtime"]), " ")[0]),
- SeriveList: serviceList,
- SaleName: common.ObjToString((*orderData)[0]["create_person"]),
- UserName: userName,
- }
- }
- return data
- }
- func SaleMail(positionId int64) string {
- person, admin, seniorAdmin := FindDeptAdmin(positionId)
- personMail := ""
- adminMail := ""
- seniorAdminMail := ""
- if person != nil {
- personMail, _ = GetMail(gconv.String(person["name"]))
- }
- if admin != nil {
- adminMail, _ = GetMail(gconv.String(admin["name"]))
- }
- if seniorAdmin != nil {
- seniorAdminMail, _ = GetMail(gconv.String(seniorAdmin["name"]))
- }
- to, cc := "", ""
- if personMail != "" {
- to = personMail
- if adminMail != "" && seniorAdminMail != "" {
- if to != adminMail {
- if to != seniorAdminMail {
- cc = fmt.Sprintf("%s,%s", adminMail, seniorAdminMail)
- }
- } else {
- if to != seniorAdminMail {
- cc = seniorAdminMail
- }
- }
- } else if seniorAdminMail != "" && to != seniorAdminMail {
- cc = seniorAdminMail
- }
- } else {
- if adminMail != "" {
- to = adminMail
- if seniorAdminMail != "" && to != seniorAdminMail {
- cc = seniorAdminMail
- }
- } else {
- to = seniorAdminMail
- }
- }
- if to != "" && cc != "" {
- to = fmt.Sprintf("%s|%s", to, cc)
- }
- return to
- }
|