network.go 45 KB

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