his_deposit_service.go 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. package service
  2. import (
  3. "XT_New/models"
  4. "XT_New/utils"
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "github.com/shopspring/decimal"
  8. "math/rand"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. //生成编号?+yymmdd+随机数(4位)
  14. //新增参数为CP
  15. //退款参数为AF
  16. func CreateCPCode(str string) string {
  17. s := fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(10000))
  18. t := time.Now().Format("20060102")
  19. code := str + t + s
  20. return code
  21. }
  22. //获取his中的有效患者
  23. func GetHisUser(orgid int64) (hisname []models.GetHisName, err error) {
  24. err = XTReadDB().Model(&models.GetHisName{}).Where("status = 1 and user_org_id = ?", orgid).Find(&hisname).Error
  25. return
  26. }
  27. //获取his中的有效患者
  28. func GetHisUserName(orgid, id int64) (hisname models.GetHisName, err error) {
  29. err = XTReadDB().Model(&models.GetHisName{}).Where("status = 1 and user_org_id = ? and id = ?", orgid, id).Find(&hisname).Error
  30. return
  31. }
  32. //添加押金(开始)调用接口前记得判断审核状态,确认通过审核后在调用
  33. func UpDeposit(code, remarks string, his_patient_id, orgid, trial_status, createid int64, deposit decimal.Decimal) (err error) {
  34. //查押金表是否存在记录
  35. tmp, tmpdeposit := IsHisPatientId(his_patient_id, orgid)
  36. if tmp == 0 {
  37. //需要生成一条数据
  38. de := models.Deposit{
  39. UserOrgId: orgid,
  40. HisPatientId: his_patient_id,
  41. Ctime: time.Now().Unix(),
  42. Mtime: time.Now().Unix(),
  43. Status: 1,
  44. }
  45. i, d, err := AddDeposit(de)
  46. if err != nil || i == 0 {
  47. utils.ErrorLog("添加用户押金记录失败: %v", err.Error())
  48. return err
  49. }
  50. tmp = i
  51. tmpdeposit = d
  52. }
  53. s_deposit := tmpdeposit
  54. if trial_status == 1 {
  55. de := models.Deposit{
  56. ID: tmp,
  57. Mtime: time.Now().Unix(),
  58. Deposit: deposit.Add(tmpdeposit),
  59. }
  60. err = UpdateDeposit(de)
  61. if err != nil {
  62. utils.ErrorLog("添加用户押金失败: %v", err.Error())
  63. return
  64. }
  65. s_deposit = deposit.Add(tmpdeposit)
  66. }
  67. dehistory := models.DepositHistory{
  68. UserOrgId: orgid,
  69. HisPatientId: his_patient_id,
  70. DepositCode: code,
  71. Deposit: deposit, //本次操作的押金
  72. SurplusDeposit: s_deposit, //剩余金额
  73. DepositStatus: 1,
  74. Status: 1,
  75. CreateId: createid,
  76. Ctime: time.Now().Unix(),
  77. Mtime: time.Now().Unix(),
  78. TrialStatus: trial_status,
  79. Remarks: remarks,
  80. }
  81. err = AddDepositHistory(dehistory)
  82. if err != nil {
  83. utils.ErrorLog("添加用户押金历史记录失败: %v", err.Error())
  84. }
  85. return
  86. }
  87. //添加押金记录
  88. func AddDepositHistory(dh models.DepositHistory) error {
  89. err := XTWriteDB().Create(&dh).Error
  90. return err
  91. }
  92. //押金中添加一条记录,并返回该数据的id和押金余额(当没有该用户记录时调用)
  93. func AddDeposit(de models.Deposit) (int64, decimal.Decimal, error) {
  94. var tmp models.Deposit
  95. err := XTWriteDB().Create(&de).Find(&tmp).Error
  96. if err != nil {
  97. return 0, decimal.NewFromFloat(0.00), err
  98. }
  99. return tmp.ID, tmp.Deposit, err
  100. }
  101. //更改押金记录
  102. func UpdateDeposit(de models.Deposit) (err error) {
  103. err = XTWriteDB().Model(&models.Deposit{}).Where("id = ? and status = 1", de.ID).Updates(map[string]interface{}{
  104. "mtime": de.Mtime,
  105. "deposit": de.Deposit,
  106. }).Error
  107. return
  108. }
  109. //判断押金表是否存在当前的患者的一条有效数据,如果有返回id和押金余额,没有返回0
  110. func IsHisPatientId(his_patient_id, orgid int64) (int64, decimal.Decimal) {
  111. var tmp []models.Deposit
  112. XTReadDB().Model(&models.Deposit{}).Where("user_org_id = ? and his_patient_id = ? and status = 1", orgid, his_patient_id).Find(&tmp)
  113. if len(tmp) != 1 {
  114. return 0, decimal.NewFromFloat(0.00)
  115. }
  116. return tmp[0].ID, tmp[0].Deposit
  117. }
  118. //查询押金编号是否重复
  119. func FindDecimalCode(orgid int64, code string) bool {
  120. var total int
  121. XTReadDB().Model(&models.DepositHistory{}).Where("user_org_id = ? and deposit_code = ? and status = 1", orgid, code).Count(&total)
  122. if total > 0 {
  123. return true
  124. } else {
  125. return false
  126. }
  127. }
  128. //充值明细列表
  129. func DetailsList(orgid, stime, etime int64, keyword string, slicekey []int64) (deposithistory []models.DepositHistoryname, err error) {
  130. db := XTReadDB().Model(&models.DepositHistory{}).Where("status = 1 and user_org_id = ? and deposit_status = 1 ", orgid).Where("ctime >= ? and ctime <= ?", stime, etime)
  131. if len(keyword) > 0 {
  132. var tmp string = "deposit_code like ?"
  133. if len(slicekey) > 0 {
  134. for i := 0; i < len(slicekey); i++ {
  135. tmp = tmp + " or his_patient_id = " + strconv.FormatInt(slicekey[i], 10)
  136. }
  137. }
  138. keyword = "%" + keyword + "%"
  139. db = db.Where(tmp, keyword)
  140. }
  141. err = db.Order("ctime desc").Find(&deposithistory).Error
  142. return
  143. }
  144. //充值汇总列表
  145. func SummaryList(orgid, stime, etime int64, keyword string, slicekey []int64) (deposithistory []models.DepositHistory, err error) {
  146. 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)
  147. if len(slicekey) > 0 {
  148. tmp := ""
  149. for i := 0; i < len(slicekey); i++ {
  150. tmp = tmp + " his_patient_id = " + strconv.FormatInt(slicekey[i], 10)
  151. if i < len(slicekey)-1 {
  152. tmp = tmp + " or "
  153. }
  154. }
  155. db = db.Where(tmp)
  156. } else {
  157. if len(keyword) > 0 {
  158. return
  159. }
  160. }
  161. err = db.Order("ctime desc").Find(&deposithistory).Error
  162. return
  163. }
  164. //获取本周周一和周日的起止时间戳
  165. func GetMondayOfWeek() (int64, int64) {
  166. t := time.Now()
  167. dayObj := GetZeroTime(t)
  168. var dayStr string
  169. loc, _ := time.LoadLocation("Local")
  170. if t.Weekday() == time.Monday {
  171. dayStr = dayObj.Format("2006-01-02")
  172. } else {
  173. offset := int(time.Monday - t.Weekday())
  174. if offset > 0 {
  175. offset = -6
  176. }
  177. dayStr = dayObj.AddDate(0, 0, offset).Format("2006-01-02")
  178. }
  179. dayStr = dayStr + " 00:00:00"
  180. stime, _ := time.ParseInLocation("2006-01-02 15:04:05", dayStr, loc)
  181. return stime.Unix(), stime.Unix() + 604799
  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. func GetCreateidName(id int64) string {
  192. var tmp models.CreateUser
  193. XTReadDB().Select("name").Where("id = ?", id).Find(&tmp)
  194. return tmp.Name
  195. }
  196. //审核通过
  197. func UpDecimalHistory(id int64) (err error) {
  198. //开事务
  199. tx := XTWriteDB().Begin()
  200. defer func() {
  201. if err != nil {
  202. utils.ErrorLog("事务失败,原因为: %v", err)
  203. tx.Rollback()
  204. } else {
  205. tx.Commit()
  206. }
  207. }()
  208. //改状态
  209. err = tx.Model(models.DepositHistory{}).Where("id = ?", id).Updates(map[string]interface{}{
  210. "trial_status": 1,
  211. "mtime": time.Now().Unix(),
  212. }).Error
  213. if err != nil {
  214. return
  215. }
  216. //查记录x2
  217. var history models.DepositHistory
  218. var detmp models.Deposit
  219. err = tx.Model(&models.DepositHistory{}).Where("id = ?", id).Find(&history).Error
  220. if err != nil {
  221. return
  222. }
  223. err = tx.Model(&models.Deposit{}).Where("user_org_id = ? and his_patient_id = ? and status = 1 ", history.UserOrgId, history.HisPatientId).Find(&detmp).Error
  224. if err != nil {
  225. return
  226. }
  227. //相加
  228. err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", detmp.ID).Updates(map[string]interface{}{
  229. "mtime": time.Now().Unix(),
  230. "deposit": detmp.Deposit.Add(history.Deposit),
  231. }).Error
  232. return
  233. }
  234. //删除本次记录
  235. func DelDecimalHistory(id int64) (err error) {
  236. err = XTWriteDB().Model(models.DepositHistory{}).Where("id = ?", id).Updates(map[string]interface{}{
  237. "status": 0,
  238. "mtime": time.Now().Unix(),
  239. }).Error
  240. return
  241. }
  242. //根据id获取一条押金历史记录
  243. func GetDecimalHistoryOne(id int64) (history models.DepositHistory, err error) {
  244. err = XTReadDB().Model(&models.DepositHistory{}).Where("id = ?", id).Find(&history).Error
  245. return
  246. }
  247. //根据患者id获取该患者当前剩余的押金
  248. func GetUserMoney(id, orgid int64) decimal.Decimal {
  249. tmp := models.Deposit{}
  250. err := XTReadDB().Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&tmp).Error
  251. if err != nil {
  252. return decimal.NewFromFloat(0)
  253. }
  254. return tmp.Deposit
  255. }
  256. //查询时间段内患者的余额
  257. func GetMoneyforTime(id, orgid, etime int64) decimal.Decimal {
  258. tmp := models.DepositHistory{}
  259. XTReadDB().Model(&models.DepositHistory{}).Where("his_patient_id = ? and user_org_id = ? and status = 1 and mtime <= ?", id, orgid, etime).Order("mtime").Find(&tmp)
  260. return tmp.SurplusDeposit
  261. }
  262. //押金流水
  263. func GetFlowList(id, orgid, stime, etime, deposit_status int64) (deposit []models.DepositHistory, err error) {
  264. s := "status = 1 and trial_status = 1 and user_org_id = ? and his_patient_id = ? and mtime >= ? and mtime <= ?"
  265. if deposit_status != 0 {
  266. s = s + " and deposit_status = " + string(deposit_status)
  267. }
  268. err = XTReadDB().Model(&models.DepositHistory{}).Where(s, orgid, id, stime, etime).Order("mtime desc").Find(&deposit).Error
  269. return
  270. }
  271. //获取患者押金列表
  272. func GetUserList(page, limit, orgid int64, keyword string, slicekey []int64) (m []models.Deposit1, total int64, err error) {
  273. db := XTReadDB().Model(&models.Deposit{}).Where("status = 1 and user_org_id = ? ", orgid)
  274. offset := (page - 1) * limit
  275. if len(keyword) > 0 {
  276. db = db.Where(" his_patient_id = ? ", keyword)
  277. }
  278. err = db.Count(&total).Offset(offset).Order("mtime desc").Find(&m).Error
  279. return
  280. }
  281. //扣费明细列表
  282. func DeductionList(orgid, stime, etime int64, keyword string, slicekey []int64) (deposithistory []models.DepositHistory, err error) {
  283. db := XTReadDB().Model(&models.DepositHistory{}).Where("status = 1 and user_org_id = ? and deposit_status = 2 ", orgid).Where("ctime >= ? and ctime <= ?", stime, etime)
  284. if len(keyword) > 0 {
  285. var tmp string = "deposit_code like ?"
  286. if len(slicekey) > 0 {
  287. for i := 0; i < len(slicekey); i++ {
  288. tmp = tmp + " or his_patient_id = " + strconv.FormatInt(slicekey[i], 10)
  289. }
  290. }
  291. keyword = "%" + keyword + "%"
  292. db = db.Where(tmp, keyword)
  293. }
  294. err = db.Order("ctime desc").Find(&deposithistory).Error
  295. return
  296. }
  297. //获取医疗费总额
  298. func MedicalTotal(orgid, patient_id int64, code string) decimal.Decimal {
  299. tmp := models.HisChargeSettleOrder{}
  300. 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)
  301. return decimal.NewFromFloat(tmp.MedfeeSumamt)
  302. }
  303. //根据就诊号查询出医收费类型
  304. func CodeToChargetype(orgid int64, code string) (s string) {
  305. tmp := models.HisPrintPatient{}
  306. XTReadDB().Model(&models.HisPrintPatient{}).Where("number = ? and user_org_id = ?", code, orgid).Find(&tmp)
  307. //tmp.BalanceAccountsType
  308. switch tmp.BalanceAccountsType {
  309. case 1:
  310. s = "医保"
  311. case 2:
  312. s = "自费"
  313. case 3:
  314. s = "公费"
  315. case 4:
  316. s = "农保"
  317. case 5:
  318. s = "会员"
  319. case 6:
  320. s = "职工"
  321. case 7:
  322. s = "合同"
  323. case 8:
  324. s = "医保自费"
  325. default:
  326. s = "类型未定义"
  327. }
  328. return
  329. }
  330. //扣除患者的押金,并生成一条扣费历史记录
  331. //orgid 机构id;
  332. //his_user_id 患者id;
  333. //create_id 创建者id;
  334. //code 就诊号;
  335. //deposit 本次使用的押金金额,支持float64、int、int64、decimal.Decimal类型
  336. func SpendDeposit(orgid, his_user_id, create_id int64, code string, deposit interface{}) (err error) {
  337. tmp_deposit := decimal.Decimal{} //本次患者使用的押金
  338. tmp_deposit, err = ToDecimal(deposit)
  339. if err != nil {
  340. return
  341. }
  342. //开事务
  343. tx := XTWriteDB().Begin()
  344. defer func() {
  345. if err != nil {
  346. utils.ErrorLog("事务失败,原因为: %v", err.Error())
  347. tx.Rollback()
  348. } else {
  349. tx.Commit()
  350. }
  351. }()
  352. //查询患者押金
  353. tmp := models.Deposit{}
  354. err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", his_user_id, orgid).Find(&tmp).Error
  355. if err != nil {
  356. err = fmt.Errorf("押金余额不足")
  357. return
  358. }
  359. //判断能否扣除
  360. if tmp.Deposit.Cmp(tmp_deposit) == -1 {
  361. err = fmt.Errorf("押金余额不足")
  362. return
  363. }
  364. //扣除患者押金
  365. err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", tmp.ID).Updates(map[string]interface{}{
  366. "mtime": time.Now().Unix(),
  367. "deposit": tmp.Deposit.Sub(tmp_deposit),
  368. }).Error
  369. if err != nil {
  370. return
  371. }
  372. //生成一条历史记录
  373. dehistory := models.DepositHistory{
  374. UserOrgId: orgid,
  375. HisPatientId: his_user_id,
  376. DepositCode: code,
  377. Deposit: tmp_deposit,
  378. SurplusDeposit: tmp.Deposit.Sub(tmp_deposit),
  379. DepositStatus: 2,
  380. Status: 1,
  381. CreateId: create_id,
  382. Ctime: time.Now().Unix(),
  383. Mtime: time.Now().Unix(),
  384. TrialStatus: 1,
  385. }
  386. err = tx.Create(&dehistory).Error
  387. return
  388. }
  389. //新增一条退款申请
  390. func RefundApplication(orgid, his_patient_id, trial_status, createid int64, code string, deposit decimal.Decimal) (err error) {
  391. //开事务
  392. tx := XTWriteDB().Begin()
  393. defer func() {
  394. if err != nil {
  395. utils.ErrorLog("事务失败,原因为: %v", err.Error())
  396. tx.Rollback()
  397. } else {
  398. tx.Commit()
  399. }
  400. }()
  401. //获取患者当前余额
  402. tmp := models.Deposit{}
  403. err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", his_patient_id, orgid).Find(&tmp).Error
  404. if err != nil {
  405. return
  406. }
  407. //判断是否可以退款
  408. if tmp.Deposit.Cmp(deposit) == -1 {
  409. err = fmt.Errorf("退款金额不得大于押金余额!")
  410. return
  411. }
  412. s_deposit := tmp.Deposit
  413. //判断是否审核通过,通过更新当前押金,否则只新增一条历史记录
  414. if trial_status == 1 {
  415. //更新患者当前押金余额
  416. err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", tmp.ID).Updates(map[string]interface{}{
  417. "mtime": time.Now().Unix(),
  418. "deposit": tmp.Deposit.Sub(deposit),
  419. }).Error
  420. if err != nil {
  421. return
  422. }
  423. s_deposit = tmp.Deposit.Sub(deposit)
  424. }
  425. //生成一条历史记录
  426. dehistory := models.DepositHistory{
  427. UserOrgId: orgid,
  428. HisPatientId: his_patient_id,
  429. DepositCode: code,
  430. Deposit: deposit,
  431. SurplusDeposit: s_deposit,
  432. DepositStatus: 3, //3退款,4退费
  433. Status: 1,
  434. CreateId: createid,
  435. Ctime: time.Now().Unix(),
  436. Mtime: time.Now().Unix(),
  437. TrialStatus: trial_status,
  438. }
  439. err = tx.Create(&dehistory).Error
  440. return
  441. }
  442. //退款审核通过/拒绝批量处理
  443. func RefundReviewMore(orgid, trial_status int64, ids string) (err error) {
  444. //处理下字符串
  445. t_ids := ""
  446. if ids[len(ids)-1] == 44 {
  447. t_ids = ids[:len(ids)-1]
  448. } else {
  449. t_ids = ids
  450. }
  451. tmp_id := strings.Split(t_ids, ",")
  452. //开事务
  453. tx := XTWriteDB().Begin()
  454. defer func() {
  455. if err != nil {
  456. utils.ErrorLog("事务失败,原因为: %v", err.Error())
  457. tx.Rollback()
  458. } else {
  459. tx.Commit()
  460. }
  461. }()
  462. //循环处理id
  463. for i := 0; i < len(tmp_id); i++ {
  464. var id int64
  465. id, err = strconv.ParseInt(tmp_id[i], 10, 64)
  466. if err != nil {
  467. return err
  468. }
  469. err = RefundReview(orgid, id, trial_status, tx)
  470. if err != nil {
  471. return err
  472. }
  473. }
  474. return err
  475. }
  476. //退款审核通过/拒绝
  477. func RefundReview(orgid, id, trial_status int64, tx *gorm.DB) (err error) {
  478. //根据id查询该条历史记录
  479. history := models.DepositHistory{}
  480. err = tx.Model(&models.DepositHistory{}).Where("id = ? and status = 1", id).Find(&history).Error
  481. if err != nil {
  482. return err
  483. }
  484. //判断状态是否为未审核
  485. if history.TrialStatus != 0 {
  486. err = fmt.Errorf("所选单据中包含了无需审核的单据")
  487. return err
  488. }
  489. //判断类型是否为3,3代表退款,4为退费
  490. if history.DepositStatus != 3 {
  491. err = fmt.Errorf("所选单据中包含了无需审核的单据")
  492. return err
  493. }
  494. up := make(map[string]interface{})
  495. //通过
  496. if trial_status == 1 {
  497. //拿患者的id和机构id在去查患者的余额
  498. tt := models.Deposit{}
  499. err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", history.HisPatientId, orgid).Find(&tt).Error
  500. if err != nil {
  501. return err
  502. }
  503. //判断是否可以修改患者押金
  504. if tt.Deposit.Cmp(history.Deposit) == -1 {
  505. err = fmt.Errorf("当前押金余额小于退款金额,无法完成退款")
  506. return err
  507. }
  508. //修改患者押金
  509. err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", tt.ID).Updates(map[string]interface{}{
  510. "mtime": time.Now().Unix(),
  511. "deposit": tt.Deposit.Sub(history.Deposit),
  512. }).Error
  513. if err != nil {
  514. return err
  515. }
  516. up = map[string]interface{}{
  517. "mtime": time.Now().Unix(),
  518. "surplus_deposit": tt.Deposit.Sub(history.Deposit),
  519. "trial_status": 1,
  520. }
  521. } else {
  522. up = map[string]interface{}{
  523. "mtime": time.Now().Unix(),
  524. "trial_status": 2,
  525. }
  526. }
  527. //修改一条历史记录
  528. err = tx.Model(&models.DepositHistory{}).Where("id = ? and status = 1", id).Updates(up).Error
  529. return err
  530. }
  531. //退款删除
  532. func DeleteRefund(orgid, id int64) (err error) {
  533. //根据id查询历史记录
  534. history := models.DepositHistory{}
  535. err = XTReadDB().Model(&models.DepositHistory{}).Where("id = ?", id).Find(&history).Error
  536. if err != nil {
  537. return
  538. }
  539. //判断是否可以删除
  540. if history.TrialStatus == 1 || history.DepositStatus != 3 {
  541. err = fmt.Errorf("当前状态不可删除!")
  542. return
  543. }
  544. //删除
  545. err = XTWriteDB().Model(&models.DepositHistory{}).Where("id = ?", id).Updates(map[string]interface{}{
  546. "mtime": time.Now().Unix(),
  547. "status": 0,
  548. }).Error
  549. return
  550. }
  551. //更改退款申请
  552. func ChangeRefund(orgid, his_patient_id, trial_status, id int64, code string, deposit decimal.Decimal) (err error) {
  553. //开事务
  554. tx := XTWriteDB().Begin()
  555. defer func() {
  556. if err != nil {
  557. utils.ErrorLog("事务失败,原因为: %v", err.Error())
  558. tx.Rollback()
  559. } else {
  560. tx.Commit()
  561. }
  562. }()
  563. //获取患者当前余额
  564. tmp := models.Deposit{}
  565. err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", his_patient_id, orgid).Find(&tmp).Error
  566. if err != nil {
  567. return
  568. }
  569. //判断是否可以退款
  570. if tmp.Deposit.Cmp(deposit) == -1 {
  571. err = fmt.Errorf("退款金额不得大于押金余额!")
  572. return
  573. }
  574. up := make(map[string]interface{})
  575. //判断是否审核通过,通过更新当前押金,否则只新增一条历史记录
  576. if trial_status == 1 {
  577. //更新患者当前押金余额
  578. err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", tmp.ID).Updates(map[string]interface{}{
  579. "mtime": time.Now().Unix(),
  580. "deposit": tmp.Deposit.Sub(deposit),
  581. }).Error
  582. if err != nil {
  583. return
  584. }
  585. up = map[string]interface{}{
  586. "mtime": time.Now().Unix(),
  587. "deposit_code": code,
  588. "his_patient_id": his_patient_id,
  589. "surplus_deposit": tmp.Deposit.Sub(deposit),
  590. "deposit": deposit,
  591. "trial_status": trial_status,
  592. }
  593. } else {
  594. up = map[string]interface{}{
  595. "mtime": time.Now().Unix(),
  596. "deposit_code": code,
  597. "his_patient_id": his_patient_id,
  598. "deposit": deposit,
  599. "trial_status": trial_status,
  600. }
  601. }
  602. //更新一条历史记录
  603. err = tx.Model(&models.DepositHistory{}).Where("id = ? and status = 1", id).Updates(up).Error
  604. return
  605. }
  606. //退款分页
  607. func RefundList(orgid, stime, etime, refundtype, examinetype int64, keyword string, slicekey []int64) (depo []models.RefundList, err error) {
  608. db := XTReadDB().Model(&models.DepositHistory{}).Where("status = 1 and user_org_id = ? ", orgid).Where("ctime >= ? and ctime <= ?", stime, etime)
  609. //退款类型
  610. switch refundtype {
  611. case 0:
  612. db = db.Where(" deposit_status in (3,4) ")
  613. case 3, 4:
  614. db = db.Where(" deposit_status = ? ", refundtype)
  615. default:
  616. err = fmt.Errorf("退款类型错误")
  617. return
  618. }
  619. //审核状态
  620. switch examinetype {
  621. case 0, 1, 2:
  622. db = db.Where(" trial_status = ? ", examinetype)
  623. case 3: //代表查询全部
  624. db = db
  625. default:
  626. err = fmt.Errorf("审核状态错误")
  627. return
  628. }
  629. if len(keyword) > 0 {
  630. tmp := "deposit_code like ? "
  631. if len(slicekey) > 0 {
  632. for i := 0; i < len(slicekey); i++ {
  633. tmp = tmp + " or his_patient_id = " + strconv.FormatInt(slicekey[i], 10)
  634. }
  635. }
  636. keyword = "%" + keyword + "%"
  637. db = db.Where(tmp, keyword)
  638. }
  639. err = db.Order("ctime desc").Find(&depo).Error
  640. return
  641. }
  642. //退回患者的押金,并生成一条退费历史记录
  643. //orgid 机构id
  644. //his_user_id 患者id
  645. //code 编号
  646. //deposit 本次退费的金额,支持float64、int、int64、decimal.Decimal类型
  647. func MoneyIncrease(orgid, his_user_id int64, code string, deposit interface{}) (err error) {
  648. tmp_deposit := decimal.Decimal{}
  649. tmp_deposit, err = ToDecimal(deposit)
  650. if err != nil {
  651. return
  652. }
  653. //开事务
  654. tx := XTWriteDB().Begin()
  655. defer func() {
  656. if err != nil {
  657. utils.ErrorLog("事务失败,原因为: %v", err.Error())
  658. tx.Rollback()
  659. } else {
  660. tx.Commit()
  661. }
  662. }()
  663. //获取当前患者押金
  664. tmp := models.Deposit{}
  665. err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", his_user_id, orgid).Find(&tmp).Error
  666. if err != nil {
  667. return
  668. }
  669. //退回患者押金
  670. err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", tmp.ID).Updates(map[string]interface{}{
  671. "mtime": time.Now().Unix(),
  672. "deposit": tmp.Deposit.Add(tmp_deposit),
  673. }).Error
  674. if err != nil {
  675. return
  676. }
  677. //生成一条退费记录
  678. dehistory := models.DepositHistory{
  679. UserOrgId: orgid,
  680. HisPatientId: his_user_id,
  681. DepositCode: code,
  682. Deposit: tmp_deposit,
  683. SurplusDeposit: tmp.Deposit.Add(tmp_deposit),
  684. DepositStatus: 4,
  685. Status: 1,
  686. CreateId: 0,
  687. Ctime: time.Now().Unix(),
  688. Mtime: time.Now().Unix(),
  689. TrialStatus: 1,
  690. }
  691. err = tx.Create(&dehistory).Error
  692. return
  693. }
  694. func GetorgName(orgid int64) (name string) {
  695. tmp := models.GetorgName{}
  696. XTReadDB().Model(&models.GetorgName{}).Where("id = ? ", orgid).Find(&tmp)
  697. return tmp.OrgName
  698. }
  699. //根据id查询就诊号
  700. func FindcodeToid(id int64) (code string) {
  701. tmp := models.GetMdtrtId{}
  702. XTReadDB().Model(&models.GetMdtrtId{}).Where("id = ?", id).Find(&tmp)
  703. return tmp.MdtrtId
  704. }
  705. //根据id查询就诊号
  706. func FindnumberToid(id int64) (number string) {
  707. tmp := models.GetMdtrtId{}
  708. XTReadDB().Model(&models.GetMdtrtId{}).Where("id = ?", id).Find(&tmp)
  709. return tmp.Number
  710. }
  711. //把其他的类型转换成decimal.Decimal类型
  712. func ToDecimal(i interface{}) (d decimal.Decimal, err error) {
  713. switch i.(type) {
  714. case float64:
  715. d = decimal.NewFromFloat(i.(float64))
  716. case decimal.Decimal:
  717. d = i.(decimal.Decimal)
  718. case int64:
  719. d = decimal.NewFromFloat(float64(i.(int64)))
  720. case int:
  721. d = decimal.NewFromFloat(float64(i.(int)))
  722. default:
  723. err = fmt.Errorf("类型解析错误")
  724. }
  725. return
  726. }