package services import ( "Medical_ERP/common/actions" "Medical_ERP/common/global" db "Medical_ERP/common/initialize" "Medical_ERP/common/translate" "Medical_ERP/dto" "Medical_ERP/models" "Medical_ERP/utils" "errors" "github.com/beego/beego/v2/core/logs" "gorm.io/gorm" ) type MedicineTemplate struct { } // 初始化药品信息 func (e *MedicineTemplate) InitMedicineTemplate(deptId, userId int) error { tx := db.DB.Begin() // 1、创建模板 initMedicineTemplateInfoList := models.GetInitMedicineTemplateInfo(deptId, userId) err := tx.Create(&initMedicineTemplateInfoList).Error if err != nil { tx.Rollback() logs.Error("db error: %s", err) return global.CreateFailedErr } err = tx.Exec(models.GetInitMedicineTemplateSql(deptId)).Error if err != nil { tx.Rollback() logs.Error("db error: %s", err) return global.CreateFailedErr } tx.Commit() return nil } // GetPage 获取MedicineTemplate列表 func (e *MedicineTemplate) GetPage(list *[]models.MedicineTemplate, count *int64, p *actions.DataPermission) error { var err error var data models.MedicineTemplate err = db.DB.Model(&data). Scopes( actions.Permission(data.TableName(), p), ). Order("sort"). Find(list).Limit(-1).Offset(-1). Count(count).Error if err != nil { logs.Error("db error: %s ", err) return global.GetFailedErr } return nil } func (e *MedicineTemplate) ListForStock(list *[]dto.MedicineTemplateForStockRes, count *int64, p *actions.DataPermission) error { var err error var data models.MedicineTemplate err = db.DB.Model(&data). Scopes( actions.Permission(data.TableName(), p), ). Order("sort"). 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++ { switch (*list)[i].Type { case 1: var product []models.Product err = db.DB.Model(&models.Product{}).Where("dept_id = ?", p.DeptId).Find(&product).Error (*list)[i].List = product case 2: var enterprise []models.Enterprise err = db.DB.Model(&models.Enterprise{}).Where("dept_id = ?", p.DeptId).Find(&enterprise).Error (*list)[i].List = enterprise case 3: var spec []models.Spec err = db.DB.Model(&models.Spec{}).Where("dept_id = ?", p.DeptId).Find(&spec).Error (*list)[i].List = spec case 4: var unit []models.Unit err = db.DB.Model(&models.Unit{}).Where("dept_id = ?", p.DeptId).Find(&unit).Error (*list)[i].List = unit case 5: var dosageForm []models.DosageForm err = db.DB.Model(&models.DosageForm{}).Where("dept_id = ?", p.DeptId).Find(&dosageForm).Error (*list)[i].List = dosageForm default: (*list)[i].List = nil } } return nil } func (e *MedicineTemplate) ListForStockInScanCode(res *[]dto.MedicineTemplateForStockRes, count *int64, p *actions.DataPermission) error { var err error var data models.MedicineTemplate var list []dto.MedicineTemplateForStockRes err = db.DB.Model(&data). Scopes( actions.Permission(data.TableName(), p), ). Order("sort"). 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++ { switch list[i].Type { case 1: list[i].Type = 6 list[i].FieldName = models.FieldProductName case 2: list[i].Type = 6 list[i].FieldName = models.FieldEnterpriseName case 3: list[i].Type = 6 list[i].FieldName = models.FieldSpecName case 4: list[i].Type = 6 list[i].FieldName = models.FieldDosageFormName //case 5: // (*list)[i].Type = 6 // (*list)[i].FieldName = models.FieldUnitName } } for i, v := range list { if list[i].FieldName != models.FieldProductName && list[i].FieldName != models.FieldEnterpriseName && list[i].FieldName != models.FieldDosageFormName && list[i].FieldName != models.FieldSpecName && list[i].FieldName != models.FieldBatchNumber && list[i].FieldName != models.FieldExpiryDate && list[i].FieldName != models.FieldApprovalNumber && list[i].FieldName != models.FieldProducedDate { continue } *res = append(*res, v) } *count = int64(len(*res)) return nil } func (e *MedicineTemplate) ListForStockOut(res *[]dto.MedicineTemplateForStockRes, count *int64, p *actions.DataPermission) error { var err error var data models.MedicineTemplate var list []dto.MedicineTemplateForStockRes err = db.DB.Model(&data). Scopes(actions.Permission(data.TableName(), p)). Order("sort"). Find(&list).Error if err != nil { logs.Error("db error: %s ", err) return global.GetFailedErr } for i := 0; i < len(list); i++ { switch list[i].Type { case 1: var product []models.Product err = db.DB.Model(&models.Product{}).Where("dept_id = ?", p.DeptId).Find(&product).Error list[i].List = product case 2: var enterprise []models.Enterprise err = db.DB.Model(&models.Enterprise{}).Where("dept_id = ?", p.DeptId).Find(&enterprise).Error list[i].List = enterprise case 3: var spec []models.Spec err = db.DB.Model(&models.Spec{}).Where("dept_id = ?", p.DeptId).Find(&spec).Error list[i].List = spec //case 4: // var dosageForm []models.DosageForm // err = db.DB.Model(&models.DosageForm{}).Where("dept_id = ?", p.DeptId).Find(&dosageForm).Error // list[i].List = dosageForm //case 5: // var unit []models.Unit // err = db.DB.Model(&models.Unit{}).Where("dept_id = ?", p.DeptId).Find(&unit).Error // list[i].List = unit default: list[i].List = nil } } for i, v := range list { if list[i].FieldName != models.FieldProductID && list[i].FieldName != models.FieldEnterpriseID && list[i].FieldName != models.FieldSpecID && list[i].FieldName != models.FieldBatchNumber { continue } *res = append(*res, v) } *count = int64(len(*res)) return nil } func (e *MedicineTemplate) ListForStockOutScanCode(res *[]dto.MedicineTemplateForStockRes, count *int64, p *actions.DataPermission) error { var err error var data models.MedicineTemplate var list []dto.MedicineTemplateForStockRes err = db.DB.Model(&data). Scopes(actions.Permission(data.TableName(), p)). Order("sort"). Find(&list).Error if err != nil { logs.Error("db error: %s ", err) return global.GetFailedErr } for i := 0; i < len(list); i++ { switch list[i].Type { case 1: list[i].Type = 6 list[i].FieldName = models.FieldProductName case 2: list[i].Type = 6 list[i].FieldName = models.FieldEnterpriseName case 3: list[i].Type = 6 list[i].FieldName = models.FieldSpecName } } for i, v := range list { if list[i].FieldName != models.FieldProductName && list[i].FieldName != models.FieldEnterpriseName && list[i].FieldName != models.FieldSpecName && list[i].FieldName != models.FieldBatchNumber { continue } *res = append(*res, v) } *count = int64(len(*res)) return nil } // Insert 创建MedicineTemplate对象 func (e *MedicineTemplate) Insert(c *dto.MedicineTemplateInsertReq) error { var err error var data models.MedicineTemplate err = db.DB.Where("name = ? AND dept_id = ?", c.Name, c.DeptId).Find(&data).Error if data.Id > 0 { return errors.New("标签名称已存在") } if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { logs.Error("db error: %s", err) return global.CreateFailedErr } c.Generate(&data) data.State = 2 // 用户添加 en, err := translate.Translate(c.Name) if err != nil { logs.Error("火山引擎翻译出错: %s", err) return errors.New("火山引擎出错,请联系管理员") } data.FieldName = utils.WordsToSnake(en) tx := db.DB.Begin() err = tx.Create(&data).Error if err != nil { tx.Rollback() logs.Error("db error: %s", err) return global.CreateFailedErr } // 向表内新增字段 err = tx.Exec(models.GetInsertMedicineTemplateSql(c.DeptId, c.Type, data.FieldName)).Error if err != nil { tx.Rollback() logs.Error("db error: %s", err) return global.CreateFailedErr } tx.Commit() c.Id = data.Id return nil } // Update 修改MedicineTemplate对象 func (e *MedicineTemplate) Update(c *dto.MedicineTemplateUpdateReq) error { var mtModel = models.MedicineTemplate{} err := db.DB.First(&mtModel, c.GetId()).Error if err != nil { logs.Error("db error: %s", err) if errors.Is(err, gorm.ErrRecordNotFound) { return global.UpdateNotFoundOrNoPermissionErr } return global.UpdateFailedErr } tx := db.DB.Begin() // 修改列名 系统初始化的不能修改字段名 if mtModel.Name != c.Name && mtModel.State != 1 { en, err := translate.Translate(c.Name) if err != nil { tx.Rollback() logs.Error("火山引擎翻译出错: %s", err) return errors.New("火山引擎出错,请联系管理员") } newFieldName := utils.WordsToSnake(en) err = tx.Exec(models.GetUpdateMedicineTemplateSql(mtModel.DeptId, mtModel.Type, mtModel.FieldName, newFieldName)).Error if err != nil { tx.Rollback() logs.Error("db error: %s", err) return global.CreateFailedErr } mtModel.FieldName = newFieldName } c.Generate(&mtModel) err = tx.Save(&mtModel).Error if err != nil { tx.Rollback() logs.Error("db error: %s", err) return global.UpdateFailedErr } tx.Commit() return nil } // Remove 删除MedicineTemplate func (e *MedicineTemplate) Remove(d *dto.MedicineTemplateDeleteReq) error { var err error var mt models.MedicineTemplate var data models.MedicineTemplate err = db.DB.First(&mt, d.GetId()).Error if err != nil { logs.Error("db error: %s", err) if errors.Is(err, gorm.ErrRecordNotFound) { return global.DeleteNotFoundOrNoPermissionErr } return global.DeleteFailedErr } tx := db.DB.Begin() result := tx.Model(&data).Delete(&data, d.GetId()) if err = result.Error; err != nil { tx.Rollback() logs.Error("Delete error: %s", err) return global.DeleteFailedErr } // 删除表内字段 err = tx.Exec(models.GetDeleteMedicineTemplateSql(mt.DeptId, mt.FieldName)).Error if err != nil { tx.Rollback() logs.Error("db error: %s", err) return global.CreateFailedErr } tx.Commit() return nil } // GetColumns 获取前端渲染表格-表头 func (e *MedicineTemplate) GetColumns(columnsList *[]dto.MedicineColumnsRes, p *actions.DataPermission) error { var err error var data models.MedicineTemplate var list []models.MedicineTemplate err = db.DB.Model(&data). Scopes( actions.Permission(data.TableName(), p), ). Where("`show` = 1"). Order("sort"). Find(&list).Error if err != nil { logs.Error("db error: %s ", err) return global.GetFailedErr } *columnsList = append(*columnsList, dto.MedicineColumnsRes{ Label: "条码", Prop: "qrcode", Width: 200, }) for _, v := range list { fieldName := v.FieldName switch v.FieldName { case models.FieldProductID: fieldName = models.FieldProductName case models.FieldEnterpriseID: fieldName = models.FieldEnterpriseName case models.FieldSpecID: fieldName = models.FieldSpecName case models.FieldUnitID: fieldName = models.FieldUnitName case models.FieldDosageFormID: fieldName = models.FieldDosageFormName } *columnsList = append(*columnsList, dto.MedicineColumnsRes{ Label: v.Name, Prop: fieldName, Width: v.Width, }) } return nil }