network.go 44 KB

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