medicine_template.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. package services
  2. import (
  3. "Medical_ERP/common/actions"
  4. "Medical_ERP/common/global"
  5. db "Medical_ERP/common/initialize"
  6. "Medical_ERP/common/translate"
  7. "Medical_ERP/dto"
  8. "Medical_ERP/models"
  9. "Medical_ERP/utils"
  10. "errors"
  11. "github.com/beego/beego/v2/core/logs"
  12. "gorm.io/gorm"
  13. )
  14. type MedicineTemplate struct {
  15. }
  16. // 初始化药品信息
  17. func (e *MedicineTemplate) InitMedicineTemplate(deptId, userId int) error {
  18. tx := db.DB.Begin()
  19. // 1、创建模板
  20. initMedicineTemplateInfoList := models.GetInitMedicineTemplateInfo(deptId, userId)
  21. err := tx.Create(&initMedicineTemplateInfoList).Error
  22. if err != nil {
  23. tx.Rollback()
  24. logs.Error("db error: %s", err)
  25. return global.CreateFailedErr
  26. }
  27. err = tx.Exec(models.GetInitMedicineTemplateSql(deptId)).Error
  28. if err != nil {
  29. tx.Rollback()
  30. logs.Error("db error: %s", err)
  31. return global.CreateFailedErr
  32. }
  33. tx.Commit()
  34. return nil
  35. }
  36. // GetPage 获取MedicineTemplate列表
  37. func (e *MedicineTemplate) GetPage(list *[]models.MedicineTemplate, count *int64, p *actions.DataPermission) error {
  38. var err error
  39. var data models.MedicineTemplate
  40. err = db.DB.Model(&data).
  41. Scopes(
  42. actions.Permission(data.TableName(), p),
  43. ).
  44. Order("sort").
  45. Find(list).Limit(-1).Offset(-1).
  46. Count(count).Error
  47. if err != nil {
  48. logs.Error("db error: %s ", err)
  49. return global.GetFailedErr
  50. }
  51. return nil
  52. }
  53. func (e *MedicineTemplate) ListForStock(list *[]dto.MedicineTemplateForStockRes, count *int64, p *actions.DataPermission) error {
  54. var err error
  55. var data models.MedicineTemplate
  56. err = db.DB.Model(&data).
  57. Scopes(
  58. actions.Permission(data.TableName(), p),
  59. ).
  60. Order("sort").
  61. Find(list).Limit(-1).Offset(-1).
  62. Count(count).Error
  63. if err != nil {
  64. logs.Error("db error: %s ", err)
  65. return global.GetFailedErr
  66. }
  67. for i := 0; i < len(*list); i++ {
  68. switch (*list)[i].Type {
  69. case 1:
  70. var product []models.Product
  71. err = db.DB.Model(&models.Product{}).Where("dept_id = ?", p.DeptId).Find(&product).Error
  72. (*list)[i].List = product
  73. case 2:
  74. var enterprise []models.Enterprise
  75. err = db.DB.Model(&models.Enterprise{}).Where("dept_id = ?", p.DeptId).Find(&enterprise).Error
  76. (*list)[i].List = enterprise
  77. case 3:
  78. var spec []models.Spec
  79. err = db.DB.Model(&models.Spec{}).Where("dept_id = ?", p.DeptId).Find(&spec).Error
  80. (*list)[i].List = spec
  81. case 4:
  82. var unit []models.Unit
  83. err = db.DB.Model(&models.Unit{}).Where("dept_id = ?", p.DeptId).Find(&unit).Error
  84. (*list)[i].List = unit
  85. case 5:
  86. var dosageForm []models.DosageForm
  87. err = db.DB.Model(&models.DosageForm{}).Where("dept_id = ?", p.DeptId).Find(&dosageForm).Error
  88. (*list)[i].List = dosageForm
  89. default:
  90. (*list)[i].List = nil
  91. }
  92. }
  93. return nil
  94. }
  95. func (e *MedicineTemplate) ListForStockInScanCode(res *[]dto.MedicineTemplateForStockRes, count *int64, p *actions.DataPermission) error {
  96. var err error
  97. var data models.MedicineTemplate
  98. var list []dto.MedicineTemplateForStockRes
  99. err = db.DB.Model(&data).
  100. Scopes(
  101. actions.Permission(data.TableName(), p),
  102. ).
  103. Order("sort").
  104. Find(&list).Limit(-1).Offset(-1).
  105. Count(count).Error
  106. if err != nil {
  107. logs.Error("db error: %s ", err)
  108. return global.GetFailedErr
  109. }
  110. for i := 0; i < len(list); i++ {
  111. switch list[i].Type {
  112. case 1:
  113. list[i].Type = 6
  114. list[i].FieldName = models.FieldProductName
  115. case 2:
  116. list[i].Type = 6
  117. list[i].FieldName = models.FieldEnterpriseName
  118. case 3:
  119. list[i].Type = 6
  120. list[i].FieldName = models.FieldSpecName
  121. case 4:
  122. list[i].Type = 6
  123. list[i].FieldName = models.FieldDosageFormName
  124. //case 5:
  125. // (*list)[i].Type = 6
  126. // (*list)[i].FieldName = models.FieldUnitName
  127. }
  128. }
  129. for i, v := range list {
  130. if list[i].FieldName != models.FieldProductName &&
  131. list[i].FieldName != models.FieldEnterpriseName &&
  132. list[i].FieldName != models.FieldDosageFormName &&
  133. list[i].FieldName != models.FieldSpecName &&
  134. list[i].FieldName != models.FieldBatchNumber &&
  135. list[i].FieldName != models.FieldExpiryDate &&
  136. list[i].FieldName != models.FieldApprovalNumber &&
  137. list[i].FieldName != models.FieldProducedDate {
  138. continue
  139. }
  140. *res = append(*res, v)
  141. }
  142. *count = int64(len(*res))
  143. return nil
  144. }
  145. func (e *MedicineTemplate) ListForStockOut(res *[]dto.MedicineTemplateForStockRes, count *int64, p *actions.DataPermission) error {
  146. var err error
  147. var data models.MedicineTemplate
  148. var list []dto.MedicineTemplateForStockRes
  149. err = db.DB.Model(&data).
  150. Scopes(actions.Permission(data.TableName(), p)).
  151. Order("sort").
  152. Find(&list).Error
  153. if err != nil {
  154. logs.Error("db error: %s ", err)
  155. return global.GetFailedErr
  156. }
  157. for i := 0; i < len(list); i++ {
  158. switch list[i].Type {
  159. case 1:
  160. var product []models.Product
  161. err = db.DB.Model(&models.Product{}).Where("dept_id = ?", p.DeptId).Find(&product).Error
  162. list[i].List = product
  163. case 2:
  164. var enterprise []models.Enterprise
  165. err = db.DB.Model(&models.Enterprise{}).Where("dept_id = ?", p.DeptId).Find(&enterprise).Error
  166. list[i].List = enterprise
  167. case 3:
  168. var spec []models.Spec
  169. err = db.DB.Model(&models.Spec{}).Where("dept_id = ?", p.DeptId).Find(&spec).Error
  170. list[i].List = spec
  171. //case 4:
  172. // var dosageForm []models.DosageForm
  173. // err = db.DB.Model(&models.DosageForm{}).Where("dept_id = ?", p.DeptId).Find(&dosageForm).Error
  174. // list[i].List = dosageForm
  175. //case 5:
  176. // var unit []models.Unit
  177. // err = db.DB.Model(&models.Unit{}).Where("dept_id = ?", p.DeptId).Find(&unit).Error
  178. // list[i].List = unit
  179. default:
  180. list[i].List = nil
  181. }
  182. }
  183. for i, v := range list {
  184. if list[i].FieldName != models.FieldProductID &&
  185. list[i].FieldName != models.FieldEnterpriseID &&
  186. list[i].FieldName != models.FieldSpecID &&
  187. list[i].FieldName != models.FieldBatchNumber {
  188. continue
  189. }
  190. *res = append(*res, v)
  191. }
  192. *count = int64(len(*res))
  193. return nil
  194. }
  195. func (e *MedicineTemplate) ListForStockOutScanCode(res *[]dto.MedicineTemplateForStockRes, count *int64, p *actions.DataPermission) error {
  196. var err error
  197. var data models.MedicineTemplate
  198. var list []dto.MedicineTemplateForStockRes
  199. err = db.DB.Model(&data).
  200. Scopes(actions.Permission(data.TableName(), p)).
  201. Order("sort").
  202. Find(&list).Error
  203. if err != nil {
  204. logs.Error("db error: %s ", err)
  205. return global.GetFailedErr
  206. }
  207. for i := 0; i < len(list); i++ {
  208. switch list[i].Type {
  209. case 1:
  210. list[i].Type = 6
  211. list[i].FieldName = models.FieldProductName
  212. case 2:
  213. list[i].Type = 6
  214. list[i].FieldName = models.FieldEnterpriseName
  215. case 3:
  216. list[i].Type = 6
  217. list[i].FieldName = models.FieldSpecName
  218. }
  219. }
  220. for i, v := range list {
  221. if list[i].FieldName != models.FieldProductName &&
  222. list[i].FieldName != models.FieldEnterpriseName &&
  223. list[i].FieldName != models.FieldSpecName &&
  224. list[i].FieldName != models.FieldBatchNumber {
  225. continue
  226. }
  227. *res = append(*res, v)
  228. }
  229. *count = int64(len(*res))
  230. return nil
  231. }
  232. // Insert 创建MedicineTemplate对象
  233. func (e *MedicineTemplate) Insert(c *dto.MedicineTemplateInsertReq) error {
  234. var err error
  235. var data models.MedicineTemplate
  236. err = db.DB.Where("name = ? AND dept_id = ?", c.Name, c.DeptId).Find(&data).Error
  237. if data.Id > 0 {
  238. return errors.New("标签名称已存在")
  239. }
  240. if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
  241. logs.Error("db error: %s", err)
  242. return global.CreateFailedErr
  243. }
  244. c.Generate(&data)
  245. data.State = 2 // 用户添加
  246. en, err := translate.Translate(c.Name)
  247. if err != nil {
  248. logs.Error("火山引擎翻译出错: %s", err)
  249. return errors.New("火山引擎出错,请联系管理员")
  250. }
  251. data.FieldName = utils.WordsToSnake(en)
  252. tx := db.DB.Begin()
  253. err = tx.Create(&data).Error
  254. if err != nil {
  255. tx.Rollback()
  256. logs.Error("db error: %s", err)
  257. return global.CreateFailedErr
  258. }
  259. // 向表内新增字段
  260. err = tx.Exec(models.GetInsertMedicineTemplateSql(c.DeptId, c.Type, data.FieldName)).Error
  261. if err != nil {
  262. tx.Rollback()
  263. logs.Error("db error: %s", err)
  264. return global.CreateFailedErr
  265. }
  266. tx.Commit()
  267. c.Id = data.Id
  268. return nil
  269. }
  270. // Update 修改MedicineTemplate对象
  271. func (e *MedicineTemplate) Update(c *dto.MedicineTemplateUpdateReq) error {
  272. var mtModel = models.MedicineTemplate{}
  273. err := db.DB.First(&mtModel, c.GetId()).Error
  274. if err != nil {
  275. logs.Error("db error: %s", err)
  276. if errors.Is(err, gorm.ErrRecordNotFound) {
  277. return global.UpdateNotFoundOrNoPermissionErr
  278. }
  279. return global.UpdateFailedErr
  280. }
  281. tx := db.DB.Begin()
  282. // 修改列名 系统初始化的不能修改字段名
  283. if mtModel.Name != c.Name && mtModel.State != 1 {
  284. en, err := translate.Translate(c.Name)
  285. if err != nil {
  286. tx.Rollback()
  287. logs.Error("火山引擎翻译出错: %s", err)
  288. return errors.New("火山引擎出错,请联系管理员")
  289. }
  290. newFieldName := utils.WordsToSnake(en)
  291. err = tx.Exec(models.GetUpdateMedicineTemplateSql(mtModel.DeptId, mtModel.Type, mtModel.FieldName, newFieldName)).Error
  292. if err != nil {
  293. tx.Rollback()
  294. logs.Error("db error: %s", err)
  295. return global.CreateFailedErr
  296. }
  297. mtModel.FieldName = newFieldName
  298. }
  299. c.Generate(&mtModel)
  300. err = tx.Save(&mtModel).Error
  301. if err != nil {
  302. tx.Rollback()
  303. logs.Error("db error: %s", err)
  304. return global.UpdateFailedErr
  305. }
  306. tx.Commit()
  307. return nil
  308. }
  309. // Remove 删除MedicineTemplate
  310. func (e *MedicineTemplate) Remove(d *dto.MedicineTemplateDeleteReq) error {
  311. var err error
  312. var mt models.MedicineTemplate
  313. var data models.MedicineTemplate
  314. err = db.DB.First(&mt, d.GetId()).Error
  315. if err != nil {
  316. logs.Error("db error: %s", err)
  317. if errors.Is(err, gorm.ErrRecordNotFound) {
  318. return global.DeleteNotFoundOrNoPermissionErr
  319. }
  320. return global.DeleteFailedErr
  321. }
  322. tx := db.DB.Begin()
  323. result := tx.Model(&data).Delete(&data, d.GetId())
  324. if err = result.Error; err != nil {
  325. tx.Rollback()
  326. logs.Error("Delete error: %s", err)
  327. return global.DeleteFailedErr
  328. }
  329. // 删除表内字段
  330. err = tx.Exec(models.GetDeleteMedicineTemplateSql(mt.DeptId, mt.FieldName)).Error
  331. if err != nil {
  332. tx.Rollback()
  333. logs.Error("db error: %s", err)
  334. return global.CreateFailedErr
  335. }
  336. tx.Commit()
  337. return nil
  338. }
  339. // GetColumns 获取前端渲染表格-表头
  340. func (e *MedicineTemplate) GetColumns(columnsList *[]dto.MedicineColumnsRes, p *actions.DataPermission) error {
  341. var err error
  342. var data models.MedicineTemplate
  343. var list []models.MedicineTemplate
  344. err = db.DB.Model(&data).
  345. Scopes(
  346. actions.Permission(data.TableName(), p),
  347. ).
  348. Where("`show` = 1").
  349. Order("sort").
  350. Find(&list).Error
  351. if err != nil {
  352. logs.Error("db error: %s ", err)
  353. return global.GetFailedErr
  354. }
  355. *columnsList = append(*columnsList, dto.MedicineColumnsRes{
  356. Label: "条码",
  357. Prop: "qrcode",
  358. Width: 200,
  359. })
  360. for _, v := range list {
  361. fieldName := v.FieldName
  362. switch v.FieldName {
  363. case models.FieldProductID:
  364. fieldName = models.FieldProductName
  365. case models.FieldEnterpriseID:
  366. fieldName = models.FieldEnterpriseName
  367. case models.FieldSpecID:
  368. fieldName = models.FieldSpecName
  369. case models.FieldUnitID:
  370. fieldName = models.FieldUnitName
  371. case models.FieldDosageFormID:
  372. fieldName = models.FieldDosageFormName
  373. }
  374. *columnsList = append(*columnsList, dto.MedicineColumnsRes{
  375. Label: v.Name,
  376. Prop: fieldName,
  377. Width: v.Width,
  378. })
  379. }
  380. return nil
  381. }