network.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "log"
  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/sort"
  16. . "bp.jydev.jianyu360.cn/CRM/application/api/common"
  17. "bp.jydev.jianyu360.cn/CRM/application/api/internal/types"
  18. "github.com/shopspring/decimal"
  19. "github.com/zeromicro/go-zero/core/logx"
  20. )
  21. var Network = &network{}
  22. type network struct {
  23. }
  24. type networkTree struct {
  25. Count int64 `json:"count"`
  26. Name string `json:"name"`
  27. Children []*networkTreeChild `json:"children"`
  28. }
  29. type networkTreeChild struct {
  30. Count int64 `json:"count"`
  31. Id int64 `json:"id"`
  32. CompanyId string `json:"companyId"`
  33. CompanyName string `json:"companyName"`
  34. Type string `json:"type"`
  35. CreateTime string `json:"createTime"`
  36. }
  37. func (s *networkTree) Len() int {
  38. return len(s.Children)
  39. }
  40. func (s *networkTree) Less(i, j int) bool {
  41. if s.Name == "甲方" {
  42. return s.Children[i].CompanyName < s.Children[j].CompanyName
  43. }
  44. it, err1 := time.ParseInLocation(Date_Full_Layout, s.Children[i].CreateTime, time.Local)
  45. if err1 != nil {
  46. return true
  47. }
  48. jt, err2 := time.ParseInLocation(Date_Full_Layout, s.Children[j].CreateTime, time.Local)
  49. if err2 != nil {
  50. return true
  51. }
  52. return it.Unix() < jt.Unix()
  53. }
  54. func (s *networkTree) Swap(i, j int) {
  55. s.Children[i], s.Children[j] = s.Children[j], s.Children[i]
  56. }
  57. type projectInfo struct {
  58. BuyerCount int64
  59. ProjectCount int64
  60. ProjectAmount float64
  61. MonitorCount int64
  62. ExportId []string
  63. }
  64. type firstpartyNetwork struct {
  65. CompanyId string
  66. CompanyName string
  67. Name string
  68. }
  69. type idName struct {
  70. Id string
  71. Name string
  72. }
  73. type introduceOwnerProject struct {
  74. Networks []*myNetwork
  75. FirstpartyNetwork map[string][]*firstpartyNetwork
  76. Firstparty map[string]*projectInfo
  77. Supplier map[string]*projectInfo
  78. Adiffb map[string]*projectInfo
  79. Agency map[string]*projectInfo
  80. Middleman map[string]*projectInfo
  81. }
  82. type myNetwork struct {
  83. Id int64
  84. Company_id string
  85. Company_name string
  86. Qyxy_id string
  87. Itype int64
  88. Person string
  89. Phone string
  90. Buyer_count int64
  91. Project_count int64
  92. Relate_buyer_id string
  93. Relate_project_id string
  94. Create_time string
  95. }
  96. //人脉库-添加/修改人脉
  97. func (n *network) AddOrUpdate(in *types.AddOrUpdateReq) *types.Reply {
  98. reply := &types.Reply{Data: map[string]interface{}{
  99. "status": 0,
  100. }}
  101. if in.Type != "middleman" && in.Company_id == "" {
  102. return reply
  103. }
  104. if in.Company_name == "" {
  105. in.Company_name = "未填写"
  106. }
  107. nowFormat := NowFormat(Date_Full_Layout)
  108. var saveIntroduce = func(tx *sql.Tx, cid int64, isUpdate bool) bool {
  109. if in.Type != "middleman" {
  110. return true
  111. } else if in.Introduce_owner_id == "" || in.Introduce_project_id == "" {
  112. return false
  113. }
  114. values := []interface{}{}
  115. ioi := strings.Split(in.Introduce_owner_id, ",")
  116. ioqi := strings.Split(in.Introduce_owner_qyxy_id, ",")
  117. ion := strings.Split(in.Introduce_owner_name, ",")
  118. if len(ioi) != len(ion) || len(ioi) != len(ioqi) {
  119. return false
  120. }
  121. for k, v := range ioi {
  122. values = append(values, in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, cid, ioqi[k], v, ion[k], 1, nowFormat)
  123. }
  124. ipi := strings.Split(in.Introduce_project_id, ",")
  125. ipn := strings.Split(in.Introduce_project_name, ",")
  126. if len(ipi) != len(ipn) {
  127. return false
  128. }
  129. for k, v := range ipi {
  130. values = append(values, in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, cid, nil, v, ipn[k], 2, nowFormat)
  131. }
  132. var r2 int64
  133. if isUpdate {
  134. r2 = CrmMysql.UpdateOrDeleteBySqlByTx(tx, `delete from crm.connection_introduce where connection_id=? and position_id=?`, in.Id, in.PositionId)
  135. }
  136. 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)
  137. return r2 >= 0 && r3 > 0
  138. }
  139. itype := n.TypeStrConvert(in.Type)
  140. if in.Id > 0 {
  141. if CrmMysql.ExecTx("更新人脉", func(tx *sql.Tx) bool {
  142. if in.Company_id != "" {
  143. 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)
  144. if count == -1 {
  145. return false
  146. } else if count > 0 {
  147. reply.Data = map[string]interface{}{
  148. "status": -1,
  149. }
  150. return false
  151. }
  152. }
  153. r1 := CrmMysql.UpdateOrDeleteBySqlByTx(tx, `update crm.connection set company_name=?,company_id=?,contact_person=?,contact_phone=?,update_time=? where id=? and position_id=?`, in.Company_name, in.Company_id, in.Contact_person, in.Contact_phone, nowFormat, in.Id, in.PositionId)
  154. return r1 >= 0 && saveIntroduce(tx, in.Id, true)
  155. }) {
  156. reply.Data = map[string]interface{}{
  157. "status": 1,
  158. }
  159. }
  160. } else {
  161. if in.Company_id != "" {
  162. 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)
  163. if count == -1 {
  164. return reply
  165. } else if count > 0 {
  166. reply.Data = map[string]interface{}{
  167. "status": -1,
  168. }
  169. return reply
  170. }
  171. }
  172. var r1 int64
  173. if CrmMysql.ExecTx("新增人脉", func(tx *sql.Tx) bool {
  174. _, 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})
  175. return r1 > 0 && saveIntroduce(tx, r1, false)
  176. }) {
  177. reply.Data = map[string]interface{}{
  178. "status": 1,
  179. "id": r1,
  180. }
  181. }
  182. }
  183. return reply
  184. }
  185. //人脉库-业主名称联想
  186. func (n *network) Associate(in *types.AssociateReq) (reply *types.Reply) {
  187. //类型;firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 middleman_owner:中间人-业主 middleman_project:中间人-项目 agency:招标代理机构
  188. res := []map[string]string{}
  189. reply = &types.Reply{Data: res}
  190. in.Name = strings.TrimSpace(in.Name)
  191. if in.Name == "" {
  192. return
  193. }
  194. pageSize := 10
  195. if in.Type == "adiffb" {
  196. probusfors := NetworkCom.GetMyProbusfor(in.EntAccountId)
  197. if len(probusfors) > 0 {
  198. args := []interface{}{in.EntName}
  199. wh, newArgs := NetworkCom.WhArgs(probusfors)
  200. args = append(args, newArgs...)
  201. q := `select DISTINCT winner_id,winner from information.transaction_info where buyer_id in (select buyer_id from information.transaction_info where has(winner, ?) and buyer_id<>'') and hasAny(property_form,[` + wh + `])=0 ORDER BY project_id`
  202. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  203. if err != nil {
  204. logx.Error(err)
  205. } else {
  206. repeat := map[string]bool{}
  207. ids := []string{}
  208. for rows.Next() {
  209. var (
  210. winner_id []string
  211. winner []string
  212. )
  213. if err := rows.Scan(&winner_id, &winner); err != nil {
  214. logx.Error(err)
  215. continue
  216. }
  217. for k, v := range winner {
  218. if repeat[v] || !strings.Contains(v, in.Name) {
  219. continue
  220. }
  221. repeat[v] = true
  222. if k >= len(winner_id) {
  223. continue
  224. }
  225. ids = append(ids, winner_id[k])
  226. res = append(res, map[string]string{
  227. "company_name": v,
  228. "company_id": winner_id[k],
  229. })
  230. if len(res) == pageSize {
  231. break
  232. }
  233. }
  234. }
  235. rows.Close()
  236. if err := rows.Err(); err != nil {
  237. logx.Error(err)
  238. }
  239. qyxyId := n.GetQyxyId(ids)
  240. for _, v := range res {
  241. v["qyxy_id"] = qyxyId[v["company_id"]]
  242. }
  243. }
  244. }
  245. } else {
  246. must := []string{fmt.Sprintf(`{"multi_match":{"query":"%s","type":"phrase","fields":["company_name"]}}`, in.Name)}
  247. switch in.Type {
  248. case "firstparty":
  249. must = append(must, fmt.Sprintf(`{"terms":{"company_label":["%s"]}}`, strings.Join(NetworkCom.GetEntTagSeat(2), `","`)))
  250. must = append(must, `{"terms":{"company_unit_type":[1,2]}}`)
  251. case "supplier":
  252. must = append(must, fmt.Sprintf(`{"terms":{"company_label":["%s"]}}`, strings.Join(NetworkCom.GetEntTagSeat(2), `","`)))
  253. must = append(must, `{"term":{"company_unit_type":3}}`)
  254. case "middleman_owner":
  255. must = append(must, `{"terms":{"company_unit_type":[1,2]}}`)
  256. case "agency":
  257. must = append(must, `{"term":{"company_unit_type":4}}`)
  258. }
  259. q := fmt.Sprintf(`{"query":{"bool":{"must":[%s]}},"size":%d,"_source":["id","company_id","company_name"]}`, strings.Join(must, ","), pageSize)
  260. logx.Info("人脉库-业主名称联想", q)
  261. datas := VarEs.Get("ent_info", "ent_info", q)
  262. if datas != nil {
  263. for _, v := range *datas {
  264. res = append(res, map[string]string{
  265. "company_name": ObjToString(v["company_name"]),
  266. "company_id": ObjToString(v["id"]),
  267. "qyxy_id": ObjToString(v["company_id"]),
  268. })
  269. }
  270. }
  271. }
  272. reply.Data = res
  273. return
  274. }
  275. //人脉库-全部人脉项目
  276. func (n *network) AllProject(in *types.AllprojectReq) (reply *types.Reply) {
  277. reply = &types.Reply{}
  278. probusfors := NetworkCom.GetMyProbusfor(in.EntAccountId)
  279. var count int64
  280. var list []*networkTree
  281. firstpartyChild := map[string][]*firstpartyNetwork{}
  282. if in.Id != "" {
  283. if in.Type == "firstparty" {
  284. fpn := n.FirstpartyNetwork(in.Name, []string{in.Id})
  285. if fpn[in.Id] != nil {
  286. firstparty := n.Introduce_Firstparty(fpn, map[string]bool{}, probusfors)
  287. nameIndex := map[string]int{}
  288. for _, v := range fpn[in.Id] {
  289. if _, ok := nameIndex[v.Name]; !ok {
  290. nameIndex[v.Name] = len(list)
  291. list = append(list, &networkTree{
  292. Name: v.Name,
  293. })
  294. }
  295. ntc := &networkTreeChild{
  296. CompanyName: v.CompanyName,
  297. CompanyId: v.CompanyId,
  298. Type: "firstparty",
  299. }
  300. if firstparty[v.CompanyId] != nil {
  301. ntc.Count = firstparty[v.CompanyId].ProjectCount
  302. }
  303. count++
  304. list[nameIndex[v.Name]].Count += ntc.Count
  305. list[nameIndex[v.Name]].Children = append(list[nameIndex[v.Name]].Children, ntc)
  306. }
  307. }
  308. }
  309. } else {
  310. args := []interface{}{in.PositionId, in.PositionId, in.PositionId}
  311. sqlAppend1 := ""
  312. if in.Name != "" {
  313. sqlAppend1 += ` and a.company_name like ?`
  314. args = append(args, "%"+in.Name+"%")
  315. }
  316. aio := n.AllIntroduceOwner(sqlAppend1, "", args, true, probusfors, map[string]bool{})
  317. firstpartyChild = aio.FirstpartyNetwork
  318. list = []*networkTree{
  319. &networkTree{
  320. Name: "甲方",
  321. Children: []*networkTreeChild{},
  322. },
  323. &networkTree{
  324. Name: "供应商",
  325. Children: []*networkTreeChild{},
  326. },
  327. &networkTree{
  328. Name: "同甲异业渠道",
  329. Children: []*networkTreeChild{},
  330. },
  331. &networkTree{
  332. Name: "中间人",
  333. Children: []*networkTreeChild{},
  334. },
  335. &networkTree{
  336. Name: "招标代理",
  337. Children: []*networkTreeChild{},
  338. },
  339. }
  340. //
  341. for _, v := range aio.Networks {
  342. if v.Itype <= 0 || v.Itype > int64(len(list)) {
  343. return
  344. }
  345. ntc := &networkTreeChild{
  346. Id: v.Id,
  347. CompanyName: v.Company_name,
  348. CompanyId: v.Company_id,
  349. Type: n.TypeIntConvert(v.Itype),
  350. CreateTime: v.Create_time,
  351. }
  352. switch v.Itype {
  353. case 1:
  354. if aio.Firstparty[v.Company_id] != nil {
  355. ntc.Count = aio.Firstparty[v.Company_id].ProjectCount
  356. }
  357. case 2:
  358. if aio.Supplier[v.Company_id] != nil {
  359. ntc.Count = aio.Supplier[v.Company_id].ProjectCount
  360. }
  361. case 3:
  362. if aio.Adiffb[v.Company_id] != nil {
  363. ntc.Count = aio.Adiffb[v.Company_id].ProjectCount
  364. }
  365. case 4:
  366. if v.Relate_project_id != "" {
  367. for _, v := range strings.Split(v.Relate_project_id, ",") {
  368. if aio.Middleman[v] != nil {
  369. ntc.Count++
  370. }
  371. }
  372. }
  373. case 5:
  374. if aio.Agency[v.Company_id] != nil {
  375. ntc.Count = aio.Agency[v.Company_id].ProjectCount
  376. }
  377. }
  378. count += ntc.Count
  379. list[v.Itype-1].Count += ntc.Count
  380. list[v.Itype-1].Children = append(list[v.Itype-1].Children, ntc)
  381. }
  382. }
  383. convList := []map[string]interface{}{}
  384. for _, v := range list {
  385. pm := map[string]interface{}{
  386. "CODE": v.Name,
  387. "NAME": v.Name,
  388. "SZ_PID0": v.Name,
  389. "SZ_LEVEL": 0,
  390. "DATACOUNT": 0,
  391. }
  392. if len(v.Children) > 0 {
  393. pm["SZ_LEAF"] = 0
  394. } else {
  395. pm["SZ_LEAF"] = 1
  396. }
  397. sort.Sort(v)
  398. id := ""
  399. pType := ""
  400. for _, vv := range v.Children {
  401. tempId := vv.CompanyId
  402. if vv.Type == "middleman" {
  403. tempId = fmt.Sprint(vv.Id)
  404. }
  405. myChildIds, myChildTypes := "", ""
  406. if in.Id == "" && v.Name == "甲方" {
  407. for _, vvv := range firstpartyChild[vv.CompanyId] {
  408. if id != "" {
  409. id += ","
  410. }
  411. id += vvv.CompanyId
  412. if pType != "" {
  413. pType += ","
  414. }
  415. pType += vv.Type
  416. if myChildIds != "" {
  417. myChildIds += ","
  418. }
  419. myChildIds += vvv.CompanyId
  420. if myChildTypes != "" {
  421. myChildTypes += ","
  422. }
  423. myChildTypes += vv.Type
  424. }
  425. } else {
  426. if id != "" {
  427. id += ","
  428. }
  429. id += tempId
  430. if pType != "" {
  431. pType += ","
  432. }
  433. pType += vv.Type
  434. myChildIds = tempId
  435. myChildTypes = vv.Type
  436. }
  437. cm := map[string]interface{}{
  438. "NAME": vv.CompanyName,
  439. "ID": myChildIds,
  440. "SZ_PID0": v.Name,
  441. "SZ_PID1": v.Name + ":" + tempId,
  442. "CODE": v.Name + ":" + tempId,
  443. "PCODE": v.Name,
  444. "DATACOUNT": vv.Count,
  445. "SZ_LEVEL": 1,
  446. "SZ_LEAF": 1,
  447. "TYPE": myChildTypes,
  448. "MYID": tempId,
  449. }
  450. convList = append(convList, cm)
  451. }
  452. pm["ID"] = id
  453. pm["TYPE"] = pType
  454. convList = append(convList, pm)
  455. }
  456. reply = &types.Reply{
  457. Data: map[string]interface{}{
  458. "count": count,
  459. "list": convList,
  460. },
  461. }
  462. return reply
  463. }
  464. //人脉库-列表
  465. func (n *network) List(in *types.NetWorkListReq) *types.Reply {
  466. if in.Page_size <= 0 {
  467. in.Page_size = 10
  468. }
  469. if in.Current_page <= 0 {
  470. in.Current_page = 1
  471. }
  472. sqlAppendArgs := []interface{}{}
  473. sqlAppend1 := ""
  474. if in.Type != "" {
  475. sqlAppend1 += ` and a.itype=?`
  476. sqlAppendArgs = append(sqlAppendArgs, n.TypeStrConvert(in.Type))
  477. }
  478. if in.Starttime != "" {
  479. sqlAppend1 += ` and a.create_time>=?`
  480. sqlAppendArgs = append(sqlAppendArgs, in.Starttime+" 00:00:00")
  481. }
  482. if in.Endtime != "" {
  483. sqlAppend1 += ` and a.create_time<=?`
  484. if in.Starttime == in.Endtime {
  485. in.Endtime += " 23:59:59"
  486. } else {
  487. in.Endtime += " 00:00:00"
  488. }
  489. sqlAppendArgs = append(sqlAppendArgs, in.Endtime)
  490. }
  491. if in.Name != "" {
  492. sqlAppend1 += ` and a.company_name like ?`
  493. sqlAppendArgs = append(sqlAppendArgs, "%"+in.Name+"%")
  494. }
  495. var count int64
  496. start := (in.Current_page - 1) * in.Page_size
  497. end := start + in.Page_size
  498. dbPaging := in.Buyercount_start == 0 && in.Buyercount_end == 0 && in.Monitor == 0 && in.Monitorcount_start == 0 && in.Monitorcount_end == 0 && in.Project_matchme == 0 && in.Order_amount == 0
  499. args := []interface{}{in.PositionId, in.PositionId, in.PositionId}
  500. args = append(args, sqlAppendArgs...)
  501. sqlAppend2 := ""
  502. if dbPaging {
  503. newArgs := []interface{}{in.PositionId}
  504. newArgs = append(newArgs, sqlAppendArgs...)
  505. count = CrmMysql.CountBySql(`select count(1) as count from crm.connection a where a.position_id=?`+sqlAppend1, newArgs...)
  506. sqlAppend2 = ` limit ?,?`
  507. args = append(args, start, end)
  508. }
  509. //
  510. firstparty_count, supplier_count, adiffb_count, middleman_count, agency_count := 0, 0, 0, 0, 0
  511. list := []*map[string]interface{}{}
  512. isTjProject := true
  513. probusfors := []string{}
  514. if in.Project_matchme == 1 {
  515. probusfors = NetworkCom.GetMyProbusfor(in.EntAccountId)
  516. if len(probusfors) == 0 {
  517. isTjProject = false
  518. }
  519. }
  520. if isTjProject {
  521. entMonitor := NetworkCom.EntMonitor(in.UserId)
  522. log.Println(entMonitor)
  523. aio := n.AllIntroduceOwner(sqlAppend1, sqlAppend2, args, isTjProject, probusfors, entMonitor)
  524. for _, v := range aio.Networks {
  525. itype := ""
  526. buyer_count, project_count, expect_amount, monitor_count := int64(0), int64(0), float64(0), int64(0)
  527. export_id := []string{}
  528. jump_type, jump_id := "", ""
  529. switch v.Itype {
  530. case 1:
  531. itype = "甲方"
  532. jump_type = "firstparty"
  533. for _, vv := range aio.FirstpartyNetwork[v.Company_id] {
  534. if jump_id != "" {
  535. jump_id += ","
  536. }
  537. jump_id += vv.CompanyId
  538. }
  539. if aio.Firstparty[v.Company_id] != nil {
  540. buyer_count = aio.Firstparty[v.Company_id].BuyerCount
  541. project_count = aio.Firstparty[v.Company_id].ProjectCount
  542. expect_amount = aio.Firstparty[v.Company_id].ProjectAmount
  543. monitor_count = aio.Firstparty[v.Company_id].MonitorCount
  544. export_id = aio.Firstparty[v.Company_id].ExportId
  545. }
  546. case 2:
  547. itype = "供应商"
  548. jump_type = "supplier"
  549. jump_id = v.Company_id
  550. if aio.Supplier[v.Company_id] != nil {
  551. buyer_count = aio.Supplier[v.Company_id].BuyerCount
  552. project_count = aio.Supplier[v.Company_id].ProjectCount
  553. expect_amount = aio.Supplier[v.Company_id].ProjectAmount
  554. monitor_count = aio.Supplier[v.Company_id].MonitorCount
  555. export_id = aio.Supplier[v.Company_id].ExportId
  556. }
  557. case 3:
  558. itype = "同甲异业渠道"
  559. jump_type = "adiffb"
  560. jump_id = v.Company_id
  561. if aio.Adiffb[v.Company_id] != nil {
  562. buyer_count = aio.Adiffb[v.Company_id].BuyerCount
  563. project_count = aio.Adiffb[v.Company_id].ProjectCount
  564. expect_amount = aio.Adiffb[v.Company_id].ProjectAmount
  565. monitor_count = aio.Adiffb[v.Company_id].MonitorCount
  566. export_id = aio.Adiffb[v.Company_id].ExportId
  567. }
  568. case 4:
  569. itype = "中间人"
  570. jump_type = "middleman"
  571. jump_id = fmt.Sprint(v.Id)
  572. buyer_count = v.Buyer_count
  573. if v.Relate_buyer_id != "" {
  574. for _, v := range strings.Split(v.Relate_buyer_id, ",") {
  575. if v == "" {
  576. continue
  577. }
  578. if entMonitor[v] {
  579. monitor_count++
  580. }
  581. }
  582. }
  583. if v.Relate_project_id != "" {
  584. export_id = strings.Split(v.Relate_project_id, ",")
  585. for _, v := range export_id {
  586. if aio.Middleman[v] != nil {
  587. project_count++
  588. expect_amount += aio.Middleman[v].ProjectAmount
  589. }
  590. }
  591. }
  592. case 5:
  593. itype = "招标代理机构"
  594. jump_type = "agency"
  595. jump_id = v.Company_id
  596. if aio.Agency[v.Company_id] != nil {
  597. buyer_count = aio.Agency[v.Company_id].BuyerCount
  598. project_count = aio.Agency[v.Company_id].ProjectCount
  599. expect_amount = aio.Agency[v.Company_id].ProjectAmount
  600. monitor_count = aio.Agency[v.Company_id].MonitorCount
  601. export_id = aio.Agency[v.Company_id].ExportId
  602. }
  603. }
  604. if buyer_count < in.Buyercount_start {
  605. continue
  606. } else if in.Buyercount_end > 0 && buyer_count > in.Buyercount_end {
  607. continue
  608. } else if in.Monitor == 1 && monitor_count <= 0 {
  609. continue
  610. } else if in.Monitor == -1 && monitor_count > 0 {
  611. continue
  612. } else if monitor_count < in.Monitorcount_start {
  613. continue
  614. } else if in.Monitorcount_end > 0 && monitor_count > in.Monitorcount_end {
  615. continue
  616. } else if in.Project_matchme == 1 && project_count == 0 {
  617. continue
  618. }
  619. switch v.Itype {
  620. case 1:
  621. firstparty_count++
  622. case 2:
  623. supplier_count++
  624. case 3:
  625. adiffb_count++
  626. case 4:
  627. middleman_count++
  628. case 5:
  629. agency_count++
  630. }
  631. export_url := ""
  632. if len(export_id) > 0 {
  633. exportIdRepeat := map[string]bool{}
  634. exportId := ""
  635. for _, v := range export_id {
  636. if exportIdRepeat[v] {
  637. continue
  638. }
  639. exportIdRepeat[v] = true
  640. if exportId != "" {
  641. exportId += ","
  642. }
  643. exportId += v
  644. }
  645. export_url = "/subscribepay/network/projectExport?export_id=" + encrypt.SE.EncodeStringByCheck(exportId)
  646. }
  647. url := ""
  648. if v.Qyxy_id != "" && (jump_type == "supplier" || jump_type == "adiffb") {
  649. url = "/swordfish/page_big_pc/ent_portrait/" + encrypt.EncodeArticleId2ByCheck(v.Qyxy_id)
  650. } else if jump_type == "firstparty" && v.Company_name != "" {
  651. url = "/swordfish/page_big_pc/unit_portrayal/" + v.Company_name
  652. }
  653. list = append(list, &map[string]interface{}{
  654. "company_id": v.Company_id,
  655. "company_name": v.Company_name,
  656. "type": itype,
  657. "jump_type": jump_type,
  658. "jump_id": jump_id,
  659. "person": v.Person,
  660. "phone": v.Phone,
  661. "buyer_count": buyer_count,
  662. "monitor_count": monitor_count,
  663. "expect_amount": RetainDecimal(expect_amount/10000, 2),
  664. "project_count": project_count,
  665. "create_time": v.Create_time,
  666. "export_url": export_url,
  667. "url": url,
  668. "id": v.Id,
  669. })
  670. }
  671. }
  672. csList := &ComSortList{
  673. SortKeys: []*ComSortKey{
  674. &ComSortKey{
  675. Keys: []string{"expect_amount"},
  676. Order: 1,
  677. Type: "float",
  678. },
  679. },
  680. List: list,
  681. }
  682. if in.Order_amount == -1 {
  683. csList.SortKeys[0].Order = -1
  684. }
  685. if in.Order_amount != 0 {
  686. sort.Sort(csList)
  687. }
  688. finalList := []*map[string]interface{}{}
  689. if !dbPaging {
  690. count = int64(len(csList.List))
  691. if count > 0 && start < count {
  692. if end > count {
  693. end = count
  694. }
  695. finalList = csList.List[start:end]
  696. }
  697. } else {
  698. finalList = csList.List
  699. }
  700. total_page := int64(math.Ceil(float64(count) / float64(in.Page_size)))
  701. return &types.Reply{
  702. Data: map[string]interface{}{
  703. "count": count,
  704. "total_page": total_page,
  705. "firstparty_count": firstparty_count,
  706. "supplier_count": supplier_count,
  707. "adiffb_count": adiffb_count,
  708. "middleman_count": middleman_count,
  709. "agency_count": agency_count,
  710. "list": finalList,
  711. },
  712. }
  713. }
  714. //
  715. func (n *network) FirstpartyNetwork(name string, values []string) map[string][]*firstpartyNetwork {
  716. result := map[string][]*firstpartyNetwork{}
  717. wh, args := NetworkCom.WhArgs(values)
  718. 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
  719. inner join information.ent_code b on (a.a_id in (` + wh + `) and b.pcode in ('0100','0200') and a.code=b.code)`
  720. if name != "" {
  721. q += ` where c.company_name like ?`
  722. args = append(args, "%"+name+"%")
  723. }
  724. q += ` order by b.name,a.a_id,a.b_name`
  725. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  726. if err != nil {
  727. logx.Error(err)
  728. return result
  729. }
  730. for rows.Next() {
  731. var (
  732. a_id string
  733. company_id string
  734. company_name string
  735. name string
  736. )
  737. if err := rows.Scan(&a_id, &company_id, &company_name, &name); err != nil {
  738. logx.Error(err)
  739. continue
  740. }
  741. if company_id == "" || company_name == "" {
  742. continue
  743. }
  744. result[a_id] = append(result[a_id], &firstpartyNetwork{
  745. CompanyId: company_id,
  746. CompanyName: company_name,
  747. Name: name,
  748. })
  749. }
  750. return result
  751. }
  752. //
  753. func (n *network) Introduce_Firstparty(fpn map[string][]*firstpartyNetwork, entMonitor map[string]bool, probusfors []string) map[string]*projectInfo {
  754. values := []string{}
  755. vm := map[string]*projectInfo{}
  756. for _, v := range fpn {
  757. for _, vv := range v {
  758. vm[vv.CompanyId] = &projectInfo{}
  759. values = append(values, vv.CompanyId)
  760. }
  761. }
  762. result := map[string]*projectInfo{}
  763. if len(values) == 0 {
  764. return result
  765. }
  766. wh, args := NetworkCom.WhArgs(values)
  767. q := `select buyer_id,count(project_id) AS project_count,sum(project_money) AS project_amount,groupUniqArray(project_id) from information.transaction_info where buyer_id in (` + wh + `)`
  768. if len(probusfors) > 0 {
  769. newWh, newArgs := NetworkCom.WhArgs(probusfors)
  770. q += ` and hasAny(property_form,[` + newWh + `])`
  771. args = append(args, newArgs...)
  772. }
  773. q += ` group by buyer_id`
  774. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  775. if err != nil {
  776. logx.Error(err)
  777. return nil
  778. }
  779. for rows.Next() {
  780. var (
  781. buyer_id string
  782. project_count uint64
  783. project_amount decimal.Decimal
  784. project_id []string
  785. )
  786. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  787. logx.Error(err)
  788. continue
  789. }
  790. if vm[buyer_id] == nil {
  791. continue
  792. }
  793. vm[buyer_id].ProjectCount += int64(project_count)
  794. pf, _ := project_amount.Float64()
  795. vm[buyer_id].ProjectAmount += pf
  796. vm[buyer_id].ExportId = project_id
  797. }
  798. rows.Close()
  799. if err := rows.Err(); err != nil {
  800. logx.Error(err)
  801. }
  802. for k, v := range fpn {
  803. if result[k] == nil {
  804. result[k] = &projectInfo{}
  805. }
  806. result[k].BuyerCount = int64(len(v))
  807. for _, vv := range v {
  808. if entMonitor[vv.CompanyName] {
  809. result[k].MonitorCount++
  810. }
  811. if vm[vv.CompanyId] == nil {
  812. continue
  813. }
  814. result[k].ProjectCount += vm[vv.CompanyId].ProjectCount
  815. result[k].ProjectAmount += vm[vv.CompanyId].ProjectAmount
  816. result[k].ExportId = append(result[k].ExportId, vm[vv.CompanyId].ExportId...)
  817. }
  818. }
  819. return result
  820. }
  821. //
  822. func (n *network) Introduce_Supplier(values []string, entMonitor map[string]bool, probusfors []string) map[string]*projectInfo {
  823. if len(values) == 0 {
  824. return map[string]*projectInfo{}
  825. }
  826. wh, args := NetworkCom.WhArgs(values)
  827. vbs := map[string][]*idName{}
  828. repeat := map[string]bool{}
  829. whRepeat := map[string]map[string]bool{}
  830. buyers := []string{}
  831. q := `select winner_id,buyer_id,buyer from information.transaction_info where hasAny(winner_id,[` + wh + `])`
  832. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  833. if err != nil {
  834. logx.Error(err)
  835. } else {
  836. for rows.Next() {
  837. var (
  838. winner_id []string
  839. buyer_id string
  840. buyer string
  841. )
  842. if err := rows.Scan(&winner_id, &buyer_id, &buyer); err != nil {
  843. logx.Error(err)
  844. continue
  845. }
  846. if buyer_id == "" || buyer == "" {
  847. continue
  848. }
  849. if !repeat[buyer_id] {
  850. repeat[buyer_id] = true
  851. buyers = append(buyers, buyer_id)
  852. }
  853. for _, v := range winner_id {
  854. if whRepeat[v] != nil && whRepeat[v][buyer_id] {
  855. continue
  856. }
  857. if whRepeat[v] == nil {
  858. whRepeat[v] = map[string]bool{}
  859. }
  860. whRepeat[v][buyer_id] = true
  861. vbs[v] = append(vbs[v], &idName{
  862. Id: buyer_id,
  863. Name: buyer,
  864. })
  865. }
  866. }
  867. rows.Close()
  868. if err := rows.Err(); err != nil {
  869. logx.Error(err)
  870. }
  871. }
  872. return n.MakeProjectInfo(buyers, vbs, probusfors, entMonitor)
  873. }
  874. //
  875. func (n *network) Introduce_Agency(values []string, entMonitor map[string]bool, probusfors []string) map[string]*projectInfo {
  876. if len(values) == 0 {
  877. return map[string]*projectInfo{}
  878. }
  879. wh, args := NetworkCom.WhArgs(values)
  880. q := `select DISTINCT agency_id,buyer_id,buyer from information.transaction_info where agency_id in (` + wh + `)`
  881. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  882. if err != nil {
  883. logx.Error(err)
  884. return nil
  885. }
  886. vbs := map[string][]*idName{}
  887. buyers := []string{}
  888. repeat := map[string]bool{}
  889. for rows.Next() {
  890. var (
  891. agency_id string
  892. buyer_id string
  893. buyer string
  894. )
  895. if err := rows.Scan(&agency_id, &buyer_id, &buyer); err != nil {
  896. logx.Error(err)
  897. continue
  898. }
  899. if buyer_id == "" || buyer == "" {
  900. continue
  901. }
  902. vbs[agency_id] = append(vbs[agency_id], &idName{
  903. Id: buyer_id,
  904. Name: buyer,
  905. })
  906. if !repeat[buyer_id] {
  907. repeat[buyer_id] = true
  908. buyers = append(buyers, buyer_id)
  909. }
  910. }
  911. rows.Close()
  912. if err := rows.Err(); err != nil {
  913. logx.Error(err)
  914. }
  915. return n.MakeProjectInfo(buyers, vbs, probusfors, entMonitor)
  916. }
  917. //
  918. func (n *network) Introduce_Middleman(values []string, entMonitor map[string]bool, probusfors []string) map[string]*projectInfo {
  919. result := map[string]*projectInfo{}
  920. if len(values) == 0 {
  921. return result
  922. }
  923. wh, args := NetworkCom.WhArgs(values)
  924. q := `select DISTINCT project_id,project_money from information.transaction_info where project_id in (` + wh + `)`
  925. if len(probusfors) > 0 {
  926. newWh, newArgs := NetworkCom.WhArgs(probusfors)
  927. q += ` and hasAny(property_form,[` + newWh + `])`
  928. args = append(args, newArgs...)
  929. }
  930. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  931. if err != nil {
  932. logx.Error(err)
  933. return nil
  934. }
  935. for rows.Next() {
  936. var (
  937. project_id string
  938. project_money decimal.Decimal
  939. )
  940. if err := rows.Scan(&project_id, &project_money); err != nil {
  941. logx.Error(err)
  942. continue
  943. }
  944. pf, _ := project_money.Float64()
  945. result[project_id] = &projectInfo{
  946. ProjectAmount: pf,
  947. }
  948. }
  949. rows.Close()
  950. if err := rows.Err(); err != nil {
  951. logx.Error(err)
  952. }
  953. return result
  954. }
  955. //
  956. func (n *network) TypeStrConvert(itype string) int {
  957. //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  958. switch itype {
  959. case "firstparty":
  960. return 1
  961. case "supplier":
  962. return 2
  963. case "adiffb":
  964. return 3
  965. case "middleman":
  966. return 4
  967. case "agency":
  968. return 5
  969. }
  970. return 0
  971. }
  972. //
  973. func (n *network) TypeIntConvert(itype int64) string {
  974. //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  975. switch itype {
  976. case 1:
  977. return "firstparty"
  978. case 2:
  979. return "supplier"
  980. case 3:
  981. return "adiffb"
  982. case 4:
  983. return "middleman"
  984. case 5:
  985. return "agency"
  986. }
  987. return ""
  988. }
  989. //
  990. func (n *network) GetQyxyId(ids []string) map[string]string {
  991. wh, args := NetworkCom.WhArgs(ids)
  992. q := `select id,company_id from information.ent_info where id in (` + wh + `)`
  993. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  994. m := map[string]string{}
  995. if err != nil {
  996. logx.Error(err)
  997. } else {
  998. for rows.Next() {
  999. var (
  1000. id string
  1001. company_id string
  1002. )
  1003. if err := rows.Scan(&id, &company_id); err != nil {
  1004. logx.Error(err)
  1005. continue
  1006. }
  1007. m[id] = company_id
  1008. }
  1009. rows.Close()
  1010. if err := rows.Err(); err != nil {
  1011. logx.Error(err)
  1012. }
  1013. }
  1014. return m
  1015. }
  1016. //
  1017. func (n *network) BuyerProjectInfo(ids []string, probusfors []string) map[string]*projectInfo {
  1018. vm := map[string]*projectInfo{}
  1019. wh, args := NetworkCom.WhArgs(ids)
  1020. q := `select buyer_id,count(DISTINCT project_id) AS project_count,sum(project_money) AS project_amount,groupUniqArray(project_id) from information.transaction_info where buyer_id in (` + wh + `)`
  1021. if len(probusfors) > 0 {
  1022. newWh, newArgs := NetworkCom.WhArgs(probusfors)
  1023. q += ` and hasAny(property_form,[` + newWh + `])`
  1024. args = append(args, newArgs...)
  1025. }
  1026. q += ` group by buyer_id`
  1027. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1028. if err != nil {
  1029. logx.Error(err)
  1030. return nil
  1031. }
  1032. for rows.Next() {
  1033. var (
  1034. buyer_id string
  1035. project_count uint64
  1036. project_amount decimal.Decimal
  1037. project_id []string
  1038. )
  1039. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  1040. logx.Error(err)
  1041. continue
  1042. }
  1043. pf, _ := project_amount.Float64()
  1044. vm[buyer_id] = &projectInfo{
  1045. ProjectCount: int64(project_count),
  1046. ExportId: project_id,
  1047. ProjectAmount: pf,
  1048. }
  1049. }
  1050. rows.Close()
  1051. if err := rows.Err(); err != nil {
  1052. logx.Error(err)
  1053. }
  1054. return vm
  1055. }
  1056. //
  1057. func (n *network) MakeProjectInfo(buyers []string, vbs map[string][]*idName, probusfors []string, entMonitor map[string]bool) map[string]*projectInfo {
  1058. pis := n.BuyerProjectInfo(buyers, probusfors)
  1059. vm := map[string]*projectInfo{}
  1060. for k, v := range vbs {
  1061. pi := &projectInfo{
  1062. BuyerCount: int64(len(v)),
  1063. }
  1064. re := map[string]bool{}
  1065. for _, vv := range v {
  1066. if pis[vv.Id] != nil {
  1067. pi.ProjectCount += pis[vv.Id].ProjectCount
  1068. pi.ProjectAmount += pis[vv.Id].ProjectAmount
  1069. for _, vvv := range pis[vv.Id].ExportId {
  1070. if re[vvv] {
  1071. continue
  1072. }
  1073. pi.ExportId = append(pi.ExportId, vvv)
  1074. re[vvv] = true
  1075. }
  1076. }
  1077. if entMonitor[vv.Name] {
  1078. pi.MonitorCount++
  1079. }
  1080. }
  1081. vm[k] = pi
  1082. }
  1083. return vm
  1084. }
  1085. //
  1086. func (n *network) AllIntroduceOwner(sqlAppend1, sqlAppend2 string, args []interface{}, isTjProject bool, probusfors []string, entMonitor map[string]bool) *introduceOwnerProject {
  1087. 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=2,b.relate_id,NULL)) AS relate_project_id,a.create_time from crm.connection a
  1088. 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
  1089. listTemp := CrmMysql.SelectBySql(q, args...)
  1090. firstparty_array, supplier_array, adiffb_array, agency_array, middleman_project_array := []string{}, []string{}, []string{}, []string{}, []string{}
  1091. myNetworks := []*myNetwork{}
  1092. for _, v := range *listTemp {
  1093. myNetwork := &myNetwork{
  1094. Id: Int64All(v["id"]),
  1095. Company_id: ObjToString(v["company_id"]),
  1096. Company_name: ObjToString(v["company_name"]),
  1097. Qyxy_id: ObjToString(v["qyxy_id"]),
  1098. Itype: Int64All(v["itype"]),
  1099. Person: ObjToString(v["person"]),
  1100. Phone: ObjToString(v["phone"]),
  1101. Buyer_count: Int64All(v["buyer_count"]),
  1102. Project_count: Int64All(v["project_count"]),
  1103. Relate_buyer_id: ObjToString(v["relate_buyer_id"]),
  1104. Relate_project_id: ObjToString(v["relate_project_id"]),
  1105. Create_time: ObjToString(v["create_time"]),
  1106. }
  1107. switch myNetwork.Itype {
  1108. case 1:
  1109. firstparty_array = append(firstparty_array, myNetwork.Company_id)
  1110. case 2:
  1111. supplier_array = append(supplier_array, myNetwork.Company_id)
  1112. case 3:
  1113. adiffb_array = append(adiffb_array, myNetwork.Company_id)
  1114. case 4:
  1115. if myNetwork.Relate_project_id != "" {
  1116. middleman_project_array = append(middleman_project_array, strings.Split(myNetwork.Relate_project_id, ",")...)
  1117. }
  1118. case 5:
  1119. agency_array = append(agency_array, myNetwork.Company_id)
  1120. }
  1121. myNetworks = append(myNetworks, myNetwork)
  1122. }
  1123. iop := &introduceOwnerProject{
  1124. Networks: myNetworks,
  1125. FirstpartyNetwork: map[string][]*firstpartyNetwork{},
  1126. Firstparty: map[string]*projectInfo{},
  1127. Supplier: map[string]*projectInfo{},
  1128. Adiffb: map[string]*projectInfo{},
  1129. Agency: map[string]*projectInfo{},
  1130. Middleman: map[string]*projectInfo{},
  1131. }
  1132. if isTjProject {
  1133. iop.FirstpartyNetwork = n.FirstpartyNetwork("", firstparty_array)
  1134. iop.Firstparty = n.Introduce_Firstparty(iop.FirstpartyNetwork, entMonitor, probusfors)
  1135. iop.Supplier = n.Introduce_Supplier(supplier_array, entMonitor, probusfors)
  1136. iop.Adiffb = n.Introduce_Supplier(adiffb_array, entMonitor, probusfors)
  1137. iop.Agency = n.Introduce_Agency(agency_array, entMonitor, probusfors)
  1138. iop.Middleman = n.Introduce_Middleman(middleman_project_array, entMonitor, probusfors)
  1139. }
  1140. return iop
  1141. }