network.go 37 KB

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