init.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package models
  2. import (
  3. db "Medical_ERP/common/initialize"
  4. "fmt"
  5. _ "github.com/go-sql-driver/mysql"
  6. "log"
  7. "sync"
  8. )
  9. func init() {
  10. AutoMigrateDB()
  11. }
  12. func AutoMigrateDB() {
  13. //自动迁移模式
  14. err := db.DB.Set("gorm:table_options", "charset=utf8mb4").
  15. AutoMigrate(
  16. &OperationLog{},
  17. &RoleApi{},
  18. &Product{},
  19. &Enterprise{},
  20. &Spec{},
  21. &DosageForm{},
  22. &Unit{},
  23. &MedicineTemplate{},
  24. &StockIn{},
  25. &StockOut{},
  26. &MedicineInventory{},
  27. &Barcode{},
  28. &MedicineImg{},
  29. &ProductCode{},
  30. )
  31. if err != nil {
  32. log.Fatalf("migrate db fail: %v", err)
  33. }
  34. }
  35. var Spec_list *sync.Map
  36. var DosageForm_list *sync.Map
  37. var Enterprise_list *sync.Map
  38. var Product_list *sync.Map
  39. var Unit_list *sync.Map
  40. // 初始化基础数据
  41. func InitBasicData(deptId int) {
  42. Spec_list = new(sync.Map)
  43. DosageForm_list = new(sync.Map)
  44. Enterprise_list = new(sync.Map)
  45. Product_list = new(sync.Map)
  46. Unit_list = new(sync.Map)
  47. Read_Spec_All_Map(deptId)
  48. Read_Product_All_Map(deptId)
  49. Read_DosageForm_All_Map(deptId)
  50. Read_Enterprise_All_Map(deptId)
  51. Read_Unit_All_Map(deptId)
  52. }
  53. // 获取全部规格
  54. func Read_Spec_All_Map(deptId int) {
  55. var spec []Spec
  56. if err := db.DB.Where("dept_id = ?", deptId).Find(&spec).Error; err != nil {
  57. fmt.Println("Error loading Specs:", err)
  58. return
  59. }
  60. for _, v := range spec {
  61. Spec_list.Store(v.Id, v.Name)
  62. }
  63. }
  64. func Read_Spec_Get(Id int) string {
  65. v, ok := Spec_list.Load(Id)
  66. if ok {
  67. return v.(string)
  68. } else {
  69. return ""
  70. }
  71. }
  72. // 获取全部剂型
  73. func Read_DosageForm_All_Map(deptId int) {
  74. var dosageForm []DosageForm
  75. if err := db.DB.Where("dept_id = ?", deptId).Find(&dosageForm).Error; err != nil {
  76. fmt.Println("Error loading DosageForm:", err)
  77. return
  78. }
  79. for _, v := range dosageForm {
  80. DosageForm_list.Store(v.Id, v.Name)
  81. }
  82. }
  83. func Read_DosageForm_Get(Id int) string {
  84. v, ok := DosageForm_list.Load(Id)
  85. if ok {
  86. return v.(string)
  87. } else {
  88. return ""
  89. }
  90. }
  91. // 获取全部生产企业
  92. func Read_Enterprise_All_Map(deptId int) {
  93. var enterprise []Enterprise
  94. if err := db.DB.Where("dept_id = ?", deptId).Find(&enterprise).Error; err != nil {
  95. fmt.Println("Error loading Enterprise:", err)
  96. return
  97. }
  98. for _, v := range enterprise {
  99. Enterprise_list.Store(v.Id, v.Name)
  100. }
  101. }
  102. func Read_Enterprise_Get(Id int) string {
  103. v, ok := Enterprise_list.Load(Id)
  104. if ok {
  105. return v.(string)
  106. } else {
  107. return ""
  108. }
  109. }
  110. // 获取全部品名
  111. func Read_Product_All_Map(deptId int) {
  112. var product []Product
  113. if err := db.DB.Where("dept_id = ?", deptId).Find(&product).Error; err != nil {
  114. fmt.Println("Error loading Product:", err)
  115. return
  116. }
  117. for _, v := range product {
  118. Product_list.Store(v.Id, v.Name)
  119. }
  120. }
  121. func Read_Product_Get(Id int) string {
  122. v, ok := Product_list.Load(Id)
  123. if ok {
  124. return v.(string)
  125. } else {
  126. return ""
  127. }
  128. }
  129. // 获取全部单位
  130. func Read_Unit_All_Map(deptId int) {
  131. var unit []Unit
  132. if err := db.DB.Where("dept_id = ?", deptId).Find(&unit).Error; err != nil {
  133. fmt.Println("Error loading Unit:", err)
  134. return
  135. }
  136. for _, v := range unit {
  137. Unit_list.Store(v.Id, v.Name)
  138. }
  139. }
  140. func Read_Unit_Get(Id int) string {
  141. v, ok := Unit_list.Load(Id)
  142. if ok {
  143. return v.(string)
  144. } else {
  145. return ""
  146. }
  147. }