dao.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package upcrmdao
  2. import (
  3. "github.com/jinzhu/gorm"
  4. "go-common/app/service/main/upcredit/conf"
  5. "context"
  6. "fmt"
  7. "go-common/app/service/main/upcredit/model/upcrmmodel"
  8. "go-common/library/log"
  9. )
  10. //Dao upcrm dao
  11. type Dao struct {
  12. conf *conf.Config
  13. crmdb *gorm.DB
  14. }
  15. //New create
  16. func New(c *conf.Config) *Dao {
  17. var d = &Dao{
  18. conf: c,
  19. }
  20. crmdb, err := gorm.Open("mysql", c.DB.Upcrm.DSN)
  21. if crmdb == nil {
  22. log.Error("connect to db fail, err=%v", err)
  23. return nil
  24. }
  25. d.crmdb = crmdb
  26. crmdb.SingularTable(true)
  27. d.crmdb.LogMode(c.IsTest)
  28. return d
  29. }
  30. //Close close
  31. func (d *Dao) Close() {
  32. if d.crmdb != nil {
  33. d.crmdb.Close()
  34. }
  35. }
  36. //AddLog add log
  37. func (d *Dao) AddLog(arg *upcrmmodel.ArgCreditLogAdd) error {
  38. var creditLog = &upcrmmodel.CreditLog{}
  39. creditLog.CopyFrom(arg)
  40. return d.crmdb.Create(creditLog).Error
  41. }
  42. //AddCreditScore add score
  43. func (d *Dao) AddCreditScore(creditScore *upcrmmodel.UpScoreHistory) error {
  44. return d.crmdb.Create(creditScore).Error
  45. }
  46. //AddOrUpdateCreditScore update score
  47. func (d *Dao) AddOrUpdateCreditScore(creditScore *upcrmmodel.UpScoreHistory) (err error) {
  48. var tablename = creditScore.TableName()
  49. var insertSQL = fmt.Sprintf("insert into %s (mid, score_type, score, generate_date, ctime) values (?,?,?,?,?) "+
  50. "on duplicate key update score=?", tablename)
  51. err = d.crmdb.Exec(
  52. insertSQL,
  53. creditScore.Mid, creditScore.ScoreType, creditScore.Score, creditScore.GenerateDate, creditScore.CTime,
  54. creditScore.Score).Error
  55. if err != nil {
  56. log.Error("add credit score fail, mid=%d, err=%+v", creditScore.Mid, err)
  57. }
  58. return
  59. }
  60. //GetCreditScore get score
  61. func (d *Dao) GetCreditScore(c context.Context, arg *upcrmmodel.GetScoreParam) (results []*upcrmmodel.UpScoreHistory, err error) {
  62. var mod = upcrmmodel.UpScoreHistory{
  63. Mid: arg.Mid,
  64. }
  65. err = d.crmdb.Table(mod.TableName()).Select("score, generate_date").
  66. Where("mid=? AND generate_date>=? AND generate_date<=? AND score_type=?", arg.Mid, arg.FromDate, arg.ToDate, arg.ScoreType).
  67. Find(&results).Error
  68. if err != nil {
  69. log.Error("get score history fail, arg=%+v, err=%+v", arg, err)
  70. }
  71. return
  72. }
  73. //GetCreditLog get log
  74. func (d *Dao) GetCreditLog(c context.Context, arg *upcrmmodel.ArgGetLogHistory) (results []*upcrmmodel.SimpleCreditLogWithContent, err error) {
  75. var mod = upcrmmodel.SimpleCreditLogWithContent{}
  76. mod.Mid = arg.Mid
  77. err = d.crmdb.Table(mod.TableName()).Select("type, op_type, reason, business_type, ctime, content, oid").
  78. //Limit(arg.Limit).
  79. //Offset(arg.Offset).
  80. Where("mid=? AND ctime>=? AND ctime<=?", arg.Mid, arg.FromDate, arg.ToDate).
  81. Order("ctime").
  82. Find(&results).Error
  83. if err != nil {
  84. log.Error("get log history fail, arg=%+v, err=%+v", arg, err)
  85. }
  86. return
  87. }