瀏覽代碼

押金管理

mainqaq 2 年之前
父節點
當前提交
173cc5f3be

+ 67 - 18
controllers/his_api_controller.go 查看文件

@@ -4520,9 +4520,40 @@ func (c *HisApiController) GetUploadInfo() {
4520 4520
 	reg_type, _ := c.GetInt64("p_type")
4521 4521
 	order_id, _ := c.GetInt64("order_id")
4522 4522
 
4523
-	his, _ := service.GetHisPatientByIdThree(his_patient_id)
4523
+	dec_way := c.GetString("dec_way") //是否使用押金支付bool类型
4524
+	tmp_decimal := c.GetString("tmp_decimal")
4525
+	var decimal float64 //本次使用的押金
4526
+	var errmsg error
4527
+	tx := service.XTWriteDB().Begin() //开了事务
4528
+	//只检测扣费函数SpendDeposit有没有返回错误,如果有则回滚所有以TX结尾的方法(去掉TX即为原先的方法)
4529
+	defer func() {
4530
+		if errmsg != nil {
4531
+			utils.ErrorLog("事务失败,原因为: %v", errmsg.Error())
4532
+			tx.Rollback()
4533
+		} else {
4534
+			tx.Commit()
4535
+		}
4536
+	}()
4537
+	if dec_way == "true" && tmp_decimal == "0" && tmp_decimal == "" {
4538
+		c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "押金不能为0")
4539
+		return
4540
+	}
4541
+	if len(tmp_decimal) > 0 {
4542
+		if tmp_decimal[0] == 45 {
4543
+			c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "押金不能为负数")
4544
+			return
4545
+		}
4546
+	}
4547
+	tt, errs := strconv.ParseFloat(tmp_decimal, 64)
4548
+	if errs != nil {
4549
+		c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "押金金额错误")
4550
+		return
4551
+	}
4552
+	decimal = tt
4524 4553
 
4525
-	tempOrder, _ := service.GetHisOrderByID(order_id)
4554
+	his, _ := service.GetHisPatientByIdThreeTX(his_patient_id, tx)
4555
+
4556
+	tempOrder, _ := service.GetHisOrderByIDTX(order_id, tx)
4526 4557
 
4527 4558
 	theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", record_time+" 00:00:00", loc)
4528 4559
 	if err != nil {
@@ -4543,9 +4574,9 @@ func (c *HisApiController) GetUploadInfo() {
4543 4574
 
4544 4575
 			if reg_type == 11 || reg_type == 1111 || reg_type == 1112 {
4545 4576
 
4546
-				prescriptions, _ = service.GetPrescriptionByIds(ids_arr, adminUser.CurrentOrgId)
4577
+				prescriptions, _ = service.GetPrescriptionByIdsTX(ids_arr, adminUser.CurrentOrgId, tx)
4547 4578
 			} else {
4548
-				prescriptions, _ = service.GetSettleHisPrescription(adminUser.CurrentOrgId, id, his_patient_id, recordDateTime, reg_type)
4579
+				prescriptions, _ = service.GetSettleHisPrescriptionTX(adminUser.CurrentOrgId, id, his_patient_id, recordDateTime, reg_type, tx)
4549 4580
 			}
4550 4581
 		} else { //月结
4551 4582
 
@@ -4567,9 +4598,8 @@ func (c *HisApiController) GetUploadInfo() {
4567 4598
 			}
4568 4599
 			recordEndTime := theEndTime.Unix()
4569 4600
 			end_time = recordEndTime
4570
-			prescriptions, _ = service.GetMonthHisPrescription(adminUser.CurrentOrgId, id, recordStartTime, recordEndTime, reg_type)
4601
+			prescriptions, _ = service.GetMonthHisPrescriptionTX(adminUser.CurrentOrgId, id, recordStartTime, recordEndTime, reg_type, tx)
4571 4602
 		}
4572
-
4573 4603
 		timestamp := time.Now().Unix()
4574 4604
 		tempTime := time.Unix(timestamp, 0)
4575 4605
 		timeFormat := tempTime.Format("20060102150405")
@@ -4611,7 +4641,7 @@ func (c *HisApiController) GetUploadInfo() {
4611 4641
 
4612 4642
 		allTotal := fmt.Sprintf("%.4f", total)
4613 4643
 		totals, _ := strconv.ParseFloat(allTotal, 64)
4614
-		order := &models.HisOrder{
4644
+		order := &models.HisOrder{ //
4615 4645
 			UserOrgId:             adminUser.CurrentOrgId,
4616 4646
 			HisPatientId:          his.ID,
4617 4647
 			PatientId:             id,
@@ -4641,8 +4671,9 @@ func (c *HisApiController) GetUploadInfo() {
4641 4671
 			Diagnosis:             diagnosis_id,
4642 4672
 			PType:                 2,
4643 4673
 			SetlTime:              tm.Format("2006-01-02 15:04:05"),
4674
+			Decimal:               decimal,
4644 4675
 		}
4645
-		err = service.CreateOrder(order)
4676
+		err = service.CreateOrderTX(order, tx)
4646 4677
 		if err != nil {
4647 4678
 			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateOrderException)
4648 4679
 			return
@@ -4738,16 +4769,28 @@ func (c *HisApiController) GetUploadInfo() {
4738 4769
 				Type:             types,
4739 4770
 				ItemId:           item_id,
4740 4771
 			}
4741
-			service.CreateOrderInfo(info)
4772
+			service.CreateOrderInfoTX(info, tx)
4742 4773
 		}
4743 4774
 		his.Diagnosis = diagnosis_id
4744 4775
 		his.SickType = sick_type
4745 4776
 		his.RegisterType = reg_type
4746 4777
 		his.MedicalTreatmentType = reg_type
4747
-		service.UpdataHisPateint(&his)
4748
-		err = service.UpDatePrescriptionNumber(adminUser.CurrentOrgId, ids, chrg_bchno)
4749
-		err = service.UpDateHisPrescriptionInfoNumber(adminUser.CurrentOrgId, id, chrg_bchno, recordDateTime, his_patient_id)
4750
-		err = service.UpdataOrderStatusTwo(chrg_bchno, adminUser.CurrentOrgId)
4778
+		service.UpdataHisPateintTX(&his, tx)
4779
+		err = service.UpDatePrescriptionNumberTX(adminUser.CurrentOrgId, ids, chrg_bchno, tx)
4780
+		err = service.UpDateHisPrescriptionInfoNumberTX(adminUser.CurrentOrgId, id, chrg_bchno, recordDateTime, his_patient_id, tx)
4781
+		err = service.UpdataOrderStatusTwoTX(chrg_bchno, adminUser.CurrentOrgId, tx)
4782
+		//判断是否使用了押金
4783
+		if dec_way == "true" {
4784
+
4785
+			tmpstring := strconv.FormatInt(order.ID, 10)
4786
+			//扣押金
4787
+			errmsg = service.SpendDeposit(c.GetAdminUserInfo().CurrentOrgId, id, c.GetAdminUserInfo().AdminUser.Id, tmpstring, decimal)
4788
+			if errmsg != nil {
4789
+				c.ServeFailJsonSend(enums.ErrorCodeParamWrong, errmsg.Error())
4790
+				//c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateOrderException)
4791
+				return
4792
+			}
4793
+		}
4751 4794
 		if err == nil {
4752 4795
 			c.ServeSuccessJSON(map[string]interface{}{
4753 4796
 				"msg": "结算成功",
@@ -4792,15 +4835,21 @@ func (c *HisApiController) Refund() {
4792 4835
 	order_id, _ := c.GetInt64("order_id")
4793 4836
 	order, _ := service.GetHisOrderByID(order_id)
4794 4837
 	adminUser := c.GetAdminUserInfo()
4838
+	orgid := c.GetAdminUserInfo().CurrentOrgId
4795 4839
 	err := service.UpdataOrderStatus(order_id, order.Number, adminUser.CurrentOrgId)
4796
-	if err == nil {
4797
-		c.ServeSuccessJSON(map[string]interface{}{
4798
-			"msg": "退费成功",
4799
-		})
4800
-	} else {
4840
+	if err != nil {
4801 4841
 		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
4802 4842
 		return
4803 4843
 	}
4844
+	tmp := strconv.FormatInt(order.ID, 10)
4845
+	err = service.MoneyIncrease(orgid, order.HisPatientId, tmp, order.Decimal)
4846
+	if err != nil {
4847
+		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
4848
+		return
4849
+	}
4850
+	c.ServeSuccessJSON(map[string]interface{}{
4851
+		"msg": "退费成功",
4852
+	})
4804 4853
 }
4805 4854
 
4806 4855
 func (c *HisApiController) RefundNumber() {

+ 544 - 30
controllers/his_deposit_controller.go 查看文件

@@ -9,6 +9,7 @@ import (
9 9
 	"fmt"
10 10
 	"github.com/astaxie/beego"
11 11
 	"github.com/shopspring/decimal"
12
+	"strconv"
12 13
 	"strings"
13 14
 	"time"
14 15
 )
@@ -18,27 +19,45 @@ type HisDepositApiController struct {
18 19
 }
19 20
 
20 21
 func HisDepositApiRegistRouters() {
21
-	beego.Router("/api/his/ttt", &HisDepositApiController{}, "get:TTT")                         //测试接口
22
-	beego.Router("/api/his/gethisuser", &HisDepositApiController{}, "get:GetHisUser")           //获取病例中心,有效患者名称
23
-	beego.Router("/api/his/adddeposit", &HisDepositApiController{}, "post:AddDeposit")          //新增押金
24
-	beego.Router("/api/his/getdepositcode", &HisDepositApiController{}, "get:GetDepositCode")   //获取新增押金编号
25
-	beego.Router("/api/his/getdeletecode", &HisDepositApiController{}, "get:GetDeleteCode")     //获取退款编号
26
-	beego.Router("/api/his/rechargedetails", &HisDepositApiController{}, "get:RechargeDetails") //充值明细列表
27
-	beego.Router("/api/his/updeposit", &HisDepositApiController{}, "get:UpDeposit")             //审核
28
-	beego.Router("/api/his/deletehistory", &HisDepositApiController{}, "get:DeleteHistory")     //删除
29
-	beego.Router("/api/his/rechargesummary", &HisDepositApiController{}, "get:RechargeSummary") //充值汇总列表
30
-	beego.Router("/api/his/getuserlist", &HisDepositApiController{}, "get:GetUserList")         //获取患者押金列表
31
-	beego.Router("/api/his/depositflow", &HisDepositApiController{}, "get:DepositFlow")         //根据患者id获取押金流水
32
-}
33
-func (this *HisDepositApiController) TTT() {
34
-	id, _ := this.GetInt64("id")
35
-	err := service.DelDecimalHistory(id)
36
-	this.ServeSuccessJSON(map[string]interface{}{
37
-		"list": err,
38
-	})
39
-	return
22
+	//beego.Router("/api/his/ttt", &HisDepositApiController{}, "get:TTT")                         //测试接口
23
+	beego.Router("/api/his/gethisuser", &HisDepositApiController{}, "get:GetHisUser")                //获取病例中心,有效患者名称
24
+	beego.Router("/api/his/adddeposit", &HisDepositApiController{}, "post:AddDeposit")               //新增押金
25
+	beego.Router("/api/his/getdepositcode", &HisDepositApiController{}, "get:GetDepositCode")        //获取新增押金编号
26
+	beego.Router("/api/his/getdeletecode", &HisDepositApiController{}, "get:GetDeleteCode")          //获取退款编号
27
+	beego.Router("/api/his/rechargedetails", &HisDepositApiController{}, "get:RechargeDetails")      //充值明细列表
28
+	beego.Router("/api/his/updeposit", &HisDepositApiController{}, "get:UpDeposit")                  //押金审核
29
+	beego.Router("/api/his/deletehistory", &HisDepositApiController{}, "get:DeleteHistory")          //押金删除
30
+	beego.Router("/api/his/rechargesummary", &HisDepositApiController{}, "get:RechargeSummary")      //充值汇总列表
31
+	beego.Router("/api/his/getuserlist", &HisDepositApiController{}, "get:GetUserList")              //获取患者押金列表
32
+	beego.Router("/api/his/depositflow", &HisDepositApiController{}, "get:DepositFlow")              //根据患者id获取押金流水
33
+	beego.Router("/api/his/deductiondetails", &HisDepositApiController{}, "get:DeductionDetails")    //扣费明细查询
34
+	beego.Router("/api/his/desummary", &HisDepositApiController{}, "get:DeSummary")                  //扣费汇总
35
+	beego.Router("/api/his/idtobalance", &HisDepositApiController{}, "get:IdToBalance")              //根据患者id查询患者的余额
36
+	beego.Router("/api/his/refundapplication", &HisDepositApiController{}, "post:RefundApplication") //新增一条退款申请
37
+	beego.Router("/api/his/refundreview", &HisDepositApiController{}, "get:RefundReview")            //退款审核通过/拒绝
38
+	beego.Router("/api/his/deleterefund", &HisDepositApiController{}, "get:DeleteRefund")            //退款删除
39
+	beego.Router("/api/his/changerefund", &HisDepositApiController{}, "post:ChangeRefund")           //更改退款申请
40
+	beego.Router("/api/his/refundlist", &HisDepositApiController{}, "get:RefundList")                //退款分页
41
+	beego.Router("/api/his/getorgname", &HisDepositApiController{}, "get:GetorgName")                //获取供应商名字
42
+	beego.Router("/api/his/getweektime", &HisDepositApiController{}, "get:GetWeekTime")              //获取本周时间
43
+	beego.Router("/api/his/getmonthtime", &HisDepositApiController{}, "get:GetMonthTime")            //获取本月时间
40 44
 }
41 45
 
46
+//func (this *HisDepositApiController) TTT() {
47
+//	orgid := this.GetAdminUserInfo().CurrentOrgId
48
+//	id, _ := this.GetInt64("id")
49
+//	code := this.GetString("code")
50
+//	deposit,_ := this.GetInt64("depostit")
51
+//	err := service.MoneyIncrease(orgid,id,code,deposit)
52
+//	if err != nil {
53
+//
54
+//	}
55
+//	this.ServeSuccessJSON(map[string]interface{}{
56
+//		"list": "成功",
57
+//	})
58
+//	return
59
+//}
60
+
42 61
 //获取病例中心,有效患者名称
43 62
 func (this *HisDepositApiController) GetHisUser() {
44 63
 	orgid := this.GetAdminUserInfo().CurrentOrgId
@@ -67,19 +86,43 @@ func (this *HisDepositApiController) AddDeposit() {
67 86
 	case dataBody["code"] == nil:
68 87
 		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "押金编号不能为空")
69 88
 		return
70
-	case dataBody["his_patient_id"] == nil:
89
+	case dataBody["his_patient_id"] == nil || dataBody["his_patient_id"] == "":
71 90
 		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "患者名称不能为空")
72 91
 		return
73
-	case dataBody["deposit"] == nil:
92
+	case dataBody["deposit"] == nil || dataBody["deposit"] == "":
74 93
 		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "押金金额不能为空")
75 94
 		return
76
-	case dataBody["trial_status"] == nil:
95
+	case dataBody["trial_status"] == nil || dataBody["trial_status"] == "":
77 96
 		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "审核状态不能为空")
78 97
 		return
79 98
 	}
80
-	code := dataBody["code"].(string)                              //押金编号
81
-	his_patient_id := int64(dataBody["his_patient_id"].(float64))  //患者编号
82
-	deposit := decimal.NewFromFloat(dataBody["deposit"].(float64)) //押金金额
99
+	code := dataBody["code"].(string)                             //押金编号
100
+	his_patient_id := int64(dataBody["his_patient_id"].(float64)) //患者编号
101
+	var tmps float64
102
+	switch dataBody["deposit"].(type) {
103
+	case string:
104
+		t_deposit := dataBody["deposit"].(string)
105
+		if len(t_deposit) > 0 {
106
+			if t_deposit[0] == 45 {
107
+				this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "押金金额错误")
108
+				return
109
+			}
110
+		}
111
+		tt, errs := strconv.ParseFloat(t_deposit, 64)
112
+		if errs != nil {
113
+			this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "押金金额错误")
114
+			return
115
+		} else {
116
+			tmps = tt
117
+		}
118
+	default:
119
+		tmps = dataBody["deposit"].(float64)
120
+	}
121
+	if tmps == 0 {
122
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "押金金额不能为0")
123
+		return
124
+	}
125
+	deposit := decimal.NewFromFloat(tmps) //押金金额
83 126
 	var remarks string
84 127
 	if dataBody["remarks"] == nil {
85 128
 		remarks = ""
@@ -149,11 +192,11 @@ func (this *HisDepositApiController) RechargeDetails() {
149 192
 
150 193
 	namemap := make(map[int64]string)
151 194
 	slicekey := make([]int64, 0)
195
+	lists, _ := service.GetHisUser(orgid)
196
+	for _, v := range lists {
197
+		namemap[v.ID] = v.Name
198
+	}
152 199
 	if len(keyword) > 0 {
153
-		list, _ := service.GetHisUser(orgid)
154
-		for _, v := range list {
155
-			namemap[v.ID] = v.Name
156
-		}
157 200
 		for k, v := range namemap {
158 201
 			res := strings.Contains(v, keyword)
159 202
 			if res == true {
@@ -181,6 +224,8 @@ func (this *HisDepositApiController) RechargeDetails() {
181 224
 			sum = sum.Add(list[i].Deposit)
182 225
 		}
183 226
 		list[i].Name = service.GetCreateidName(list[i].CreateId)
227
+		list[i].HisName = namemap[list[i].HisPatientId]
228
+		list[i].Starttime = fmt.Sprintf(time.Unix(list[i].Ctime, 0).Format("2006-01-02"))
184 229
 	}
185 230
 	this.ServeSuccessJSON(map[string]interface{}{
186 231
 		"list": list,
@@ -366,6 +411,13 @@ func (this *HisDepositApiController) DepositFlow() {
366 411
 		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, errs.Error())
367 412
 		return
368 413
 	}
414
+	for i := 0; i < len(deposirhistory); i++ {
415
+		//如果为扣费,或退费 则把订单id变为就诊号
416
+		if deposirhistory[i].DepositStatus == 2 || deposirhistory[i].DepositStatus == 4 {
417
+			tmpcode, _ := strconv.ParseInt(deposirhistory[i].DepositCode, 10, 64)
418
+			deposirhistory[i].DepositCode = service.FindcodeToid(tmpcode)
419
+		}
420
+	}
369 421
 
370 422
 	this.ServeSuccessJSON(map[string]interface{}{
371 423
 		"list":    deposirhistory,
@@ -375,7 +427,7 @@ func (this *HisDepositApiController) DepositFlow() {
375 427
 	return
376 428
 }
377 429
 
378
-//
430
+//获取患者押金列表
379 431
 func (this *HisDepositApiController) GetUserList() {
380 432
 	orgid := this.GetAdminUserInfo().CurrentOrgId
381 433
 	keyword := this.GetString("keyword")
@@ -420,6 +472,468 @@ func (this *HisDepositApiController) GetUserList() {
420 472
 	return
421 473
 }
422 474
 
475
+//扣费明细查询
476
+func (this *HisDepositApiController) DeductionDetails() {
477
+	orgid := this.GetAdminUserInfo().CurrentOrgId
478
+	timeLayout := "2006-01-02"
479
+	loc, _ := time.LoadLocation("Local")
480
+	keyword := this.GetString("keyword")
481
+	start_time := this.GetString("start_time")
482
+	end_time := this.GetString("end_time")
483
+	var stime int64 //开始时间
484
+	var etime int64 //结束时间
485
+
486
+	namemap := make(map[int64]string)
487
+	slicekey := make([]int64, 0)
488
+	lists, _ := service.GetHisUser(orgid)
489
+	for _, v := range lists {
490
+		namemap[v.ID] = v.Name
491
+	}
492
+	if len(keyword) > 0 {
493
+		for k, v := range namemap {
494
+			res := strings.Contains(v, keyword)
495
+			if res == true {
496
+				slicekey = append(slicekey, k)
497
+			}
498
+		}
499
+	}
500
+	if start_time == "" && end_time == "" {
501
+		stime, etime = service.GetMondayOfWeek()
502
+	} else {
503
+		stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
504
+		etmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
505
+		stime = stmp.Unix()
506
+		etime = etmp.Unix()
507
+	}
508
+	list, err := service.DeductionList(orgid, stime, etime, keyword, slicekey)
509
+	if err != nil {
510
+		utils.ErrorLog(err.Error())
511
+		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
512
+		return
513
+	}
514
+
515
+	var sum decimal.Decimal
516
+	var detailslist []models.Details
517
+	var details models.Details
518
+	for i := 0; i < len(list); i++ {
519
+		sum = sum.Add(list[i].Deposit)
520
+		tmp_id, _ := strconv.ParseInt(list[i].DepositCode, 10, 64)
521
+		details.ID = tmp_id
522
+		details.Code = service.FindcodeToid(tmp_id)
523
+		details.NameId = list[i].HisPatientId
524
+		details.Name = namemap[list[i].HisPatientId]
525
+		details.Number = service.FindnumberToid(tmp_id)
526
+		details.Decimal = list[i].Deposit
527
+		details.ChargeDate = fmt.Sprintf(time.Unix(list[i].Ctime, 0).Format("2006-01-02 15:04:05"))
528
+		details.Chargetype = service.CodeToChargetype(orgid, details.Code)
529
+		details.Total = service.MedicalTotal(orgid, list[i].HisPatientId, details.Code)
530
+		detailslist = append(detailslist, details)
531
+	}
532
+
533
+	this.ServeSuccessJSON(map[string]interface{}{
534
+		"list": detailslist,
535
+		"sum":  sum,
536
+	})
537
+	return
538
+}
539
+
540
+//扣费汇总
541
+func (this *HisDepositApiController) DeSummary() {
542
+	orgid := this.GetAdminUserInfo().CurrentOrgId
543
+	timeLayout := "2006-01-02"
544
+	loc, _ := time.LoadLocation("Local")
545
+	keyword := this.GetString("keyword")
546
+	start_time := this.GetString("start_time")
547
+	end_time := this.GetString("end_time")
548
+	var stime int64 //开始时间
549
+	var etime int64 //结束时间
550
+
551
+	namemap := make(map[int64]string)
552
+	slicekey := make([]int64, 0)
553
+	lists, _ := service.GetHisUser(orgid)
554
+	for _, v := range lists {
555
+		namemap[v.ID] = v.Name
556
+	}
557
+	if len(keyword) > 0 {
558
+		for k, v := range namemap {
559
+			res := strings.Contains(v, keyword)
560
+			if res == true {
561
+				slicekey = append(slicekey, k)
562
+			}
563
+		}
564
+	}
565
+	if start_time == "" && end_time == "" {
566
+		stime, etime = service.GetMondayOfWeek()
567
+	} else {
568
+		stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
569
+		etmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
570
+		stime = stmp.Unix()
571
+		etime = etmp.Unix()
572
+	}
573
+	list, err := service.DeductionList(orgid, stime, etime, keyword, slicekey)
574
+	if err != nil {
575
+		utils.ErrorLog(err.Error())
576
+		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
577
+		return
578
+	}
579
+	var sum decimal.Decimal
580
+	var detailslist []models.DeductionSummary
581
+	var details models.DeductionSummary
582
+	for i := 0; i < len(list); i++ {
583
+		sum = sum.Add(list[i].Deposit)
584
+		details.Name = namemap[list[i].HisPatientId]
585
+		details.Decimal = list[i].Deposit
586
+		details.Total = service.MedicalTotal(orgid, list[i].HisPatientId, list[i].DepositCode)
587
+		detailslist = append(detailslist, details)
588
+	}
589
+	maplist := make(map[string]models.DeductionSummary)
590
+	Finlist := []models.DeductionSummary{}
591
+	tmpslice := make([]string, 0)
592
+	for i := 0; i < len(detailslist); i++ {
593
+		if k, ok := maplist[detailslist[i].Name]; ok {
594
+			k.Total = k.Total.Add(detailslist[i].Total)
595
+			k.Decimal = k.Decimal.Add(detailslist[i].Decimal)
596
+			maplist[detailslist[i].Name] = models.DeductionSummary{
597
+				detailslist[i].Name,
598
+				k.Total,
599
+				k.Decimal,
600
+			}
601
+		} else {
602
+			maplist[detailslist[i].Name] = models.DeductionSummary{
603
+				detailslist[i].Name,
604
+				detailslist[i].Total,
605
+				detailslist[i].Decimal,
606
+			}
607
+			tmpslice = append(tmpslice, detailslist[i].Name)
608
+		}
609
+	}
610
+	for i := 0; i < len(tmpslice); i++ {
611
+		Finlist = append(Finlist, maplist[tmpslice[i]])
612
+	}
613
+	this.ServeSuccessJSON(map[string]interface{}{
614
+		"list": detailslist,
615
+		"sum":  sum,
616
+	})
617
+	return
618
+}
619
+
620
+//根据患者id查询患者的余额
621
+func (this *HisDepositApiController) IdToBalance() {
622
+	check := map[string][]string{
623
+		"his_patient_id": {"must", "int", "his_patient_id"},
624
+	}
625
+	_, err := checks(this, &check)
626
+	if err != nil {
627
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
628
+		return
629
+	}
630
+	orgid := this.GetAdminUserInfo().CurrentOrgId
631
+	his_patient_id, _ := this.GetInt64("his_patient_id")
632
+	tmp := service.GetUserMoney(his_patient_id, orgid)
633
+	this.ServeSuccessJSON(map[string]interface{}{
634
+		"list": tmp,
635
+	})
636
+	return
637
+}
638
+
639
+//新增一条退款申请
640
+func (this *HisDepositApiController) RefundApplication() {
641
+	orgid := this.GetAdminUserInfo().CurrentOrgId
642
+	createid := this.GetAdminUserInfo().AdminUser.Id
643
+	dataBody := make(map[string]interface{}, 0)
644
+	err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
645
+	if err != nil {
646
+		utils.ErrorLog(err.Error())
647
+		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
648
+		return
649
+	}
650
+	switch {
651
+	case dataBody["code"] == nil:
652
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "申请编号不能为空")
653
+		return
654
+	case dataBody["his_patient_id"] == nil:
655
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "患者名称不能为空")
656
+		return
657
+	case dataBody["deposit"] == nil || dataBody["deposit"] == "":
658
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "退款金额不能为空")
659
+		return
660
+	case dataBody["trial_status"] == nil:
661
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "审核状态不能为空")
662
+		return
663
+	}
664
+	code := dataBody["code"].(string)                             //申请编号
665
+	his_patient_id := int64(dataBody["his_patient_id"].(float64)) //患者编号
666
+	//deposit := decimal.NewFromFloat(dataBody["deposit"].(float64)) //退款金额
667
+	var tmps float64
668
+	switch dataBody["deposit"].(type) {
669
+	case string:
670
+		t_deposit := dataBody["deposit"].(string)
671
+		if len(t_deposit) > 0 {
672
+			if t_deposit[0] == 45 {
673
+				this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "退款金额错误")
674
+				return
675
+			}
676
+		}
677
+		tt, errs := strconv.ParseFloat(t_deposit, 64)
678
+		if errs != nil {
679
+			this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "退款金额错误")
680
+			return
681
+		} else {
682
+			tmps = tt
683
+		}
684
+	default:
685
+		tmps = dataBody["deposit"].(float64)
686
+	}
687
+	if tmps == 0 {
688
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "退款金额不能为0")
689
+		return
690
+	}
691
+	deposit := decimal.NewFromFloat(tmps)                     //押金金额
692
+	trial_status := int64(dataBody["trial_status"].(float64)) //审核状态
693
+
694
+	err = service.RefundApplication(orgid, his_patient_id, trial_status, createid, code, deposit)
695
+	if err != nil {
696
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
697
+		return
698
+	}
699
+	this.ServeSuccessJSON(map[string]interface{}{
700
+		"list": "新增成功",
701
+	})
702
+	return
703
+
704
+}
705
+
706
+//退款审核通过/拒绝
707
+func (this *HisDepositApiController) RefundReview() {
708
+	check := map[string][]string{
709
+		"ids":          {"must", "string", "ids"},
710
+		"trial_status": {"must", "int", "trial_status"},
711
+	}
712
+	_, err := checks(this, &check)
713
+	if err != nil {
714
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
715
+		return
716
+	}
717
+	orgid := this.GetAdminUserInfo().CurrentOrgId
718
+	ids := this.GetString("ids") //很多id
719
+
720
+	trial_status, _ := this.GetInt64("trial_status")
721
+	if trial_status != 1 && trial_status != 2 {
722
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数trial_status值错误")
723
+		return
724
+	}
725
+	err = service.RefundReviewMore(orgid, trial_status, ids)
726
+	if err != nil {
727
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
728
+		return
729
+	}
730
+	var tmplist string
731
+	if trial_status == 1 {
732
+		tmplist = "审核通过"
733
+	} else {
734
+		tmplist = "审核拒绝"
735
+	}
736
+	this.ServeSuccessJSON(map[string]interface{}{
737
+		"list": tmplist,
738
+	})
739
+	return
740
+}
741
+
742
+//退款删除
743
+func (this *HisDepositApiController) DeleteRefund() {
744
+	check := map[string][]string{
745
+		"id": {"must", "int", "id"},
746
+	}
747
+	_, err := checks(this, &check)
748
+	if err != nil {
749
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
750
+		return
751
+	}
752
+	orgid := this.GetAdminUserInfo().CurrentOrgId
753
+	id, _ := this.GetInt64("id")
754
+	err = service.DeleteRefund(orgid, id)
755
+	if err != nil {
756
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
757
+		return
758
+	}
759
+	this.ServeSuccessJSON(map[string]interface{}{
760
+		"list": "删除成功",
761
+	})
762
+	return
763
+}
764
+
765
+//更改退款申请
766
+func (this *HisDepositApiController) ChangeRefund() {
767
+	orgid := this.GetAdminUserInfo().CurrentOrgId
768
+	dataBody := make(map[string]interface{}, 0)
769
+	err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
770
+	if err != nil {
771
+		utils.ErrorLog(err.Error())
772
+		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
773
+		return
774
+	}
775
+	switch {
776
+	case dataBody["id"] == nil:
777
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "id不能为空")
778
+		return
779
+	case dataBody["code"] == nil:
780
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "申请编号不能为空")
781
+		return
782
+	case dataBody["his_patient_id"] == nil:
783
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "患者名称不能为空")
784
+		return
785
+	case dataBody["deposit"] == nil || dataBody["deposit"] == "":
786
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "退款金额不能为空")
787
+		return
788
+	case dataBody["trial_status"] == nil:
789
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "审核状态不能为空")
790
+		return
791
+	}
792
+	id := int64(dataBody["id"].(float64))
793
+	code := dataBody["code"].(string)                             //申请编号
794
+	his_patient_id := int64(dataBody["his_patient_id"].(float64)) //患者编号
795
+	//deposit := decimal.NewFromFloat(dataBody["deposit"].(float64)) //退款金额
796
+	var tmps float64
797
+	switch dataBody["deposit"].(type) {
798
+	case string:
799
+		t_deposit := dataBody["deposit"].(string)
800
+		if len(t_deposit) > 0 {
801
+			if t_deposit[0] == 45 {
802
+				this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "退款金额错误")
803
+				return
804
+			}
805
+		}
806
+		tt, errs := strconv.ParseFloat(t_deposit, 64)
807
+		if errs != nil {
808
+			this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "退款金额错误")
809
+			return
810
+		} else {
811
+			tmps = tt
812
+		}
813
+	default:
814
+		tmps = dataBody["deposit"].(float64)
815
+	}
816
+	if tmps == 0 {
817
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "退款金额不能为0")
818
+		return
819
+	}
820
+	deposit := decimal.NewFromFloat(tmps)                     //押金金额
821
+	trial_status := int64(dataBody["trial_status"].(float64)) //审核状态
822
+	err = service.ChangeRefund(orgid, his_patient_id, trial_status, id, code, deposit)
823
+	if err != nil {
824
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
825
+		return
826
+	}
827
+	this.ServeSuccessJSON(map[string]interface{}{
828
+		"list": "保存成功",
829
+	})
830
+	return
831
+}
832
+
833
+//退款分页
834
+func (this *HisDepositApiController) RefundList() {
835
+	check := map[string][]string{
836
+		"refundtype":  {"must", "int", "refundtype"},
837
+		"examinetype": {"must", "int", "examinetype"},
838
+	}
839
+	_, err := checks(this, &check)
840
+	if err != nil {
841
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
842
+		return
843
+	}
844
+	orgid := this.GetAdminUserInfo().CurrentOrgId
845
+	timeLayout := "2006-01-02"
846
+	loc, _ := time.LoadLocation("Local")
847
+	keyword := this.GetString("keyword")           //获取搜索框
848
+	refundtype, _ := this.GetInt64("refundtype")   //获取退款类型
849
+	examinetype, _ := this.GetInt64("examinetype") //获取审核状态
850
+	start_time := this.GetString("start_time")
851
+	end_time := this.GetString("end_time")
852
+	var stime int64 //开始时间
853
+	var etime int64 //结束时间
854
+
855
+	namemap := make(map[int64]string)
856
+	slicekey := make([]int64, 0)
857
+	lists, _ := service.GetHisUser(orgid)
858
+	for _, v := range lists {
859
+		namemap[v.ID] = v.Name
860
+	}
861
+	if len(keyword) > 0 {
862
+		for k, v := range namemap {
863
+			res := strings.Contains(v, keyword)
864
+			if res == true {
865
+				slicekey = append(slicekey, k)
866
+			}
867
+		}
868
+	}
869
+	if start_time == "" && end_time == "" {
870
+		stime, etime = service.GetMondayOfWeek()
871
+	} else {
872
+		stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
873
+		etmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
874
+		stime = stmp.Unix()
875
+		etime = etmp.Unix()
876
+	}
877
+	depo := []models.RefundList{}
878
+	depo, err = service.RefundList(orgid, stime, etime, refundtype, examinetype, keyword, slicekey)
879
+	if err != nil {
880
+		utils.ErrorLog("查询失败,原因为:", err.Error())
881
+		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
882
+		return
883
+	}
884
+	for i := 0; i < len(depo); i++ {
885
+		depo[i].Name = service.GetCreateidName(depo[i].CreateId)
886
+		depo[i].HisName = namemap[depo[i].HisPatientId]
887
+		if depo[i].Mtime > depo[i].Ctime {
888
+			depo[i].Starttime = fmt.Sprintf(time.Unix(depo[i].Mtime, 0).Format("2006-01-02 15:04:05"))
889
+		} else {
890
+			depo[i].Starttime = fmt.Sprintf(time.Unix(depo[i].Ctime, 0).Format("2006-01-02 15:04:05"))
891
+		}
892
+		if depo[i].DepositStatus == 4 {
893
+			depo[i].Name = "-"
894
+		}
895
+	}
896
+	this.ServeSuccessJSON(map[string]interface{}{
897
+		"list": depo,
898
+	})
899
+	return
900
+}
901
+
902
+func (this *HisDepositApiController) GetorgName() {
903
+	orgid := this.GetAdminUserInfo().CurrentOrgId
904
+	name := service.GetorgName(orgid)
905
+	this.ServeSuccessJSON(map[string]interface{}{
906
+		"list": name,
907
+	})
908
+	return
909
+}
910
+
911
+//获取本月时间
912
+func (this *HisDepositApiController) GetMonthTime() {
913
+	stime, etime := service.GetMonth()
914
+	srart_time := fmt.Sprintf(time.Unix(stime, 0).Format("2006-01-02"))
915
+	end_time := fmt.Sprintf(time.Unix(etime, 0).Format("2006-01-02"))
916
+
917
+	this.ServeSuccessJSON(map[string]interface{}{
918
+		"srart_time": srart_time,
919
+		"end_time":   end_time,
920
+	})
921
+	return
922
+}
923
+
924
+//获取本周时间
925
+func (this *HisDepositApiController) GetWeekTime() {
926
+	stime, etime := service.GetMondayOfWeek()
927
+	srart_time := fmt.Sprintf(time.Unix(stime, 0).Format("2006-01-02"))
928
+	end_time := fmt.Sprintf(time.Unix(etime, 0).Format("2006-01-02"))
929
+
930
+	this.ServeSuccessJSON(map[string]interface{}{
931
+		"srart_time": srart_time,
932
+		"end_time":   end_time,
933
+	})
934
+	return
935
+}
936
+
423 937
 //判断前端参数是否为空
424 938
 func checks(this *HisDepositApiController, m *map[string][]string) (map[string]string, error) {
425 939
 	tmp := make(map[string]string)

+ 66 - 2
models/his_deposit_models.go 查看文件

@@ -23,7 +23,7 @@ type DepositHistory struct {
23 23
 	DepositCode    string          `gorm:"column:deposit_code" json:"deposit_code" form:"deposit_code"`
24 24
 	Deposit        decimal.Decimal `gorm:"column:deposit" json:"deposit" form:"deposit"`                         //本次操作金额
25 25
 	SurplusDeposit decimal.Decimal `gorm:"column:surplus_deposit" json:"surplus_deposit" form:"surplus_deposit"` //剩余金额
26
-	DepositStatus  int64           `gorm:"column:deposit_status" json:"deposit_status" form:"deposit_status"`    //1:充值,2:扣费,3:退费
26
+	DepositStatus  int64           `gorm:"column:deposit_status" json:"deposit_status" form:"deposit_status"`    //1:充值,2:扣费,3:退款,4:退
27 27
 	Status         int64           `gorm:"column:status" json:"status" form:"status"`
28 28
 	CreateId       int64           `gorm:"column:create_id" json:"create_id" form:"create_id"`
29 29
 	Ctime          int64           `gorm:"column:ctime" json:"ctime" form:"ctime"`
@@ -52,7 +52,7 @@ type DepositHistoryname struct {
52 52
 	DepositCode    string          `gorm:"column:deposit_code" json:"deposit_code" form:"deposit_code"`
53 53
 	Deposit        decimal.Decimal `gorm:"column:deposit" json:"deposit" form:"deposit"`                         //本次操作金额
54 54
 	SurplusDeposit decimal.Decimal `gorm:"column:surplus_deposit" json:"surplus_deposit" form:"surplus_deposit"` //剩余金额
55
-	DepositStatus  int64           `gorm:"column:deposit_status" json:"deposit_status" form:"deposit_status"`    //1:充值,2:扣费,3:退费
55
+	DepositStatus  int64           `gorm:"column:deposit_status" json:"deposit_status" form:"deposit_status"`    //1:充值,2:扣费,3:退款,4:退
56 56
 	Status         int64           `gorm:"column:status" json:"status" form:"status"`
57 57
 	CreateId       int64           `gorm:"column:create_id" json:"create_id" form:"create_id"`
58 58
 	Ctime          int64           `gorm:"column:ctime" json:"ctime" form:"ctime"`
@@ -60,12 +60,36 @@ type DepositHistoryname struct {
60 60
 	TrialStatus    int64           `gorm:"column:trial_status" json:"trial_status" form:"trial_status"` //审核状态0:未审核 ,1:已审核
61 61
 	Remarks        string          `gorm:"column:remarks" json:"remarks" form:"remarks"`                //备注
62 62
 	Name           string          ` json:"name" `
63
+	HisName        string          `json:"his_name"`
64
+	Starttime      string          `json:"starttime"` //创建时间
63 65
 }
64 66
 
65 67
 func (DepositHistoryname) TableName() string {
66 68
 	return "sgj_xt.his_deposit_history"
67 69
 }
68 70
 
71
+type RefundList struct {
72
+	ID             int64           `gorm:"column:id" json:"id" form:"id"`
73
+	UserOrgId      int64           `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`          //机构id
74
+	HisPatientId   int64           `gorm:"column:his_patient_id" json:"his_patient_id" form:"his_patient_id"` //his病人id
75
+	DepositCode    string          `gorm:"column:deposit_code" json:"deposit_code" form:"deposit_code"`
76
+	Deposit        decimal.Decimal `gorm:"column:deposit" json:"deposit" form:"deposit"`                         //本次操作金额
77
+	SurplusDeposit decimal.Decimal `gorm:"column:surplus_deposit" json:"surplus_deposit" form:"surplus_deposit"` //剩余金额
78
+	DepositStatus  int64           `gorm:"column:deposit_status" json:"deposit_status" form:"deposit_status"`    //1:充值,2:扣费,3:退款,4:退费
79
+	Status         int64           `gorm:"column:status" json:"status" form:"status"`
80
+	CreateId       int64           `gorm:"column:create_id" json:"create_id" form:"create_id"`
81
+	Ctime          int64           `gorm:"column:ctime" json:"ctime" form:"ctime"`
82
+	Mtime          int64           `gorm:"column:mtime" json:"mtime" form:"mtime"`
83
+	TrialStatus    int64           `gorm:"column:trial_status" json:"trial_status" form:"trial_status"` //审核状态0:未审核 ,1:已审核
84
+	Starttime      string          `json:"starttime"`                                                   //申请时间
85
+	Name           string          ` json:"name" `                                                      //申请人
86
+	HisName        string          `json:"his_name"`
87
+}
88
+
89
+func (RefundList) TableName() string {
90
+	return "sgj_xt.his_deposit_history"
91
+}
92
+
69 93
 type CreateUser struct {
70 94
 	ID   int64  `gorm:"column:id" json:"id" form:"id"`
71 95
 	Name string `gorm:"column:name" json:"name" form:"name"`
@@ -95,3 +119,43 @@ type Deposit1 struct {
95 119
 func (Deposit1) TableName() string {
96 120
 	return "sgj_xt.his_deposit"
97 121
 }
122
+
123
+//扣费明细列表
124
+type Details struct {
125
+	ID         int64           `json:"id"`          //原始code
126
+	Code       string          `json:"code"`        //就诊号
127
+	Number     string          `json:"number"`      //批号,用来跳转用的
128
+	NameId     int64           `json:"name_id"`     //患者id
129
+	Name       string          `json:"name"`        //患者名字
130
+	Chargetype string          `json:"chargetype"`  //医收费类型
131
+	Total      decimal.Decimal `json:"total"`       //医疗费总额
132
+	Decimal    decimal.Decimal `json:"decimal"`     //押金支付金额
133
+	ChargeDate string          `json:"charge_date"` //收费日期
134
+}
135
+
136
+//扣费汇总
137
+type DeductionSummary struct {
138
+	Name    string          `json:"name"`    //患者名字
139
+	Total   decimal.Decimal `json:"total"`   //医疗费总额
140
+	Decimal decimal.Decimal `json:"decimal"` //押金支付金额
141
+}
142
+
143
+type GetorgName struct {
144
+	ID      int64  `json:"id"`
145
+	OrgName string `json:"org_name"`
146
+}
147
+
148
+func (GetorgName) TableName() string {
149
+	return "sgj_users.sgj_user_org"
150
+}
151
+
152
+//获取就诊号
153
+type GetMdtrtId struct {
154
+	Id      int64  `json:"id"`
155
+	MdtrtId string `json:"mdtrt_id"`
156
+	Number  string `json:"number"`
157
+}
158
+
159
+func (GetMdtrtId) TableName() string {
160
+	return "sgj_xt.his_order"
161
+}

+ 1 - 0
models/his_models.go 查看文件

@@ -1022,6 +1022,7 @@ type HisOrder struct {
1022 1022
 	DoctorName            string    `gorm:"-" json:"doctor_name" form:"doctor_name"`
1023 1023
 	Creator               int64     `gorm:"column:creator" json:"creator" form:"creator"`
1024 1024
 	PType                 int64     `gorm:"column:p_type" json:"p_type" form:"p_type"`
1025
+	Decimal               float64   `gorm:"column:decimal" json:"decimal" form:"decimal"`
1025 1026
 
1026 1027
 	FaPiaoCode   string `gorm:"column:fa_piao_code" json:"fa_piao_code" form:"fa_piao_code"`
1027 1028
 	FaPiaoNumber string `gorm:"column:fa_piao_number" json:"fa_piao_number" form:"fa_piao_number"`

+ 1 - 0
service/admin_service.go 查看文件

@@ -30,6 +30,7 @@ func GetTotalOrgCount() (int, error) {
30 30
 		return 0, err
31 31
 	}
32 32
 	return count, nil
33
+
33 34
 }
34 35
 
35 36
 // 获取一段时间内注册的机构数量

+ 474 - 13
service/his_deposit_service.go 查看文件

@@ -4,9 +4,11 @@ import (
4 4
 	"XT_New/models"
5 5
 	"XT_New/utils"
6 6
 	"fmt"
7
+	"github.com/jinzhu/gorm"
7 8
 	"github.com/shopspring/decimal"
8 9
 	"math/rand"
9 10
 	"strconv"
11
+	"strings"
10 12
 	"time"
11 13
 )
12 14
 
@@ -42,6 +44,7 @@ func UpDeposit(code, remarks string, his_patient_id, orgid, trial_status, create
42 44
 			UserOrgId:    orgid,
43 45
 			HisPatientId: his_patient_id,
44 46
 			Ctime:        time.Now().Unix(),
47
+			Mtime:        time.Now().Unix(),
45 48
 			Status:       1,
46 49
 		}
47 50
 		i, d, err := AddDeposit(de)
@@ -52,6 +55,7 @@ func UpDeposit(code, remarks string, his_patient_id, orgid, trial_status, create
52 55
 		tmp = i
53 56
 		tmpdeposit = d
54 57
 	}
58
+	s_deposit := tmpdeposit
55 59
 	if trial_status == 1 {
56 60
 		de := models.Deposit{
57 61
 			ID:      tmp,
@@ -63,13 +67,14 @@ func UpDeposit(code, remarks string, his_patient_id, orgid, trial_status, create
63 67
 			utils.ErrorLog("添加用户押金失败: %v", err.Error())
64 68
 			return
65 69
 		}
70
+		s_deposit = deposit.Add(tmpdeposit)
66 71
 	}
67 72
 	dehistory := models.DepositHistory{
68 73
 		UserOrgId:      orgid,
69 74
 		HisPatientId:   his_patient_id,
70 75
 		DepositCode:    code,
71
-		Deposit:        deposit,                 //本次操作的押金
72
-		SurplusDeposit: tmpdeposit.Add(deposit), //剩余金额
76
+		Deposit:        deposit,   //本次操作的押金
77
+		SurplusDeposit: s_deposit, //剩余金额
73 78
 		DepositStatus:  1,
74 79
 		Status:         1,
75 80
 		CreateId:       createid,
@@ -197,6 +202,7 @@ func GetMonth() (int64, int64) {
197 202
 	timeMonthEndUnix1 := timeToday.AddDate(0, 1, -timeToday.Day()+1).Unix() - 1                            // 获取下个月第一天/ 本月最后一天24点 时间戳类型
198 203
 	return timeMonthStartUnix1, timeMonthEndUnix1
199 204
 }
205
+
200 206
 func GetCreateidName(id int64) string {
201 207
 	var tmp models.CreateUser
202 208
 	XTReadDB().Select("name").Where("id = ?", id).Find(&tmp)
@@ -209,7 +215,7 @@ func UpDecimalHistory(id int64) (err error) {
209 215
 	tx := XTWriteDB().Begin()
210 216
 	defer func() {
211 217
 		if err != nil {
212
-			utils.ErrorLog("事务失败,原因为", err)
218
+			utils.ErrorLog("事务失败,原因为: %v", err)
213 219
 			tx.Rollback()
214 220
 		} else {
215 221
 			tx.Commit()
@@ -261,7 +267,10 @@ func GetDecimalHistoryOne(id int64) (history models.DepositHistory, err error) {
261 267
 //根据患者id获取该患者当前剩余的押金
262 268
 func GetUserMoney(id, orgid int64) decimal.Decimal {
263 269
 	tmp := models.Deposit{}
264
-	XTReadDB().Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&tmp)
270
+	err := XTReadDB().Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&tmp).Error
271
+	if err != nil {
272
+		return decimal.NewFromFloat(0)
273
+	}
265 274
 	return tmp.Deposit
266 275
 }
267 276
 
@@ -282,23 +291,475 @@ func GetFlowList(id, orgid, stime, etime, deposit_status int64) (deposit []model
282 291
 	return
283 292
 }
284 293
 
294
+//获取患者押金列表
285 295
 func GetUserList(page, limit, orgid int64, keyword string, slicekey []int64) (m []models.Deposit1, total int64, err error) {
286 296
 	db := XTReadDB().Model(&models.Deposit{}).Where("status = 1 and user_org_id = ? ", orgid)
287 297
 	offset := (page - 1) * limit
288
-	if len(slicekey) > 0 {
289
-		tmp := ""
290
-		for i := 0; i < len(slicekey); i++ {
291
-			tmp = tmp + " his_patient_id = " + strconv.FormatInt(slicekey[i], 10)
292
-			if i < len(slicekey)-1 {
293
-				tmp = tmp + " or "
298
+	if len(keyword) > 0 {
299
+		db = db.Where(" his_patient_id = ? ", keyword)
300
+	}
301
+	err = db.Count(&total).Offset(offset).Order("mtime desc").Find(&m).Error
302
+	return
303
+}
304
+
305
+//扣费明细列表
306
+func DeductionList(orgid, stime, etime int64, keyword string, slicekey []int64) (deposithistory []models.DepositHistory, err error) {
307
+	db := XTReadDB().Model(&models.DepositHistory{}).Where("status = 1 and user_org_id = ? and deposit_status = 2 ", orgid).Where("ctime >= ? and ctime <= ?", stime, etime)
308
+	if len(keyword) > 0 {
309
+		var tmp string = "deposit_code like ?"
310
+		if len(slicekey) > 0 {
311
+			for i := 0; i < len(slicekey); i++ {
312
+				tmp = tmp + " or his_patient_id = " + strconv.FormatInt(slicekey[i], 10)
294 313
 			}
295 314
 		}
296
-		db = db.Where(tmp)
315
+		keyword = "%" + keyword + "%"
316
+		db = db.Where(tmp, keyword)
317
+	}
318
+	err = db.Order("ctime desc").Find(&deposithistory).Error
319
+	return
320
+}
321
+
322
+//获取医疗费总额
323
+func MedicalTotal(orgid, patient_id int64, code string) decimal.Decimal {
324
+	tmp := models.HisChargeSettleOrder{}
325
+	XTReadDB().Model(&models.HisChargeSettleOrder{}).Where("mdtrt_id = ? and patient_id = ? and user_org_id = ? and status = 1 and order_status = 2", code, patient_id, orgid).Find(&tmp)
326
+	return decimal.NewFromFloat(tmp.MedfeeSumamt)
327
+}
328
+
329
+//根据就诊号查询出医收费类型
330
+func CodeToChargetype(orgid int64, code string) (s string) {
331
+	tmp := models.HisPrintPatient{}
332
+	XTReadDB().Model(&models.HisPrintPatient{}).Where("number = ? and user_org_id = ?", code, orgid).Find(&tmp)
333
+	//tmp.BalanceAccountsType
334
+	switch tmp.BalanceAccountsType {
335
+	case 1:
336
+		s = "医保"
337
+	case 2:
338
+		s = "自费"
339
+	case 3:
340
+		s = "公费"
341
+	case 4:
342
+		s = "农保"
343
+	case 5:
344
+		s = "会员"
345
+	case 6:
346
+		s = "职工"
347
+	case 7:
348
+		s = "合同"
349
+	case 8:
350
+		s = "医保自费"
351
+	default:
352
+		s = "类型未定义"
353
+	}
354
+	return
355
+}
356
+
357
+//扣除患者的押金,并生成一条扣费历史记录
358
+//orgid 机构id;
359
+//his_user_id	患者id;
360
+//create_id		创建者id;
361
+//code		就诊号;
362
+//deposit	本次使用的押金金额,支持float64、int、int64、decimal.Decimal类型
363
+func SpendDeposit(orgid, his_user_id, create_id int64, code string, deposit interface{}) (err error) {
364
+	tmp_deposit := decimal.Decimal{} //本次患者使用的押金
365
+	tmp_deposit, err = ToDecimal(deposit)
366
+	if err != nil {
367
+		return
368
+	}
369
+	//开事务
370
+	tx := XTWriteDB().Begin()
371
+	defer func() {
372
+		if err != nil {
373
+			utils.ErrorLog("事务失败,原因为: %v", err.Error())
374
+			tx.Rollback()
375
+		} else {
376
+			tx.Commit()
377
+		}
378
+	}()
379
+	//查询患者押金
380
+	tmp := models.Deposit{}
381
+	err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", his_user_id, orgid).Find(&tmp).Error
382
+	if err != nil {
383
+		err = fmt.Errorf("押金余额不足")
384
+		return
385
+	}
386
+	//判断能否扣除
387
+	if tmp.Deposit.Cmp(tmp_deposit) == -1 {
388
+		err = fmt.Errorf("押金余额不足")
389
+		return
390
+	}
391
+	//扣除患者押金
392
+	err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", tmp.ID).Updates(map[string]interface{}{
393
+		"mtime":   time.Now().Unix(),
394
+		"deposit": tmp.Deposit.Sub(tmp_deposit),
395
+	}).Error
396
+	if err != nil {
397
+		return
398
+	}
399
+	//生成一条历史记录
400
+	dehistory := models.DepositHistory{
401
+		UserOrgId:      orgid,
402
+		HisPatientId:   his_user_id,
403
+		DepositCode:    code,
404
+		Deposit:        tmp_deposit,
405
+		SurplusDeposit: tmp.Deposit.Sub(tmp_deposit),
406
+		DepositStatus:  2,
407
+		Status:         1,
408
+		CreateId:       create_id,
409
+		Ctime:          time.Now().Unix(),
410
+		Mtime:          time.Now().Unix(),
411
+		TrialStatus:    1,
412
+	}
413
+	err = tx.Create(&dehistory).Error
414
+	return
415
+}
416
+
417
+//新增一条退款申请
418
+func RefundApplication(orgid, his_patient_id, trial_status, createid int64, code string, deposit decimal.Decimal) (err error) {
419
+	//开事务
420
+	tx := XTWriteDB().Begin()
421
+	defer func() {
422
+		if err != nil {
423
+			utils.ErrorLog("事务失败,原因为: %v", err.Error())
424
+			tx.Rollback()
425
+		} else {
426
+			tx.Commit()
427
+		}
428
+	}()
429
+	//获取患者当前余额
430
+	tmp := models.Deposit{}
431
+	err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", his_patient_id, orgid).Find(&tmp).Error
432
+	if err != nil {
433
+		return
434
+	}
435
+	//判断是否可以退款
436
+	if tmp.Deposit.Cmp(deposit) == -1 {
437
+		err = fmt.Errorf("退款金额不得大于押金余额!")
438
+		return
439
+	}
440
+	s_deposit := tmp.Deposit
441
+	//判断是否审核通过,通过更新当前押金,否则只新增一条历史记录
442
+	if trial_status == 1 {
443
+		//更新患者当前押金余额
444
+		err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", tmp.ID).Updates(map[string]interface{}{
445
+			"mtime":   time.Now().Unix(),
446
+			"deposit": tmp.Deposit.Sub(deposit),
447
+		}).Error
448
+		if err != nil {
449
+			return
450
+		}
451
+		s_deposit = tmp.Deposit.Sub(deposit)
452
+	}
453
+	//生成一条历史记录
454
+	dehistory := models.DepositHistory{
455
+		UserOrgId:      orgid,
456
+		HisPatientId:   his_patient_id,
457
+		DepositCode:    code,
458
+		Deposit:        deposit,
459
+		SurplusDeposit: s_deposit,
460
+		DepositStatus:  3, //3退款,4退费
461
+		Status:         1,
462
+		CreateId:       createid,
463
+		Ctime:          time.Now().Unix(),
464
+		Mtime:          time.Now().Unix(),
465
+		TrialStatus:    trial_status,
466
+	}
467
+	err = tx.Create(&dehistory).Error
468
+	return
469
+}
470
+
471
+//退款审核通过/拒绝批量处理
472
+func RefundReviewMore(orgid, trial_status int64, ids string) (err error) {
473
+	//处理下字符串
474
+	t_ids := ""
475
+	if ids[len(ids)-1] == 44 {
476
+		t_ids = ids[:len(ids)-1]
297 477
 	} else {
298
-		if len(keyword) > 0 {
478
+		t_ids = ids
479
+	}
480
+	tmp_id := strings.Split(t_ids, ",")
481
+	//开事务
482
+	tx := XTWriteDB().Begin()
483
+	defer func() {
484
+		if err != nil {
485
+			utils.ErrorLog("事务失败,原因为: %v", err.Error())
486
+			tx.Rollback()
487
+		} else {
488
+			tx.Commit()
489
+		}
490
+	}()
491
+	//循环处理id
492
+	for i := 0; i < len(tmp_id); i++ {
493
+		var id int64
494
+		id, err = strconv.ParseInt(tmp_id[i], 10, 64)
495
+		if err != nil {
496
+			return err
497
+		}
498
+		err = RefundReview(orgid, id, trial_status, tx)
499
+		if err != nil {
500
+			return err
501
+		}
502
+	}
503
+	return err
504
+}
505
+
506
+//退款审核通过/拒绝
507
+func RefundReview(orgid, id, trial_status int64, tx *gorm.DB) (err error) {
508
+	//根据id查询该条历史记录
509
+	history := models.DepositHistory{}
510
+	err = tx.Model(&models.DepositHistory{}).Where("id = ? and status = 1", id).Find(&history).Error
511
+	if err != nil {
512
+		return err
513
+	}
514
+	//判断状态是否为未审核
515
+	if history.TrialStatus != 0 {
516
+		err = fmt.Errorf("所选单据中包含了无需审核的单据")
517
+		return err
518
+	}
519
+	//判断类型是否为3,3代表退款,4为退费
520
+	if history.DepositStatus != 3 {
521
+		err = fmt.Errorf("所选单据中包含了无需审核的单据")
522
+		return err
523
+	}
524
+	up := make(map[string]interface{})
525
+	//通过
526
+	if trial_status == 1 {
527
+		//拿患者的id和机构id在去查患者的余额
528
+		tt := models.Deposit{}
529
+		err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", history.HisPatientId, orgid).Find(&tt).Error
530
+		if err != nil {
531
+			return err
532
+		}
533
+		//判断是否可以修改患者押金
534
+		if tt.Deposit.Cmp(history.Deposit) == -1 {
535
+			err = fmt.Errorf("当前押金余额小于退款金额,无法完成退款")
536
+			return err
537
+		}
538
+		//修改患者押金
539
+		err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", tt.ID).Updates(map[string]interface{}{
540
+			"mtime":   time.Now().Unix(),
541
+			"deposit": tt.Deposit.Sub(history.Deposit),
542
+		}).Error
543
+		if err != nil {
544
+			return err
545
+		}
546
+		up = map[string]interface{}{
547
+			"mtime":           time.Now().Unix(),
548
+			"surplus_deposit": tt.Deposit.Sub(history.Deposit),
549
+			"trial_status":    1,
550
+		}
551
+	} else {
552
+		up = map[string]interface{}{
553
+			"mtime":        time.Now().Unix(),
554
+			"trial_status": 2,
555
+		}
556
+	}
557
+	//修改一条历史记录
558
+	err = tx.Model(&models.DepositHistory{}).Where("id = ? and status = 1", id).Updates(up).Error
559
+	return err
560
+}
561
+
562
+//退款删除
563
+func DeleteRefund(orgid, id int64) (err error) {
564
+	//根据id查询历史记录
565
+	history := models.DepositHistory{}
566
+	err = XTReadDB().Model(&models.DepositHistory{}).Where("id = ?", id).Find(&history).Error
567
+	if err != nil {
568
+		return
569
+	}
570
+	//判断是否可以删除
571
+	if history.TrialStatus == 1 || history.DepositStatus != 3 {
572
+		err = fmt.Errorf("当前状态不可删除!")
573
+		return
574
+	}
575
+	//删除
576
+	err = XTWriteDB().Model(&models.DepositHistory{}).Where("id = ?", id).Updates(map[string]interface{}{
577
+		"mtime":  time.Now().Unix(),
578
+		"status": 0,
579
+	}).Error
580
+
581
+	return
582
+}
583
+
584
+//更改退款申请
585
+func ChangeRefund(orgid, his_patient_id, trial_status, id int64, code string, deposit decimal.Decimal) (err error) {
586
+	//开事务
587
+	tx := XTWriteDB().Begin()
588
+	defer func() {
589
+		if err != nil {
590
+			utils.ErrorLog("事务失败,原因为: %v", err.Error())
591
+			tx.Rollback()
592
+		} else {
593
+			tx.Commit()
594
+		}
595
+	}()
596
+	//获取患者当前余额
597
+	tmp := models.Deposit{}
598
+	err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", his_patient_id, orgid).Find(&tmp).Error
599
+	if err != nil {
600
+		return
601
+	}
602
+	//判断是否可以退款
603
+	if tmp.Deposit.Cmp(deposit) == -1 {
604
+		err = fmt.Errorf("退款金额不得大于押金余额!")
605
+		return
606
+	}
607
+	up := make(map[string]interface{})
608
+	//判断是否审核通过,通过更新当前押金,否则只新增一条历史记录
609
+	if trial_status == 1 {
610
+		//更新患者当前押金余额
611
+		err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", tmp.ID).Updates(map[string]interface{}{
612
+			"mtime":   time.Now().Unix(),
613
+			"deposit": tmp.Deposit.Sub(deposit),
614
+		}).Error
615
+		if err != nil {
299 616
 			return
300 617
 		}
618
+		up = map[string]interface{}{
619
+			"mtime":           time.Now().Unix(),
620
+			"deposit_code":    code,
621
+			"his_patient_id":  his_patient_id,
622
+			"surplus_deposit": tmp.Deposit.Sub(deposit),
623
+			"deposit":         deposit,
624
+			"trial_status":    trial_status,
625
+		}
626
+	} else {
627
+		up = map[string]interface{}{
628
+			"mtime":          time.Now().Unix(),
629
+			"deposit_code":   code,
630
+			"his_patient_id": his_patient_id,
631
+			"deposit":        deposit,
632
+			"trial_status":   trial_status,
633
+		}
634
+	}
635
+	//更新一条历史记录
636
+	err = tx.Model(&models.DepositHistory{}).Where("id = ? and status = 1", id).Updates(up).Error
637
+	return
638
+}
639
+
640
+//退款分页
641
+func RefundList(orgid, stime, etime, refundtype, examinetype int64, keyword string, slicekey []int64) (depo []models.RefundList, err error) {
642
+	db := XTReadDB().Model(&models.DepositHistory{}).Where("status = 1 and user_org_id = ? ", orgid).Where("ctime >= ? and ctime <= ?", stime, etime)
643
+	//退款类型
644
+	switch refundtype {
645
+	case 0:
646
+		db = db.Where(" deposit_status in (3,4) ")
647
+	case 3, 4:
648
+		db = db.Where(" deposit_status = ? ", refundtype)
649
+	default:
650
+		err = fmt.Errorf("退款类型错误")
651
+		return
652
+	}
653
+	//审核状态
654
+	switch examinetype {
655
+	case 0, 1, 2:
656
+		db = db.Where(" trial_status = ? ", examinetype)
657
+	case 3: //代表查询全部
658
+		db = db
659
+	default:
660
+		err = fmt.Errorf("审核状态错误")
661
+		return
662
+	}
663
+	if len(keyword) > 0 {
664
+		tmp := "deposit_code like ? "
665
+		if len(slicekey) > 0 {
666
+			for i := 0; i < len(slicekey); i++ {
667
+				tmp = tmp + " or his_patient_id = " + strconv.FormatInt(slicekey[i], 10)
668
+			}
669
+		}
670
+		keyword = "%" + keyword + "%"
671
+		db = db.Where(tmp, keyword)
672
+	}
673
+	err = db.Order("ctime desc").Find(&depo).Error
674
+	return
675
+
676
+}
677
+
678
+//退回患者的押金,并生成一条退费历史记录
679
+//orgid 机构id
680
+//his_user_id 患者id
681
+//code 编号
682
+//deposit 本次退费的金额,支持float64、int、int64、decimal.Decimal类型
683
+func MoneyIncrease(orgid, his_user_id int64, code string, deposit interface{}) (err error) {
684
+	tmp_deposit := decimal.Decimal{}
685
+	tmp_deposit, err = ToDecimal(deposit)
686
+	if err != nil {
687
+		return
688
+	}
689
+	//开事务
690
+	tx := XTWriteDB().Begin()
691
+	defer func() {
692
+		if err != nil {
693
+			utils.ErrorLog("事务失败,原因为: %v", err.Error())
694
+			tx.Rollback()
695
+		} else {
696
+			tx.Commit()
697
+		}
698
+	}()
699
+	//获取当前患者押金
700
+	tmp := models.Deposit{}
701
+	err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", his_user_id, orgid).Find(&tmp).Error
702
+	if err != nil {
703
+		return
704
+	}
705
+	//退回患者押金
706
+	err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", tmp.ID).Updates(map[string]interface{}{
707
+		"mtime":   time.Now().Unix(),
708
+		"deposit": tmp.Deposit.Add(tmp_deposit),
709
+	}).Error
710
+	if err != nil {
711
+		return
712
+	}
713
+	//生成一条退费记录
714
+	dehistory := models.DepositHistory{
715
+		UserOrgId:      orgid,
716
+		HisPatientId:   his_user_id,
717
+		DepositCode:    code,
718
+		Deposit:        tmp_deposit,
719
+		SurplusDeposit: tmp.Deposit.Add(tmp_deposit),
720
+		DepositStatus:  4,
721
+		Status:         1,
722
+		CreateId:       0,
723
+		Ctime:          time.Now().Unix(),
724
+		Mtime:          time.Now().Unix(),
725
+		TrialStatus:    1,
726
+	}
727
+	err = tx.Create(&dehistory).Error
728
+	return
729
+}
730
+func GetorgName(orgid int64) (name string) {
731
+	tmp := models.GetorgName{}
732
+	XTReadDB().Model(&models.GetorgName{}).Where("id = ? ", orgid).Find(&tmp)
733
+	return tmp.OrgName
734
+}
735
+
736
+//根据id查询就诊号
737
+func FindcodeToid(id int64) (code string) {
738
+	tmp := models.GetMdtrtId{}
739
+	XTReadDB().Model(&models.GetMdtrtId{}).Where("id = ?", id).Find(&tmp)
740
+	return tmp.MdtrtId
741
+}
742
+
743
+//根据id查询就诊号
744
+func FindnumberToid(id int64) (number string) {
745
+	tmp := models.GetMdtrtId{}
746
+	XTReadDB().Model(&models.GetMdtrtId{}).Where("id = ?", id).Find(&tmp)
747
+	return tmp.Number
748
+}
749
+
750
+//把其他的类型转换成decimal.Decimal类型
751
+func ToDecimal(i interface{}) (d decimal.Decimal, err error) {
752
+	switch i.(type) {
753
+	case float64:
754
+		d = decimal.NewFromFloat(i.(float64))
755
+	case decimal.Decimal:
756
+		d = i.(decimal.Decimal)
757
+	case int64:
758
+		d = decimal.NewFromFloat(float64(i.(int64)))
759
+	case int:
760
+		d = decimal.NewFromFloat(float64(i.(int)))
761
+	default:
762
+		err = fmt.Errorf("类型解析错误")
301 763
 	}
302
-	err = db.Count(&total).Offset(offset).Order("mtime desc").Find(&m).Error
303 764
 	return
304 765
 }

+ 5 - 0
service/his_project_service.go 查看文件

@@ -748,6 +748,11 @@ func GetHisPatientByIdThree(id int64) (models.HisPatient, error) {
748 748
 	err := XTReadDB().Model(&patient).Where("id = ? and status = 1", id).First(&patient).Error
749 749
 	return patient, err
750 750
 }
751
+func GetHisPatientByIdThreeTX(id int64, tx *gorm.DB) (models.HisPatient, error) {
752
+	patient := models.HisPatient{}
753
+	err := tx.Model(&patient).Where("id = ? and status = 1", id).First(&patient).Error
754
+	return patient, err
755
+}
751 756
 
752 757
 func GetLastHisPatient(patient_id int64, org_id int64) (his models.HisPatient, err error) {
753 758
 	err = XTReadDB().Model(&models.HisPatient{}).Where("user_org_id = ? AND patient_id = ? and status = 1 AND patient_info <> ''", org_id, patient_id).First(&his).Error

+ 77 - 1
service/his_service.go 查看文件

@@ -514,6 +514,21 @@ func GetSettleHisPrescription(org_id int64, patient_id int64, his_patient_id int
514 514
 		Find(&prescription).Error
515 515
 	return
516 516
 }
517
+func GetSettleHisPrescriptionTX(org_id int64, patient_id int64, his_patient_id int64, record_date int64, med_type int64, tx *gorm.DB) (prescription []*models.HisPrescription, err error) {
518
+	err = tx.Model(&models.HisPrescription{}).
519
+		Preload("HisAdditionalCharge", func(tx *gorm.DB) *gorm.DB {
520
+			return tx.Where("status = 1 AND user_org_id = ?", org_id).Preload("XtHisAddtionConfig", "status=1")
521
+		}).
522
+		Preload("HisDoctorAdviceInfo", func(tx *gorm.DB) *gorm.DB {
523
+			return tx.Where("status = 1 AND user_org_id = ?", org_id).Preload("Drug", "status=1")
524
+		}).
525
+		Preload("HisPrescriptionProject", func(tx *gorm.DB) *gorm.DB {
526
+			return tx.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisProject").Preload("GoodInfo", "status=1").Preload("XtHisProjectTeam", "status = 1")
527
+		}).
528
+		Where("user_org_id = ? AND status = 1 AND record_date = ? AND patient_id = ? AND order_status <> 2 AND his_patient_id = ? AND p_type <> 1 AND med_type = ?", org_id, record_date, patient_id, his_patient_id, med_type).
529
+		Find(&prescription).Error
530
+	return
531
+}
517 532
 
518 533
 func GetMonthHisPrescription(org_id int64, patient_id int64, start_date int64, end_date int64, med_type int64) (prescription []*models.HisPrescription, err error) {
519 534
 	err = readDb.Model(&models.HisPrescription{}).
@@ -530,6 +545,21 @@ func GetMonthHisPrescription(org_id int64, patient_id int64, start_date int64, e
530 545
 		Find(&prescription).Error
531 546
 	return
532 547
 }
548
+func GetMonthHisPrescriptionTX(org_id int64, patient_id int64, start_date int64, end_date int64, med_type int64, tx *gorm.DB) (prescription []*models.HisPrescription, err error) {
549
+	err = tx.Model(&models.HisPrescription{}).
550
+		Preload("HisAdditionalCharge", func(tx *gorm.DB) *gorm.DB {
551
+			return tx.Where("status = 1 AND user_org_id = ?", org_id).Preload("XtHisAddtionConfig", "status=1")
552
+		}).
553
+		Preload("HisDoctorAdviceInfo", func(tx *gorm.DB) *gorm.DB {
554
+			return tx.Where("status = 1 AND user_org_id = ?", org_id).Preload("Drug", "status=1")
555
+		}).
556
+		Preload("HisPrescriptionProject", func(tx *gorm.DB) *gorm.DB {
557
+			return tx.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisProject").Preload("GoodInfo", "status=1").Preload("XtHisProjectTeam", "status = 1")
558
+		}).
559
+		Where("user_org_id = ? AND status = 1 AND record_date >= ? AND record_date <= ? AND patient_id = ? AND order_status <> 2 AND p_type <> 1 AND med_type = ?", org_id, start_date, end_date, patient_id, med_type).
560
+		Find(&prescription).Error
561
+	return
562
+}
533 563
 
534 564
 func GetMonthHisPrescriptionTwo(org_id int64, patient_id int64, start_date int64, end_date int64, p_type int64) (prescription []*models.HisPrescription, err error) {
535 565
 	err = readDb.Model(&models.HisPrescription{}).
@@ -842,11 +872,19 @@ func CreateOrder(order *models.HisOrder) (err error) {
842 872
 	err = writeDb.Create(&order).Error
843 873
 	return
844 874
 }
875
+func CreateOrderTX(order *models.HisOrder, tx *gorm.DB) (err error) {
876
+	err = tx.Create(&order).Error
877
+	return
878
+}
845 879
 
846 880
 func CreateOrderInfo(order *models.HisOrderInfo) (err error) {
847 881
 	err = writeDb.Create(&order).Error
848 882
 	return
849 883
 }
884
+func CreateOrderInfoTX(order *models.HisOrderInfo, tx *gorm.DB) (err error) {
885
+	err = tx.Create(&order).Error
886
+	return
887
+}
850 888
 
851 889
 func FindPatientPrescriptionInfo(org_id int64, patient_id int64, record_date int64, p_type int64, his_patient_id int64) (info models.HisPrescriptionInfo, err error) {
852 890
 	err = readDb.Model(&models.HisPrescriptionInfo{}).Where("user_org_id = ? AND status = 1 AND record_date = ? AND patient_id = ? AND p_type = ? AND his_patient_id = ? ", org_id, record_date, patient_id, p_type, his_patient_id).First(&info).Error
@@ -956,6 +994,10 @@ func UpDatePrescriptionNumber(user_org_id int64, ids []int64, number string) (er
956 994
 	err = writeDb.Model(&models.HisPrescription{}).Where("user_org_id = ? AND status = 1 AND id in (?)", user_org_id, ids).Updates(map[string]interface{}{"batch_number": number, "mtime": time.Now().Unix()}).Error
957 995
 	return
958 996
 }
997
+func UpDatePrescriptionNumberTX(user_org_id int64, ids []int64, number string, tx *gorm.DB) (err error) {
998
+	err = tx.Model(&models.HisPrescription{}).Where("user_org_id = ? AND status = 1 AND id in (?)", user_org_id, ids).Updates(map[string]interface{}{"batch_number": number, "mtime": time.Now().Unix()}).Error
999
+	return
1000
+}
959 1001
 
960 1002
 func UpDateAddtionNumber(user_org_id int64, ids []int64, number string) (err error) {
961 1003
 	err = writeDb.Model(&models.HisAdditionalCharge{}).Where("user_org_id = ? AND status = 1 AND id in (?)", user_org_id, ids).Updates(map[string]interface{}{"batch_number": number, "mtime": time.Now().Unix()}).Error
@@ -973,6 +1015,11 @@ func UpDateHisPrescriptionInfoNumber(user_org_id int64, id int64, number string,
973 1015
 
974 1016
 	return
975 1017
 }
1018
+func UpDateHisPrescriptionInfoNumberTX(user_org_id int64, id int64, number string, record_time int64, his_patient_id int64, tx *gorm.DB) (err error) {
1019
+	err = tx.Model(&models.HisPrescriptionInfo{}).Where("user_org_id = ? AND status = 1 AND his_patient_id = ? AND record_date = ?", user_org_id, his_patient_id, record_time).Updates(map[string]interface{}{"batch_number": number, "prescription_status": 3, "mtime": time.Now().Unix()}).Error
1020
+
1021
+	return
1022
+}
976 1023
 
977 1024
 func UpDateHospitalHisPrescriptionInfoNumber(user_org_id int64, number string, start_time int64, end_time int64) (err error) {
978 1025
 	err = writeDb.Model(&models.HisPrescriptionInfo{}).Where("user_org_id = ? AND status = 1  AND record_date <= ? AND record_date >= ? ", user_org_id, end_time, start_time).Updates(map[string]interface{}{"batch_number": number, "prescription_status": 3, "mtime": time.Now().Unix()}).Error
@@ -1198,6 +1245,11 @@ func UpdataOrderStatusTwo(number string, user_org_id int64) (err error) {
1198 1245
 	err = writeDb.Model(&models.HisPrescriptionInfo{}).Where("status = 1 AND batch_number = ? AND user_org_id = ?", number, user_org_id).Updates(map[string]interface{}{"prescription_status": 3, "mtime": time.Now().Unix()}).Error
1199 1246
 	return
1200 1247
 }
1248
+func UpdataOrderStatusTwoTX(number string, user_org_id int64, tx *gorm.DB) (err error) {
1249
+	err = tx.Model(&models.HisPrescription{}).Where("status = 1 AND batch_number = ? AND user_org_id = ?", number, user_org_id).Updates(map[string]interface{}{"order_status": 2, "mtime": time.Now().Unix()}).Error
1250
+	err = tx.Model(&models.HisPrescriptionInfo{}).Where("status = 1 AND batch_number = ? AND user_org_id = ?", number, user_org_id).Updates(map[string]interface{}{"prescription_status": 3, "mtime": time.Now().Unix()}).Error
1251
+	return
1252
+}
1201 1253
 
1202 1254
 func CreateErrMsgLog(log *models.HisOrderError) {
1203 1255
 	writeDb.Model(&models.HisOrderError{}).Create(&log)
@@ -1428,6 +1480,10 @@ func GetHisOrderByID(order_id int64) (order models.HisOrder, err error) {
1428 1480
 	err = readDb.Model(&models.HisOrder{}).Where("id = ? AND status = 1", order_id).First(&order).Error
1429 1481
 	return
1430 1482
 }
1483
+func GetHisOrderByIDTX(order_id int64, tx *gorm.DB) (order models.HisOrder, err error) {
1484
+	err = tx.Model(&models.HisOrder{}).Where("id = ? AND status = 1", order_id).First(&order).Error
1485
+	return
1486
+}
1431 1487
 
1432 1488
 func GetHisOrderInfoByNumber(order_number string) (order []*models.HisOrderInfo, err error) {
1433 1489
 	err = readDb.Model(&models.HisOrderInfo{}).Where("order_number = ? AND status = 1", order_number).Find(&order).Error
@@ -2233,7 +2289,9 @@ func GetChargeMonthHisPrescriptionFive(org_id int64, patient_id int64, order_num
2233 2289
 
2234 2290
 func UpdataHisPateint(his *models.HisPatient) {
2235 2291
 	writeDb.Save(&his)
2236
-
2292
+}
2293
+func UpdataHisPateintTX(his *models.HisPatient, tx *gorm.DB) {
2294
+	tx.Save(&his)
2237 2295
 }
2238 2296
 
2239 2297
 func SaveHisOrder(his *models.HisOrder) (err error) {
@@ -2259,6 +2317,24 @@ func GetPrescriptionByIds(ids []string, org_id int64) (prescription []*models.Hi
2259 2317
 
2260 2318
 	return
2261 2319
 }
2320
+func GetPrescriptionByIdsTX(ids []string, org_id int64, tx *gorm.DB) (prescription []*models.HisPrescription, err error) {
2321
+	err = tx.Model(&models.HisPrescription{}).
2322
+		Preload("HisAdditionalCharge", func(tx *gorm.DB) *gorm.DB {
2323
+			return tx.Where("status = 1 AND user_org_id = ?", org_id).Preload("XtHisAddtionConfig", "status=1")
2324
+		}).
2325
+		Preload("HisDoctorAdviceInfo", func(tx *gorm.DB) *gorm.DB {
2326
+			return tx.Where("status = 1 AND user_org_id = ?", org_id).Preload("Drug", "status=1")
2327
+		}).
2328
+		Preload("HisPrescriptionProject", func(tx *gorm.DB) *gorm.DB {
2329
+			return tx.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisProject").Preload("GoodInfo", "status=1")
2330
+		}).Preload("TempHisOrder", func(tx *gorm.DB) *gorm.DB {
2331
+		return tx.Where("status = 1 AND user_org_id = ?", org_id)
2332
+	}).
2333
+		Where("id in (?) AND user_org_id = ? AND status = 1", ids, org_id).
2334
+		Find(&prescription).Error
2335
+
2336
+	return
2337
+}
2262 2338
 
2263 2339
 func GetNewAllUnChargeHisPatientListTwo(org_id int64, keywords string, record_date int64) (patients []*NewTempPatients, err error) {
2264 2340
 	db := readDb.Model(&NewTempPatients{}).Where("xt_patients.user_org_id = ? AND xt_patients.status = 1", org_id)