medicine_template.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package models
  2. import (
  3. db "Medical_ERP/common/initialize"
  4. model2 "Medical_ERP/common/model"
  5. "github.com/beego/beego/v2/core/logs"
  6. "github.com/go-sql-driver/mysql"
  7. "strconv"
  8. )
  9. const (
  10. FieldProductID = "product_id" // 品种ID
  11. FieldEnterpriseID = "enterprise_id" // 生产企业ID
  12. FieldSpecID = "spec_id" // 规格ID
  13. FieldUnitID = "unit_id" // 单位id
  14. FieldDosageFormID = "dosage_form_id" // 剂型id
  15. FieldBatchNumber = "batch_number" // 批号
  16. FieldExpiryDate = "expiry_date" // 失效日期
  17. FieldProducedDate = "produced_date" // 生产日期
  18. FieldApprovalNumber = "approval_number" // 批准文号
  19. FieldQualificationNumber = "qualification_number" // 批签发合格编号
  20. FieldProductName = "product_name"
  21. FieldEnterpriseName = "enterprise_name"
  22. FieldSpecName = "spec_name"
  23. FieldUnitName = "unit_name"
  24. FieldDosageFormName = "dosage_form_name"
  25. )
  26. func GetMedicineInfoTableName(deptId int) string {
  27. return "medicine_info_" + strconv.Itoa(deptId)
  28. }
  29. type MedicineTemplate struct {
  30. model2.Model
  31. Type int `json:"type" gorm:"size:11;"` // 数据类型
  32. Name string `json:"name" gorm:"size:32;"` // 标签名称
  33. FieldName string `json:"field_name" gorm:"size:128;"` // 英语名称
  34. Text string `json:"text" gorm:"size:256;"` // 描述
  35. Sort int `json:"sort" gorm:"size:11;"` // 排序
  36. Width int `json:"width" gorm:"size:11;"` // 宽度
  37. Show int `json:"show" gorm:"size:11;"` // 1-显示 2-隐藏
  38. State int `json:"state" gorm:"size:11;"` // 1-系统初始化 2-用户添加
  39. model2.ControlBy
  40. model2.ModelTime
  41. }
  42. func (e *MedicineTemplate) TableName() string {
  43. return "medicine_template"
  44. }
  45. func GetInitMedicineTemplateInfo(deptId, CreateBy int) []MedicineTemplate {
  46. return []MedicineTemplate{
  47. {Type: 1, Name: "品名", FieldName: FieldProductID, Show: 1, State: 1, Sort: -9, ControlBy: model2.ControlBy{CreateBy: CreateBy, DeptId: deptId}},
  48. {Type: 2, Name: "生产企业", FieldName: FieldEnterpriseID, Show: 1, State: 1, Sort: -8, ControlBy: model2.ControlBy{CreateBy: CreateBy, DeptId: deptId}},
  49. {Type: 3, Name: "规格", FieldName: FieldSpecID, Show: 1, State: 1, Sort: -7, ControlBy: model2.ControlBy{CreateBy: CreateBy, DeptId: deptId}},
  50. {Type: 6, Name: "批号", FieldName: FieldBatchNumber, Show: 1, State: 1, Sort: -6, ControlBy: model2.ControlBy{CreateBy: CreateBy, DeptId: deptId}},
  51. {Type: 9, Name: "失效日期", FieldName: FieldExpiryDate, Show: 1, State: 1, Sort: -5, ControlBy: model2.ControlBy{CreateBy: CreateBy, DeptId: deptId}},
  52. {Type: 9, Name: "生产日期", FieldName: FieldProducedDate, Show: 1, State: 1, Sort: -5, ControlBy: model2.ControlBy{CreateBy: CreateBy, DeptId: deptId}},
  53. {Type: 6, Name: "批准文号", FieldName: FieldApprovalNumber, Show: 1, State: 1, Sort: -4, ControlBy: model2.ControlBy{CreateBy: CreateBy, DeptId: deptId}},
  54. {Type: 5, Name: "剂型", FieldName: FieldDosageFormID, Show: 1, State: 1, Sort: -3, ControlBy: model2.ControlBy{CreateBy: CreateBy, DeptId: deptId}},
  55. {Type: 4, Name: "单位", FieldName: FieldUnitID, Show: 1, State: 1, Sort: -2, ControlBy: model2.ControlBy{CreateBy: CreateBy, DeptId: deptId}},
  56. {Type: 6, Name: "批签发编号", FieldName: FieldQualificationNumber, Show: 1, State: 1, Sort: -1, ControlBy: model2.ControlBy{CreateBy: CreateBy, DeptId: deptId}},
  57. }
  58. }
  59. func GetInitMedicineTemplateSql(deptId int) string {
  60. sqlStmt := `
  61. CREATE TABLE IF NOT EXISTS ` + GetMedicineInfoTableName(deptId) + ` (
  62. id int primary key auto_increment comment '自增ID',
  63. product_id INT comment '品名ID',
  64. enterprise_id INT comment '生产企业ID',
  65. spec_id INT comment '规格ID',
  66. unit_id INT comment '单位ID',
  67. dosage_form_id INT comment '剂型ID',
  68. batch_number VARCHAR(256) comment '批号',
  69. expiry_date DATE comment '失效日期',
  70. produced_date DATE comment '生产日期',
  71. approval_number VARCHAR(256) comment '批准文号',
  72. qualification_number VARCHAR(256) comment '批签发编号',
  73. unit_price DECIMAL(10,2) comment '单价',
  74. qrcode VARCHAR(256) comment '追溯码'
  75. );`
  76. return sqlStmt
  77. }
  78. //purchase_unit_price DECIMAL(10,2) comment '购进单价',
  79. //sales_unit_price DECIMAL(10,2) comment '销售单价'
  80. // 获取插入数据sql
  81. func GetInsertMedicineTemplateSql(deptId, typeId int, columnName string) string {
  82. var typeStr string
  83. switch typeId {
  84. case 1, 2, 3, 4, 5:
  85. typeStr = " INT"
  86. case 6:
  87. typeStr = " VARCHAR(256)"
  88. case 7:
  89. typeStr = " INT"
  90. case 8:
  91. typeStr = " DECIMAL(10,2)"
  92. case 9:
  93. typeStr = " DATE"
  94. }
  95. sqlStmt := "ALTER TABLE " + GetMedicineInfoTableName(deptId) + " ADD `" + columnName + "`" + typeStr
  96. logs.Info("InsertMedicineTemplateSql", sqlStmt)
  97. return sqlStmt
  98. }
  99. // 获取更新列名sql
  100. func GetUpdateMedicineTemplateSql(deptId, typeId int, oldColumnName, newColumnName string) string {
  101. var typeStr string
  102. switch typeId {
  103. case 1, 2, 3, 4, 5:
  104. typeStr = " INT"
  105. case 6:
  106. typeStr = " VARCHAR(256)"
  107. case 7:
  108. typeStr = " INT"
  109. case 8:
  110. typeStr = " DECIMAL(10,4)"
  111. case 9:
  112. typeStr = " DATETIME"
  113. }
  114. sqlStmt := "ALTER TABLE " + GetMedicineInfoTableName(deptId) + " CHANGE `" + oldColumnName + "` `" + newColumnName + "`" + typeStr
  115. logs.Info("UpdateMedicineTemplateSql", sqlStmt)
  116. return sqlStmt
  117. }
  118. func GetDeleteMedicineTemplateSql(deptId int, columnName string) string {
  119. sqlStmt := "ALTER TABLE " + GetMedicineInfoTableName(deptId) + " DROP `" + columnName + "`"
  120. logs.Info("DeleteMedicineTemplateSql", sqlStmt)
  121. return sqlStmt
  122. }
  123. // type 说明 英文名称
  124. // 1 产品名称 ProductID
  125. // 2 生产企业 EnterpriseID
  126. // 3 规格 SpecID
  127. // 4 剂型 DosageFormID
  128. // 5 单位 UnitID
  129. // 6 文本
  130. // 7 数值(整数)
  131. // 8 数值(小数)
  132. // 9 日期
  133. // 批量修改药品信息表结构
  134. func BatchAlterMedicineTemplateTable() {
  135. // 删除表内字段
  136. var tableName []string
  137. err := db.DB.Raw("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME LIKE 'medicine_info_%';\n").
  138. Scan(&tableName).Error
  139. if err != nil {
  140. logs.Error("db error: %s", err)
  141. }
  142. tx := db.DB.Begin()
  143. for _, v := range tableName {
  144. sql := "ALTER TABLE " + v + " ADD COLUMN qrcode VARCHAR(256)"
  145. err = tx.Exec(sql).Error
  146. if err != nil {
  147. if err.(*mysql.MySQLError).Message == "Duplicate column name 'qrcode'" {
  148. continue
  149. }
  150. tx.Rollback()
  151. logs.Error("修改表结构失败: %s", err.Error())
  152. break
  153. }
  154. }
  155. tx.Commit()
  156. }