network.go 47 KB

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