123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- 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
- }
|