dailyExpenses.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package dao
  2. import (
  3. "gorm.io/gorm"
  4. "server/global"
  5. )
  6. type DailyExpenses struct {
  7. global.GVA_MODEL
  8. Name string `json:"name" form:"name" gorm:"comment:名称"`
  9. Genre int `json:"genre" form:"genre" gorm:"comment:费用类型"`
  10. ExpensesGenre ExpensesGenre `json:"expensesGenre" form:"expensesGenre" gorm:"foreignKey:Genre;references:id;"`
  11. Applicant string `json:"applicant" form:"applicant" gorm:"comment:申请人"`
  12. ApplicationTime string `json:"applicationTime" form:"applicationTime" gorm:"comment:申请日期"`
  13. Charge int `json:"charge" form:"charge" gorm:"comment:负责人"`
  14. User SysUser `json:"user" form:"user" gorm:"foreignKey:Charge;references:id;"`
  15. TotalAmount float64 `json:"totalAmount" form:"totalAmount" gorm:"type:decimal(10,2);comment:总金额"`
  16. DailyFeeDetails []DailyFeeDetails `json:"dailyFeeDetails" form:"dailyFeeDetails" gorm:"-"`
  17. }
  18. func (DailyExpenses) TableName() string {
  19. return "daily_expenses"
  20. }
  21. // TODO:日常费用查询
  22. // QueryDailyExpensesList 查询日常费用列表
  23. func QueryDailyExpensesList(limit, offset, genre int, name, time string) (dailyExpenses []DailyExpenses, total int64, err error) {
  24. // 创建db
  25. db := global.GVA_DB.Model(&DailyExpenses{})
  26. // 如果有条件搜索 下方会自动创建搜索语句
  27. if genre != 0 {
  28. db = db.Where("genre = ?", genre)
  29. }
  30. if name != "" {
  31. db = db.Where("name LIKE ?", "%"+name+"%")
  32. }
  33. if time != "" {
  34. db = db.Where("DATE_FORMAT(application_time, '%Y-%m-%d') = ?", time)
  35. }
  36. err = db.Count(&total).Error
  37. if err != nil {
  38. return
  39. }
  40. err = db.Order("id desc").Limit(limit).Offset(offset).Preload("ExpensesGenre").Preload("User").Find(&dailyExpenses).Error
  41. return dailyExpenses, total, err
  42. }
  43. // QueryDailyExpenses 查询日常费用详情
  44. func QueryDailyExpenses(id int) (dailyExpenses DailyExpenses, err error) {
  45. err = global.GVA_DB.Model(&DailyExpenses{}).Where("id = ?", id).Preload("ExpensesGenre").Preload("User").First(&dailyExpenses).Error
  46. return dailyExpenses, err
  47. }
  48. // TODO:日常费用新增
  49. // CreateDailyExpenses 新增日常费用
  50. func (de DailyExpenses) CreateDailyExpenses() error {
  51. return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  52. txErr := tx.Create(&de).Error
  53. if txErr != nil {
  54. return txErr
  55. }
  56. for _, dailyFeeDetail := range de.DailyFeeDetails {
  57. dailyFeeDetail.DailyFeeId = de.ID
  58. txErr := tx.Create(&dailyFeeDetail).Error
  59. if txErr != nil {
  60. return txErr
  61. }
  62. }
  63. return txErr
  64. })
  65. }
  66. // TODO:日常费用修改
  67. // UpdateDailyExpenses 修改日常费用
  68. func (de DailyExpenses) UpdateDailyExpenses() error {
  69. return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  70. txErr := tx.Unscoped().Where("daily_fee_id = ?", de.ID).Delete(&DailyFeeDetails{}).Error
  71. if txErr != nil {
  72. return txErr
  73. }
  74. txErr = tx.Model(&de).Updates(&de).Error
  75. if txErr != nil {
  76. return txErr
  77. }
  78. for _, dailyFeeDetail := range de.DailyFeeDetails {
  79. txErr := tx.Create(&dailyFeeDetail).Error
  80. if txErr != nil {
  81. return txErr
  82. }
  83. }
  84. return txErr
  85. })
  86. }
  87. // TODO:日常费用删除
  88. func (de DailyExpenses) DeleteDailyExpenses() error {
  89. return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  90. txErr := tx.Unscoped().Where("daily_fee_id = ?", de.ID).Delete(&DailyFeeDetails{}).Error
  91. if txErr != nil {
  92. return txErr
  93. }
  94. txErr = tx.Unscoped().Delete(&de).Error
  95. if txErr != nil {
  96. return txErr
  97. }
  98. return txErr
  99. })
  100. }