medicine_template.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. // 获取系统初始化字段
  79. func GetMedicineInfoFieldIsSysInit(field string) bool {
  80. switch field {
  81. case "product_id":
  82. return true
  83. case "enterprise_id":
  84. return true
  85. case "spec_id":
  86. return true
  87. case "unit_id":
  88. return true
  89. case "dosage_form_id":
  90. return true
  91. case "batch_number":
  92. return true
  93. case "expiry_date":
  94. return true
  95. case "produced_date":
  96. return true
  97. case "approval_number":
  98. return true
  99. case "qualification_number":
  100. return true
  101. //case "unit_price":
  102. // return true
  103. //case "qrcode":
  104. // return true
  105. default:
  106. return false
  107. }
  108. }
  109. //purchase_unit_price DECIMAL(10,2) comment '购进单价',
  110. //sales_unit_price DECIMAL(10,2) comment '销售单价'
  111. // 获取插入数据sql
  112. func GetInsertMedicineTemplateSql(deptId, typeId int, columnName string) string {
  113. var typeStr string
  114. switch typeId {
  115. case 1, 2, 3, 4, 5:
  116. typeStr = " INT"
  117. case 6:
  118. typeStr = " VARCHAR(256)"
  119. case 7:
  120. typeStr = " INT"
  121. case 8:
  122. typeStr = " DECIMAL(10,2)"
  123. case 9:
  124. typeStr = " DATE"
  125. }
  126. sqlStmt := "ALTER TABLE " + GetMedicineInfoTableName(deptId) + " ADD `" + columnName + "`" + typeStr
  127. logs.Info("InsertMedicineTemplateSql", sqlStmt)
  128. return sqlStmt
  129. }
  130. // 获取更新列名sql
  131. func GetUpdateMedicineTemplateSql(deptId, typeId int, oldColumnName, newColumnName string) string {
  132. var typeStr string
  133. switch typeId {
  134. case 1, 2, 3, 4, 5:
  135. typeStr = " INT"
  136. case 6:
  137. typeStr = " VARCHAR(256)"
  138. case 7:
  139. typeStr = " INT"
  140. case 8:
  141. typeStr = " DECIMAL(10,4)"
  142. case 9:
  143. typeStr = " DATETIME"
  144. }
  145. sqlStmt := "ALTER TABLE " + GetMedicineInfoTableName(deptId) + " CHANGE `" + oldColumnName + "` `" + newColumnName + "`" + typeStr
  146. logs.Info("UpdateMedicineTemplateSql", sqlStmt)
  147. return sqlStmt
  148. }
  149. func GetDeleteMedicineTemplateSql(deptId int, columnName string) string {
  150. sqlStmt := "ALTER TABLE " + GetMedicineInfoTableName(deptId) + " DROP `" + columnName + "`"
  151. logs.Info("DeleteMedicineTemplateSql", sqlStmt)
  152. return sqlStmt
  153. }
  154. // type 说明 英文名称
  155. // 1 产品名称 ProductID
  156. // 2 生产企业 EnterpriseID
  157. // 3 规格 SpecID
  158. // 4 剂型 DosageFormID
  159. // 5 单位 UnitID
  160. // 6 文本
  161. // 7 数值(整数)
  162. // 8 数值(小数)
  163. // 9 日期
  164. // 批量修改药品信息表结构
  165. func BatchAlterMedicineTemplateTable() {
  166. // 删除表内字段
  167. var tableName []string
  168. err := db.DB.Raw("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME LIKE 'medicine_info_%';\n").
  169. Scan(&tableName).Error
  170. if err != nil {
  171. logs.Error("db error: %s", err)
  172. }
  173. tx := db.DB.Begin()
  174. for _, v := range tableName {
  175. sql := "ALTER TABLE " + v + " ADD COLUMN qrcode VARCHAR(256)"
  176. err = tx.Exec(sql).Error
  177. if err != nil {
  178. if err.(*mysql.MySQLError).Message == "Duplicate column name 'qrcode'" {
  179. continue
  180. }
  181. tx.Rollback()
  182. logs.Error("修改表结构失败: %s", err.Error())
  183. break
  184. }
  185. }
  186. tx.Commit()
  187. }