common.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. package marketanalysis
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/encrypt"
  5. elastic "app.yhyue.com/moapp/jybase/es"
  6. "app.yhyue.com/moapp/jybase/mongodb"
  7. "encoding/json"
  8. "fmt"
  9. esV7 "github.com/olivere/elastic/v7"
  10. "log"
  11. "math"
  12. "strings"
  13. "sync"
  14. "time"
  15. )
  16. // 中标单位分析
  17. func WinningAnalysis(thisWinnerRow BuyerWinnerRow, rMap *sync.Map, entArrMap map[string]float64, winnerName map[string]string, sy *sync.WaitGroup, offline bool) {
  18. defer sy.Done()
  19. type s_Winner struct {
  20. Name string `json:"key"`
  21. TCount int `json:"t_count"` //项目数量
  22. TAmount float64 `json:"t_amount"` //项目金额
  23. TotalAmount interface{} `json:"total_amount"`
  24. TotalNumber interface{} `json:"total_number"`
  25. }
  26. //中标单位-中标规模分布
  27. winnerA := make(map[string]*distributionTrend)
  28. for _, v := range thisWinnerRow.WinnerAmountDistribution.EntidlistTerms.Buckets {
  29. if vs, ok := entArrMap[v.Key]; ok {
  30. v.Amount.Value += vs
  31. delete(entArrMap, v.Key)
  32. }
  33. amountDistribution(v.Amount.Value, winnerA)
  34. }
  35. for _, f := range entArrMap {
  36. amountDistribution(f, winnerA)
  37. }
  38. var (
  39. countAll int
  40. amountAll float64
  41. )
  42. for _, v := range winnerA {
  43. countAll += v.Count
  44. amountAll += v.Amount
  45. }
  46. buyerMap := []interface{}{}
  47. for _, v := range Analysis {
  48. var data s_Winner
  49. data.Name = v
  50. vlu, ok := winnerA[v]
  51. if ok {
  52. data.TCount = vlu.Count
  53. data.TAmount = vlu.Amount
  54. if countAll != 0 {
  55. data.TotalNumber = float64(vlu.Count) / float64(countAll)
  56. }
  57. if amountAll != 0 {
  58. data.TotalAmount = vlu.Amount / amountAll
  59. }
  60. }
  61. buyerMap = append(buyerMap, data)
  62. }
  63. type number_projects struct {
  64. Id interface{} `json:"id"`
  65. Name string `json:"name"`
  66. Number int64 `json:"number"`
  67. //Accounted interface{} `json:"accounted"`
  68. Data interface{} `json:"buyertop3"`
  69. }
  70. type number_project struct {
  71. Name string `json:"name"`
  72. Number int64 `json:"number"`
  73. //Accounted interface{} `json:"accounted"`
  74. }
  75. //中标单位-项目数量TOP3
  76. countMap := []interface{}{}
  77. for _, v := range thisWinnerRow.WinnerCountTop3.SWinnerCount {
  78. var _d number_projects
  79. _d.Number = v.BuyerCount.Value
  80. if winnerName[v.Key] == "" {
  81. continue
  82. }
  83. _d.Name = winnerName[v.Key]
  84. _d.Id = encodeId(v.Key)
  85. /*if thisWinnerRow.ProjectCount.Value != 0 {
  86. _d.Accounted = float64(v.BuyerCount.Value) / float64(thisWinnerRow.ProjectCount.Value)
  87. }*/
  88. var ss []interface{}
  89. for _, v1 := range v.BuyerTop.Buckets {
  90. var _dd number_project
  91. _dd.Name = v1.Key
  92. _dd.Number = v1.BuyerWinnerAmount.Value
  93. /*if _d.Number != 0 {
  94. _dd.Accounted = float64(v1.BuyerWinnerAmount.Value) / float64(_d.Number)
  95. }*/
  96. ss = append(ss, _dd)
  97. }
  98. _d.Data = ss
  99. if !offline {
  100. if len(ss) > Top3 {
  101. _d.Data = ss[:Top3]
  102. }
  103. if len(countMap) >= Top30 {
  104. break
  105. }
  106. }
  107. countMap = append(countMap, _d)
  108. }
  109. //中标单位-项目金额TOP3
  110. type amount_projects struct {
  111. Id interface{} `json:"id"`
  112. Name string `json:"name"`
  113. Amount float64 `json:"amount"`
  114. //Accounted interface{} `json:"accounted"`
  115. Data interface{} `json:"buyertop3"`
  116. }
  117. type buyertop3 struct {
  118. Name string `json:"name"`
  119. Amount float64 `json:"amount"`
  120. //Accounted interface{} `json:"accounted"`
  121. }
  122. amountMap := []interface{}{}
  123. for _, v := range thisWinnerRow.WinnerAmountTop3.SWinnerAmount {
  124. if v.SWinnerAmount.Value == 0 || winnerName[v.Key] == "" {
  125. continue
  126. }
  127. var _d amount_projects
  128. _d.Amount = v.SWinnerAmount.Value
  129. _d.Name = winnerName[v.Key]
  130. _d.Id = encodeId(v.Key)
  131. /*if thisWinnerRow.ProjectAmount.Value != 0 {
  132. _d.Accounted = v.SWinnerAmount.Value / thisWinnerRow.ProjectAmount.Value
  133. }*/
  134. var ss []interface{}
  135. for _, v1 := range v.BuyerTop.Buckets {
  136. if v1.BuyerWinnerAmount.Value == 0 {
  137. continue
  138. }
  139. var _dd buyertop3
  140. _dd.Name = v1.Key
  141. _dd.Amount = v1.BuyerWinnerAmount.Value
  142. /*if _d.Amount != 0 {
  143. _dd.Accounted = v1.BuyerWinnerAmount.Value / _d.Amount
  144. }*/
  145. ss = append(ss, _dd)
  146. }
  147. _d.Data = ss
  148. if !offline {
  149. if len(ss) > Top3 {
  150. _d.Data = ss[:Top3]
  151. }
  152. if len(amountMap) >= Top30 {
  153. break
  154. }
  155. }
  156. amountMap = append(amountMap, _d)
  157. }
  158. rMap.Store("winner_time_distribution", buyerMap)
  159. rMap.Store("winner_count_top3", countMap)
  160. rMap.Store("winner_amount_top3", amountMap)
  161. }
  162. func amountDistribution(v float64, data map[string]*distributionTrend) {
  163. if v <= 0 {
  164. return
  165. }
  166. if v < 100000 {
  167. if data["<10万"] == nil {
  168. data["<10万"] = new(distributionTrend)
  169. }
  170. data["<10万"].Amount += v
  171. data["<10万"].Count++
  172. } else if v < 500000 {
  173. if data["10万~50万"] == nil {
  174. data["10万~50万"] = new(distributionTrend)
  175. }
  176. data["10万~50万"].Amount += v
  177. data["10万~50万"].Count++
  178. } else if v < 1000000 {
  179. if data["50万~100万"] == nil {
  180. data["50万~100万"] = new(distributionTrend)
  181. }
  182. data["50万~100万"].Amount += v
  183. data["50万~100万"].Count++
  184. } else if v < 1000000*5 {
  185. if data["100万~500万"] == nil {
  186. data["100万~500万"] = new(distributionTrend)
  187. }
  188. data["100万~500万"].Amount += v
  189. data["100万~500万"].Count++
  190. } else if v < 1000000*10 {
  191. if data["500万~1000万"] == nil {
  192. data["500万~1000万"] = new(distributionTrend)
  193. }
  194. data["500万~1000万"].Amount += v
  195. data["500万~1000万"].Count++
  196. } else if v < 100000000 {
  197. if data["1000万~1亿"] == nil {
  198. data["1000万~1亿"] = new(distributionTrend)
  199. }
  200. data["1000万~1亿"].Amount += v
  201. data["1000万~1亿"].Count++
  202. } else if v >= 100000000 {
  203. if data["≥1亿"] == nil {
  204. data["≥1亿"] = new(distributionTrend)
  205. }
  206. data["≥1亿"].Amount += v
  207. data["≥1亿"].Count++
  208. }
  209. }
  210. func BuyerAnalysis(thisBuyerRow BuyerWinnerRow, rMap *sync.Map, winnerName map[string]string, sy *sync.WaitGroup, offline bool) {
  211. defer sy.Done()
  212. type buyer struct {
  213. Name string `json:"key"`
  214. TCount int `json:"t_count"` //项目数量
  215. TAmount float64 `json:"t_amount"` //金额数量
  216. TotalAmount interface{} `json:"total_amount"`
  217. TotalNumber interface{} `json:"total_number"`
  218. }
  219. //采购单位-采购规模分布
  220. buyerMap := []interface{}{}
  221. //计算采购单位各区间金额
  222. buyerA := make(map[string]*distributionTrend)
  223. for _, v := range thisBuyerRow.BuyerAmountDistribution.Buckets {
  224. amountDistribution(v.Amount.Value, buyerA)
  225. }
  226. var (
  227. countAll int
  228. amountAll float64
  229. )
  230. for _, v := range buyerA {
  231. countAll += v.Count
  232. amountAll += v.Amount
  233. }
  234. for _, v := range Analysis {
  235. var data buyer
  236. data.Name = v
  237. vlu, ok := buyerA[v]
  238. if ok {
  239. data.TAmount = vlu.Amount
  240. data.TCount = vlu.Count
  241. if countAll != 0 {
  242. data.TotalNumber = float64(vlu.Count) / float64(countAll)
  243. }
  244. if amountAll != 0 {
  245. data.TotalAmount = vlu.Amount / amountAll
  246. }
  247. }
  248. buyerMap = append(buyerMap, data)
  249. }
  250. type number_projects struct {
  251. Name string `json:"name"`
  252. Number int64 `json:"number"`
  253. //Accounted interface{} `json:"accounted"`
  254. Data interface{} `json:"winnertop3"`
  255. }
  256. type number_project struct {
  257. Id interface{} `json:"id"`
  258. Name string `json:"name"`
  259. Number int64 `json:"number"`
  260. //Accounted interface{} `json:"accounted"`
  261. }
  262. //采购单位-项目数量TOP3
  263. countMap := []interface{}{}
  264. for _, v := range thisBuyerRow.BuyerCountTop3.Buckets {
  265. var _d number_projects
  266. _d.Name = v.Key
  267. _d.Number = v.BuyerCount.Value
  268. /*if thisBuyerRow.ProjectCount.Value != 0 {
  269. _d.Accounted = float64(v.BuyerCount.Value) / float64(thisBuyerRow.ProjectCount.Value)
  270. }*/
  271. var ss []interface{}
  272. for _, v1 := range v.SWinnerTop.Buckets {
  273. var _dd number_project
  274. _dd.Number = v1.BuyerWinnerAmount.Value
  275. if winnerName[v1.Key] == "" {
  276. continue
  277. }
  278. _dd.Name = winnerName[v1.Key]
  279. _dd.Id = encodeId(v1.Key)
  280. /*if _d.Number != 0 {
  281. _dd.Accounted = float64(v1.BuyerWinnerAmount.Value) / float64(_d.Number)
  282. }*/
  283. ss = append(ss, _dd)
  284. }
  285. _d.Data = ss
  286. if !offline {
  287. if len(ss) > Top3 {
  288. _d.Data = ss[:Top3]
  289. }
  290. if len(countMap) >= Top30 {
  291. break
  292. }
  293. }
  294. countMap = append(countMap, _d)
  295. }
  296. //采购单位-项目金额TOP3
  297. type amount_projects struct {
  298. Name string `json:"name"`
  299. Amount float64 `json:"amount"`
  300. //Accounted interface{} `json:"accounted"`
  301. Data interface{} `json:"winnertop3"`
  302. }
  303. type amount_project struct {
  304. Id interface{} `json:"id"`
  305. Name string `json:"name"`
  306. Amount float64 `json:"amount"`
  307. //Accounted interface{} `json:"accounted"`
  308. }
  309. amountMap := []interface{}{}
  310. for _, v := range thisBuyerRow.BuyerAmountTop3.Buckets {
  311. if v.BuyerAmount.Value == 0 {
  312. continue
  313. }
  314. var _d amount_projects
  315. _d.Name = v.Key
  316. _d.Amount = v.BuyerAmount.Value
  317. /*if thisBuyerRow.ProjectAmount.Value != 0 {
  318. _d.Accounted = v.BuyerAmount.Value / thisBuyerRow.ProjectAmount.Value
  319. }*/
  320. var ss []interface{}
  321. for _, v1 := range v.SWinnerTop.Buckets {
  322. if v1.BuyerWinnerAmount.Value == 0 || winnerName[v1.Key] == "" {
  323. continue
  324. }
  325. var _dd amount_project
  326. _dd.Amount = v1.BuyerWinnerAmount.Value
  327. _dd.Name = winnerName[v1.Key]
  328. _dd.Id = encodeId(v1.Key)
  329. /* if _d.Amount != 0 {
  330. _dd.Accounted = v1.BuyerWinnerAmount.Value / _d.Amount
  331. }*/
  332. ss = append(ss, _dd)
  333. }
  334. _d.Data = ss
  335. if !offline {
  336. if len(ss) > Top3 {
  337. _d.Data = ss[:Top3]
  338. }
  339. if len(amountMap) >= Top30 {
  340. break
  341. }
  342. }
  343. amountMap = append(amountMap, _d)
  344. }
  345. rMap.Store("buyer_time_distribution", buyerMap)
  346. rMap.Store("buyer_count_top3", countMap)
  347. rMap.Store("buyer_amount_top3", amountMap)
  348. }
  349. // top3数量计算
  350. func CountCompute(thisRow AreaCTop, types string, eid map[string]string, Offline bool) (rData []map[string]interface{}) {
  351. if types == "area" {
  352. area_infos := thisRow.AreaCountTop3
  353. for _, v := range area_infos.Buckets {
  354. if v.Total == 0 {
  355. break
  356. }
  357. rM := map[string]interface{}{}
  358. rWinner := []map[string]interface{}{}
  359. rM["name"] = v.Area
  360. rM["area_count"] = v.Total
  361. for _, va := range v.WinnerTop.Buckets {
  362. if va.WinnerTotal == 0 {
  363. break
  364. }
  365. rW := map[string]interface{}{}
  366. id := encodeId(va.Winner)
  367. rW["id"] = id
  368. rW["winner"] = eid[va.Winner]
  369. if eid[va.Winner] == "" {
  370. continue
  371. }
  372. rW["winner_total"] = va.WinnerTotal
  373. rWinner = append(rWinner, rW)
  374. }
  375. if !Offline && len(rWinner) == 0 {
  376. continue
  377. }
  378. rM["winner"] = rWinner
  379. //实时分析 取前三个
  380. if len(rWinner) > Top3 && !Offline {
  381. rM["winner"] = rWinner[:Top3]
  382. }
  383. if thisRow.Total != 0 {
  384. vv := float64(v.Total) / float64(thisRow.Total)
  385. rM["area_scale"] = vv
  386. } else {
  387. rM["area_scale"] = 0
  388. }
  389. rData = append(rData, rM)
  390. }
  391. } else {
  392. area_infos := thisRow.BuyclassCountTop3
  393. for _, v := range area_infos.Buckets {
  394. if v.Total == 0 {
  395. break
  396. }
  397. rM := map[string]interface{}{}
  398. rWinner := []map[string]interface{}{}
  399. rM["name"] = v.Buyclass
  400. rM["buyclass_count"] = v.Total
  401. for _, va := range v.BidcountTop.Buckets {
  402. if va.WinnerTotal == 0 {
  403. break
  404. }
  405. rW := map[string]interface{}{}
  406. id := encodeId(va.Winner)
  407. rW["id"] = id
  408. rW["winner"] = eid[va.Winner]
  409. if eid[va.Winner] == "" {
  410. continue
  411. }
  412. rW["winner_total"] = va.WinnerTotal
  413. rWinner = append(rWinner, rW)
  414. }
  415. if !Offline && len(rWinner) == 0 {
  416. continue
  417. }
  418. rM["winner"] = rWinner
  419. //实时分析 取前三个
  420. if len(rWinner) > Top3 && !Offline {
  421. rM["winner"] = rWinner[:Top3]
  422. }
  423. if thisRow.Total != 0 {
  424. vv := float64(v.Total) / float64(thisRow.Total)
  425. rM["buyclass_scale"] = vv
  426. } else {
  427. rM["buyclass_scale"] = 0
  428. }
  429. rData = append(rData, rM)
  430. }
  431. }
  432. return
  433. }
  434. // top3金额计算
  435. func AmountCompute(thisRow AreaCTop, types string, eid map[string]string, Offline bool) (rData []map[string]interface{}) {
  436. if types == "area" {
  437. area_infos := thisRow.AreaAmountTop3
  438. for _, v := range area_infos.Buckets {
  439. if v.AreaAmount.Amount == 0 {
  440. break
  441. }
  442. rM := map[string]interface{}{}
  443. rWinner := []map[string]interface{}{}
  444. rM["name"] = v.Key
  445. rM["area_amount"] = v.AreaAmount.Amount
  446. for _, va := range v.WinnerTop.Buckets {
  447. if va.WinnerAmount.Amount == 0 {
  448. break
  449. }
  450. rW := map[string]interface{}{}
  451. id := encodeId(va.Winner)
  452. rW["id"] = id
  453. rW["winner"] = eid[va.Winner]
  454. if eid[va.Winner] == "" {
  455. continue
  456. }
  457. rW["winner_amount"] = va.WinnerAmount.Amount
  458. //rW["amount_scale"] = va.WinnerAmount.Amount / v.AreaAmount.Amount
  459. rWinner = append(rWinner, rW)
  460. }
  461. rM["winner"] = rWinner
  462. //实时分析 取前三个
  463. if len(rWinner) > Top3 && !Offline {
  464. rM["winner"] = rWinner[:Top3]
  465. }
  466. if thisRow.Amount.Value != 0 {
  467. vv := v.AreaAmount.Amount / thisRow.Amount.Value
  468. rM["area_scale"] = vv
  469. } else {
  470. rM["area_scale"] = 0
  471. }
  472. rData = append(rData, rM)
  473. }
  474. } else {
  475. area_infos := thisRow.BuyclassAmountTop3
  476. for _, v := range area_infos.Buckets {
  477. if v.AreaAmount.Amount == 0 {
  478. break
  479. }
  480. rM := map[string]interface{}{}
  481. rWinner := []map[string]interface{}{}
  482. rM["name"] = v.Key
  483. rM["buyclass_amount"] = v.AreaAmount.Amount
  484. for _, va := range v.WinnerTop.Buckets {
  485. if va.WinnerAmount.Amount == 0 {
  486. break
  487. }
  488. rW := map[string]interface{}{}
  489. id := encodeId(va.Winner)
  490. rW["id"] = id
  491. rW["winner"] = eid[va.Winner]
  492. if eid[va.Winner] == "" {
  493. continue
  494. }
  495. rW["winner_amount"] = va.WinnerAmount.Amount
  496. //rW["amount_scale"] = va.WinnerAmount.Amount / v.AreaAmount.Amount
  497. rWinner = append(rWinner, rW)
  498. }
  499. rM["winner"] = rWinner
  500. //实时分析 取前三个
  501. if len(rWinner) > Top3 && !Offline {
  502. rM["winner"] = rWinner[:Top3]
  503. }
  504. if thisRow.Amount.Value != 0 {
  505. vv := v.AreaAmount.Amount / thisRow.Amount.Value
  506. rM["buyclass_scale"] = vv
  507. } else {
  508. rM["buyclass_scale"] = 0
  509. }
  510. rData = append(rData, rM)
  511. }
  512. }
  513. return
  514. }
  515. // 企业id查企业名 传入数组
  516. func IDToName(ids []string) (iMap map[string]string) {
  517. iMap = map[string]string{}
  518. d := strings.Join(ids, `","`)
  519. finalSql := fmt.Sprintf(queryIdto, d, len(ids))
  520. log.Println("IDToName sql:", finalSql)
  521. hits := elastic.Get("qyxy", "qyxy", finalSql)
  522. if hits == nil || len(*hits) == 0 {
  523. return
  524. }
  525. for _, item := range *hits {
  526. id, _ := item["_id"].(string)
  527. name, _ := item["company_name"].(string)
  528. if id != "" && name != "" {
  529. iMap[id] = name
  530. }
  531. }
  532. return
  533. }
  534. // 客户分布
  535. func CustomerDistribute(thisRow AreaCTop) (data []map[string]interface{}, otherData map[string]interface{}) {
  536. var (
  537. total int64
  538. amount float64
  539. )
  540. for _, v := range thisRow.BuyerclassScale.Buckets {
  541. if v.AreaTotal == 0 && v.BuyclassAmount.Value == 0 {
  542. continue
  543. }
  544. rM := map[string]interface{}{}
  545. rM["buyclass"] = v.Area
  546. rM["total"] = v.AreaTotal
  547. rM["amount"] = v.BuyclassAmount.Value
  548. total += v.AreaTotal
  549. amount += v.BuyclassAmount.Value
  550. data = append(data, rM)
  551. }
  552. //for _, v := range thisRow.BuyerclassScaleOther.Buckets {
  553. // total += v.AreaTotal
  554. // amount += v.BuyclassAmount.Value
  555. //}
  556. otherData = make(map[string]interface{})
  557. otherData["buyclass"] = "其它"
  558. if pCount := common.Int64All(thisRow.ProjectCount.DocCount) - total; pCount > 0 {
  559. otherData["total"] = pCount
  560. }
  561. if pAmount := thisRow.Amount.Value - amount; pAmount > 0 {
  562. otherData["amount"] = pAmount
  563. }
  564. return
  565. }
  566. // 地区分布
  567. func AreaDistribute(thisRow AreaCTop) (data []map[string]interface{}) {
  568. for _, v := range thisRow.AreaDistribution.Buckets {
  569. rM := map[string]interface{}{}
  570. rM["area"] = v.Area
  571. rM["total"] = v.AreaTotal
  572. rM["amount"] = v.AreaAmount.Value
  573. var rmc []map[string]interface{}
  574. if len(v.CityGroup.Buckets) > 0 {
  575. for _, c := range v.CityGroup.Buckets {
  576. rmc = append(rmc, map[string]interface{}{
  577. "city": c.City,
  578. "total": c.CityTotal,
  579. "amount": c.CityAmount.Value,
  580. })
  581. }
  582. rM["areaDetails"] = rmc
  583. }
  584. data = append(data, rM)
  585. }
  586. return
  587. }
  588. // 项目规模分布
  589. func ProjectScale(thisRow AreaCTop) (data []interface{}) {
  590. ammount := thisRow.Amount.Value
  591. total := thisRow.CountNot0.Count
  592. buckets := thisRow.SortpriceRanges.Buckets
  593. type Scale struct {
  594. Name string `bson:"Name"`
  595. Persent_c float64 `bson:"Persent_c"`
  596. Persent_a float64 `bson:"Persent_a"`
  597. Ammount float64 `json:"ammount"`
  598. Total int64 `json:"total"`
  599. }
  600. for _, v := range buckets {
  601. data = append(data, Scale{
  602. Name: v.Name,
  603. Ammount: v.SumSortprice.Value,
  604. Total: v.Total,
  605. Persent_a: Formula(float64(v.Total), float64(total)),
  606. Persent_c: Formula(v.SumSortprice.Value, ammount),
  607. })
  608. }
  609. return
  610. }
  611. // 计算公式
  612. func Formula(current, total float64) (result float64) {
  613. if total == 0 {
  614. return 0
  615. }
  616. result = current / total
  617. return
  618. }
  619. func InterToSliceString(obj interface{}) []string {
  620. var sli = make([]string, 0)
  621. if obj == nil {
  622. return sli
  623. }
  624. if _, ok := obj.([]interface{}); ok {
  625. for _, i := range obj.([]interface{}) {
  626. sli = append(sli, i.(string))
  627. }
  628. }
  629. if _, ok := obj.([]string); ok {
  630. return obj.([]string)
  631. }
  632. return sli
  633. }
  634. func sequential(now, old float64) interface{} {
  635. if old == 0 {
  636. return nil
  637. }
  638. return (now - old) / old
  639. }
  640. // GetAggs 聚合查询
  641. func GetAggs(index, itype, query string) esV7.Aggregations {
  642. v1, _, _ := elastic.GetAggs(index, itype, query)
  643. return v1
  644. }
  645. func GetAggsWithCount(index, itype, query string) (esV7.Aggregations, int64) {
  646. if len(query) > 0 {
  647. query = `{"track_total_hits": true,` + query[1:]
  648. }
  649. v1, v2, _ := elastic.GetAggs(index, itype, query)
  650. return v1, v2
  651. }
  652. func getPreviousMarket(sTime, eTime time.Time) int64 {
  653. var os_time int64
  654. s_time := sTime
  655. if SEMonth(sTime, eTime) {
  656. var min int
  657. //统计月份
  658. for sTime.Before(eTime) {
  659. sTime = sTime.AddDate(0, 1, 0)
  660. min++
  661. }
  662. os_time = s_time.AddDate(0, -min, 0).Unix()
  663. } else {
  664. os_time = s_time.AddDate(0, 0, -int(math.Ceil(eTime.Sub(sTime).Hours()/24))).Unix()
  665. }
  666. return os_time
  667. }
  668. func Rest(res esV7.Aggregations, thisRow *marketTime) {
  669. for name, object := range res {
  670. bArr, err := object.MarshalJSON()
  671. if len(bArr) == 0 || err != nil {
  672. continue
  673. }
  674. if name == "thismarket" {
  675. if json.Unmarshal(bArr, &thisRow.Thismarket) != nil {
  676. continue
  677. }
  678. } else if name == "oldmarket" {
  679. if json.Unmarshal(bArr, &thisRow.Oldmarket) != nil {
  680. continue
  681. }
  682. } else if name == "monthtime" {
  683. if json.Unmarshal(bArr, &thisRow.Monthtime) != nil {
  684. continue
  685. }
  686. } else if name == "yeartime" {
  687. if json.Unmarshal(bArr, &thisRow.Yeartime) != nil {
  688. continue
  689. }
  690. }
  691. }
  692. }
  693. func GetMonthData(isOffline bool, sTime, eTime time.Time) (bool, string) {
  694. var _b bool
  695. //整月多取一个月进行环比
  696. if !isOffline && SEMonth(sTime, eTime) {
  697. _b = true
  698. sTime = sTime.AddDate(0, -1, 0)
  699. }
  700. return _b, getBidamountStatistics(sTime, eTime)
  701. }
  702. func getBidamountStatistics(sTime, eTime time.Time) string {
  703. timeRange := ``
  704. tmpTime, rTime, tEndTime := sTime, sTime, getMonthRange(eTime, false)
  705. for rTime.Before(tEndTime) {
  706. ts, te := getMonthRange(tmpTime, true), getMonthRange(tmpTime, false)
  707. if sTime == tmpTime {
  708. ts = sTime
  709. }
  710. if te == tEndTime {
  711. te = eTime
  712. }
  713. if ts.Before(te) {
  714. timeRange += fmt.Sprintf(`{"key":"%s","from":%d,"to":%d},`, fmt.Sprintf("%d-%d", ts.Year(), ts.Month()), ts.Unix(), te.Unix())
  715. }
  716. rTime = rTime.AddDate(0, 1, 0)
  717. if int(rTime.Month())-int(tmpTime.Month()) > 1 {
  718. rTime = rTime.AddDate(0, -1, 0)
  719. }
  720. tmpTime = rTime
  721. }
  722. if timeRange == `` {
  723. return ""
  724. }
  725. return timeRange[:len(timeRange)-1]
  726. }
  727. // getMonthRange获取月份范围
  728. // isStart true本月月初 false 本月月末(下月月初)
  729. func getMonthRange(t time.Time, isStart bool) time.Time {
  730. if isStart {
  731. return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
  732. }
  733. return time.Date(t.Year(), t.Month()+1, 1, 0, 0, 0, 0, t.Location())
  734. }
  735. func GetYearData(isOffline bool, sTime, eTime time.Time) (bool, string) {
  736. var _b bool
  737. //整月多取一个月进行环比
  738. if !isOffline && sTime.Month() == 1 && sTime.Day() == 1 && eTime.Month() == 12 && eTime.Day() == 31 {
  739. _b = true
  740. sTime = sTime.AddDate(-1, 0, 0)
  741. }
  742. return _b, getCommonYearStatistics(sTime, eTime)
  743. }
  744. // getYearRange获取月份范围
  745. // isStart true本月月初 false 本月月末(下月月初)
  746. func getYearRange(t time.Time, isStart bool) time.Time {
  747. if isStart {
  748. return time.Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location())
  749. }
  750. return time.Date(t.Year()+1, 1, 1, 0, 0, 0, 0, t.Location())
  751. }
  752. // 年份统计
  753. func getCommonYearStatistics(sTime, eTime time.Time) string {
  754. timeRange := ``
  755. tmpTime, rTime, tEndTime := sTime, sTime, getYearRange(eTime, false)
  756. for rTime.Before(tEndTime) {
  757. ts, te := getYearRange(tmpTime, true), getYearRange(tmpTime, false)
  758. if sTime == tmpTime {
  759. ts = sTime
  760. }
  761. if te == tEndTime {
  762. te = eTime
  763. }
  764. if ts.Before(te) {
  765. timeRange += fmt.Sprintf(`{"key":"%d","from":%d,"to":%d},`, ts.Year(), ts.Unix(), te.Unix())
  766. }
  767. rTime = rTime.AddDate(1, 0, 0)
  768. tmpTime = rTime
  769. }
  770. if timeRange == `` {
  771. return ""
  772. }
  773. return timeRange[:len(timeRange)-1]
  774. }
  775. func GetEntNameByIds(ids []string) (returnMap map[string]string) {
  776. returnMap = map[string]string{}
  777. if len(ids) == 0 {
  778. return
  779. }
  780. list := elastic.Get("qyxy", "qyxy", fmt.Sprintf(`{"query":{"bool":{"must":[{"terms":{"_id":["%s"]}}]}},"_source":["_id","company_name"],"size":%d}`, strings.Join(ids, `","`), len(ids)))
  781. if list == nil || len(*list) == 0 {
  782. return
  783. }
  784. for _, item := range *list {
  785. id, _ := item["_id"].(string)
  786. name, _ := item["company_name"].(string)
  787. if id != "" && name != "" {
  788. returnMap[id] = name
  789. }
  790. }
  791. return
  792. }
  793. // GetMsgOpen 获取用户服务通知开关是否开启
  794. func GetMsgOpen(mgo *mongodb.MongodbSim, mgoUserId string, positionType int, entId, entUserId int) bool {
  795. //-- 服务通知不区分身份,只存在user表中
  796. //pushSetMap := &map[string]interface{}{}
  797. //if positionType == 1 {
  798. // pushSetMap, _ = mgo.FindOne("ent_user", map[string]interface{}{"i_entid": entId, "i_userid": entUserId})
  799. //} else {
  800. // pushSetMap, _ = mgo.FindById("user", mgoUserId, `{"o_pushset":1,"s_m_openid":1}`)
  801. //}
  802. pushSetMap, _ := mgo.FindById("user", mgoUserId, `{"o_pushset":1,"s_m_openid":1}`)
  803. //log.Println(mgoUserId, pushSetMap)
  804. if pushSetMap != nil && len(*pushSetMap) > 0 {
  805. pushset := common.ObjToMap((*pushSetMap)["o_pushset"])
  806. if pushset == nil || len(*pushset) == 0 {
  807. return false
  808. }
  809. msgServicePushSet := common.ObjToMap((*pushset)["o_msg_service"])
  810. if msgServicePushSet != nil {
  811. if common.IntAll((*msgServicePushSet)["i_apppush"]) == 1 || common.IntAll((*msgServicePushSet)["i_wxpush"]) == 1 {
  812. return true
  813. }
  814. }
  815. }
  816. return false
  817. }
  818. func encodeId(sid string) string {
  819. if sid == "" || sid == "-" { //不存在的id为-
  820. return ""
  821. }
  822. return encrypt.EncodeArticleId2ByCheck(sid)
  823. }
  824. // getGroupKeywordArr 模糊拆分为多个精准匹配
  825. func getGroupKeywordArr(res []viewKeyWord) (rData []viewKeyWord) {
  826. for _, kw := range res {
  827. if kw.MatchWay == 1 {
  828. for _, kk := range kw.Keyword {
  829. rData = append(rData, viewKeyWord{
  830. Keyword: []string{kk},
  831. Exclude: kw.Exclude,
  832. })
  833. }
  834. for _, kk := range kw.Appended {
  835. rData = append(rData, viewKeyWord{
  836. Keyword: []string{kk},
  837. Exclude: kw.Exclude,
  838. })
  839. }
  840. } else {
  841. rData = append(rData, kw)
  842. }
  843. }
  844. return
  845. }
  846. // getAllKeywordArr 获取所有匹配词
  847. func getAllKeywordArr(res []keyWordGroup) (rData []viewKeyWord) {
  848. for _, kwg := range res {
  849. rData = append(rData, getGroupKeywordArr(kwg.A_Key)...)
  850. }
  851. return
  852. }
  853. func getKeyWordSql(v viewKeyWord, matchingMode string) string {
  854. var (
  855. shoulds, must_not []string
  856. //默认查询项目名称与标的物
  857. localMultiMatch = `{"multi_match": {"query": %s,"type": "phrase", "fields": [` + FieldsDetail + `]}}`
  858. )
  859. if matchingMode == "title" { //只匹配项目名称
  860. localMultiMatch = `{"multi_match": {"query": %s,"type": "phrase", "fields": [` + Fields + `]}}`
  861. }
  862. //附加词
  863. for _, vv := range v.Keyword {
  864. vv = strings.TrimSpace(vv)
  865. if vv == "" {
  866. continue
  867. }
  868. shoulds = append(shoulds, fmt.Sprintf(localMultiMatch, "\""+vv+"\""))
  869. }
  870. for _, vv := range v.Appended {
  871. vv = strings.TrimSpace(vv)
  872. if vv == "" {
  873. continue
  874. }
  875. shoulds = append(shoulds, fmt.Sprintf(localMultiMatch, "\""+vv+"\""))
  876. }
  877. //排除词
  878. for _, vv := range v.Exclude {
  879. vv = strings.TrimSpace(vv)
  880. if vv == "" {
  881. continue
  882. }
  883. must_not = append(must_not, fmt.Sprintf(localMultiMatch, "\""+vv+"\""))
  884. }
  885. //添加
  886. if len(shoulds) > 0 {
  887. notStr := ""
  888. if len(must_not) > 0 {
  889. notStr = fmt.Sprintf(`,"must_not":[%s]`, strings.Join(must_not, ","))
  890. }
  891. return fmt.Sprintf(queryBoolMustAnd, strings.Join(shoulds, ","), notStr)
  892. }
  893. return ""
  894. }
  895. // 判断是否月初到月末
  896. func SEMonth(sTime, eTime time.Time) bool {
  897. var day int
  898. month := int(eTime.Month())
  899. if month == 2 {
  900. if eTime.Year()%4 == 0 {
  901. day = 29
  902. } else {
  903. day = 28
  904. }
  905. } else {
  906. day = yMDay[month]
  907. }
  908. if sTime.Day() == 1 && eTime.Day() == day {
  909. return true
  910. }
  911. return false
  912. }