unit.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. now := *unit.UnitIntegral
  34. now = now * 10
  35. unit.UnitIntegral = &now
  36. err = global.GVA_DB.Save(&unit).Error
  37. return err
  38. }
  39. // UpdateUnit 开始下期
  40. // Author [piexlmax](https://github.com/piexlmax)
  41. func (unitService *UnitService) UpdateUnitNext(unit autocode.Unit) (err error) {
  42. //开始下一期
  43. now := *unit.UnitIntegral
  44. now = now * 10
  45. unit.UnitIntegral = &now
  46. global.GVA_DB.Session(&gorm.Session{
  47. AllowGlobalUpdate: true,
  48. }).Model(&unit).Updates(&autocode.Unit{UnitMaxIntegral: unit.UnitIntegral, UnitIntegral: unit.UnitIntegral, Period: time.Now().Format("20060102150405")})
  49. return err
  50. }
  51. func (unitService *UnitService) UpdateUnitA(unit autocode.Unit) (err error) {
  52. err = global.GVA_DB.Save(&unit).Error
  53. return err
  54. }
  55. // GetUnit 根据id获取Unit记录
  56. // Author [piexlmax](https://github.com/piexlmax)
  57. func (unitService *UnitService) GetUnit(id uint) (err error, unit autocode.Unit) {
  58. err = global.GVA_DB.Where("id = ?", id).First(&unit).Error
  59. return
  60. }
  61. func (unitService *UnitService) GetUnitTop() (err error, unit autocode.Unit) {
  62. top := autocode.Unit{}
  63. err = global.GVA_DB.First(&top).Error
  64. return err, top
  65. }
  66. // GetUnitInfoList 分页获取Unit记录
  67. // Author [piexlmax](https://github.com/piexlmax)
  68. func (unitService *UnitService) GetUnitInfoList(info autoCodeReq.UnitSearch) (err error, list interface{}, total int64) {
  69. limit := info.PageSize
  70. offset := info.PageSize * (info.Page - 1)
  71. // 创建db
  72. db := global.GVA_DB.Model(&autocode.Unit{})
  73. if info.UnitName != "" {
  74. db.Where("unit_name = ?", info.UnitName)
  75. }
  76. var units []autocode.Unit
  77. // 如果有条件搜索 下方会自动创建搜索语句
  78. err = db.Count(&total).Error
  79. err = db.Limit(limit).Offset(offset).Find(&units).Error
  80. return err, units, total
  81. }
  82. func (unitService *UnitService) GetUnitInfoListAll() (err error, list []autocode.Unit) {
  83. var units []autocode.Unit
  84. // 创建db
  85. err = global.GVA_DB.Model(&autocode.Unit{}).Find(&units).Error
  86. return err, units
  87. }