network.go 39 KB

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