|
@@ -0,0 +1,876 @@
|
|
1
|
+package service
|
|
2
|
+
|
|
3
|
+import (
|
|
4
|
+ "XT_New/models"
|
|
5
|
+ "XT_New/utils"
|
|
6
|
+ "fmt"
|
|
7
|
+ "github.com/jinzhu/gorm"
|
|
8
|
+ "github.com/shopspring/decimal"
|
|
9
|
+ "math/rand"
|
|
10
|
+ "strconv"
|
|
11
|
+ "strings"
|
|
12
|
+ "time"
|
|
13
|
+)
|
|
14
|
+
|
|
15
|
+//生成编号?+yymmdd+随机数(4位)
|
|
16
|
+//新增参数为CP
|
|
17
|
+//退款参数为AF
|
|
18
|
+func CreateCPCode(str string) string {
|
|
19
|
+ s := fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(10000))
|
|
20
|
+ t := time.Now().Format("20060102")
|
|
21
|
+ code := str + t + s
|
|
22
|
+ return code
|
|
23
|
+}
|
|
24
|
+
|
|
25
|
+//获取his中的有效患者
|
|
26
|
+func GetHisUser(orgid int64) (hisname []models.GetHisName, err error) {
|
|
27
|
+ err = XTReadDB().Model(&models.GetHisName{}).Where("status = 1 and user_org_id = ?", orgid).Find(&hisname).Error
|
|
28
|
+ return
|
|
29
|
+}
|
|
30
|
+
|
|
31
|
+//获取his中的有效患者去除了转出和死亡的
|
|
32
|
+func GetHisUserToAlive(orgid int64) (hisname []models.GetHisName, err error) {
|
|
33
|
+ err = XTReadDB().Model(&models.GetHisName{}).Where("status = 1 and user_org_id = ? and lapseto = 1", orgid).Find(&hisname).Error
|
|
34
|
+ return
|
|
35
|
+}
|
|
36
|
+
|
|
37
|
+//获取his中的有效患者
|
|
38
|
+func GetHisUserName(orgid, id int64) (hisname models.GetHisName, err error) {
|
|
39
|
+ err = XTReadDB().Model(&models.GetHisName{}).Where("status = 1 and user_org_id = ? and id = ?", orgid, id).Find(&hisname).Error
|
|
40
|
+ return
|
|
41
|
+}
|
|
42
|
+
|
|
43
|
+//添加押金(开始)调用接口前记得判断审核状态,确认通过审核后在调用
|
|
44
|
+func UpDeposit(code, remarks string, his_patient_id, orgid, trial_status, createid int64, deposit decimal.Decimal) (err error) {
|
|
45
|
+ //查押金表是否存在记录
|
|
46
|
+ tmp, tmpdeposit := IsHisPatientId(his_patient_id, orgid)
|
|
47
|
+ if tmp == 0 {
|
|
48
|
+ //需要生成一条数据
|
|
49
|
+ de := models.Deposit{
|
|
50
|
+ UserOrgId: orgid,
|
|
51
|
+ HisPatientId: his_patient_id,
|
|
52
|
+ Ctime: time.Now().Unix(),
|
|
53
|
+ Mtime: time.Now().Unix(),
|
|
54
|
+ Status: 1,
|
|
55
|
+ }
|
|
56
|
+ i, d, err := AddDeposit(de)
|
|
57
|
+ if err != nil || i == 0 {
|
|
58
|
+ utils.ErrorLog("添加用户押金记录失败: %v", err.Error())
|
|
59
|
+ return err
|
|
60
|
+ }
|
|
61
|
+ tmp = i
|
|
62
|
+ tmpdeposit = d
|
|
63
|
+ }
|
|
64
|
+ s_deposit := tmpdeposit
|
|
65
|
+ if trial_status == 1 {
|
|
66
|
+ de := models.Deposit{
|
|
67
|
+ ID: tmp,
|
|
68
|
+ Mtime: time.Now().Unix(),
|
|
69
|
+ Deposit: deposit.Add(tmpdeposit),
|
|
70
|
+ }
|
|
71
|
+ err = UpdateDeposit(de)
|
|
72
|
+ if err != nil {
|
|
73
|
+ utils.ErrorLog("添加用户押金失败: %v", err.Error())
|
|
74
|
+ return
|
|
75
|
+ }
|
|
76
|
+ s_deposit = deposit.Add(tmpdeposit)
|
|
77
|
+ }
|
|
78
|
+ dehistory := models.DepositHistory{
|
|
79
|
+ UserOrgId: orgid,
|
|
80
|
+ HisPatientId: his_patient_id,
|
|
81
|
+ DepositCode: code,
|
|
82
|
+ Deposit: deposit, //本次操作的押金
|
|
83
|
+ SurplusDeposit: s_deposit, //剩余金额
|
|
84
|
+ DepositStatus: 1,
|
|
85
|
+ Status: 1,
|
|
86
|
+ CreateId: createid,
|
|
87
|
+ Ctime: time.Now().Unix(),
|
|
88
|
+ Mtime: time.Now().Unix(),
|
|
89
|
+ TrialStatus: trial_status,
|
|
90
|
+ Remarks: remarks,
|
|
91
|
+ }
|
|
92
|
+ err = AddDepositHistory(dehistory)
|
|
93
|
+ if err != nil {
|
|
94
|
+ utils.ErrorLog("添加用户押金历史记录失败: %v", err.Error())
|
|
95
|
+ }
|
|
96
|
+ return
|
|
97
|
+}
|
|
98
|
+
|
|
99
|
+//添加押金记录
|
|
100
|
+func AddDepositHistory(dh models.DepositHistory) error {
|
|
101
|
+ err := XTWriteDB().Create(&dh).Error
|
|
102
|
+ return err
|
|
103
|
+}
|
|
104
|
+
|
|
105
|
+//押金中添加一条记录,并返回该数据的id和押金余额(当没有该用户记录时调用)
|
|
106
|
+func AddDeposit(de models.Deposit) (int64, decimal.Decimal, error) {
|
|
107
|
+ var tmp models.Deposit
|
|
108
|
+ err := XTWriteDB().Create(&de).Find(&tmp).Error
|
|
109
|
+ if err != nil {
|
|
110
|
+ return 0, decimal.NewFromFloat(0.00), err
|
|
111
|
+ }
|
|
112
|
+ return tmp.ID, tmp.Deposit, err
|
|
113
|
+}
|
|
114
|
+
|
|
115
|
+//更改押金记录
|
|
116
|
+func UpdateDeposit(de models.Deposit) (err error) {
|
|
117
|
+ err = XTWriteDB().Model(&models.Deposit{}).Where("id = ? and status = 1", de.ID).Updates(map[string]interface{}{
|
|
118
|
+ "mtime": de.Mtime,
|
|
119
|
+ "deposit": de.Deposit,
|
|
120
|
+ }).Error
|
|
121
|
+ return
|
|
122
|
+}
|
|
123
|
+
|
|
124
|
+//判断押金表是否存在当前的患者的一条有效数据,如果有返回id和押金余额,没有返回0
|
|
125
|
+func IsHisPatientId(his_patient_id, orgid int64) (int64, decimal.Decimal) {
|
|
126
|
+ var tmp []models.Deposit
|
|
127
|
+ XTReadDB().Model(&models.Deposit{}).Where("user_org_id = ? and his_patient_id = ? and status = 1", orgid, his_patient_id).Find(&tmp)
|
|
128
|
+ if len(tmp) != 1 {
|
|
129
|
+ return 0, decimal.NewFromFloat(0.00)
|
|
130
|
+ }
|
|
131
|
+ return tmp[0].ID, tmp[0].Deposit
|
|
132
|
+}
|
|
133
|
+
|
|
134
|
+//查询押金编号是否重复
|
|
135
|
+func FindDecimalCode(orgid int64, code string) bool {
|
|
136
|
+ var total int
|
|
137
|
+ XTReadDB().Model(&models.DepositHistory{}).Where("user_org_id = ? and deposit_code = ? and status = 1", orgid, code).Count(&total)
|
|
138
|
+ if total > 0 {
|
|
139
|
+ return true
|
|
140
|
+ } else {
|
|
141
|
+ return false
|
|
142
|
+ }
|
|
143
|
+}
|
|
144
|
+
|
|
145
|
+//充值明细列表
|
|
146
|
+func DetailsList(orgid, stime, etime int64, keyword string, slicekey []int64) (deposithistory []models.DepositHistoryname, err error) {
|
|
147
|
+ db := XTReadDB().Model(&models.DepositHistory{}).Where("status = 1 and user_org_id = ? and deposit_status = 1 ", orgid).Where("ctime >= ? and ctime <= ?", stime, etime)
|
|
148
|
+ if len(keyword) > 0 {
|
|
149
|
+ var tmp string = "deposit_code like ?"
|
|
150
|
+ if len(slicekey) > 0 {
|
|
151
|
+ for i := 0; i < len(slicekey); i++ {
|
|
152
|
+ tmp = tmp + " or his_patient_id = " + strconv.FormatInt(slicekey[i], 10)
|
|
153
|
+ }
|
|
154
|
+ }
|
|
155
|
+ keyword = "%" + keyword + "%"
|
|
156
|
+ db = db.Where(tmp, keyword)
|
|
157
|
+ }
|
|
158
|
+ err = db.Order("ctime desc").Find(&deposithistory).Error
|
|
159
|
+ return
|
|
160
|
+}
|
|
161
|
+
|
|
162
|
+//充值汇总列表
|
|
163
|
+func SummaryList(orgid, stime, etime int64, keyword string, slicekey []int64) (deposithistory []models.DepositHistory, err error) {
|
|
164
|
+ db := XTReadDB().Model(&models.DepositHistory{}).Where("status = 1 and trial_status = 1 and user_org_id = ? and deposit_status = 1 ", orgid).Where("ctime >= ? and ctime <= ?", stime, etime)
|
|
165
|
+ if len(slicekey) > 0 {
|
|
166
|
+ tmp := ""
|
|
167
|
+ for i := 0; i < len(slicekey); i++ {
|
|
168
|
+ tmp = tmp + " his_patient_id = " + strconv.FormatInt(slicekey[i], 10)
|
|
169
|
+ if i < len(slicekey)-1 {
|
|
170
|
+ tmp = tmp + " or "
|
|
171
|
+ }
|
|
172
|
+ }
|
|
173
|
+ db = db.Where(tmp)
|
|
174
|
+ } else {
|
|
175
|
+ if len(keyword) > 0 {
|
|
176
|
+ return
|
|
177
|
+ }
|
|
178
|
+ }
|
|
179
|
+ err = db.Order("ctime desc").Find(&deposithistory).Error
|
|
180
|
+ return
|
|
181
|
+}
|
|
182
|
+
|
|
183
|
+//获取本月的起止时间戳
|
|
184
|
+func GetMonth() (int64, int64) {
|
|
185
|
+ timeNow := time.Now()
|
|
186
|
+ timeToday := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, timeNow.Location()) // 获取当天0点时间 time类型
|
|
187
|
+ timeMonthStartUnix1 := timeToday.AddDate(0, 0, -timeToday.Day()+1).Unix() // 获取本月第一天0点 时间戳类型
|
|
188
|
+ timeMonthEndUnix1 := timeToday.AddDate(0, 1, -timeToday.Day()+1).Unix() - 1 // 获取下个月第一天/ 本月最后一天24点 时间戳类型
|
|
189
|
+ return timeMonthStartUnix1, timeMonthEndUnix1
|
|
190
|
+}
|
|
191
|
+
|
|
192
|
+//获取创建者的姓名
|
|
193
|
+func GetCreateidName(id int64) string {
|
|
194
|
+ var tmp models.CreateUser
|
|
195
|
+ XTReadDB().Select("name").Where("id = ?", id).Find(&tmp)
|
|
196
|
+ return tmp.Name
|
|
197
|
+}
|
|
198
|
+
|
|
199
|
+//审核通过
|
|
200
|
+func UpDecimalHistory(id int64) (err error) {
|
|
201
|
+ //开事务
|
|
202
|
+ tx := XTWriteDB().Begin()
|
|
203
|
+ defer func() {
|
|
204
|
+ if err != nil {
|
|
205
|
+ utils.ErrorLog("事务失败,原因为: %v", err)
|
|
206
|
+ tx.Rollback()
|
|
207
|
+ } else {
|
|
208
|
+ tx.Commit()
|
|
209
|
+ }
|
|
210
|
+ }()
|
|
211
|
+
|
|
212
|
+ //查记录x2
|
|
213
|
+ var history models.DepositHistory
|
|
214
|
+ var detmp models.Deposit
|
|
215
|
+ err = tx.Model(&models.DepositHistory{}).Where("id = ?", id).Find(&history).Error
|
|
216
|
+ if err != nil {
|
|
217
|
+ return
|
|
218
|
+ }
|
|
219
|
+ err = tx.Model(&models.Deposit{}).Where("user_org_id = ? and his_patient_id = ? and status = 1 ", history.UserOrgId, history.HisPatientId).Find(&detmp).Error
|
|
220
|
+ if err != nil {
|
|
221
|
+ return
|
|
222
|
+ }
|
|
223
|
+ //改状态,=====剩余押金
|
|
224
|
+ err = tx.Model(models.DepositHistory{}).Where("id = ?", id).Updates(map[string]interface{}{
|
|
225
|
+ "trial_status": 1,
|
|
226
|
+ "surplus_deposit": detmp.Deposit.Add(history.Deposit),
|
|
227
|
+ "mtime": time.Now().Unix(),
|
|
228
|
+ }).Error
|
|
229
|
+ if err != nil {
|
|
230
|
+ return
|
|
231
|
+ }
|
|
232
|
+ //相加
|
|
233
|
+ err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", detmp.ID).Updates(map[string]interface{}{
|
|
234
|
+ "mtime": time.Now().Unix(),
|
|
235
|
+ "deposit": detmp.Deposit.Add(history.Deposit),
|
|
236
|
+ }).Error
|
|
237
|
+
|
|
238
|
+ return
|
|
239
|
+}
|
|
240
|
+
|
|
241
|
+//删除本次记录
|
|
242
|
+func DelDecimalHistory(id int64) (err error) {
|
|
243
|
+ err = XTWriteDB().Model(models.DepositHistory{}).Where("id = ?", id).Updates(map[string]interface{}{
|
|
244
|
+ "status": 0,
|
|
245
|
+ "mtime": time.Now().Unix(),
|
|
246
|
+ }).Error
|
|
247
|
+ return
|
|
248
|
+}
|
|
249
|
+
|
|
250
|
+//根据id获取一条押金历史记录
|
|
251
|
+func GetDecimalHistoryOne(id int64) (history models.DepositHistory, err error) {
|
|
252
|
+ err = XTReadDB().Model(&models.DepositHistory{}).Where("id = ?", id).Find(&history).Error
|
|
253
|
+ return
|
|
254
|
+}
|
|
255
|
+
|
|
256
|
+//根据患者id获取该患者当前剩余的押金
|
|
257
|
+func GetUserMoney(id, orgid int64) decimal.Decimal {
|
|
258
|
+ tmp := models.Deposit{}
|
|
259
|
+ err := XTReadDB().Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&tmp).Error
|
|
260
|
+ if err != nil {
|
|
261
|
+ return decimal.NewFromFloat(0)
|
|
262
|
+ }
|
|
263
|
+ return tmp.Deposit
|
|
264
|
+}
|
|
265
|
+
|
|
266
|
+//查询时间段内患者的余额
|
|
267
|
+func GetMoneyforTime(id, orgid, etime int64) decimal.Decimal {
|
|
268
|
+ tmp := models.DepositHistory{}
|
|
269
|
+ XTReadDB().Model(&models.DepositHistory{}).Where("his_patient_id = ? and user_org_id = ? and status = 1 and mtime <= ?", id, orgid, etime).Order("mtime").Find(&tmp)
|
|
270
|
+ return tmp.SurplusDeposit
|
|
271
|
+}
|
|
272
|
+
|
|
273
|
+//押金流水
|
|
274
|
+func GetFlowList(page, limit, id, orgid, stime, etime, deposit_status int64) (deposit []models.DepositHistory, total int64, err error) {
|
|
275
|
+ s := "status = 1 and trial_status = 1 and user_org_id = ? and his_patient_id = ? and mtime >= ? and mtime <= ?"
|
|
276
|
+ offset := (page - 1) * limit
|
|
277
|
+ if deposit_status != 0 {
|
|
278
|
+ s = s + " and deposit_status = " + strconv.FormatInt(deposit_status, 10)
|
|
279
|
+ }
|
|
280
|
+ err = XTReadDB().Model(&models.DepositHistory{}).Where(s, orgid, id, stime, etime).Count(&total).Offset(offset).Order("mtime desc").Limit(limit).Find(&deposit).Error
|
|
281
|
+ return
|
|
282
|
+}
|
|
283
|
+
|
|
284
|
+//获取患者押金列表
|
|
285
|
+func GetUserList(page, limit, orgid int64, keyword string, slicekey []int64) (m []models.Deposit1, total int64, err error) {
|
|
286
|
+ db := XTReadDB().Model(&models.Deposit{}).Where("status = 1 and user_org_id = ? ", orgid)
|
|
287
|
+ offset := (page - 1) * limit
|
|
288
|
+ if len(keyword) > 0 {
|
|
289
|
+ db = db.Where(" his_patient_id = ? ", keyword)
|
|
290
|
+ }
|
|
291
|
+ err = db.Count(&total).Offset(offset).Order("mtime desc").Limit(limit).Find(&m).Error
|
|
292
|
+ return
|
|
293
|
+}
|
|
294
|
+
|
|
295
|
+//扣费明细列表
|
|
296
|
+func DeductionList(orgid, stime, etime int64, keyword string, slicekey []int64) (deposithistory []models.DepositHistory, err error) {
|
|
297
|
+ db := XTReadDB().Model(&models.DepositHistory{}).Where("status = 1 and user_org_id = ? and deposit_status = 2 ", orgid).Where("ctime >= ? and ctime <= ?", stime, etime)
|
|
298
|
+ if len(keyword) > 0 {
|
|
299
|
+ var tmp string = "deposit_code like ?"
|
|
300
|
+ if len(slicekey) > 0 {
|
|
301
|
+ for i := 0; i < len(slicekey); i++ {
|
|
302
|
+ tmp = tmp + " or his_patient_id = " + strconv.FormatInt(slicekey[i], 10)
|
|
303
|
+ }
|
|
304
|
+ }
|
|
305
|
+ keyword = "%" + keyword + "%"
|
|
306
|
+ db = db.Where(tmp, keyword)
|
|
307
|
+ }
|
|
308
|
+ err = db.Order("ctime desc").Find(&deposithistory).Error
|
|
309
|
+ return
|
|
310
|
+}
|
|
311
|
+
|
|
312
|
+//获取医疗费总额
|
|
313
|
+func MedicalTotal(orgid, id int64) decimal.Decimal {
|
|
314
|
+ tmp := models.HisChargeSettleOrder{}
|
|
315
|
+ //and status = 1 and order_status = 2
|
|
316
|
+ XTReadDB().Model(&models.HisChargeSettleOrder{}).Where("id = ? and user_org_id = ? ", id, orgid).Find(&tmp)
|
|
317
|
+ return decimal.NewFromFloat(tmp.MedfeeSumamt)
|
|
318
|
+}
|
|
319
|
+
|
|
320
|
+//根据就诊号查询出医收费类型
|
|
321
|
+func CodeToChargetype(orgid int64, code string) (s string) {
|
|
322
|
+ tmp := models.HisPrintPatient{}
|
|
323
|
+ tmp_hhcr := models.HisHospitalCheckRecordTwo{}
|
|
324
|
+ XTReadDB().Model(&models.HisPrintPatient{}).Where("number = ? and user_org_id = ?", code, orgid).Find(&tmp)
|
|
325
|
+ //tmp.BalanceAccountsType
|
|
326
|
+ if tmp.BalanceAccountsType == 0 {
|
|
327
|
+ XTReadDB().Model(&models.HisHospitalCheckRecordTwo{}).Where("number = ? and user_org_id = ?", code, orgid).Find(&tmp_hhcr)
|
|
328
|
+ tmp.BalanceAccountsType = tmp_hhcr.BalanceAccountsType
|
|
329
|
+ }
|
|
330
|
+ switch tmp.BalanceAccountsType {
|
|
331
|
+ case 1:
|
|
332
|
+ s = "医保"
|
|
333
|
+ case 2:
|
|
334
|
+ s = "自费"
|
|
335
|
+ case 3:
|
|
336
|
+ s = "公费"
|
|
337
|
+ case 4:
|
|
338
|
+ s = "农保"
|
|
339
|
+ case 5:
|
|
340
|
+ s = "会员"
|
|
341
|
+ case 6:
|
|
342
|
+ s = "职工"
|
|
343
|
+ case 7:
|
|
344
|
+ s = "合同"
|
|
345
|
+ case 8:
|
|
346
|
+ s = "医保自费"
|
|
347
|
+ default:
|
|
348
|
+ s = "类型未定义"
|
|
349
|
+ }
|
|
350
|
+
|
|
351
|
+ return
|
|
352
|
+}
|
|
353
|
+
|
|
354
|
+//扣除患者的押金,并生成一条扣费历史记录
|
|
355
|
+//orgid 机构id;
|
|
356
|
+//his_user_id 患者id;
|
|
357
|
+//create_id 创建者id;
|
|
358
|
+//code 就诊号;
|
|
359
|
+//deposit 本次使用的押金金额,支持float64、int、int64、decimal.Decimal类型
|
|
360
|
+func SpendDeposit(orgid, his_user_id, create_id int64, code string, deposit interface{}) (err error) {
|
|
361
|
+ tmp_deposit := decimal.Decimal{} //本次患者使用的押金
|
|
362
|
+ tmp_deposit, err = ToDecimal(deposit)
|
|
363
|
+ if err != nil {
|
|
364
|
+ return
|
|
365
|
+ }
|
|
366
|
+ //开事务
|
|
367
|
+ tx := XTWriteDB().Begin()
|
|
368
|
+ defer func() {
|
|
369
|
+ if err != nil {
|
|
370
|
+ utils.ErrorLog("事务失败,原因为: %v", err.Error())
|
|
371
|
+ tx.Rollback()
|
|
372
|
+ } else {
|
|
373
|
+ tx.Commit()
|
|
374
|
+ }
|
|
375
|
+ }()
|
|
376
|
+ //查询患者押金
|
|
377
|
+ tmp := models.Deposit{}
|
|
378
|
+ err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", his_user_id, orgid).Find(&tmp).Error
|
|
379
|
+ if err != nil {
|
|
380
|
+ err = fmt.Errorf("押金余额不足")
|
|
381
|
+ return
|
|
382
|
+ }
|
|
383
|
+ //判断能否扣除
|
|
384
|
+ if tmp.Deposit.Cmp(tmp_deposit) == -1 {
|
|
385
|
+ err = fmt.Errorf("押金余额不足")
|
|
386
|
+ return
|
|
387
|
+ }
|
|
388
|
+ //扣除患者押金
|
|
389
|
+ err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", tmp.ID).Updates(map[string]interface{}{
|
|
390
|
+ "mtime": time.Now().Unix(),
|
|
391
|
+ "deposit": tmp.Deposit.Sub(tmp_deposit),
|
|
392
|
+ }).Error
|
|
393
|
+ if err != nil {
|
|
394
|
+ return
|
|
395
|
+ }
|
|
396
|
+ //生成一条历史记录
|
|
397
|
+ dehistory := models.DepositHistory{
|
|
398
|
+ UserOrgId: orgid,
|
|
399
|
+ HisPatientId: his_user_id,
|
|
400
|
+ DepositCode: code,
|
|
401
|
+ Deposit: tmp_deposit,
|
|
402
|
+ SurplusDeposit: tmp.Deposit.Sub(tmp_deposit),
|
|
403
|
+ DepositStatus: 2,
|
|
404
|
+ Status: 1,
|
|
405
|
+ CreateId: create_id,
|
|
406
|
+ Ctime: time.Now().Unix(),
|
|
407
|
+ Mtime: time.Now().Unix(),
|
|
408
|
+ TrialStatus: 1,
|
|
409
|
+ }
|
|
410
|
+ err = tx.Create(&dehistory).Error
|
|
411
|
+ return
|
|
412
|
+}
|
|
413
|
+
|
|
414
|
+//新增一条退款申请
|
|
415
|
+func RefundApplication(orgid, his_patient_id, trial_status, createid int64, code string, deposit decimal.Decimal) (err error) {
|
|
416
|
+ //开事务
|
|
417
|
+ tx := XTWriteDB().Begin()
|
|
418
|
+ defer func() {
|
|
419
|
+ if err != nil {
|
|
420
|
+ utils.ErrorLog("事务失败,原因为: %v", err.Error())
|
|
421
|
+ tx.Rollback()
|
|
422
|
+ } else {
|
|
423
|
+ tx.Commit()
|
|
424
|
+ }
|
|
425
|
+ }()
|
|
426
|
+ //获取患者当前余额
|
|
427
|
+ tmp := models.Deposit{}
|
|
428
|
+ err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", his_patient_id, orgid).Find(&tmp).Error
|
|
429
|
+ if err != nil {
|
|
430
|
+ if err.Error() == "record not found" {
|
|
431
|
+ err = fmt.Errorf("没有可退押金,保存失败")
|
|
432
|
+ }
|
|
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]
|
|
477
|
+ } else {
|
|
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 {
|
|
616
|
+ return
|
|
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(page, limit, orgid, stime, etime, refundtype, examinetype int64, keyword string, slicekey []int64) (depo []models.RefundList, total int64, err error) {
|
|
642
|
+ db := XTReadDB().Model(&models.DepositHistory{}).Where("status = 1 and user_org_id = ? ", orgid).Where("ctime >= ? and ctime <= ?", stime, etime)
|
|
643
|
+ offset := (page - 1) * limit
|
|
644
|
+ //退款类型
|
|
645
|
+ switch refundtype {
|
|
646
|
+ case 0:
|
|
647
|
+ db = db.Where(" deposit_status in (3,4) ")
|
|
648
|
+ case 3, 4:
|
|
649
|
+ db = db.Where(" deposit_status = ? ", refundtype)
|
|
650
|
+ default:
|
|
651
|
+ err = fmt.Errorf("退款类型错误")
|
|
652
|
+ return
|
|
653
|
+ }
|
|
654
|
+ //审核状态
|
|
655
|
+ switch examinetype {
|
|
656
|
+ case 0, 1, 2:
|
|
657
|
+ db = db.Where(" trial_status = ? ", examinetype)
|
|
658
|
+ case 3: //代表查询全部
|
|
659
|
+ db = db
|
|
660
|
+ default:
|
|
661
|
+ err = fmt.Errorf("审核状态错误")
|
|
662
|
+ return
|
|
663
|
+ }
|
|
664
|
+ if len(keyword) > 0 {
|
|
665
|
+ tmp := "deposit_code like ? "
|
|
666
|
+ if len(slicekey) > 0 {
|
|
667
|
+ for i := 0; i < len(slicekey); i++ {
|
|
668
|
+ tmp = tmp + " or his_patient_id = " + strconv.FormatInt(slicekey[i], 10)
|
|
669
|
+ }
|
|
670
|
+ }
|
|
671
|
+ keyword = "%" + keyword + "%"
|
|
672
|
+ db = db.Where(tmp, keyword)
|
|
673
|
+ }
|
|
674
|
+ err = db.Count(&total).Offset(offset).Order("ctime desc").Limit(limit).Find(&depo).Error
|
|
675
|
+ return
|
|
676
|
+
|
|
677
|
+}
|
|
678
|
+
|
|
679
|
+//退回患者的押金,并生成一条退费历史记录
|
|
680
|
+//orgid 机构id
|
|
681
|
+//his_user_id 患者id
|
|
682
|
+//code 编号
|
|
683
|
+//deposit 本次退费的金额,支持float64、int、int64、decimal.Decimal类型
|
|
684
|
+func MoneyIncrease(orgid, his_user_id int64, code string, deposit interface{}) (err error) {
|
|
685
|
+ tmp_deposit := decimal.Decimal{}
|
|
686
|
+ tmp_deposit, err = ToDecimal(deposit)
|
|
687
|
+ if err != nil {
|
|
688
|
+ return
|
|
689
|
+ }
|
|
690
|
+ //开事务
|
|
691
|
+ tx := XTWriteDB().Begin()
|
|
692
|
+ defer func() {
|
|
693
|
+ if err != nil {
|
|
694
|
+ utils.ErrorLog("事务失败,原因为: %v", err.Error())
|
|
695
|
+ tx.Rollback()
|
|
696
|
+ } else {
|
|
697
|
+ tx.Commit()
|
|
698
|
+ }
|
|
699
|
+ }()
|
|
700
|
+ //获取当前患者押金
|
|
701
|
+ tmp := models.Deposit{}
|
|
702
|
+ err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", his_user_id, orgid).Find(&tmp).Error
|
|
703
|
+ if err != nil {
|
|
704
|
+ return
|
|
705
|
+ }
|
|
706
|
+ //退回患者押金
|
|
707
|
+ err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", tmp.ID).Updates(map[string]interface{}{
|
|
708
|
+ "mtime": time.Now().Unix(),
|
|
709
|
+ "deposit": tmp.Deposit.Add(tmp_deposit),
|
|
710
|
+ }).Error
|
|
711
|
+ if err != nil {
|
|
712
|
+ return
|
|
713
|
+ }
|
|
714
|
+ //生成一条退费记录
|
|
715
|
+ dehistory := models.DepositHistory{
|
|
716
|
+ UserOrgId: orgid,
|
|
717
|
+ HisPatientId: his_user_id,
|
|
718
|
+ DepositCode: code,
|
|
719
|
+ Deposit: tmp_deposit,
|
|
720
|
+ SurplusDeposit: tmp.Deposit.Add(tmp_deposit),
|
|
721
|
+ DepositStatus: 4,
|
|
722
|
+ Status: 1,
|
|
723
|
+ CreateId: 0,
|
|
724
|
+ Ctime: time.Now().Unix(),
|
|
725
|
+ Mtime: time.Now().Unix(),
|
|
726
|
+ TrialStatus: 1,
|
|
727
|
+ }
|
|
728
|
+ err = tx.Create(&dehistory).Error
|
|
729
|
+ return
|
|
730
|
+}
|
|
731
|
+func GetorgName(orgid int64) (name string) {
|
|
732
|
+ tmp := models.GetorgName{}
|
|
733
|
+ XTReadDB().Model(&models.GetorgName{}).Where("id = ? ", orgid).Find(&tmp)
|
|
734
|
+ return tmp.OrgName
|
|
735
|
+}
|
|
736
|
+
|
|
737
|
+//根据id查询就诊号
|
|
738
|
+func FindcodeToid(id int64) (code string) {
|
|
739
|
+ tmp := models.GetMdtrtId{}
|
|
740
|
+ XTReadDB().Model(&models.GetMdtrtId{}).Where("id = ?", id).Find(&tmp)
|
|
741
|
+ return tmp.MdtrtId
|
|
742
|
+}
|
|
743
|
+
|
|
744
|
+//根据id查询就诊号
|
|
745
|
+func FindnumberToid(id int64) (number string) {
|
|
746
|
+ tmp := models.GetMdtrtId{}
|
|
747
|
+ XTReadDB().Model(&models.GetMdtrtId{}).Where("id = ?", id).Find(&tmp)
|
|
748
|
+ return tmp.Number
|
|
749
|
+}
|
|
750
|
+
|
|
751
|
+//扣费明细列表,查看详情按钮是否显示,0隐藏,1显示
|
|
752
|
+func IsButtonShow(code string, orgid, his_patient_id int64) (tmp int64) {
|
|
753
|
+ var total int
|
|
754
|
+ XTReadDB().Model(&models.RefundList{}).Where(" user_org_id = ? and his_patient_id = ? and deposit_code = ? and deposit_status = 4 and status = 1", orgid, his_patient_id, code).Count(&total)
|
|
755
|
+ if total == 0 {
|
|
756
|
+ return 1
|
|
757
|
+ } else {
|
|
758
|
+ //预防万一,多加一个验证
|
|
759
|
+ if total%2 == 0 {
|
|
760
|
+ return 1
|
|
761
|
+ } else {
|
|
762
|
+ return 0
|
|
763
|
+ }
|
|
764
|
+ }
|
|
765
|
+}
|
|
766
|
+
|
|
767
|
+//把其他的类型转换成decimal.Decimal类型
|
|
768
|
+func ToDecimal(i interface{}) (d decimal.Decimal, err error) {
|
|
769
|
+ switch i.(type) {
|
|
770
|
+ case float64:
|
|
771
|
+ d = decimal.NewFromFloat(i.(float64))
|
|
772
|
+ case decimal.Decimal:
|
|
773
|
+ d = i.(decimal.Decimal)
|
|
774
|
+ case int64:
|
|
775
|
+ d = decimal.NewFromFloat(float64(i.(int64)))
|
|
776
|
+ case int:
|
|
777
|
+ d = decimal.NewFromFloat(float64(i.(int)))
|
|
778
|
+ default:
|
|
779
|
+ err = fmt.Errorf("类型解析错误")
|
|
780
|
+ }
|
|
781
|
+ return
|
|
782
|
+}
|
|
783
|
+
|
|
784
|
+func TypeConversion(tmp float64) (s string) {
|
|
785
|
+ switch tmp {
|
|
786
|
+ case 1:
|
|
787
|
+ s = "g"
|
|
788
|
+ case 2:
|
|
789
|
+ s = "mg"
|
|
790
|
+ case 3:
|
|
791
|
+ s = "u"
|
|
792
|
+ case 4:
|
|
793
|
+ s = "ml"
|
|
794
|
+ case 5:
|
|
795
|
+ s = "万U"
|
|
796
|
+ case 6:
|
|
797
|
+ s = "枚"
|
|
798
|
+ case 7:
|
|
799
|
+ s = "粒"
|
|
800
|
+ case 8:
|
|
801
|
+ s = "片"
|
|
802
|
+ case 9:
|
|
803
|
+ s = "支"
|
|
804
|
+ case 10:
|
|
805
|
+ s = "μg"
|
|
806
|
+ case 11:
|
|
807
|
+ s = "iu"
|
|
808
|
+ case 12:
|
|
809
|
+ s = "包"
|
|
810
|
+ case 13:
|
|
811
|
+ s = "袋"
|
|
812
|
+ case 14:
|
|
813
|
+ s = "万"
|
|
814
|
+ case 15:
|
|
815
|
+ s = "万iu"
|
|
816
|
+ case 16:
|
|
817
|
+ s = "丸"
|
|
818
|
+ case 17:
|
|
819
|
+ s = "盒"
|
|
820
|
+ case 18:
|
|
821
|
+ s = "瓶"
|
|
822
|
+ case 19:
|
|
823
|
+ s = "瓶(袋)"
|
|
824
|
+ case 20:
|
|
825
|
+ s = "次"
|
|
826
|
+ }
|
|
827
|
+ return s
|
|
828
|
+}
|
|
829
|
+
|
|
830
|
+func TypeConversion02(tmp string) (s string) {
|
|
831
|
+ switch tmp {
|
|
832
|
+ case "1":
|
|
833
|
+ s = "g"
|
|
834
|
+ case "2":
|
|
835
|
+ s = "mg"
|
|
836
|
+ case "3":
|
|
837
|
+ s = "u"
|
|
838
|
+ case "4":
|
|
839
|
+ s = "ml"
|
|
840
|
+ case "5":
|
|
841
|
+ s = "万U"
|
|
842
|
+ case "6":
|
|
843
|
+ s = "枚"
|
|
844
|
+ case "7":
|
|
845
|
+ s = "粒"
|
|
846
|
+ case "8":
|
|
847
|
+ s = "片"
|
|
848
|
+ case "9":
|
|
849
|
+ s = "支"
|
|
850
|
+ case "10":
|
|
851
|
+ s = "μg"
|
|
852
|
+ case "11":
|
|
853
|
+ s = "iu"
|
|
854
|
+ case "12":
|
|
855
|
+ s = "包"
|
|
856
|
+ case "13":
|
|
857
|
+ s = "袋"
|
|
858
|
+ case "14":
|
|
859
|
+ s = "万"
|
|
860
|
+ case "15":
|
|
861
|
+ s = "万iu"
|
|
862
|
+ case "16":
|
|
863
|
+ s = "丸"
|
|
864
|
+ case "17":
|
|
865
|
+ s = "盒"
|
|
866
|
+ case "18":
|
|
867
|
+ s = "瓶"
|
|
868
|
+ case "19":
|
|
869
|
+ s = "瓶(袋)"
|
|
870
|
+ case "20":
|
|
871
|
+ s = "次"
|
|
872
|
+ default:
|
|
873
|
+ s = tmp
|
|
874
|
+ }
|
|
875
|
+ return s
|
|
876
|
+}
|