his_deposit_service.go 24KB

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