unit.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package autocode
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
  5. autoCodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
  6. "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
  7. "gorm.io/gorm"
  8. "time"
  9. )
  10. type UnitService struct {
  11. }
  12. // CreateUnit 创建Unit记录
  13. // Author [piexlmax](https://github.com/piexlmax)
  14. func (unitService *UnitService) CreateUnit(unit autocode.Unit) (err error) {
  15. err = global.GVA_DB.Create(&unit).Error
  16. return err
  17. }
  18. // DeleteUnit 删除Unit记录
  19. // Author [piexlmax](https://github.com/piexlmax)
  20. func (unitService *UnitService) DeleteUnit(unit autocode.Unit) (err error) {
  21. err = global.GVA_DB.Delete(&unit).Error
  22. return err
  23. }
  24. // DeleteUnitByIds 批量删除Unit记录
  25. // Author [piexlmax](https://github.com/piexlmax)
  26. func (unitService *UnitService) DeleteUnitByIds(ids request.IdsReq) (err error) {
  27. err = global.GVA_DB.Delete(&[]autocode.Unit{}, "id in ?", ids.Ids).Error
  28. return err
  29. }
  30. // UpdateUnit 更新Unit记录
  31. // Author [piexlmax](https://github.com/piexlmax)
  32. func (unitService *UnitService) UpdateUnit(unit autocode.Unit) (err error) {
  33. //if unit {
  34. //
  35. //}
  36. //now := *unit.UnitIntegral
  37. //now = now * 100
  38. //unit.UnitIntegral = &now
  39. err = global.GVA_DB.Save(&unit).Error
  40. return err
  41. }
  42. func (unitService *UnitService) UpdateUnitMaxIntegral(id *int, maxIntergral int) (err error) {
  43. var unit autocode.Unit
  44. global.GVA_DB.Where("id = ?", id).First(&unit)
  45. unit.UnitMaxIntegral = &maxIntergral
  46. err = global.GVA_DB.Save(unit).Error
  47. return err
  48. }
  49. // UpdateUnit 开始下期
  50. // Author [piexlmax](https://github.com/piexlmax)
  51. func (unitService *UnitService) UpdateUnitNext(unit autocode.Unit) (err error) {
  52. //开始下一期
  53. now := *unit.UnitIntegral
  54. now = now * 10
  55. unit.UnitIntegral = &now
  56. global.GVA_DB.Session(&gorm.Session{
  57. AllowGlobalUpdate: true,
  58. }).Model(&unit).Updates(&autocode.Unit{UnitMaxIntegral: unit.UnitIntegral, UnitIntegral: unit.UnitIntegral, Period: time.Now().Format("20060102150405")})
  59. return err
  60. }
  61. func (unitService *UnitService) UpdateUnitA(unit autocode.Unit) (err error) {
  62. err = global.GVA_DB.Save(&unit).Error
  63. return err
  64. }
  65. // GetUnit 根据id获取Unit记录
  66. // Author [piexlmax](https://github.com/piexlmax)
  67. func (unitService *UnitService) GetUnit(id uint) (err error, unit autocode.Unit) {
  68. err = global.GVA_DB.Where("id = ?", id).First(&unit).Error
  69. return
  70. }
  71. func (unitService *UnitService) GetUnitTop() (err error, unit autocode.Unit) {
  72. top := autocode.Unit{}
  73. err = global.GVA_DB.First(&top).Error
  74. return err, top
  75. }
  76. // GetUnitInfoList 分页获取Unit记录
  77. // Author [piexlmax](https://github.com/piexlmax)
  78. func (unitService *UnitService) GetUnitInfoList(info autoCodeReq.UnitSearch) (err error, list interface{}, total int64) {
  79. limit := info.PageSize
  80. offset := info.PageSize * (info.Page - 1)
  81. // 创建db
  82. db := global.GVA_DB.Model(&autocode.Unit{})
  83. if info.UnitName != "" {
  84. db.Where("unit_name like ?", "%"+info.UnitName+"%")
  85. }
  86. var units []autocode.Unit
  87. // 如果有条件搜索 下方会自动创建搜索语句
  88. err = db.Count(&total).Error
  89. err = db.Limit(limit).Offset(offset).Find(&units).Error
  90. return err, units, total
  91. }
  92. func (unitService *UnitService) GetUnitInfoListAll() (err error, list []autocode.Unit) {
  93. var units []autocode.Unit
  94. // 创建db
  95. err = global.GVA_DB.Model(&autocode.Unit{}).Find(&units).Error
  96. return err, units
  97. }