borrowing.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package dao
  2. import (
  3. "github.com/shopspring/decimal"
  4. "server/global"
  5. )
  6. type Borrowing struct {
  7. global.GVA_MODEL
  8. Name string `json:"name" form:"name" gorm:"comment:支借名称"`
  9. Applicant string `json:"applicant" form:"applicant" gorm:"comment:申请人"`
  10. ApplicationTime string `json:"applicationTime" form:"applicationTime" gorm:"comment:申请日期"`
  11. Charge int `json:"charge" form:"charge" gorm:"comment:负责人"`
  12. User SysUser `json:"user" form:"user" gorm:"foreignKey:Charge;references:id;"`
  13. BorrowingPrice decimal.Decimal `json:"borrowingPrice" form:"borrowingPrice" gorm:"comment:支借金额;type:decimal(10,2)"`
  14. }
  15. func (Borrowing) TableName() string {
  16. return "borrowing"
  17. }
  18. // TODO:支借查询
  19. // QueryBorrowingList 查询支借列表
  20. func QueryBorrowingList(limit, offset int, name, time string) (borrowing []Borrowing, total int64, err error) {
  21. // 创建db
  22. db := global.GVA_DB.Model(&Borrowing{})
  23. // 如果有条件搜索 下方会自动创建搜索语句
  24. if name != "" {
  25. db = db.Where("name LIKE ?", "%"+name+"%")
  26. }
  27. if time != "" {
  28. db = db.Where("DATE_FORMAT(application_time, '%Y-%m-%d') = ?", time)
  29. }
  30. err = db.Count(&total).Error
  31. if err != nil {
  32. return
  33. }
  34. err = db.Order("id desc").Limit(limit).Offset(offset).Preload("User").Find(&borrowing).Error
  35. return borrowing, total, err
  36. }
  37. // QueryBorrowing 查询支借
  38. func QueryBorrowing(id int) (borrowing Borrowing, err error) {
  39. err = global.GVA_DB.Model(&Borrowing{}).Where("id = ?", id).Preload("User").First(&borrowing).Error
  40. return borrowing, err
  41. }
  42. // TODO:支借新增
  43. // CreateBorrowing 新增支借
  44. func (b Borrowing) CreateBorrowing() error {
  45. return global.GVA_DB.Create(&b).Error
  46. }
  47. // TODO:支借修改
  48. // UpdateBorrowing 更新支借
  49. func (b Borrowing) UpdateBorrowing() error {
  50. return global.GVA_DB.Model(&b).Updates(&b).Error
  51. }
  52. // TODO:支借删除
  53. // DeleteBorrowing 删除支借
  54. func (b Borrowing) DeleteBorrowing() error {
  55. return global.GVA_DB.Unscoped().Delete(&b).Error
  56. }