network.go 38 KB

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