medicine.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package controllers
  2. import (
  3. "Medical_ERP/common/global"
  4. "Medical_ERP/common/redis"
  5. _ "Medical_ERP/common/response"
  6. "Medical_ERP/conf"
  7. "Medical_ERP/dto"
  8. "Medical_ERP/models"
  9. "Medical_ERP/services"
  10. "encoding/json"
  11. "errors"
  12. "fmt"
  13. beego "github.com/beego/beego/v2/server/web"
  14. "github.com/go-resty/resty/v2"
  15. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/beegouser"
  16. "strings"
  17. "time"
  18. )
  19. type MedicineController struct {
  20. BaseController
  21. }
  22. // BatchNumber 批号列表
  23. // @Summary 批号列表
  24. // @Description 批号列表
  25. // @Tags 药品
  26. // @Param data body dto.MedicineBatchNumberReq true "body"
  27. // @Success 200 {object} response.Page "{"code": 200, "data": [...]}"
  28. // @Router /medicine/batch-number [post]
  29. // @Security Bearer
  30. func (c MedicineController) BatchNumber() {
  31. s := services.Medicine{}
  32. reqData := dto.MedicineBatchNumberReq{}
  33. if err := c.ParseAndValidate(&c.Ctx.Input.RequestBody, &reqData); err != nil {
  34. err = errors.New("解析表单数据异常")
  35. c.Error(global.ParseFormErr, err, err.Error())
  36. return
  37. }
  38. list := make([]string, 0)
  39. if reqData.SpecID == 0 || reqData.EnterpriseID == 0 || reqData.ProductID == 0 {
  40. c.OK(list, "查询成功")
  41. return
  42. }
  43. err := s.GetBatchNumber(&reqData, &list, beegouser.GetDeptId(c.Ctx))
  44. if err != nil {
  45. c.Error(global.BadRequest, err, err.Error())
  46. return
  47. }
  48. c.OK(list, "查询成功")
  49. }
  50. // BasicDataStat 基本数据统计
  51. // @Summary 基本数据统计
  52. // @Description 基本数据统计
  53. // @Tags 药品
  54. // @Success 200 {object} response.Page "{"code": 200, "data": [...]}"
  55. // @Router /medicine/basic-data-stat [post]
  56. // @Security Bearer
  57. func (c MedicineController) BasicDataStat() {
  58. s := services.Medicine{}
  59. list, err := s.BasicDataStat(beegouser.GetDeptId(c.Ctx))
  60. if err != nil {
  61. c.Error(global.BadRequest, err, err.Error())
  62. return
  63. }
  64. c.OK(list, "查询成功")
  65. }
  66. // 码上放心药品查询
  67. func (c MedicineController) MSFXQuery() {
  68. code := c.GetString("code")
  69. if len(code) == 0 {
  70. err := errors.New("码上放心追溯码不能为空")
  71. c.Error(400, err, err.Error())
  72. return
  73. }
  74. if strings.HasPrefix(code, "9") {
  75. // 宝智达溯码平台
  76. type Res struct {
  77. Code int
  78. Msg string
  79. Data struct {
  80. CodeNum string
  81. CreateTime string
  82. Data map[string]interface{}
  83. }
  84. }
  85. var res Res
  86. client := resty.New()
  87. resp, err := client.R().Get(conf.BZD_pcodeUrl + code)
  88. if err != nil {
  89. c.Error(400, err, err.Error())
  90. return
  91. }
  92. if err = json.Unmarshal(resp.Body(), &res); err != nil {
  93. c.Error(400, err, err.Error())
  94. return
  95. }
  96. if res.Code != 200 {
  97. err = errors.New(res.Msg)
  98. c.Error(400, err, err.Error())
  99. return
  100. }
  101. info_ := make([]map[string]interface{}, 0)
  102. info_ = append(info_, res.Data.Data)
  103. c.OK(info_, "查询成功")
  104. return
  105. }
  106. MSFX_baseUrl, _ := beego.AppConfig.String("MSFX_baseUrl")
  107. var res models.MSFXResponse
  108. err := redis.GetJson(global.RedisMSFXMedicineInfo+code, &res)
  109. if err == nil {
  110. c.OK(res, "查询成功")
  111. return
  112. }
  113. client := resty.New()
  114. resp, err := client.R().
  115. SetQueryParams(map[string]string{
  116. "code": code,
  117. }).
  118. Get(MSFX_baseUrl)
  119. if err != nil {
  120. c.Error(400, err, "获取药品扫码信息失败")
  121. return
  122. }
  123. if err = json.Unmarshal(resp.Body(), &res); err != nil {
  124. c.Error(400, err, "获取药品扫码信息失败")
  125. return
  126. }
  127. // Unmarshal the inner "data" field which is a string containing JSON
  128. var result struct {
  129. Result models.MSFXResult `json:"result"`
  130. }
  131. err = json.Unmarshal([]byte(res.Data), &result)
  132. if err != nil {
  133. fmt.Println("Error unmarshalling inner data:", err)
  134. return
  135. }
  136. info := result.Result.Models
  137. info_ := make([]map[string]interface{}, len(info))
  138. for i := 0; i < len(info); i++ {
  139. info_[i] = make(map[string]interface{})
  140. //if len(info[i]["名称"].(string)) == 0 {
  141. // c.Error(400, err, "药品信息不存在")
  142. //}
  143. info_[i]["product_name"] = info[i].DrugEntBaseDTO.PhysicName
  144. info_[i]["enterprise_name"] = info[i].PUserEntDTO.EntName
  145. info_[i]["spec_name"] = fmt.Sprintf("%s×%s", info[i].DrugEntBaseDTO.PrepnSpec, info[i].DrugEntBaseDTO.PkgSpecCrit)
  146. info_[i]["dosage_form_name"] = info[i].DrugEntBaseDTO.PrepnTypeDesc
  147. info_[i]["approval_number"] = info[i].DrugEntBaseDTO.ApprovalLicenceNo
  148. if len(info[i].CodeProduceInfoDTO.ProduceInfoList) > 0 {
  149. info_[i]["quantity"] = info[i].CodeProduceInfoDTO.ProduceInfoList[0].PkgAmount
  150. info_[i]["batch_number"] = info[i].CodeProduceInfoDTO.ProduceInfoList[0].BatchNo
  151. date := info[i].CodeProduceInfoDTO.ProduceInfoList[0].ExpireDate
  152. info_[i]["expiry_date"] = fmt.Sprintf("%s-%s-%s", date[0:4], date[4:6], date[6:8])
  153. info_[i]["produced_date"] = info[i].CodeProduceInfoDTO.ProduceInfoList[0].ProduceDateStr
  154. }
  155. }
  156. redis.SetJson(global.RedisMSFXMedicineInfo+code, info_, 7*24*time.Hour)
  157. c.OK(info_, "查询成功")
  158. }