面包店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

108 lines
2.8 KiB

  1. package db
  2. import (
  3. "applet/app/admin/md"
  4. "applet/app/db/model"
  5. "applet/app/utils/logx"
  6. "reflect"
  7. "xorm.io/xorm"
  8. )
  9. type ExportRecordsDb struct {
  10. Db *xorm.Engine `json:"db"`
  11. }
  12. func (exportRecordsDb *ExportRecordsDb) Set() { // set方法
  13. exportRecordsDb.Db = Db
  14. }
  15. func (exportRecordsDb *ExportRecordsDb) GetExportRecordsExportRecords(id int) (m *model.ExportRecords, err error) {
  16. m = new(model.ExportRecords)
  17. has, err := exportRecordsDb.Db.Where("id =?", id).Get(m)
  18. if err != nil {
  19. return nil, logx.Error(err)
  20. }
  21. if has == false {
  22. return nil, nil
  23. }
  24. return m, nil
  25. }
  26. func (exportRecordsDb *ExportRecordsDb) GetExportRecordsExportRecordsByMonth(year, month string) (m *model.ExportRecords, err error) {
  27. m = new(model.ExportRecords)
  28. has, err := exportRecordsDb.Db.Where("year =? and month =?", year, month).Get(m)
  29. if err != nil {
  30. return nil, logx.Error(err)
  31. }
  32. if has == false {
  33. return nil, nil
  34. }
  35. return m, nil
  36. }
  37. func (exportRecordsDb *ExportRecordsDb) FindExportRecordsExportRecords() (*[]model.ExportRecords, error) {
  38. var m []model.ExportRecords
  39. if err := exportRecordsDb.Db.Desc("id").Find(&m); err != nil {
  40. return nil, logx.Error(err)
  41. }
  42. return &m, nil
  43. }
  44. func (exportRecordsDb *ExportRecordsDb) ExportRecordsExportRecordsInsert(m *model.ExportRecords) (int, error) {
  45. _, err := exportRecordsDb.Db.InsertOne(m)
  46. if err != nil {
  47. return 0, err
  48. }
  49. return m.Id, nil
  50. }
  51. func (exportRecordsDb *ExportRecordsDb) ExportRecordsExportRecordsInsertBySession(session *xorm.Session, m *model.ExportRecords) (int, error) {
  52. _, err := session.InsertOne(m)
  53. if err != nil {
  54. return 0, err
  55. }
  56. return m.Id, nil
  57. }
  58. func (exportRecordsDb *ExportRecordsDb) ExportRecordsExportRecordsDelete(id interface{}) (int64, error) {
  59. if reflect.TypeOf(id).Kind() == reflect.Slice {
  60. return Db.In("id", id).Delete(model.ExportRecords{})
  61. } else {
  62. return Db.Where("id = ?", id).Delete(model.ExportRecords{})
  63. }
  64. }
  65. func (exportRecordsDb *ExportRecordsDb) ExportRecordsExportRecordsUpdateBySession(session *xorm.Session, id interface{}, m *model.ExportRecords, forceColums ...string) (int64, error) {
  66. var (
  67. affected int64
  68. err error
  69. )
  70. if forceColums != nil {
  71. affected, err = session.Where("id=?", id).Cols(forceColums...).Update(m)
  72. } else {
  73. affected, err = session.Where("id=?", id).Update(m)
  74. }
  75. if err != nil {
  76. return 0, err
  77. }
  78. return affected, nil
  79. }
  80. func (exportRecordsDb *ExportRecordsDb) ExportRecordsExportRecordsList(req md.ExportRecordsListReq) (m []model.ExportRecords, total int64, err error) {
  81. sess := exportRecordsDb.Db.Desc("id")
  82. if req.Kind != 0 {
  83. sess.And("kind =?", req.Kind)
  84. }
  85. if req.StartDate != "" {
  86. sess.And("create_at >=?", req.StartDate)
  87. }
  88. if req.EndDate != "" {
  89. sess.And("create_at <=?", req.EndDate)
  90. }
  91. total, err = sess.Limit(req.Limit, (req.Page-1)*req.Limit).FindAndCount(&m)
  92. if err != nil {
  93. return
  94. }
  95. return
  96. }