upcrm.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package http
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/csv"
  6. "fmt"
  7. "github.com/siddontang/go-mysql/mysql"
  8. "go-common/app/admin/main/up/dao/upcrm"
  9. "go-common/app/admin/main/up/model/upcrmmodel"
  10. "go-common/app/admin/main/up/service"
  11. "go-common/library/ecode"
  12. "go-common/library/log"
  13. "go-common/library/net/http/blademaster"
  14. "time"
  15. )
  16. func getTitleFields(compareType int) []string {
  17. var compareTitle = ""
  18. switch compareType {
  19. case upcrmmodel.CompareType7day:
  20. compareTitle = "7日前"
  21. case upcrmmodel.CompareType30day:
  22. compareTitle = "30日前"
  23. case upcrmmodel.CompareTypeMonthFirstDay:
  24. compareTitle = "本月1号"
  25. }
  26. return []string{
  27. "分数段",
  28. "昨日",
  29. "占比",
  30. compareTitle,
  31. "占比",
  32. }
  33. }
  34. func getScoreName(scoreType int) string {
  35. var name = "分数"
  36. switch scoreType {
  37. case upcrm.ScoreTypePr:
  38. name = "影响力分"
  39. case upcrm.ScoreTypeQuality:
  40. name = "质量分"
  41. case upcrm.ScoreTypeCredit:
  42. name = "信用分"
  43. }
  44. return name
  45. }
  46. func getScoreQueryContentField(result *upcrmmodel.ScoreQueryResult, index int) []string {
  47. if result == nil {
  48. return nil
  49. }
  50. var fields []string
  51. if index >= len(result.XAxis) {
  52. return nil
  53. }
  54. fields = append(fields, result.XAxis[index])
  55. if index < len(result.YAxis) {
  56. fields = append(fields, fmt.Sprintf("%d", result.YAxis[index].Value))
  57. fields = append(fields, fmt.Sprintf("%0.2f%%", float32(result.YAxis[index].Percent)/100.0))
  58. } else {
  59. fields = append(fields, "-", "-")
  60. }
  61. if index < len(result.CompareAxis) {
  62. fields = append(fields, fmt.Sprintf("%d", result.CompareAxis[index].Value))
  63. fields = append(fields, fmt.Sprintf("%0.2f%%", float32(result.CompareAxis[index].Percent)/100.0))
  64. } else {
  65. fields = append(fields, "-", "-")
  66. }
  67. return fields
  68. }
  69. func crmScoreQuery(c *blademaster.Context) {
  70. var arg = new(upcrmmodel.ScoreQueryArgs)
  71. var res interface{}
  72. var err error
  73. var errMsg string
  74. var scoreRes upcrmmodel.ScoreQueryResult
  75. switch {
  76. default:
  77. if err = c.Bind(arg); err != nil {
  78. log.Error("request argument bind fail, err=%v", err)
  79. errMsg = fmt.Sprintf("wrong argument, %s", err.Error())
  80. err = ecode.RequestErr
  81. break
  82. }
  83. scoreRes, err = Svc.Crmservice.ScoreQuery(c, arg)
  84. if err != nil {
  85. errMsg = err.Error()
  86. log.Error("score query fail, req=%+v, err=%+v", arg, err)
  87. break
  88. }
  89. log.Info("score query ok, req=%+v, result=%+v", arg, scoreRes)
  90. res = scoreRes
  91. }
  92. if err != nil {
  93. service.BmHTTPErrorWithMsg(c, err, errMsg)
  94. } else {
  95. if arg.Export == "csv" {
  96. c.Writer.Header().Set("Content-Type", "application/csv")
  97. c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment;filename=\"%s_%s.csv\"", getScoreName(arg.ScoreType), time.Now().Format(mysql.TimeFormat)))
  98. var buf = &bytes.Buffer{}
  99. var csvWriter = csv.NewWriter(buf)
  100. csvWriter.Write(getTitleFields(arg.CompareType))
  101. for i := 0; i < len(scoreRes.XAxis); i++ {
  102. csvWriter.Write(getScoreQueryContentField(&scoreRes, i))
  103. }
  104. csvWriter.Flush()
  105. c.Writer.Write(buf.Bytes())
  106. } else {
  107. c.JSON(res, err)
  108. }
  109. }
  110. }
  111. func crmScoreQueryUp(c *blademaster.Context) {
  112. httpQueryFunc(new(upcrmmodel.ScoreQueryUpArgs),
  113. func(context context.Context, arg interface{}) (res interface{}, err error) {
  114. return Svc.Crmservice.ScoreQueryUp(context, arg.(*upcrmmodel.ScoreQueryUpArgs))
  115. },
  116. "ScoreQueryUp")(c)
  117. }
  118. func crmScoreQueryUpHistory(c *blademaster.Context) {
  119. httpQueryFunc(new(upcrmmodel.ScoreQueryUpHistoryArgs),
  120. // 由于不支持泛型,所以这里只能再包一层
  121. func(context context.Context, arg interface{}) (res interface{}, err error) {
  122. return Svc.Crmservice.ScoreQueryUpHistory(context, arg.(*upcrmmodel.ScoreQueryUpHistoryArgs))
  123. },
  124. "ScoreQueryUpHistory")(c)
  125. }
  126. func crmPlayQueryInfo(c *blademaster.Context) {
  127. httpQueryFunc(new(upcrmmodel.PlayQueryArgs),
  128. func(context context.Context, arg interface{}) (res interface{}, err error) {
  129. return Svc.Crmservice.PlayQueryInfo(context, arg.(*upcrmmodel.PlayQueryArgs))
  130. },
  131. "PlayQueryInfo")(c)
  132. }
  133. func crmInfoQueryUp(c *blademaster.Context) {
  134. httpQueryFunc(new(upcrmmodel.InfoQueryArgs),
  135. func(context context.Context, arg interface{}) (res interface{}, err error) {
  136. return Svc.Crmservice.UpBaseInfoQuery(context, arg.(*upcrmmodel.InfoQueryArgs))
  137. },
  138. "QueryBaseUpInfo")(c)
  139. }
  140. func crmCreditLogQueryUp(c *blademaster.Context) {
  141. httpQueryFunc(new(upcrmmodel.CreditLogQueryArgs),
  142. func(context context.Context, arg interface{}) (res interface{}, err error) {
  143. return Svc.Crmservice.CreditLogQueryUp(context, arg.(*upcrmmodel.CreditLogQueryArgs))
  144. },
  145. "CreditLogQueryUp")(c)
  146. }
  147. func crmRankQueryList(c *blademaster.Context) {
  148. httpQueryFunc(new(upcrmmodel.UpRankQueryArgs),
  149. func(context context.Context, arg interface{}) (res interface{}, err error) {
  150. return Svc.Crmservice.UpRankQueryList(context, arg.(*upcrmmodel.UpRankQueryArgs))
  151. },
  152. "UpRankQueryList")(c)
  153. }
  154. func crmInfoAccountInfo(c *blademaster.Context) {
  155. httpQueryFunc(new(upcrmmodel.InfoAccountInfoArgs),
  156. func(context context.Context, arg interface{}) (res interface{}, err error) {
  157. return Svc.Crmservice.UpAccountInfo(context, arg.(*upcrmmodel.InfoAccountInfoArgs))
  158. },
  159. "InfoAccountInfo")(c)
  160. }
  161. func crmInfoSearch(c *blademaster.Context) {
  162. httpPostFunc(new(upcrmmodel.InfoSearchArgs),
  163. func(context context.Context, arg interface{}) (res interface{}, err error) {
  164. return Svc.Crmservice.UpInfoSearch(context, arg.(*upcrmmodel.InfoSearchArgs))
  165. },
  166. "UpInfoSearch")(c)
  167. }
  168. func testGetViewBase(c *blademaster.Context) {
  169. httpQueryFunc(new(upcrmmodel.TestGetViewBaseArgs),
  170. func(context context.Context, arg interface{}) (res interface{}, err error) {
  171. return Svc.Crmservice.TestGetViewBase(context, arg.(*upcrmmodel.TestGetViewBaseArgs))
  172. },
  173. "TestGetViewBase")(c)
  174. }
  175. func crmQueryUpInfoWithViewerData(c *blademaster.Context) {
  176. httpQueryFunc(new(upcrmmodel.UpInfoWithViewerArg),
  177. func(context context.Context, arg interface{}) (res interface{}, err error) {
  178. return Svc.Crmservice.QueryUpInfoWithViewerData(context, arg.(*upcrmmodel.UpInfoWithViewerArg))
  179. },
  180. "QueryUpInfoWithViewerData")(c)
  181. }