network.go 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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. convList := &nodeTrees{}
  413. for _, v := range list {
  414. pm := &nodeTree{
  415. CODE: v.Name,
  416. NAME: v.Name,
  417. SZ_PID0: v.Name,
  418. SZ_LEVEL: 0,
  419. DATACOUNT: 0,
  420. }
  421. if len(v.Children) > 0 {
  422. pm.SZ_LEAF = 0
  423. } else {
  424. pm.SZ_LEAF = 1
  425. }
  426. sort.Sort(v)
  427. id, pType, pMyType, pMyId := "", "", "", ""
  428. for _, vv := range v.Children {
  429. myId := vv.CompanyId
  430. if vv.Type == "middleman" {
  431. myId = fmt.Sprint(vv.Id)
  432. }
  433. if pMyId != "" {
  434. pMyId += ","
  435. }
  436. pMyId += myId
  437. if pMyType != "" {
  438. pMyType += ","
  439. }
  440. pMyType += vv.Type
  441. myChildIds, myChildTypes := "", ""
  442. if in.Id == "" && v.Name == "甲方" {
  443. cccNodes := []*nodeTree{}
  444. cccIndexMap := map[string]int{}
  445. for _, vvv := range firstpartyChild[vv.CompanyId] {
  446. cccIndex, cccOk := cccIndexMap[vvv.Name]
  447. if cccOk {
  448. cccNodes[cccIndex].ID += "," + vvv.CompanyId
  449. cccNodes[cccIndex].MYID += "," + vvv.CompanyId
  450. cccNodes[cccIndex].TYPE += ",firstparty"
  451. cccNodes[cccIndex].MYTYPE += ",firstparty"
  452. } else {
  453. cccNodes = append(cccNodes, &nodeTree{
  454. NAME: vvv.Name,
  455. ID: vvv.CompanyId,
  456. SZ_PID0: v.Name,
  457. SZ_PID1: v.Name + ":" + myId,
  458. SZ_PID2: v.Name + ":" + myId + ":" + vvv.Name,
  459. CODE: v.Name + ":" + myId + ":" + vvv.Name,
  460. PCODE: v.Name + ":" + myId,
  461. SZ_LEVEL: 2,
  462. SZ_LEAF: 0,
  463. TYPE: "firstparty",
  464. MYID: vvv.CompanyId,
  465. MYTYPE: "firstparty",
  466. })
  467. cccIndexMap[vvv.Name] = len(cccNodes) - 1
  468. }
  469. if id != "" {
  470. id += ","
  471. }
  472. id += vvv.CompanyId
  473. if pType != "" {
  474. pType += ","
  475. }
  476. pType += vv.Type
  477. if myChildIds != "" {
  478. myChildIds += ","
  479. }
  480. myChildIds += vvv.CompanyId
  481. if myChildTypes != "" {
  482. myChildTypes += ","
  483. }
  484. myChildTypes += vv.Type
  485. *convList = append(*convList, &nodeTree{
  486. NAME: vvv.CompanyName,
  487. ID: vvv.CompanyId,
  488. SZ_PID0: v.Name,
  489. SZ_PID1: v.Name + ":" + myId,
  490. SZ_PID2: cccNodes[cccIndex].SZ_PID2,
  491. SZ_PID3: v.Name + ":" + myId + ":" + vvv.Name + ":" + vvv.CompanyId,
  492. CODE: v.Name + ":" + myId + ":" + vvv.Name + ":" + vvv.CompanyId,
  493. PCODE: cccNodes[cccIndex].CODE,
  494. SZ_LEVEL: 3,
  495. SZ_LEAF: 1,
  496. TYPE: "firstparty",
  497. MYID: vvv.CompanyId,
  498. MYTYPE: "firstparty",
  499. })
  500. }
  501. for _, cccNode := range cccNodes {
  502. *convList = append(*convList, cccNode)
  503. }
  504. } else {
  505. if id != "" {
  506. id += ","
  507. }
  508. id += myId
  509. if pType != "" {
  510. pType += ","
  511. }
  512. pType += vv.Type
  513. myChildIds = myId
  514. myChildTypes = vv.Type
  515. }
  516. cm := &nodeTree{
  517. NAME: vv.CompanyName,
  518. ID: myChildIds,
  519. SZ_PID0: v.Name,
  520. SZ_PID1: v.Name + ":" + myId,
  521. CODE: v.Name + ":" + myId,
  522. PCODE: v.Name,
  523. DATACOUNT: vv.Count,
  524. SZ_LEVEL: 1,
  525. SZ_LEAF: If(myChildIds == "", 1, 0).(int),
  526. TYPE: myChildTypes,
  527. MYID: myId,
  528. MYTYPE: vv.Type,
  529. }
  530. *convList = append(*convList, cm)
  531. }
  532. pm.ID = id
  533. pm.TYPE = pType
  534. pm.MYTYPE = pMyType
  535. pm.MYID = pMyId
  536. *convList = append(*convList, pm)
  537. }
  538. sort.Sort(convList)
  539. reply = &types.Reply{
  540. Data: map[string]interface{}{
  541. "count": count,
  542. "list": convList,
  543. },
  544. }
  545. return reply
  546. }
  547. //人脉库-列表
  548. func (n *network) List(in *types.NetWorkListReq) *types.Reply {
  549. if in.Page_size <= 0 {
  550. in.Page_size = 10
  551. }
  552. if in.Current_page <= 0 {
  553. in.Current_page = 1
  554. }
  555. 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
  556. sqlAppendArgs := []interface{}{}
  557. sqlAppend1 := ""
  558. if dbPaging && in.Type != "" {
  559. sqlAppend1 += ` and a.itype=?`
  560. sqlAppendArgs = append(sqlAppendArgs, n.TypeStrConvert(in.Type))
  561. }
  562. comSqlAppend := ""
  563. comSqlArgs := []interface{}{}
  564. if in.Starttime != "" {
  565. comSqlAppend += ` and a.create_time>=?`
  566. comSqlArgs = append(comSqlArgs, in.Starttime+" 00:00:00")
  567. }
  568. if in.Endtime != "" {
  569. comSqlAppend += ` and a.create_time<=?`
  570. if in.Starttime == in.Endtime {
  571. in.Endtime += " 23:59:59"
  572. } else {
  573. in.Endtime += " 00:00:00"
  574. }
  575. comSqlArgs = append(comSqlArgs, in.Endtime)
  576. }
  577. if in.Name != "" {
  578. comSqlAppend += ` and a.company_name like ?`
  579. comSqlArgs = append(comSqlArgs, "%"+in.Name+"%")
  580. }
  581. sqlAppend1 += comSqlAppend
  582. sqlAppendArgs = append(sqlAppendArgs, comSqlArgs...)
  583. var count int64
  584. start := (in.Current_page - 1) * in.Page_size
  585. args := []interface{}{in.PositionId, in.PositionId, in.PositionId}
  586. args = append(args, sqlAppendArgs...)
  587. sqlAppend2 := ""
  588. firstparty_count, supplier_count, adiffb_count, middleman_count, agency_count := 0, 0, 0, 0, 0
  589. if dbPaging {
  590. newArgs := []interface{}{in.PositionId}
  591. newArgs = append(newArgs, sqlAppendArgs...)
  592. count = CrmMysql.CountBySql(`select count(1) as count from crm.connection a where a.position_id=?`+sqlAppend1, newArgs...)
  593. sqlAppend2 = ` limit ?,?`
  594. args = append(args, start, in.Page_size)
  595. itemArgs := []interface{}{in.PositionId}
  596. itemArgs = append(itemArgs, comSqlArgs...)
  597. items := CrmMysql.SelectBySql(`SELECT itype,SUM(1) AS sum FROM crm.connection a WHERE a.position_id=?`+comSqlAppend+` GROUP BY a.itype`, itemArgs...)
  598. if items != nil {
  599. for _, v := range *items {
  600. switch Int64All(v["itype"]) {
  601. case 1:
  602. firstparty_count = IntAll(v["sum"])
  603. case 2:
  604. supplier_count = IntAll(v["sum"])
  605. case 3:
  606. adiffb_count = IntAll(v["sum"])
  607. case 4:
  608. middleman_count = IntAll(v["sum"])
  609. case 5:
  610. agency_count = IntAll(v["sum"])
  611. }
  612. }
  613. }
  614. }
  615. //
  616. list := []*map[string]interface{}{}
  617. isTjProject := true
  618. probusfors := []string{}
  619. if in.Project_matchme == 1 {
  620. probusfors = NetworkCom.GetMyProbusfor(in.EntAccountId)
  621. if len(probusfors) == 0 {
  622. isTjProject = false
  623. }
  624. }
  625. if isTjProject {
  626. entMonitor := NetworkCom.EntMonitor(in.UserId)
  627. aio := n.AllIntroduceOwner(sqlAppend1, sqlAppend2, args, isTjProject, probusfors, entMonitor)
  628. for _, v := range aio.Networks {
  629. itype := ""
  630. buyer_count, project_count, expect_amount, monitor_count := int64(0), int64(0), float64(0), int64(0)
  631. export_id := []string{}
  632. jump_type, jump_id := "", ""
  633. switch v.Itype {
  634. case 1:
  635. itype = "甲方"
  636. jump_type = "firstparty"
  637. for _, vv := range aio.FirstpartyNetwork[v.Company_id] {
  638. if jump_id != "" {
  639. jump_id += ","
  640. }
  641. jump_id += vv.CompanyId
  642. }
  643. if aio.Firstparty[v.Company_id] != nil {
  644. buyer_count = aio.Firstparty[v.Company_id].BuyerCount
  645. project_count = aio.Firstparty[v.Company_id].ProjectCount
  646. expect_amount = aio.Firstparty[v.Company_id].ProjectAmount
  647. monitor_count = aio.Firstparty[v.Company_id].MonitorCount
  648. export_id = aio.Firstparty[v.Company_id].ExportId
  649. }
  650. case 2:
  651. itype = "供应商"
  652. jump_type = "supplier"
  653. jump_id = v.Company_id
  654. if aio.Supplier[v.Company_id] != nil {
  655. buyer_count = aio.Supplier[v.Company_id].BuyerCount
  656. project_count = aio.Supplier[v.Company_id].ProjectCount
  657. expect_amount = aio.Supplier[v.Company_id].ProjectAmount
  658. monitor_count = aio.Supplier[v.Company_id].MonitorCount
  659. export_id = aio.Supplier[v.Company_id].ExportId
  660. }
  661. case 3:
  662. itype = "同甲异业渠道"
  663. jump_type = "adiffb"
  664. jump_id = v.Company_id
  665. if aio.Adiffb[v.Company_id] != nil {
  666. buyer_count = aio.Adiffb[v.Company_id].BuyerCount
  667. project_count = aio.Adiffb[v.Company_id].ProjectCount
  668. expect_amount = aio.Adiffb[v.Company_id].ProjectAmount
  669. monitor_count = aio.Adiffb[v.Company_id].MonitorCount
  670. export_id = aio.Adiffb[v.Company_id].ExportId
  671. }
  672. case 4:
  673. itype = "中间人"
  674. jump_type = "middleman"
  675. jump_id = fmt.Sprint(v.Id)
  676. buyer_count = v.Buyer_count
  677. if v.Relate_buyer_name != "" {
  678. for _, v := range strings.Split(v.Relate_buyer_name, ",") {
  679. if v == "" {
  680. continue
  681. }
  682. if entMonitor[v] {
  683. monitor_count++
  684. }
  685. }
  686. }
  687. if v.Relate_project_id != "" {
  688. export_id = strings.Split(v.Relate_project_id, ",")
  689. for _, v := range export_id {
  690. if aio.Middleman[v] != nil {
  691. project_count++
  692. expect_amount += aio.Middleman[v].ProjectAmount
  693. }
  694. }
  695. }
  696. case 5:
  697. itype = "招标代理机构"
  698. jump_type = "agency"
  699. jump_id = v.Company_id
  700. if aio.Agency[v.Company_id] != nil {
  701. buyer_count = aio.Agency[v.Company_id].BuyerCount
  702. project_count = aio.Agency[v.Company_id].ProjectCount
  703. expect_amount = aio.Agency[v.Company_id].ProjectAmount
  704. monitor_count = aio.Agency[v.Company_id].MonitorCount
  705. export_id = aio.Agency[v.Company_id].ExportId
  706. }
  707. }
  708. if buyer_count < in.Buyercount_start {
  709. continue
  710. } else if in.Buyercount_end > 0 && buyer_count > in.Buyercount_end {
  711. continue
  712. } else if in.Monitor == 1 && monitor_count <= 0 {
  713. continue
  714. } else if in.Monitor == -1 && monitor_count > 0 {
  715. continue
  716. } else if monitor_count < in.Monitorcount_start {
  717. continue
  718. } else if in.Monitorcount_end > 0 && monitor_count > in.Monitorcount_end {
  719. continue
  720. } else if in.Project_matchme == 1 && project_count == 0 {
  721. continue
  722. }
  723. if !dbPaging {
  724. switch v.Itype {
  725. case 1:
  726. firstparty_count++
  727. case 2:
  728. supplier_count++
  729. case 3:
  730. adiffb_count++
  731. case 4:
  732. middleman_count++
  733. case 5:
  734. agency_count++
  735. }
  736. if in.Type != "" && n.TypeStrConvert(in.Type) != v.Itype {
  737. continue
  738. }
  739. }
  740. export_url := ""
  741. if len(export_id) > 0 {
  742. exportIdRepeat := map[string]bool{}
  743. exportId := ""
  744. for _, v := range export_id {
  745. if exportIdRepeat[v] {
  746. continue
  747. }
  748. exportIdRepeat[v] = true
  749. if exportId != "" {
  750. exportId += ","
  751. }
  752. exportId += v
  753. }
  754. export_url = "/subscribepay/network/projectExport?export_id=" + encrypt.SE.EncodeStringByCheck(exportId)
  755. }
  756. url := ""
  757. if v.Qyxy_id != "" && (jump_type == "supplier" || jump_type == "adiffb") {
  758. url = "/swordfish/page_big_pc/ent_portrait/" + encrypt.EncodeArticleId2ByCheck(v.Qyxy_id)
  759. } else if jump_type == "firstparty" && v.Company_name != "" {
  760. url = "/swordfish/page_big_pc/unit_portrayal/" + v.Company_name
  761. }
  762. list = append(list, &map[string]interface{}{
  763. "company_id": v.Company_id,
  764. "company_name": v.Company_name,
  765. "type": itype,
  766. "jump_type": jump_type,
  767. "jump_id": jump_id,
  768. "person": v.Person,
  769. "phone": v.Phone,
  770. "buyer_count": buyer_count,
  771. "monitor_count": monitor_count,
  772. "expect_amount": RetainDecimal(expect_amount/10000, 2),
  773. "project_count": project_count,
  774. "create_time": v.Create_time,
  775. "export_url": export_url,
  776. "url": url,
  777. "id": v.Id,
  778. })
  779. }
  780. }
  781. csList := &ComSortList{
  782. SortKeys: []*ComSortKey{
  783. &ComSortKey{
  784. Keys: []string{"expect_amount"},
  785. Order: 1,
  786. Type: "float",
  787. },
  788. },
  789. List: list,
  790. }
  791. if in.Order_amount == -1 {
  792. csList.SortKeys[0].Order = -1
  793. }
  794. if in.Order_amount != 0 {
  795. sort.Sort(csList)
  796. }
  797. finalList := []*map[string]interface{}{}
  798. if !dbPaging {
  799. count = int64(len(csList.List))
  800. if count > 0 && start < count {
  801. end := start + in.Page_size
  802. if end > count {
  803. end = count
  804. }
  805. finalList = csList.List[start:end]
  806. }
  807. } else {
  808. finalList = csList.List
  809. }
  810. total_page := int64(math.Ceil(float64(count) / float64(in.Page_size)))
  811. return &types.Reply{
  812. Data: map[string]interface{}{
  813. "count": count,
  814. "total_page": total_page,
  815. "firstparty_count": firstparty_count,
  816. "supplier_count": supplier_count,
  817. "adiffb_count": adiffb_count,
  818. "middleman_count": middleman_count,
  819. "agency_count": agency_count,
  820. "list": finalList,
  821. },
  822. }
  823. }
  824. //
  825. func (n *network) FirstpartyNetwork(name string, values []string) map[string][]*firstpartyNetwork {
  826. result := map[string][]*firstpartyNetwork{}
  827. if len(values) == 0 {
  828. return result
  829. }
  830. wh, args := NetworkCom.WhArgs(values)
  831. 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
  832. inner join information.ent_code b on (a.a_id in (` + wh + `) and b.pcode in ('0100','0200') and a.code=b.code)`
  833. if name != "" {
  834. q += ` where c.company_name like ?`
  835. args = append(args, "%"+name+"%")
  836. }
  837. q += ` order by b.name,a.a_id,a.b_name`
  838. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  839. if err != nil {
  840. logx.Error(err)
  841. return result
  842. }
  843. for rows.Next() {
  844. var (
  845. a_id string
  846. company_id string
  847. company_name string
  848. name string
  849. )
  850. if err := rows.Scan(&a_id, &company_id, &company_name, &name); err != nil {
  851. logx.Error(err)
  852. continue
  853. }
  854. if company_id == "" || company_name == "" {
  855. continue
  856. }
  857. result[a_id] = append(result[a_id], &firstpartyNetwork{
  858. CompanyId: company_id,
  859. CompanyName: company_name,
  860. Name: name,
  861. })
  862. }
  863. return result
  864. }
  865. //
  866. func (n *network) Introduce_Firstparty(fpn map[string][]*firstpartyNetwork, entMonitor map[string]bool, probusfors []string) map[string]*projectInfo {
  867. values := []string{}
  868. vm := map[string]*projectInfo{}
  869. for _, v := range fpn {
  870. for _, vv := range v {
  871. vm[vv.CompanyId] = &projectInfo{}
  872. values = append(values, vv.CompanyId)
  873. }
  874. }
  875. result := map[string]*projectInfo{}
  876. if len(values) == 0 {
  877. return result
  878. }
  879. wh, args := NetworkCom.WhArgs(values)
  880. 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 + `)`
  881. if len(probusfors) > 0 {
  882. newWh, newArgs := NetworkCom.WhArgs(probusfors)
  883. q += ` and hasAny(property_form,[` + newWh + `])`
  884. args = append(args, newArgs...)
  885. }
  886. q += ` group by buyer_id`
  887. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  888. if err != nil {
  889. logx.Error(err)
  890. return nil
  891. }
  892. for rows.Next() {
  893. var (
  894. buyer_id string
  895. project_count uint64
  896. project_amount decimal.Decimal
  897. project_id []string
  898. )
  899. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  900. logx.Error(err)
  901. continue
  902. }
  903. if vm[buyer_id] == nil {
  904. continue
  905. }
  906. vm[buyer_id].ProjectCount += int64(project_count)
  907. pf, _ := project_amount.Float64()
  908. vm[buyer_id].ProjectAmount += pf
  909. vm[buyer_id].ExportId = project_id
  910. }
  911. rows.Close()
  912. if err := rows.Err(); err != nil {
  913. logx.Error(err)
  914. }
  915. for k, v := range fpn {
  916. if result[k] == nil {
  917. result[k] = &projectInfo{}
  918. }
  919. result[k].BuyerCount = int64(len(v))
  920. for _, vv := range v {
  921. if entMonitor[vv.CompanyName] {
  922. result[k].MonitorCount++
  923. }
  924. if vm[vv.CompanyId] == nil {
  925. continue
  926. }
  927. result[k].ProjectCount += vm[vv.CompanyId].ProjectCount
  928. result[k].ProjectAmount += vm[vv.CompanyId].ProjectAmount
  929. result[k].ExportId = append(result[k].ExportId, vm[vv.CompanyId].ExportId...)
  930. }
  931. }
  932. return result
  933. }
  934. //
  935. func (n *network) Introduce_Supplier(values []string, entMonitor map[string]bool, probusfors []string) map[string]*projectInfo {
  936. if len(values) == 0 {
  937. return map[string]*projectInfo{}
  938. }
  939. wh, args := NetworkCom.WhArgs(values)
  940. vbs := map[string][]*idName{}
  941. repeat := map[string]bool{}
  942. whRepeat := map[string]map[string]bool{}
  943. buyers := []string{}
  944. q := `select winner_id,buyer_id,buyer from information.transaction_info where hasAny(winner_id,[` + wh + `])`
  945. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  946. if err != nil {
  947. logx.Error(err)
  948. } else {
  949. for rows.Next() {
  950. var (
  951. winner_id []string
  952. buyer_id string
  953. buyer string
  954. )
  955. if err := rows.Scan(&winner_id, &buyer_id, &buyer); err != nil {
  956. logx.Error(err)
  957. continue
  958. }
  959. if buyer_id == "" || buyer == "" {
  960. continue
  961. }
  962. if !repeat[buyer_id] {
  963. repeat[buyer_id] = true
  964. buyers = append(buyers, buyer_id)
  965. }
  966. for _, v := range winner_id {
  967. if whRepeat[v] != nil && whRepeat[v][buyer_id] {
  968. continue
  969. }
  970. if whRepeat[v] == nil {
  971. whRepeat[v] = map[string]bool{}
  972. }
  973. whRepeat[v][buyer_id] = true
  974. vbs[v] = append(vbs[v], &idName{
  975. Id: buyer_id,
  976. Name: buyer,
  977. })
  978. }
  979. }
  980. rows.Close()
  981. if err := rows.Err(); err != nil {
  982. logx.Error(err)
  983. }
  984. }
  985. return n.MakeProjectInfo(buyers, vbs, probusfors, entMonitor)
  986. }
  987. //
  988. func (n *network) Introduce_Agency(values []string, entMonitor map[string]bool, probusfors []string) map[string]*projectInfo {
  989. if len(values) == 0 {
  990. return map[string]*projectInfo{}
  991. }
  992. wh, args := NetworkCom.WhArgs(values)
  993. q := `select DISTINCT agency_id,buyer_id,buyer from information.transaction_info where agency_id in (` + wh + `)`
  994. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  995. if err != nil {
  996. logx.Error(err)
  997. return nil
  998. }
  999. vbs := map[string][]*idName{}
  1000. buyers := []string{}
  1001. repeat := map[string]bool{}
  1002. for rows.Next() {
  1003. var (
  1004. agency_id string
  1005. buyer_id string
  1006. buyer string
  1007. )
  1008. if err := rows.Scan(&agency_id, &buyer_id, &buyer); err != nil {
  1009. logx.Error(err)
  1010. continue
  1011. }
  1012. if buyer_id == "" || buyer == "" {
  1013. continue
  1014. }
  1015. vbs[agency_id] = append(vbs[agency_id], &idName{
  1016. Id: buyer_id,
  1017. Name: buyer,
  1018. })
  1019. if !repeat[buyer_id] {
  1020. repeat[buyer_id] = true
  1021. buyers = append(buyers, buyer_id)
  1022. }
  1023. }
  1024. rows.Close()
  1025. if err := rows.Err(); err != nil {
  1026. logx.Error(err)
  1027. }
  1028. return n.MakeProjectInfo(buyers, vbs, probusfors, entMonitor)
  1029. }
  1030. //
  1031. func (n *network) Introduce_Middleman(values []string, entMonitor map[string]bool, probusfors []string) map[string]*projectInfo {
  1032. result := map[string]*projectInfo{}
  1033. if len(values) == 0 {
  1034. return result
  1035. }
  1036. wh, args := NetworkCom.WhArgs(values)
  1037. q := `select DISTINCT project_id,project_money from information.transaction_info where project_id in (` + wh + `)`
  1038. if len(probusfors) > 0 {
  1039. newWh, newArgs := NetworkCom.WhArgs(probusfors)
  1040. q += ` and hasAny(property_form,[` + newWh + `])`
  1041. args = append(args, newArgs...)
  1042. }
  1043. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1044. if err != nil {
  1045. logx.Error(err)
  1046. return nil
  1047. }
  1048. for rows.Next() {
  1049. var (
  1050. project_id string
  1051. project_money decimal.Decimal
  1052. )
  1053. if err := rows.Scan(&project_id, &project_money); err != nil {
  1054. logx.Error(err)
  1055. continue
  1056. }
  1057. pf, _ := project_money.Float64()
  1058. result[project_id] = &projectInfo{
  1059. ProjectAmount: pf,
  1060. }
  1061. }
  1062. rows.Close()
  1063. if err := rows.Err(); err != nil {
  1064. logx.Error(err)
  1065. }
  1066. return result
  1067. }
  1068. //
  1069. func (n *network) TypeStrConvert(itype string) int64 {
  1070. //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  1071. switch itype {
  1072. case "firstparty":
  1073. return 1
  1074. case "supplier":
  1075. return 2
  1076. case "adiffb":
  1077. return 3
  1078. case "middleman":
  1079. return 4
  1080. case "agency":
  1081. return 5
  1082. }
  1083. return 0
  1084. }
  1085. //
  1086. func (n *network) TypeIntConvert(itype int64) string {
  1087. //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  1088. switch itype {
  1089. case 1:
  1090. return "firstparty"
  1091. case 2:
  1092. return "supplier"
  1093. case 3:
  1094. return "adiffb"
  1095. case 4:
  1096. return "middleman"
  1097. case 5:
  1098. return "agency"
  1099. }
  1100. return ""
  1101. }
  1102. //
  1103. func (n *network) GetQyxyId(ids []string) map[string]string {
  1104. m := map[string]string{}
  1105. if len(ids) == 0 {
  1106. return m
  1107. }
  1108. wh, args := NetworkCom.WhArgs(ids)
  1109. q := `select id,company_id from information.ent_info where id in (` + wh + `)`
  1110. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1111. if err != nil {
  1112. logx.Error(err)
  1113. } else {
  1114. for rows.Next() {
  1115. var (
  1116. id string
  1117. company_id string
  1118. )
  1119. if err := rows.Scan(&id, &company_id); err != nil {
  1120. logx.Error(err)
  1121. continue
  1122. }
  1123. m[id] = company_id
  1124. }
  1125. rows.Close()
  1126. if err := rows.Err(); err != nil {
  1127. logx.Error(err)
  1128. }
  1129. }
  1130. return m
  1131. }
  1132. //
  1133. func (n *network) BuyerProjectInfo(ids []string, probusfors []string) map[string]*projectInfo {
  1134. vm := map[string]*projectInfo{}
  1135. if len(ids) == 0 {
  1136. return vm
  1137. }
  1138. wh, args := NetworkCom.WhArgs(ids)
  1139. 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 + `)`
  1140. if len(probusfors) > 0 {
  1141. newWh, newArgs := NetworkCom.WhArgs(probusfors)
  1142. q += ` and hasAny(property_form,[` + newWh + `])`
  1143. args = append(args, newArgs...)
  1144. }
  1145. q += ` group by buyer_id`
  1146. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1147. if err != nil {
  1148. logx.Error(err)
  1149. return nil
  1150. }
  1151. for rows.Next() {
  1152. var (
  1153. buyer_id string
  1154. project_count uint64
  1155. project_amount decimal.Decimal
  1156. project_id []string
  1157. )
  1158. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  1159. logx.Error(err)
  1160. continue
  1161. }
  1162. pf, _ := project_amount.Float64()
  1163. vm[buyer_id] = &projectInfo{
  1164. ProjectCount: int64(project_count),
  1165. ExportId: project_id,
  1166. ProjectAmount: pf,
  1167. }
  1168. }
  1169. rows.Close()
  1170. if err := rows.Err(); err != nil {
  1171. logx.Error(err)
  1172. }
  1173. return vm
  1174. }
  1175. //
  1176. func (n *network) MakeProjectInfo(buyers []string, vbs map[string][]*idName, probusfors []string, entMonitor map[string]bool) map[string]*projectInfo {
  1177. pis := n.BuyerProjectInfo(buyers, probusfors)
  1178. vm := map[string]*projectInfo{}
  1179. for k, v := range vbs {
  1180. pi := &projectInfo{
  1181. BuyerCount: int64(len(v)),
  1182. }
  1183. re := map[string]bool{}
  1184. for _, vv := range v {
  1185. if pis[vv.Id] != nil {
  1186. pi.ProjectCount += pis[vv.Id].ProjectCount
  1187. pi.ProjectAmount += pis[vv.Id].ProjectAmount
  1188. for _, vvv := range pis[vv.Id].ExportId {
  1189. if re[vvv] {
  1190. continue
  1191. }
  1192. pi.ExportId = append(pi.ExportId, vvv)
  1193. re[vvv] = true
  1194. }
  1195. }
  1196. if entMonitor[vv.Name] {
  1197. pi.MonitorCount++
  1198. }
  1199. }
  1200. vm[k] = pi
  1201. }
  1202. return vm
  1203. }
  1204. //
  1205. func (n *network) AllIntroduceOwner(sqlAppend1, sqlAppend2 string, args []interface{}, isTjProject bool, probusfors []string, entMonitor map[string]bool) *introduceOwnerProject {
  1206. 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
  1207. 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
  1208. listTemp := CrmMysql.SelectBySql(q, args...)
  1209. firstparty_array, supplier_array, adiffb_array, agency_array, middleman_project_array := []string{}, []string{}, []string{}, []string{}, []string{}
  1210. myNetworks := []*myNetwork{}
  1211. for _, v := range *listTemp {
  1212. myNetwork := &myNetwork{
  1213. Id: Int64All(v["id"]),
  1214. Company_id: ObjToString(v["company_id"]),
  1215. Company_name: ObjToString(v["company_name"]),
  1216. Qyxy_id: ObjToString(v["qyxy_id"]),
  1217. Itype: Int64All(v["itype"]),
  1218. Person: ObjToString(v["person"]),
  1219. Phone: ObjToString(v["phone"]),
  1220. Buyer_count: Int64All(v["buyer_count"]),
  1221. Project_count: Int64All(v["project_count"]),
  1222. Relate_buyer_id: ObjToString(v["relate_buyer_id"]),
  1223. Relate_buyer_name: ObjToString(v["relate_buyer_name"]),
  1224. Relate_project_id: ObjToString(v["relate_project_id"]),
  1225. Create_time: ObjToString(v["create_time"]),
  1226. }
  1227. switch myNetwork.Itype {
  1228. case 1:
  1229. firstparty_array = append(firstparty_array, myNetwork.Company_id)
  1230. case 2:
  1231. supplier_array = append(supplier_array, myNetwork.Company_id)
  1232. case 3:
  1233. adiffb_array = append(adiffb_array, myNetwork.Company_id)
  1234. case 4:
  1235. if myNetwork.Relate_project_id != "" {
  1236. middleman_project_array = append(middleman_project_array, strings.Split(myNetwork.Relate_project_id, ",")...)
  1237. }
  1238. case 5:
  1239. agency_array = append(agency_array, myNetwork.Company_id)
  1240. }
  1241. myNetworks = append(myNetworks, myNetwork)
  1242. }
  1243. iop := &introduceOwnerProject{
  1244. Networks: myNetworks,
  1245. FirstpartyNetwork: map[string][]*firstpartyNetwork{},
  1246. Firstparty: map[string]*projectInfo{},
  1247. Supplier: map[string]*projectInfo{},
  1248. Adiffb: map[string]*projectInfo{},
  1249. Agency: map[string]*projectInfo{},
  1250. Middleman: map[string]*projectInfo{},
  1251. }
  1252. if isTjProject {
  1253. iop.FirstpartyNetwork = n.FirstpartyNetwork("", firstparty_array)
  1254. iop.Firstparty = n.Introduce_Firstparty(iop.FirstpartyNetwork, entMonitor, probusfors)
  1255. iop.Supplier = n.Introduce_Supplier(supplier_array, entMonitor, probusfors)
  1256. iop.Adiffb = n.Introduce_Supplier(adiffb_array, entMonitor, probusfors)
  1257. iop.Agency = n.Introduce_Agency(agency_array, entMonitor, probusfors)
  1258. iop.Middleman = n.Introduce_Middleman(middleman_project_array, entMonitor, probusfors)
  1259. }
  1260. return iop
  1261. }