network.go 44 KB

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