123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package services
- import (
- "Medical_ERP/common/actions"
- cDto "Medical_ERP/common/dto"
- "Medical_ERP/common/global"
- db "Medical_ERP/common/initialize"
- "Medical_ERP/conf"
- "Medical_ERP/dto"
- "Medical_ERP/models"
- "Medical_ERP/utils"
- "errors"
- "fmt"
- "github.com/beego/beego/v2/adapter/context"
- "github.com/beego/beego/v2/core/logs"
- "github.com/go-resty/resty/v2"
- "github.com/tidwall/gjson"
- "gorm.io/gorm"
- "net/http"
- "strings"
- )
- type MedicineImg struct {
- }
- // MedicineImg 通过名称、生产企业、规格、批号查询药品图片,
- // 如果未查询到,则查询根公司图片信息
- func (e *MedicineImg) Get(deptId, rootId int, product, enterprise, spec, batchNumber string) (medicineImg models.MedicineImg, err error) {
- medicineImgList := make([]models.MedicineImg, 0)
- err = db.DB.Where("product = ? AND enterprise = ? AND spec = ? AND batch_number = ? AND dept_id in ? AND img != ''",
- product, enterprise, spec, batchNumber, []int{deptId, rootId}).Find(&medicineImgList).Error
- if err != nil {
- logs.Error("db error: %s ", err)
- return medicineImg, global.GetFailedErr
- }
- if len(medicineImgList) == 0 {
- return medicineImg, global.GetNotFoundErr
- }
- if len(medicineImgList) == 1 {
- return medicineImgList[0], nil
- }
- if len(medicineImgList) == 2 {
- for _, img := range medicineImgList {
- if img.DeptId == deptId {
- return img, nil
- }
- }
- }
- return medicineImg, nil
- }
- // GetPage 获取Spec列表
- func (e *MedicineImg) GetPage(c *dto.MedicineImgPageReq, list *[]models.MedicineImg, count *int64, rootId int, p *actions.DataPermission) error {
- var err error
- var data models.MedicineImg
- err = db.DB.Model(&data).
- Scopes(
- cDto.MakeCondition(c.GetNeedSearch()),
- cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
- actions.Permission(data.TableName(), p),
- ).
- Find(list).Limit(-1).Offset(-1).
- Count(count).Error
- if err != nil {
- logs.Error("db error: %s ", err)
- return global.GetFailedErr
- }
- for i := 0; i < len(*list); i++ {
- if len((*list)[i].Img) == 0 {
- img, _ := e.Get((*list)[i].DeptId, rootId, (*list)[i].Product, (*list)[i].Enterprise, (*list)[i].Spec, (*list)[i].BatchNumber)
- (*list)[i].Img = img.Img
- }
- }
- return nil
- }
- // Update 修改Spec对象
- func (e *MedicineImg) Update(c *dto.MedicineImgUpdateReq) error {
- var medicineImg = models.MedicineImg{}
- err := db.DB.First(&medicineImg, c.GetId()).Error
- if err != nil {
- logs.Error("db error: %s", err)
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return global.GetNotFoundErr
- }
- return global.GetFailedErr
- }
- c.Generate(&medicineImg)
- err = db.DB.Save(&medicineImg).Error
- if err != nil {
- logs.Error("db error: %s", err)
- return global.UpdateFailedErr
- }
- return nil
- }
- func (e *MedicineImg) GetRootID(c *context.Context, rootId int) (id int, err error) {
- reqPath := fmt.Sprintf("/api/dept/%d", rootId)
- url := conf.OAuthBaseUrl + reqPath
- r, reqError := resty.New().R().SetHeaders(map[string]string{
- "Authorization": c.Request.Header.Get("Authorization"),
- }).Get(url)
- if reqError != nil {
- err = errors.New("获取根公司失败")
- logs.Error(err.Error(), reqError)
- return
- }
- code := gjson.GetBytes(r.Body(), "code").Int()
- deptPath := gjson.GetBytes(r.Body(), "data.deptPath").String()
- if code != http.StatusOK {
- err = errors.New("获取根公司失败")
- logs.Error(err.Error(), string(r.Body()))
- return
- }
- list := strings.Split(strings.Trim(deptPath, "/"), "/")
- if len(list) > 1 {
- id = utils.ToInt(list[1])
- }
- return
- }
|