network.go 37 KB

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