network.go 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "encoding/json"
  6. "fmt"
  7. "math"
  8. "sort"
  9. "strings"
  10. "sync"
  11. "time"
  12. . "app.yhyue.com/moapp/jybase/common"
  13. . "app.yhyue.com/moapp/jybase/date"
  14. "app.yhyue.com/moapp/jybase/encrypt"
  15. . "app.yhyue.com/moapp/jybase/es"
  16. "app.yhyue.com/moapp/jybase/redis"
  17. . "app.yhyue.com/moapp/jybase/sort"
  18. . "bp.jydev.jianyu360.cn/CRM/networkManage/api/common"
  19. "bp.jydev.jianyu360.cn/CRM/networkManage/api/internal/types"
  20. "github.com/shopspring/decimal"
  21. "github.com/zeromicro/go-zero/core/logx"
  22. )
  23. const (
  24. NetworkManageAllProjectKey = "networkManage_allProject_%d"
  25. NetworkManageList = "networkManage_networkList_%d_%s"
  26. )
  27. var Network = &network{}
  28. type network struct {
  29. }
  30. type networkTree struct {
  31. Count int64 `json:"count"`
  32. Name string `json:"name"`
  33. Children []*networkTreeChild `json:"children"`
  34. }
  35. type networkTreeChild struct {
  36. Count int64 `json:"count"`
  37. Id int64 `json:"id"`
  38. CompanyId string `json:"companyId"`
  39. CompanyName string `json:"companyName"`
  40. Type string `json:"type"`
  41. CreateTime string `json:"createTime"`
  42. IdNames []*idName `json:"idNames"`
  43. }
  44. func (n *networkTree) Len() int {
  45. return len(n.Children)
  46. }
  47. func (n *networkTree) Less(i, j int) bool {
  48. if n.Name == "甲方" {
  49. return n.Children[i].CompanyName < n.Children[j].CompanyName
  50. }
  51. it, err1 := time.ParseInLocation(Date_Full_Layout, n.Children[i].CreateTime, time.Local)
  52. if err1 != nil {
  53. return true
  54. }
  55. jt, err2 := time.ParseInLocation(Date_Full_Layout, n.Children[j].CreateTime, time.Local)
  56. if err2 != nil {
  57. return true
  58. }
  59. return it.Unix() < jt.Unix()
  60. }
  61. func (n *networkTree) Swap(i, j int) {
  62. n.Children[i], n.Children[j] = n.Children[j], n.Children[i]
  63. }
  64. type projectInfo struct {
  65. BuyerCount int64
  66. ProjectCount int64
  67. ProjectAmount float64
  68. ExportId []string
  69. IdNames []*idName
  70. }
  71. type firstpartyNetwork struct {
  72. CompanyId string
  73. CompanyName string
  74. Name string
  75. }
  76. type idName struct {
  77. Id string
  78. Name string
  79. }
  80. type introduceOwnerProject struct {
  81. Networks []*myNetwork
  82. FirstpartyNetwork map[string][]*firstpartyNetwork
  83. Firstparty map[string]*projectInfo
  84. Supplier map[string]*projectInfo
  85. Adiffb map[string]*projectInfo
  86. Agency map[string]*projectInfo
  87. Middleman map[string]*projectInfo
  88. }
  89. type myNetwork struct {
  90. Id int64
  91. Company_id string
  92. Company_name string
  93. Qyxy_id string
  94. Itype int64
  95. Person string
  96. Phone string
  97. Buyer_count int64
  98. Project_count int64
  99. Relate_buyer_id string
  100. Relate_buyer_name string
  101. Relate_project_id string
  102. Create_time string
  103. }
  104. type nodeTree struct {
  105. NAME string
  106. ID string
  107. SZ_PID0 string
  108. SZ_PID1 string
  109. SZ_PID2 string
  110. SZ_PID3 string
  111. CODE string
  112. PCODE string
  113. DATACOUNT int64
  114. SZ_LEVEL int
  115. SZ_LEAF int
  116. TYPE string
  117. MYID string
  118. MYTYPE string
  119. }
  120. type nodeTrees []*nodeTree
  121. func (n *nodeTrees) Len() int {
  122. return len(*n)
  123. }
  124. func (n *nodeTrees) Less(i, j int) bool {
  125. if (*n)[i].PCODE == (*n)[j].PCODE {
  126. return (*n)[i].CODE < (*n)[j].CODE
  127. }
  128. return (*n)[i].PCODE < (*n)[j].PCODE
  129. }
  130. func (n *nodeTrees) Swap(i, j int) {
  131. (*n)[i], (*n)[j] = (*n)[j], (*n)[i]
  132. }
  133. // 人脉库-添加/修改人脉
  134. func (n *network) AddOrUpdate(in *types.AddOrUpdateReq) *types.Reply {
  135. reply := &types.Reply{Data: map[string]interface{}{
  136. "status": 0,
  137. }}
  138. if in.Type != "middleman" && in.Company_id == "" {
  139. return reply
  140. }
  141. if in.Company_name == "" {
  142. in.Company_name = "未填写"
  143. }
  144. nowFormat := NowFormat(Date_Full_Layout)
  145. var saveIntroduce = func(tx *sql.Tx, cid int64, isUpdate bool) bool {
  146. if in.Type != "middleman" {
  147. return true
  148. } else if in.Introduce_owner_id == "" || in.Introduce_project_id == "" {
  149. return false
  150. }
  151. values := []interface{}{}
  152. ioi := strings.Split(in.Introduce_owner_id, ",")
  153. ioqi := strings.Split(in.Introduce_owner_qyxy_id, ",")
  154. ion := strings.Split(in.Introduce_owner_name, ",")
  155. if len(ioi) != len(ion) || len(ioi) != len(ioqi) {
  156. return false
  157. }
  158. for k, v := range ioi {
  159. values = append(values, in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, cid, ioqi[k], v, ion[k], 1, nowFormat)
  160. }
  161. ipi := strings.Split(in.Introduce_project_id, ",")
  162. ipn := strings.Split(in.Introduce_project_name, ",")
  163. if len(ipi) != len(ipn) {
  164. return false
  165. }
  166. for k, v := range ipi {
  167. values = append(values, in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, cid, nil, v, ipn[k], 2, nowFormat)
  168. }
  169. var r2 int64
  170. if isUpdate {
  171. r2 = CrmMysql.UpdateOrDeleteBySqlByTx(tx, `delete from crm.connection_introduce where connection_id=? and position_id=?`, in.Id, in.PositionId)
  172. }
  173. r3, _ := CrmMysql.InsertBatchByTx(tx, "crm.connection_introduce", []string{"position_id", "ent_id", "ent_dept_id", "ent_user_id", "connection_id", "qyxy_id", "relate_id", "relate_name", "itype", "create_time"}, values)
  174. return r2 >= 0 && r3 > 0
  175. }
  176. itype := n.TypeStrConvert(in.Type)
  177. if in.Id > 0 {
  178. if CrmMysql.ExecTx("更新人脉", func(tx *sql.Tx) bool {
  179. if in.Company_id != "" {
  180. count := CrmMysql.CountBySql(`select count(1) as count from crm.connection where position_id=? and company_id=? and itype=? and id<>? and status=1`, in.PositionId, in.Company_id, itype, in.Id)
  181. if count == -1 {
  182. return false
  183. } else if count > 0 {
  184. reply.Data = map[string]interface{}{
  185. "status": -1,
  186. }
  187. return false
  188. }
  189. }
  190. r1 := CrmMysql.UpdateOrDeleteBySqlByTx(tx, `update crm.connection set company_name=?,company_id=?,contact_person=?,contact_phone=?,source=1,update_time=? where id=? and position_id=?`, in.Company_name, in.Company_id, in.Contact_person, in.Contact_phone, nowFormat, in.Id, in.PositionId)
  191. return r1 >= 0 && saveIntroduce(tx, in.Id, true)
  192. }) {
  193. reply.Data = map[string]interface{}{
  194. "status": 1,
  195. }
  196. }
  197. } else {
  198. if in.Company_id != "" {
  199. count := CrmMysql.CountBySql(`select count(1) as count from crm.connection where position_id=? and company_id=? and itype=? and status=1`, in.PositionId, in.Company_id, itype)
  200. if count == -1 {
  201. return reply
  202. } else if count > 0 {
  203. reply.Data = map[string]interface{}{
  204. "status": -1,
  205. }
  206. return reply
  207. }
  208. }
  209. var r1 int64
  210. if CrmMysql.ExecTx("新增人脉", func(tx *sql.Tx) bool {
  211. _, r1 = CrmMysql.InsertBatchByTx(tx, "crm.connection", []string{"position_id", "ent_id", "ent_dept_id", "ent_user_id", "itype", "company_name", "company_id", "qyxy_id", "contact_person", "contact_phone", "status", "create_time", "update_time"}, []interface{}{in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, itype, in.Company_name, in.Company_id, in.Qyxy_id, in.Contact_person, in.Contact_phone, 1, nowFormat, nowFormat})
  212. return r1 > 0 && saveIntroduce(tx, r1, false)
  213. }) {
  214. reply.Data = map[string]interface{}{
  215. "status": 1,
  216. "id": r1,
  217. }
  218. }
  219. }
  220. n.DeleteCache(in.PositionId)
  221. return reply
  222. }
  223. // 人脉库-业主名称联想
  224. func (n *network) Associate(in *types.AssociateReq) (reply *types.Reply) {
  225. //类型;firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 middleman_owner:中间人-业主 middleman_project:中间人-项目 agency:招标代理机构
  226. res := []map[string]string{}
  227. reply = &types.Reply{Data: res}
  228. in.Name = strings.TrimSpace(in.Name)
  229. if in.Name == "" {
  230. return
  231. }
  232. pageSize := 10
  233. if in.Type == "adiffb" {
  234. businessType := strings.Split(FindBusiness(in.EntId, in.MgoUserId), ",")
  235. if len(businessType) > 0 {
  236. args := []interface{}{in.EntName}
  237. wh, newArgs := WhArgs(businessType)
  238. args = append(args, newArgs...)
  239. q := `select DISTINCT winner_id,winner from information.transaction_info_all prewhere buyer_id in (select buyer_id from information.transaction_info_all prewhere has(winner, ?) and buyer_id<>'') and hasAny(topscopeclass,[` + wh + `])=0 ORDER BY project_id`
  240. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  241. if err != nil {
  242. logx.Error(err)
  243. } else {
  244. repeat := map[string]bool{}
  245. ids := []string{}
  246. for rows.Next() {
  247. var (
  248. winner_id []string
  249. winner []string
  250. )
  251. if err := rows.Scan(&winner_id, &winner); err != nil {
  252. logx.Error(err)
  253. continue
  254. }
  255. for k, v := range winner {
  256. if repeat[v] || !strings.Contains(v, in.Name) {
  257. continue
  258. }
  259. repeat[v] = true
  260. if k >= len(winner_id) {
  261. continue
  262. }
  263. ids = append(ids, winner_id[k])
  264. res = append(res, map[string]string{
  265. "company_name": v,
  266. "company_id": winner_id[k],
  267. })
  268. if len(res) == pageSize {
  269. break
  270. }
  271. }
  272. }
  273. rows.Close()
  274. if err := rows.Err(); err != nil {
  275. logx.Error(err)
  276. }
  277. qyxyId := n.GetQyxyId(ids)
  278. for _, v := range res {
  279. v["qyxy_id"] = qyxyId[v["company_id"]]
  280. }
  281. }
  282. }
  283. } else {
  284. must := []string{fmt.Sprintf(`{"multi_match":{"query":"%s","type":"phrase","fields":["company_name"]}}`, in.Name)}
  285. switch in.Type {
  286. case "firstparty":
  287. must = append(must, fmt.Sprintf(`{"terms":{"company_label":["%s"]}}`, strings.Join(NetworkCom.GetEntTagSeat(2), `","`)))
  288. must = append(must, `{"terms":{"company_unit_type":[1,2]}}`)
  289. case "supplier":
  290. must = append(must, fmt.Sprintf(`{"terms":{"company_label":["%s"]}}`, strings.Join(NetworkCom.GetEntTagSeat(2), `","`)))
  291. must = append(must, `{"term":{"company_unit_type":3}}`)
  292. case "middleman_owner":
  293. must = append(must, `{"terms":{"company_unit_type":[1,2]}}`)
  294. case "agency":
  295. must = append(must, `{"term":{"company_unit_type":4}}`)
  296. }
  297. q := fmt.Sprintf(`{"query":{"bool":{"must":[%s]}},"size":%d,"_source":["id","company_id","company_name"]}`, strings.Join(must, ","), pageSize)
  298. logx.Info("人脉库-业主名称联想", q)
  299. datas := VarEs.Get("ent_info", "ent_info", q)
  300. if datas != nil {
  301. for _, v := range *datas {
  302. res = append(res, map[string]string{
  303. "company_name": ObjToString(v["company_name"]),
  304. "company_id": ObjToString(v["id"]),
  305. "qyxy_id": ObjToString(v["company_id"]),
  306. })
  307. }
  308. }
  309. }
  310. reply.Data = res
  311. return
  312. }
  313. // 人脉库-全部人脉项目
  314. func (n *network) AllProject(in *types.AllprojectReq) (reply *types.Reply) {
  315. reply = &types.Reply{}
  316. businessType := strings.Split(FindBusiness(in.EntId, in.MgoUserId), ",")
  317. var count int64
  318. var list []*networkTree
  319. firstparty := map[string]*projectInfo{}
  320. firstpartyChild := map[string][]*firstpartyNetwork{}
  321. if in.Id != "" {
  322. if in.Type == "firstparty" {
  323. fpn := n.FirstpartyNetwork(in.Name, []string{in.Id})
  324. if fpn[in.Id] != nil {
  325. firstparty = n.Introduce_Firstparty(fpn, businessType)
  326. nameIndex := map[string]int{}
  327. for _, v := range fpn[in.Id] {
  328. if _, ok := nameIndex[v.Name]; !ok {
  329. nameIndex[v.Name] = len(list)
  330. list = append(list, &networkTree{
  331. Name: v.Name,
  332. })
  333. }
  334. ntc := &networkTreeChild{
  335. CompanyName: v.CompanyName,
  336. CompanyId: v.CompanyId,
  337. Type: "firstparty",
  338. }
  339. if firstparty[v.CompanyId] != nil {
  340. ntc.Count = firstparty[v.CompanyId].ProjectCount
  341. }
  342. count++
  343. list[nameIndex[v.Name]].Count += ntc.Count
  344. list[nameIndex[v.Name]].Children = append(list[nameIndex[v.Name]].Children, ntc)
  345. }
  346. }
  347. }
  348. } else {
  349. args := []interface{}{in.PositionId, in.PositionId, in.PositionId}
  350. sqlAppend1 := ""
  351. if in.Name != "" {
  352. sqlAppend1 += ` and a.company_name like ?`
  353. args = append(args, "%"+in.Name+"%")
  354. }
  355. aio := &introduceOwnerProject{}
  356. key := fmt.Sprintf(NetworkManageAllProjectKey, in.PositionId)
  357. if rbt, rerr := redis.GetNewBytes("newother", key); rerr == nil && rbt != nil {
  358. json.Unmarshal(*rbt, &aio)
  359. } else {
  360. aio = n.AllIntroduceOwner(in.PositionId, sqlAppend1, "", args, true, businessType)
  361. redis.Put("newother", key, aio, NetworkCom.CacheTimeout())
  362. }
  363. firstparty = aio.Firstparty
  364. firstpartyChild = aio.FirstpartyNetwork
  365. list = []*networkTree{
  366. &networkTree{
  367. Name: "甲方",
  368. Children: []*networkTreeChild{},
  369. },
  370. &networkTree{
  371. Name: "供应商/渠道",
  372. Children: []*networkTreeChild{},
  373. },
  374. &networkTree{
  375. Name: "同甲异业渠道",
  376. Children: []*networkTreeChild{},
  377. },
  378. &networkTree{
  379. Name: "中间人",
  380. Children: []*networkTreeChild{},
  381. },
  382. &networkTree{
  383. Name: "招标代理",
  384. Children: []*networkTreeChild{},
  385. },
  386. }
  387. //
  388. for _, v := range aio.Networks {
  389. if v.Itype <= 0 || v.Itype > int64(len(list)) {
  390. return
  391. }
  392. ntc := &networkTreeChild{
  393. Id: v.Id,
  394. CompanyName: v.Company_name,
  395. CompanyId: v.Company_id,
  396. Type: n.TypeIntConvert(v.Itype),
  397. CreateTime: v.Create_time,
  398. }
  399. switch v.Itype {
  400. case 1:
  401. if firstparty[v.Company_id] != nil {
  402. ntc.Count = firstparty[v.Company_id].ProjectCount
  403. }
  404. case 2:
  405. if aio.Supplier[v.Company_id] != nil {
  406. ntc.Count = aio.Supplier[v.Company_id].ProjectCount
  407. ntc.IdNames = aio.Supplier[v.Company_id].IdNames
  408. }
  409. case 3:
  410. if aio.Adiffb[v.Company_id] != nil {
  411. ntc.Count = aio.Adiffb[v.Company_id].ProjectCount
  412. ntc.IdNames = aio.Adiffb[v.Company_id].IdNames
  413. }
  414. case 4:
  415. if v.Relate_buyer_id != "" {
  416. for _, rbi := range strings.Split(v.Relate_buyer_id, ",") {
  417. ntc.IdNames = append(ntc.IdNames, &idName{Id: rbi})
  418. if aio.Middleman[rbi] != nil {
  419. ntc.Count++
  420. }
  421. }
  422. }
  423. case 5:
  424. if aio.Agency[v.Company_id] != nil {
  425. ntc.Count = aio.Agency[v.Company_id].ProjectCount
  426. ntc.IdNames = aio.Agency[v.Company_id].IdNames
  427. }
  428. }
  429. count += ntc.Count
  430. list[v.Itype-1].Count += ntc.Count
  431. list[v.Itype-1].Children = append(list[v.Itype-1].Children, ntc)
  432. }
  433. }
  434. checkCode := []string{}
  435. parentConvList := &nodeTrees{}
  436. convList := &nodeTrees{}
  437. for _, v := range list {
  438. if len(v.Children) == 0 {
  439. continue
  440. }
  441. pm := &nodeTree{
  442. CODE: v.Name,
  443. NAME: v.Name,
  444. SZ_PID0: v.Name,
  445. SZ_LEVEL: 0,
  446. DATACOUNT: 0,
  447. }
  448. if len(v.Children) > 0 {
  449. pm.SZ_LEAF = 0
  450. } else {
  451. pm.SZ_LEAF = 1
  452. }
  453. sort.Sort(v)
  454. id, pType, pMyType, pMyId := "", "", "", ""
  455. for _, vv := range v.Children {
  456. myId := vv.CompanyId
  457. if vv.Type == "middleman" {
  458. myId = fmt.Sprint(vv.Id)
  459. }
  460. if pMyId != "" {
  461. pMyId += ","
  462. }
  463. pMyId += myId
  464. if pMyType != "" {
  465. pMyType += ","
  466. }
  467. pMyType += vv.Type
  468. myChildIds, myChildTypes := "", ""
  469. compareId := fmt.Sprint(vv.Id)
  470. thisLength := len(checkCode)
  471. if in.Id == "" && v.Name == "甲方" {
  472. cccNodes := []*nodeTree{}
  473. cccIndexMap := map[string]int{}
  474. for _, vvv := range firstpartyChild[vv.CompanyId] {
  475. if in.CheckCode != "" {
  476. compareId = vvv.CompanyId
  477. }
  478. cccIndex, cccOk := cccIndexMap[vvv.Name]
  479. var dataCount int64
  480. if firstparty[vvv.CompanyId] != nil {
  481. dataCount = firstparty[vvv.CompanyId].ProjectCount
  482. }
  483. if cccOk {
  484. cccNodes[cccIndex].ID += "," + vvv.CompanyId
  485. cccNodes[cccIndex].MYID += "," + vvv.CompanyId
  486. cccNodes[cccIndex].TYPE += ",firstparty"
  487. cccNodes[cccIndex].MYTYPE += ",firstparty"
  488. cccNodes[cccIndex].DATACOUNT += dataCount
  489. } else {
  490. nt := &nodeTree{
  491. NAME: vvv.Name,
  492. ID: vvv.CompanyId,
  493. SZ_PID0: v.Name,
  494. SZ_PID1: v.Name + ":" + myId,
  495. SZ_PID2: v.Name + ":" + myId + ":" + vvv.Name,
  496. CODE: v.Name + ":" + myId + ":" + vvv.Name,
  497. PCODE: v.Name + ":" + myId,
  498. SZ_LEVEL: 2,
  499. SZ_LEAF: 0,
  500. TYPE: "firstparty",
  501. MYID: vvv.CompanyId,
  502. MYTYPE: "firstparty",
  503. DATACOUNT: dataCount,
  504. }
  505. cccNodes = append(cccNodes, nt)
  506. cccIndexMap[vvv.Name] = len(cccNodes) - 1
  507. if compareId == in.ClickId {
  508. checkCode = append(checkCode, nt.CODE)
  509. }
  510. }
  511. if id != "" {
  512. id += ","
  513. }
  514. id += vvv.CompanyId
  515. if pType != "" {
  516. pType += ","
  517. }
  518. pType += vv.Type
  519. if myChildIds != "" {
  520. myChildIds += ","
  521. }
  522. myChildIds += vvv.CompanyId
  523. if myChildTypes != "" {
  524. myChildTypes += ","
  525. }
  526. myChildTypes += vv.Type
  527. nt := &nodeTree{
  528. NAME: vvv.CompanyName,
  529. ID: vvv.CompanyId,
  530. SZ_PID0: v.Name,
  531. SZ_PID1: v.Name + ":" + myId,
  532. SZ_PID2: cccNodes[cccIndex].SZ_PID2,
  533. SZ_PID3: v.Name + ":" + myId + ":" + vvv.Name + ":" + vvv.CompanyId,
  534. CODE: v.Name + ":" + myId + ":" + vvv.Name + ":" + vvv.CompanyId,
  535. PCODE: cccNodes[cccIndex].CODE,
  536. SZ_LEVEL: 3,
  537. SZ_LEAF: 1,
  538. TYPE: "firstparty",
  539. MYID: vvv.CompanyId,
  540. MYTYPE: "firstparty",
  541. DATACOUNT: dataCount,
  542. }
  543. if compareId == in.ClickId {
  544. checkCode = append(checkCode, nt.CODE)
  545. }
  546. *convList = append(*convList, nt)
  547. }
  548. for _, cccNode := range cccNodes {
  549. *convList = append(*convList, cccNode)
  550. }
  551. if in.CheckCode != "" {
  552. compareId = "-1"
  553. }
  554. } else {
  555. if id != "" {
  556. id += ","
  557. }
  558. id += myId
  559. if pType != "" {
  560. pType += ","
  561. }
  562. pType += vv.Type
  563. myChildIds = myId
  564. myChildTypes = vv.Type
  565. if in.CheckCode != "" {
  566. if len(vv.IdNames) == 0 {
  567. compareId = "-1"
  568. }
  569. for in_k, in_v := range vv.IdNames {
  570. if in_v.Id == in.ClickId {
  571. break
  572. } else if in_k == len(vv.IdNames)-1 {
  573. compareId = "-1"
  574. }
  575. }
  576. }
  577. }
  578. cm := &nodeTree{
  579. NAME: vv.CompanyName,
  580. ID: myChildIds,
  581. SZ_PID0: v.Name,
  582. SZ_PID1: v.Name + ":" + myId,
  583. CODE: v.Name + ":" + myId,
  584. PCODE: v.Name,
  585. DATACOUNT: vv.Count,
  586. SZ_LEVEL: 1,
  587. SZ_LEAF: If(myChildIds == "", 1, 0).(int),
  588. TYPE: myChildTypes,
  589. MYID: myId,
  590. MYTYPE: vv.Type,
  591. }
  592. //如果甲方的子节点被选中了,父节点跟着被选中,非甲方的要对比子节点可介绍业主
  593. if thisLength == len(checkCode) && ((in.ClickId != "" && in.ClickId == fmt.Sprint(vv.Id)) || (in.CheckCode != "" && compareId == fmt.Sprint(vv.Id))) {
  594. checkCode = append(checkCode, cm.CODE)
  595. }
  596. *convList = append(*convList, cm)
  597. }
  598. pm.ID = id
  599. pm.TYPE = pType
  600. pm.MYTYPE = pMyType
  601. pm.MYID = pMyId
  602. *parentConvList = append(*parentConvList, pm)
  603. }
  604. sort.Sort(convList)
  605. *parentConvList = append(*parentConvList, *convList...)
  606. newCheckCode := []string{}
  607. if in.CheckCode != "" {
  608. m := map[string]bool{}
  609. for _, v := range strings.Split(in.CheckCode, ",") {
  610. m[v] = true
  611. }
  612. for _, v := range checkCode {
  613. if m[v] {
  614. newCheckCode = append(newCheckCode, v)
  615. }
  616. }
  617. } else {
  618. newCheckCode = checkCode
  619. }
  620. if in.CheckCode == "" && in.ClickId == "" && len(newCheckCode) == 0 && len(*parentConvList) > 0 && (*parentConvList)[0].SZ_LEVEL == 0 {
  621. if (*parentConvList)[0].NAME == "甲方" {
  622. var level1 *nodeTree
  623. var level2 *nodeTree
  624. var level3 *nodeTree
  625. for _, v := range *parentConvList {
  626. if (*parentConvList)[0].NAME != v.SZ_PID0 {
  627. continue
  628. }
  629. if level1 == nil && v.SZ_LEVEL == 1 {
  630. level1 = v
  631. }
  632. if level2 == nil && v.SZ_LEVEL == 2 {
  633. level2 = v
  634. }
  635. if level3 == nil && v.SZ_LEVEL == 3 {
  636. level3 = v
  637. }
  638. }
  639. if level3 != nil && level2 != nil && level1 != nil && level3.SZ_PID2 == level2.CODE && level2.SZ_PID1 == level1.CODE {
  640. newCheckCode = append(newCheckCode, level3.CODE)
  641. } else if level2 != nil && level1 != nil && level2.SZ_PID1 == level1.CODE {
  642. newCheckCode = append(newCheckCode, level2.CODE)
  643. } else if level1 != nil {
  644. newCheckCode = append(newCheckCode, level1.CODE)
  645. }
  646. } else {
  647. for _, v := range *parentConvList {
  648. if (*parentConvList)[0].NAME != v.SZ_PID0 {
  649. continue
  650. }
  651. newCheckCode = append(newCheckCode, v.CODE)
  652. break
  653. }
  654. }
  655. }
  656. reply = &types.Reply{
  657. Data: map[string]interface{}{
  658. "count": count,
  659. "list": parentConvList,
  660. "checked": newCheckCode,
  661. },
  662. }
  663. return reply
  664. }
  665. // 人脉库-列表
  666. func (n *network) List(in *types.NetWorkListReq) *types.Reply {
  667. if in.Page_size <= 0 {
  668. in.Page_size = 10
  669. }
  670. if in.Current_page <= 0 {
  671. in.Current_page = 1
  672. }
  673. dbPaging := in.Buyercount_start == 0 && in.Buyercount_end == 0 && in.Monitor == 0 && in.Monitorcount_start == 0 && in.Monitorcount_end == 0 && in.Order_amount == 0
  674. sqlAppendArgs := []interface{}{}
  675. sqlAppend1 := ""
  676. if dbPaging && in.Type != "" {
  677. sqlAppend1 += ` and a.itype=?`
  678. sqlAppendArgs = append(sqlAppendArgs, n.TypeStrConvert(in.Type))
  679. }
  680. comSqlAppend := ""
  681. comSqlArgs := []interface{}{}
  682. if in.Starttime != "" {
  683. comSqlAppend += ` and a.create_time>=?`
  684. comSqlArgs = append(comSqlArgs, in.Starttime+" 00:00:00")
  685. }
  686. if in.Endtime != "" {
  687. comSqlAppend += ` and a.create_time<=?`
  688. if in.Starttime == in.Endtime {
  689. in.Endtime += " 23:59:59"
  690. } else {
  691. in.Endtime += " 00:00:00"
  692. }
  693. comSqlArgs = append(comSqlArgs, in.Endtime)
  694. }
  695. if in.Name != "" {
  696. comSqlAppend += ` and a.company_name like ?`
  697. comSqlArgs = append(comSqlArgs, "%"+in.Name+"%")
  698. }
  699. sqlAppend1 += comSqlAppend
  700. sqlAppendArgs = append(sqlAppendArgs, comSqlArgs...)
  701. var count int64
  702. start := (in.Current_page - 1) * in.Page_size
  703. args := []interface{}{in.PositionId, in.PositionId, in.PositionId}
  704. args = append(args, sqlAppendArgs...)
  705. sqlAppend2 := ""
  706. firstparty_count, supplier_count, adiffb_count, middleman_count, agency_count := 0, 0, 0, 0, 0
  707. if dbPaging {
  708. newArgs := []interface{}{in.PositionId}
  709. newArgs = append(newArgs, sqlAppendArgs...)
  710. count = CrmMysql.CountBySql(`select count(1) as count from crm.connection a where a.position_id=?`+sqlAppend1, newArgs...)
  711. sqlAppend2 = ` limit ?,?`
  712. args = append(args, start, in.Page_size)
  713. itemArgs := []interface{}{in.PositionId}
  714. itemArgs = append(itemArgs, comSqlArgs...)
  715. items := CrmMysql.SelectBySql(`SELECT itype,SUM(1) AS sum FROM crm.connection a WHERE a.position_id=?`+comSqlAppend+` GROUP BY a.itype`, itemArgs...)
  716. if items != nil {
  717. for _, v := range *items {
  718. switch Int64All(v["itype"]) {
  719. case 1:
  720. firstparty_count = IntAll(v["sum"])
  721. case 2:
  722. supplier_count = IntAll(v["sum"])
  723. case 3:
  724. adiffb_count = IntAll(v["sum"])
  725. case 4:
  726. middleman_count = IntAll(v["sum"])
  727. case 5:
  728. agency_count = IntAll(v["sum"])
  729. }
  730. }
  731. }
  732. }
  733. //
  734. list := []*map[string]interface{}{}
  735. isTjProject := true
  736. businessType := []string{}
  737. businessType = strings.Split(FindBusiness(in.EntId, in.MgoUserId), ",")
  738. if len(businessType) == 0 {
  739. isTjProject = false
  740. }
  741. redisKey := fmt.Sprintf(NetworkManageList, in.PositionId, GetMd5String(fmt.Sprintf("%+v", in)))
  742. aio := &introduceOwnerProject{}
  743. aioByte, aioErr := redis.GetNewBytes("newother", redisKey)
  744. timeOut := NetworkCom.CacheTimeout()
  745. if aioErr == nil && aioByte != nil {
  746. json.Unmarshal(*aioByte, &aio)
  747. } else {
  748. aio = n.AllIntroduceOwner(in.PositionId, sqlAppend1, sqlAppend2, args, isTjProject, businessType)
  749. redis.Put("newother", redisKey, aio, timeOut)
  750. }
  751. entMonitor := NetworkCom.EntMonitor(in.UserId)
  752. for _, v := range aio.Networks {
  753. itype := ""
  754. buyer_count, project_count, expect_amount, monitor_count := int64(0), int64(0), float64(0), int64(0)
  755. export_id := []string{}
  756. jump_type, jump_id := "", ""
  757. switch v.Itype {
  758. case 1:
  759. itype = "甲方"
  760. jump_type = "firstparty"
  761. for _, vv := range aio.FirstpartyNetwork[v.Company_id] {
  762. if jump_id != "" {
  763. jump_id += ","
  764. }
  765. jump_id += vv.CompanyId
  766. if entMonitor[vv.CompanyName] {
  767. monitor_count++
  768. }
  769. }
  770. if aio.Firstparty[v.Company_id] != nil {
  771. buyer_count = aio.Firstparty[v.Company_id].BuyerCount
  772. project_count = aio.Firstparty[v.Company_id].ProjectCount
  773. expect_amount = aio.Firstparty[v.Company_id].ProjectAmount
  774. export_id = aio.Firstparty[v.Company_id].ExportId
  775. }
  776. case 2:
  777. itype = "供应商/渠道"
  778. jump_type = "supplier"
  779. jump_id = v.Company_id
  780. if aio.Supplier[v.Company_id] != nil {
  781. buyer_count = aio.Supplier[v.Company_id].BuyerCount
  782. project_count = aio.Supplier[v.Company_id].ProjectCount
  783. expect_amount = aio.Supplier[v.Company_id].ProjectAmount
  784. export_id = aio.Supplier[v.Company_id].ExportId
  785. for _, vv := range aio.Supplier[v.Company_id].IdNames {
  786. if entMonitor[vv.Name] {
  787. monitor_count++
  788. }
  789. }
  790. }
  791. case 3:
  792. itype = "同甲异业渠道"
  793. jump_type = "adiffb"
  794. jump_id = v.Company_id
  795. if aio.Adiffb[v.Company_id] != nil {
  796. buyer_count = aio.Adiffb[v.Company_id].BuyerCount
  797. project_count = aio.Adiffb[v.Company_id].ProjectCount
  798. expect_amount = aio.Adiffb[v.Company_id].ProjectAmount
  799. export_id = aio.Adiffb[v.Company_id].ExportId
  800. for _, vv := range aio.Adiffb[v.Company_id].IdNames {
  801. if entMonitor[vv.Name] {
  802. monitor_count++
  803. }
  804. }
  805. }
  806. case 4:
  807. itype = "中间人"
  808. jump_type = "middleman"
  809. jump_id = fmt.Sprint(v.Id)
  810. buyer_count = v.Buyer_count
  811. if v.Relate_buyer_name != "" {
  812. for _, v := range strings.Split(v.Relate_buyer_name, ",") {
  813. if v == "" {
  814. continue
  815. }
  816. if entMonitor[v] {
  817. monitor_count++
  818. }
  819. }
  820. }
  821. if v.Relate_buyer_id != "" {
  822. for _, v := range strings.Split(v.Relate_buyer_id, ",") {
  823. if aio.Middleman[v] != nil {
  824. project_count++
  825. expect_amount += aio.Middleman[v].ProjectAmount
  826. export_id = append(export_id, aio.Middleman[v].ExportId...)
  827. }
  828. }
  829. }
  830. case 5:
  831. itype = "招标代理机构"
  832. jump_type = "agency"
  833. jump_id = v.Company_id
  834. if aio.Agency[v.Company_id] != nil {
  835. buyer_count = aio.Agency[v.Company_id].BuyerCount
  836. project_count = aio.Agency[v.Company_id].ProjectCount
  837. expect_amount = aio.Agency[v.Company_id].ProjectAmount
  838. export_id = aio.Agency[v.Company_id].ExportId
  839. for _, vv := range aio.Agency[v.Company_id].IdNames {
  840. if entMonitor[vv.Name] {
  841. monitor_count++
  842. }
  843. }
  844. }
  845. }
  846. if buyer_count < in.Buyercount_start {
  847. continue
  848. } else if in.Buyercount_end > 0 && buyer_count > in.Buyercount_end {
  849. continue
  850. } else if in.Monitor == 1 && monitor_count <= 0 {
  851. continue
  852. } else if in.Monitor == -1 && monitor_count > 0 {
  853. continue
  854. } else if monitor_count < in.Monitorcount_start {
  855. continue
  856. } else if in.Monitorcount_end > 0 && monitor_count > in.Monitorcount_end {
  857. continue
  858. }
  859. if !dbPaging {
  860. switch v.Itype {
  861. case 1:
  862. firstparty_count++
  863. case 2:
  864. supplier_count++
  865. case 3:
  866. adiffb_count++
  867. case 4:
  868. middleman_count++
  869. case 5:
  870. agency_count++
  871. }
  872. if in.Type != "" && n.TypeStrConvert(in.Type) != v.Itype {
  873. continue
  874. }
  875. }
  876. url := ""
  877. if v.Qyxy_id != "" && (jump_type == "supplier" || jump_type == "adiffb") {
  878. url = "/swordfish/page_big_pc/ent_portrait/" + encrypt.EncodeArticleId2ByCheck(v.Qyxy_id)
  879. } else if jump_type == "firstparty" && v.Company_name != "" {
  880. url = "/swordfish/page_big_pc/unit_portrayal/" + v.Company_name
  881. }
  882. list = append(list, &map[string]interface{}{
  883. "company_id": v.Company_id,
  884. "company_name": v.Company_name,
  885. "type": itype,
  886. "jump_type": jump_type,
  887. "jump_id": jump_id,
  888. "person": v.Person,
  889. "phone": v.Phone,
  890. "buyer_count": buyer_count,
  891. "monitor_count": monitor_count,
  892. "expect_amount": RetainDecimal(expect_amount/10000, 2),
  893. "project_count": project_count,
  894. "create_time": v.Create_time,
  895. "url": url,
  896. "id": v.Id,
  897. "export_id": strings.Join(export_id, ","),
  898. })
  899. }
  900. csList := &ComSortList{
  901. SortKeys: []*ComSortKey{
  902. &ComSortKey{
  903. Keys: []string{"expect_amount"},
  904. Order: 1,
  905. Type: "float",
  906. },
  907. },
  908. List: list,
  909. }
  910. if in.Order_amount == -1 {
  911. csList.SortKeys[0].Order = -1
  912. }
  913. if in.Order_amount != 0 {
  914. sort.Sort(csList)
  915. }
  916. finalList := []*map[string]interface{}{}
  917. if !dbPaging {
  918. count = int64(len(csList.List))
  919. if count > 0 && start < count {
  920. end := start + in.Page_size
  921. if end > count {
  922. end = count
  923. }
  924. finalList = csList.List[start:end]
  925. }
  926. } else {
  927. finalList = csList.List
  928. }
  929. pool := make(chan bool, C.ExportIdCachaPoolSize)
  930. wait := &sync.WaitGroup{}
  931. for _, v := range finalList {
  932. exportId := ObjToString((*v)["export_id"])
  933. md5Id := GetMd5String(exportId)
  934. (*v)["export_url"] = "/subscribepay/network/projectExport?version=1&export_id=" + md5Id
  935. delete(*v, "export_id")
  936. pool <- true
  937. wait.Add(1)
  938. go func(md5IdNew, exportIdNew string) {
  939. defer func() {
  940. <-pool
  941. wait.Done()
  942. }()
  943. key := fmt.Sprintf("network_export_%s", md5IdNew)
  944. if ok, _ := redis.Exists("newother", key); !ok {
  945. redis.Put("newother", key, exportIdNew, timeOut)
  946. }
  947. }(md5Id, exportId)
  948. }
  949. wait.Wait()
  950. total_page := int64(math.Ceil(float64(count) / float64(in.Page_size)))
  951. return &types.Reply{
  952. Data: map[string]interface{}{
  953. "count": count,
  954. "total_page": total_page,
  955. "firstparty_count": firstparty_count,
  956. "supplier_count": supplier_count,
  957. "adiffb_count": adiffb_count,
  958. "middleman_count": middleman_count,
  959. "agency_count": agency_count,
  960. "list": finalList,
  961. },
  962. }
  963. }
  964. func (n *network) FirstpartyNetwork(name string, values []string) map[string][]*firstpartyNetwork {
  965. result := map[string][]*firstpartyNetwork{}
  966. if len(values) == 0 {
  967. return result
  968. }
  969. wh, args := WhArgs(values)
  970. q := `select DISTINCT a.a_id,a.b_id as company_id,a.b_name as company_name,b.name from information.ent_map_code a
  971. inner join information.ent_code b on (a.a_id in (` + wh + `) and b.pcode in ('0100','0200') and a.code=b.code)`
  972. if name != "" {
  973. q += ` prewhere c.company_name like ?`
  974. args = append(args, "%"+name+"%")
  975. }
  976. q += ` order by b.name,a.a_id,a.b_name`
  977. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  978. if err != nil {
  979. logx.Error(err)
  980. return result
  981. }
  982. for rows.Next() {
  983. var (
  984. a_id string
  985. company_id string
  986. company_name string
  987. name string
  988. )
  989. if err := rows.Scan(&a_id, &company_id, &company_name, &name); err != nil {
  990. logx.Error(err)
  991. continue
  992. }
  993. if company_id == "" || company_name == "" {
  994. continue
  995. }
  996. result[a_id] = append(result[a_id], &firstpartyNetwork{
  997. CompanyId: company_id,
  998. CompanyName: company_name,
  999. Name: name,
  1000. })
  1001. }
  1002. return result
  1003. }
  1004. func (n *network) Introduce_Firstparty(fpn map[string][]*firstpartyNetwork, businessType []string) map[string]*projectInfo {
  1005. values := []string{}
  1006. vm := map[string]*projectInfo{}
  1007. for _, v := range fpn {
  1008. for _, vv := range v {
  1009. vm[vv.CompanyId] = &projectInfo{}
  1010. values = append(values, vv.CompanyId)
  1011. }
  1012. }
  1013. result := map[string]*projectInfo{}
  1014. if len(values) == 0 {
  1015. return result
  1016. }
  1017. wh, args := WhArgs(values)
  1018. q := `select buyer_id,count(project_id) AS project_count,sum(project_money) AS project_amount,groupUniqArray(project_id) from information.transaction_info_all prewhere buyer_id in (` + wh + `) and zbtime>? and project_bidstatus>1`
  1019. args = append(args, NetworkCom.ProjectYearLimit())
  1020. if len(businessType) > 0 {
  1021. newWh, newArgs := WhArgs(businessType)
  1022. q += ` and hasAny(topscopeclass,[` + newWh + `])`
  1023. args = append(args, newArgs...)
  1024. }
  1025. q += ` group by buyer_id`
  1026. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1027. if err != nil {
  1028. logx.Error(err)
  1029. return nil
  1030. }
  1031. for rows.Next() {
  1032. var (
  1033. buyer_id string
  1034. project_count uint64
  1035. project_amount decimal.Decimal
  1036. project_id []string
  1037. )
  1038. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  1039. logx.Error(err)
  1040. continue
  1041. }
  1042. if vm[buyer_id] == nil {
  1043. continue
  1044. }
  1045. vm[buyer_id].ProjectCount += int64(project_count)
  1046. pf, _ := project_amount.Float64()
  1047. vm[buyer_id].ProjectAmount += pf
  1048. vm[buyer_id].ExportId = project_id
  1049. }
  1050. rows.Close()
  1051. if err := rows.Err(); err != nil {
  1052. logx.Error(err)
  1053. }
  1054. for k, v := range fpn {
  1055. if result[k] == nil {
  1056. result[k] = &projectInfo{}
  1057. }
  1058. result[k].BuyerCount = int64(len(v))
  1059. for _, vv := range v {
  1060. if vm[vv.CompanyId] == nil {
  1061. continue
  1062. }
  1063. result[k].ProjectCount += vm[vv.CompanyId].ProjectCount
  1064. result[k].ProjectAmount += vm[vv.CompanyId].ProjectAmount
  1065. result[k].ExportId = append(result[k].ExportId, vm[vv.CompanyId].ExportId...)
  1066. }
  1067. }
  1068. return result
  1069. }
  1070. func (n *network) Introduce_Supplier(values []string, businessType []string) map[string]*projectInfo {
  1071. if len(values) == 0 {
  1072. return map[string]*projectInfo{}
  1073. }
  1074. wh, args := WhArgs(values)
  1075. repeat := map[string]bool{}
  1076. whRepeat := map[string]map[string]bool{}
  1077. buyers := []string{}
  1078. q := `select winner_id,buyer_id,buyer from information.transaction_info_all prewhere hasAny(winner_id,[` + wh + `]) and buyer_id<>''`
  1079. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1080. if err != nil {
  1081. logx.Error(err)
  1082. return map[string]*projectInfo{}
  1083. }
  1084. vbs := map[string][]*idName{}
  1085. for rows.Next() {
  1086. var (
  1087. winner_id []string
  1088. buyer_id string
  1089. buyer string
  1090. )
  1091. if err := rows.Scan(&winner_id, &buyer_id, &buyer); err != nil {
  1092. logx.Error(err)
  1093. continue
  1094. }
  1095. if buyer_id == "" || buyer == "" {
  1096. continue
  1097. }
  1098. if !repeat[buyer_id] {
  1099. repeat[buyer_id] = true
  1100. buyers = append(buyers, buyer_id)
  1101. }
  1102. for _, v := range winner_id {
  1103. if whRepeat[v] != nil && whRepeat[v][buyer_id] {
  1104. continue
  1105. }
  1106. if whRepeat[v] == nil {
  1107. whRepeat[v] = map[string]bool{}
  1108. }
  1109. whRepeat[v][buyer_id] = true
  1110. vbs[v] = append(vbs[v], &idName{
  1111. Id: buyer_id,
  1112. Name: buyer,
  1113. })
  1114. }
  1115. }
  1116. rows.Close()
  1117. if err := rows.Err(); err != nil {
  1118. logx.Error(err)
  1119. }
  1120. return n.MakeProjectInfo(nil, values, vbs, businessType)
  1121. }
  1122. func (n *network) Introduce_Agency(values []string, businessType []string) map[string]*projectInfo {
  1123. if len(values) == 0 {
  1124. return map[string]*projectInfo{}
  1125. }
  1126. wh, args := WhArgs(values)
  1127. q := `select DISTINCT agency_id,buyer_id,buyer from information.transaction_info_all prewhere agency_id in (` + wh + `) and buyer_id<>''`
  1128. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1129. if err != nil {
  1130. logx.Error(err)
  1131. return map[string]*projectInfo{}
  1132. }
  1133. vbs := map[string][]*idName{}
  1134. buyers := []string{}
  1135. repeat := map[string]bool{}
  1136. for rows.Next() {
  1137. var (
  1138. agency_id string
  1139. buyer_id string
  1140. buyer string
  1141. )
  1142. if err := rows.Scan(&agency_id, &buyer_id, &buyer); err != nil {
  1143. logx.Error(err)
  1144. continue
  1145. }
  1146. if buyer_id == "" || buyer == "" {
  1147. continue
  1148. }
  1149. vbs[agency_id] = append(vbs[agency_id], &idName{
  1150. Id: buyer_id,
  1151. Name: buyer,
  1152. })
  1153. if !repeat[buyer_id] {
  1154. repeat[buyer_id] = true
  1155. buyers = append(buyers, buyer_id)
  1156. }
  1157. }
  1158. rows.Close()
  1159. if err := rows.Err(); err != nil {
  1160. logx.Error(err)
  1161. }
  1162. return n.MakeProjectInfo(values, nil, vbs, businessType)
  1163. }
  1164. func (n *network) Introduce_Middleman_Buyer(values []string, businessType []string) map[string]*projectInfo {
  1165. return n.BuyerProjectInfo(values, businessType)
  1166. }
  1167. func (n *network) Introduce_Middleman_Project(values []string, businessType []string) map[string]*projectInfo {
  1168. result := map[string]*projectInfo{}
  1169. if len(values) == 0 {
  1170. return result
  1171. }
  1172. wh, args := WhArgs(values)
  1173. q := `select DISTINCT project_id,project_money from information.transaction_info_all prewhere project_id in (` + wh + `)`
  1174. if len(businessType) > 0 {
  1175. newWh, newArgs := WhArgs(businessType)
  1176. q += ` and hasAny(topscopeclass,[` + newWh + `])`
  1177. args = append(args, newArgs...)
  1178. }
  1179. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1180. if err != nil {
  1181. logx.Error(err)
  1182. return nil
  1183. }
  1184. for rows.Next() {
  1185. var (
  1186. project_id string
  1187. project_money decimal.Decimal
  1188. )
  1189. if err := rows.Scan(&project_id, &project_money); err != nil {
  1190. logx.Error(err)
  1191. continue
  1192. }
  1193. pf, _ := project_money.Float64()
  1194. result[project_id] = &projectInfo{
  1195. ProjectAmount: pf,
  1196. }
  1197. }
  1198. rows.Close()
  1199. if err := rows.Err(); err != nil {
  1200. logx.Error(err)
  1201. }
  1202. return result
  1203. }
  1204. func (n *network) TypeStrConvert(itype string) int64 {
  1205. //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  1206. switch itype {
  1207. case "firstparty":
  1208. return 1
  1209. case "supplier":
  1210. return 2
  1211. case "adiffb":
  1212. return 3
  1213. case "middleman":
  1214. return 4
  1215. case "agency":
  1216. return 5
  1217. }
  1218. return 0
  1219. }
  1220. func (n *network) TypeIntConvert(itype int64) string {
  1221. //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  1222. switch itype {
  1223. case 1:
  1224. return "firstparty"
  1225. case 2:
  1226. return "supplier"
  1227. case 3:
  1228. return "adiffb"
  1229. case 4:
  1230. return "middleman"
  1231. case 5:
  1232. return "agency"
  1233. }
  1234. return ""
  1235. }
  1236. func (n *network) GetQyxyId(ids []string) map[string]string {
  1237. m := map[string]string{}
  1238. if len(ids) == 0 {
  1239. return m
  1240. }
  1241. wh, args := WhArgs(ids)
  1242. q := `select id,company_id from information.ent_info prewhere id in (` + wh + `)`
  1243. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1244. if err != nil {
  1245. logx.Error(err)
  1246. } else {
  1247. for rows.Next() {
  1248. var (
  1249. id string
  1250. company_id string
  1251. )
  1252. if err := rows.Scan(&id, &company_id); err != nil {
  1253. logx.Error(err)
  1254. continue
  1255. }
  1256. m[id] = company_id
  1257. }
  1258. rows.Close()
  1259. if err := rows.Err(); err != nil {
  1260. logx.Error(err)
  1261. }
  1262. }
  1263. return m
  1264. }
  1265. func (n *network) BuyerProjectInfo(ids []string, businessType []string) map[string]*projectInfo {
  1266. vm := map[string]*projectInfo{}
  1267. m := map[string]bool{}
  1268. newIds := []string{}
  1269. for _, v := range ids {
  1270. if m[v] {
  1271. continue
  1272. }
  1273. m[v] = true
  1274. newIds = append(newIds, v)
  1275. if len(newIds) == C.SelectBatchSize {
  1276. n.BuyerProjectInfoBatch(newIds, businessType, vm)
  1277. newIds = []string{}
  1278. }
  1279. }
  1280. if len(newIds) > 0 {
  1281. n.BuyerProjectInfoBatch(newIds, businessType, vm)
  1282. }
  1283. return vm
  1284. }
  1285. func (n *network) BuyerProjectInfoBatch(ids []string, businessType []string, vm map[string]*projectInfo) {
  1286. if len(ids) == 0 {
  1287. return
  1288. }
  1289. wh, args := WhArgs(ids)
  1290. q := `select buyer_id,uniqCombined(project_id) AS project_count,sum(project_money) AS project_amount,groupUniqArray(project_id) from information.transaction_info_all prewhere buyer_id in (` + wh + `) and zbtime>? and project_bidstatus>1`
  1291. args = append(args, NetworkCom.ProjectYearLimit())
  1292. if len(businessType) > 0 {
  1293. newWh, newArgs := WhArgs(businessType)
  1294. q += ` and hasAny(topscopeclass,[` + newWh + `])`
  1295. args = append(args, newArgs...)
  1296. }
  1297. q += ` group by buyer_id`
  1298. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1299. if err != nil {
  1300. logx.Error(err)
  1301. return
  1302. }
  1303. for rows.Next() {
  1304. var (
  1305. buyer_id string
  1306. project_count uint64
  1307. project_amount decimal.Decimal
  1308. project_id []string
  1309. )
  1310. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  1311. logx.Error(err)
  1312. continue
  1313. }
  1314. pf, _ := project_amount.Float64()
  1315. if vm[buyer_id] == nil {
  1316. vm[buyer_id] = &projectInfo{
  1317. ProjectCount: int64(project_count),
  1318. ExportId: project_id,
  1319. ProjectAmount: pf,
  1320. }
  1321. } else {
  1322. vm[buyer_id].ProjectAmount += pf
  1323. vm[buyer_id].ProjectCount += int64(project_count)
  1324. vm[buyer_id].ExportId = append(vm[buyer_id].ExportId, project_id...)
  1325. }
  1326. }
  1327. rows.Close()
  1328. if err := rows.Err(); err != nil {
  1329. logx.Error(err)
  1330. }
  1331. return
  1332. }
  1333. func (n *network) TjProject(agencyIds, winnerIds []string, vbs map[string][]*idName, businessType []string) map[string]*projectInfo {
  1334. vm := map[string]*projectInfo{}
  1335. if len(agencyIds) == 0 && len(winnerIds) == 0 {
  1336. return vm
  1337. }
  1338. yearLimit := NetworkCom.ProjectYearLimit()
  1339. allArgs := []interface{}{}
  1340. q := `select buyer_id,uniqCombined(project_id) AS project_count,sum(project_money) AS project_amount,groupUniqArray(project_id) from information.transaction_info_all prewhere buyer_id in (select DISTINCT buyer_id from information.transaction_info_all prewhere `
  1341. if len(agencyIds) > 0 {
  1342. wh, args := WhArgs(agencyIds)
  1343. allArgs = append(allArgs, args...)
  1344. allArgs = append(allArgs, yearLimit)
  1345. q += `agency_id in (` + wh + `) and buyer_id<>'' and zbtime>?`
  1346. } else if len(winnerIds) > 0 {
  1347. wh, args := WhArgs(winnerIds)
  1348. allArgs = append(allArgs, args...)
  1349. allArgs = append(allArgs, yearLimit)
  1350. q += `hasAny(winner_id,[` + wh + `]) and buyer_id<>'' and zbtime>?`
  1351. }
  1352. q += `) and zbtime>? and project_bidstatus>1`
  1353. allArgs = append(allArgs, yearLimit)
  1354. if len(businessType) > 0 {
  1355. newWh, newArgs := WhArgs(businessType)
  1356. q += ` and hasAny(topscopeclass,[` + newWh + `])`
  1357. allArgs = append(allArgs, newArgs...)
  1358. }
  1359. q += ` group by buyer_id`
  1360. logx.Info("全部人脉项目sql", q, allArgs)
  1361. rows, err := ClickhouseConn.Query(context.Background(), q, allArgs...)
  1362. if err != nil {
  1363. logx.Error(err)
  1364. return vm
  1365. }
  1366. for rows.Next() {
  1367. var (
  1368. buyer_id string
  1369. project_count uint64
  1370. project_amount decimal.Decimal
  1371. project_id []string
  1372. )
  1373. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  1374. logx.Error(err)
  1375. continue
  1376. }
  1377. pf, _ := project_amount.Float64()
  1378. if vm[buyer_id] == nil {
  1379. vm[buyer_id] = &projectInfo{
  1380. ProjectCount: int64(project_count),
  1381. ExportId: project_id,
  1382. ProjectAmount: pf,
  1383. }
  1384. } else {
  1385. vm[buyer_id].ProjectAmount += pf
  1386. vm[buyer_id].ProjectCount += int64(project_count)
  1387. vm[buyer_id].ExportId = append(vm[buyer_id].ExportId, project_id...)
  1388. }
  1389. }
  1390. rows.Close()
  1391. if err := rows.Err(); err != nil {
  1392. logx.Error(err)
  1393. }
  1394. return vm
  1395. }
  1396. func (n *network) MakeProjectInfo(agencyIds, winnerIds []string, vbs map[string][]*idName, businessType []string) map[string]*projectInfo {
  1397. pis := n.TjProject(agencyIds, winnerIds, vbs, businessType)
  1398. vm := map[string]*projectInfo{}
  1399. for k, v := range vbs {
  1400. pi := &projectInfo{
  1401. BuyerCount: int64(len(v)),
  1402. IdNames: v,
  1403. }
  1404. re := map[string]bool{}
  1405. for _, vv := range v {
  1406. if pis[vv.Id] != nil {
  1407. pi.ProjectCount += pis[vv.Id].ProjectCount
  1408. pi.ProjectAmount += pis[vv.Id].ProjectAmount
  1409. for _, vvv := range pis[vv.Id].ExportId {
  1410. if re[vvv] {
  1411. continue
  1412. }
  1413. pi.ExportId = append(pi.ExportId, vvv)
  1414. re[vvv] = true
  1415. }
  1416. }
  1417. }
  1418. vm[k] = pi
  1419. }
  1420. return vm
  1421. }
  1422. func (n *network) AllIntroduceOwner(positionId int64, sqlAppend1, sqlAppend2 string, args []interface{}, isTjProject bool, businessType []string) *introduceOwnerProject {
  1423. logx.Info(positionId, " AllIntroduceOwner start 。。。")
  1424. q := `select a.id,a.company_id,a.company_name,a.qyxy_id,a.itype,a.contact_person as person,a.contact_phone as phone,count(distinct if(b.itype=1,b.relate_id,null)) as buyer_count,count(distinct if(b.itype=2,b.relate_id,null)) as project_count,GROUP_CONCAT(IF(b.itype=1,b.relate_id,NULL)) AS relate_buyer_id,GROUP_CONCAT(IF(b.itype=1,b.relate_name,NULL)) AS relate_buyer_name,GROUP_CONCAT(IF(b.itype=2,b.relate_id,NULL)) AS relate_project_id,a.create_time from crm.connection a
  1425. left join crm.connection_introduce b on (a.position_id=? and b.position_id=? and a.id=b.connection_id) where a.position_id=?` + sqlAppend1 + ` GROUP BY a.id order by a.create_time desc` + sqlAppend2
  1426. listTemp := CrmMysql.SelectBySql(q, args...)
  1427. firstparty_array, supplier_array, adiffb_array, agency_array, middleman_buyer_array, middleman_project_array := []string{}, []string{}, []string{}, []string{}, []string{}, []string{}
  1428. myNetworks := []*myNetwork{}
  1429. for _, v := range *listTemp {
  1430. myNetwork := &myNetwork{
  1431. Id: Int64All(v["id"]),
  1432. Company_id: ObjToString(v["company_id"]),
  1433. Company_name: ObjToString(v["company_name"]),
  1434. Qyxy_id: ObjToString(v["qyxy_id"]),
  1435. Itype: Int64All(v["itype"]),
  1436. Person: ObjToString(v["person"]),
  1437. Phone: ObjToString(v["phone"]),
  1438. Buyer_count: Int64All(v["buyer_count"]),
  1439. Project_count: Int64All(v["project_count"]),
  1440. Relate_buyer_id: ObjToString(v["relate_buyer_id"]),
  1441. Relate_buyer_name: ObjToString(v["relate_buyer_name"]),
  1442. Relate_project_id: ObjToString(v["relate_project_id"]),
  1443. Create_time: ObjToString(v["create_time"]),
  1444. }
  1445. switch myNetwork.Itype {
  1446. case 1:
  1447. firstparty_array = append(firstparty_array, myNetwork.Company_id)
  1448. case 2:
  1449. supplier_array = append(supplier_array, myNetwork.Company_id)
  1450. case 3:
  1451. adiffb_array = append(adiffb_array, myNetwork.Company_id)
  1452. case 4:
  1453. if myNetwork.Relate_buyer_id != "" {
  1454. middleman_buyer_array = append(middleman_buyer_array, strings.Split(myNetwork.Relate_buyer_id, ",")...)
  1455. }
  1456. if myNetwork.Relate_project_id != "" {
  1457. middleman_project_array = append(middleman_project_array, strings.Split(myNetwork.Relate_project_id, ",")...)
  1458. }
  1459. case 5:
  1460. agency_array = append(agency_array, myNetwork.Company_id)
  1461. }
  1462. myNetworks = append(myNetworks, myNetwork)
  1463. }
  1464. iop := &introduceOwnerProject{
  1465. Networks: myNetworks,
  1466. FirstpartyNetwork: map[string][]*firstpartyNetwork{},
  1467. Firstparty: map[string]*projectInfo{},
  1468. Supplier: map[string]*projectInfo{},
  1469. Adiffb: map[string]*projectInfo{},
  1470. Agency: map[string]*projectInfo{},
  1471. Middleman: map[string]*projectInfo{},
  1472. }
  1473. if isTjProject {
  1474. iop.FirstpartyNetwork = n.FirstpartyNetwork("", firstparty_array)
  1475. logx.Info(positionId, " FirstpartyNetwork over 。。。")
  1476. iop.Firstparty = n.Introduce_Firstparty(iop.FirstpartyNetwork, businessType)
  1477. logx.Info(positionId, " Introduce_Firstparty over 。。。")
  1478. iop.Supplier = n.Introduce_Supplier(supplier_array, businessType)
  1479. logx.Info(positionId, " Introduce_Supplier over 。。。")
  1480. iop.Adiffb = n.Introduce_Supplier(adiffb_array, businessType)
  1481. logx.Info(positionId, " Introduce_Supplier over 。。。")
  1482. iop.Agency = n.Introduce_Agency(agency_array, businessType)
  1483. logx.Info(positionId, " Introduce_Agency over 。。。")
  1484. iop.Middleman = n.Introduce_Middleman_Buyer(middleman_buyer_array, businessType)
  1485. logx.Info(positionId, " Introduce_Middleman_Buyer over 。。。")
  1486. }
  1487. return iop
  1488. }
  1489. func (n *network) DeleteCache(positionId int64) {
  1490. redis.Del("newother", fmt.Sprintf(NetworkManageAllProjectKey, positionId))
  1491. redis.DelByCodePattern("newother", fmt.Sprintf(NetworkManageList, positionId, "*"))
  1492. redis.DelByCodePattern("newother", fmt.Sprintf(NetworkManageCoopHistory, positionId, "*"))
  1493. redis.DelByCodePattern("newother", fmt.Sprintf(NetworkManageOwnerlList, positionId, "*"))
  1494. redis.DelByCodePattern("newother", fmt.Sprintf(NetworkManageProjectList, positionId, "*"))
  1495. }