log_query_provider_count_export.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. package crontab
  2. import (
  3. "context"
  4. "gd_crontab/apis"
  5. "gd_crontab/errors"
  6. "gd_crontab/rpc_apis"
  7. "gd_crontab/rpc_apis/gd_management"
  8. "fmt"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/tidwall/gjson"
  13. "github.com/astaxie/beego/orm"
  14. )
  15. const pageSize = 1000
  16. func zqyProviderCountSql(req *apis.LogQueryProviderCountExportReq) string {
  17. if req.EndTimestamp == 0 || req.StartTimestamp == 0 {
  18. nowTime := time.Now()
  19. zeroTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, nowTime.Location())
  20. req.StartTimestamp = zeroTime.Unix()
  21. req.EndTimestamp = nowTime.Unix()
  22. }
  23. if req.GroupByMerchantApi == true {
  24. return fmt.Sprintf("select response_params from %s where provider_api_id=? and merchant_id = ? and api_id = ? and raw_code=1 and create_time=? GROUP BY search limit ? offset ?", req.TabName)
  25. }
  26. return fmt.Sprintf("select response_params from %s where provider_api_id=? and raw_code=1 and create_time=? GROUP BY search limit ? offset ?", req.TabName)
  27. }
  28. func zqyProviderCount(req *apis.LogQueryProviderCountExportReq, countExport *apis.LogQueryProviderCountExport) {
  29. countExport.Charge = 0
  30. var rCount int64
  31. var err error
  32. sqlStr := zqyProviderCountSql(req)
  33. o := orm.NewOrm()
  34. offset := 0
  35. for {
  36. results := make([]string, 0)
  37. if req.GroupByMerchantApi == true {
  38. rCount, err = o.Raw(sqlStr, countExport.ProviderApiId, req.MerchantId, req.ApiId, countExport.Date, pageSize, offset*pageSize).QueryRows(&results)
  39. if err != nil {
  40. break
  41. }
  42. } else {
  43. rCount, err = o.Raw(sqlStr, countExport.ProviderApiId, countExport.Date, pageSize, offset*pageSize).QueryRows(&results)
  44. if err != nil {
  45. break
  46. }
  47. }
  48. for _, r := range results {
  49. arr := gjson.Get(r, "ds").Array()
  50. flag := false
  51. for _, a := range arr {
  52. count := 0
  53. if countExport.ProviderApiName == "车牌查车档" {
  54. if vin := gjson.Get(a.String(), "车辆识别代码").String(); vin == "" {
  55. count += 1
  56. }
  57. } else if countExport.ProviderApiName == "VIN码查车档" {
  58. if plateNo := gjson.Get(a.String(), "CPH").String(); plateNo == "" {
  59. count += 1
  60. }
  61. } else if countExport.ProviderApiName == "VIN6获取车档" {
  62. if vin := gjson.Get(a.String(), "车辆识别代码").String(); vin == "" {
  63. count += 1
  64. }
  65. if plateNo := gjson.Get(a.String(), "CPH").String(); plateNo == "" {
  66. count += 1
  67. }
  68. }
  69. if v := gjson.Get(a.String(), "品牌").String(); v == "" {
  70. count += 1
  71. }
  72. if v := gjson.Get(a.String(), "车辆型号").String(); v == "" {
  73. count += 1
  74. }
  75. if v := gjson.Get(a.String(), "发动机号").String(); v == "" {
  76. count += 1
  77. }
  78. if v := gjson.Get(a.String(), "使用性质").String(); v == "" {
  79. count += 1
  80. }
  81. if v := gjson.Get(a.String(), "初次登记时间").String(); v == "" {
  82. count += 1
  83. }
  84. if v := gjson.Get(a.String(), "燃料种类").String(); v == "" {
  85. count += 1
  86. }
  87. if v := gjson.Get(a.String(), "排量").String(); v == "" {
  88. count += 1
  89. }
  90. if v := gjson.Get(a.String(), "发动机型号").String(); v == "" {
  91. count += 1
  92. }
  93. if v := gjson.Get(a.String(), "额定载客").String(); v == "" {
  94. count += 1
  95. }
  96. if v := gjson.Get(a.String(), "车辆名称").String(); v == "" {
  97. count += 1
  98. }
  99. if v := gjson.Get(a.String(), "车身颜色").String(); v == "" {
  100. count += 1
  101. }
  102. if v := gjson.Get(a.String(), "出厂日期").String(); v == "" {
  103. count += 1
  104. }
  105. if v := gjson.Get(a.String(), "轴数").String(); v == "" {
  106. count += 1
  107. }
  108. if v := gjson.Get(a.String(), "轴距").String(); v == "" {
  109. count += 1
  110. }
  111. if v := gjson.Get(a.String(), "前轮距").String(); v == "" {
  112. count += 1
  113. }
  114. if v := gjson.Get(a.String(), "后轮距").String(); v == "" {
  115. count += 1
  116. }
  117. if v := gjson.Get(a.String(), "总质量").String(); v == "" {
  118. count += 1
  119. }
  120. if v := gjson.Get(a.String(), "整备质量").String(); v == "" {
  121. count += 1
  122. }
  123. if v := gjson.Get(a.String(), "额定载质量").String(); v == "" {
  124. count += 1
  125. }
  126. if v := gjson.Get(a.String(), "功率").String(); v == "" {
  127. count += 1
  128. }
  129. if v := gjson.Get(a.String(), "承保日期").String(); v == "" {
  130. count += 1
  131. }
  132. if count < 3 {
  133. flag = true
  134. }
  135. }
  136. if flag == true {
  137. countExport.Charge++
  138. }
  139. }
  140. if rCount < pageSize {
  141. break
  142. }
  143. offset++
  144. }
  145. }
  146. func getProviderCountWhereExport(req *apis.LogQueryProviderCountExportReq) (w string, s []interface{}) {
  147. if req.EndTimestamp != 0 && req.StartTimestamp != 0 {
  148. w += " and " + "timestamp >=" + " ? and timestamp <?"
  149. s = append(s, req.StartTimestamp, req.EndTimestamp)
  150. } else {
  151. nowTime := time.Now()
  152. zeroTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, nowTime.Location())
  153. startTimeStamp := zeroTime.Unix()
  154. endTimeStamp := nowTime.Unix()
  155. w += " and " + "timestamp >=" + " ? and timestamp <?"
  156. s = append(s, startTimeStamp, endTimeStamp)
  157. }
  158. if len(req.ProviderApiIdList) != 0 {
  159. w += " and provider_api_id in (" + strings.Replace(strings.Trim(fmt.Sprint(req.ProviderApiIdList), "[]"), " ", ",", -1) + ")"
  160. }
  161. if req.ProviderId != 0 {
  162. //strings.Replace(strings.Trim(fmt.Sprint(array_or_slice), "[]"), " ", ",", -1)
  163. w += " and " + "provider_id" + " = ?"
  164. s = append(s, req.ProviderId)
  165. }
  166. if req.ApiId != 0 {
  167. w += " and " + "api_id" + " = ?"
  168. s = append(s, req.ApiId)
  169. }
  170. if req.MerchantId != 0 {
  171. w += " and " + "merchant_id" + " = ?"
  172. s = append(s, req.MerchantId)
  173. }
  174. if len(s) > 0 {
  175. //有条件才截取最前面的and
  176. w = string([]byte(w)[4:])
  177. w = " where" + w
  178. return w, s
  179. } else {
  180. return "", nil
  181. }
  182. }
  183. func getProviderExportSql(req *apis.LogQueryProviderCountExportReq, where string) string {
  184. sqlStr := "select create_time as date, provider_api_id, count(state) as total, " +
  185. "sum(state=1) as success, sum(code<>1100 and state=1) as query, " +
  186. "sum(state<>1) as failed," +
  187. "sum(code=1100) as query_no_record, " +
  188. "avg(elapsed) as avg_elapsed, " +
  189. "sum(elapsed) as sum_elapsed" +
  190. "%s" +
  191. " from " + req.TabName + " as a %s group by create_time,provider_api_id order by timestamp"
  192. if req.GroupByMerchantApi {
  193. sqlStr = "select create_time as date, provider_api_id, merchant_id, api_id, merchant_name, api_name, count(state) as total, " +
  194. "sum(state=1) as success, sum(code<>1100 and state=1) as query, " +
  195. "sum(state<>1) as failed," +
  196. "sum(code=1100) as query_no_record, " +
  197. "avg(elapsed) as avg_elapsed, " +
  198. "sum(elapsed) as sum_elapsed" +
  199. "%s" +
  200. " from " + req.TabName + " as a %s group by create_time, merchant_id, api_id,provider_api_id order by merchant_id, api_id, provider_api_id, timestamp"
  201. }
  202. //if req.HasMerchantName {
  203. // sqlStr = "select create_time as date, provider_api_id, merchant_name, api_name, count(state) as total, " +
  204. // "sum(state=1) as success, sum(code<>20001 and state=1) as query, " +
  205. // "sum(state<>1) as failed," +
  206. // "sum(code=20001) as query_no_record, " +
  207. // "avg(elapsed) as avg_elapsed, " +
  208. // "sum(elapsed) as sum_elapsed" +
  209. // "%s" +
  210. // " from " + req.TabName + " as a %s group by create_time,provider_api_id order by timestamp"
  211. //}
  212. concat := ""
  213. subSql := ""
  214. timeList := strings.Split(req.TimeList, ",")
  215. for _, v := range timeList {
  216. if v == "" {
  217. break
  218. }
  219. if subSql == "" {
  220. subSql = fmt.Sprintf("sum(elapsed > %s)", v)
  221. continue
  222. }
  223. subSql = fmt.Sprintf("%s,sum(elapsed > %s)", subSql, v)
  224. }
  225. if subSql != "" {
  226. concat = fmt.Sprintf(",concat_ws(',', %s) as big_elapseds", subSql)
  227. }
  228. sqlStr = fmt.Sprintf(sqlStr, concat, where)
  229. return sqlStr
  230. }
  231. type ProviderCountInfo struct {
  232. ProviderApiId int64 `json:"provider_api_id"`
  233. CountType int `json:"count_type"`
  234. CountKey string `json:"count_key"`
  235. CountValue string `json:"count_value"`
  236. RawCode string `json:"raw_code"`
  237. ProviderName string `json:"provider_name"`
  238. ProviderApiName string `json:"provider_api_name"`
  239. }
  240. func getProviderCountInfo() (map[int64]ProviderCountInfo, error) {
  241. req := gd_management.ManagementGetProviderCountInfoReq{}
  242. reply, err := rpc_apis.Management.ManagementGetProviderCountInfo(context.Background(), &req)
  243. if err != nil {
  244. return nil, err
  245. }
  246. ret := map[int64]ProviderCountInfo{}
  247. for _, v := range reply.Infos {
  248. item := ProviderCountInfo{}
  249. item.ProviderApiId = v.ProviderApiId
  250. item.CountType = v.CountType
  251. item.CountKey = v.CountKey
  252. item.CountValue = v.CountValue
  253. item.ProviderName = v.ProviderName
  254. item.ProviderApiName = v.ProviderApiName
  255. delimer := ","
  256. if strings.Contains(v.CountValue, ";") {
  257. delimer = ";"
  258. }
  259. array := strings.Split(v.CountValue, delimer)
  260. for _, code := range array {
  261. if item.RawCode == "" {
  262. item.RawCode = fmt.Sprintf("'%s'", code)
  263. continue
  264. }
  265. item.RawCode = fmt.Sprintf("%s,'%s'", item.RawCode, code)
  266. }
  267. ret[v.ProviderApiId] = item
  268. }
  269. return ret, nil
  270. }
  271. func computeProviderCharge(o orm.Ormer, req *apis.LogQueryProviderCountExportReq,
  272. info *apis.LogQueryProviderCountExport, countInfos map[int64]ProviderCountInfo) error {
  273. concat := ""
  274. subSql := ""
  275. timeList := strings.Split(req.TimeList, ",")
  276. for _, v := range timeList {
  277. if v == "" {
  278. break
  279. }
  280. if subSql == "" {
  281. subSql = fmt.Sprintf("sum(elapsed > %s)", v)
  282. continue
  283. }
  284. subSql = fmt.Sprintf("%s,sum(elapsed > %s)", subSql, v)
  285. }
  286. if subSql != "" {
  287. concat = fmt.Sprintf(",concat_ws(',', %s) as big_elapseds", subSql)
  288. }
  289. chargeBigElapseds := ""
  290. info.ChargeBigElapsedList = make([]int, len(timeList))
  291. if countInfo, ok := countInfos[info.ProviderApiId]; ok {
  292. if countInfo.CountType == 0 {
  293. info.Charge = info.Total
  294. info.ChargeRate = "100%"
  295. chargeBigElapseds = info.BigElapseds
  296. } else if countInfo.CountValue == "" {
  297. info.Charge = 0
  298. info.ChargeRate = "0%"
  299. for i := 0; i < len(timeList); i++ {
  300. if chargeBigElapseds == "" {
  301. chargeBigElapseds = "0"
  302. } else {
  303. chargeBigElapseds = fmt.Sprintf("%s,0", chargeBigElapseds)
  304. }
  305. }
  306. } else {
  307. start, end := getStartEndTimestamp(req.StartTimestamp, req.EndTimestamp, info.Date)
  308. count := int64(0)
  309. sql := fmt.Sprintf("select count(1) "+concat+" from "+req.TabName+" where raw_code in(%s) and provider_api_id=%d and timestamp >= %d and timestamp < %d", countInfo.RawCode, info.ProviderApiId, start, end)
  310. if req.MerchantId > 0 {
  311. sql = fmt.Sprintf("%s and merchant_id=%d", sql, req.MerchantId)
  312. }
  313. if req.ApiId > 0 {
  314. sql = fmt.Sprintf("%s and api_id=%d", sql, req.ApiId)
  315. }
  316. if req.GroupByMerchantApi {
  317. sql = fmt.Sprintf("%s and merchant_id=%d and api_id=%d", sql, info.MerchantId, info.ApiId)
  318. }
  319. if concat == "" {
  320. err := o.Raw(sql).QueryRow(&count)
  321. if err != nil {
  322. return errors.DataBaseError
  323. }
  324. } else {
  325. err := o.Raw(sql).QueryRow(&count, &chargeBigElapseds)
  326. if err != nil {
  327. return errors.DataBaseError
  328. }
  329. }
  330. info.Charge = count
  331. info.ChargeRate = strconv.FormatFloat(float64(100*count)/float64(info.Total), 'f', 2, 64) + "%"
  332. }
  333. } else {
  334. info.Charge = info.Total
  335. info.ChargeRate = "100%"
  336. chargeBigElapseds = info.BigElapseds
  337. }
  338. if chargeBigElapseds != "" {
  339. array := strings.Split(chargeBigElapseds, ",")
  340. for i, _ := range array {
  341. vint, _ := strconv.Atoi(array[i])
  342. info.ChargeBigElapsedList[i] += vint
  343. }
  344. }
  345. return nil
  346. }
  347. func logQueryProviderCountExport(req *apis.LogQueryProviderCountExportReq, reply *apis.LogQueryProviderCountExportReply) error {
  348. o := orm.NewOrm()
  349. where, val := getProviderCountWhereExport(req)
  350. sqlStr := getProviderExportSql(req, where)
  351. _, err := o.Raw(sqlStr, val).QueryRows(&reply.LogQueryProviderCount)
  352. if err != nil && err != orm.ErrNoRows {
  353. return errors.DataBaseError
  354. }
  355. countInfos, err := getProviderCountInfo()
  356. if err != nil {
  357. return err
  358. }
  359. for i, v := range reply.LogQueryProviderCount {
  360. if info, ok := countInfos[v.ProviderApiId]; ok {
  361. reply.LogQueryProviderCount[i].ProviderName = info.ProviderName
  362. reply.LogQueryProviderCount[i].ProviderApiName = info.ProviderApiName
  363. }
  364. }
  365. for index, info := range reply.LogQueryProviderCount {
  366. if err := computeProviderCharge(o, req, &reply.LogQueryProviderCount[index], countInfos); err != nil {
  367. return err
  368. }
  369. if info.BigElapseds != "" {
  370. array := strings.Split(info.BigElapseds, ",")
  371. reply.LogQueryProviderCount[index].BigElapsedList = make([]int, len(array))
  372. for i, _ := range array {
  373. vint, _ := strconv.Atoi(array[i])
  374. reply.LogQueryProviderCount[index].BigElapsedList[i] = vint
  375. }
  376. }
  377. reply.LogQueryProviderCount[index].Failed = info.Total - info.Success
  378. if info.Total > 0 {
  379. reply.LogQueryProviderCount[index].FailedRate = strconv.FormatFloat(float64(100*info.Failed)/float64(info.Total), 'f', 2, 64) + "%"
  380. reply.LogQueryProviderCount[index].QueryNoRecordRate = strconv.FormatFloat(float64(100*info.QueryNoRecord)/float64(info.Total), 'f', 2, 64) + "%"
  381. } else {
  382. reply.LogQueryProviderCount[index].FailedRate = "0%"
  383. reply.LogQueryProviderCount[index].QueryNoRecordRate = "0%"
  384. }
  385. }
  386. return nil
  387. }
  388. func LogQueryProviderCountExportNew(req *apis.LogQueryProviderCountExportReq, reply *apis.LogQueryProviderCountExportReply) error {
  389. if req.EndTimestamp == 0 || req.StartTimestamp == 0 {
  390. now := time.Now()
  391. req.EndTimestamp = now.Unix()
  392. req.StartTimestamp = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()
  393. }
  394. if req.TabName == "t_gd_thirdpart_log_day" {
  395. return logQueryProviderCountExport(req, reply)
  396. }
  397. exportTimes := getExportTimes(req.StartTimestamp, req.EndTimestamp)
  398. for _, v := range exportTimes {
  399. req.StartTimestamp = v.start
  400. req.EndTimestamp = v.end
  401. req.TabName = getMonthTab("t_gd_thirdpart_log_month", req.StartTimestamp)
  402. subReply := &apis.LogQueryProviderCountExportReply{}
  403. err := logQueryProviderCountExport(req, subReply)
  404. if err != nil {
  405. return err
  406. }
  407. reply.LogQueryProviderCount = append(reply.LogQueryProviderCount, subReply.LogQueryProviderCount...)
  408. }
  409. return nil
  410. }
  411. func LogQueryProviderCountExport(ctx context.Context, req *apis.LogQueryProviderCountExportReq, reply *apis.LogQueryProviderCountExportReply) error {
  412. return LogQueryProviderCountExportNew(req, reply)
  413. }
  414. func ExportProviderDay(ctx context.Context, req *apis.LogQueryProviderCountExportReq, reply *apis.LogQueryProviderCountExportReply) error {
  415. exportProviderDay(req.StartTimestamp, req.EndTimestamp, 0, true)
  416. return nil
  417. }