network.go 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  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. key := fmt.Sprintf(NetworkManageAllProjectKey, in.PositionId)
  316. rbt, rerr := redis.GetNewBytes("newother", key)
  317. if rerr == nil && rbt != nil {
  318. json.Unmarshal(*rbt, &reply)
  319. return
  320. }
  321. businessType := strings.Split(FindBusiness(in.EntId, in.MgoUserId), ",")
  322. var count int64
  323. var list []*networkTree
  324. firstpartyChild := map[string][]*firstpartyNetwork{}
  325. if in.Id != "" {
  326. if in.Type == "firstparty" {
  327. fpn := n.FirstpartyNetwork(in.Name, []string{in.Id})
  328. if fpn[in.Id] != nil {
  329. firstparty := n.Introduce_Firstparty(fpn, businessType)
  330. nameIndex := map[string]int{}
  331. for _, v := range fpn[in.Id] {
  332. if _, ok := nameIndex[v.Name]; !ok {
  333. nameIndex[v.Name] = len(list)
  334. list = append(list, &networkTree{
  335. Name: v.Name,
  336. })
  337. }
  338. ntc := &networkTreeChild{
  339. CompanyName: v.CompanyName,
  340. CompanyId: v.CompanyId,
  341. Type: "firstparty",
  342. }
  343. if firstparty[v.CompanyId] != nil {
  344. ntc.Count = firstparty[v.CompanyId].ProjectCount
  345. }
  346. count++
  347. list[nameIndex[v.Name]].Count += ntc.Count
  348. list[nameIndex[v.Name]].Children = append(list[nameIndex[v.Name]].Children, ntc)
  349. }
  350. }
  351. }
  352. } else {
  353. args := []interface{}{in.PositionId, in.PositionId, in.PositionId}
  354. sqlAppend1 := ""
  355. if in.Name != "" {
  356. sqlAppend1 += ` and a.company_name like ?`
  357. args = append(args, "%"+in.Name+"%")
  358. }
  359. aio := n.AllIntroduceOwner(sqlAppend1, "", args, true, businessType)
  360. firstpartyChild = aio.FirstpartyNetwork
  361. list = []*networkTree{
  362. &networkTree{
  363. Name: "甲方",
  364. Children: []*networkTreeChild{},
  365. },
  366. &networkTree{
  367. Name: "供应商/渠道",
  368. Children: []*networkTreeChild{},
  369. },
  370. &networkTree{
  371. Name: "同甲异业渠道",
  372. Children: []*networkTreeChild{},
  373. },
  374. &networkTree{
  375. Name: "中间人",
  376. Children: []*networkTreeChild{},
  377. },
  378. &networkTree{
  379. Name: "招标代理",
  380. Children: []*networkTreeChild{},
  381. },
  382. }
  383. //
  384. for _, v := range aio.Networks {
  385. if v.Itype <= 0 || v.Itype > int64(len(list)) {
  386. return
  387. }
  388. ntc := &networkTreeChild{
  389. Id: v.Id,
  390. CompanyName: v.Company_name,
  391. CompanyId: v.Company_id,
  392. Type: n.TypeIntConvert(v.Itype),
  393. CreateTime: v.Create_time,
  394. }
  395. switch v.Itype {
  396. case 1:
  397. if aio.Firstparty[v.Company_id] != nil {
  398. ntc.Count = aio.Firstparty[v.Company_id].ProjectCount
  399. }
  400. case 2:
  401. if aio.Supplier[v.Company_id] != nil {
  402. ntc.Count = aio.Supplier[v.Company_id].ProjectCount
  403. ntc.IdNames = aio.Supplier[v.Company_id].IdNames
  404. }
  405. case 3:
  406. if aio.Adiffb[v.Company_id] != nil {
  407. ntc.Count = aio.Adiffb[v.Company_id].ProjectCount
  408. ntc.IdNames = aio.Adiffb[v.Company_id].IdNames
  409. }
  410. case 4:
  411. if v.Relate_buyer_id != "" {
  412. for _, rbi := range strings.Split(v.Relate_buyer_id, ",") {
  413. ntc.IdNames = append(ntc.IdNames, &idName{Id: rbi})
  414. if aio.Middleman[rbi] != nil {
  415. ntc.Count++
  416. }
  417. }
  418. }
  419. case 5:
  420. if aio.Agency[v.Company_id] != nil {
  421. ntc.Count = aio.Agency[v.Company_id].ProjectCount
  422. ntc.IdNames = aio.Agency[v.Company_id].IdNames
  423. }
  424. }
  425. count += ntc.Count
  426. list[v.Itype-1].Count += ntc.Count
  427. list[v.Itype-1].Children = append(list[v.Itype-1].Children, ntc)
  428. }
  429. }
  430. checkCode := []string{}
  431. parentConvList := &nodeTrees{}
  432. convList := &nodeTrees{}
  433. hasDefaultCheck := false
  434. if in.ClickId != "" {
  435. hasDefaultCheck = true
  436. }
  437. for _, v := range list {
  438. if len(v.Children) == 0 {
  439. continue
  440. }
  441. parentStartCheckCodeLen := len(checkCode)
  442. pm := &nodeTree{
  443. CODE: v.Name,
  444. NAME: v.Name,
  445. SZ_PID0: v.Name,
  446. SZ_LEVEL: 0,
  447. DATACOUNT: 0,
  448. }
  449. if len(v.Children) > 0 {
  450. pm.SZ_LEAF = 0
  451. } else {
  452. pm.SZ_LEAF = 1
  453. }
  454. sort.Sort(v)
  455. id, pType, pMyType, pMyId := "", "", "", ""
  456. for _, vv := range v.Children {
  457. childStartCheckCodeLen := len(checkCode)
  458. myId := vv.CompanyId
  459. if vv.Type == "middleman" {
  460. myId = fmt.Sprint(vv.Id)
  461. }
  462. if pMyId != "" {
  463. pMyId += ","
  464. }
  465. pMyId += myId
  466. if pMyType != "" {
  467. pMyType += ","
  468. }
  469. pMyType += vv.Type
  470. myChildIds, myChildTypes := "", ""
  471. compareId := fmt.Sprint(vv.Id)
  472. if in.Id == "" && v.Name == "甲方" {
  473. cccNodes := []*nodeTree{}
  474. cccIndexMap := map[string]int{}
  475. for _, vvv := range firstpartyChild[vv.CompanyId] {
  476. if in.CheckCode != "" {
  477. compareId = vvv.CompanyId
  478. }
  479. cccIndex, cccOk := cccIndexMap[vvv.Name]
  480. if cccOk {
  481. cccNodes[cccIndex].ID += "," + vvv.CompanyId
  482. cccNodes[cccIndex].MYID += "," + vvv.CompanyId
  483. cccNodes[cccIndex].TYPE += ",firstparty"
  484. cccNodes[cccIndex].MYTYPE += ",firstparty"
  485. } else {
  486. nt := &nodeTree{
  487. NAME: vvv.Name,
  488. ID: vvv.CompanyId,
  489. SZ_PID0: v.Name,
  490. SZ_PID1: v.Name + ":" + myId,
  491. SZ_PID2: v.Name + ":" + myId + ":" + vvv.Name,
  492. CODE: v.Name + ":" + myId + ":" + vvv.Name,
  493. PCODE: v.Name + ":" + myId,
  494. SZ_LEVEL: 2,
  495. SZ_LEAF: 0,
  496. TYPE: "firstparty",
  497. MYID: vvv.CompanyId,
  498. MYTYPE: "firstparty",
  499. }
  500. cccNodes = append(cccNodes, nt)
  501. cccIndexMap[vvv.Name] = len(cccNodes) - 1
  502. if !hasDefaultCheck || compareId == in.ClickId {
  503. checkCode = append(checkCode, nt.CODE)
  504. }
  505. }
  506. if id != "" {
  507. id += ","
  508. }
  509. id += vvv.CompanyId
  510. if pType != "" {
  511. pType += ","
  512. }
  513. pType += vv.Type
  514. if myChildIds != "" {
  515. myChildIds += ","
  516. }
  517. myChildIds += vvv.CompanyId
  518. if myChildTypes != "" {
  519. myChildTypes += ","
  520. }
  521. myChildTypes += vv.Type
  522. nt := &nodeTree{
  523. NAME: vvv.CompanyName,
  524. ID: vvv.CompanyId,
  525. SZ_PID0: v.Name,
  526. SZ_PID1: v.Name + ":" + myId,
  527. SZ_PID2: cccNodes[cccIndex].SZ_PID2,
  528. SZ_PID3: v.Name + ":" + myId + ":" + vvv.Name + ":" + vvv.CompanyId,
  529. CODE: v.Name + ":" + myId + ":" + vvv.Name + ":" + vvv.CompanyId,
  530. PCODE: cccNodes[cccIndex].CODE,
  531. SZ_LEVEL: 3,
  532. SZ_LEAF: 1,
  533. TYPE: "firstparty",
  534. MYID: vvv.CompanyId,
  535. MYTYPE: "firstparty",
  536. }
  537. if !hasDefaultCheck || compareId == in.ClickId {
  538. checkCode = append(checkCode, nt.CODE)
  539. }
  540. *convList = append(*convList, nt)
  541. }
  542. for _, cccNode := range cccNodes {
  543. *convList = append(*convList, cccNode)
  544. }
  545. } else {
  546. if id != "" {
  547. id += ","
  548. }
  549. id += myId
  550. if pType != "" {
  551. pType += ","
  552. }
  553. pType += vv.Type
  554. myChildIds = myId
  555. myChildTypes = vv.Type
  556. if in.CheckCode != "" {
  557. if len(vv.IdNames) == 0 {
  558. compareId = "-1"
  559. }
  560. for in_k, in_v := range vv.IdNames {
  561. if in_v.Id == in.ClickId {
  562. break
  563. } else if in_k == len(vv.IdNames)-1 {
  564. compareId = "-1"
  565. }
  566. }
  567. }
  568. }
  569. cm := &nodeTree{
  570. NAME: vv.CompanyName,
  571. ID: myChildIds,
  572. SZ_PID0: v.Name,
  573. SZ_PID1: v.Name + ":" + myId,
  574. CODE: v.Name + ":" + myId,
  575. PCODE: v.Name,
  576. DATACOUNT: vv.Count,
  577. SZ_LEVEL: 1,
  578. SZ_LEAF: If(myChildIds == "", 1, 0).(int),
  579. TYPE: myChildTypes,
  580. MYID: myId,
  581. MYTYPE: vv.Type,
  582. }
  583. //如果甲方的子节点被选中了,父节点跟着被选中,非甲方的要对比子节点可介绍业主
  584. if !hasDefaultCheck || childStartCheckCodeLen != len(checkCode) || (v.Name != "甲方" && compareId == fmt.Sprint(vv.Id)) {
  585. checkCode = append(checkCode, cm.CODE)
  586. }
  587. *convList = append(*convList, cm)
  588. }
  589. pm.ID = id
  590. pm.TYPE = pType
  591. pm.MYTYPE = pMyType
  592. pm.MYID = pMyId
  593. *parentConvList = append(*parentConvList, pm)
  594. if parentStartCheckCodeLen != len(checkCode) {
  595. checkCode = append(checkCode, pm.CODE)
  596. }
  597. hasDefaultCheck = true
  598. }
  599. sort.Sort(convList)
  600. *parentConvList = append(*parentConvList, *convList...)
  601. var newCheckCode []string
  602. if in.CheckCode != "" {
  603. m := map[string]bool{}
  604. for _, v := range strings.Split(in.CheckCode, ",") {
  605. m[v] = true
  606. }
  607. for _, v := range checkCode {
  608. if m[v] {
  609. newCheckCode = append(newCheckCode, v)
  610. }
  611. }
  612. } else {
  613. newCheckCode = checkCode
  614. }
  615. reply = &types.Reply{
  616. Data: map[string]interface{}{
  617. "count": count,
  618. "list": parentConvList,
  619. "checked": checkCode,
  620. },
  621. }
  622. redis.Put("newother", key, reply, C.CacheTimeOut)
  623. return reply
  624. }
  625. // 人脉库-列表
  626. func (n *network) List(in *types.NetWorkListReq) *types.Reply {
  627. if in.Page_size <= 0 {
  628. in.Page_size = 10
  629. }
  630. if in.Current_page <= 0 {
  631. in.Current_page = 1
  632. }
  633. dbPaging := in.Buyercount_start == 0 && in.Buyercount_end == 0 && in.Monitor == 0 && in.Monitorcount_start == 0 && in.Monitorcount_end == 0 && in.Order_amount == 0
  634. sqlAppendArgs := []interface{}{}
  635. sqlAppend1 := ""
  636. if dbPaging && in.Type != "" {
  637. sqlAppend1 += ` and a.itype=?`
  638. sqlAppendArgs = append(sqlAppendArgs, n.TypeStrConvert(in.Type))
  639. }
  640. comSqlAppend := ""
  641. comSqlArgs := []interface{}{}
  642. if in.Starttime != "" {
  643. comSqlAppend += ` and a.create_time>=?`
  644. comSqlArgs = append(comSqlArgs, in.Starttime+" 00:00:00")
  645. }
  646. if in.Endtime != "" {
  647. comSqlAppend += ` and a.create_time<=?`
  648. if in.Starttime == in.Endtime {
  649. in.Endtime += " 23:59:59"
  650. } else {
  651. in.Endtime += " 00:00:00"
  652. }
  653. comSqlArgs = append(comSqlArgs, in.Endtime)
  654. }
  655. if in.Name != "" {
  656. comSqlAppend += ` and a.company_name like ?`
  657. comSqlArgs = append(comSqlArgs, "%"+in.Name+"%")
  658. }
  659. sqlAppend1 += comSqlAppend
  660. sqlAppendArgs = append(sqlAppendArgs, comSqlArgs...)
  661. var count int64
  662. start := (in.Current_page - 1) * in.Page_size
  663. args := []interface{}{in.PositionId, in.PositionId, in.PositionId}
  664. args = append(args, sqlAppendArgs...)
  665. sqlAppend2 := ""
  666. firstparty_count, supplier_count, adiffb_count, middleman_count, agency_count := 0, 0, 0, 0, 0
  667. if dbPaging {
  668. newArgs := []interface{}{in.PositionId}
  669. newArgs = append(newArgs, sqlAppendArgs...)
  670. count = CrmMysql.CountBySql(`select count(1) as count from crm.connection a where a.position_id=?`+sqlAppend1, newArgs...)
  671. sqlAppend2 = ` limit ?,?`
  672. args = append(args, start, in.Page_size)
  673. itemArgs := []interface{}{in.PositionId}
  674. itemArgs = append(itemArgs, comSqlArgs...)
  675. items := CrmMysql.SelectBySql(`SELECT itype,SUM(1) AS sum FROM crm.connection a WHERE a.position_id=?`+comSqlAppend+` GROUP BY a.itype`, itemArgs...)
  676. if items != nil {
  677. for _, v := range *items {
  678. switch Int64All(v["itype"]) {
  679. case 1:
  680. firstparty_count = IntAll(v["sum"])
  681. case 2:
  682. supplier_count = IntAll(v["sum"])
  683. case 3:
  684. adiffb_count = IntAll(v["sum"])
  685. case 4:
  686. middleman_count = IntAll(v["sum"])
  687. case 5:
  688. agency_count = IntAll(v["sum"])
  689. }
  690. }
  691. }
  692. }
  693. //
  694. list := []*map[string]interface{}{}
  695. isTjProject := true
  696. businessType := []string{}
  697. businessType = strings.Split(FindBusiness(in.EntId, in.MgoUserId), ",")
  698. if len(businessType) == 0 {
  699. isTjProject = false
  700. }
  701. redisKey := fmt.Sprintf(NetworkManageList, in.PositionId, GetMd5String(fmt.Sprintf("%+v", in)))
  702. aio := &introduceOwnerProject{}
  703. aioByte, aioErr := redis.GetNewBytes("newother", redisKey)
  704. if aioErr == nil && aioByte != nil {
  705. json.Unmarshal(*aioByte, &aio)
  706. } else {
  707. aio = n.AllIntroduceOwner(sqlAppend1, sqlAppend2, args, isTjProject, businessType)
  708. redis.Put("newother", redisKey, aio, C.CacheTimeOut)
  709. }
  710. entMonitor := NetworkCom.EntMonitor(in.UserId)
  711. for _, v := range aio.Networks {
  712. itype := ""
  713. buyer_count, project_count, expect_amount, monitor_count := int64(0), int64(0), float64(0), int64(0)
  714. export_id := []string{}
  715. jump_type, jump_id := "", ""
  716. switch v.Itype {
  717. case 1:
  718. itype = "甲方"
  719. jump_type = "firstparty"
  720. for _, vv := range aio.FirstpartyNetwork[v.Company_id] {
  721. if jump_id != "" {
  722. jump_id += ","
  723. }
  724. jump_id += vv.CompanyId
  725. if entMonitor[vv.CompanyName] {
  726. monitor_count++
  727. }
  728. }
  729. if aio.Firstparty[v.Company_id] != nil {
  730. buyer_count = aio.Firstparty[v.Company_id].BuyerCount
  731. project_count = aio.Firstparty[v.Company_id].ProjectCount
  732. expect_amount = aio.Firstparty[v.Company_id].ProjectAmount
  733. export_id = aio.Firstparty[v.Company_id].ExportId
  734. }
  735. case 2:
  736. itype = "供应商/渠道"
  737. jump_type = "supplier"
  738. jump_id = v.Company_id
  739. if aio.Supplier[v.Company_id] != nil {
  740. buyer_count = aio.Supplier[v.Company_id].BuyerCount
  741. project_count = aio.Supplier[v.Company_id].ProjectCount
  742. expect_amount = aio.Supplier[v.Company_id].ProjectAmount
  743. export_id = aio.Supplier[v.Company_id].ExportId
  744. for _, vv := range aio.Supplier[v.Company_id].IdNames {
  745. if entMonitor[vv.Name] {
  746. monitor_count++
  747. }
  748. }
  749. }
  750. case 3:
  751. itype = "同甲异业渠道"
  752. jump_type = "adiffb"
  753. jump_id = v.Company_id
  754. if aio.Adiffb[v.Company_id] != nil {
  755. buyer_count = aio.Adiffb[v.Company_id].BuyerCount
  756. project_count = aio.Adiffb[v.Company_id].ProjectCount
  757. expect_amount = aio.Adiffb[v.Company_id].ProjectAmount
  758. export_id = aio.Adiffb[v.Company_id].ExportId
  759. for _, vv := range aio.Adiffb[v.Company_id].IdNames {
  760. if entMonitor[vv.Name] {
  761. monitor_count++
  762. }
  763. }
  764. }
  765. case 4:
  766. itype = "中间人"
  767. jump_type = "middleman"
  768. jump_id = fmt.Sprint(v.Id)
  769. buyer_count = v.Buyer_count
  770. if v.Relate_buyer_name != "" {
  771. for _, v := range strings.Split(v.Relate_buyer_name, ",") {
  772. if v == "" {
  773. continue
  774. }
  775. if entMonitor[v] {
  776. monitor_count++
  777. }
  778. }
  779. }
  780. if v.Relate_buyer_id != "" {
  781. for _, v := range strings.Split(v.Relate_buyer_id, ",") {
  782. if aio.Middleman[v] != nil {
  783. project_count++
  784. expect_amount += aio.Middleman[v].ProjectAmount
  785. export_id = append(export_id, aio.Middleman[v].ExportId...)
  786. }
  787. }
  788. }
  789. case 5:
  790. itype = "招标代理机构"
  791. jump_type = "agency"
  792. jump_id = v.Company_id
  793. if aio.Agency[v.Company_id] != nil {
  794. buyer_count = aio.Agency[v.Company_id].BuyerCount
  795. project_count = aio.Agency[v.Company_id].ProjectCount
  796. expect_amount = aio.Agency[v.Company_id].ProjectAmount
  797. export_id = aio.Agency[v.Company_id].ExportId
  798. for _, vv := range aio.Agency[v.Company_id].IdNames {
  799. if entMonitor[vv.Name] {
  800. monitor_count++
  801. }
  802. }
  803. }
  804. }
  805. if buyer_count < in.Buyercount_start {
  806. continue
  807. } else if in.Buyercount_end > 0 && buyer_count > in.Buyercount_end {
  808. continue
  809. } else if in.Monitor == 1 && monitor_count <= 0 {
  810. continue
  811. } else if in.Monitor == -1 && monitor_count > 0 {
  812. continue
  813. } else if monitor_count < in.Monitorcount_start {
  814. continue
  815. } else if in.Monitorcount_end > 0 && monitor_count > in.Monitorcount_end {
  816. continue
  817. }
  818. if !dbPaging {
  819. switch v.Itype {
  820. case 1:
  821. firstparty_count++
  822. case 2:
  823. supplier_count++
  824. case 3:
  825. adiffb_count++
  826. case 4:
  827. middleman_count++
  828. case 5:
  829. agency_count++
  830. }
  831. if in.Type != "" && n.TypeStrConvert(in.Type) != v.Itype {
  832. continue
  833. }
  834. }
  835. export_url := ""
  836. if len(export_id) > 0 {
  837. exportIdRepeat := map[string]bool{}
  838. exportId := ""
  839. for _, v := range export_id {
  840. if exportIdRepeat[v] {
  841. continue
  842. }
  843. exportIdRepeat[v] = true
  844. if exportId != "" {
  845. exportId += ","
  846. }
  847. exportId += v
  848. }
  849. md5Id := GetMd5String(exportId)
  850. redis.Put("newother", fmt.Sprintf("network_export_%s", md5Id), exportId, 259200)
  851. export_url = "/subscribepay/network/projectExport?version=1&export_id=" + md5Id
  852. }
  853. url := ""
  854. if v.Qyxy_id != "" && (jump_type == "supplier" || jump_type == "adiffb") {
  855. url = "/swordfish/page_big_pc/ent_portrait/" + encrypt.EncodeArticleId2ByCheck(v.Qyxy_id)
  856. } else if jump_type == "firstparty" && v.Company_name != "" {
  857. url = "/swordfish/page_big_pc/unit_portrayal/" + v.Company_name
  858. }
  859. list = append(list, &map[string]interface{}{
  860. "company_id": v.Company_id,
  861. "company_name": v.Company_name,
  862. "type": itype,
  863. "jump_type": jump_type,
  864. "jump_id": jump_id,
  865. "person": v.Person,
  866. "phone": v.Phone,
  867. "buyer_count": buyer_count,
  868. "monitor_count": monitor_count,
  869. "expect_amount": RetainDecimal(expect_amount/10000, 2),
  870. "project_count": project_count,
  871. "create_time": v.Create_time,
  872. "export_url": export_url,
  873. "url": url,
  874. "id": v.Id,
  875. })
  876. }
  877. csList := &ComSortList{
  878. SortKeys: []*ComSortKey{
  879. &ComSortKey{
  880. Keys: []string{"expect_amount"},
  881. Order: 1,
  882. Type: "float",
  883. },
  884. },
  885. List: list,
  886. }
  887. if in.Order_amount == -1 {
  888. csList.SortKeys[0].Order = -1
  889. }
  890. if in.Order_amount != 0 {
  891. sort.Sort(csList)
  892. }
  893. finalList := []*map[string]interface{}{}
  894. if !dbPaging {
  895. count = int64(len(csList.List))
  896. if count > 0 && start < count {
  897. end := start + in.Page_size
  898. if end > count {
  899. end = count
  900. }
  901. finalList = csList.List[start:end]
  902. }
  903. } else {
  904. finalList = csList.List
  905. }
  906. total_page := int64(math.Ceil(float64(count) / float64(in.Page_size)))
  907. return &types.Reply{
  908. Data: map[string]interface{}{
  909. "count": count,
  910. "total_page": total_page,
  911. "firstparty_count": firstparty_count,
  912. "supplier_count": supplier_count,
  913. "adiffb_count": adiffb_count,
  914. "middleman_count": middleman_count,
  915. "agency_count": agency_count,
  916. "list": finalList,
  917. },
  918. }
  919. }
  920. func (n *network) FirstpartyNetwork(name string, values []string) map[string][]*firstpartyNetwork {
  921. result := map[string][]*firstpartyNetwork{}
  922. if len(values) == 0 {
  923. return result
  924. }
  925. wh, args := WhArgs(values)
  926. 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
  927. inner join information.ent_code b on (a.a_id in (` + wh + `) and b.pcode in ('0100','0200') and a.code=b.code)`
  928. if name != "" {
  929. q += ` where c.company_name like ?`
  930. args = append(args, "%"+name+"%")
  931. }
  932. q += ` order by b.name,a.a_id,a.b_name`
  933. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  934. if err != nil {
  935. logx.Error(err)
  936. return result
  937. }
  938. for rows.Next() {
  939. var (
  940. a_id string
  941. company_id string
  942. company_name string
  943. name string
  944. )
  945. if err := rows.Scan(&a_id, &company_id, &company_name, &name); err != nil {
  946. logx.Error(err)
  947. continue
  948. }
  949. if company_id == "" || company_name == "" {
  950. continue
  951. }
  952. result[a_id] = append(result[a_id], &firstpartyNetwork{
  953. CompanyId: company_id,
  954. CompanyName: company_name,
  955. Name: name,
  956. })
  957. }
  958. return result
  959. }
  960. func (n *network) Introduce_Firstparty(fpn map[string][]*firstpartyNetwork, businessType []string) map[string]*projectInfo {
  961. values := []string{}
  962. vm := map[string]*projectInfo{}
  963. for _, v := range fpn {
  964. for _, vv := range v {
  965. vm[vv.CompanyId] = &projectInfo{}
  966. values = append(values, vv.CompanyId)
  967. }
  968. }
  969. result := map[string]*projectInfo{}
  970. if len(values) == 0 {
  971. return result
  972. }
  973. wh, args := WhArgs(values)
  974. 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 + `)`
  975. if len(businessType) > 0 {
  976. newWh, newArgs := WhArgs(businessType)
  977. q += ` and hasAny(topscopeclass,[` + newWh + `])`
  978. args = append(args, newArgs...)
  979. }
  980. q += ` and project_bidstatus>1 group by buyer_id`
  981. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  982. if err != nil {
  983. logx.Error(err)
  984. return nil
  985. }
  986. for rows.Next() {
  987. var (
  988. buyer_id string
  989. project_count uint64
  990. project_amount decimal.Decimal
  991. project_id []string
  992. )
  993. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  994. logx.Error(err)
  995. continue
  996. }
  997. if vm[buyer_id] == nil {
  998. continue
  999. }
  1000. vm[buyer_id].ProjectCount += int64(project_count)
  1001. pf, _ := project_amount.Float64()
  1002. vm[buyer_id].ProjectAmount += pf
  1003. vm[buyer_id].ExportId = project_id
  1004. }
  1005. rows.Close()
  1006. if err := rows.Err(); err != nil {
  1007. logx.Error(err)
  1008. }
  1009. for k, v := range fpn {
  1010. if result[k] == nil {
  1011. result[k] = &projectInfo{}
  1012. }
  1013. result[k].BuyerCount = int64(len(v))
  1014. for _, vv := range v {
  1015. if vm[vv.CompanyId] == nil {
  1016. continue
  1017. }
  1018. result[k].ProjectCount += vm[vv.CompanyId].ProjectCount
  1019. result[k].ProjectAmount += vm[vv.CompanyId].ProjectAmount
  1020. result[k].ExportId = append(result[k].ExportId, vm[vv.CompanyId].ExportId...)
  1021. }
  1022. }
  1023. return result
  1024. }
  1025. func (n *network) Introduce_Supplier(values []string, businessType []string) map[string]*projectInfo {
  1026. if len(values) == 0 {
  1027. return map[string]*projectInfo{}
  1028. }
  1029. wh, args := WhArgs(values)
  1030. repeat := map[string]bool{}
  1031. whRepeat := map[string]map[string]bool{}
  1032. buyers := []string{}
  1033. q := `select winner_id,buyer_id,buyer from information.transaction_info_all where hasAny(winner_id,[` + wh + `]) and buyer_id<>''`
  1034. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1035. if err != nil {
  1036. logx.Error(err)
  1037. return map[string]*projectInfo{}
  1038. }
  1039. vbs := map[string][]*idName{}
  1040. for rows.Next() {
  1041. var (
  1042. winner_id []string
  1043. buyer_id string
  1044. buyer string
  1045. )
  1046. if err := rows.Scan(&winner_id, &buyer_id, &buyer); err != nil {
  1047. logx.Error(err)
  1048. continue
  1049. }
  1050. if buyer_id == "" || buyer == "" {
  1051. continue
  1052. }
  1053. if !repeat[buyer_id] {
  1054. repeat[buyer_id] = true
  1055. buyers = append(buyers, buyer_id)
  1056. }
  1057. for _, v := range winner_id {
  1058. if whRepeat[v] != nil && whRepeat[v][buyer_id] {
  1059. continue
  1060. }
  1061. if whRepeat[v] == nil {
  1062. whRepeat[v] = map[string]bool{}
  1063. }
  1064. whRepeat[v][buyer_id] = true
  1065. vbs[v] = append(vbs[v], &idName{
  1066. Id: buyer_id,
  1067. Name: buyer,
  1068. })
  1069. }
  1070. }
  1071. rows.Close()
  1072. if err := rows.Err(); err != nil {
  1073. logx.Error(err)
  1074. }
  1075. return n.MakeProjectInfo(nil, values, vbs, businessType)
  1076. }
  1077. func (n *network) Introduce_Agency(values []string, businessType []string) map[string]*projectInfo {
  1078. if len(values) == 0 {
  1079. return map[string]*projectInfo{}
  1080. }
  1081. wh, args := WhArgs(values)
  1082. q := `select DISTINCT agency_id,buyer_id,buyer from information.transaction_info_all where agency_id in (` + wh + `) and buyer_id<>''`
  1083. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1084. if err != nil {
  1085. logx.Error(err)
  1086. return map[string]*projectInfo{}
  1087. }
  1088. vbs := map[string][]*idName{}
  1089. buyers := []string{}
  1090. repeat := map[string]bool{}
  1091. for rows.Next() {
  1092. var (
  1093. agency_id string
  1094. buyer_id string
  1095. buyer string
  1096. )
  1097. if err := rows.Scan(&agency_id, &buyer_id, &buyer); err != nil {
  1098. logx.Error(err)
  1099. continue
  1100. }
  1101. if buyer_id == "" || buyer == "" {
  1102. continue
  1103. }
  1104. vbs[agency_id] = append(vbs[agency_id], &idName{
  1105. Id: buyer_id,
  1106. Name: buyer,
  1107. })
  1108. if !repeat[buyer_id] {
  1109. repeat[buyer_id] = true
  1110. buyers = append(buyers, buyer_id)
  1111. }
  1112. }
  1113. rows.Close()
  1114. if err := rows.Err(); err != nil {
  1115. logx.Error(err)
  1116. }
  1117. return n.MakeProjectInfo(values, nil, vbs, businessType)
  1118. }
  1119. func (n *network) Introduce_Middleman_Buyer(values []string, businessType []string) map[string]*projectInfo {
  1120. return n.BuyerProjectInfo(values, businessType)
  1121. }
  1122. func (n *network) Introduce_Middleman_Project(values []string, businessType []string) map[string]*projectInfo {
  1123. result := map[string]*projectInfo{}
  1124. if len(values) == 0 {
  1125. return result
  1126. }
  1127. wh, args := WhArgs(values)
  1128. q := `select DISTINCT project_id,project_money from information.transaction_info_all where project_id in (` + wh + `)`
  1129. if len(businessType) > 0 {
  1130. newWh, newArgs := WhArgs(businessType)
  1131. q += ` and hasAny(topscopeclass,[` + newWh + `])`
  1132. args = append(args, newArgs...)
  1133. }
  1134. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1135. if err != nil {
  1136. logx.Error(err)
  1137. return nil
  1138. }
  1139. for rows.Next() {
  1140. var (
  1141. project_id string
  1142. project_money decimal.Decimal
  1143. )
  1144. if err := rows.Scan(&project_id, &project_money); err != nil {
  1145. logx.Error(err)
  1146. continue
  1147. }
  1148. pf, _ := project_money.Float64()
  1149. result[project_id] = &projectInfo{
  1150. ProjectAmount: pf,
  1151. }
  1152. }
  1153. rows.Close()
  1154. if err := rows.Err(); err != nil {
  1155. logx.Error(err)
  1156. }
  1157. return result
  1158. }
  1159. func (n *network) TypeStrConvert(itype string) int64 {
  1160. //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  1161. switch itype {
  1162. case "firstparty":
  1163. return 1
  1164. case "supplier":
  1165. return 2
  1166. case "adiffb":
  1167. return 3
  1168. case "middleman":
  1169. return 4
  1170. case "agency":
  1171. return 5
  1172. }
  1173. return 0
  1174. }
  1175. func (n *network) TypeIntConvert(itype int64) string {
  1176. //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  1177. switch itype {
  1178. case 1:
  1179. return "firstparty"
  1180. case 2:
  1181. return "supplier"
  1182. case 3:
  1183. return "adiffb"
  1184. case 4:
  1185. return "middleman"
  1186. case 5:
  1187. return "agency"
  1188. }
  1189. return ""
  1190. }
  1191. func (n *network) GetQyxyId(ids []string) map[string]string {
  1192. m := map[string]string{}
  1193. if len(ids) == 0 {
  1194. return m
  1195. }
  1196. wh, args := WhArgs(ids)
  1197. q := `select id,company_id from information.ent_info where id in (` + wh + `)`
  1198. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1199. if err != nil {
  1200. logx.Error(err)
  1201. } else {
  1202. for rows.Next() {
  1203. var (
  1204. id string
  1205. company_id string
  1206. )
  1207. if err := rows.Scan(&id, &company_id); err != nil {
  1208. logx.Error(err)
  1209. continue
  1210. }
  1211. m[id] = company_id
  1212. }
  1213. rows.Close()
  1214. if err := rows.Err(); err != nil {
  1215. logx.Error(err)
  1216. }
  1217. }
  1218. return m
  1219. }
  1220. func (n *network) BuyerProjectInfo(ids []string, businessType []string) map[string]*projectInfo {
  1221. vm := map[string]*projectInfo{}
  1222. m := map[string]bool{}
  1223. newIds := []string{}
  1224. for _, v := range ids {
  1225. if m[v] {
  1226. continue
  1227. }
  1228. m[v] = true
  1229. newIds = append(newIds, v)
  1230. if len(newIds) == C.BuyerBatch {
  1231. n.BuyerProjectInfoBatch(newIds, businessType, vm)
  1232. newIds = []string{}
  1233. }
  1234. }
  1235. if len(newIds) > 0 {
  1236. n.BuyerProjectInfoBatch(newIds, businessType, vm)
  1237. }
  1238. return vm
  1239. }
  1240. func (n *network) BuyerProjectInfoBatch(ids []string, businessType []string, vm map[string]*projectInfo) {
  1241. if len(ids) == 0 {
  1242. return
  1243. }
  1244. wh, args := WhArgs(ids)
  1245. 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 project_bidstatus>1`
  1246. if len(businessType) > 0 {
  1247. newWh, newArgs := WhArgs(businessType)
  1248. q += ` and hasAny(topscopeclass,[` + newWh + `])`
  1249. args = append(args, newArgs...)
  1250. }
  1251. q += ` group by buyer_id`
  1252. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1253. if err != nil {
  1254. logx.Error(err)
  1255. return
  1256. }
  1257. for rows.Next() {
  1258. var (
  1259. buyer_id string
  1260. project_count uint64
  1261. project_amount decimal.Decimal
  1262. project_id []string
  1263. )
  1264. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  1265. logx.Error(err)
  1266. continue
  1267. }
  1268. pf, _ := project_amount.Float64()
  1269. if vm[buyer_id] == nil {
  1270. vm[buyer_id] = &projectInfo{
  1271. ProjectCount: int64(project_count),
  1272. ExportId: project_id,
  1273. ProjectAmount: pf,
  1274. }
  1275. } else {
  1276. vm[buyer_id].ProjectAmount += pf
  1277. vm[buyer_id].ProjectCount += int64(project_count)
  1278. vm[buyer_id].ExportId = append(vm[buyer_id].ExportId, project_id...)
  1279. }
  1280. }
  1281. rows.Close()
  1282. if err := rows.Err(); err != nil {
  1283. logx.Error(err)
  1284. }
  1285. return
  1286. }
  1287. func (n *network) TjProject(agencyIds, winnerIds []string, vbs map[string][]*idName, businessType []string) map[string]*projectInfo {
  1288. vm := map[string]*projectInfo{}
  1289. if len(agencyIds) == 0 && len(winnerIds) == 0 {
  1290. return vm
  1291. }
  1292. allArgs := []interface{}{}
  1293. 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 `
  1294. if len(agencyIds) > 0 {
  1295. wh, args := WhArgs(agencyIds)
  1296. allArgs = append(allArgs, args...)
  1297. q += `agency_id in (` + wh + `) and buyer_id<>''`
  1298. } else if len(winnerIds) > 0 {
  1299. wh, args := WhArgs(winnerIds)
  1300. allArgs = append(allArgs, args...)
  1301. q += `hasAny(winner_id,[` + wh + `]) and buyer_id<>''`
  1302. }
  1303. q += `) and project_bidstatus>1`
  1304. if len(businessType) > 0 {
  1305. newWh, newArgs := WhArgs(businessType)
  1306. q += ` and hasAny(topscopeclass,[` + newWh + `])`
  1307. allArgs = append(allArgs, newArgs...)
  1308. }
  1309. q += ` group by buyer_id`
  1310. logx.Info("全部人脉项目sql", q, allArgs)
  1311. rows, err := ClickhouseConn.Query(context.Background(), q, allArgs...)
  1312. if err != nil {
  1313. logx.Error(err)
  1314. return vm
  1315. }
  1316. for rows.Next() {
  1317. var (
  1318. buyer_id string
  1319. project_count uint64
  1320. project_amount decimal.Decimal
  1321. project_id []string
  1322. )
  1323. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  1324. logx.Error(err)
  1325. continue
  1326. }
  1327. pf, _ := project_amount.Float64()
  1328. if vm[buyer_id] == nil {
  1329. vm[buyer_id] = &projectInfo{
  1330. ProjectCount: int64(project_count),
  1331. ExportId: project_id,
  1332. ProjectAmount: pf,
  1333. }
  1334. } else {
  1335. vm[buyer_id].ProjectAmount += pf
  1336. vm[buyer_id].ProjectCount += int64(project_count)
  1337. vm[buyer_id].ExportId = append(vm[buyer_id].ExportId, project_id...)
  1338. }
  1339. }
  1340. rows.Close()
  1341. if err := rows.Err(); err != nil {
  1342. logx.Error(err)
  1343. }
  1344. return vm
  1345. }
  1346. func (n *network) MakeProjectInfo(agencyIds, winnerIds []string, vbs map[string][]*idName, businessType []string) map[string]*projectInfo {
  1347. pis := n.TjProject(agencyIds, winnerIds, vbs, businessType)
  1348. vm := map[string]*projectInfo{}
  1349. for k, v := range vbs {
  1350. pi := &projectInfo{
  1351. BuyerCount: int64(len(v)),
  1352. IdNames: v,
  1353. }
  1354. re := map[string]bool{}
  1355. for _, vv := range v {
  1356. if pis[vv.Id] != nil {
  1357. pi.ProjectCount += pis[vv.Id].ProjectCount
  1358. pi.ProjectAmount += pis[vv.Id].ProjectAmount
  1359. for _, vvv := range pis[vv.Id].ExportId {
  1360. if re[vvv] {
  1361. continue
  1362. }
  1363. pi.ExportId = append(pi.ExportId, vvv)
  1364. re[vvv] = true
  1365. }
  1366. }
  1367. }
  1368. vm[k] = pi
  1369. }
  1370. return vm
  1371. }
  1372. func (n *network) AllIntroduceOwner(sqlAppend1, sqlAppend2 string, args []interface{}, isTjProject bool, businessType []string) *introduceOwnerProject {
  1373. 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
  1374. 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
  1375. listTemp := CrmMysql.SelectBySql(q, args...)
  1376. firstparty_array, supplier_array, adiffb_array, agency_array, middleman_buyer_array, middleman_project_array := []string{}, []string{}, []string{}, []string{}, []string{}, []string{}
  1377. myNetworks := []*myNetwork{}
  1378. for _, v := range *listTemp {
  1379. myNetwork := &myNetwork{
  1380. Id: Int64All(v["id"]),
  1381. Company_id: ObjToString(v["company_id"]),
  1382. Company_name: ObjToString(v["company_name"]),
  1383. Qyxy_id: ObjToString(v["qyxy_id"]),
  1384. Itype: Int64All(v["itype"]),
  1385. Person: ObjToString(v["person"]),
  1386. Phone: ObjToString(v["phone"]),
  1387. Buyer_count: Int64All(v["buyer_count"]),
  1388. Project_count: Int64All(v["project_count"]),
  1389. Relate_buyer_id: ObjToString(v["relate_buyer_id"]),
  1390. Relate_buyer_name: ObjToString(v["relate_buyer_name"]),
  1391. Relate_project_id: ObjToString(v["relate_project_id"]),
  1392. Create_time: ObjToString(v["create_time"]),
  1393. }
  1394. switch myNetwork.Itype {
  1395. case 1:
  1396. firstparty_array = append(firstparty_array, myNetwork.Company_id)
  1397. case 2:
  1398. supplier_array = append(supplier_array, myNetwork.Company_id)
  1399. case 3:
  1400. adiffb_array = append(adiffb_array, myNetwork.Company_id)
  1401. case 4:
  1402. if myNetwork.Relate_buyer_id != "" {
  1403. middleman_buyer_array = append(middleman_buyer_array, strings.Split(myNetwork.Relate_buyer_id, ",")...)
  1404. }
  1405. if myNetwork.Relate_project_id != "" {
  1406. middleman_project_array = append(middleman_project_array, strings.Split(myNetwork.Relate_project_id, ",")...)
  1407. }
  1408. case 5:
  1409. agency_array = append(agency_array, myNetwork.Company_id)
  1410. }
  1411. myNetworks = append(myNetworks, myNetwork)
  1412. }
  1413. iop := &introduceOwnerProject{
  1414. Networks: myNetworks,
  1415. FirstpartyNetwork: map[string][]*firstpartyNetwork{},
  1416. Firstparty: map[string]*projectInfo{},
  1417. Supplier: map[string]*projectInfo{},
  1418. Adiffb: map[string]*projectInfo{},
  1419. Agency: map[string]*projectInfo{},
  1420. Middleman: map[string]*projectInfo{},
  1421. }
  1422. if isTjProject {
  1423. iop.FirstpartyNetwork = n.FirstpartyNetwork("", firstparty_array)
  1424. iop.Firstparty = n.Introduce_Firstparty(iop.FirstpartyNetwork, businessType)
  1425. iop.Supplier = n.Introduce_Supplier(supplier_array, businessType)
  1426. iop.Adiffb = n.Introduce_Supplier(adiffb_array, businessType)
  1427. iop.Agency = n.Introduce_Agency(agency_array, businessType)
  1428. iop.Middleman = n.Introduce_Middleman_Buyer(middleman_buyer_array, businessType)
  1429. }
  1430. return iop
  1431. }
  1432. func (n *network) DeleteCache(positionId int64) {
  1433. redis.Del("newother", fmt.Sprintf(NetworkManageAllProjectKey, positionId))
  1434. redis.DelByCodePattern("newother", fmt.Sprintf(NetworkManageList, positionId, "*"))
  1435. redis.DelByCodePattern("newother", fmt.Sprintf(NetworkManageCoopHistory, positionId, "*"))
  1436. redis.DelByCodePattern("newother", fmt.Sprintf(NetworkManageOwnerlList, positionId, "*"))
  1437. redis.DelByCodePattern("newother", fmt.Sprintf(NetworkManageProjectList, positionId, "*"))
  1438. }