medicine_img.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package services
  2. import (
  3. "Medical_ERP/common/actions"
  4. cDto "Medical_ERP/common/dto"
  5. "Medical_ERP/common/global"
  6. db "Medical_ERP/common/initialize"
  7. "Medical_ERP/conf"
  8. "Medical_ERP/dto"
  9. "Medical_ERP/models"
  10. "Medical_ERP/utils"
  11. "errors"
  12. "fmt"
  13. "github.com/beego/beego/v2/adapter/context"
  14. "github.com/beego/beego/v2/core/logs"
  15. "github.com/go-resty/resty/v2"
  16. "github.com/tidwall/gjson"
  17. "gorm.io/gorm"
  18. "net/http"
  19. "strings"
  20. )
  21. type MedicineImg struct {
  22. }
  23. // MedicineImg 通过名称、生产企业、规格、批号查询药品图片,
  24. // 如果未查询到,则查询根公司图片信息
  25. func (e *MedicineImg) Get(deptId, rootId int, product, enterprise, spec, batchNumber string) (medicineImg models.MedicineImg, err error) {
  26. medicineImgList := make([]models.MedicineImg, 0)
  27. err = db.DB.Where("product = ? AND enterprise = ? AND spec = ? AND batch_number = ? AND dept_id in ? AND img != ''",
  28. product, enterprise, spec, batchNumber, []int{deptId, rootId}).Find(&medicineImgList).Error
  29. if err != nil {
  30. logs.Error("db error: %s ", err)
  31. return medicineImg, global.GetFailedErr
  32. }
  33. if len(medicineImgList) == 0 {
  34. return medicineImg, global.GetNotFoundErr
  35. }
  36. if len(medicineImgList) == 1 {
  37. return medicineImgList[0], nil
  38. }
  39. if len(medicineImgList) == 2 {
  40. for _, img := range medicineImgList {
  41. if img.DeptId == deptId {
  42. return img, nil
  43. }
  44. }
  45. }
  46. return medicineImg, nil
  47. }
  48. // GetPage 获取Spec列表
  49. func (e *MedicineImg) GetPage(c *dto.MedicineImgPageReq, list *[]models.MedicineImg, count *int64, rootId int, p *actions.DataPermission) error {
  50. var err error
  51. var data models.MedicineImg
  52. err = db.DB.Model(&data).
  53. Scopes(
  54. cDto.MakeCondition(c.GetNeedSearch()),
  55. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  56. actions.Permission(data.TableName(), p),
  57. ).
  58. Find(list).Limit(-1).Offset(-1).
  59. Count(count).Error
  60. if err != nil {
  61. logs.Error("db error: %s ", err)
  62. return global.GetFailedErr
  63. }
  64. for i := 0; i < len(*list); i++ {
  65. if len((*list)[i].Img) == 0 {
  66. img, _ := e.Get((*list)[i].DeptId, rootId, (*list)[i].Product, (*list)[i].Enterprise, (*list)[i].Spec, (*list)[i].BatchNumber)
  67. (*list)[i].Img = img.Img
  68. }
  69. }
  70. return nil
  71. }
  72. // Update 修改Spec对象
  73. func (e *MedicineImg) Update(c *dto.MedicineImgUpdateReq) error {
  74. var medicineImg = models.MedicineImg{}
  75. err := db.DB.First(&medicineImg, c.GetId()).Error
  76. if err != nil {
  77. logs.Error("db error: %s", err)
  78. if errors.Is(err, gorm.ErrRecordNotFound) {
  79. return global.GetNotFoundErr
  80. }
  81. return global.GetFailedErr
  82. }
  83. c.Generate(&medicineImg)
  84. err = db.DB.Save(&medicineImg).Error
  85. if err != nil {
  86. logs.Error("db error: %s", err)
  87. return global.UpdateFailedErr
  88. }
  89. return nil
  90. }
  91. func (e *MedicineImg) GetRootID(c *context.Context, rootId int) (id int, err error) {
  92. reqPath := fmt.Sprintf("/api/dept/%d", rootId)
  93. url := conf.OAuthBaseUrl + reqPath
  94. r, reqError := resty.New().R().SetHeaders(map[string]string{
  95. "Authorization": c.Request.Header.Get("Authorization"),
  96. }).Get(url)
  97. if reqError != nil {
  98. err = errors.New("获取根公司失败")
  99. logs.Error(err.Error(), reqError)
  100. return
  101. }
  102. code := gjson.GetBytes(r.Body(), "code").Int()
  103. deptPath := gjson.GetBytes(r.Body(), "data.deptPath").String()
  104. if code != http.StatusOK {
  105. err = errors.New("获取根公司失败")
  106. logs.Error(err.Error(), string(r.Body()))
  107. return
  108. }
  109. list := strings.Split(strings.Trim(deptPath, "/"), "/")
  110. if len(list) > 1 {
  111. id = utils.ToInt(list[1])
  112. }
  113. return
  114. }