barcode.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package services
  2. import (
  3. "Medical_ERP/common/global"
  4. db "Medical_ERP/common/initialize"
  5. model2 "Medical_ERP/common/model"
  6. "Medical_ERP/dto"
  7. "Medical_ERP/models"
  8. "errors"
  9. "fmt"
  10. "github.com/beego/beego/v2/core/logs"
  11. "gorm.io/gorm"
  12. "time"
  13. )
  14. type Barcode struct {
  15. }
  16. // getNextIncrementalCode 从数据库中获取下一个递增码
  17. func (e *Barcode) getNextIncrementalCode(deptId int) (string, error) {
  18. // 获取当前日期
  19. var count int64
  20. current := fmt.Sprintf("%05d%s", deptId, time.Now().Format("20060102"))
  21. err := db.DB.Model(&models.Barcode{}).Where("code LIKE ?", current+"%").Count(&count).Error
  22. if err != nil {
  23. logs.Error("db error: %s", err)
  24. return "", err
  25. }
  26. return fmt.Sprintf("%s%04d", current, count+1), nil
  27. }
  28. // Insert 创建Spec对象
  29. func (e *Barcode) Insert(c models.TransportRecord) (code string, err error) {
  30. incrementalCode, err := e.getNextIncrementalCode(c.DeptID)
  31. if err != nil {
  32. logs.Error("生成条形码失败", err)
  33. return code, global.CreateFailedErr
  34. }
  35. data := models.Barcode{Code: incrementalCode, Data: c, ControlBy: model2.ControlBy{CreateBy: c.CreateBy, DeptId: c.DeptId}}
  36. err = db.DB.Create(&data).Error
  37. if err != nil {
  38. logs.Error("db error: %s", err)
  39. return code, global.CreateFailedErr
  40. }
  41. return data.Code, nil
  42. }
  43. // Insert 创建Spec对象
  44. func (e *Barcode) InsertRefundOut(c dto.StockRefundOutExport) (code string, err error) {
  45. incrementalCode, err := e.getNextIncrementalCode(c.DeptId)
  46. if err != nil {
  47. logs.Error("生成条形码失败", err)
  48. return code, global.CreateFailedErr
  49. }
  50. var obj models.TransportRecord
  51. obj.StockOutIds = c.StockOutIds
  52. obj.DeptID = c.DeptId
  53. obj.DeptName = c.DeptName
  54. data := models.Barcode{Code: incrementalCode, Data: obj, ControlBy: model2.ControlBy{CreateBy: obj.CreateBy, DeptId: c.DeptId}}
  55. err = db.DB.Create(&data).Error
  56. if err != nil {
  57. logs.Error("db error: %s", err)
  58. return code, global.CreateFailedErr
  59. }
  60. return data.Code, nil
  61. }
  62. // Get 获取DosageForm对象
  63. func (e *Barcode) Get(d *dto.GetBarCodeInfoReq, barcodeModel *models.Barcode) error {
  64. err := db.DB.Where("code = ?", d.Code).First(barcodeModel).Error
  65. if err != nil {
  66. logs.Error("db error: %s", err)
  67. if errors.Is(err, gorm.ErrRecordNotFound) {
  68. return global.GetNotFoundOrNoPermissionErr
  69. }
  70. return global.GetFailedErr
  71. }
  72. return nil
  73. }