| 
					
				 | 
			
			
				@@ -12,9 +12,9 @@ import ( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"fmt" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"github.com/beego/beego/v2/core/logs" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"gorm.io/gorm" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	"sort" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"strconv" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"strings" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	"time" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 ) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 type StockTemplate struct { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -32,6 +32,88 @@ func (e *StockTemplate) GetMedicineInfo(deptId int, medicine map[string]interfac 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// FirstOrCreateMedicineInfo 查询或创建 品名、生产企业、规格、单位、剂型 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+func (e *StockTemplate) FirstOrCreateMedicineInfo(deptId, createBy int, medicine *map[string]interface{}) (err error) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	// 查询或创建品名 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	product := models.Product{} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	if productName, ok := (*medicine)[models.FieldProductName].(string); ok && len(productName) > 0 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		err = db.DB.Attrs(map[string]interface{}{"create_by": createBy}).FirstOrCreate(&product, map[string]interface{}{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			"name":    productName, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			"dept_id": deptId, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		}).Error 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return errors.New("获取品名失败") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		delete(*medicine, models.FieldProductName) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		return errors.New("品名不能为空") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	// 查询或创建生产企业 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	enterprise := models.Enterprise{} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	if enterpriseName, ok := (*medicine)[models.FieldEnterpriseName].(string); ok && len(enterpriseName) > 0 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		err = db.DB.Attrs(map[string]interface{}{"create_by": createBy}).FirstOrCreate(&enterprise, map[string]interface{}{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			"name":    enterpriseName, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			"dept_id": deptId, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		}).Error 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return errors.New("获取生产企业失败") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		delete(*medicine, models.FieldEnterpriseName) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		return errors.New("生产企业不能为空") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	// 查询或创建规格 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	spec := models.Spec{} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	if specName, ok := (*medicine)[models.FieldSpecName].(string); ok && len(specName) > 0 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		err = db.DB.Attrs(map[string]interface{}{"create_by": createBy}).FirstOrCreate(&spec, map[string]interface{}{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			"name":    specName, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			"dept_id": deptId, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		}).Error 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return errors.New("获取规格失败") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		delete(*medicine, models.FieldSpecName) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		return errors.New("规格不能为空") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	// 查询或创建单位 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	unit := models.Unit{} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	if unitName, ok := (*medicine)[models.FieldUnitName].(string); ok && len(unitName) > 0 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		err = db.DB.Attrs(map[string]interface{}{"create_by": createBy}).FirstOrCreate(&unit, map[string]interface{}{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			"name":    unitName, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			"dept_id": deptId, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		}).Error 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return errors.New("获取单位失败") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	// 查询或创建剂型 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	dosageForm := models.DosageForm{} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	if dosageFormName, ok := (*medicine)[models.FieldDosageFormName].(string); ok && len(dosageFormName) > 0 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		err = db.DB.Attrs(map[string]interface{}{"create_by": createBy}).FirstOrCreate(&dosageForm, map[string]interface{}{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			"name":    dosageFormName, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			"dept_id": deptId, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		}).Error 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return errors.New("获取剂型失败") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	delete(*medicine, models.FieldUnitName) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	delete(*medicine, models.FieldDosageFormName) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	(*medicine)[models.FieldProductID] = product.Id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	(*medicine)[models.FieldEnterpriseID] = enterprise.Id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	(*medicine)[models.FieldSpecID] = spec.Id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	(*medicine)[models.FieldUnitID] = unit.Id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	(*medicine)[models.FieldDosageFormID] = dosageForm.Id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 // BatchStockTemplateIn 批量入库 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 func (e *StockTemplate) BatchStockTemplateIn(req *dto.BatchStockTemplateInInsertReq) error { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	//var err error 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -135,6 +217,118 @@ func (e *StockTemplate) BatchStockTemplateIn(req *dto.BatchStockTemplateInInsert 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	return nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// BatchStockTemplateIn 扫码入库 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+func (e *StockTemplate) StockTemplateInScanCode(req *dto.BatchStockTemplateInInsertReq) error { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	//var err error 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	tx := db.DB.Begin() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	for _, c := range req.StockInList { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		// 检查药品信息是否已存在 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		err := e.FirstOrCreateMedicineInfo(req.DeptId, req.CreateBy, &c.MedicineInfo) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			tx.Rollback() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			logs.Error("【扫码入库】初始化药品信息失败:%s", err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return err 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		// 检查药品信息是否已存在 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		medicineInfo, err := e.GetMedicineInfo(req.DeptId, c.MedicineInfo) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			tx.Rollback() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			logs.Error("db error: %s", err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return global.CreateFailedErr 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		var medicineInfoId int 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		// 如果药品信息不存在,则创建新的药品信息 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if medicineInfo == nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			sql := "INSERT INTO " + models.GetMedicineInfoTableName(req.DeptId) + " SET " 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			for k, v := range c.MedicineInfo { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				sql += fmt.Sprintf("`%s`='%v',", k, v) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			sql += fmt.Sprintf("`%s`='%v',", "purchase_unit_price", c.UnitPrice) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			sql = sql[:len(sql)-1] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			err = tx.Exec(sql).Error 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				tx.Rollback() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				logs.Error("db error: %s", err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				return global.CreateFailedErr 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			err = tx.Raw("SELECT LAST_INSERT_ID()").Scan(&medicineInfoId).Error 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				tx.Rollback() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				logs.Error("db error: %s", err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				return global.CreateFailedErr 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			medicineInfoId = utils.ToInt(medicineInfo["id"]) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			err = tx.Table(models.GetMedicineInfoTableName(req.DeptId)).Where("id = ?", medicineInfoId).Update("purchase_unit_price", c.UnitPrice).Error 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				tx.Rollback() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				logs.Error("db error: %s", err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				return global.CreateFailedErr 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		// 进行入库操作 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		stockInRecord := models.StockIn{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			MedicineID:     medicineInfoId, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			Quantity:       c.Quantity, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			UnitPrice:      c.UnitPrice, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			Operator:       c.Operator, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			ForwardingUnit: c.ForwardingUnit, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			Date:           c.Date, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			ControlBy: model2.ControlBy{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				DeptId:   req.DeptId, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				CreateBy: req.CreateBy, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		err = tx.Create(&stockInRecord).Error 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			tx.Rollback() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			logs.Error("db error: %s", err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return global.CreateFailedErr 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		c.Id = stockInRecord.Id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		var inventory models.MedicineInventory 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		err = tx.Last(&inventory, "medicine_id = ?", medicineInfoId).Error 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			tx.Rollback() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			logs.Error("db error: %s", err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return global.CreateFailedErr 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		// 添加库存记录 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		data := models.MedicineInventory{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			MedicineID:     medicineInfoId, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			TotalIn:        c.Quantity, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			StockInID:      stockInRecord.Id, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			TotalOut:       0, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			StockOutID:     0, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			Balance:        inventory.Balance + c.Quantity, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			ForwardingUnit: c.ForwardingUnit, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			Operator:       c.Operator, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			Date:           c.Date, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			ControlBy: model2.ControlBy{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				DeptId:   req.DeptId, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				CreateBy: req.CreateBy, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		err = tx.Create(&data).Error 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			tx.Rollback() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			logs.Error("db error: %s", err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return global.CreateFailedErr 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	tx.Commit() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	return nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 // StockTemplateIn 入库 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 func (e *StockTemplate) StockTemplateIn(c *dto.StockTemplateInInsertReq) error { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	var err error 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -231,7 +425,8 @@ func (e *StockTemplate) StockTemplateIn(c *dto.StockTemplateInInsertReq) error { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	tx.Commit() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	return nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-} // StockTemplateIn 入库 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 func (e *StockTemplate) StockTemplateInEdit(c *dto.StockTemplateInEditReq) error { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	var err error 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	// 检查药品信息是否已存在 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -920,6 +1115,108 @@ func (e *StockTemplate) BatchStockTemplateOut(req *dto.BatchStockTemplateOutInse 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	return nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// StockTemplateOutScanCode 扫码出库 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+func (e *StockTemplate) StockTemplateOutScanCode(req *dto.BatchStockTemplateOutInsertReq) error { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	tx := db.DB.Begin() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	for _, c := range req.StockOutList { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		// 初始化药品信息 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		err := e.FirstOrCreateMedicineInfo(req.DeptId, req.CreateBy, &c.MedicineInfo) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			tx.Rollback() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			logs.Error("【扫码出库】初始化药品信息失败: %s", err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return err 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		// 检查药品信息是否已存在 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		medicineInfo, err := e.GetMedicineInfo(req.DeptId, c.MedicineInfo) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			tx.Rollback() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			logs.Error("db error: %s", err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return global.CreateFailedErr 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		// 如果药品信息不存在,则不能出库 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if medicineInfo == nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			tx.Rollback() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			logs.Error("db error: %s", err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return errors.New("药品信息不存在,禁止出库") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		medicineInfoId := utils.ToInt(medicineInfo["id"]) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		// 查询库查询信息 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		var mi models.MedicineInventory 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		err = tx.Model(mi).Where("medicine_id = ? AND dept_id = ?", medicineInfoId, req.DeptId).Last(&mi).Error 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			tx.Rollback() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			logs.Error("db error: %s", err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return global.CreateFailedErr 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if mi.Balance < c.Quantity { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			tx.Rollback() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return errors.New(fmt.Sprintf("【%s】库存量【%d】小于出库库存量【%d】,出库失败", c.MedicineInfo[models.FieldProductName], mi.Balance, c.Quantity)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		err = tx.Table(models.GetMedicineInfoTableName(req.DeptId)).Where("id = ?", medicineInfoId).Update("sales_unit_price", c.UnitPrice).Error 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			tx.Rollback() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			logs.Error("db error: %s", err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return global.CreateFailedErr 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		// 进行出库操作 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		stockOutRecord := models.StockOut{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			MedicineID:    medicineInfoId, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			Quantity:      c.Quantity, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			UnitPrice:     c.UnitPrice, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			Operator:      c.Operator, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			ReceivingUnit: c.ReceivingUnit, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			Date:          c.Date, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			ControlBy: model2.ControlBy{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				DeptId:   req.DeptId, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				CreateBy: req.CreateBy, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		err = tx.Create(&stockOutRecord).Error 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			tx.Rollback() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			logs.Error("db error: %s", err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return global.CreateFailedErr 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		c.Id = stockOutRecord.Id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		var inventory models.MedicineInventory 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		err = tx.Last(&inventory, "medicine_id = ?", medicineInfoId).Error 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			tx.Rollback() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			logs.Error("db error: %s", err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return global.CreateFailedErr 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		// 添加库存记录 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		data := models.MedicineInventory{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			MedicineID:    medicineInfoId, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			TotalIn:       0, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			StockInID:     0, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			TotalOut:      c.Quantity, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			StockOutID:    stockOutRecord.Id, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			Balance:       inventory.Balance - c.Quantity, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			ReceivingUnit: c.ReceivingUnit, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			Operator:      c.Operator, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			Date:          c.Date, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			ControlBy: model2.ControlBy{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				DeptId:   req.DeptId, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				CreateBy: req.CreateBy, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		err = tx.Create(&data).Error 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			tx.Rollback() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			logs.Error("db error: %s", err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return global.CreateFailedErr 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	tx.Commit() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	return nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 // StockTemplateInList 出库列表 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 func (e *StockTemplate) StockTemplateInList(c *dto.StockTemplateInPageReq, deptId int) (list []map[string]interface{}, count int64, err error) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -981,9 +1278,8 @@ func (e *StockTemplate) StockTemplateInList(c *dto.StockTemplateInPageReq, deptI 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 				list[i][models.FieldDosageFormName] = models.Read_DosageForm_Get(utils.ToInt(id)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		if date, ok := list[i][models.FieldExpiryDate]; ok && date != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-			list[i][models.FieldExpiryDate] = date.(time.Time).Format("2006-01-02") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		list[i][models.FieldExpiryDate] = utils.ToDate(list[i][models.FieldExpiryDate]) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		list[i][models.FieldProducedDate] = utils.ToDate(list[i][models.FieldProducedDate]) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	return list, count, nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -1050,9 +1346,7 @@ func (e *StockTemplate) StockTemplateOutList(c *dto.StockTemplateOutPageReq, dep 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		if date, ok := list[i][models.FieldExpiryDate]; ok && date != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-			list[i][models.FieldExpiryDate] = date.(time.Time).Format("2006-01-02") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		list[i][models.FieldExpiryDate] = utils.ToDate(list[i][models.FieldExpiryDate]) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	return list, count, nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -1110,9 +1404,7 @@ func (e *StockTemplate) StockTemplateInventoryList(c *dto.StockTemplateInventory 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		if id, ok := list[i][models.FieldDosageFormID]; ok { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			list[i][models.FieldDosageFormName] = models.Read_DosageForm_Get(utils.ToInt(id)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		if date, ok := list[i][models.FieldExpiryDate]; ok && date != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-			list[i][models.FieldExpiryDate] = date.(time.Time).Format("2006-01-02") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		list[i][models.FieldExpiryDate] = utils.ToDate(list[i][models.FieldExpiryDate]) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	return list, count, nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -1308,9 +1600,7 @@ func (e *StockTemplate) StockInquiryList(c *dto.StockStatListReq, deptId int) (l 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		if id, ok := list[i][models.FieldDosageFormID]; ok { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			list[i][models.FieldDosageFormName] = models.Read_DosageForm_Get(utils.ToInt(id)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		if date, ok := list[i][models.FieldExpiryDate]; ok && date != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-			list[i][models.FieldExpiryDate] = date.(time.Time).Format("2006-01-02") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		list[i][models.FieldExpiryDate] = utils.ToDate(list[i][models.FieldExpiryDate]) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	return list, count, nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -1364,9 +1654,7 @@ func (e *StockTemplate) StockInquiryExcel(c *dto.StockStatListReq, deptId int) ( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		if id, ok := list[i][models.FieldDosageFormID]; ok { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			list[i][models.FieldDosageFormName] = models.Read_DosageForm_Get(utils.ToInt(id)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		if date, ok := list[i][models.FieldExpiryDate]; ok && date != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-			list[i][models.FieldExpiryDate] = date.(time.Time).Format("2006-01-02") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		list[i][models.FieldExpiryDate] = utils.ToDate(list[i][models.FieldExpiryDate]) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	return list, nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -1385,15 +1673,6 @@ func (e *StockTemplate) StockStat(c *dto.StockStatReq, deptId int) (list []map[s 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	sql := "SELECT mi.balance,m_info.product_id,m_info.spec_id,m_info.unit_id FROM (SELECT medicine_id,MAX(id) AS latest_id FROM medicine_inventory GROUP BY medicine_id) AS mi_latest " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		"JOIN medicine_inventory AS mi ON mi.medicine_id=mi_latest.medicine_id AND mi.id=mi_latest.latest_id " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		"LEFT JOIN " + mtable + " AS m_info ON mi.medicine_id=m_info.id" + whereSql 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	//sqlWhereCount := "SELECT COUNT(1) FROM (SELECT medicine_id,MAX(id) AS latest_id FROM medicine_inventory GROUP BY medicine_id) AS mi_latest " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	//	"JOIN medicine_inventory AS mi ON mi.medicine_id=mi_latest.medicine_id AND mi.id=mi_latest.latest_id " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	//	"LEFT JOIN " + mtable + " AS m_info ON mi.medicine_id=m_info.id" + whereSql 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	//err = db.DB.Raw(sqlWhereCount).Scan(&count).Error 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	//if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	//	logs.Error("db error: %s ", err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	//	return list, count, global.GetFailedErr 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	//} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	err = db.DB.Raw(sql).Scan(&result).Error 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	if err != nil { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -1403,6 +1682,7 @@ func (e *StockTemplate) StockStat(c *dto.StockStatReq, deptId int) (list []map[s 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	models.InitBasicData(deptId) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	statMap := map[string]int{} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	unitMap := map[string]string{} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	for i := 0; i < len(result); i++ { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		key := fmt.Sprintf("%v-%v", result[i][models.FieldProductID], result[i][models.FieldSpecID]) 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -1412,20 +1692,49 @@ func (e *StockTemplate) StockStat(c *dto.StockStatReq, deptId int) (list []map[s 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		} else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			statMap[key] = utils.ToInt(result[i]["balance"]) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if result[i][models.FieldUnitID] != nil && utils.ToInt(result[i][models.FieldUnitID]) != 0 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			if v, ok := unitMap[key]; ok { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				id := fmt.Sprintf("|%v|", result[i][models.FieldUnitID]) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				if strings.Contains(v, id) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+					unitMap[key] = v + id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			} else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				unitMap[key] = fmt.Sprintf("|%v|", result[i][models.FieldUnitID]) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	models.InitBasicData(deptId) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	for key, num := range statMap { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		product_id := utils.ToInt(strings.Split(key, "-")[0]) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		spec_id := utils.ToInt(strings.Split(key, "-")[1]) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		unit_ids := []string{} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if len(unitMap[key]) > 0 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			unit_ids = strings.Split(strings.Trim(unitMap[key], "|"), "|") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		unit_name := "" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		unit_ids_int := []int{} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		for _, id := range unit_ids { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			unit_name += models.Read_Unit_Get(utils.ToInt(id)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			unit_name += "/" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			unit_ids_int = append(unit_ids_int, utils.ToInt(id)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		unit_name = strings.Trim(unit_name, "/") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		list = append(list, map[string]interface{}{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			"product_id":   product_id, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			"product_name": models.Read_Product_Get(product_id), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			"spec_id":      spec_id, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			"spec_name":    models.Read_Spec_Get(spec_id), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			"balance":      num, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			"unit_name":    unit_name, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			"unit_ids":     unit_ids_int, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		}) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	sort.Slice(list, func(i, j int) bool { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		return utils.ToInt(list[i]["product_id"]) < utils.ToInt(list[j]["product_id"]) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	}) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	return list, count, nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 |