unit.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. func (unitService *UnitService) UpdateUnitIntergral() {
  50. var integral = 0
  51. global.GVA_DB.Session(&gorm.Session{
  52. AllowGlobalUpdate: true,
  53. }).Model(&autocode.Unit{}).Updates(&autocode.Unit{UnitIntegral: &integral})
  54. }
  55. // UpdateUnit 开始下期
  56. // Author [piexlmax](https://github.com/piexlmax)
  57. func (unitService *UnitService) UpdateUnitNext(unit autocode.Unit) (err error) {
  58. //开始下一期
  59. now := *unit.UnitIntegral
  60. now = now * 10
  61. unit.UnitIntegral = &now
  62. global.GVA_DB.Session(&gorm.Session{
  63. AllowGlobalUpdate: true,
  64. }).Model(&unit).Updates(&autocode.Unit{UnitMaxIntegral: unit.UnitIntegral, UnitIntegral: unit.UnitIntegral, Period: time.Now().Format("20060102150405")})
  65. return err
  66. }
  67. func (unitService *UnitService) UpdateUnitA(unit autocode.Unit) (err error) {
  68. err = global.GVA_DB.Save(&unit).Error
  69. return err
  70. }
  71. // GetUnit 根据id获取Unit记录
  72. // Author [piexlmax](https://github.com/piexlmax)
  73. func (unitService *UnitService) GetUnit(id uint) (err error, unit autocode.Unit) {
  74. err = global.GVA_DB.Where("id = ?", id).First(&unit).Error
  75. return
  76. }
  77. func (unitService *UnitService) GetUnitTop() (err error, unit autocode.Unit) {
  78. top := autocode.Unit{}
  79. err = global.GVA_DB.First(&top).Error
  80. return err, top
  81. }
  82. // GetUnitInfoList 分页获取Unit记录
  83. // Author [piexlmax](https://github.com/piexlmax)
  84. func (unitService *UnitService) GetUnitInfoList(info autoCodeReq.UnitSearch) (err error, list interface{}, total int64) {
  85. limit := info.PageSize
  86. offset := info.PageSize * (info.Page - 1)
  87. // 创建db
  88. db := global.GVA_DB.Model(&autocode.Unit{})
  89. if info.UnitName != "" {
  90. db.Where("unit_name like ?", "%"+info.UnitName+"%")
  91. }
  92. var units []autocode.Unit
  93. // 如果有条件搜索 下方会自动创建搜索语句
  94. err = db.Count(&total).Error
  95. err = db.Limit(limit).Offset(offset).Find(&units).Error
  96. return err, units, total
  97. }
  98. func (unitService *UnitService) GetUnitInfoListAll() (err error, list []autocode.Unit) {
  99. var units []autocode.Unit
  100. // 创建db
  101. err = global.GVA_DB.Model(&autocode.Unit{}).Find(&units).Error
  102. return err, units
  103. }