network.go 43 KB

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