clue.go 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/message/db"
  4. "context"
  5. "database/sql"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/gogf/gf/v2/util/gconv"
  9. "io/ioutil"
  10. "log"
  11. "math"
  12. "net/http"
  13. "net/url"
  14. "regexp"
  15. "strings"
  16. "sync"
  17. "sync/atomic"
  18. "time"
  19. "app.yhyue.com/moapp/jybase/mongodb"
  20. "app.yhyue.com/moapp/jybase/common"
  21. "app.yhyue.com/moapp/jybase/date"
  22. "app.yhyue.com/moapp/jybase/es"
  23. . "bp.jydev.jianyu360.cn/BaseService/biService/entity"
  24. "bp.jydev.jianyu360.cn/BaseService/biService/rpc/biservice"
  25. "github.com/zeromicro/go-zero/core/logx"
  26. )
  27. const (
  28. competitors = "www.chinabidding.cn,www.yingcaicheng.com,cg.zbj.com,www.chinabidding.cn,bid.cyc-fund.com.cn,www.zbytb.com"
  29. )
  30. func ClueImportTt(this *biservice.ClueImportReq) *biservice.ClueImportResp {
  31. result, status := ClueImportTtSync(this)
  32. return &biservice.ClueImportResp{
  33. ErrorCode: 0,
  34. Data: &biservice.ClueImport{
  35. Status: int64(status),
  36. Result: result,
  37. },
  38. }
  39. }
  40. func ClueImportTtSync(this *biservice.ClueImportReq) (string, int) {
  41. if ClueImportTtLock.TryLock() {
  42. defer ClueImportTtLock.Unlock()
  43. result, status, counts := "导入成功", 1, int64(0)
  44. data := BiService.Find("customer_data_temp", map[string]interface{}{"import_pc": this.Pcbh}, "", "", -1, -1)
  45. if data != nil && len(*data) > 0 {
  46. wg := new(sync.WaitGroup)
  47. ch := make(chan bool, 20)
  48. for _, v := range *data {
  49. wg.Add(1)
  50. ch <- true
  51. go func(v map[string]interface{}) {
  52. defer func() {
  53. wg.Done()
  54. <-ch
  55. }()
  56. v["isCompetitors"] = "否"
  57. id := common.ObjToString(v["id"])
  58. href := common.ObjToString(v["href"])
  59. for _, vv := range strings.Split(competitors, ",") {
  60. if strings.Contains(href, vv) {
  61. v["isCompetitors"] = "是"
  62. }
  63. }
  64. delete(v, "temp_id")
  65. delete(v, "import_pc")
  66. delete(v, "projectId")
  67. id_new := common.GetMd5String(fmt.Sprintf("%s_%d", common.ObjToString(v["id"]), common.Int64All(v["multipackage_id"])))
  68. v["id_new"] = id_new
  69. if BiService.Count("customer_data_ttzl", map[string]interface{}{"id_new": id_new}) > 0 {
  70. logx.Info("数据重复", id)
  71. return
  72. }
  73. if dataId := BiService.Insert("customer_data_ttzl", v); dataId > 0 {
  74. if details, ok := CreateEs(v); ok {
  75. BiService.Insert("customer_data_ttzl_gl", map[string]interface{}{"msg_id": dataId, "info_id": id, "details": details, "isHistory": "否"})
  76. atomic.AddInt64(&counts, 1)
  77. logx.Info("es存入成功", id)
  78. } else {
  79. logx.Info("es存入失败!!", id)
  80. }
  81. } else {
  82. logx.Info("tidb存入失败!!", id)
  83. }
  84. }(v)
  85. }
  86. wg.Wait()
  87. }
  88. BiService.Update("customer_data_import_record", map[string]interface{}{"import_pc": this.Pcbh}, map[string]interface{}{
  89. "import_end_time": time.Now().Format(date.Date_Full_Layout),
  90. "import_count": counts,
  91. "import_res": result,
  92. })
  93. return result, status
  94. } else {
  95. BiService.Update("customer_data_import_record", map[string]interface{}{"import_pc": this.Pcbh}, map[string]interface{}{
  96. "import_end_time": time.Now().Format(date.Date_Full_Layout),
  97. "import_res": "导入失败,有正在进行的导入任务",
  98. })
  99. return "有正在进行的导入任务", 2
  100. }
  101. }
  102. // 根据id重新生成es
  103. func CreateEsById(newId string) bool {
  104. data := BiService.FindOne("customer_data_ttzl", map[string]interface{}{"id_new": newId}, "", "")
  105. if data == nil || len(*data) == 0 {
  106. logx.Info("没有找到该数据", newId)
  107. return false
  108. }
  109. _, ok := CreateEs(*data)
  110. return ok
  111. }
  112. // 重新生成es
  113. func CreateEs(v map[string]interface{}) (string, bool) {
  114. details := ""
  115. biddingData, ok := Bidding.FindOne("bidding", map[string]interface{}{"_id": mongodb.StringTOBsonId(common.ObjToString(v["id"]))})
  116. if ok && biddingData != nil && len(*biddingData) > 0 {
  117. details = common.ObjToString((*biddingData)["detail"])
  118. }
  119. delete(v, "isCompetitors")
  120. delete(v, "msg_id")
  121. v["details"] = cleanHTML(details)
  122. if common.ObjToString(v["publishtime"]) != "" {
  123. v["publishtime2"] = common.ObjToString(v["publishtime"])
  124. publishtime, _ := time.ParseInLocation(date.Date_Full_Layout, common.ObjToString(v["publishtime"]), time.Local)
  125. v["publishtime"] = publishtime.UnixMilli()
  126. } else {
  127. delete(v, "publishtime")
  128. }
  129. for k, vv := range v {
  130. if vv == nil {
  131. delete(v, k)
  132. }
  133. }
  134. //
  135. esV7 := Es.(*es.EsV7)
  136. client := esV7.GetEsConn()
  137. defer esV7.DestoryEsConn(client)
  138. newId := common.ObjToString(v["id_new"])
  139. client.Delete().Index("ttbid").Id(newId).Refresh("true").Do(context.TODO())
  140. _, err := client.Index().Index("ttbid").Id(newId).BodyJson(v).Refresh("true").Do(context.TODO())
  141. if err != nil {
  142. logx.Info("保存到ES出错", err.Error())
  143. return details, false
  144. } else {
  145. return details, true
  146. }
  147. }
  148. func DelById(index, id string) bool {
  149. esV7 := Es.(*es.EsV7)
  150. client := esV7.GetEsConn()
  151. defer esV7.DestoryEsConn(client)
  152. _, err := client.Delete().Index(index).Id(id).Refresh("true").Do(context.TODO())
  153. if err != nil {
  154. logx.Info("删除es出错:", id, err)
  155. return false
  156. }
  157. return true
  158. }
  159. func cleanHTML(htmlString string) string {
  160. // 清理HTML标签
  161. htmlRegex := regexp.MustCompile("<[^>]*>")
  162. cleanedString := htmlRegex.ReplaceAllString(htmlString, "")
  163. // 清理换行符和制表符
  164. cleanedString = strings.ReplaceAll(cleanedString, "\n", "")
  165. cleanedString = strings.ReplaceAll(cleanedString, "\t", "")
  166. // 清理多余的空格
  167. cleanedString = strings.TrimSpace(cleanedString)
  168. return cleanedString
  169. }
  170. func ClueAdd(this *biservice.ClueAddReq, col string) *biservice.AddProjectResp {
  171. status := 1
  172. nowTime := time.Now().Format(date.Date_Full_Layout)
  173. if this.Company == "" {
  174. this.Company = this.Phone
  175. }
  176. isGroup, isCommerce := CompanyType(this.Company)
  177. saveMap := map[string]interface{}{
  178. "unique_id": this.Phone,
  179. "phone": this.Phone,
  180. "username": this.Username,
  181. "source": this.Source,
  182. "status999": this.Status999,
  183. "owner": this.Owner,
  184. "empNo": this.EmpNo,
  185. "company": this.Company,
  186. "isPolicymaker": this.IsPolicymaker,
  187. "belongToIndustry": this.BelongToIndustry,
  188. "job": this.Job,
  189. "customerNeeds": this.CustomerNeeds,
  190. "belongTo": this.BelongTo,
  191. "wantGoods": this.WantGoods,
  192. "customerBudget": this.CustomerBudget,
  193. "createTime": nowTime,
  194. "lastUpdateTime": nowTime,
  195. }
  196. token := getToken()
  197. updateData := map[string]interface{}{
  198. "dbType": "0001",
  199. "customerList": []map[string]interface{}{saveMap},
  200. }
  201. dataByte, _ := json.Marshal(&updateData)
  202. url := `https://a1.7x24cc.com/commonInte?flag=1007&account=N000000029739&accessToken=` + token + `&json=` + url.QueryEscape(string(dataByte))
  203. bs, err := doGet(url)
  204. if err != nil {
  205. status = -1
  206. logx.Info("调用接口失败")
  207. } else {
  208. resMap := common.StringToMap(string(bs))
  209. if resMap["success"] != nil && resMap["success"].(bool) {
  210. saveMap["company_nature"] = isGroup
  211. saveMap["company_verification"] = isCommerce
  212. CallTidb.Insert(col, saveMap)
  213. } else {
  214. status = -1
  215. }
  216. }
  217. return &biservice.AddProjectResp{
  218. ErrorCode: 0,
  219. Data: &biservice.AddProject{
  220. Status: int64(status),
  221. },
  222. }
  223. }
  224. func DistributeClue(this *biservice.DistributeClueReq) *biservice.AddProjectResp {
  225. count, status := DistributeClueSync(this)
  226. logx.Info("分配数量 ", count)
  227. return &biservice.AddProjectResp{
  228. ErrorCode: 0,
  229. Data: &biservice.AddProject{
  230. Status: int64(status),
  231. Count: int64(count),
  232. },
  233. }
  234. }
  235. func getToken() string {
  236. url := "https://a1.7x24cc.com/accessToken?account=N000000029739&appid=w4w2ex0bnt1n61or&secret=3c8f7dd04d2c11edb786132b38c4d48a"
  237. bs, err := doGet(url)
  238. if err != nil {
  239. logx.Info("token生成失败", err)
  240. return ""
  241. }
  242. tokenMap := common.StringToMap(string(bs))
  243. if tokenMap["success"].(bool) {
  244. return common.ObjToString(tokenMap["accessToken"])
  245. }
  246. return ""
  247. }
  248. func DistributeClueSync(this *biservice.DistributeClueReq) (int, int) {
  249. if DistributeLock.TryLock() {
  250. defer DistributeLock.Unlock()
  251. saleMap, count := map[string]map[string]interface{}{}, 0
  252. saleData := JyBiTidb.SelectBySql("select * from dwd_f_crm_personnel_management where resign = 0")
  253. if saleData != nil && len(*saleData) > 0 {
  254. for _, v := range *saleData {
  255. name := common.ObjToString(v["name"])
  256. saleMap[name] = v
  257. }
  258. }
  259. for _, data := range this.Datas {
  260. seatNumber := common.ObjToString(saleMap[data.Name]["seat_number"])
  261. distributedCount := int(data.DistributedCount)
  262. if distributedCount > 0 {
  263. distributedArr := this.ClueIdList[count : count+distributedCount]
  264. count += distributedCount
  265. DistributeClueMore(saleMap, distributedArr, seatNumber, data.Name, data.PositionId, this.PositionId, this.IsTask)
  266. }
  267. }
  268. return count, 1
  269. } else {
  270. return 0, 2
  271. }
  272. }
  273. func DistributeClueMore(saleMap map[string]map[string]interface{}, distributedArr []int64, seatNumber, name string, positionId, thispositionId, isTask int64) {
  274. wg := new(sync.WaitGroup)
  275. ch := make(chan bool, 20)
  276. for _, v := range distributedArr {
  277. wg.Add(1)
  278. ch <- true
  279. go func(v int64) {
  280. defer func() {
  281. wg.Done()
  282. <-ch
  283. }()
  284. clueData := JyBiTidb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"id": v}, "", "")
  285. nowTime := time.Now().Format(date.Date_Full_Layout)
  286. if clueData != nil && len(*clueData) > 0 {
  287. isAssign := common.IntAll((*clueData)["is_assign"])
  288. clueSeatNumber := common.ObjToString((*clueData)["seatNumber"])
  289. oldName := ""
  290. trailstatus := common.ObjToString((*clueData)["trailstatus"])
  291. if clueSeatNumber != "" {
  292. for _, s := range saleMap {
  293. if common.ObjToString(s["seat_number"]) == clueSeatNumber {
  294. oldName = common.ObjToString(s["name"])
  295. }
  296. }
  297. }
  298. if isAssign == 1 {
  299. oldpositionId := common.Int64All((*clueData)["position_id"])
  300. updateClue := map[string]interface{}{
  301. "position_id": positionId,
  302. "seatNumber": seatNumber,
  303. "is_assign": 1,
  304. "updatetime": nowTime,
  305. "comeintime": nowTime,
  306. "comeinsource_private": 4,
  307. "level_open": nil,
  308. "clue_level": nil,
  309. "out_task_time": nil,
  310. "out_task_status": nil,
  311. // "comeinsource_open": nil,
  312. }
  313. if oldName != name {
  314. updateClue["start_trail_time"] = nil
  315. updateClue["next_trail_time"] = nil
  316. }
  317. if isTask == int64(1) {
  318. updateClue["is_task"] = 1
  319. updateClue["task_time"] = nowTime
  320. updateClue["tasktime"] = nowTime
  321. updateClue["taskstatus"] = 0
  322. updateClue["tasksource"] = "主动分配客户"
  323. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  324. "clue_id": v,
  325. "position_id": positionId,
  326. "change_type": "加入任务车",
  327. "new_value": "主动分配客户",
  328. "createtime": nowTime,
  329. "BCPCID": common.GetRandom(32),
  330. "operator_id": thispositionId,
  331. })
  332. }
  333. ok := JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": v}, updateClue)
  334. // ok := JyBiTidb.Update("dwd_f_crm_private_sea", map[string]interface{}{"clue_id": v}, map[string]interface{}{
  335. // "position_id": positionId,
  336. // "seatNumber": seatNumber,
  337. // "comeinsource": 4,
  338. // "comeintime": nowTime,
  339. // })
  340. if ok {
  341. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  342. "clue_id": v,
  343. "position_id": positionId,
  344. "change_field": "position_id",
  345. "change_type": "所属人变更",
  346. "old_value": oldName,
  347. "new_value": name,
  348. "createtime": nowTime,
  349. "BCPCID": common.GetRandom(32),
  350. "operator_id": thispositionId,
  351. })
  352. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  353. "clue_id": v,
  354. "position_id": oldpositionId,
  355. "change_field": "trailstatus",
  356. "change_type": "基本信息变更",
  357. "old_value": CodeTrail[trailstatus],
  358. "new_value": "流失",
  359. "createtime": nowTime,
  360. "BCPCID": common.GetRandom(32),
  361. "operator_id": thispositionId,
  362. })
  363. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  364. "clue_id": v,
  365. "position_id": positionId,
  366. "change_field": "trailstatus",
  367. "change_type": "基本信息变更",
  368. "old_value": CodeTrail[trailstatus],
  369. "new_value": "新增",
  370. "createtime": nowTime,
  371. "BCPCID": common.GetRandom(32),
  372. "operator_id": thispositionId,
  373. })
  374. } else {
  375. logx.Info("私海修改失败 ", v, positionId, seatNumber)
  376. }
  377. } else {
  378. updateClue := map[string]interface{}{
  379. "position_id": positionId,
  380. "seatNumber": seatNumber,
  381. "is_assign": 1,
  382. "updatetime": nowTime,
  383. "comeintime": nowTime,
  384. "comeinsource_private": 4,
  385. "is_task": 0,
  386. "taskstatus": 0,
  387. "level_open": nil,
  388. "clue_level": nil,
  389. "out_task_time": nil,
  390. "out_task_status": nil,
  391. "next_trail_time": nil,
  392. "start_trail_time": nil,
  393. // "comeinsource_open": nil,
  394. }
  395. updateClue["trailstatus"] = "01"
  396. if isTask == int64(1) {
  397. updateClue["is_task"] = 1
  398. updateClue["task_time"] = nowTime
  399. updateClue["tasktime"] = nowTime
  400. updateClue["taskstatus"] = 0
  401. updateClue["tasksource"] = "主动分配客户"
  402. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  403. "clue_id": v,
  404. "position_id": positionId,
  405. "change_type": "加入任务车",
  406. "new_value": "主动分配客户",
  407. "createtime": nowTime,
  408. "BCPCID": common.GetRandom(32),
  409. "operator_id": thispositionId,
  410. })
  411. }
  412. ok := JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": v}, updateClue)
  413. // seaId := JyBiTidb.Insert("dwd_f_crm_private_sea", map[string]interface{}{
  414. // "clue_id": v,
  415. // "position_id": positionId,
  416. // "seatNumber": seatNumber,
  417. // "comeinsource": 4,
  418. // "comeintime": nowTime,
  419. // "is_task": 0,
  420. // // "task_time": nowTime,
  421. // // "tasktime": nowTime,
  422. // "taskstatus": 0,
  423. // // "tasksource": "线索批量分配",
  424. // })
  425. if ok {
  426. // JyBiTidb.Delete("dwd_f_crm_open_sea", map[string]interface{}{"clue_id": v})
  427. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  428. "clue_id": v,
  429. "position_id": positionId,
  430. "change_field": "position_id",
  431. "change_type": "所属人变更",
  432. "old_value": "/",
  433. "new_value": name,
  434. "createtime": nowTime,
  435. "BCPCID": common.GetRandom(32),
  436. "operator_id": thispositionId,
  437. })
  438. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  439. "clue_id": v,
  440. "position_id": positionId,
  441. "change_field": "trailstatus",
  442. "change_type": "基本信息变更",
  443. "old_value": "商机线索",
  444. "new_value": "新增",
  445. "createtime": nowTime,
  446. "BCPCID": common.GetRandom(32),
  447. "operator_id": thispositionId,
  448. })
  449. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  450. "clue_id": v,
  451. "position_id": positionId,
  452. "change_field": "trailstatus",
  453. "change_type": "基本信息变更",
  454. "old_value": CodeTrail[trailstatus],
  455. "new_value": "商机线索",
  456. "createtime": nowTime,
  457. "BCPCID": common.GetRandom(32),
  458. "operator_id": thispositionId,
  459. })
  460. } else {
  461. logx.Info("私海插入失败 ", v, positionId, seatNumber)
  462. }
  463. }
  464. }
  465. }(v)
  466. }
  467. wg.Wait()
  468. }
  469. func DrawClue(this *biservice.DrawClueReq) *biservice.AddProjectResp {
  470. count, status := DrawClueSync(this)
  471. logx.Info("领取数量 ", count)
  472. return &biservice.AddProjectResp{
  473. ErrorCode: 0,
  474. Data: &biservice.AddProject{
  475. Status: int64(status),
  476. Count: int64(count),
  477. },
  478. }
  479. }
  480. func DrawClueSync(this *biservice.DrawClueReq) (int, int) {
  481. if DataLock.TryLock() {
  482. defer DataLock.Unlock()
  483. count1 := JyBiTidb.CountBySql(`select count(1) as count from dwd_f_crm_clue_info where level_open=1 and is_assign=0 and is_transfer != 1`)
  484. count2 := JyBiTidb.CountBySql(`select count(1) as count from dwd_f_crm_clue_info where level_open=2 and is_assign=0 and is_transfer != 1`)
  485. counts1, counts2, counts3 := int64(0), int64(0), int64(0)
  486. counts1 = int64(math.Ceil(float64(this.Count) / float64(10) * 5))
  487. if this.Count-counts1 == 0 {
  488. counts2 = 0
  489. counts3 = 0
  490. } else {
  491. counts2 = int64(math.Ceil(float64(this.Count) / float64(10) * 4))
  492. if this.Count-counts1-counts2 == 0 {
  493. counts3 = 0
  494. } else {
  495. counts3 = this.Count - counts1 - counts2
  496. }
  497. }
  498. if counts1 > count1 {
  499. counts2 += counts1 - count1
  500. counts1 = count1
  501. }
  502. if counts2 > count2 {
  503. counts3 += counts2 - count2
  504. counts2 = count2
  505. }
  506. logx.Info(count1, count2)
  507. logx.Info(counts1, counts2, counts3)
  508. return DrawClues(this.PositionId, counts1, counts2, counts3), 1
  509. } else {
  510. return 0, 2
  511. }
  512. }
  513. func DrawClues(positionId, count1, count2, count3 int64) int {
  514. nowTime := time.Now().Format("2006-01-02 15:04:05")
  515. seatNumber, name := getSeatNumber(positionId)
  516. drawCount := 0
  517. //data1, data2, data3, drawCount := &[]map[string]interface{}{}, &[]map[string]interface{}{}, &[]map[string]interface{}{}, 0
  518. /*if count1 > 0 {
  519. data1 = JyBiTidb.SelectBySql(`SELECT * from dwd_f_crm_clue_info where is_assign = 0 and level_open = ? and cluename in (SELECT cluename from dwd_f_crm_clue_info where position_id = ? GROUP BY cluename)`, 1, positionId)
  520. if data1 != nil && len(*data1) > 0 {
  521. if int64(len(*data1)) >= count1 {
  522. data11 := (*data1)[0:count1]
  523. data1 = &data11
  524. count1 = 0
  525. } else {
  526. count1 = count1 - int64(len(*data1))
  527. }
  528. }
  529. }
  530. if count2 > 0 {
  531. data2 = JyBiTidb.SelectBySql(`SELECT * from dwd_f_crm_clue_info where is_assign = 0 and level_open = ? and cluename in (SELECT cluename from dwd_f_crm_clue_info where position_id = ? GROUP BY cluename)`, 2, positionId)
  532. if data2 != nil && len(*data2) > 0 {
  533. if int64(len(*data2)) >= count2 {
  534. data11 := (*data2)[0:count2]
  535. data2 = &data11
  536. count2 = 0
  537. } else {
  538. count2 = count2 - int64(len(*data2))
  539. }
  540. }
  541. }
  542. if count3 > 0 {
  543. data3 = JyBiTidb.SelectBySql(`SELECT * from dwd_f_crm_clue_info where is_assign = 0 and level_open = ? and cluename in (SELECT cluename from dwd_f_crm_clue_info where position_id = ? GROUP BY cluename)`, 3, positionId)
  544. if data3 != nil && len(*data3) > 0 {
  545. if int64(len(*data3)) >= count3 {
  546. data11 := (*data3)[0:count3]
  547. data3 = &data11
  548. count3 = 0
  549. } else {
  550. count3 = count3 - int64(len(*data3))
  551. }
  552. }
  553. }*/
  554. //
  555. /*if data1 != nil && len(*data1) > 0 {
  556. count := batchDraw(*data1, nowTime, seatNumber, name, positionId, "1")
  557. drawCount += count
  558. }
  559. if data2 != nil && len(*data2) > 0 {
  560. count := batchDraw(*data2, nowTime, seatNumber, name, positionId, "1")
  561. drawCount += count
  562. }
  563. if data3 != nil && len(*data3) > 0 {
  564. count := batchDraw(*data3, nowTime, seatNumber, name, positionId, "1")
  565. drawCount += count
  566. }*/
  567. //
  568. logx.Info("count1,count2,count3 ", count1, count2, count3, drawCount)
  569. if count1 > 0 {
  570. counts, counts1 := 0, 0
  571. for {
  572. logx.Info("等级1线索领取", count1, counts1, counts)
  573. data := JyBiTidb.SelectBySql(`SELECT a.label,a.labelChangeTime,a.last_ring_time as ctime,a.id,a.trailstatus,a.cluename FROM dwd_f_crm_clue_info a
  574. WHERE a.level_open = 1 AND a.is_assign = 0 AND a.uid !="" AND a.is_transfer != 1 GROUP BY a.id ORDER BY ctime asc limit ?,1`, counts1)
  575. counts1++
  576. if data != nil && len(*data) > 0 {
  577. count := batchDraw(*data, nowTime, seatNumber, name, positionId, "2")
  578. if count > 0 {
  579. counts++
  580. }
  581. }
  582. if counts == int(count1) {
  583. drawCount += int(count1)
  584. break
  585. }
  586. if counts1 > 5*int(count1) {
  587. count2 += count1 - int64(counts)
  588. drawCount += int(counts)
  589. break
  590. }
  591. }
  592. }
  593. if count2 > 0 {
  594. counts, counts1 := 0, 0
  595. for {
  596. logx.Info("等级2线索领取", count2, counts1, counts)
  597. data := JyBiTidb.SelectBySql(`SELECT a.label,a.labelChangeTime,a.last_ring_time as ctime,a.id,a.trailstatus,a.cluename FROM dwd_f_crm_clue_info a
  598. WHERE a.level_open = 2 AND a.is_assign = 0 AND a.uid !="" AND a.is_transfer != 1 GROUP BY a.id ORDER BY ctime asc limit ?,1`, counts1)
  599. counts1++
  600. if data != nil && len(*data) > 0 {
  601. count := batchDraw(*data, nowTime, seatNumber, name, positionId, "2")
  602. if count > 0 {
  603. counts++
  604. }
  605. }
  606. if counts == int(count2) {
  607. drawCount += int(count2)
  608. break
  609. }
  610. if counts1 > 5*int(count2) {
  611. count3 += count2 - int64(counts)
  612. drawCount += int(counts)
  613. break
  614. }
  615. }
  616. }
  617. if count3 > 0 {
  618. counts, counts1 := 0, 0
  619. for {
  620. logx.Info("等级3线索领取", count3, counts1, counts)
  621. data := JyBiTidb.SelectBySql(`SELECT a.label,a.labelChangeTime, a.last_ring_time as ctime,a.id,a.trailstatus,a.cluename FROM dwd_f_crm_clue_info a
  622. WHERE a.level_open = 3 AND a.is_assign = 0 AND a.uid !="" AND a.is_transfer != 1 GROUP BY a.id ORDER BY ctime asc limit ?,1`, counts1)
  623. counts1++
  624. if data != nil && len(*data) > 0 {
  625. count := batchDraw(*data, nowTime, seatNumber, name, positionId, "2")
  626. if count > 0 {
  627. counts++
  628. }
  629. }
  630. if counts == int(count3) {
  631. drawCount += int(count3)
  632. break
  633. }
  634. if counts1 > 5*int(count3) {
  635. drawCount += int(counts)
  636. break
  637. }
  638. }
  639. }
  640. return drawCount
  641. }
  642. func batchDraw(data []map[string]interface{}, nowTime, seatNumber, name string, positionId int64, dataType string) int {
  643. count := 0
  644. for _, v := range data {
  645. clueId := common.Int64All(v["id"])
  646. //cluename := common.ObjToString(v["cluename"])
  647. /* if dataType == "2" {
  648. cdata := JyBiMysql.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"cluename": cluename, "is_assign": 1, "company_nature": 0, "company_verification": 1}, "", "")
  649. if cdata != nil && len(*cdata) > 0 {
  650. salePositionId := common.Int64All((*cdata)["position_id"])
  651. if pcount := JyBiMysql.Count("dwd_f_crm_personnel_management", map[string]interface{}{"resign": 0, "position_id": salePositionId}); pcount > 0 {
  652. logx.Info("当前线索有在职销售 ", clueId, cluename)
  653. return count
  654. } else {
  655. saledata := JyBiMysql.SelectBySql(`SELECT position_id from dwd_d_crm_department_level_succbi WHERE bi_pcode in (SELECT bi_pcode from dwd_d_crm_department_level_succbi WHERE position_id = ?) AND resign = 0`, salePositionId)
  656. if saledata != nil && len(*saledata) > 0 {
  657. isOk := false
  658. for _, s := range *saledata {
  659. sid := common.Int64All(s["position_id"])
  660. if sid == positionId {
  661. isOk = true
  662. }
  663. }
  664. if !isOk {
  665. logx.Info("当前线索销售离职,只能同部门领取 ", clueId, cluename)
  666. return count
  667. }
  668. }
  669. }
  670. }
  671. }*/
  672. trailstatus := common.ObjToString(v["trailstatus"])
  673. if JyBiMysql.ExecTx("领取线索等", func(tx *sql.Tx) bool {
  674. updateClue := map[string]interface{}{
  675. "position_id": positionId,
  676. "seatNumber": seatNumber,
  677. "is_assign": 1,
  678. "updatetime": nowTime,
  679. "comeintime": nowTime,
  680. "comeinsource_private": 3,
  681. "is_task": 1,
  682. "task_time": nowTime,
  683. "tasktime": nowTime,
  684. "taskstatus": 0,
  685. "tasksource": "领取公海线索",
  686. "level_open": nil,
  687. "clue_level": nil,
  688. }
  689. updateClue["trailstatus"] = "01"
  690. ok1 := JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateClue)
  691. recordId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  692. "clue_id": clueId,
  693. "position_id": positionId,
  694. "change_field": "position_id",
  695. "change_type": "所属人变更",
  696. "old_value": "/",
  697. "new_value": name,
  698. "createtime": nowTime,
  699. "BCPCID": common.GetRandom(32),
  700. "operator_id": positionId,
  701. })
  702. recordId1 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  703. "clue_id": clueId,
  704. "position_id": positionId,
  705. "change_type": "领取公海线索",
  706. "createtime": nowTime,
  707. "BCPCID": common.GetRandom(32),
  708. "operator_id": positionId,
  709. })
  710. recordId2 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  711. "clue_id": clueId,
  712. "position_id": positionId,
  713. "change_type": "加入任务车",
  714. "new_value": "领取公海线索",
  715. "createtime": nowTime,
  716. "BCPCID": common.GetRandom(32),
  717. "operator_id": positionId,
  718. })
  719. recordId3, recordId4 := int64(0), int64(0)
  720. recordId3 = JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  721. "clue_id": clueId,
  722. "position_id": positionId,
  723. "change_field": "trailstatus",
  724. "change_type": "基本信息变更",
  725. "old_value": "商机线索",
  726. "new_value": "新增",
  727. "createtime": nowTime,
  728. "BCPCID": common.GetRandom(32),
  729. "operator_id": positionId,
  730. })
  731. recordId4 = JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  732. "clue_id": clueId,
  733. "position_id": positionId,
  734. "change_field": "trailstatus",
  735. "change_type": "基本信息变更",
  736. "old_value": CodeTrail[trailstatus],
  737. "new_value": "商机线索",
  738. "createtime": nowTime,
  739. "BCPCID": common.GetRandom(32),
  740. "operator_id": positionId,
  741. })
  742. //标签处理
  743. label := gconv.Int64(v["label"])
  744. trailstatusCode := gconv.String(v["trailstatus"])
  745. LabelHandel(tx, label, 3, clueId, "", "01", trailstatusCode, name, "/", 0)
  746. return ok1 && recordId > 0 && recordId1 > 0 && recordId2 > 0 && recordId3 > -1 && recordId4 > -1
  747. }) {
  748. logx.Info("领取线索成功")
  749. count++
  750. } else {
  751. logx.Info("领取线索失败")
  752. }
  753. }
  754. return count
  755. }
  756. func getSeatNumber(positionId int64) (seatNumber, name string) {
  757. saleData := JyBiTidb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{"position_id": positionId}, "", "")
  758. if saleData != nil && len(*saleData) > 0 {
  759. seatNumber = common.ObjToString((*saleData)["seat_number"])
  760. name = common.ObjToString((*saleData)["name"])
  761. }
  762. return
  763. }
  764. func ClueImport(this *biservice.ClueImportReq) *biservice.ClueImportResp {
  765. result, status := ClueImportSync(this)
  766. return &biservice.ClueImportResp{
  767. ErrorCode: 0,
  768. Data: &biservice.ClueImport{
  769. Status: int64(status),
  770. Result: result,
  771. },
  772. }
  773. }
  774. func ClueImportSync(this *biservice.ClueImportReq) (string, int) {
  775. if ClueImportLock.TryLock() {
  776. defer ClueImportLock.Unlock()
  777. result, status, dataArr, countOpen, countAdd, countPrivate := "", 1, []map[string]interface{}{}, 0, 0, 0
  778. phoneRegexp := regexp.MustCompile(`^1[0-9]{10}$`)
  779. nowTime, counts := time.Now().Format(date.Date_Full_Layout), 0
  780. data := JyBiTidb.Find("dwd_f_crm_bulk_import", map[string]interface{}{"PCBH": this.Pcbh}, "", "", -1, -1)
  781. if data != nil && len(*data) > 0 {
  782. for k, v := range *data {
  783. companyName := common.ObjToString(v["GSMC"])
  784. name := common.ObjToString(v["XM"])
  785. position := common.ObjToString(v["ZW"])
  786. phone := common.ObjToString(v["LXFS"])
  787. clueSource := common.ObjToString(v["XSXSLY"])
  788. if companyName == "" {
  789. companyName = phone
  790. }
  791. isOK, isOks, dataMap := false, true, map[string]interface{}{
  792. "cluename": companyName,
  793. "companyName": common.ObjToString(v["GSMC"]),
  794. "name": name,
  795. "phone": phone,
  796. "position": position,
  797. "clueSource": clueSource,
  798. }
  799. if phone == "" {
  800. isOks = false
  801. counts++
  802. result += fmt.Sprint(counts) + ".第" + fmt.Sprint(k+2) + "行,“联系方式为空”"
  803. } else {
  804. if !phoneRegexp.MatchString(phone) {
  805. if !isOks {
  806. result += ",“联系方式格式有误”"
  807. } else {
  808. counts++
  809. result += fmt.Sprint(counts) + ".第" + fmt.Sprint(k+2) + "行,“联系方式格式有误”"
  810. }
  811. isOks = false
  812. } else {
  813. userData := JyBiTidb.FindOne("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}, "", "")
  814. if userData != nil && len(*userData) > 0 {
  815. source := common.ObjToString((*userData)["source"])
  816. userId, belong_to := "", ""
  817. uId := common.ObjToString((*userData)["baseinfo_id"])
  818. baseData := JyBiTidb.FindOne("dwd_f_userbase_baseinfo", map[string]interface{}{"uid": uId}, "", "")
  819. if baseData != nil {
  820. userId = common.ObjToString((*baseData)["userid"])
  821. belong_to = common.ObjToString((*baseData)["belong_to"])
  822. }
  823. if source == "0205" {
  824. if !isOks {
  825. result += ",“该线索已归属域外”"
  826. } else {
  827. counts++
  828. result += fmt.Sprint(counts) + ".第" + fmt.Sprint(k+2) + "行,“该线索已归属域外”"
  829. }
  830. isOks = false
  831. } else if strings.HasPrefix(belong_to, "03") {
  832. changeMap := db.Tidb.FindOne("dwd_d_userbase_belongto_rulecode", map[string]interface{}{"code": belong_to}, "", "")
  833. changeName := ""
  834. if changeMap != nil && len(*changeMap) > 0 {
  835. changeName = gconv.String((*changeMap)["name"])
  836. }
  837. if !isOks {
  838. result += fmt.Sprintf(`,“该线索已归属合作渠道%s”`, changeName)
  839. } else {
  840. counts++
  841. result += fmt.Sprint(counts) + ".第" + fmt.Sprint(k+2) + fmt.Sprintf(`行,“该线索已归属合作渠道%s”`, changeName)
  842. }
  843. isOks = false
  844. } else {
  845. isOK = true
  846. dataMap["userId"] = userId
  847. dataMap["uId"] = uId
  848. }
  849. } else {
  850. dataMap["uId"] = common.GetRandom(32)
  851. dataMap["isOk"] = "0"
  852. }
  853. }
  854. }
  855. if clueSource == "" {
  856. if !isOks {
  857. result += ",“销售线索来源为空”"
  858. } else {
  859. counts++
  860. result += fmt.Sprint(counts) + ".第" + fmt.Sprint(k+2) + "行,“销售线索来源为空”"
  861. }
  862. isOks = false
  863. } else {
  864. sourceData := JyBiTidb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"name": clueSource}, "", "")
  865. if sourceData == nil || len(*sourceData) == 0 {
  866. if !isOks {
  867. result += ",“销售线索来源不存在”"
  868. } else {
  869. counts++
  870. result += fmt.Sprint(counts) + ".第" + fmt.Sprint(k+2) + "行,“销售线索来源不存在”"
  871. }
  872. isOks = false
  873. } else {
  874. isOK = true
  875. code := common.ObjToString((*sourceData)["code"])
  876. pcode := common.ObjToString((*sourceData)["pcode"])
  877. dataMap["top_cluetype"] = pcode
  878. dataMap["sub_cluetype"] = code
  879. dataMap["clue_level"] = gconv.String((*sourceData)["clue_level"])
  880. }
  881. }
  882. if isOK {
  883. dataArr = append(dataArr, dataMap)
  884. }
  885. if !isOks {
  886. result += "\n"
  887. }
  888. }
  889. if result != "" {
  890. status = -1
  891. JyBiTidb.Update("dwd_f_crm_bulk_import_record", map[string]interface{}{"PCBH": this.Pcbh}, map[string]interface{}{
  892. "DRJSSJ": nowTime, //结束时间
  893. "SBYY": result, //失败原因
  894. "DRZT": "导入失败",
  895. })
  896. } else {
  897. for _, v := range dataArr {
  898. phone := common.ObjToString(v["phone"])
  899. cluename := common.ObjToString(v["cluename"])
  900. name := common.ObjToString(v["name"])
  901. level := gconv.String(v["clue_level"])
  902. position := common.ObjToString(v["position"])
  903. isGroup, isCommerce := CompanyType(cluename)
  904. clueData := JyBiTidb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"phone": phone}, "", "")
  905. if clueData == nil || len(*clueData) == 0 {
  906. countAdd++
  907. if v["isOk"] != nil {
  908. JyBiTidb.Insert("dwd_f_userbase_baseinfo", map[string]interface{}{
  909. "userid": v["userId"],
  910. "uid": v["uId"],
  911. "name": name,
  912. "phone": phone,
  913. "belong_to": "0100",
  914. "source": "0203",
  915. "company_name": cluename,
  916. "status": 3,
  917. "createtime": nowTime,
  918. "updatetime": nowTime,
  919. })
  920. }
  921. clueId := JyBiTidb.Insert("dwd_f_crm_clue_info", map[string]interface{}{
  922. "userid": v["userId"],
  923. "uid": v["uId"],
  924. "is_assign": 0,
  925. "comeintime": nowTime,
  926. "createtime": nowTime,
  927. "updatetime": nowTime,
  928. "cluename": cluename,
  929. "top_cluetype": v["top_cluetype"],
  930. "sub_cluetype": v["sub_cluetype"],
  931. "trailstatus": "01",
  932. "name": name,
  933. "phone": phone,
  934. "position": position,
  935. "batch_import": this.Pcbh,
  936. "comeintime_open": nowTime,
  937. "comeinsource_open": 1,
  938. "company_nature": isGroup,
  939. "company_verification": isCommerce,
  940. })
  941. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  942. "clue_id": clueId,
  943. "position_id": -1,
  944. "change_type": "创建线索",
  945. "new_value": "线索导入创建",
  946. "createtime": nowTime,
  947. "BCPCID": common.GetRandom(32),
  948. "operator_id": this.PositionId,
  949. })
  950. if JyBiTidb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
  951. JyBiTidb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  952. "status": 1,
  953. "is_delete": 1,
  954. "createtime": nowTime,
  955. "updatetime": nowTime,
  956. "phone": phone,
  957. "baseinfo_id": v["uId"],
  958. "source": "0203",
  959. })
  960. }
  961. } else {
  962. isUpdate := gconv.Int64((*clueData)["is_artificially_modified"])
  963. if isUpdate == 1 {
  964. cluename = ""
  965. } else {
  966. if gconv.String(v["companyName"]) != "" {
  967. cluename = gconv.String(v["companyName"])
  968. } else {
  969. cluename = ""
  970. }
  971. }
  972. is_assign := common.IntAll((*clueData)["is_assign"])
  973. clueId := common.Int64All((*clueData)["id"])
  974. batch_import := common.ObjToString((*clueData)["batch_import"])
  975. old_cluename := common.ObjToString((*clueData)["cluename"])
  976. old_name := common.ObjToString((*clueData)["name"])
  977. old_position := common.ObjToString((*clueData)["position"])
  978. old_top_cluetype := common.ObjToString((*clueData)["top_cluetype"])
  979. old_sub_cluetype := common.ObjToString((*clueData)["sub_cluetype"])
  980. top_cluetype := common.ObjToString(v["top_cluetype"])
  981. sub_cluetype := common.ObjToString(v["sub_cluetype"])
  982. label := gconv.Int64(v["label"])
  983. trailstatusCode := gconv.String(v["trailstatus"])
  984. old_topname, old_subname := "", ""
  985. if old_top_cluetype != "" {
  986. pcodeData := JyBiTidb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_top_cluetype}, "", "")
  987. if pcodeData != nil && len(*pcodeData) > 0 {
  988. old_topname = common.ObjToString((*pcodeData)["name"])
  989. }
  990. }
  991. if old_sub_cluetype != "" {
  992. pcodeData := JyBiTidb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_sub_cluetype}, "", "")
  993. if pcodeData != nil && len(*pcodeData) > 0 {
  994. old_subname = common.ObjToString((*pcodeData)["name"])
  995. }
  996. }
  997. positionId := common.Int64All((*clueData)["position_id"])
  998. batch_imports := ""
  999. if is_assign == 0 {
  1000. countOpen++
  1001. if batch_import != "" {
  1002. batch_imports = batch_import + "," + this.Pcbh
  1003. } else {
  1004. batch_imports = this.Pcbh
  1005. }
  1006. updateData := map[string]interface{}{
  1007. "batch_import": batch_imports,
  1008. "updatetime": nowTime,
  1009. "top_cluetype": top_cluetype,
  1010. "sub_cluetype": sub_cluetype,
  1011. }
  1012. if cluename != "" && cluename != old_cluename {
  1013. updateData["cluename"] = cluename
  1014. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  1015. "clue_id": clueId,
  1016. "position_id": -1,
  1017. "change_field": "cluename",
  1018. "change_type": "基本信息变更",
  1019. "old_value": common.If(old_cluename == "", "/", old_cluename),
  1020. "new_value": cluename,
  1021. "createtime": nowTime,
  1022. "BCPCID": common.GetRandom(32),
  1023. "operator_id": this.PositionId,
  1024. })
  1025. }
  1026. if name != "" && name != old_name {
  1027. updateData["name"] = name
  1028. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  1029. "clue_id": clueId,
  1030. "position_id": -1,
  1031. "change_field": "name",
  1032. "change_type": "基本信息变更",
  1033. "old_value": common.If(old_name == "", "/", old_name),
  1034. "new_value": name,
  1035. "createtime": nowTime,
  1036. "BCPCID": common.GetRandom(32),
  1037. "operator_id": this.PositionId,
  1038. })
  1039. }
  1040. if position != "" && position != old_position {
  1041. updateData["position"] = position
  1042. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  1043. "clue_id": clueId,
  1044. "position_id": -1,
  1045. "change_field": "position",
  1046. "change_type": "基本信息变更",
  1047. "old_value": common.If(old_position == "", "/", old_position),
  1048. "new_value": position,
  1049. "createtime": nowTime,
  1050. "BCPCID": common.GetRandom(32),
  1051. "operator_id": this.PositionId,
  1052. })
  1053. }
  1054. if top_cluetype != "" && top_cluetype != old_top_cluetype {
  1055. updateData["position"] = position
  1056. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  1057. "clue_id": clueId,
  1058. "position_id": -1,
  1059. "change_field": "top_cluetype",
  1060. "change_type": "基本信息变更",
  1061. "old_value": common.If(old_topname == "", "/", old_topname),
  1062. "new_value": "批量导入线索",
  1063. "createtime": nowTime,
  1064. "BCPCID": common.GetRandom(32),
  1065. "operator_id": this.PositionId,
  1066. })
  1067. }
  1068. if sub_cluetype != "" && sub_cluetype != old_sub_cluetype {
  1069. updateData["position"] = position
  1070. taskstatus := gconv.Int64((*clueData)["taskstatus"])
  1071. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  1072. "clue_id": clueId,
  1073. "position_id": -1,
  1074. "change_field": "sub_cluetype", //111
  1075. "change_type": "基本信息变更",
  1076. "old_value": common.If(old_subname == "", "/", old_subname),
  1077. "new_value": v["clueSource"],
  1078. "createtime": nowTime,
  1079. "BCPCID": common.GetRandom(32),
  1080. "operator_id": this.PositionId,
  1081. })
  1082. LabelHandel(nil, label, 2, clueId, level, trailstatusCode, trailstatusCode, "", "", taskstatus)
  1083. }
  1084. JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateData)
  1085. } else if is_assign == 1 {
  1086. countPrivate++
  1087. if batch_import != "" {
  1088. batch_imports = batch_import + "," + this.Pcbh
  1089. } else {
  1090. batch_imports = this.Pcbh
  1091. }
  1092. updateData := map[string]interface{}{
  1093. "batch_import": batch_imports,
  1094. "updatetime": nowTime,
  1095. "top_cluetype": v["top_cluetype"],
  1096. "sub_cluetype": v["sub_cluetype"],
  1097. }
  1098. updateData["is_task"] = 1
  1099. updateData["task_time"] = nowTime
  1100. updateData["tasktime"] = nowTime
  1101. updateData["taskstatus"] = 0
  1102. updateData["tasksource"] = "线索来源更新客户"
  1103. if cluename != "" && cluename != old_cluename {
  1104. updateData["cluename"] = cluename
  1105. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  1106. "clue_id": clueId,
  1107. "position_id": positionId,
  1108. "change_field": "cluename",
  1109. "change_type": "基本信息变更",
  1110. "old_value": common.If(old_cluename == "", "/", old_cluename),
  1111. "new_value": cluename,
  1112. "createtime": nowTime,
  1113. "BCPCID": common.GetRandom(32),
  1114. "operator_id": this.PositionId,
  1115. })
  1116. }
  1117. if name != "" && name != old_name {
  1118. updateData["name"] = name
  1119. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  1120. "clue_id": clueId,
  1121. "position_id": positionId,
  1122. "change_field": "name",
  1123. "change_type": "基本信息变更",
  1124. "old_value": common.If(old_name == "", "/", old_name),
  1125. "new_value": name,
  1126. "createtime": nowTime,
  1127. "BCPCID": common.GetRandom(32),
  1128. "operator_id": this.PositionId,
  1129. })
  1130. }
  1131. if position != "" && position != old_position {
  1132. updateData["position"] = position
  1133. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  1134. "clue_id": clueId,
  1135. "position_id": positionId,
  1136. "change_field": "position",
  1137. "change_type": "基本信息变更",
  1138. "old_value": common.If(old_position == "", "/", old_position),
  1139. "new_value": position,
  1140. "createtime": nowTime,
  1141. "BCPCID": common.GetRandom(32),
  1142. "operator_id": this.PositionId,
  1143. })
  1144. }
  1145. if top_cluetype != "" && top_cluetype != old_top_cluetype {
  1146. updateData["position"] = position
  1147. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  1148. "clue_id": clueId,
  1149. "position_id": positionId,
  1150. "change_field": "top_cluetype",
  1151. "change_type": "基本信息变更",
  1152. "old_value": common.If(old_topname == "", "/", old_topname),
  1153. "new_value": "批量导入线索",
  1154. "createtime": nowTime,
  1155. "BCPCID": common.GetRandom(32),
  1156. "operator_id": this.PositionId,
  1157. })
  1158. taskstatus := gconv.Int64((*clueData)["taskstatus"])
  1159. LabelHandel(nil, label, 2, positionId, level, trailstatusCode, trailstatusCode, "", "", taskstatus)
  1160. }
  1161. if sub_cluetype != "" && sub_cluetype != old_sub_cluetype {
  1162. updateData["position"] = position
  1163. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  1164. "clue_id": clueId,
  1165. "position_id": positionId,
  1166. "change_field": "sub_cluetype", //222
  1167. "change_type": "基本信息变更",
  1168. "old_value": common.If(old_subname == "", "/", old_subname),
  1169. "new_value": v["clueSource"],
  1170. "createtime": nowTime,
  1171. "BCPCID": common.GetRandom(32),
  1172. "operator_id": this.PositionId,
  1173. })
  1174. }
  1175. JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateData)
  1176. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  1177. "clue_id": clueId,
  1178. "position_id": positionId,
  1179. "change_type": "加入任务车",
  1180. "new_value": "线索来源更新客户",
  1181. "createtime": nowTime,
  1182. "BCPCID": common.GetRandom(32),
  1183. "operator_id": this.PositionId,
  1184. })
  1185. }
  1186. }
  1187. }
  1188. JyBiTidb.Update("dwd_f_crm_bulk_import_record", map[string]interface{}{"PCBH": this.Pcbh}, map[string]interface{}{
  1189. "DRJSSJ": nowTime, //结束时间
  1190. "ZJXST": len(dataArr), //总计线索条数
  1191. "ZFXSSHXST": countPrivate, //私海重复条数
  1192. "ZFXSGHXST": countOpen, //公海重复条数
  1193. "XZXST": countAdd, //新增条数
  1194. "DRZT": "导入成功",
  1195. })
  1196. }
  1197. }
  1198. return result, status
  1199. } else {
  1200. JyBiTidb.Update("dwd_f_crm_bulk_import_record", map[string]interface{}{"PCBH": this.Pcbh}, map[string]interface{}{
  1201. "DRJSSJ": time.Now().Format(date.Date_Full_Layout), //结束时间
  1202. "SBYY": "有正在进行的导入任务", //失败原因
  1203. "DRZT": "导入失败",
  1204. })
  1205. return "有正在进行的导入任务", 2
  1206. }
  1207. }
  1208. func doGet(url string) ([]byte, error) {
  1209. req, err := http.NewRequest("GET", url, nil)
  1210. if err != nil {
  1211. return nil, err
  1212. }
  1213. resp, err := http.DefaultClient.Do(req)
  1214. if err != nil {
  1215. return nil, err
  1216. }
  1217. bs, err := ioutil.ReadAll(resp.Body)
  1218. if err != nil {
  1219. return nil, err
  1220. }
  1221. defer func() {
  1222. _ = resp.Body.Close()
  1223. }()
  1224. logx.Info(url, "调用结果 ", string(bs))
  1225. return bs, nil
  1226. }
  1227. func FindClueInfo(in *biservice.FindClueInfoReq) (map[string]interface{}, int64) {
  1228. data := map[string]interface{}{}
  1229. uId := ""
  1230. contactsData := JyBiMysql.SelectBySql("select * from dwd_f_userbase_contacts where phone = ? and is_delete = 1", in.Phone)
  1231. if contactsData != nil && len(*contactsData) > 0 {
  1232. if common.ObjToString((*contactsData)[0]["baseinfo_id"]) != "" {
  1233. uId = common.ObjToString((*contactsData)[0]["baseinfo_id"])
  1234. }
  1235. }
  1236. if uId == "" {
  1237. return data, 1
  1238. }
  1239. //线索查询
  1240. clueData := JyBiMysql.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", "")
  1241. if clueData != nil && len(*clueData) > 0 {
  1242. data["clue_id"] = gconv.Int64((*clueData)["id"])
  1243. data["position_id"] = in.PositionId
  1244. data["uid"] = gconv.String((*clueData)["uid"])
  1245. data["userid"] = gconv.String((*clueData)["userid"])
  1246. data["clue_position_id"] = gconv.Int64((*clueData)["position_id"])
  1247. } else {
  1248. return data, 1
  1249. }
  1250. return data, 0
  1251. }
  1252. // 线索标签修改 label labelChangeTime
  1253. func LabelHandel(tx *sql.Tx, oldlabelType, labelType, clueId int64, level, oldTrailstatusCode, trailstatusCode, name, oldName string, status int64) {
  1254. log.Println("线索标签修改:", "oldlabelType:", oldlabelType, "labelType:", labelType, "clueId:", clueId, "level:", level, "oldTrailstatusCode:", oldTrailstatusCode, "trailstatusCode:", trailstatusCode, "status:", status)
  1255. //先查询以前什么类型
  1256. if trailstatusCode != "01" && oldTrailstatusCode != "01" {
  1257. log.Println("不属于商机类型", clueId)
  1258. return
  1259. }
  1260. if status != 0 {
  1261. log.Println("不属于未完成", clueId)
  1262. return
  1263. }
  1264. updateLabelType := int64(0)
  1265. nowStr := time.Now().Format("2006-01-02")
  1266. if labelType == 2 {
  1267. if level != "A" && level != "B" && level != "C" {
  1268. return
  1269. }
  1270. //查询以前是什么数据
  1271. //人员变更
  1272. if oldlabelType != 1 {
  1273. updateLabelType = labelType
  1274. }
  1275. } else {
  1276. //查询以前是什么数据
  1277. if oldName != name {
  1278. updateLabelType = 3
  1279. }
  1280. }
  1281. if updateLabelType > 0 {
  1282. ok := false
  1283. if tx == nil {
  1284. ok = JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{
  1285. "id": clueId,
  1286. }, map[string]interface{}{
  1287. "label": updateLabelType,
  1288. "labelChangeTime": nowStr,
  1289. })
  1290. } else {
  1291. ok = JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{
  1292. "id": clueId,
  1293. }, map[string]interface{}{
  1294. "label": updateLabelType,
  1295. "labelChangeTime": nowStr,
  1296. })
  1297. }
  1298. log.Println("线索标签修改:", ok)
  1299. }
  1300. }