network.go 37 KB

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