unit.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. func (unitService *UnitService) GetUnitIntergralRate(remark string) (err error, countInfo []autocode.UnitInitIntegral) {
  56. global.GVA_DB.Raw("SELECT remark,unit_name,unit_max_integral - unit_integral score,1- ( unit_integral / unit_max_integral ) scorerate FROM unit "+
  57. "WHERE ( remark IS NOT NULL && remark != '' ) and remark =? ", remark).Scan(&countInfo)
  58. return err, countInfo
  59. }
  60. func (unitService *UnitService) GetUnitDesc(remark string) (err error, countInfo []autocode.UnitDesc) {
  61. global.GVA_DB.Raw("SELECT remark,unit_name,unit_max_integral-unit_integral score FROM unit WHERE (remark IS NOT NULL && remark !='') AND remark=? ORDER BY score DESC ", remark).Scan(&countInfo)
  62. return err, countInfo
  63. }
  64. // UpdateUnit 开始下期
  65. // Author [piexlmax](https://github.com/piexlmax)
  66. func (unitService *UnitService) UpdateUnitNext(unit autocode.Unit) (err error) {
  67. //开始下一期
  68. now := *unit.UnitIntegral
  69. now = now * 10
  70. unit.UnitIntegral = &now
  71. global.GVA_DB.Session(&gorm.Session{
  72. AllowGlobalUpdate: true,
  73. }).Model(&unit).Updates(&autocode.Unit{UnitMaxIntegral: unit.UnitIntegral, UnitIntegral: unit.UnitIntegral, Period: time.Now().Format("20060102150405")})
  74. return err
  75. }
  76. func (unitService *UnitService) UpdateUnitA(unit autocode.Unit) (err error) {
  77. err = global.GVA_DB.Save(&unit).Error
  78. return err
  79. }
  80. // GetUnit 根据id获取Unit记录
  81. // Author [piexlmax](https://github.com/piexlmax)
  82. func (unitService *UnitService) GetUnit(id uint) (err error, unit autocode.Unit) {
  83. err = global.GVA_DB.Where("id = ?", id).First(&unit).Error
  84. return
  85. }
  86. func (unitService *UnitService) GetUnitTop() (err error, unit autocode.Unit) {
  87. top := autocode.Unit{}
  88. err = global.GVA_DB.First(&top).Error
  89. return err, top
  90. }
  91. // GetUnitInfoList 分页获取Unit记录
  92. // Author [piexlmax](https://github.com/piexlmax)
  93. func (unitService *UnitService) GetUnitInfoList(info autoCodeReq.UnitSearch) (err error, list interface{}, total int64) {
  94. limit := info.PageSize
  95. offset := info.PageSize * (info.Page - 1)
  96. // 创建db
  97. db := global.GVA_DB.Model(&autocode.Unit{})
  98. if info.UnitName != "" {
  99. db.Where("unit_name like ?", "%"+info.UnitName+"%")
  100. }
  101. var units []autocode.Unit
  102. // 如果有条件搜索 下方会自动创建搜索语句
  103. err = db.Count(&total).Error
  104. err = db.Limit(limit).Offset(offset).Find(&units).Error
  105. return err, units, total
  106. }
  107. func (unitService *UnitService) GetUnitInfoListAll() (err error, list []autocode.Unit) {
  108. var units []autocode.Unit
  109. // 创建db
  110. err = global.GVA_DB.Model(&autocode.Unit{}).Find(&units).Error
  111. return err, units
  112. }