up_base_info.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package upcrmdao
  2. import (
  3. "fmt"
  4. "github.com/siddontang/go-mysql/mysql"
  5. "go-common/app/service/main/upcredit/model/upcrmmodel"
  6. "go-common/library/log"
  7. xtime "go-common/library/time"
  8. "strings"
  9. "time"
  10. )
  11. const (
  12. //TimeFmtMysql mysql time format
  13. TimeFmtMysql = mysql.TimeFormat
  14. //TimeFmtDate with only date
  15. TimeFmtDate = "2006-01-02"
  16. )
  17. //UpQualityInfo struct
  18. type UpQualityInfo struct {
  19. Mid int64 `json:"mid"`
  20. QualityValue int `json:"quality_value"`
  21. PrValue int `json:"pr_value"`
  22. Cdate string `json:"cdate"` // 产生时间 "2018-01-01"
  23. }
  24. // AsPrScore copy to db struct
  25. func (u *UpQualityInfo) AsPrScore() (history *upcrmmodel.UpScoreHistory) {
  26. if u == nil {
  27. return &upcrmmodel.UpScoreHistory{}
  28. }
  29. history = &upcrmmodel.UpScoreHistory{
  30. Mid: u.Mid,
  31. ScoreType: upcrmmodel.ScoreTypePr,
  32. Score: u.PrValue,
  33. }
  34. var date, _ = time.Parse(TimeFmtDate, u.Cdate)
  35. history.GenerateDate = xtime.Time(date.Unix())
  36. return
  37. }
  38. // AsQualityScore copy to db struct
  39. func (u *UpQualityInfo) AsQualityScore() (history *upcrmmodel.UpScoreHistory) {
  40. if u == nil {
  41. return &upcrmmodel.UpScoreHistory{}
  42. }
  43. history = &upcrmmodel.UpScoreHistory{
  44. Mid: u.Mid,
  45. ScoreType: upcrmmodel.ScoreTypeQuality,
  46. Score: u.QualityValue,
  47. }
  48. var date, _ = time.Parse(TimeFmtDate, u.Cdate)
  49. history.GenerateDate = xtime.Time(date.Unix())
  50. return
  51. }
  52. //UpdateCreditScore update score
  53. func (d *Dao) UpdateCreditScore(score int, mid int64) (affectRow int64, err error) {
  54. var db = d.crmdb.Model(upcrmmodel.UpBaseInfo{}).Where("mid = ? and business_type = 1", mid).Update("credit_score", score)
  55. return db.RowsAffected, db.Error
  56. }
  57. //UpdateQualityAndPrScore update score
  58. func (d *Dao) UpdateQualityAndPrScore(prScore int, qualityScore int, mid int64) (affectRow int64, err error) {
  59. var db = d.crmdb.Model(upcrmmodel.UpBaseInfo{}).Where("mid = ? and business_type = 1", mid).Update(map[string]int{"pr_score": prScore, "quality_score": qualityScore})
  60. return db.RowsAffected, db.Error
  61. }
  62. //InsertScoreHistory insert into score history
  63. func (d *Dao) InsertScoreHistory(info *UpQualityInfo) (affectRow int64, err error) {
  64. var qualityScoreSt = info.AsQualityScore()
  65. err = d.crmdb.Save(qualityScoreSt).Error
  66. if err != nil {
  67. log.Error("insert quality score error, err=%+v", err)
  68. }
  69. var prScore = info.AsPrScore()
  70. err = d.crmdb.Save(prScore).Error
  71. if err != nil {
  72. log.Error("insert pr score error, err=%+v", err)
  73. }
  74. return
  75. }
  76. //InsertBatchScoreHistory insert batch sql
  77. func (d *Dao) InsertBatchScoreHistory(infoList []*UpQualityInfo, tablenum int) (affectRow int64, err error) {
  78. var batchSQL = fmt.Sprintf("insert into up_scores_history_%02d (mid, score_type, score, generate_date) values ", tablenum)
  79. var valueString []string
  80. var valueArgs []interface{}
  81. for _, info := range infoList {
  82. valueString = append(valueString, "(?,?,?,?),(?,?,?,?)")
  83. valueArgs = append(valueArgs, info.Mid, upcrmmodel.ScoreTypePr, info.PrValue, info.Cdate)
  84. valueArgs = append(valueArgs, info.Mid, upcrmmodel.ScoreTypeQuality, info.QualityValue, info.Cdate)
  85. }
  86. var db = d.crmdb.Exec(batchSQL+strings.Join(valueString, ","), valueArgs...)
  87. affectRow = db.RowsAffected
  88. err = db.Error
  89. return
  90. }