logcredit.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package http
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "go-common/app/service/main/upcredit/model/calculator"
  6. "go-common/app/service/main/upcredit/model/upcrmmodel"
  7. "go-common/app/service/main/upcredit/service"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "go-common/library/net/http/blademaster"
  11. "io/ioutil"
  12. "time"
  13. )
  14. func logCredit(c *blademaster.Context) {
  15. var data interface{}
  16. var err error
  17. var errMsg string
  18. var r = new(upcrmmodel.ArgCreditLogAdd)
  19. switch {
  20. default:
  21. var body, _ = ioutil.ReadAll(c.Request.Body)
  22. if err = json.Unmarshal(body, r); err != nil {
  23. log.Error("request argument json decode fail, err=%v", err)
  24. errMsg = fmt.Sprintf("wrong argument, %s, body={%s}", err.Error(), string(body))
  25. err = ecode.RequestErr
  26. break
  27. }
  28. err = Svc.LogCredit(c, r)
  29. }
  30. if err != nil {
  31. service.BmHTTPErrorWithMsg(c, err, errMsg)
  32. } else {
  33. c.JSON(data, err)
  34. }
  35. }
  36. func recalc(c *blademaster.Context) {
  37. var data interface{}
  38. var err error
  39. var errMsg string
  40. var r = new(struct {
  41. TableNum int `form:"tablenum"`
  42. CalcDate string `form:"date"`
  43. AllTable bool `form:"all_table" default:"false"`
  44. })
  45. switch {
  46. default:
  47. if err = c.Bind(r); err != nil {
  48. log.Error("param error")
  49. err = ecode.RequestErr
  50. break
  51. }
  52. var calc = calculator.New(Svc.CreditScoreInputChan)
  53. var date = time.Time{}
  54. if r.CalcDate != "" {
  55. date, _ = time.Parse("2006-01-02", r.CalcDate)
  56. } else {
  57. date = time.Now()
  58. }
  59. if r.AllTable {
  60. Svc.CalcSvc.AddCalcJob(date)
  61. } else {
  62. go calc.CalcLogTable(r.TableNum, date, nil)
  63. }
  64. log.Info("start calculate process, req=%+v", r)
  65. }
  66. if err != nil {
  67. service.BmHTTPErrorWithMsg(c, err, errMsg)
  68. } else {
  69. c.JSON(data, err)
  70. }
  71. }
  72. func scoreGet(c *blademaster.Context) {
  73. var data interface{}
  74. var err error
  75. var errMsg string
  76. var r = new(upcrmmodel.ArgMidDate)
  77. switch {
  78. default:
  79. if err = c.Bind(r); err != nil {
  80. log.Error("param error")
  81. err = ecode.RequestErr
  82. break
  83. }
  84. var arg = upcrmmodel.GetScoreParam{
  85. Mid: r.Mid,
  86. ScoreType: r.ScoreType,
  87. }
  88. var now = time.Now()
  89. if r.Days > 60 {
  90. r.Days = 60
  91. }
  92. if r.FromDate != "" {
  93. arg.FromDate, err = time.Parse(upcrmmodel.DateStr, r.FromDate)
  94. if err != nil {
  95. errMsg = err.Error()
  96. err = ecode.RequestErr
  97. break
  98. }
  99. }
  100. if r.ToDate != "" {
  101. arg.ToDate, err = time.Parse(upcrmmodel.DateStr, r.FromDate)
  102. if err != nil {
  103. errMsg = err.Error()
  104. err = ecode.RequestErr
  105. break
  106. }
  107. }
  108. if r.Days != 0 {
  109. var y, m, d = now.Date()
  110. arg.ToDate = time.Date(y, m, d, 0, 0, 0, 0, now.Location())
  111. arg.FromDate = arg.ToDate.AddDate(0, 0, -r.Days)
  112. }
  113. var result, e = Svc.GetCreditScore(c, &arg)
  114. err = e
  115. if err != nil {
  116. log.Error("fail to get credit score, req=%+v, err=%+v", arg, err)
  117. break
  118. }
  119. data = map[string]interface{}{
  120. "score_list": result,
  121. }
  122. log.Info("get credit score, req=%+v, datalen=%d", r, len(result))
  123. }
  124. if err != nil {
  125. service.BmHTTPErrorWithMsg(c, err, errMsg)
  126. } else {
  127. c.JSON(data, err)
  128. }
  129. }
  130. func logGet(c *blademaster.Context) {
  131. var data interface{}
  132. var err error
  133. var errMsg string
  134. var r = new(upcrmmodel.ArgGetLogHistory)
  135. switch {
  136. default:
  137. if err = c.Bind(r); err != nil {
  138. log.Error("param error")
  139. errMsg = "param error"
  140. err = ecode.RequestErr
  141. break
  142. }
  143. var result, e = Svc.GetCreditLog(c, r)
  144. err = e
  145. if err != nil {
  146. errMsg = err.Error()
  147. log.Error("fail to get credit log, req=%+v, err=%+v", r, err)
  148. break
  149. }
  150. data = map[string]interface{}{
  151. "log_list": result,
  152. }
  153. log.Info("get credit log ok, req=%+v, datalen=%d", r, len(result))
  154. }
  155. if err != nil {
  156. service.BmHTTPErrorWithMsg(c, err, errMsg)
  157. } else {
  158. c.JSON(data, err)
  159. }
  160. }
  161. func calcSection(c *blademaster.Context) {
  162. var data interface{}
  163. var err error
  164. var errMsg string
  165. var r = new(struct {
  166. CalcDate string `form:"date"`
  167. })
  168. switch {
  169. default:
  170. if err = c.Bind(r); err != nil {
  171. log.Error("param error")
  172. err = ecode.RequestErr
  173. break
  174. }
  175. var date = time.Time{}
  176. if r.CalcDate != "" {
  177. date, _ = time.Parse("2006-01-02", r.CalcDate)
  178. } else {
  179. date = time.Now().AddDate(0, 0, -1)
  180. }
  181. var job = &service.CalcStatisticJob{
  182. ID: 1,
  183. Date: date,
  184. Svc: Svc.CalcSvc,
  185. }
  186. Svc.CalcSvc.JobChannel <- job
  187. log.Info("start calculate process, req=%+v, job=%+v", r, job)
  188. }
  189. if err != nil {
  190. service.BmHTTPErrorWithMsg(c, err, errMsg)
  191. } else {
  192. c.JSON(data, err)
  193. }
  194. }
  195. func test(c *blademaster.Context) {
  196. Svc.Test()
  197. }