network.go 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "encoding/json"
  6. "fmt"
  7. "log"
  8. "math"
  9. "sort"
  10. "strings"
  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 where buyer_id in (select buyer_id from information.transaction_info_all where 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(sqlAppend1, "", args, true, businessType)
  361. redis.Put("newother", key, aio, C.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. if aioErr == nil && aioByte != nil {
  745. log.Println("-----3333---")
  746. json.Unmarshal(*aioByte, &aio)
  747. } else {
  748. log.Println("--------")
  749. aio = n.AllIntroduceOwner(sqlAppend1, sqlAppend2, args, isTjProject, businessType)
  750. redis.Put("newother", redisKey, aio, C.CacheTimeOut)
  751. }
  752. entMonitor := NetworkCom.EntMonitor(in.UserId)
  753. for _, v := range aio.Networks {
  754. itype := ""
  755. buyer_count, project_count, expect_amount, monitor_count := int64(0), int64(0), float64(0), int64(0)
  756. export_id := []string{}
  757. jump_type, jump_id := "", ""
  758. switch v.Itype {
  759. case 1:
  760. itype = "甲方"
  761. jump_type = "firstparty"
  762. for _, vv := range aio.FirstpartyNetwork[v.Company_id] {
  763. if jump_id != "" {
  764. jump_id += ","
  765. }
  766. jump_id += vv.CompanyId
  767. if entMonitor[vv.CompanyName] {
  768. monitor_count++
  769. }
  770. }
  771. if aio.Firstparty[v.Company_id] != nil {
  772. buyer_count = aio.Firstparty[v.Company_id].BuyerCount
  773. project_count = aio.Firstparty[v.Company_id].ProjectCount
  774. expect_amount = aio.Firstparty[v.Company_id].ProjectAmount
  775. export_id = aio.Firstparty[v.Company_id].ExportId
  776. }
  777. case 2:
  778. itype = "供应商/渠道"
  779. jump_type = "supplier"
  780. jump_id = v.Company_id
  781. if aio.Supplier[v.Company_id] != nil {
  782. buyer_count = aio.Supplier[v.Company_id].BuyerCount
  783. project_count = aio.Supplier[v.Company_id].ProjectCount
  784. expect_amount = aio.Supplier[v.Company_id].ProjectAmount
  785. export_id = aio.Supplier[v.Company_id].ExportId
  786. for _, vv := range aio.Supplier[v.Company_id].IdNames {
  787. if entMonitor[vv.Name] {
  788. monitor_count++
  789. }
  790. }
  791. }
  792. case 3:
  793. itype = "同甲异业渠道"
  794. jump_type = "adiffb"
  795. jump_id = v.Company_id
  796. if aio.Adiffb[v.Company_id] != nil {
  797. buyer_count = aio.Adiffb[v.Company_id].BuyerCount
  798. project_count = aio.Adiffb[v.Company_id].ProjectCount
  799. expect_amount = aio.Adiffb[v.Company_id].ProjectAmount
  800. export_id = aio.Adiffb[v.Company_id].ExportId
  801. for _, vv := range aio.Adiffb[v.Company_id].IdNames {
  802. if entMonitor[vv.Name] {
  803. monitor_count++
  804. }
  805. }
  806. }
  807. case 4:
  808. itype = "中间人"
  809. jump_type = "middleman"
  810. jump_id = fmt.Sprint(v.Id)
  811. buyer_count = v.Buyer_count
  812. if v.Relate_buyer_name != "" {
  813. for _, v := range strings.Split(v.Relate_buyer_name, ",") {
  814. if v == "" {
  815. continue
  816. }
  817. if entMonitor[v] {
  818. monitor_count++
  819. }
  820. }
  821. }
  822. if v.Relate_buyer_id != "" {
  823. for _, v := range strings.Split(v.Relate_buyer_id, ",") {
  824. if aio.Middleman[v] != nil {
  825. project_count++
  826. expect_amount += aio.Middleman[v].ProjectAmount
  827. export_id = append(export_id, aio.Middleman[v].ExportId...)
  828. }
  829. }
  830. }
  831. case 5:
  832. itype = "招标代理机构"
  833. jump_type = "agency"
  834. jump_id = v.Company_id
  835. if aio.Agency[v.Company_id] != nil {
  836. buyer_count = aio.Agency[v.Company_id].BuyerCount
  837. project_count = aio.Agency[v.Company_id].ProjectCount
  838. expect_amount = aio.Agency[v.Company_id].ProjectAmount
  839. export_id = aio.Agency[v.Company_id].ExportId
  840. for _, vv := range aio.Agency[v.Company_id].IdNames {
  841. if entMonitor[vv.Name] {
  842. monitor_count++
  843. }
  844. }
  845. }
  846. }
  847. if buyer_count < in.Buyercount_start {
  848. continue
  849. } else if in.Buyercount_end > 0 && buyer_count > in.Buyercount_end {
  850. continue
  851. } else if in.Monitor == 1 && monitor_count <= 0 {
  852. continue
  853. } else if in.Monitor == -1 && monitor_count > 0 {
  854. continue
  855. } else if monitor_count < in.Monitorcount_start {
  856. continue
  857. } else if in.Monitorcount_end > 0 && monitor_count > in.Monitorcount_end {
  858. continue
  859. }
  860. if !dbPaging {
  861. switch v.Itype {
  862. case 1:
  863. firstparty_count++
  864. case 2:
  865. supplier_count++
  866. case 3:
  867. adiffb_count++
  868. case 4:
  869. middleman_count++
  870. case 5:
  871. agency_count++
  872. }
  873. if in.Type != "" && n.TypeStrConvert(in.Type) != v.Itype {
  874. continue
  875. }
  876. }
  877. export_url := ""
  878. if len(export_id) > 0 {
  879. exportIdRepeat := map[string]bool{}
  880. exportId := ""
  881. for _, v := range export_id {
  882. if exportIdRepeat[v] {
  883. continue
  884. }
  885. exportIdRepeat[v] = true
  886. if exportId != "" {
  887. exportId += ","
  888. }
  889. exportId += v
  890. }
  891. md5Id := GetMd5String(exportId)
  892. redis.Put("newother", fmt.Sprintf("network_export_%s", md5Id), exportId, 259200)
  893. export_url = "/subscribepay/network/projectExport?version=1&export_id=" + md5Id
  894. }
  895. url := ""
  896. if v.Qyxy_id != "" && (jump_type == "supplier" || jump_type == "adiffb") {
  897. url = "/swordfish/page_big_pc/ent_portrait/" + encrypt.EncodeArticleId2ByCheck(v.Qyxy_id)
  898. } else if jump_type == "firstparty" && v.Company_name != "" {
  899. url = "/swordfish/page_big_pc/unit_portrayal/" + v.Company_name
  900. }
  901. list = append(list, &map[string]interface{}{
  902. "company_id": v.Company_id,
  903. "company_name": v.Company_name,
  904. "type": itype,
  905. "jump_type": jump_type,
  906. "jump_id": jump_id,
  907. "person": v.Person,
  908. "phone": v.Phone,
  909. "buyer_count": buyer_count,
  910. "monitor_count": monitor_count,
  911. "expect_amount": RetainDecimal(expect_amount/10000, 2),
  912. "project_count": project_count,
  913. "create_time": v.Create_time,
  914. "export_url": export_url,
  915. "url": url,
  916. "id": v.Id,
  917. })
  918. }
  919. csList := &ComSortList{
  920. SortKeys: []*ComSortKey{
  921. &ComSortKey{
  922. Keys: []string{"expect_amount"},
  923. Order: 1,
  924. Type: "float",
  925. },
  926. },
  927. List: list,
  928. }
  929. if in.Order_amount == -1 {
  930. csList.SortKeys[0].Order = -1
  931. }
  932. if in.Order_amount != 0 {
  933. sort.Sort(csList)
  934. }
  935. finalList := []*map[string]interface{}{}
  936. if !dbPaging {
  937. count = int64(len(csList.List))
  938. if count > 0 && start < count {
  939. end := start + in.Page_size
  940. if end > count {
  941. end = count
  942. }
  943. finalList = csList.List[start:end]
  944. }
  945. } else {
  946. finalList = csList.List
  947. }
  948. total_page := int64(math.Ceil(float64(count) / float64(in.Page_size)))
  949. return &types.Reply{
  950. Data: map[string]interface{}{
  951. "count": count,
  952. "total_page": total_page,
  953. "firstparty_count": firstparty_count,
  954. "supplier_count": supplier_count,
  955. "adiffb_count": adiffb_count,
  956. "middleman_count": middleman_count,
  957. "agency_count": agency_count,
  958. "list": finalList,
  959. },
  960. }
  961. }
  962. func (n *network) FirstpartyNetwork(name string, values []string) map[string][]*firstpartyNetwork {
  963. result := map[string][]*firstpartyNetwork{}
  964. if len(values) == 0 {
  965. return result
  966. }
  967. wh, args := WhArgs(values)
  968. 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
  969. inner join information.ent_code b on (a.a_id in (` + wh + `) and b.pcode in ('0100','0200') and a.code=b.code)`
  970. if name != "" {
  971. q += ` where c.company_name like ?`
  972. args = append(args, "%"+name+"%")
  973. }
  974. q += ` order by b.name,a.a_id,a.b_name`
  975. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  976. if err != nil {
  977. logx.Error(err)
  978. return result
  979. }
  980. for rows.Next() {
  981. var (
  982. a_id string
  983. company_id string
  984. company_name string
  985. name string
  986. )
  987. if err := rows.Scan(&a_id, &company_id, &company_name, &name); err != nil {
  988. logx.Error(err)
  989. continue
  990. }
  991. if company_id == "" || company_name == "" {
  992. continue
  993. }
  994. result[a_id] = append(result[a_id], &firstpartyNetwork{
  995. CompanyId: company_id,
  996. CompanyName: company_name,
  997. Name: name,
  998. })
  999. }
  1000. return result
  1001. }
  1002. func (n *network) Introduce_Firstparty(fpn map[string][]*firstpartyNetwork, businessType []string) map[string]*projectInfo {
  1003. values := []string{}
  1004. vm := map[string]*projectInfo{}
  1005. for _, v := range fpn {
  1006. for _, vv := range v {
  1007. vm[vv.CompanyId] = &projectInfo{}
  1008. values = append(values, vv.CompanyId)
  1009. }
  1010. }
  1011. result := map[string]*projectInfo{}
  1012. if len(values) == 0 {
  1013. return result
  1014. }
  1015. wh, args := WhArgs(values)
  1016. q := `select buyer_id,count(project_id) AS project_count,sum(project_money) AS project_amount,groupUniqArray(project_id) from information.transaction_info_all where buyer_id in (` + wh + `) and zbtime>? and project_bidstatus>1`
  1017. args = append(args, NetworkCom.ProjectYearLimit())
  1018. if len(businessType) > 0 {
  1019. newWh, newArgs := WhArgs(businessType)
  1020. q += ` and hasAny(topscopeclass,[` + newWh + `])`
  1021. args = append(args, newArgs...)
  1022. }
  1023. q += ` group by buyer_id`
  1024. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1025. if err != nil {
  1026. logx.Error(err)
  1027. return nil
  1028. }
  1029. for rows.Next() {
  1030. var (
  1031. buyer_id string
  1032. project_count uint64
  1033. project_amount decimal.Decimal
  1034. project_id []string
  1035. )
  1036. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  1037. logx.Error(err)
  1038. continue
  1039. }
  1040. if vm[buyer_id] == nil {
  1041. continue
  1042. }
  1043. vm[buyer_id].ProjectCount += int64(project_count)
  1044. pf, _ := project_amount.Float64()
  1045. vm[buyer_id].ProjectAmount += pf
  1046. vm[buyer_id].ExportId = project_id
  1047. }
  1048. rows.Close()
  1049. if err := rows.Err(); err != nil {
  1050. logx.Error(err)
  1051. }
  1052. for k, v := range fpn {
  1053. if result[k] == nil {
  1054. result[k] = &projectInfo{}
  1055. }
  1056. result[k].BuyerCount = int64(len(v))
  1057. for _, vv := range v {
  1058. if vm[vv.CompanyId] == nil {
  1059. continue
  1060. }
  1061. result[k].ProjectCount += vm[vv.CompanyId].ProjectCount
  1062. result[k].ProjectAmount += vm[vv.CompanyId].ProjectAmount
  1063. result[k].ExportId = append(result[k].ExportId, vm[vv.CompanyId].ExportId...)
  1064. }
  1065. }
  1066. return result
  1067. }
  1068. func (n *network) Introduce_Supplier(values []string, businessType []string) map[string]*projectInfo {
  1069. if len(values) == 0 {
  1070. return map[string]*projectInfo{}
  1071. }
  1072. wh, args := WhArgs(values)
  1073. repeat := map[string]bool{}
  1074. whRepeat := map[string]map[string]bool{}
  1075. buyers := []string{}
  1076. q := `select winner_id,buyer_id,buyer from information.transaction_info_all where hasAny(winner_id,[` + wh + `]) and buyer_id<>''`
  1077. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1078. if err != nil {
  1079. logx.Error(err)
  1080. return map[string]*projectInfo{}
  1081. }
  1082. vbs := map[string][]*idName{}
  1083. for rows.Next() {
  1084. var (
  1085. winner_id []string
  1086. buyer_id string
  1087. buyer string
  1088. )
  1089. if err := rows.Scan(&winner_id, &buyer_id, &buyer); err != nil {
  1090. logx.Error(err)
  1091. continue
  1092. }
  1093. if buyer_id == "" || buyer == "" {
  1094. continue
  1095. }
  1096. if !repeat[buyer_id] {
  1097. repeat[buyer_id] = true
  1098. buyers = append(buyers, buyer_id)
  1099. }
  1100. for _, v := range winner_id {
  1101. if whRepeat[v] != nil && whRepeat[v][buyer_id] {
  1102. continue
  1103. }
  1104. if whRepeat[v] == nil {
  1105. whRepeat[v] = map[string]bool{}
  1106. }
  1107. whRepeat[v][buyer_id] = true
  1108. vbs[v] = append(vbs[v], &idName{
  1109. Id: buyer_id,
  1110. Name: buyer,
  1111. })
  1112. }
  1113. }
  1114. rows.Close()
  1115. if err := rows.Err(); err != nil {
  1116. logx.Error(err)
  1117. }
  1118. return n.MakeProjectInfo(nil, values, vbs, businessType)
  1119. }
  1120. func (n *network) Introduce_Agency(values []string, businessType []string) map[string]*projectInfo {
  1121. if len(values) == 0 {
  1122. return map[string]*projectInfo{}
  1123. }
  1124. wh, args := WhArgs(values)
  1125. q := `select DISTINCT agency_id,buyer_id,buyer from information.transaction_info_all where agency_id in (` + wh + `) and buyer_id<>''`
  1126. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1127. if err != nil {
  1128. logx.Error(err)
  1129. return map[string]*projectInfo{}
  1130. }
  1131. vbs := map[string][]*idName{}
  1132. buyers := []string{}
  1133. repeat := map[string]bool{}
  1134. for rows.Next() {
  1135. var (
  1136. agency_id string
  1137. buyer_id string
  1138. buyer string
  1139. )
  1140. if err := rows.Scan(&agency_id, &buyer_id, &buyer); err != nil {
  1141. logx.Error(err)
  1142. continue
  1143. }
  1144. if buyer_id == "" || buyer == "" {
  1145. continue
  1146. }
  1147. vbs[agency_id] = append(vbs[agency_id], &idName{
  1148. Id: buyer_id,
  1149. Name: buyer,
  1150. })
  1151. if !repeat[buyer_id] {
  1152. repeat[buyer_id] = true
  1153. buyers = append(buyers, buyer_id)
  1154. }
  1155. }
  1156. rows.Close()
  1157. if err := rows.Err(); err != nil {
  1158. logx.Error(err)
  1159. }
  1160. return n.MakeProjectInfo(values, nil, vbs, businessType)
  1161. }
  1162. func (n *network) Introduce_Middleman_Buyer(values []string, businessType []string) map[string]*projectInfo {
  1163. return n.BuyerProjectInfo(values, businessType)
  1164. }
  1165. func (n *network) Introduce_Middleman_Project(values []string, businessType []string) map[string]*projectInfo {
  1166. result := map[string]*projectInfo{}
  1167. if len(values) == 0 {
  1168. return result
  1169. }
  1170. wh, args := WhArgs(values)
  1171. q := `select DISTINCT project_id,project_money from information.transaction_info_all where project_id in (` + wh + `)`
  1172. if len(businessType) > 0 {
  1173. newWh, newArgs := WhArgs(businessType)
  1174. q += ` and hasAny(topscopeclass,[` + newWh + `])`
  1175. args = append(args, newArgs...)
  1176. }
  1177. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1178. if err != nil {
  1179. logx.Error(err)
  1180. return nil
  1181. }
  1182. for rows.Next() {
  1183. var (
  1184. project_id string
  1185. project_money decimal.Decimal
  1186. )
  1187. if err := rows.Scan(&project_id, &project_money); err != nil {
  1188. logx.Error(err)
  1189. continue
  1190. }
  1191. pf, _ := project_money.Float64()
  1192. result[project_id] = &projectInfo{
  1193. ProjectAmount: pf,
  1194. }
  1195. }
  1196. rows.Close()
  1197. if err := rows.Err(); err != nil {
  1198. logx.Error(err)
  1199. }
  1200. return result
  1201. }
  1202. func (n *network) TypeStrConvert(itype string) int64 {
  1203. //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  1204. switch itype {
  1205. case "firstparty":
  1206. return 1
  1207. case "supplier":
  1208. return 2
  1209. case "adiffb":
  1210. return 3
  1211. case "middleman":
  1212. return 4
  1213. case "agency":
  1214. return 5
  1215. }
  1216. return 0
  1217. }
  1218. func (n *network) TypeIntConvert(itype int64) string {
  1219. //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  1220. switch itype {
  1221. case 1:
  1222. return "firstparty"
  1223. case 2:
  1224. return "supplier"
  1225. case 3:
  1226. return "adiffb"
  1227. case 4:
  1228. return "middleman"
  1229. case 5:
  1230. return "agency"
  1231. }
  1232. return ""
  1233. }
  1234. func (n *network) GetQyxyId(ids []string) map[string]string {
  1235. m := map[string]string{}
  1236. if len(ids) == 0 {
  1237. return m
  1238. }
  1239. wh, args := WhArgs(ids)
  1240. q := `select id,company_id from information.ent_info where id in (` + wh + `)`
  1241. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1242. if err != nil {
  1243. logx.Error(err)
  1244. } else {
  1245. for rows.Next() {
  1246. var (
  1247. id string
  1248. company_id string
  1249. )
  1250. if err := rows.Scan(&id, &company_id); err != nil {
  1251. logx.Error(err)
  1252. continue
  1253. }
  1254. m[id] = company_id
  1255. }
  1256. rows.Close()
  1257. if err := rows.Err(); err != nil {
  1258. logx.Error(err)
  1259. }
  1260. }
  1261. return m
  1262. }
  1263. func (n *network) BuyerProjectInfo(ids []string, businessType []string) map[string]*projectInfo {
  1264. vm := map[string]*projectInfo{}
  1265. m := map[string]bool{}
  1266. newIds := []string{}
  1267. for _, v := range ids {
  1268. if m[v] {
  1269. continue
  1270. }
  1271. m[v] = true
  1272. newIds = append(newIds, v)
  1273. if len(newIds) == C.BuyerBatch {
  1274. n.BuyerProjectInfoBatch(newIds, businessType, vm)
  1275. newIds = []string{}
  1276. }
  1277. }
  1278. if len(newIds) > 0 {
  1279. n.BuyerProjectInfoBatch(newIds, businessType, vm)
  1280. }
  1281. return vm
  1282. }
  1283. func (n *network) BuyerProjectInfoBatch(ids []string, businessType []string, vm map[string]*projectInfo) {
  1284. if len(ids) == 0 {
  1285. return
  1286. }
  1287. wh, args := WhArgs(ids)
  1288. q := `select buyer_id,count(DISTINCT project_id) AS project_count,sum(project_money) AS project_amount,groupUniqArray(project_id) from information.transaction_info_all where buyer_id in (` + wh + `) and zbtime>? and project_bidstatus>1`
  1289. args = append(args, NetworkCom.ProjectYearLimit())
  1290. if len(businessType) > 0 {
  1291. newWh, newArgs := WhArgs(businessType)
  1292. q += ` and hasAny(topscopeclass,[` + newWh + `])`
  1293. args = append(args, newArgs...)
  1294. }
  1295. q += ` group by buyer_id`
  1296. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1297. if err != nil {
  1298. logx.Error(err)
  1299. return
  1300. }
  1301. for rows.Next() {
  1302. var (
  1303. buyer_id string
  1304. project_count uint64
  1305. project_amount decimal.Decimal
  1306. project_id []string
  1307. )
  1308. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  1309. logx.Error(err)
  1310. continue
  1311. }
  1312. pf, _ := project_amount.Float64()
  1313. if vm[buyer_id] == nil {
  1314. vm[buyer_id] = &projectInfo{
  1315. ProjectCount: int64(project_count),
  1316. ExportId: project_id,
  1317. ProjectAmount: pf,
  1318. }
  1319. } else {
  1320. vm[buyer_id].ProjectAmount += pf
  1321. vm[buyer_id].ProjectCount += int64(project_count)
  1322. vm[buyer_id].ExportId = append(vm[buyer_id].ExportId, project_id...)
  1323. }
  1324. }
  1325. rows.Close()
  1326. if err := rows.Err(); err != nil {
  1327. logx.Error(err)
  1328. }
  1329. return
  1330. }
  1331. func (n *network) TjProject(agencyIds, winnerIds []string, vbs map[string][]*idName, businessType []string) map[string]*projectInfo {
  1332. vm := map[string]*projectInfo{}
  1333. if len(agencyIds) == 0 && len(winnerIds) == 0 {
  1334. return vm
  1335. }
  1336. yearLimit := NetworkCom.ProjectYearLimit()
  1337. allArgs := []interface{}{}
  1338. q := `select buyer_id,count(DISTINCT project_id) AS project_count,sum(project_money) AS project_amount,groupUniqArray(project_id) from information.transaction_info_all where buyer_id in (select DISTINCT buyer_id from information.transaction_info_all where `
  1339. if len(agencyIds) > 0 {
  1340. wh, args := WhArgs(agencyIds)
  1341. allArgs = append(allArgs, args...)
  1342. allArgs = append(allArgs, yearLimit)
  1343. q += `agency_id in (` + wh + `) and buyer_id<>'' and zbtime>?`
  1344. } else if len(winnerIds) > 0 {
  1345. wh, args := WhArgs(winnerIds)
  1346. allArgs = append(allArgs, args...)
  1347. allArgs = append(allArgs, yearLimit)
  1348. q += `hasAny(winner_id,[` + wh + `]) and buyer_id<>'' and zbtime>?`
  1349. }
  1350. q += `) and zbtime>? and project_bidstatus>1`
  1351. allArgs = append(allArgs, yearLimit)
  1352. if len(businessType) > 0 {
  1353. newWh, newArgs := WhArgs(businessType)
  1354. q += ` and hasAny(topscopeclass,[` + newWh + `])`
  1355. allArgs = append(allArgs, newArgs...)
  1356. }
  1357. q += ` group by buyer_id`
  1358. logx.Info("全部人脉项目sql", q, allArgs)
  1359. rows, err := ClickhouseConn.Query(context.Background(), q, allArgs...)
  1360. if err != nil {
  1361. logx.Error(err)
  1362. return vm
  1363. }
  1364. for rows.Next() {
  1365. var (
  1366. buyer_id string
  1367. project_count uint64
  1368. project_amount decimal.Decimal
  1369. project_id []string
  1370. )
  1371. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  1372. logx.Error(err)
  1373. continue
  1374. }
  1375. pf, _ := project_amount.Float64()
  1376. if vm[buyer_id] == nil {
  1377. vm[buyer_id] = &projectInfo{
  1378. ProjectCount: int64(project_count),
  1379. ExportId: project_id,
  1380. ProjectAmount: pf,
  1381. }
  1382. } else {
  1383. vm[buyer_id].ProjectAmount += pf
  1384. vm[buyer_id].ProjectCount += int64(project_count)
  1385. vm[buyer_id].ExportId = append(vm[buyer_id].ExportId, project_id...)
  1386. }
  1387. }
  1388. rows.Close()
  1389. if err := rows.Err(); err != nil {
  1390. logx.Error(err)
  1391. }
  1392. return vm
  1393. }
  1394. func (n *network) MakeProjectInfo(agencyIds, winnerIds []string, vbs map[string][]*idName, businessType []string) map[string]*projectInfo {
  1395. pis := n.TjProject(agencyIds, winnerIds, vbs, businessType)
  1396. vm := map[string]*projectInfo{}
  1397. for k, v := range vbs {
  1398. pi := &projectInfo{
  1399. BuyerCount: int64(len(v)),
  1400. IdNames: v,
  1401. }
  1402. re := map[string]bool{}
  1403. for _, vv := range v {
  1404. if pis[vv.Id] != nil {
  1405. pi.ProjectCount += pis[vv.Id].ProjectCount
  1406. pi.ProjectAmount += pis[vv.Id].ProjectAmount
  1407. for _, vvv := range pis[vv.Id].ExportId {
  1408. if re[vvv] {
  1409. continue
  1410. }
  1411. pi.ExportId = append(pi.ExportId, vvv)
  1412. re[vvv] = true
  1413. }
  1414. }
  1415. }
  1416. vm[k] = pi
  1417. }
  1418. return vm
  1419. }
  1420. func (n *network) AllIntroduceOwner(sqlAppend1, sqlAppend2 string, args []interface{}, isTjProject bool, businessType []string) *introduceOwnerProject {
  1421. 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
  1422. 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
  1423. listTemp := CrmMysql.SelectBySql(q, args...)
  1424. firstparty_array, supplier_array, adiffb_array, agency_array, middleman_buyer_array, middleman_project_array := []string{}, []string{}, []string{}, []string{}, []string{}, []string{}
  1425. myNetworks := []*myNetwork{}
  1426. for _, v := range *listTemp {
  1427. myNetwork := &myNetwork{
  1428. Id: Int64All(v["id"]),
  1429. Company_id: ObjToString(v["company_id"]),
  1430. Company_name: ObjToString(v["company_name"]),
  1431. Qyxy_id: ObjToString(v["qyxy_id"]),
  1432. Itype: Int64All(v["itype"]),
  1433. Person: ObjToString(v["person"]),
  1434. Phone: ObjToString(v["phone"]),
  1435. Buyer_count: Int64All(v["buyer_count"]),
  1436. Project_count: Int64All(v["project_count"]),
  1437. Relate_buyer_id: ObjToString(v["relate_buyer_id"]),
  1438. Relate_buyer_name: ObjToString(v["relate_buyer_name"]),
  1439. Relate_project_id: ObjToString(v["relate_project_id"]),
  1440. Create_time: ObjToString(v["create_time"]),
  1441. }
  1442. switch myNetwork.Itype {
  1443. case 1:
  1444. firstparty_array = append(firstparty_array, myNetwork.Company_id)
  1445. case 2:
  1446. supplier_array = append(supplier_array, myNetwork.Company_id)
  1447. case 3:
  1448. adiffb_array = append(adiffb_array, myNetwork.Company_id)
  1449. case 4:
  1450. if myNetwork.Relate_buyer_id != "" {
  1451. middleman_buyer_array = append(middleman_buyer_array, strings.Split(myNetwork.Relate_buyer_id, ",")...)
  1452. }
  1453. if myNetwork.Relate_project_id != "" {
  1454. middleman_project_array = append(middleman_project_array, strings.Split(myNetwork.Relate_project_id, ",")...)
  1455. }
  1456. case 5:
  1457. agency_array = append(agency_array, myNetwork.Company_id)
  1458. }
  1459. myNetworks = append(myNetworks, myNetwork)
  1460. }
  1461. iop := &introduceOwnerProject{
  1462. Networks: myNetworks,
  1463. FirstpartyNetwork: map[string][]*firstpartyNetwork{},
  1464. Firstparty: map[string]*projectInfo{},
  1465. Supplier: map[string]*projectInfo{},
  1466. Adiffb: map[string]*projectInfo{},
  1467. Agency: map[string]*projectInfo{},
  1468. Middleman: map[string]*projectInfo{},
  1469. }
  1470. if isTjProject {
  1471. iop.FirstpartyNetwork = n.FirstpartyNetwork("", firstparty_array)
  1472. iop.Firstparty = n.Introduce_Firstparty(iop.FirstpartyNetwork, businessType)
  1473. iop.Supplier = n.Introduce_Supplier(supplier_array, businessType)
  1474. iop.Adiffb = n.Introduce_Supplier(adiffb_array, businessType)
  1475. iop.Agency = n.Introduce_Agency(agency_array, businessType)
  1476. iop.Middleman = n.Introduce_Middleman_Buyer(middleman_buyer_array, businessType)
  1477. }
  1478. return iop
  1479. }
  1480. func (n *network) DeleteCache(positionId int64) {
  1481. redis.Del("newother", fmt.Sprintf(NetworkManageAllProjectKey, positionId))
  1482. redis.DelByCodePattern("newother", fmt.Sprintf(NetworkManageList, positionId, "*"))
  1483. redis.DelByCodePattern("newother", fmt.Sprintf(NetworkManageCoopHistory, positionId, "*"))
  1484. redis.DelByCodePattern("newother", fmt.Sprintf(NetworkManageOwnerlList, positionId, "*"))
  1485. redis.DelByCodePattern("newother", fmt.Sprintf(NetworkManageProjectList, positionId, "*"))
  1486. }