Browse Source

Merge branch '20230223_xt_api_new_branch' of http://git.shengws.com/csx/XT_New into 20230223_xt_api_new_branch

陈少旭 6 days ago
parent
commit
415c2463ad

BIN
XT_New.exe View File


+ 4 - 4
controllers/base_api_controller.go View File

83
 		userAdmin.ModifyTime = 1530786071
83
 		userAdmin.ModifyTime = 1530786071
84
 		var subscibe models.ServeSubscibe
84
 		var subscibe models.ServeSubscibe
85
 		subscibe.ID = 11
85
 		subscibe.ID = 11
86
-		subscibe.OrgId = 10164 //机构id
86
+		subscibe.OrgId = 10677 //机构id
87
 		subscibe.PeriodStart = 1547447814
87
 		subscibe.PeriodStart = 1547447814
88
 		subscibe.PeriodEnd = 1550039814
88
 		subscibe.PeriodEnd = 1550039814
89
 		subscibe.State = 1
89
 		subscibe.State = 1
93
 		subscibes := make(map[int64]*models.ServeSubscibe, 0)
93
 		subscibes := make(map[int64]*models.ServeSubscibe, 0)
94
 		subscibes[4] = &subscibe
94
 		subscibes[4] = &subscibe
95
 		var adminUserInfo service.AdminUserInfo
95
 		var adminUserInfo service.AdminUserInfo
96
-		adminUserInfo.CurrentOrgId = 10164 //机构id小英9675或4
96
+		adminUserInfo.CurrentOrgId = 10677 //机构id小英9675或4
97
 		adminUserInfo.CurrentAppId = 12123 //4
97
 		adminUserInfo.CurrentAppId = 12123 //4
98
 		adminUserInfo.AdminUser = &userAdmin
98
 		adminUserInfo.AdminUser = &userAdmin
99
 		adminUserInfo.Subscibes = subscibes
99
 		adminUserInfo.Subscibes = subscibes
329
 		userAdmin.ModifyTime = 1530786071
329
 		userAdmin.ModifyTime = 1530786071
330
 		var subscibe models.ServeSubscibe
330
 		var subscibe models.ServeSubscibe
331
 		subscibe.ID = 11
331
 		subscibe.ID = 11
332
-		subscibe.OrgId = 10164 //机构id小英9675或4
332
+		subscibe.OrgId = 10677 //机构id小英9675或4
333
 		subscibe.PeriodStart = 1538035409
333
 		subscibe.PeriodStart = 1538035409
334
 		subscibe.PeriodEnd = 1569571409
334
 		subscibe.PeriodEnd = 1569571409
335
 		subscibe.State = 1
335
 		subscibe.State = 1
339
 		subscibes := make(map[int64]*models.ServeSubscibe, 0)
339
 		subscibes := make(map[int64]*models.ServeSubscibe, 0)
340
 		subscibes[4] = &subscibe
340
 		subscibes[4] = &subscibe
341
 		var adminUserInfo service.AdminUserInfo
341
 		var adminUserInfo service.AdminUserInfo
342
-		adminUserInfo.CurrentOrgId = 10164 //机构id小英9675或4
342
+		adminUserInfo.CurrentOrgId = 10677 //机构id小英9675或4
343
 		adminUserInfo.CurrentAppId = 12123 //4
343
 		adminUserInfo.CurrentAppId = 12123 //4
344
 		adminUserInfo.AdminUser = &userAdmin
344
 		adminUserInfo.AdminUser = &userAdmin
345
 		adminUserInfo.Subscibes = subscibes
345
 		adminUserInfo.Subscibes = subscibes

+ 1 - 2
controllers/drug_pharmacy_management_controller.go View File

111
 
111
 
112
 	idArrayOne := strings.Split(hisStr, ",")
112
 	idArrayOne := strings.Split(hisStr, ",")
113
 
113
 
114
-	fmt.Println("idArray23332233223", idArray)
115
-	fmt.Println("idArrayOne23332233223", idArrayOne)
114
+	fmt.Println("", idArrayOne)
116
 
115
 
117
 	orgId := this.GetAdminUserInfo().CurrentOrgId
116
 	orgId := this.GetAdminUserInfo().CurrentOrgId
118
 	service.UpdatePharmacyAdviceBaseDrug(idArray, orgId)
117
 	service.UpdatePharmacyAdviceBaseDrug(idArray, orgId)

File diff suppressed because it is too large
+ 195 - 457
controllers/drug_stock_api_contorller.go


+ 2 - 2
controllers/his_api_controller.go View File

5778
 						out, _ := service.FindPrescriptionWarehouseOut(adminInfo.CurrentOrgId, nowtime)
5778
 						out, _ := service.FindPrescriptionWarehouseOut(adminInfo.CurrentOrgId, nowtime)
5779
 
5779
 
5780
 						if out.ID == 0 {
5780
 						if out.ID == 0 {
5781
-							service.AddSigleWarehouseOut(&warehouseOut)
5781
+							service.AddSigleWarehouseOutOne(&warehouseOut)
5782
 						}
5782
 						}
5783
 
5783
 
5784
 						lastOut, _ := service.FindLastPrescriptionWarehouseOut(adminInfo.CurrentOrgId, nowtime)
5784
 						lastOut, _ := service.FindLastPrescriptionWarehouseOut(adminInfo.CurrentOrgId, nowtime)
10990
 					//查询是否生成出库单
10990
 					//查询是否生成出库单
10991
 					out, _ := service.FindAllSettleWarehouseOut(orgId, nowtime, 5)
10991
 					out, _ := service.FindAllSettleWarehouseOut(orgId, nowtime, 5)
10992
 					if out.ID == 0 {
10992
 					if out.ID == 0 {
10993
-						service.AddSigleWarehouseOut(&warehouseOut)
10993
+						service.AddSigleWarehouseOutOne(&warehouseOut)
10994
 					}
10994
 					}
10995
 
10995
 
10996
 					lastOut, _ := service.FindAllSettleWarehouseOut(orgId, nowtime, 5)
10996
 					lastOut, _ := service.FindAllSettleWarehouseOut(orgId, nowtime, 5)

+ 8 - 8
controllers/mobile_api_controllers/dialysis_api_controller.go View File

7356
 				StorehouseId:            houseConfig.StorehouseOutInfo,
7356
 				StorehouseId:            houseConfig.StorehouseOutInfo,
7357
 				IsCheck:                 1,
7357
 				IsCheck:                 1,
7358
 			}
7358
 			}
7359
-			err := service.AddSigleWarehouseOut(&warehouseOut)
7359
+			err := service.AddSigleWarehouseOutOne(&warehouseOut)
7360
 			if err != nil {
7360
 			if err != nil {
7361
 				goodErrcode := models.XtGoodErrcode{
7361
 				goodErrcode := models.XtGoodErrcode{
7362
 					UserOrgId:      adminInfo.Org.Id,
7362
 					UserOrgId:      adminInfo.Org.Id,
7746
 				StorehouseId:            houseConfig.StorehouseOutInfo,
7746
 				StorehouseId:            houseConfig.StorehouseOutInfo,
7747
 				IsCheck:                 1,
7747
 				IsCheck:                 1,
7748
 			}
7748
 			}
7749
-			err := service.AddSigleWarehouseOut(&warehouseOut)
7749
+			err := service.AddSigleWarehouseOutOne(&warehouseOut)
7750
 			if err != nil {
7750
 			if err != nil {
7751
 				goodErrcode := models.XtGoodErrcode{
7751
 				goodErrcode := models.XtGoodErrcode{
7752
 					UserOrgId:      adminInfo.Org.Id,
7752
 					UserOrgId:      adminInfo.Org.Id,
8292
 						_, errcod := service.GetWarehouseOutInfoIsExistOne(item.GoodId, patient_id, record_time, 0)
8292
 						_, errcod := service.GetWarehouseOutInfoIsExistOne(item.GoodId, patient_id, record_time, 0)
8293
 
8293
 
8294
 						if errcod == gorm.ErrRecordNotFound {
8294
 						if errcod == gorm.ErrRecordNotFound {
8295
-							service.AddSigleWarehouseOutInfo(warehouseOutInfo)
8295
+							service.AddSigleWarehouseOutInfoOne(warehouseOutInfo)
8296
 							//插入详情明细表
8296
 							//插入详情明细表
8297
 							stockFlow := models.VmStockFlow{
8297
 							stockFlow := models.VmStockFlow{
8298
 								WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
8298
 								WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
8573
 									StorehouseId:            houseConfig.StorehouseOutInfo,
8573
 									StorehouseId:            houseConfig.StorehouseOutInfo,
8574
 									IsCheck:                 1,
8574
 									IsCheck:                 1,
8575
 								}
8575
 								}
8576
-								service.AddSigleWarehouseOut(&warehouseOut)
8576
+								service.AddSigleWarehouseOutOne(&warehouseOut)
8577
 
8577
 
8578
 							}
8578
 							}
8579
 							//出库
8579
 							//出库
9599
 	_, errcod := service.GetWarehouseOutInfoIsExistOne(good_yc.GoodId, good_yc.PatientId, record_time, good_yc.ProjectId)
9599
 	_, errcod := service.GetWarehouseOutInfoIsExistOne(good_yc.GoodId, good_yc.PatientId, record_time, good_yc.ProjectId)
9600
 
9600
 
9601
 	if errcod == gorm.ErrRecordNotFound {
9601
 	if errcod == gorm.ErrRecordNotFound {
9602
-		errOne := service.AddSigleWarehouseOutInfo(warehouseOutInfo)
9602
+		errOne := service.AddSigleWarehouseOutInfoOne(warehouseOutInfo)
9603
 		//插入详情明细表
9603
 		//插入详情明细表
9604
 		stockFlow := models.VmStockFlow{
9604
 		stockFlow := models.VmStockFlow{
9605
 			WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
9605
 			WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
9853
 				StorehouseId:            houseConfig.StorehouseOutInfo,
9853
 				StorehouseId:            houseConfig.StorehouseOutInfo,
9854
 				IsCheck:                 1,
9854
 				IsCheck:                 1,
9855
 			}
9855
 			}
9856
-			err := service.AddSigleWarehouseOut(&warehouseOut)
9856
+			err := service.AddSigleWarehouseOutOne(&warehouseOut)
9857
 			if err != nil {
9857
 			if err != nil {
9858
 				utils.TraceLog("创建出库单失败 err = %v", err)
9858
 				utils.TraceLog("创建出库单失败 err = %v", err)
9859
 				return err
9859
 				return err
9933
 		warehouseOutInfo.Count = delete_count
9933
 		warehouseOutInfo.Count = delete_count
9934
 		stockInInfo, _ := service.FindLastStockInInfoRecord(good_yc.GoodId, orgID)
9934
 		stockInInfo, _ := service.FindLastStockInInfoRecord(good_yc.GoodId, orgID)
9935
 		warehouseOutInfo.Price = stockInInfo.Price
9935
 		warehouseOutInfo.Price = stockInInfo.Price
9936
-		errOne := service.AddSigleWarehouseOutInfo(warehouseOutInfo)
9936
+		errOne := service.AddSigleWarehouseOutInfoOne(warehouseOutInfo)
9937
 
9937
 
9938
 		if errOne != nil {
9938
 		if errOne != nil {
9939
 			return errOne
9939
 			return errOne
10070
 	_, errcod := service.GetWarehouseOutInfoIsExistOne(good_yc.GoodId, good_yc.PatientId, record_time, good_yc.ProjectId)
10070
 	_, errcod := service.GetWarehouseOutInfoIsExistOne(good_yc.GoodId, good_yc.PatientId, record_time, good_yc.ProjectId)
10071
 
10071
 
10072
 	if errcod == gorm.ErrRecordNotFound {
10072
 	if errcod == gorm.ErrRecordNotFound {
10073
-		errOne := service.AddSigleWarehouseOutInfo(warehouseOutInfo)
10073
+		errOne := service.AddSigleWarehouseOutInfoOne(warehouseOutInfo)
10074
 
10074
 
10075
 		//插入详情明细表
10075
 		//插入详情明细表
10076
 		if errOne != nil {
10076
 		if errOne != nil {

+ 1 - 1
controllers/mobile_api_controllers/dialysis_api_controller_extend.go View File

735
 	scheduleType, _ := this.GetInt64("schedule_type")
735
 	scheduleType, _ := this.GetInt64("schedule_type")
736
 	modeID, _ := this.GetInt64("mode")
736
 	modeID, _ := this.GetInt64("mode")
737
 	bedID, _ := this.GetInt64("bed")
737
 	bedID, _ := this.GetInt64("bed")
738
-	if patientID <= 0 || scheduleType < 1 || scheduleType > 3 || modeID < 1 || modeID > 14 || bedID <= 0 {
738
+	if patientID <= 0 || scheduleType < 1 || scheduleType > 3 || modeID < 1 || bedID <= 0 {
739
 		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
739
 		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
740
 		return
740
 		return
741
 	}
741
 	}

+ 9 - 8
controllers/secondary_order_api_contorller.go View File

1
 package controllers
1
 package controllers
2
 
2
 
3
 import (
3
 import (
4
-	"XT_New/enums"
5
-	"XT_New/models"
6
-	"XT_New/service"
7
-	"XT_New/utils"
8
 	"encoding/json"
4
 	"encoding/json"
9
 	"fmt"
5
 	"fmt"
10
-	"github.com/astaxie/beego"
11
-	"github.com/jinzhu/gorm"
12
 	"math/rand"
6
 	"math/rand"
13
 	"reflect"
7
 	"reflect"
14
 	"strconv"
8
 	"strconv"
15
 	"strings"
9
 	"strings"
16
 	"time"
10
 	"time"
11
+
12
+	"XT_New/enums"
13
+	"XT_New/models"
14
+	"XT_New/service"
15
+	"XT_New/utils"
16
+	"github.com/astaxie/beego"
17
+	"github.com/jinzhu/gorm"
17
 )
18
 )
18
 
19
 
19
 type SecondaryOrderApiController struct {
20
 type SecondaryOrderApiController struct {
1278
 					//查询今日是否存在调拨出库
1279
 					//查询今日是否存在调拨出库
1279
 					_, errcodes := service.FindStockOutByIsSysSix(orgId, 12, record_date, item.WarehouseId)
1280
 					_, errcodes := service.FindStockOutByIsSysSix(orgId, 12, record_date, item.WarehouseId)
1280
 					if errcodes == gorm.ErrRecordNotFound {
1281
 					if errcodes == gorm.ErrRecordNotFound {
1281
-						service.AddSigleWarehouseOut(&warehouseOut)
1282
+						service.AddSigleWarehouseOutOne(&warehouseOut)
1282
 					}
1283
 					}
1283
 
1284
 
1284
 					out, _ := service.GetLastWarehouseOutBySys(12, orgId, record_date, item.WarehouseId)
1285
 					out, _ := service.GetLastWarehouseOutBySys(12, orgId, record_date, item.WarehouseId)
1787
 				//查询今日是否存在调拨出库
1788
 				//查询今日是否存在调拨出库
1788
 				_, errcodes := service.FindStockOutByIsSysSix(orgId, 12, record_date, item.WarehouseId)
1789
 				_, errcodes := service.FindStockOutByIsSysSix(orgId, 12, record_date, item.WarehouseId)
1789
 				if errcodes == gorm.ErrRecordNotFound {
1790
 				if errcodes == gorm.ErrRecordNotFound {
1790
-					service.AddSigleWarehouseOut(&warehouseOut)
1791
+					service.AddSigleWarehouseOutOne(&warehouseOut)
1791
 				}
1792
 				}
1792
 
1793
 
1793
 				out, _ := service.GetLastWarehouseOutBySys(12, orgId, record_date, item.WarehouseId)
1794
 				out, _ := service.GetLastWarehouseOutBySys(12, orgId, record_date, item.WarehouseId)

+ 8 - 8
controllers/sign_api_controller.go View File

1287
 	//	}
1287
 	//	}
1288
 	//}
1288
 	//}
1289
 
1289
 
1290
-	adviceList, _ := service.GetHisDoctorAdviceNoCheck(10721)
1291
-
1292
-	if len(adviceList) > 0 {
1293
-		for _, item := range adviceList {
1294
-			checkObj, _ := service.GetDobuleCheckOne(item.UserOrgId, item.AdviceDate, item.PatientId)
1295
-			service.UpdateHisAdvice(item.PatientId, item.AdviceDate, item.UserOrgId, checkObj.Creater, checkObj.CheckDate)
1296
-		}
1297
-	}
1290
+	//adviceList, _ := service.GetHisDoctorAdviceNoCheck(10721)
1291
+	//
1292
+	//if len(adviceList) > 0 {
1293
+	//	for _, item := range adviceList {
1294
+	//		checkObj, _ := service.GetDobuleCheckOne(item.UserOrgId, item.AdviceDate, item.PatientId)
1295
+	//		service.UpdateHisAdvice(item.PatientId, item.AdviceDate, item.UserOrgId, checkObj.Creater, checkObj.CheckDate)
1296
+	//	}
1297
+	//}
1298
 
1298
 
1299
 	this.ServeSuccessJSON(map[string]interface{}{
1299
 	this.ServeSuccessJSON(map[string]interface{}{
1300
 		"msg": "ok",
1300
 		"msg": "ok",

+ 193 - 331
controllers/stock_in_api_controller.go View File

274
 	ctime := time.Now().Unix()
274
 	ctime := time.Now().Unix()
275
 	adminUserInfo := c.GetAdminUserInfo()
275
 	adminUserInfo := c.GetAdminUserInfo()
276
 
276
 
277
+	// 开始外部循环的事务
278
+	db := service.XTWriteDB()
279
+	tx := db.Begin()
280
+
281
+	// 在函数结束时处理事务回滚
282
+	defer func() {
283
+		if r := recover(); r != nil {
284
+			tx.Rollback()
285
+		}
286
+	}()
287
+
277
 	warehousingDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", warehousing_time)
288
 	warehousingDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", warehousing_time)
278
 	if parseDateErr != nil {
289
 	if parseDateErr != nil {
279
 		c.ErrorLog("日期(%v)解析错误:%v", warehousingDate, parseDateErr)
290
 		c.ErrorLog("日期(%v)解析错误:%v", warehousingDate, parseDateErr)
280
 		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
291
 		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
281
 		return
292
 		return
282
 	}
293
 	}
294
+
283
 	timeStr := time.Now().Format("2006-01-02")
295
 	timeStr := time.Now().Format("2006-01-02")
284
 	timeArr := strings.Split(timeStr, "-")
296
 	timeArr := strings.Split(timeStr, "-")
285
-	total, _ := service.FindAllWarehouseTotal(adminUserInfo.CurrentOrgId)
297
+	total, _ := service.FindNewAllWarehouseTotal(adminUserInfo.CurrentOrgId, tx)
286
 	total = total + 1
298
 	total = total + 1
287
 	warehousing_order := "RKD" + strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
299
 	warehousing_order := "RKD" + strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
288
 	operation_time := time.Now().Unix()
300
 	operation_time := time.Now().Unix()
300
 		IsCheck:          2,
312
 		IsCheck:          2,
301
 	}
313
 	}
302
 	if warehousing_info_id == 0 {
314
 	if warehousing_info_id == 0 {
303
-		service.AddSigleWarehouse(&warehousing)
315
+
316
+		service.AddNewSigleWarehouse(&warehousing, tx)
304
 	}
317
 	}
305
 	if warehousing_info_id > 0 {
318
 	if warehousing_info_id > 0 {
306
-		service.UpdateWarehouse(warehousing_info_id, storehouse_id)
319
+
320
+		service.UpdateNewWarehouse(warehousing_info_id, storehouse_id, tx)
307
 	}
321
 	}
308
-	info, _ := service.FindLastWarehousingSix(adminUserInfo.CurrentOrgId)
322
+	info, _ := service.FindNewLastWarehousingSix(adminUserInfo.CurrentOrgId, tx)
309
 	dataBody := make(map[string]interface{}, 0)
323
 	dataBody := make(map[string]interface{}, 0)
310
 	err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
324
 	err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
311
 	if err != nil {
325
 	if err != nil {
446
 
460
 
447
 	for _, item := range warehousingInfo {
461
 	for _, item := range warehousingInfo {
448
 		if item.ID == 0 {
462
 		if item.ID == 0 {
449
-			service.CreatedWarehouseingDetail(item)
463
+			service.CreatedNewWarehouseingDetail(item, tx)
450
 		}
464
 		}
451
 		if item.ID > 0 {
465
 		if item.ID > 0 {
452
-			service.UpdateWarehouseDetail(item, item.ID)
466
+
467
+			service.UpdateNewWarehouseDetail(item, item.ID, tx)
453
 		}
468
 		}
454
 
469
 
455
 	}
470
 	}
456
 
471
 
457
 	//查询入库数据
472
 	//查询入库数据
458
-	list, _ := service.GetGoodWarehouseInfoById(info.ID, info.OrgId)
473
+	list, _ := service.GetNewGoodWarehouseInfoById(info.ID, info.OrgId, tx)
474
+
475
+	tx.Commit()
459
 	dealerList, _ := service.GetAllDealerList(adminUserInfo.CurrentOrgId)
476
 	dealerList, _ := service.GetAllDealerList(adminUserInfo.CurrentOrgId)
460
 	manufacturerList, _ := service.GetAllManufacturerList(adminUserInfo.CurrentOrgId)
477
 	manufacturerList, _ := service.GetAllManufacturerList(adminUserInfo.CurrentOrgId)
461
 
478
 
560
 	}
577
 	}
561
 }
578
 }
562
 func (c *StockManagerApiController) EditWarehouse() {
579
 func (c *StockManagerApiController) EditWarehouse() {
580
+
581
+	// 开始外部循环的事务
582
+	db := service.XTWriteDB()
583
+	tx := db.Begin()
584
+
585
+	// 在函数结束时处理事务回滚
586
+	defer func() {
587
+		if r := recover(); r != nil {
588
+			tx.Rollback()
589
+		}
590
+	}()
591
+
563
 	warehousing_time := c.GetString("warehousing_time")
592
 	warehousing_time := c.GetString("warehousing_time")
564
 	id, _ := c.GetInt64("id", 0)
593
 	id, _ := c.GetInt64("id", 0)
565
 	types, _ := c.GetInt64("type", 0)
594
 	types, _ := c.GetInt64("type", 0)
580
 		return
609
 		return
581
 	}
610
 	}
582
 
611
 
583
-	warehouse, _ := service.FindWarehousingById(id)
612
+	warehouse, _ := service.FindNewWarehousingById(id, tx)
584
 
613
 
585
 	warehousing := models.Warehousing{
614
 	warehousing := models.Warehousing{
586
 		WarehousingOrder: warehouse.WarehousingOrder,
615
 		WarehousingOrder: warehouse.WarehousingOrder,
593
 		StorehouseId:     storehouse_id,
622
 		StorehouseId:     storehouse_id,
594
 	}
623
 	}
595
 
624
 
596
-	service.EditWarehousing(warehousing)
625
+	service.EditNewWarehousing(warehousing, tx)
597
 
626
 
598
 	dataBody := make(map[string]interface{}, 0)
627
 	dataBody := make(map[string]interface{}, 0)
599
 	err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
628
 	err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
605
 
634
 
606
 	var warehousingInfo []*models.WarehousingInfo
635
 	var warehousingInfo []*models.WarehousingInfo
607
 	var upDateWarehousingInfo []*models.WarehousingInfo
636
 	var upDateWarehousingInfo []*models.WarehousingInfo
608
-	var stockFlow []*models.VmStockFlow
609
-
610
 	if dataBody["stockIn"] != nil && reflect.TypeOf(dataBody["stockIn"]).String() == "[]interface {}" {
637
 	if dataBody["stockIn"] != nil && reflect.TypeOf(dataBody["stockIn"]).String() == "[]interface {}" {
611
 		thisStockIn, _ := dataBody["stockIn"].([]interface{})
638
 		thisStockIn, _ := dataBody["stockIn"].([]interface{})
612
 		if len(thisStockIn) > 0 {
639
 		if len(thisStockIn) > 0 {
753
 					}
780
 					}
754
 					upDateWarehousingInfo = append(upDateWarehousingInfo, warehouseInfo)
781
 					upDateWarehousingInfo = append(upDateWarehousingInfo, warehouseInfo)
755
 
782
 
756
-					flow := &models.VmStockFlow{
757
-						WarehousingOrder:    warehousing.WarehousingOrder,
758
-						WarehousingId:       id,
759
-						GoodId:              good_id,
760
-						Number:              number,
761
-						ProductDate:         productDates,
762
-						ExpireDate:          expiryDates,
763
-						Count:               warehousing_count,
764
-						Price:               price,
765
-						Status:              1,
766
-						Ctime:               ctime,
767
-						UserOrgId:           adminUserInfo.CurrentOrgId,
768
-						Manufacturer:        manufacturer,
769
-						Dealer:              dealer,
770
-						LicenseNumber:       license_number,
771
-						IsEdit:              2,
772
-						Creator:             adminUserInfo.AdminUser.Id,
773
-						SystemTime:          ctime,
774
-						ConsumableType:      1,
775
-						WarehousingDetailId: warehouseInfo.ID,
776
-						StorehouseId:        storehouse_id,
777
-						RegisterNumber:      register_number,
778
-						GoodCode:            good_code,
779
-					}
780
-
781
-					stockFlow = append(stockFlow, flow)
782
 				}
783
 				}
783
 			}
784
 			}
784
 		}
785
 		}
785
 	}
786
 	}
786
 	var errs error
787
 	var errs error
787
 	if len(warehousingInfo) > 0 {
788
 	if len(warehousingInfo) > 0 {
788
-		errs = service.CreateWarehousingInfo(warehousingInfo)
789
+
790
+		errs = service.CreateNewWarehousingInfo(warehousingInfo, tx)
789
 	}
791
 	}
790
 
792
 
791
 	if len(upDateWarehousingInfo) > 0 {
793
 	if len(upDateWarehousingInfo) > 0 {
792
 		for _, item := range upDateWarehousingInfo {
794
 		for _, item := range upDateWarehousingInfo {
793
-			service.UpdateWarehouseInfoByGoodIdOne(item, item.ID)
795
+			if item.ID > 0 {
796
+				service.UpdateNewWarehouseInfoByGoodIdOne(item, item.ID, tx)
797
+			}
798
+
794
 		}
799
 		}
795
 	}
800
 	}
796
 
801
 
800
 		return
805
 		return
801
 	}
806
 	}
802
 
807
 
803
-	list, _ := service.GetAllStockInList(id, adminUserInfo.CurrentOrgId)
808
+	list, _ := service.GetNewAllStockInList(id, adminUserInfo.CurrentOrgId, tx)
809
+	tx.Commit()
804
 	manufacturerList, _ := service.GetAllManufacturerList(adminUserInfo.CurrentOrgId)
810
 	manufacturerList, _ := service.GetAllManufacturerList(adminUserInfo.CurrentOrgId)
805
 	dealerList, _ := service.GetAllDealerList(adminUserInfo.CurrentOrgId)
811
 	dealerList, _ := service.GetAllDealerList(adminUserInfo.CurrentOrgId)
806
 	c.ServeSuccessJSON(map[string]interface{}{
812
 	c.ServeSuccessJSON(map[string]interface{}{
1258
 		return
1264
 		return
1259
 	}
1265
 	}
1260
 
1266
 
1267
+	// 开始外部循环的事务
1268
+	db := service.XTWriteDB()
1269
+	tx := db.Begin()
1270
+
1271
+	// 在函数结束时处理事务回滚
1272
+	defer func() {
1273
+		if r := recover(); r != nil {
1274
+			tx.Rollback()
1275
+		}
1276
+	}()
1277
+
1261
 	if warehouseOut_id == 0 {
1278
 	if warehouseOut_id == 0 {
1262
 		//查询是否生成出库单
1279
 		//查询是否生成出库单
1263
-		warehouseOutObj, _ := service.FindStockOutByIsSysOne(adminUserInfo.CurrentOrgId, 0, operation_time, warehousing_out_order)
1280
+		warehouseOutObj, _ := service.FindStockOutByIsSysOne(adminUserInfo.CurrentOrgId, 0, operation_time, warehousing_out_order, tx)
1264
 
1281
 
1265
 		if warehouseOutObj.ID == 0 {
1282
 		if warehouseOutObj.ID == 0 {
1266
-			service.AddSigleWarehouseOut(&warehouseOut)
1283
+			service.AddSigleWarehouseOut(&warehouseOut, tx)
1267
 		}
1284
 		}
1268
 
1285
 
1269
 	}
1286
 	}
1270
 
1287
 
1271
 	if warehouseOut_id > 0 {
1288
 	if warehouseOut_id > 0 {
1272
-		service.UpdateSingleWarehoseOut(warehouseOut_id, storehouse_id)
1289
+		service.UpdateSingleWarehoseOut(warehouseOut_id, storehouse_id, tx)
1273
 	}
1290
 	}
1274
 
1291
 
1275
 	var warehousingOutInfo []*models.WarehouseOutInfo
1292
 	var warehousingOutInfo []*models.WarehouseOutInfo
1407
 		}
1424
 		}
1408
 	}
1425
 	}
1409
 
1426
 
1410
-	goodWarehouseOut, _ := service.GetLastGoodWarehouseOutTwenty(adminUserInfo.CurrentOrgId)
1427
+	goodWarehouseOut, _ := service.GetLastGoodWarehouseOutTwentyOne(adminUserInfo.CurrentOrgId, tx)
1411
 
1428
 
1412
 	if len(warehousingOutInfo) > 0 {
1429
 	if len(warehousingOutInfo) > 0 {
1413
 		for _, it := range warehousingOutInfo {
1430
 		for _, it := range warehousingOutInfo {
1445
 			if it.ID == 0 {
1462
 			if it.ID == 0 {
1446
 
1463
 
1447
 				//查询改耗材是否出库
1464
 				//查询改耗材是否出库
1448
-				outInfo, _ := service.GetGoodsIsExsit(it.GoodId, it.SysRecordTime, it.OrgId, goodWarehouseOut.ID)
1465
+				outInfo, _ := service.GetGoodsIsExsit(it.GoodId, it.SysRecordTime, it.OrgId, goodWarehouseOut.ID, tx)
1449
 				if outInfo.ID == 0 {
1466
 				if outInfo.ID == 0 {
1450
-					service.AddSigleWarehouseOutInfo(warehouseOutInfo)
1467
+					service.AddSigleWarehouseOutInfo(warehouseOutInfo, tx)
1451
 				}
1468
 				}
1452
 
1469
 
1453
 			}
1470
 			}
1454
 			if it.ID > 0 {
1471
 			if it.ID > 0 {
1455
-				service.ModifyWarehouseOutInfo(it.ID, warehouseOutInfo)
1472
+				service.ModifyWarehouseOutInfo(it.ID, warehouseOutInfo, tx)
1456
 			}
1473
 			}
1457
 		}
1474
 		}
1458
 	}
1475
 	}
1459
 
1476
 
1460
 	appId := adminUserInfo.CurrentAppId
1477
 	appId := adminUserInfo.CurrentAppId
1461
 
1478
 
1462
-	warehouseOutInfoList, _ := service.GetLastGoodWarehouseOutInfo(adminUserInfo.CurrentOrgId, goodWarehouseOut.ID)
1479
+	warehouseOutInfoList, _ := service.GetLastGoodWarehouseOutInfo(adminUserInfo.CurrentOrgId, goodWarehouseOut.ID, tx)
1463
 	manufacturerList, _ := service.GetAllManufacturerList(adminUserInfo.CurrentOrgId)
1480
 	manufacturerList, _ := service.GetAllManufacturerList(adminUserInfo.CurrentOrgId)
1464
 	dealerList, _ := service.GetAllDealerList(adminUserInfo.CurrentOrgId)
1481
 	dealerList, _ := service.GetAllDealerList(adminUserInfo.CurrentOrgId)
1465
 	doctorList, _ := service.GetAllDoctorListSix(adminUserInfo.CurrentOrgId, appId)
1482
 	doctorList, _ := service.GetAllDoctorListSix(adminUserInfo.CurrentOrgId, appId)
1483
+	tx.Commit()
1466
 	c.ServeSuccessJSON(map[string]interface{}{
1484
 	c.ServeSuccessJSON(map[string]interface{}{
1467
 		"msg":                  "2",
1485
 		"msg":                  "2",
1468
 		"good_name":            "",
1486
 		"good_name":            "",
1618
 func (c *StockManagerApiController) GetWarehouseOutInfoList() {
1636
 func (c *StockManagerApiController) GetWarehouseOutInfoList() {
1619
 	id, _ := c.GetInt64("id", 0)
1637
 	id, _ := c.GetInt64("id", 0)
1620
 	warehouseOutInfo, _ := service.FindWarehouseOutInfoById(id)
1638
 	warehouseOutInfo, _ := service.FindWarehouseOutInfoById(id)
1621
-	warehouseOut, _ := service.FindWareHouseOutById(id)
1639
+	warehouseOut, _ := service.FindWareHouseOutByIdOne(id)
1622
 	adminUserInfo := c.GetAdminUserInfo()
1640
 	adminUserInfo := c.GetAdminUserInfo()
1623
 	orgId := adminUserInfo.CurrentOrgId
1641
 	orgId := adminUserInfo.CurrentOrgId
1624
 	wareoutList, _ := service.GetWareOutInfoById(id, orgId)
1642
 	wareoutList, _ := service.GetWareOutInfoById(id, orgId)
1630
 
1648
 
1631
 }
1649
 }
1632
 func (c *StockManagerApiController) EditWarehouseOut() {
1650
 func (c *StockManagerApiController) EditWarehouseOut() {
1651
+
1633
 	warehouse_out_time := c.GetString("warehouse_out_time")
1652
 	warehouse_out_time := c.GetString("warehouse_out_time")
1634
 	id, _ := c.GetInt64("id", 0)
1653
 	id, _ := c.GetInt64("id", 0)
1635
 	types, _ := c.GetInt64("type", 0)
1654
 	types, _ := c.GetInt64("type", 0)
1650
 		return
1669
 		return
1651
 	}
1670
 	}
1652
 
1671
 
1653
-	warehouseOut, _ := service.FindWareHouseOutById(id)
1672
+	// 开始外部循环的事务
1673
+	db := service.XTWriteDB()
1674
+	tx := db.Begin()
1675
+
1676
+	// 在函数结束时处理事务回滚
1677
+	defer func() {
1678
+		if r := recover(); r != nil {
1679
+			tx.Rollback()
1680
+		}
1681
+	}()
1682
+
1683
+	warehouseOut, _ := service.FindWareHouseOutById(id, tx)
1654
 
1684
 
1655
 	tempWarehouseOut := models.WarehouseOut{
1685
 	tempWarehouseOut := models.WarehouseOut{
1656
 		ID:               warehouseOut.ID,
1686
 		ID:               warehouseOut.ID,
1659
 		StorehouseId:     storehouse_id,
1689
 		StorehouseId:     storehouse_id,
1660
 	}
1690
 	}
1661
 
1691
 
1662
-	service.EditWarehouseOut(tempWarehouseOut)
1692
+	service.EditWarehouseOut(tempWarehouseOut, tx)
1663
 
1693
 
1664
 	dataBody := make(map[string]interface{}, 0)
1694
 	dataBody := make(map[string]interface{}, 0)
1665
 	err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
1695
 	err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
1871
 
1901
 
1872
 		for _, item := range warehousingOutInfo {
1902
 		for _, item := range warehousingOutInfo {
1873
 			//查询改耗材是否出库
1903
 			//查询改耗材是否出库
1874
-			outInfo, _ := service.GetGoodsIsExsit(item.GoodId, item.SysRecordTime, item.OrgId, item.WarehouseOutId)
1904
+			outInfo, _ := service.GetGoodsIsExsit(item.GoodId, item.SysRecordTime, item.OrgId, item.WarehouseOutId, tx)
1875
 			if outInfo.ID == 0 {
1905
 			if outInfo.ID == 0 {
1876
-				service.AddSigleWarehouseOutInfo(item)
1906
+				service.AddSigleWarehouseOutInfo(item, tx)
1877
 			}
1907
 			}
1878
 
1908
 
1879
 		}
1909
 		}
1881
 
1911
 
1882
 	if len(upDateWarehouseOutInfos) > 0 {
1912
 	if len(upDateWarehouseOutInfos) > 0 {
1883
 		for _, item := range upDateWarehouseOutInfos {
1913
 		for _, item := range upDateWarehouseOutInfos {
1884
-			service.UpDateWarehouseOutInfo(item)
1914
+			if item.ID > 0 {
1915
+				service.UpDateWarehouseOutInfo(item, tx)
1916
+			}
1917
+
1885
 		}
1918
 		}
1886
 	}
1919
 	}
1887
 
1920
 
1921
+	tx.Commit()
1888
 	if errs != nil {
1922
 	if errs != nil {
1889
 		utils.ErrorLog(errs.Error())
1923
 		utils.ErrorLog(errs.Error())
1890
 		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockInFail)
1924
 		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockInFail)
5755
 	_, errcodes := service.FindStockOutByIsSys(adminUserInfo.CurrentOrgId, 0, operation_time)
5789
 	_, errcodes := service.FindStockOutByIsSys(adminUserInfo.CurrentOrgId, 0, operation_time)
5756
 
5790
 
5757
 	if errcodes == gorm.ErrRecordNotFound {
5791
 	if errcodes == gorm.ErrRecordNotFound {
5758
-		service.AddSigleWarehouseOut(&warehouseOut)
5792
+		service.AddSigleWarehouseOutOne(&warehouseOut)
5759
 
5793
 
5760
 	}
5794
 	}
5761
 	out, _ := service.GetLastGoodWarehouseOut(adminUserInfo.CurrentOrgId)
5795
 	out, _ := service.GetLastGoodWarehouseOut(adminUserInfo.CurrentOrgId)
5796
 			LicenseNumber:           item.LicenseNumber,
5830
 			LicenseNumber:           item.LicenseNumber,
5797
 			GoodTypeId:              goodinfo.GoodTypeId,
5831
 			GoodTypeId:              goodinfo.GoodTypeId,
5798
 		}
5832
 		}
5799
-		errOne := service.AddSigleWarehouseOutInfo(warehouseOutInfo)
5800
-		fmt.Println("99999999999999", errOne)
5833
+		errOne := service.AddSigleWarehouseOutInfoOne(warehouseOutInfo)
5834
+		fmt.Println("", errOne)
5801
 
5835
 
5802
 		//更改库存
5836
 		//更改库存
5803
 		info, _ := service.GetWarehouseInfoById(item.WarehousingInfoId)
5837
 		info, _ := service.GetWarehouseInfoById(item.WarehousingInfoId)
6309
 				ProductDate:             warehouseInfo.ProductDate,
6343
 				ProductDate:             warehouseInfo.ProductDate,
6310
 				ExpireDate:              warehouseInfo.ExpiryDate,
6344
 				ExpireDate:              warehouseInfo.ExpiryDate,
6311
 			}
6345
 			}
6312
-			errOne := service.AddSigleWarehouseOutInfo(warehouseOutInfo)
6346
+			errOne := service.AddSigleWarehouseOutInfoOne(warehouseOutInfo)
6313
 			fmt.Println(errOne)
6347
 			fmt.Println(errOne)
6314
 			//创建出库明细
6348
 			//创建出库明细
6315
 			service.CreateStockFlowOne(stockFlow)
6349
 			service.CreateStockFlowOne(stockFlow)
7252
 
7286
 
7253
 	id, _ := this.GetInt64("id")
7287
 	id, _ := this.GetInt64("id")
7254
 	orgId := this.GetAdminUserInfo().CurrentOrgId
7288
 	orgId := this.GetAdminUserInfo().CurrentOrgId
7255
-	creater := this.GetAdminUserInfo().AdminUser.Id
7256
-	warehouseOut, _ := service.GetWarehouseById(id)
7289
+	// 开始外部循环的事务
7290
+	db := service.XTWriteDB()
7291
+	tx := db.Begin()
7292
+
7293
+	// 在函数结束时处理事务回滚
7294
+	defer func() {
7295
+		if r := recover(); r != nil {
7296
+			tx.Rollback()
7297
+		}
7298
+	}()
7299
+
7300
+	warehouseOut, _ := service.GetNewWarehouseById(id, tx)
7257
 
7301
 
7258
 	//查询已保存的单据
7302
 	//查询已保存的单据
7259
-	warehousingOutInfo, _ := service.GetWarehouseOutById(id, orgId)
7303
+	warehousingOutInfo, _ := service.GetNewWarehouseOutById(id, orgId, tx)
7260
 
7304
 
7261
 	////出库逻辑
7305
 	////出库逻辑
7262
 	for _, item := range warehousingOutInfo {
7306
 	for _, item := range warehousingOutInfo {
7263
 		// 查询该耗材是否有库存
7307
 		// 查询该耗材是否有库存
7264
-		warehouseOne, _ := service.FindWarehousingInfoTen(item.GoodId, item.StorehouseId)
7265
-		storehouse, _ := service.FindStoreHouseByStorehouseId(item.StorehouseId, item.OrgId)
7308
+		warehouseOne, _ := service.FindNewWarehousingInfoTen(item.GoodId, item.StorehouseId, tx)
7309
+
7310
+		storehouse, _ := service.FindNewStoreHouseByStorehouseId(item.StorehouseId, item.OrgId, tx)
7266
 
7311
 
7267
 		// 如果出库数量大于该批次剩余库存数量
7312
 		// 如果出库数量大于该批次剩余库存数量
7268
 		if item.Count > warehouseOne.StockCount {
7313
 		if item.Count > warehouseOne.StockCount {
7269
-
7270
-			goodErrcode := models.XtGoodErrcode{
7271
-				UserOrgId:      orgId,
7272
-				Errcode:        "出库数量大于该批次剩余库存",
7273
-				GoodId:         0,
7274
-				Status:         0,
7275
-				Ctime:          0,
7276
-				Mtime:          0,
7277
-				Count:          0,
7278
-				StockCount:     0,
7279
-				Creater:        creater,
7280
-				BatchNumberId:  warehouseOne.ID,
7281
-				WarehouseOutId: item.ID,
7282
-			}
7283
-			service.CreateGoodErrcode(goodErrcode)
7284
-			goodObj, _ := service.GetGoodInformationByGoodId(item.GoodId)
7314
+			goodObj, _ := service.GetNewGoodInformationByGoodId(item.GoodId, tx)
7315
+			tx.Rollback()
7285
 			this.ServeSuccessJSON(map[string]interface{}{
7316
 			this.ServeSuccessJSON(map[string]interface{}{
7286
 				"msg":                "1",
7317
 				"msg":                "1",
7287
 				"good_name":          goodObj.GoodName,
7318
 				"good_name":          goodObj.GoodName,
7297
 		creater := this.GetAdminUserInfo().AdminUser.Id
7328
 		creater := this.GetAdminUserInfo().AdminUser.Id
7298
 
7329
 
7299
 		//出库
7330
 		//出库
7300
-		err := service.ConsumablesDeliveryThirty(orgId, item.SysRecordTime, item, &warehouseOut, item.Count, creater)
7331
+		err := service.ConsumablesNewDeliveryThirty(orgId, item.SysRecordTime, item, &warehouseOut, item.Count, creater, tx)
7332
+
7301
 		if err != nil {
7333
 		if err != nil {
7302
-			goodErrcode := models.XtGoodErrcode{
7303
-				UserOrgId:      orgId,
7304
-				Errcode:        "调用出库接口报错",
7305
-				GoodId:         item.GoodId,
7306
-				Status:         1,
7307
-				Ctime:          time.Now().Unix(),
7308
-				Mtime:          0,
7309
-				Count:          0,
7310
-				StockCount:     0,
7311
-				Creater:        creater,
7312
-				BatchNumberId:  0,
7313
-				WarehouseOutId: 0,
7314
-			}
7315
-			service.CreateGoodErrcode(goodErrcode)
7334
+			tx.Rollback()
7335
+			return
7316
 		}
7336
 		}
7317
 
7337
 
7318
-		goodList, _ := service.GetSumGoodList(orgId, item.StorehouseId, item.GoodId)
7338
+		goodList, _ := service.GetNewSumGoodList(orgId, item.StorehouseId, item.GoodId, tx)
7319
 		var flush_count int64
7339
 		var flush_count int64
7320
 		for _, it := range goodList {
7340
 		for _, it := range goodList {
7321
 			flush_count += it.StockCount
7341
 			flush_count += it.StockCount
7322
 		}
7342
 		}
7323
-		errs := service.UpdateSumGood(orgId, item.StorehouseId, item.GoodId, flush_count)
7324
-		if errs != nil {
7325
-			goodErrcode := models.XtGoodErrcode{
7326
-				UserOrgId:      orgId,
7327
-				Errcode:        "手动出库更新剩余出库失败",
7328
-				GoodId:         item.GoodId,
7329
-				Status:         1,
7330
-				Ctime:          time.Now().Unix(),
7331
-				Mtime:          0,
7332
-				Count:          0,
7333
-				StockCount:     0,
7334
-				Creater:        creater,
7335
-				BatchNumberId:  0,
7336
-				WarehouseOutId: 0,
7337
-			}
7338
-			service.CreateGoodErrcode(goodErrcode)
7339
-		}
7343
+		service.UpdateNewSumGood(orgId, item.StorehouseId, item.GoodId, flush_count, tx)
7340
 
7344
 
7341
 		orgId := this.GetAdminUserInfo().CurrentOrgId
7345
 		orgId := this.GetAdminUserInfo().CurrentOrgId
7342
 		//查询默认仓库
7346
 		//查询默认仓库
7343
-		storeHouseConfig, _ := service.GetAllStoreHouseConfig(orgId)
7344
-		stockList, _ := service.GetStockCountByGoodId(item.GoodId, storeHouseConfig.StorehouseOutInfo, orgId)
7347
+		storeHouseConfig, _ := service.GetNewAllStoreHouseConfig(orgId, tx)
7348
+		stockList, _ := service.GetNewStockCountByGoodId(item.GoodId, storeHouseConfig.StorehouseOutInfo, orgId, tx)
7345
 		var total_count int64
7349
 		var total_count int64
7346
 		for _, it := range stockList {
7350
 		for _, it := range stockList {
7347
 			total_count += it.StockCount
7351
 			total_count += it.StockCount
7348
 		}
7352
 		}
7349
 		//基础库扣减库存
7353
 		//基础库扣减库存
7350
-		errsum := service.UpdateGoodInfoReduceSumCount(item.GoodId, total_count, orgId)
7351
-		if errsum != nil {
7352
-			goodErrcode := models.XtGoodErrcode{
7353
-				UserOrgId:      orgId,
7354
-				Errcode:        "基础库扣减库存报错",
7355
-				GoodId:         item.GoodId,
7356
-				Status:         1,
7357
-				Ctime:          time.Now().Unix(),
7358
-				Mtime:          0,
7359
-				Count:          0,
7360
-				StockCount:     0,
7361
-				Creater:        creater,
7362
-				BatchNumberId:  0,
7363
-				WarehouseOutId: 0,
7364
-			}
7365
-			service.CreateGoodErrcode(goodErrcode)
7366
-		}
7354
+		service.UpdateNewGoodInfoReduceSumCount(item.GoodId, total_count, orgId, tx)
7355
+
7367
 	}
7356
 	}
7368
 
7357
 
7369
 	//审核单据
7358
 	//审核单据
7370
-	err := service.UpdatedWarehouseOut(id)
7371
-	if err != nil {
7372
-		goodErrcode := models.XtGoodErrcode{
7373
-			UserOrgId:      orgId,
7374
-			Errcode:        "审核单据报错",
7375
-			GoodId:         0,
7376
-			Status:         1,
7377
-			Ctime:          time.Now().Unix(),
7378
-			Mtime:          0,
7379
-			Count:          0,
7380
-			StockCount:     0,
7381
-			Creater:        creater,
7382
-			BatchNumberId:  0,
7383
-			WarehouseOutId: 0,
7384
-		}
7385
-		service.CreateGoodErrcode(goodErrcode)
7386
-	}
7359
+	err := service.UpdatedNewWarehouseOut(id, tx)
7360
+
7361
+	tx.Commit()
7387
 	if err == nil {
7362
 	if err == nil {
7388
 		this.ServeSuccessJSON(map[string]interface{}{
7363
 		this.ServeSuccessJSON(map[string]interface{}{
7389
 			"msg":                "2",
7364
 			"msg":                "2",
7399
 
7374
 
7400
 	id, _ := this.GetInt64("id")
7375
 	id, _ := this.GetInt64("id")
7401
 	orgId := this.GetAdminUserInfo().CurrentOrgId
7376
 	orgId := this.GetAdminUserInfo().CurrentOrgId
7402
-	creater := this.GetAdminUserInfo().AdminUser.Id
7377
+
7378
+	// 开始外部循环的事务
7379
+	db := service.XTWriteDB()
7380
+	tx := db.Begin()
7381
+
7382
+	// 在函数结束时处理事务回滚
7383
+	defer func() {
7384
+		if r := recover(); r != nil {
7385
+			tx.Rollback()
7386
+		}
7387
+	}()
7403
 	// 查询出库数据
7388
 	// 查询出库数据
7404
-	list, err := service.GetWarehouseOutById(id, orgId)
7405
-	if err != nil {
7406
-		goodErrcode := models.XtGoodErrcode{
7407
-			UserOrgId:      orgId,
7408
-			Errcode:        "反审核查询出库单报错",
7409
-			GoodId:         0,
7410
-			Status:         1,
7411
-			Ctime:          time.Now().Unix(),
7412
-			Mtime:          0,
7413
-			Count:          0,
7414
-			StockCount:     0,
7415
-			Creater:        creater,
7416
-			BatchNumberId:  0,
7417
-			WarehouseOutId: 0,
7418
-		}
7419
-		service.CreateGoodErrcode(goodErrcode)
7420
-	}
7389
+	list, err := service.GetNewWarehouseOutById(id, orgId, tx)
7421
 	for _, item := range list {
7390
 	for _, item := range list {
7422
 		//回退库存
7391
 		//回退库存
7423
-		err := service.UpdateaGoodWarehouseInfo(item.Count, item.GoodId, item.OrgId, item.WarehouseInfotId)
7424
-		if err != nil {
7425
-			goodErrcode := models.XtGoodErrcode{
7426
-				UserOrgId:      orgId,
7427
-				Errcode:        "反审核回退库存报错",
7428
-				GoodId:         0,
7429
-				Status:         1,
7430
-				Ctime:          time.Now().Unix(),
7431
-				Mtime:          0,
7432
-				Count:          0,
7433
-				StockCount:     0,
7434
-				Creater:        creater,
7435
-				BatchNumberId:  0,
7436
-				WarehouseOutId: 0,
7437
-			}
7438
-			service.CreateGoodErrcode(goodErrcode)
7439
-		}
7392
+		service.UpdateaNewGoodWarehouseInfo(item.Count, item.GoodId, item.OrgId, item.WarehouseInfotId, tx)
7393
+
7440
 		//查询默认仓库
7394
 		//查询默认仓库
7441
-		storeHouseConfig, errs := service.GetAllStoreHouseConfig(orgId)
7442
-		if errs != nil {
7443
-			goodErrcode := models.XtGoodErrcode{
7444
-				UserOrgId:      orgId,
7445
-				Errcode:        "反审核查询默认仓库报错",
7446
-				GoodId:         0,
7447
-				Status:         1,
7448
-				Ctime:          time.Now().Unix(),
7449
-				Mtime:          0,
7450
-				Count:          0,
7451
-				StockCount:     0,
7452
-				Creater:        creater,
7453
-				BatchNumberId:  0,
7454
-				WarehouseOutId: 0,
7455
-			}
7456
-			service.CreateGoodErrcode(goodErrcode)
7457
-		}
7458
-		stockList, errcodes := service.GetStockCountByGoodId(item.GoodId, storeHouseConfig.StorehouseOutInfo, orgId)
7459
-		if errcodes != nil {
7460
-			goodErrcode := models.XtGoodErrcode{
7461
-				UserOrgId:      orgId,
7462
-				Errcode:        "反审核查询默认仓库库存报错",
7463
-				GoodId:         0,
7464
-				Status:         1,
7465
-				Ctime:          time.Now().Unix(),
7466
-				Mtime:          0,
7467
-				Count:          0,
7468
-				StockCount:     0,
7469
-				Creater:        creater,
7470
-				BatchNumberId:  0,
7471
-				WarehouseOutId: 0,
7472
-			}
7473
-			service.CreateGoodErrcode(goodErrcode)
7474
-		}
7395
+		storeHouseConfig, _ := service.GetNewAllStoreHouseConfig(orgId, tx)
7396
+
7397
+		stockList, _ := service.GetNewStockCountByGoodId(item.GoodId, storeHouseConfig.StorehouseOutInfo, orgId, tx)
7398
+
7475
 		var total_count int64
7399
 		var total_count int64
7476
 		for _, it := range stockList {
7400
 		for _, it := range stockList {
7477
 			total_count += it.StockCount
7401
 			total_count += it.StockCount
7478
 		}
7402
 		}
7479
 		//基础库扣减库存
7403
 		//基础库扣减库存
7480
-		errs = service.UpdateGoodInfoReduceSumCount(item.GoodId, total_count, orgId)
7481
-		if errs != nil {
7482
-			goodErrcode := models.XtGoodErrcode{
7483
-				UserOrgId:      orgId,
7484
-				Errcode:        "反审核基础库扣减库存报错",
7485
-				GoodId:         0,
7486
-				Status:         1,
7487
-				Ctime:          time.Now().Unix(),
7488
-				Mtime:          0,
7489
-				Count:          0,
7490
-				StockCount:     0,
7491
-				Creater:        creater,
7492
-				BatchNumberId:  0,
7493
-				WarehouseOutId: 0,
7494
-			}
7495
-			service.CreateGoodErrcode(goodErrcode)
7496
-		}
7404
+		service.UpdateNewGoodInfoReduceSumCount(item.GoodId, total_count, orgId, tx)
7497
 
7405
 
7498
-		goodList, _ := service.GetSumGoodList(orgId, item.StorehouseId, item.GoodId)
7406
+		goodList, _ := service.GetNewSumGoodList(orgId, item.StorehouseId, item.GoodId, tx)
7499
 		var flush_count int64
7407
 		var flush_count int64
7500
 		for _, it := range goodList {
7408
 		for _, it := range goodList {
7501
 			flush_count += it.StockCount
7409
 			flush_count += it.StockCount
7502
 		}
7410
 		}
7503
-		erros := service.UpdateSumGood(orgId, item.StorehouseId, item.GoodId, flush_count)
7504
-		if erros != nil {
7505
-			goodErrcode := models.XtGoodErrcode{
7506
-				UserOrgId:      orgId,
7507
-				Errcode:        "手动出库更新剩余库存报错",
7508
-				GoodId:         0,
7509
-				Status:         1,
7510
-				Ctime:          time.Now().Unix(),
7511
-				Mtime:          0,
7512
-				Count:          0,
7513
-				StockCount:     0,
7514
-				Creater:        creater,
7515
-				BatchNumberId:  0,
7516
-				WarehouseOutId: 0,
7517
-			}
7518
-			service.CreateGoodErrcode(goodErrcode)
7519
-		}
7411
+		service.UpdateNewSumGood(orgId, item.StorehouseId, item.GoodId, flush_count, tx)
7412
+
7520
 		//扣减出库数据
7413
 		//扣减出库数据
7521
-		errcoeds := service.ModifyReduceGoodSumCount(item.StorehouseId, item.Count, orgId, item.GoodId)
7522
-		if errcoeds != nil {
7523
-			goodErrcode := models.XtGoodErrcode{
7524
-				UserOrgId:      orgId,
7525
-				Errcode:        "反审核扣减出库数据报错",
7526
-				GoodId:         0,
7527
-				Status:         1,
7528
-				Ctime:          time.Now().Unix(),
7529
-				Mtime:          0,
7530
-				Count:          0,
7531
-				StockCount:     0,
7532
-				Creater:        creater,
7533
-				BatchNumberId:  0,
7534
-				WarehouseOutId: 0,
7535
-			}
7536
-			service.CreateGoodErrcode(goodErrcode)
7537
-		}
7414
+		service.ModifyNewReduceGoodSumCount(item.StorehouseId, item.Count, orgId, item.GoodId, tx)
7538
 
7415
 
7539
 	}
7416
 	}
7540
 	//删除流水,改变审核状态
7417
 	//删除流水,改变审核状态
7541
-	errcos := service.ModifyGoodWarehouseOut(id)
7542
-	if errcos != nil {
7543
-		goodErrcode := models.XtGoodErrcode{
7544
-			UserOrgId:      orgId,
7545
-			Errcode:        "反审核删除流水,改变审核状态报错",
7546
-			GoodId:         0,
7547
-			Status:         1,
7548
-			Ctime:          time.Now().Unix(),
7549
-			Mtime:          0,
7550
-			Count:          0,
7551
-			StockCount:     0,
7552
-			Creater:        creater,
7553
-			BatchNumberId:  0,
7554
-			WarehouseOutId: 0,
7555
-		}
7556
-		service.CreateGoodErrcode(goodErrcode)
7557
-	}
7418
+	service.ModifyNewGoodWarehouseOut(id, tx)
7419
+
7420
+	tx.Commit()
7558
 
7421
 
7559
 	if err == nil {
7422
 	if err == nil {
7560
 		this.ServeSuccessJSON(map[string]interface{}{
7423
 		this.ServeSuccessJSON(map[string]interface{}{
7568
 	warehousing_info_id, _ := this.GetInt64("warehousing_info_id")
7431
 	warehousing_info_id, _ := this.GetInt64("warehousing_info_id")
7569
 	orgId := this.GetAdminUserInfo().CurrentOrgId
7432
 	orgId := this.GetAdminUserInfo().CurrentOrgId
7570
 	Creator := this.GetAdminUserInfo().AdminUser.Id
7433
 	Creator := this.GetAdminUserInfo().AdminUser.Id
7434
+
7435
+	// 开始外部循环的事务
7436
+	db := service.XTWriteDB()
7437
+	tx := db.Begin()
7438
+
7439
+	// 在函数结束时处理事务回滚
7440
+	defer func() {
7441
+		if r := recover(); r != nil {
7442
+			tx.Rollback()
7443
+		}
7444
+	}()
7445
+
7571
 	//查询默认仓库
7446
 	//查询默认仓库
7572
-	storeHouseConfig, _ := service.GetAllStoreHouseConfig(orgId)
7447
+	storeHouseConfig, _ := service.GetNewAllStoreHouseConfig(orgId, tx)
7573
 
7448
 
7574
 	checker := this.GetAdminUserInfo().AdminUser.Id
7449
 	checker := this.GetAdminUserInfo().AdminUser.Id
7575
 	//更改审核状态
7450
 	//更改审核状态
7576
-	err := service.UpdateCheckWarehouseInfo(warehousing_info_id, checker)
7451
+	err := service.UpdateNewCheckWarehouseInfo(warehousing_info_id, checker, tx)
7577
 	if err == nil {
7452
 	if err == nil {
7578
-		list, _ := service.GetWarehouseInfoList(warehousing_info_id, orgId)
7453
+		list, _ := service.GetNewWarehouseInfoList(warehousing_info_id, orgId, tx)
7579
 
7454
 
7580
 		for _, item := range list {
7455
 		for _, item := range list {
7581
-			service.UpdateWarehouseInfoByIdListThree(item.WarehousingCount, item.ID)
7456
+			service.UpdateNewWarehouseInfoByIdListThree(item.WarehousingCount, item.ID, tx)
7582
 
7457
 
7583
 			//查询该耗材剩余库存库
7458
 			//查询该耗材剩余库存库
7584
-			stockList, _ := service.GetStockCountByGoodId(item.GoodId, storeHouseConfig.StorehouseOutInfo, orgId)
7459
+			stockList, _ := service.GetNewStockCountByGoodId(item.GoodId, storeHouseConfig.StorehouseOutInfo, orgId, tx)
7585
 			var total_count int64
7460
 			var total_count int64
7586
 			var sum_in_count int64
7461
 			var sum_in_count int64
7587
 			for _, it := range stockList {
7462
 			for _, it := range stockList {
7589
 				sum_in_count += it.WarehousingCount
7464
 				sum_in_count += it.WarehousingCount
7590
 			}
7465
 			}
7591
 			//基础库插入数据
7466
 			//基础库插入数据
7592
-			service.UpdateGoodInfoAddSumCount(item.GoodId, total_count, orgId, sum_in_count)
7467
+			service.UpdateNewGoodInfoAddSumCount(item.GoodId, total_count, orgId, sum_in_count, tx)
7593
 
7468
 
7594
 			//查询该仓库是否有数据
7469
 			//查询该仓库是否有数据
7595
-			_, errcode := service.GetGoodStockCount(orgId, item.StorehouseId, item.GoodId)
7596
-			goodList, _ := service.GetSumGoodList(orgId, item.StorehouseId, item.GoodId)
7470
+			_, errcode := service.GetNewGoodStockCount(orgId, item.StorehouseId, item.GoodId, tx)
7471
+
7472
+			goodList, _ := service.GetNewSumGoodList(orgId, item.StorehouseId, item.GoodId, tx)
7597
 			var flush_count int64
7473
 			var flush_count int64
7598
 			for _, it := range goodList {
7474
 			for _, it := range goodList {
7599
 				flush_count += it.StockCount
7475
 				flush_count += it.StockCount
7640
 				GoodCode:                item.GoodCode,
7516
 				GoodCode:                item.GoodCode,
7641
 			}
7517
 			}
7642
 			//创建出库流水
7518
 			//创建出库流水
7643
-			service.CreateStockFlowOne(stockFlow)
7519
+			service.CreateNewStockFlowOne(stockFlow, tx)
7644
 
7520
 
7645
 			if errcode == gorm.ErrRecordNotFound {
7521
 			if errcode == gorm.ErrRecordNotFound {
7646
 
7522
 
7657
 					FlushCount:       flush_count,
7533
 					FlushCount:       flush_count,
7658
 					StockActOutCount: 0,
7534
 					StockActOutCount: 0,
7659
 				}
7535
 				}
7660
-				service.CreateGoodCount(goodCount)
7536
+				service.CreateNewGoodCount(goodCount, tx)
7661
 			}
7537
 			}
7662
 			if errcode == nil {
7538
 			if errcode == nil {
7663
-				service.UpdateGoodStockCount(orgId, item.StorehouseId, item.GoodId, item.WarehousingCount, flush_count)
7539
+				service.UpdateNewGoodStockCount(orgId, item.StorehouseId, item.GoodId, item.WarehousingCount, flush_count, tx)
7664
 			}
7540
 			}
7665
 		}
7541
 		}
7666
 	}
7542
 	}
7667
 
7543
 
7544
+	tx.Commit()
7545
+
7668
 	if err == nil {
7546
 	if err == nil {
7669
 		this.ServeSuccessJSON(map[string]interface{}{
7547
 		this.ServeSuccessJSON(map[string]interface{}{
7670
 			"list": "list",
7548
 			"list": "list",
7676
 
7554
 
7677
 	warehousing_info_id, _ := this.GetInt64("warehousing_info_id")
7555
 	warehousing_info_id, _ := this.GetInt64("warehousing_info_id")
7678
 	orgId := this.GetAdminUserInfo().CurrentOrgId
7556
 	orgId := this.GetAdminUserInfo().CurrentOrgId
7557
+
7558
+	// 开始外部循环的事务
7559
+	db := service.XTWriteDB()
7560
+	tx := db.Begin()
7561
+
7562
+	// 在函数结束时处理事务回滚
7563
+	defer func() {
7564
+		if r := recover(); r != nil {
7565
+			tx.Rollback()
7566
+		}
7567
+	}()
7568
+
7679
 	//查询入库数据
7569
 	//查询入库数据
7680
-	list, _ := service.GetWarehouseInfoList(warehousing_info_id, orgId)
7570
+	list, _ := service.GetNewWarehouseInfoList(warehousing_info_id, orgId, tx)
7681
 	for _, item := range list {
7571
 	for _, item := range list {
7682
 		//查询该耗材 是否有出库数据
7572
 		//查询该耗材 是否有出库数据
7683
-		outlist, _ := service.GetWarehouseOutByWarehouseInfoId(item.ID, item.GoodId, item.OrgId)
7573
+		outlist, _ := service.GetNewWarehouseOutByWarehouseInfoId(item.ID, item.GoodId, item.OrgId, tx)
7684
 
7574
 
7685
 		if len(outlist) > 0 {
7575
 		if len(outlist) > 0 {
7576
+			tx.Rollback()
7686
 			this.ServeFailJSONWithSGJErrorCode(enums.ErrorCheckGoodParamWrong)
7577
 			this.ServeFailJSONWithSGJErrorCode(enums.ErrorCheckGoodParamWrong)
7687
 			return
7578
 			return
7688
 		}
7579
 		}
7689
 	}
7580
 	}
7690
-	//for _, item := range list {
7691
-	//
7692
-	//	//扣减数量
7693
-	//	service.UpdateWarehouseInfoByIdListTwo(item.WarehousingCount, item.ID)
7694
-	//
7695
-	//	//删除流水
7696
-	//	service.UpdateWarehouseInfoFlow(item.ID)
7697
-	//
7698
-	//	goodList, _ := service.GetSumGoodList(orgId, item.StorehouseId, item.GoodId)
7699
-	//	var flush_count int64
7700
-	//	for _, it := range goodList {
7701
-	//		flush_count += it.StockCount
7702
-	//	}
7703
-	//
7704
-	//	//扣减数量
7705
-	//	service.UpdateGoodInCount(item.WarehousingCount, item.OrgId, item.GoodId, item.StorehouseId, flush_count)
7706
-	//
7707
-	//	//查询默认仓库
7708
-	//	storeHouseConfig, _ := service.GetAllStoreHouseConfig(orgId)
7709
-	//	//查询该耗材剩余多少库存
7710
-	//	stockList, _ := service.GetStockCountByGoodId(item.GoodId, storeHouseConfig.StorehouseOutInfo, orgId)
7711
-	//	var sum_count int64
7712
-	//	var sum_in_count int64
7713
-	//	for _, it := range stockList {
7714
-	//		sum_count += it.StockCount
7715
-	//		sum_in_count += it.WarehousingCount
7716
-	//	}
7717
-	//	service.UpdateGoodInfoAddSumCount(item.GoodId, sum_count, orgId, sum_in_count)
7718
-	//}
7719
-	//err := service.ReturnCheckWarehouseInfo(warehousing_info_id)
7720
 
7581
 
7721
-	err := service.UpdateWarehouseInfoMap(list, warehousing_info_id, orgId)
7582
+	err := service.UpdateNewWarehouseInfoMap(list, warehousing_info_id, orgId, tx)
7583
+	tx.Commit()
7722
 	if err == nil {
7584
 	if err == nil {
7723
 		this.ServeSuccessJSON(map[string]interface{}{
7585
 		this.ServeSuccessJSON(map[string]interface{}{
7724
 			"msg": "反审核成功!",
7586
 			"msg": "反审核成功!",
8223
 	//	service.UpdateDealerManufacturerOne(item.ID, manufacturer.ManufacturerName)
8085
 	//	service.UpdateDealerManufacturerOne(item.ID, manufacturer.ManufacturerName)
8224
 	//}
8086
 	//}
8225
 
8087
 
8226
-	drugList, _ := service.GetAllBaseDrugListTwentyOne(10598)
8088
+	drugList, _ := service.GetAllBaseDrugListTwentyOne(10215)
8227
 
8089
 
8228
 	for _, item := range drugList {
8090
 	for _, item := range drugList {
8229
 		var drug_spec = item.Dose + item.DoseUnit + "*" + strconv.FormatInt(item.MinNumber, 10) + item.MinUnit + "/" + item.MaxUnit
8091
 		var drug_spec = item.Dose + item.DoseUnit + "*" + strconv.FormatInt(item.MinNumber, 10) + item.MinUnit + "/" + item.MaxUnit

+ 8 - 7
controllers/supply_order_api_contorller.go View File

1
 package controllers
1
 package controllers
2
 
2
 
3
 import (
3
 import (
4
-	"XT_New/enums"
5
-	"XT_New/models"
6
-	"XT_New/service"
7
-	"XT_New/utils"
8
 	"encoding/json"
4
 	"encoding/json"
9
 	"fmt"
5
 	"fmt"
10
-	"github.com/astaxie/beego"
11
-	"github.com/jinzhu/gorm"
12
 	"reflect"
6
 	"reflect"
13
 	"strconv"
7
 	"strconv"
14
 	"strings"
8
 	"strings"
15
 	"time"
9
 	"time"
10
+
11
+	"XT_New/enums"
12
+	"XT_New/models"
13
+	"XT_New/service"
14
+	"XT_New/utils"
15
+	"github.com/astaxie/beego"
16
+	"github.com/jinzhu/gorm"
16
 )
17
 )
17
 
18
 
18
 type SupplyOrderApiController struct {
19
 type SupplyOrderApiController struct {
3673
 			//查询是否生成出库单
3674
 			//查询是否生成出库单
3674
 			_, errcodes := service.FindStockOutByIsSys(orgId, 0, record_date)
3675
 			_, errcodes := service.FindStockOutByIsSys(orgId, 0, record_date)
3675
 			if errcodes == gorm.ErrRecordNotFound {
3676
 			if errcodes == gorm.ErrRecordNotFound {
3676
-				service.AddSigleWarehouseOut(&warehouseOut)
3677
+				service.AddSigleWarehouseOutOne(&warehouseOut)
3677
 			}
3678
 			}
3678
 			outWarehouse, _ := service.GetlastWarehouseOutById(orgId, record_date)
3679
 			outWarehouse, _ := service.GetlastWarehouseOutById(orgId, record_date)
3679
 
3680
 

+ 122 - 0
service/new_stock_service.go View File

19
 	return &goodStock, nil
19
 	return &goodStock, nil
20
 }
20
 }
21
 
21
 
22
+func GetNewGoodStockCount(user_org_id int64, storehouse_id int64, good_id int64, tx *gorm.DB) (*models.XtGoodStockCount, error) {
23
+
24
+	goodStock := models.XtGoodStockCount{}
25
+	var err error
26
+	err = tx.Where("user_org_id = ? and storehouse_id = ? and good_id = ?", user_org_id, storehouse_id, good_id).Find(&goodStock).Error
27
+	if err == gorm.ErrRecordNotFound {
28
+		if err != nil {
29
+			tx.Rollback()
30
+			return nil, err
31
+		}
32
+
33
+	}
34
+	if err != nil {
35
+		return nil, err
36
+	}
37
+	return &goodStock, nil
38
+}
39
+
22
 func CreateGoodCount(good models.XtGoodStockCount) error {
40
 func CreateGoodCount(good models.XtGoodStockCount) error {
23
 
41
 
24
 	db := writeDb.Begin()
42
 	db := writeDb.Begin()
31
 	return err
49
 	return err
32
 }
50
 }
33
 
51
 
52
+func CreateNewGoodCount(good models.XtGoodStockCount, tx *gorm.DB) error {
53
+
54
+	err := tx.Create(&good).Error
55
+	if err != nil {
56
+		tx.Rollback()
57
+		return err
58
+	}
59
+	return err
60
+}
61
+
34
 func UpdateGoodStockCount(user_org_id int64, storehouse_id int64, good_id int64, count int64, flush_count int64) error {
62
 func UpdateGoodStockCount(user_org_id int64, storehouse_id int64, good_id int64, count int64, flush_count int64) error {
35
 
63
 
36
 	db := writeDb.Begin()
64
 	db := writeDb.Begin()
48
 	return err
76
 	return err
49
 }
77
 }
50
 
78
 
79
+func UpdateNewGoodStockCount(user_org_id int64, storehouse_id int64, good_id int64, count int64, flush_count int64, tx *gorm.DB) error {
80
+
81
+	err = tx.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id  = ? and good_id  = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count + ?", count)).Error
82
+	if err != nil {
83
+		tx.Rollback()
84
+		return err
85
+	}
86
+	err = tx.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
87
+	if err != nil {
88
+		tx.Rollback()
89
+		return err
90
+	}
91
+
92
+	return err
93
+}
94
+
51
 func UpdateGoodInCount(count int64, user_org_id int64, good_id int64, storehouse_id int64, flush_count int64) error {
95
 func UpdateGoodInCount(count int64, user_org_id int64, good_id int64, storehouse_id int64, flush_count int64) error {
52
 	db := writeDb.Begin()
96
 	db := writeDb.Begin()
53
 	err := db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and good_id  = ? and storehouse_id = ? and status = 1", user_org_id, good_id, storehouse_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count - ?", count)).Error
97
 	err := db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and good_id  = ? and storehouse_id = ? and status = 1", user_org_id, good_id, storehouse_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count - ?", count)).Error
128
 	return info, err
172
 	return info, err
129
 }
173
 }
130
 
174
 
175
+func GetNewSumGoodList(orgid int64, storehouse_id int64, good_id int64, tx *gorm.DB) (info []*models.WarehousingInfo, err error) {
176
+
177
+	err = tx.Where("org_id = ? and storehouse_id = ? and good_id = ? and status =1  and is_check = 1", orgid, storehouse_id, good_id).Find(&info).Error
178
+	if err != gorm.ErrRecordNotFound {
179
+		if err != nil {
180
+			return info, err
181
+		}
182
+	}
183
+	return info, err
184
+}
185
+
131
 func UpdateSumGood(orgid int64, storehouse_id int64, good_id int64, flush_count int64) error {
186
 func UpdateSumGood(orgid int64, storehouse_id int64, good_id int64, flush_count int64) error {
132
 	db := writeDb.Begin()
187
 	db := writeDb.Begin()
133
 	err := db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", orgid, storehouse_id, good_id).Updates(map[string]interface{}{"flush_count": flush_count}).Error
188
 	err := db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", orgid, storehouse_id, good_id).Updates(map[string]interface{}{"flush_count": flush_count}).Error
139
 	return err
194
 	return err
140
 }
195
 }
141
 
196
 
197
+func UpdateNewSumGood(orgid int64, storehouse_id int64, good_id int64, flush_count int64, tx *gorm.DB) error {
198
+
199
+	err := tx.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", orgid, storehouse_id, good_id).Updates(map[string]interface{}{"flush_count": flush_count}).Error
200
+	if err != nil {
201
+		tx.Rollback()
202
+		return err
203
+	}
204
+	return err
205
+}
206
+
142
 func ReduceSumOutCount(good_id int64, total_count int64, storehouse_id int64, user_org_id int64) error {
207
 func ReduceSumOutCount(good_id int64, total_count int64, storehouse_id int64, user_org_id int64) error {
143
 	ut := XTWriteDB().Begin()
208
 	ut := XTWriteDB().Begin()
144
 
209
 
282
 	return &drugStockCount, nil
347
 	return &drugStockCount, nil
283
 }
348
 }
284
 
349
 
350
+func GetNewDrugStockCount(storehouse_id int64, drug_id int64, org_id int64, tx *gorm.DB) (*models.XtDrugStockCount, error) {
351
+	drugStockCount := models.XtDrugStockCount{}
352
+	var err error
353
+	err = tx.Where("storehouse_id = ? and drug_id = ? and user_org_id  = ? and status = 1", storehouse_id, drug_id, org_id).Find(&drugStockCount).Error
354
+	if err == gorm.ErrRecordNotFound {
355
+		if err != nil {
356
+			tx.Rollback()
357
+			return nil, err
358
+		}
359
+		return nil, err
360
+	}
361
+	if err != nil {
362
+		return nil, err
363
+	}
364
+	return &drugStockCount, nil
365
+}
366
+
285
 func GetDrugStockCountSix(storehouse_id int64, drug_id int64, org_id int64) (models.XtDrugStockCount, error) {
367
 func GetDrugStockCountSix(storehouse_id int64, drug_id int64, org_id int64) (models.XtDrugStockCount, error) {
286
 
368
 
287
 	drugStockCount := models.XtDrugStockCount{}
369
 	drugStockCount := models.XtDrugStockCount{}
301
 	return err
383
 	return err
302
 }
384
 }
303
 
385
 
386
+func CreateNewDrugStockSum(drug models.XtDrugStockCount, tx *gorm.DB) error {
387
+
388
+	err := tx.Create(&drug).Error
389
+	if err != nil {
390
+		tx.Rollback()
391
+		return err
392
+	}
393
+	return err
394
+}
395
+
304
 func AddDrugStockSum(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64) error {
396
 func AddDrugStockSum(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64) error {
305
 
397
 
306
 	db := writeDb.Begin()
398
 	db := writeDb.Begin()
334
 	return err
426
 	return err
335
 }
427
 }
336
 
428
 
429
+func UpdateNewDrugStockSum(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64, tx *gorm.DB) error {
430
+
431
+	err = tx.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id  = ? and drug_id  = ? and status = 1", user_org_id, storehouse_id, drug_id).Update(map[string]interface{}{"sum_in_count": waresing_count}).Error
432
+	if err != nil {
433
+		tx.Rollback()
434
+		return err
435
+	}
436
+	err = tx.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).Update(map[string]interface{}{"flush_count": over_count}).Error
437
+	if err != nil {
438
+		tx.Rollback()
439
+		return err
440
+	}
441
+	return err
442
+}
443
+
337
 func ReduceDrugStockCount(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64) error {
444
 func ReduceDrugStockCount(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64) error {
338
 	db := writeDb.Begin()
445
 	db := writeDb.Begin()
339
 	err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id  = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count - ?", waresing_count)).Error
446
 	err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id  = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count - ?", waresing_count)).Error
350
 	return err
457
 	return err
351
 }
458
 }
352
 
459
 
460
+func ReduceNewDrugStockCount(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64, tx *gorm.DB) error {
461
+
462
+	err = tx.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id  = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count - ?", waresing_count)).Error
463
+	if err != nil {
464
+		tx.Rollback()
465
+		return err
466
+	}
467
+	err = tx.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).Update(map[string]interface{}{"flush_count": over_count}).Error
468
+	if err != nil {
469
+		tx.Rollback()
470
+		return err
471
+	}
472
+	return err
473
+}
474
+
353
 func AddCancelSumCount(storehouse_id int64, drug_id int64, user_org_id int64, cancel_count int64, over_count int64) error {
475
 func AddCancelSumCount(storehouse_id int64, drug_id int64, user_org_id int64, cancel_count int64, over_count int64) error {
354
 
476
 
355
 	db := writeDb.Begin()
477
 	db := writeDb.Begin()

+ 17 - 2
service/new_warehouse_service.go View File

102
 		}
102
 		}
103
 		_, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
103
 		_, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
104
 		if errcodes == gorm.ErrRecordNotFound {
104
 		if errcodes == gorm.ErrRecordNotFound {
105
-			errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
105
+			errOne := AddSigleWarehouseOutInfoOne(warehouseOutInfo)
106
 			if errOne != nil {
106
 			if errOne != nil {
107
 				return errOne
107
 				return errOne
108
 			}
108
 			}
366
 		_, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
366
 		_, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
367
 
367
 
368
 		if errcodes == gorm.ErrRecordNotFound {
368
 		if errcodes == gorm.ErrRecordNotFound {
369
-			errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
369
+			errOne := AddSigleWarehouseOutInfoOne(warehouseOutInfo)
370
 			if errOne != nil {
370
 			if errOne != nil {
371
 				return errOne
371
 				return errOne
372
 			}
372
 			}
697
 	return err
697
 	return err
698
 }
698
 }
699
 
699
 
700
+func ReduceNewDrugCount(drug_id int64, org_id int64, storehouse_id int64, out_count int64, tx *gorm.DB) error {
701
+
702
+	err := tx.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", out_count)).Error
703
+	if err != nil {
704
+		tx.Rollback()
705
+		return err
706
+	}
707
+	err = tx.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count - ?", out_count)).Error
708
+	if err != nil {
709
+		tx.Rollback()
710
+		return err
711
+	}
712
+	return err
713
+}
714
+
700
 func ReduceDrugCountTwo(drug_id int64, org_id int64, storehouse_id int64, out_count int64) error {
715
 func ReduceDrugCountTwo(drug_id int64, org_id int64, storehouse_id int64, out_count int64) error {
701
 	ut := XTWriteDB().Begin()
716
 	ut := XTWriteDB().Begin()
702
 	err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", out_count)).Error
717
 	err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", out_count)).Error

+ 26 - 0
service/secondary_service.go View File

829
 
829
 
830
 	config := models.XtStorehouseConfig{}
830
 	config := models.XtStorehouseConfig{}
831
 	err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).First(&config).Error
831
 	err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).First(&config).Error
832
+
832
 	return config, err
833
 	return config, err
833
 }
834
 }
834
 
835
 
838
 	return storehouse, err
839
 	return storehouse, err
839
 }
840
 }
840
 
841
 
842
+func FindNewStoreHouseByStorehouseId(id int64, user_org_id int64, tx *gorm.DB) (models.XtStorehouse, error) {
843
+	storehouse := models.XtStorehouse{}
844
+	err := tx.Where("id = ? and user_org_id = ? and status = 1", id, user_org_id).Find(&storehouse).Error
845
+	if err != gorm.ErrRecordNotFound {
846
+		if err != nil {
847
+			tx.Rollback()
848
+			return storehouse, err
849
+		}
850
+	}
851
+	return storehouse, err
852
+}
853
+
841
 func GetSecondWarehouseByNumber(user_org_id int64, warehousing_order string) (*models.XtSecondWarehouse, error) {
854
 func GetSecondWarehouseByNumber(user_org_id int64, warehousing_order string) (*models.XtSecondWarehouse, error) {
842
 
855
 
843
 	warehouse := models.XtSecondWarehouse{}
856
 	warehouse := models.XtSecondWarehouse{}
1091
 	return info, err
1104
 	return info, err
1092
 }
1105
 }
1093
 
1106
 
1107
+func GetNewDrugSumCountByStorehouseId(storehouse_id int64, orgid int64, drug_id int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
1108
+
1109
+	err = tx.Where("storehouse_id = ? and org_id = ? and status = 1  and drug_id =? and is_check = 1", storehouse_id, orgid, drug_id).Find(&info).Error
1110
+	if err != gorm.ErrRecordNotFound {
1111
+		if err != nil {
1112
+			tx.Rollback()
1113
+			return info, err
1114
+		}
1115
+
1116
+	}
1117
+	return info, err
1118
+}
1119
+
1094
 func GetDrugSumCountByStorehouseIdSix(storehouse_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
1120
 func GetDrugSumCountByStorehouseIdSix(storehouse_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
1095
 
1121
 
1096
 	err = XTReadDB().Where("storehouse_id = ? and org_id = ? and status = 1 and (stock_max_number >0 or stock_min_number>0)", storehouse_id, orgid).Find(&info).Error
1122
 	err = XTReadDB().Where("storehouse_id = ? and org_id = ? and status = 1 and (stock_max_number >0 or stock_min_number>0)", storehouse_id, orgid).Find(&info).Error

+ 9 - 10
service/self_drug_service.go View File

138
 	return drug, err
138
 	return drug, err
139
 }
139
 }
140
 
140
 
141
-func GetDrugWarehosueInfoById(drug_id int64, org_id int64) (drugInfo []*models.XtDrugWarehouseInfo, err error) {
142
-
143
-	err = XTReadDB().Where("drug_id = ? and org_id = ? and status =1", drug_id, org_id).Find(&drugInfo).Error
144
-
145
-	return drugInfo, err
146
-}
147
-
148
-func GetBaseDrugMedicalOne(id int64, tx *gorm.DB) (models.XtBaseDrug, error) {
141
+func GetNewBaseDrugMedical(id int64, tx *gorm.DB) (models.XtBaseDrug, error) {
149
 	drug := models.XtBaseDrug{}
142
 	drug := models.XtBaseDrug{}
150
 	err := tx.Model(&drug).Where("id = ? and status = 1", id).Find(&drug).Error
143
 	err := tx.Model(&drug).Where("id = ? and status = 1", id).Find(&drug).Error
151
 	if err != gorm.ErrRecordNotFound {
144
 	if err != gorm.ErrRecordNotFound {
153
 			tx.Rollback()
146
 			tx.Rollback()
154
 			return drug, err
147
 			return drug, err
155
 		}
148
 		}
156
-
157
 	}
149
 	}
158
 	return drug, err
150
 	return drug, err
159
 }
151
 }
160
 
152
 
161
-func GetNewBaseDrugMedical(id int64, tx *gorm.DB) (models.XtBaseDrug, error) {
153
+func GetDrugWarehosueInfoById(drug_id int64, org_id int64) (drugInfo []*models.XtDrugWarehouseInfo, err error) {
154
+
155
+	err = XTReadDB().Where("drug_id = ? and org_id = ? and status =1", drug_id, org_id).Find(&drugInfo).Error
156
+
157
+	return drugInfo, err
158
+}
159
+
160
+func GetBaseDrugMedicalOne(id int64, tx *gorm.DB) (models.XtBaseDrug, error) {
162
 	drug := models.XtBaseDrug{}
161
 	drug := models.XtBaseDrug{}
163
 	err := tx.Model(&drug).Where("id = ? and status = 1", id).Find(&drug).Error
162
 	err := tx.Model(&drug).Where("id = ? and status = 1", id).Find(&drug).Error
164
 	if err != gorm.ErrRecordNotFound {
163
 	if err != gorm.ErrRecordNotFound {

File diff suppressed because it is too large
+ 1047 - 34
service/stock_service.go


+ 16 - 2
service/supply_service.go View File

1
 package service
1
 package service
2
 
2
 
3
 import (
3
 import (
4
-	"XT_New/models"
5
 	"fmt"
4
 	"fmt"
6
-	"github.com/jinzhu/gorm"
7
 	"strings"
5
 	"strings"
8
 	"time"
6
 	"time"
7
+
8
+	"XT_New/models"
9
+	"github.com/jinzhu/gorm"
9
 )
10
 )
10
 
11
 
11
 // 根据供应商编号获取首要联系人
12
 // 根据供应商编号获取首要联系人
1172
 	return out, err
1173
 	return out, err
1173
 }
1174
 }
1174
 
1175
 
1176
+func GetNewWarehouseOutById(id int64, orgid int64, tx *gorm.DB) (out []*models.WarehouseOutInfo, err error) {
1177
+
1178
+	err = tx.Where("warehouse_out_id = ? and status= 1 and org_id = ?", id, orgid).Find(&out).Error
1179
+
1180
+	if err != gorm.ErrRecordNotFound {
1181
+		if err != nil {
1182
+			tx.Rollback()
1183
+			return out, err
1184
+		}
1185
+	}
1186
+	return out, err
1187
+}
1188
+
1175
 func DeleteWarehouseOutById(id int64) error {
1189
 func DeleteWarehouseOutById(id int64) error {
1176
 
1190
 
1177
 	err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
1191
 	err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error

+ 574 - 16
service/warhouse_service.go View File

1384
 				StorehouseId:            houseConfig.StorehouseOutInfo,
1384
 				StorehouseId:            houseConfig.StorehouseOutInfo,
1385
 				IsCheck:                 1,
1385
 				IsCheck:                 1,
1386
 			}
1386
 			}
1387
-			err := AddSigleWarehouseOut(&warehouseOut)
1387
+			err := AddSigleWarehouseOutOne(&warehouseOut)
1388
 			if err != nil {
1388
 			if err != nil {
1389
 				utils.TraceLog("创建出库单失败 err = %v", err)
1389
 				utils.TraceLog("创建出库单失败 err = %v", err)
1390
 				return err
1390
 				return err
1536
 
1536
 
1537
 		_, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
1537
 		_, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
1538
 		if errcodes == gorm.ErrRecordNotFound {
1538
 		if errcodes == gorm.ErrRecordNotFound {
1539
-			errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
1539
+			errOne := AddSigleWarehouseOutInfoOne(warehouseOutInfo)
1540
 			if errOne != nil {
1540
 			if errOne != nil {
1541
 				return errOne
1541
 				return errOne
1542
 			}
1542
 			}
1769
 		_, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, goods.PatientId, record_time, goods.ProjectId)
1769
 		_, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, goods.PatientId, record_time, goods.ProjectId)
1770
 
1770
 
1771
 		if errcodes == gorm.ErrRecordNotFound {
1771
 		if errcodes == gorm.ErrRecordNotFound {
1772
-			errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
1772
+			errOne := AddSigleWarehouseOutInfoOne(warehouseOutInfo)
1773
 
1773
 
1774
 			if errOne != nil {
1774
 			if errOne != nil {
1775
 				return errOne
1775
 				return errOne
2166
 	return out, err
2166
 	return out, err
2167
 }
2167
 }
2168
 
2168
 
2169
+func GetNewWarehouseById(id int64, tx *gorm.DB) (models.WarehouseOut, error) {
2170
+	out := models.WarehouseOut{}
2171
+	err := tx.Where("id=? and status = 1", id).Find(&out).Error
2172
+	if err != gorm.ErrRecordNotFound {
2173
+		if err != nil {
2174
+			return out, err
2175
+		}
2176
+	}
2177
+	return out, err
2178
+}
2179
+
2169
 // 耗材出库
2180
 // 耗材出库
2170
 func ConsumablesDeliveryOne(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64) (err error) {
2181
 func ConsumablesDeliveryOne(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64) (err error) {
2171
 
2182
 
2220
 		}
2231
 		}
2221
 		warehouseOutInfo.Count = goods.Count
2232
 		warehouseOutInfo.Count = goods.Count
2222
 		//添加出库单详情
2233
 		//添加出库单详情
2223
-		errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
2234
+		errOne := AddSigleWarehouseOutInfoOne(warehouseOutInfo)
2224
 		if errOne != nil {
2235
 		if errOne != nil {
2225
 			return errOne
2236
 			return errOne
2226
 		}
2237
 		}
2335
 		info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
2346
 		info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
2336
 		if errcodes == gorm.ErrRecordNotFound {
2347
 		if errcodes == gorm.ErrRecordNotFound {
2337
 			//创建出库单
2348
 			//创建出库单
2338
-			errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
2349
+			errOne := AddSigleWarehouseOutInfoOne(warehouseOutInfo)
2339
 			if errOne != nil {
2350
 			if errOne != nil {
2340
 				return errOne
2351
 				return errOne
2341
 			}
2352
 			}
2460
 		}
2471
 		}
2461
 		warehouseOutInfo.Count = goods.Count
2472
 		warehouseOutInfo.Count = goods.Count
2462
 
2473
 
2463
-		errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
2474
+		errOne := AddSigleWarehouseOutInfoOne(warehouseOutInfo)
2464
 		if errOne != nil {
2475
 		if errOne != nil {
2465
 			return errOne
2476
 			return errOne
2466
 		}
2477
 		}
2516
 		}
2527
 		}
2517
 		warehouseOutInfo.Count = stock_number
2528
 		warehouseOutInfo.Count = stock_number
2518
 
2529
 
2519
-		errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
2530
+		errOne := AddSigleWarehouseOutInfoOne(warehouseOutInfo)
2520
 		if errOne != nil {
2531
 		if errOne != nil {
2521
 			return errOne
2532
 			return errOne
2522
 		}
2533
 		}
3520
 		}
3531
 		}
3521
 		warehouseOutInfo.Count = goods.Count
3532
 		warehouseOutInfo.Count = goods.Count
3522
 		//添加出库单详情
3533
 		//添加出库单详情
3523
-		errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
3534
+		errOne := AddSigleWarehouseOutInfoOne(warehouseOutInfo)
3524
 		if errOne != nil {
3535
 		if errOne != nil {
3525
 			return errOne
3536
 			return errOne
3526
 		}
3537
 		}
3615
 		info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
3626
 		info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
3616
 		if errcodes == gorm.ErrRecordNotFound {
3627
 		if errcodes == gorm.ErrRecordNotFound {
3617
 			//创建出库单
3628
 			//创建出库单
3618
-			errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
3629
+			errOne := AddSigleWarehouseOutInfoOne(warehouseOutInfo)
3619
 			if errOne != nil {
3630
 			if errOne != nil {
3620
 				return errOne
3631
 				return errOne
3621
 			}
3632
 			}
4050
 
4061
 
4051
 		warehouseOutInfo.Count = goods.Count
4062
 		warehouseOutInfo.Count = goods.Count
4052
 		//添加出库单详情
4063
 		//添加出库单详情
4053
-		errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
4064
+		errOne := AddSigleWarehouseOutInfoOne(warehouseOutInfo)
4054
 		if errOne != nil {
4065
 		if errOne != nil {
4055
 			return errOne
4066
 			return errOne
4056
 		}
4067
 		}
4225
 		info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
4236
 		info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
4226
 		if errcodes == gorm.ErrRecordNotFound {
4237
 		if errcodes == gorm.ErrRecordNotFound {
4227
 			//创建出库单
4238
 			//创建出库单
4228
-			errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
4239
+			errOne := AddSigleWarehouseOutInfoOne(warehouseOutInfo)
4229
 			if errOne != nil {
4240
 			if errOne != nil {
4230
 				return errOne
4241
 				return errOne
4231
 			}
4242
 			}
6100
 	return err
6111
 	return err
6101
 }
6112
 }
6102
 
6113
 
6114
+func UpdateNewDrugWarehouseOutInfo(id int64, warehouse_info_id int64, org_id int64, over_count int64, drug_price float64, tx *gorm.DB) error {
6115
+
6116
+	err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and org_id = ? and status = 1", id, org_id).Update(map[string]interface{}{"warehouse_info_id": warehouse_info_id, "over_count": over_count, "price": drug_price, "retail_price": drug_price}).Error
6117
+	if err != nil {
6118
+		tx.Rollback()
6119
+		return err
6120
+	}
6121
+	return err
6122
+}
6123
+
6103
 func UpdateGoodWarehouseOutById(id int64, warehouse_info_id int64, org_id int64, over_count int64) error {
6124
 func UpdateGoodWarehouseOutById(id int64, warehouse_info_id int64, org_id int64, over_count int64) error {
6104
 	utx := XTWriteDB().Begin()
6125
 	utx := XTWriteDB().Begin()
6105
 	err := utx.Model(models.WarehouseOutInfo{}).Where("id = ?  and org_id = ? and status = 1", id, org_id).Update(map[string]interface{}{"warehouse_info_id": warehouse_info_id, "over_count": over_count}).Error
6126
 	err := utx.Model(models.WarehouseOutInfo{}).Where("id = ?  and org_id = ? and status = 1", id, org_id).Update(map[string]interface{}{"warehouse_info_id": warehouse_info_id, "over_count": over_count}).Error
6227
 	return info, err
6248
 	return info, err
6228
 }
6249
 }
6229
 
6250
 
6251
+func FindNewDrugWarehouseInfoList(drugid int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
6252
+
6253
+	err = tx.Where("drug_id = ? and org_id = ? and status =1 and is_check =1  and (stock_max_number>0 or stock_min_number>0)", drugid, orgid).Find(&info).Error
6254
+	if err != gorm.ErrRecordNotFound {
6255
+		if err != nil {
6256
+			tx.Rollback()
6257
+			return info, err
6258
+		}
6259
+	}
6260
+	return info, err
6261
+}
6262
+
6230
 func CreatedDrugFlushInfo(info models.XtDrugWarehouseFlushInfo) error {
6263
 func CreatedDrugFlushInfo(info models.XtDrugWarehouseFlushInfo) error {
6231
 	ut := writeDb.Begin()
6264
 	ut := writeDb.Begin()
6232
 	err := ut.Create(&info).Error
6265
 	err := ut.Create(&info).Error
6238
 	return err
6271
 	return err
6239
 }
6272
 }
6240
 
6273
 
6274
+func CreatedNewDrugFlushInfo(info models.XtDrugWarehouseFlushInfo, tx *gorm.DB) error {
6275
+
6276
+	err := tx.Create(&info).Error
6277
+	if err != nil {
6278
+		tx.Rollback()
6279
+		return err
6280
+	}
6281
+	return err
6282
+}
6283
+
6241
 func UpdateWarehouseFlushInfo(warehouse_out_id int64, record_time int64, patient_id int64, batch_nubmer_id int64, good_id int64, sum_count int64, orgid int64) error {
6284
 func UpdateWarehouseFlushInfo(warehouse_out_id int64, record_time int64, patient_id int64, batch_nubmer_id int64, good_id int64, sum_count int64, orgid int64) error {
6242
 
6285
 
6243
 	err := XTWriteDB().Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id = ? and system_time = ? and patient_id = ? and batch_number_id = ? and good_id = ? and user_org_id = ?", warehouse_out_id, record_time, patient_id, batch_nubmer_id, good_id, orgid).Update(map[string]interface{}{"flush_count": sum_count}).Error
6286
 	err := XTWriteDB().Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id = ? and system_time = ? and patient_id = ? and batch_number_id = ? and good_id = ? and user_org_id = ?", warehouse_out_id, record_time, patient_id, batch_nubmer_id, good_id, orgid).Update(map[string]interface{}{"flush_count": sum_count}).Error
6323
 	return err
6366
 	return err
6324
 }
6367
 }
6325
 
6368
 
6369
+func ModifyNewReduceDrugInformation(id int64, count int64, orgid int64, tx *gorm.DB) error {
6370
+
6371
+	err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", count)).Error
6372
+	if err != nil {
6373
+		tx.Rollback()
6374
+		return err
6375
+	}
6376
+	return err
6377
+}
6378
+
6326
 func ModifyAddCancelAddInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
6379
 func ModifyAddCancelAddInfomation(id int64, count int64, orgid int64, storehouse_id int64) error {
6327
 	ut := XTWriteDB().Begin()
6380
 	ut := XTWriteDB().Begin()
6328
 	err = ut.Model(&models.XtDrugStockCount{}).Where("drug_id = ? and status = 1 and user_org_id = ? and storehouse_id = ?", id, orgid, storehouse_id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", count)).Error
6381
 	err = ut.Model(&models.XtDrugStockCount{}).Where("drug_id = ? and status = 1 and user_org_id = ? and storehouse_id = ?", id, orgid, storehouse_id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", count)).Error
6424
 	return err
6477
 	return err
6425
 }
6478
 }
6426
 
6479
 
6480
+func ModifyNewReduceGoodSumCount(storehouse_id int64, count int64, user_org_id int64, good_id int64, tx *gorm.DB) error {
6481
+
6482
+	err = tx.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id  = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count - ?", count)).Error
6483
+	if err != nil {
6484
+		tx.Rollback()
6485
+		return err
6486
+	}
6487
+
6488
+	err = tx.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id  = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_act_out_count", gorm.Expr("stock_act_out_count - ?", count)).Error
6489
+	if err != nil {
6490
+		tx.Rollback()
6491
+		return err
6492
+	}
6493
+
6494
+	return err
6495
+}
6496
+
6427
 func ModifyReduceGoodSumCountTwenty(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
6497
 func ModifyReduceGoodSumCountTwenty(storehouse_id int64, count int64, user_org_id int64, good_id int64) error {
6428
 	ut := XTWriteDB().Begin()
6498
 	ut := XTWriteDB().Begin()
6429
 
6499
 
6512
 	return info, err
6582
 	return info, err
6513
 }
6583
 }
6514
 
6584
 
6585
+func FindNewDrugWarehouseInfoFlushCount(drug_id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
6586
+
6587
+	err = tx.Where("drug_id = ? and org_id = ? and status = 1 and is_check = 1", drug_id, orgid).Find(&info).Error
6588
+	if err != gorm.ErrRecordNotFound {
6589
+		if err != nil {
6590
+			tx.Rollback()
6591
+			return info, err
6592
+		}
6593
+	}
6594
+	return info, err
6595
+}
6596
+
6515
 func FindDrugWarehouseInfoFlushCountByStorehouse(drug_id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
6597
 func FindDrugWarehouseInfoFlushCountByStorehouse(drug_id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
6516
 
6598
 
6517
 	err = XTReadDB().Where("drug_id = ? and org_id = ? and status = 1 and is_check = 1 and storehouse_id =?", drug_id, orgid, storehouse_id).Find(&info).Error
6599
 	err = XTReadDB().Where("drug_id = ? and org_id = ? and status = 1 and is_check = 1 and storehouse_id =?", drug_id, orgid, storehouse_id).Find(&info).Error
6518
 	return info, err
6600
 	return info, err
6519
 }
6601
 }
6520
 
6602
 
6603
+func FindNewDrugWarehouseInfoFlushCountByStorehouse(drug_id int64, orgid int64, storehouse_id int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
6604
+
6605
+	err = tx.Where("drug_id = ? and org_id = ? and status = 1 and is_check = 1 and storehouse_id =?", drug_id, orgid, storehouse_id).Find(&info).Error
6606
+	if err != gorm.ErrRecordNotFound {
6607
+		if err != nil {
6608
+			tx.Rollback()
6609
+			return info, err
6610
+		}
6611
+	}
6612
+	return info, err
6613
+}
6614
+
6521
 // 结算出库
6615
 // 结算出库
6522
 func HisSettleDrugsDelivery(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo, order_id int64) (err error) {
6616
 func HisSettleDrugsDelivery(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo, order_id int64) (err error) {
6523
 
6617
 
6719
 			RegisterNumber:          warehouse.RegisterNumber,
6813
 			RegisterNumber:          warehouse.RegisterNumber,
6720
 			OrderId:                 order_id,
6814
 			OrderId:                 order_id,
6721
 		}
6815
 		}
6722
-		fmt.Println("goods.count2323232233232233232322332322332", goods.Count)
6816
+
6723
 		OutInfo.Count = goods.Count
6817
 		OutInfo.Count = goods.Count
6724
 
6818
 
6725
-		AddSigleWarehouseOutInfo(OutInfo)
6819
+		AddSigleWarehouseOutInfoOne(OutInfo)
6726
 
6820
 
6727
 		stockFlow := models.VmStockFlow{
6821
 		stockFlow := models.VmStockFlow{
6728
 			WarehousingId:           warehouse.ID,
6822
 			WarehousingId:           warehouse.ID,
6905
 		}
6999
 		}
6906
 		OutInfo.Count = stock_number
7000
 		OutInfo.Count = stock_number
6907
 
7001
 
6908
-		AddSigleWarehouseOutInfo(OutInfo)
7002
+		AddSigleWarehouseOutInfoOne(OutInfo)
6909
 
7003
 
6910
 		stockFlow := models.VmStockFlow{
7004
 		stockFlow := models.VmStockFlow{
6911
 			WarehousingId:           warehouse.ID,
7005
 			WarehousingId:           warehouse.ID,
7159
 
7253
 
7160
 		_, errcodes := GetNewWarehouseOutInfoIsExistSix(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID)
7254
 		_, errcodes := GetNewWarehouseOutInfoIsExistSix(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID)
7161
 		if errcodes == gorm.ErrRecordNotFound {
7255
 		if errcodes == gorm.ErrRecordNotFound {
7162
-			errOne := AddSigleWarehouseOutInfo(OutInfo)
7256
+			errOne := AddSigleWarehouseOutInfoOne(OutInfo)
7163
 			if errOne != nil {
7257
 			if errOne != nil {
7164
 				return errOne
7258
 				return errOne
7165
 			}
7259
 			}
7477
 
7571
 
7478
 		_, errcodes := GetNewWarehouseOutInfoIsExistSix(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID)
7572
 		_, errcodes := GetNewWarehouseOutInfoIsExistSix(goods.GoodId, patient_id, record_time, goods.ProjectId, warehouse.ID)
7479
 		if errcodes == gorm.ErrRecordNotFound {
7573
 		if errcodes == gorm.ErrRecordNotFound {
7480
-			errOne := AddSigleWarehouseOutInfo(OutInfo)
7574
+			errOne := AddSigleWarehouseOutInfoOne(OutInfo)
7481
 			if errOne != nil {
7575
 			if errOne != nil {
7482
 				return errOne
7576
 				return errOne
7483
 			}
7577
 			}
10723
 
10817
 
10724
 	return
10818
 	return
10725
 }
10819
 }
10820
+
10821
+// 耗材手动出库
10822
+func ConsumablesNewDeliveryThirty(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64, tx *gorm.DB) (err error) {
10823
+
10824
+	var deliver_number int64 = 0
10825
+	var stock_number int64 = 0
10826
+	var maxNumber int64 = 0
10827
+
10828
+	deliver_number = goods.Count
10829
+
10830
+	// 根据先进先出原则,查询最先入库的批次,进行出库
10831
+
10832
+	// 如果没有对应的库存,则报错
10833
+	warehouse, err := FindNewFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId, tx)
10834
+
10835
+	stock_number = warehouse.StockCount
10836
+
10837
+	// 当库存数量大于或等于出库数量的话,则正常出库该批次
10838
+	if stock_number >= deliver_number {
10839
+		maxNumber = goods.Count
10840
+		warehouse.StockCount = warehouse.StockCount - maxNumber
10841
+		warehouse.Mtime = time.Now().Unix()
10842
+
10843
+		//扣减库存
10844
+		UpDateNewWarehouseInfoByStock(&warehouse, tx)
10845
+
10846
+		//查询剩余库存
10847
+		goodList, _ := GetNewAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId, tx)
10848
+		var sum_count int64
10849
+		for _, item := range goodList {
10850
+			sum_count += item.StockCount
10851
+		}
10852
+
10853
+		warehouseOutInfo := &models.WarehouseOutInfo{
10854
+			WarehouseInfotId: warehouse.ID,
10855
+		}
10856
+		//添加入库单详情ID
10857
+		UpdatedNewSigleWarehouseOutInfo(goods.ID, warehouseOutInfo, tx)
10858
+
10859
+		stockFlow := models.VmStockFlow{
10860
+			WarehousingId:           warehouse.ID,
10861
+			GoodId:                  goods.GoodId,
10862
+			Number:                  warehouse.Number,
10863
+			LicenseNumber:           goods.LicenseNumber,
10864
+			Count:                   goods.Count,
10865
+			UserOrgId:               goods.OrgId,
10866
+			PatientId:               goods.PatientId,
10867
+			SystemTime:              record_time,
10868
+			ConsumableType:          2,
10869
+			IsSys:                   goods.IsSys,
10870
+			WarehousingOrder:        warehouse.WarehousingOrder,
10871
+			WarehouseOutId:          warehouseOut.ID,
10872
+			WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
10873
+			IsEdit:                  1,
10874
+			CancelStockId:           0,
10875
+			CancelOrderNumber:       "",
10876
+			Manufacturer:            goods.Manufacturer,
10877
+			Dealer:                  goods.Dealer,
10878
+			Creator:                 creator,
10879
+			UpdateCreator:           0,
10880
+			Status:                  1,
10881
+			Ctime:                   record_time,
10882
+			Mtime:                   time.Now().Unix(),
10883
+			Price:                   warehouse.PackingPrice, //零售价
10884
+			WarehousingDetailId:     warehouse.ID,
10885
+			WarehouseOutDetailId:    goods.ID,
10886
+			CancelOutDetailId:       0,
10887
+			ProductDate:             goods.ProductDate,
10888
+			ExpireDate:              goods.ExpiryDate,
10889
+			SupplyWarehouseId:       warehouse.SupplyWarehouseId,
10890
+			StorehouseId:            warehouse.StorehouseId,
10891
+			AdminUserId:             goods.AdminUserId,
10892
+			BuyPrice:                warehouse.Price, //进货价
10893
+			StockCount:              goods.StockCount,
10894
+			BatchNumberCount:        warehouse.StockCount - goods.Count,
10895
+			IsCheck:                 1,
10896
+			OverCount:               sum_count,
10897
+			RegisterNumber:          goods.RegisterNumber,
10898
+			Remark:                  goods.Remark,
10899
+		}
10900
+		//创建出库流水
10901
+		CreateNewStockFlowOne(stockFlow, tx)
10902
+
10903
+		//添加入库ID
10904
+		UpdateNewGoodWarehouseOutById(goods.ID, warehouse.ID, goods.OrgId, sum_count, tx)
10905
+
10906
+		//更新出库数量
10907
+		ModifyNewGoodSumCount(goods.StorehouseId, goods.Count, goods.OrgId, goods.GoodId, tx)
10908
+
10909
+		if warehouse.StockCount < maxNumber {
10910
+			tx.Rollback()
10911
+			return errors.New("库存数量不足")
10912
+
10913
+		}
10914
+
10915
+		return nil
10916
+	} else {
10917
+		// 出库完成后,要将该批次库存清零
10918
+		warehouse.StockCount = 0
10919
+		warehouse.Mtime = time.Now().Unix()
10920
+
10921
+		UpDateNewWarehouseInfoByStock(&warehouse, tx)
10922
+
10923
+		//查询剩余库存
10924
+		goodList, _ := GetNewAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId, tx)
10925
+		var sum_count int64
10926
+		for _, item := range goodList {
10927
+			sum_count += item.StockCount
10928
+		}
10929
+
10930
+		stockFlow := models.VmStockFlow{
10931
+			WarehousingId:           warehouse.ID,
10932
+			GoodId:                  goods.GoodId,
10933
+			Number:                  warehouse.Number,
10934
+			LicenseNumber:           goods.LicenseNumber,
10935
+			Count:                   stock_number,
10936
+			UserOrgId:               goods.OrgId,
10937
+			PatientId:               goods.PatientId,
10938
+			SystemTime:              record_time,
10939
+			ConsumableType:          2,
10940
+			IsSys:                   goods.IsSys,
10941
+			WarehousingOrder:        warehouse.WarehousingOrder,
10942
+			WarehouseOutId:          warehouseOut.ID,
10943
+			WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
10944
+			IsEdit:                  1,
10945
+			CancelStockId:           0,
10946
+			CancelOrderNumber:       "",
10947
+			Manufacturer:            goods.Manufacturer,
10948
+			Dealer:                  goods.Dealer,
10949
+			Creator:                 creator,
10950
+			UpdateCreator:           0,
10951
+			Status:                  1,
10952
+			Ctime:                   record_time,
10953
+			Mtime:                   time.Now().Unix(),
10954
+			Price:                   warehouse.PackingPrice, //零售价
10955
+			WarehousingDetailId:     warehouse.ID,
10956
+			WarehouseOutDetailId:    goods.ID,
10957
+			CancelOutDetailId:       0,
10958
+			ProductDate:             goods.ProductDate,
10959
+			ExpireDate:              goods.ExpiryDate,
10960
+			SupplyWarehouseId:       warehouse.SupplyWarehouseId,
10961
+			StorehouseId:            warehouse.StorehouseId,
10962
+			AdminUserId:             goods.AdminUserId,
10963
+			BuyPrice:                warehouse.Price, //进货价
10964
+			StockCount:              goods.StockCount,
10965
+			BatchNumberCount:        0,
10966
+			IsCheck:                 1,
10967
+			OverCount:               sum_count,
10968
+			RegisterNumber:          goods.RegisterNumber,
10969
+			Remark:                  goods.Remark,
10970
+		}
10971
+		//创建出库流水
10972
+		CreateNewStockFlowOne(stockFlow, tx)
10973
+
10974
+		UpdateNewGoodWarehouseOutById(goods.ID, warehouse.ID, goods.OrgId, sum_count, tx)
10975
+
10976
+		//更新出库数量
10977
+		ModifyNewGoodSumCount(goods.StorehouseId, stock_number, goods.OrgId, goods.GoodId, tx)
10978
+
10979
+		goods.Count = deliver_number - stock_number
10980
+		ConsumablesNewDeliveryThirty(orgID, record_time, goods, warehouseOut, count, creator, tx)
10981
+	}
10982
+	return nil
10983
+}
10984
+
10985
+// 药品手动出库 递归方式
10986
+func AutoNewDrugDeliverInfoFourtyOne(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo, unit string, tx *gorm.DB) (err error) {
10987
+
10988
+	// 判断处方里药品单位是拆零单位还是包装单位,	如果是包装单位,则根据规格,将包装数量转为拆零数量
10989
+	var deliver_number int64 = 0
10990
+
10991
+	var stock_number int64 = 0
10992
+
10993
+	var drug_price float64
10994
+	if advice.CountUnit == drup.MaxUnit {
10995
+		deliver_number = prescribingNumber * drup.MinNumber
10996
+
10997
+	} else {
10998
+		deliver_number = prescribingNumber
10999
+
11000
+	}
11001
+	if advice.CountUnit == drup.MaxUnit {
11002
+		drug_price = drup.RetailPrice
11003
+	}
11004
+	fmt.Println(drug_price)
11005
+
11006
+	if advice.CountUnit == drup.MinUnit {
11007
+		drug_price = drup.MinPrice
11008
+	}
11009
+
11010
+	if advice.CountUnit != drup.MaxUnit || advice.CountUnit != drup.MinUnit {
11011
+		drug_price = drup.RetailPrice
11012
+	}
11013
+
11014
+	// 根据先进先出原则,查询最先入库的批次,进行出库
11015
+	// 如果没有对应的库存,则报错
11016
+	lastWarehouse, _ := FindNewLastDrugWarehousingInfoByID(advice.DrugId, advice.StorehouseId, tx)
11017
+
11018
+	if lastWarehouse.MaxUnit != lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
11019
+		if lastWarehouse.StockMinNumber >= drup.MinNumber {
11020
+			var stockMax int64
11021
+			var stockMin int64
11022
+			stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
11023
+			stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
11024
+
11025
+			ChangeNewMaxNumberOne(lastWarehouse.ID, stockMax, tx)
11026
+			UpdateNewMinNumber(lastWarehouse.ID, stockMin, tx)
11027
+
11028
+		}
11029
+	}
11030
+
11031
+	if lastWarehouse.MaxUnit == lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
11032
+
11033
+		lastWarehouse.StockMaxNumber = lastWarehouse.StockMaxNumber + lastWarehouse.StockMinNumber
11034
+		ChangeNewMaxNumberOne(lastWarehouse.ID, lastWarehouse.StockMaxNumber, tx)
11035
+
11036
+	}
11037
+
11038
+	warehouse, err := FindNewLastDrugWarehousingInfoByID(advice.DrugId, advice.StorehouseId, tx)
11039
+
11040
+	// 将该批次的剩余库存数量转换为拆零数量
11041
+	if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
11042
+
11043
+		stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
11044
+
11045
+	}
11046
+	if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
11047
+
11048
+		stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
11049
+
11050
+	}
11051
+	if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
11052
+
11053
+		stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
11054
+
11055
+	}
11056
+	if stock_number >= deliver_number {
11057
+
11058
+		var maxNumber int64 = 0
11059
+		var minNumber int64 = 0
11060
+
11061
+		maxNumber = deliver_number / drup.MinNumber
11062
+		minNumber = deliver_number % drup.MinNumber
11063
+
11064
+		if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
11065
+			minNumber = maxNumber
11066
+		}
11067
+
11068
+		if drup.MaxUnit != drup.MinUnit {
11069
+			if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
11070
+				return errors.New("库存数量不足")
11071
+			}
11072
+		}
11073
+
11074
+		if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
11075
+			//fmt.Println("金1")
11076
+			warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
11077
+		}
11078
+
11079
+		if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
11080
+			//fmt.Println("金2")
11081
+			warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
11082
+		}
11083
+		if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
11084
+			//fmt.Println("金3")
11085
+			warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
11086
+		}
11087
+
11088
+		if warehouse.StockMaxNumber < 0 {
11089
+			warehouse.StockMaxNumber = 0
11090
+		}
11091
+		if warehouse.StockMinNumber < 0 {
11092
+			warehouse.StockMinNumber = 0
11093
+		}
11094
+
11095
+		warehouse.Mtime = time.Now().Unix()
11096
+
11097
+		if warehouse.StockMinNumber < minNumber {
11098
+			//fmt.Println("金4")
11099
+			warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
11100
+			if warehouse.MaxUnit != warehouse.MinUnit {
11101
+				warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
11102
+			}
11103
+			if minNumber == 2 && warehouse.MaxUnit == warehouse.MinUnit && warehouse.StockMinNumber == 0 {
11104
+				warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
11105
+			}
11106
+		} else {
11107
+			//fmt.Println("金5")
11108
+			if minNumber > 0 {
11109
+				if (warehouse.StockMinNumber - minNumber) >= 0 {
11110
+					warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
11111
+				}
11112
+
11113
+			}
11114
+
11115
+			if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
11116
+				//fmt.Println("6")
11117
+				if warehouse.StockMinNumber > 0 {
11118
+					if warehouse.StockMinNumber-deliver_number >= 0 {
11119
+						warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
11120
+					}
11121
+
11122
+				}
11123
+
11124
+			}
11125
+
11126
+		}
11127
+
11128
+		if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit && warehouse.MaxUnit != warehouse.MinUnit {
11129
+			if (warehouse.StockMinNumber - deliver_number) >= 0 {
11130
+				//fmt.Println("金7")
11131
+				warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
11132
+			}
11133
+
11134
+		}
11135
+
11136
+		if drup.MaxUnit != drup.MinUnit {
11137
+			if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
11138
+				return errors.New("库存数量不足")
11139
+			}
11140
+		}
11141
+		if warehouse.StockMinNumber <= 0 {
11142
+			warehouse.StockMinNumber = 0
11143
+		}
11144
+
11145
+		errThree := UpDateNewDrugWarehouseInfoByStock(&warehouse, tx)
11146
+
11147
+		if errThree != nil {
11148
+			return errors.New("扣减库存失败")
11149
+		}
11150
+
11151
+		//查询该药品剩余库存
11152
+		infolist, _ := FindNewDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId, tx)
11153
+		var over_count int64
11154
+		for _, its := range infolist {
11155
+			if its.MaxUnit == drup.MaxUnit {
11156
+				its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
11157
+			}
11158
+			over_count += its.StockMaxNumber + its.StockMinNumber
11159
+		}
11160
+
11161
+		drugWareInfo, _ := GetLastNewWarehouseOutInfoById(advice.DrugId, tx)
11162
+		drugflow := models.DrugFlow{
11163
+			WarehouseOutId:          warehouseout.ID,
11164
+			WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
11165
+			DrugId:                  advice.DrugId,
11166
+			Number:                  warehouse.Number,
11167
+			ProductDate:             advice.ProductDate,
11168
+			ExpireDate:              advice.ExpiryDate,
11169
+			Count:                   deliver_number,
11170
+			Price:                   warehouse.RetailPrice,
11171
+			Status:                  1,
11172
+			Ctime:                   advice.SysRecordTime,
11173
+			Mtime:                   time.Now().Unix(),
11174
+			UserOrgId:               advice.OrgId,
11175
+			Manufacturer:            advice.Manufacturer,
11176
+			Dealer:                  advice.Dealer,
11177
+			BatchNumber:             warehouse.BatchNumber,
11178
+			MaxUnit:                 drup.MinUnit,
11179
+			ConsumableType:          2,
11180
+			IsEdit:                  1,
11181
+			Creator:                 0,
11182
+			IsSys:                   0,
11183
+			WarehouseOutDetailId:    drugWareInfo.ID,
11184
+			AdviceId:                advice.ID,
11185
+			SupplyWarehouseId:       warehouse.SupplyWarehouseId,
11186
+			StorehouseId:            advice.StorehouseId,
11187
+			AdminUserId:             advice.AdminUserId,
11188
+			LastPrice:               warehouse.Price,
11189
+			StockCount:              advice.StockCount,
11190
+			WarehousingDetailId:     warehouse.ID,
11191
+			OverCount:               over_count,
11192
+			RetailPrice:             drug_price, //手动出库出库价格
11193
+			WarehousingId:           warehouse.ID,
11194
+			SystemTime:              advice.SysRecordTime,
11195
+			PatientId:               advice.PatientId,
11196
+			Remark:                  advice.Remark,
11197
+		}
11198
+		if warehouse.RetailPrice == 0 {
11199
+			drugflow.Price = advice.Price
11200
+		}
11201
+
11202
+		CreateDrugFlowOne(drugflow)
11203
+
11204
+		//更新出库id
11205
+		UpdateNewDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId, over_count, drug_price, tx)
11206
+
11207
+		return nil
11208
+
11209
+	} else {
11210
+		stock_number_int := strconv.FormatInt(stock_number, 10)
11211
+		stock_number_int_count, _ := strconv.ParseFloat(stock_number_int, 64)
11212
+		drug_price = stock_number_int_count * advice.Price
11213
+		//扣减库存
11214
+		warehouse.StockMaxNumber = 0
11215
+		warehouse.StockMinNumber = 0
11216
+		warehouse.Mtime = time.Now().Unix()
11217
+
11218
+		errThree := UpDateNewDrugWarehouseInfoByStock(&warehouse, tx)
11219
+		if errThree != nil {
11220
+			return errThree
11221
+		}
11222
+
11223
+		//查询该药品剩余库存
11224
+		infolist, _ := FindNewDrugWarehouseInfoFlushCount(advice.DrugId, advice.OrgId, tx)
11225
+		var over_count int64
11226
+		for _, its := range infolist {
11227
+			if its.MaxUnit == drup.MaxUnit {
11228
+				its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
11229
+			}
11230
+			over_count += its.StockMaxNumber + its.StockMinNumber
11231
+		}
11232
+		drugWareInfo, _ := GetLastNewWarehouseOutInfoById(advice.DrugId, tx)
11233
+		drugflow := models.DrugFlow{
11234
+			WarehouseOutId:          warehouseout.ID,
11235
+			WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
11236
+			DrugId:                  advice.DrugId,
11237
+			Number:                  warehouse.Number,
11238
+			ProductDate:             advice.ProductDate,
11239
+			ExpireDate:              advice.ExpiryDate,
11240
+			Count:                   stock_number,
11241
+			Price:                   warehouse.RetailPrice,
11242
+			Status:                  1,
11243
+			Ctime:                   advice.SysRecordTime,
11244
+			Mtime:                   time.Now().Unix(),
11245
+			UserOrgId:               advice.OrgId,
11246
+			Manufacturer:            advice.Manufacturer,
11247
+			Dealer:                  advice.Dealer,
11248
+			BatchNumber:             warehouse.BatchNumber,
11249
+			MaxUnit:                 drup.MinUnit,
11250
+			ConsumableType:          2,
11251
+			IsEdit:                  1,
11252
+			Creator:                 0,
11253
+			IsSys:                   0,
11254
+			WarehouseOutDetailId:    drugWareInfo.ID,
11255
+			AdviceId:                advice.ID,
11256
+			SupplyWarehouseId:       warehouse.SupplyWarehouseId,
11257
+			StorehouseId:            advice.StorehouseId,
11258
+			AdminUserId:             advice.AdminUserId,
11259
+			LastPrice:               warehouse.Price,
11260
+			StockCount:              advice.StockCount,
11261
+			OverCount:               over_count,
11262
+			RetailPrice:             drug_price, //手动出库出库价格
11263
+			WarehousingId:           warehouse.ID,
11264
+			WarehousingDetailId:     warehouse.ID,
11265
+			SystemTime:              advice.SysRecordTime,
11266
+			PatientId:               advice.PatientId,
11267
+			Remark:                  advice.Remark,
11268
+		}
11269
+
11270
+		CreateNewDrugFlowOne(drugflow, tx)
11271
+		//更新出库id
11272
+		UpdateNewDrugWarehouseOutInfo(advice.ID, warehouse.ID, advice.OrgId, over_count, drug_price, tx)
11273
+		// 出库完成后,要将该批次库存清零
11274
+
11275
+		prescribingNumber_two_temp := deliver_number - stock_number
11276
+
11277
+		advice.CountUnit = drup.MinUnit
11278
+
11279
+		AutoNewDrugDeliverInfoFourtyOne(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, unit, tx)
11280
+	}
11281
+
11282
+	return
11283
+}