clue.go 46 KB

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