his_deposit_service.go 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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 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 GetMondayOfWeek() (int64, int64) {
  171. t := time.Now()
  172. dayObj := GetZeroTime(t)
  173. var dayStr string
  174. loc, _ := time.LoadLocation("Local")
  175. if t.Weekday() == time.Monday {
  176. dayStr = dayObj.Format("2006-01-02")
  177. } else {
  178. offset := int(time.Monday - t.Weekday())
  179. if offset > 0 {
  180. offset = -6
  181. }
  182. dayStr = dayObj.AddDate(0, 0, offset).Format("2006-01-02")
  183. }
  184. dayStr = dayStr + " 00:00:00"
  185. stime, _ := time.ParseInLocation("2006-01-02 15:04:05", dayStr, loc)
  186. return stime.Unix(), stime.Unix() + 604799
  187. }
  188. //获取本月的起止时间戳
  189. func GetMonth() (int64, int64) {
  190. timeNow := time.Now()
  191. timeToday := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, timeNow.Location()) // 获取当天0点时间 time类型
  192. timeMonthStartUnix1 := timeToday.AddDate(0, 0, -timeToday.Day()+1).Unix() // 获取本月第一天0点 时间戳类型
  193. timeMonthEndUnix1 := timeToday.AddDate(0, 1, -timeToday.Day()+1).Unix() - 1 // 获取下个月第一天/ 本月最后一天24点 时间戳类型
  194. return timeMonthStartUnix1, timeMonthEndUnix1
  195. }
  196. //获取当天起止时间戳
  197. func GetNowTime() (int64, int64) {
  198. t := time.Now()
  199. addTime := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  200. timesamp := addTime.Unix()
  201. return timesamp, timesamp + 86399
  202. }
  203. //获取创建者的姓名
  204. //原先的(sgj_users.sgj_user_admin)
  205. //新加的(sgj_users.sgj_user_admin_role)
  206. func GetCreateidName(id, orgid int64) string {
  207. //var tmp models.CreateUser
  208. //XTReadDB().Select("name").Where("id = ?", id).Find(&tmp)
  209. //return tmp.Name
  210. var tmp models.CreateUserNames
  211. XTReadDB().Select("user_name").Where("admin_user_id = ? and org_id = ?", id, orgid).Order("ctime desc").Find(&tmp)
  212. return tmp.UserName
  213. }
  214. //审核通过
  215. func UpDecimalHistory(id int64) (err error) {
  216. //开事务
  217. tx := XTWriteDB().Begin()
  218. defer func() {
  219. if err != nil {
  220. utils.ErrorLog("事务失败,原因为: %v", err)
  221. tx.Rollback()
  222. } else {
  223. tx.Commit()
  224. }
  225. }()
  226. //查记录x2
  227. var history models.DepositHistory
  228. var detmp models.Deposit
  229. err = tx.Model(&models.DepositHistory{}).Where("id = ?", id).Find(&history).Error
  230. if err != nil {
  231. return
  232. }
  233. err = tx.Model(&models.Deposit{}).Where("user_org_id = ? and his_patient_id = ? and status = 1 ", history.UserOrgId, history.HisPatientId).Find(&detmp).Error
  234. if err != nil {
  235. return
  236. }
  237. //改状态,=====剩余押金
  238. err = tx.Model(models.DepositHistory{}).Where("id = ?", id).Updates(map[string]interface{}{
  239. "trial_status": 1,
  240. "surplus_deposit": detmp.Deposit.Add(history.Deposit),
  241. "mtime": time.Now().Unix(),
  242. }).Error
  243. if err != nil {
  244. return
  245. }
  246. //相加
  247. err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", detmp.ID).Updates(map[string]interface{}{
  248. "mtime": time.Now().Unix(),
  249. "deposit": detmp.Deposit.Add(history.Deposit),
  250. }).Error
  251. return
  252. }
  253. //删除本次记录
  254. func DelDecimalHistory(id int64) (err error) {
  255. err = XTWriteDB().Model(models.DepositHistory{}).Where("id = ?", id).Updates(map[string]interface{}{
  256. "status": 0,
  257. "mtime": time.Now().Unix(),
  258. }).Error
  259. return
  260. }
  261. //根据id获取一条押金历史记录
  262. func GetDecimalHistoryOne(id int64) (history models.DepositHistory, err error) {
  263. err = XTReadDB().Model(&models.DepositHistory{}).Where("id = ?", id).Find(&history).Error
  264. return
  265. }
  266. //根据患者id获取该患者当前剩余的押金
  267. func GetUserMoney(id, orgid int64) decimal.Decimal {
  268. tmp := models.Deposit{}
  269. err := XTReadDB().Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&tmp).Error
  270. if err != nil {
  271. return decimal.NewFromFloat(0)
  272. }
  273. return tmp.Deposit
  274. }
  275. //查询时间段内患者的余额
  276. func GetMoneyforTime(id, orgid, etime int64) decimal.Decimal {
  277. tmp := models.DepositHistory{}
  278. XTReadDB().Model(&models.DepositHistory{}).Where("his_patient_id = ? and user_org_id = ? and status = 1 and mtime <= ?", id, orgid, etime).Order("mtime").Find(&tmp)
  279. return tmp.SurplusDeposit
  280. }
  281. //押金流水
  282. func GetFlowList(page, limit, id, orgid, stime, etime, deposit_status int64) (deposit []models.DepositHistory, total int64, err error) {
  283. s := "status = 1 and trial_status = 1 and user_org_id = ? and his_patient_id = ? and mtime >= ? and mtime <= ?"
  284. offset := (page - 1) * limit
  285. if deposit_status != 0 {
  286. s = s + " and deposit_status = " + strconv.FormatInt(deposit_status, 10)
  287. }
  288. err = XTReadDB().Model(&models.DepositHistory{}).Where(s, orgid, id, stime, etime).Count(&total).Offset(offset).Order("mtime desc").Limit(limit).Find(&deposit).Error
  289. return
  290. }
  291. //获取患者押金列表
  292. func GetUserList(page, limit, orgid int64, keyword string, slicekey []int64) (m []models.Deposit1, total int64, err error) {
  293. db := XTReadDB().Model(&models.Deposit{}).Where("status = 1 and user_org_id = ? ", orgid)
  294. offset := (page - 1) * limit
  295. if len(keyword) > 0 {
  296. db = db.Where(" his_patient_id = ? ", keyword)
  297. }
  298. err = db.Count(&total).Offset(offset).Order("mtime desc").Limit(limit).Find(&m).Error
  299. return
  300. }
  301. //扣费明细列表
  302. func DeductionList(orgid, stime, etime int64, keyword string, slicekey []int64) (deposithistory []models.DepositHistory, err error) {
  303. db := XTReadDB().Model(&models.DepositHistory{}).Where("status = 1 and user_org_id = ? and deposit_status = 2 ", orgid).Where("ctime >= ? and ctime <= ?", stime, etime)
  304. if len(keyword) > 0 {
  305. var tmp string = "deposit_code like ?"
  306. if len(slicekey) > 0 {
  307. for i := 0; i < len(slicekey); i++ {
  308. tmp = tmp + " or his_patient_id = " + strconv.FormatInt(slicekey[i], 10)
  309. }
  310. }
  311. keyword = "%" + keyword + "%"
  312. db = db.Where(tmp, keyword)
  313. }
  314. err = db.Order("ctime desc").Find(&deposithistory).Error
  315. return
  316. }
  317. //获取医疗费总额
  318. func MedicalTotal(orgid, id int64) decimal.Decimal {
  319. tmp := models.HisChargeSettleOrder{}
  320. //and status = 1 and order_status = 2
  321. XTReadDB().Model(&models.HisChargeSettleOrder{}).Where("id = ? and user_org_id = ? ", id, orgid).Find(&tmp)
  322. return decimal.NewFromFloat(tmp.MedfeeSumamt)
  323. }
  324. //根据就诊号查询出医收费类型
  325. func CodeToChargetype(orgid int64, code string) (s string) {
  326. tmp := models.HisPrintPatient{}
  327. tmp_hhcr := models.HisHospitalCheckRecordTwo{}
  328. XTReadDB().Model(&models.HisPrintPatient{}).Where("number = ? and user_org_id = ?", code, orgid).Find(&tmp)
  329. //tmp.BalanceAccountsType
  330. if tmp.BalanceAccountsType == 0 {
  331. XTReadDB().Model(&models.HisHospitalCheckRecordTwo{}).Where("number = ? and user_org_id = ?", code, orgid).Find(&tmp_hhcr)
  332. tmp.BalanceAccountsType = tmp_hhcr.BalanceAccountsType
  333. }
  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. //orgid 机构id;
  358. //his_user_id 患者id;
  359. //create_id 创建者id;
  360. //code 就诊号;
  361. //deposit 本次使用的押金金额,支持float64、int、int64、decimal.Decimal类型
  362. func SpendDeposit(orgid, his_user_id, create_id int64, code string, deposit interface{}) (err error) {
  363. tmp_deposit := decimal.Decimal{} //本次患者使用的押金
  364. tmp_deposit, err = ToDecimal(deposit)
  365. if err != nil {
  366. return
  367. }
  368. //开事务
  369. tx := XTWriteDB().Begin()
  370. defer func() {
  371. if err != nil {
  372. utils.ErrorLog("事务失败,原因为: %v", err.Error())
  373. tx.Rollback()
  374. } else {
  375. tx.Commit()
  376. }
  377. }()
  378. //查询患者押金
  379. tmp := models.Deposit{}
  380. err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", his_user_id, orgid).Find(&tmp).Error
  381. if err != nil {
  382. err = fmt.Errorf("押金余额不足")
  383. return
  384. }
  385. //判断能否扣除
  386. if tmp.Deposit.Cmp(tmp_deposit) == -1 {
  387. err = fmt.Errorf("押金余额不足")
  388. return
  389. }
  390. //扣除患者押金
  391. err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", tmp.ID).Updates(map[string]interface{}{
  392. "mtime": time.Now().Unix(),
  393. "deposit": tmp.Deposit.Sub(tmp_deposit),
  394. }).Error
  395. if err != nil {
  396. return
  397. }
  398. //生成一条历史记录
  399. dehistory := models.DepositHistory{
  400. UserOrgId: orgid,
  401. HisPatientId: his_user_id,
  402. DepositCode: code,
  403. Deposit: tmp_deposit,
  404. SurplusDeposit: tmp.Deposit.Sub(tmp_deposit),
  405. DepositStatus: 2,
  406. Status: 1,
  407. CreateId: create_id,
  408. Ctime: time.Now().Unix(),
  409. Mtime: time.Now().Unix(),
  410. TrialStatus: 1,
  411. }
  412. err = tx.Create(&dehistory).Error
  413. return
  414. }
  415. //新增一条退款申请
  416. func RefundApplication(orgid, his_patient_id, trial_status, createid int64, code string, deposit decimal.Decimal) (err error) {
  417. //开事务
  418. tx := XTWriteDB().Begin()
  419. defer func() {
  420. if err != nil {
  421. utils.ErrorLog("事务失败,原因为: %v", err.Error())
  422. tx.Rollback()
  423. } else {
  424. tx.Commit()
  425. }
  426. }()
  427. //获取患者当前余额
  428. tmp := models.Deposit{}
  429. err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", his_patient_id, orgid).Find(&tmp).Error
  430. if err != nil {
  431. if err.Error() == "record not found" {
  432. err = fmt.Errorf("没有可退押金,保存失败")
  433. }
  434. return
  435. }
  436. //判断是否可以退款
  437. if tmp.Deposit.Cmp(deposit) == -1 {
  438. err = fmt.Errorf("退款金额不得大于押金余额!")
  439. return
  440. }
  441. s_deposit := tmp.Deposit
  442. //判断是否审核通过,通过更新当前押金,否则只新增一条历史记录
  443. if trial_status == 1 {
  444. //更新患者当前押金余额
  445. err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", tmp.ID).Updates(map[string]interface{}{
  446. "mtime": time.Now().Unix(),
  447. "deposit": tmp.Deposit.Sub(deposit),
  448. }).Error
  449. if err != nil {
  450. return
  451. }
  452. s_deposit = tmp.Deposit.Sub(deposit)
  453. }
  454. //生成一条历史记录
  455. dehistory := models.DepositHistory{
  456. UserOrgId: orgid,
  457. HisPatientId: his_patient_id,
  458. DepositCode: code,
  459. Deposit: deposit,
  460. SurplusDeposit: s_deposit,
  461. DepositStatus: 3, //3退款,4退费
  462. Status: 1,
  463. CreateId: createid,
  464. Ctime: time.Now().Unix(),
  465. Mtime: time.Now().Unix(),
  466. TrialStatus: trial_status,
  467. }
  468. err = tx.Create(&dehistory).Error
  469. return
  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. func RefundReview(orgid, id, trial_status int64, tx *gorm.DB) (err error) {
  507. //根据id查询该条历史记录
  508. history := models.DepositHistory{}
  509. err = tx.Model(&models.DepositHistory{}).Where("id = ? and status = 1", id).Find(&history).Error
  510. if err != nil {
  511. return err
  512. }
  513. //判断状态是否为未审核
  514. if history.TrialStatus != 0 {
  515. err = fmt.Errorf("所选单据中包含了无需审核的单据")
  516. return err
  517. }
  518. //判断类型是否为3,3代表退款,4为退费
  519. if history.DepositStatus != 3 {
  520. err = fmt.Errorf("所选单据中包含了无需审核的单据")
  521. return err
  522. }
  523. up := make(map[string]interface{})
  524. //通过
  525. if trial_status == 1 {
  526. //拿患者的id和机构id在去查患者的余额
  527. tt := models.Deposit{}
  528. err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", history.HisPatientId, orgid).Find(&tt).Error
  529. if err != nil {
  530. return err
  531. }
  532. //判断是否可以修改患者押金
  533. if tt.Deposit.Cmp(history.Deposit) == -1 {
  534. err = fmt.Errorf("当前押金余额小于退款金额,无法完成退款")
  535. return err
  536. }
  537. //修改患者押金
  538. err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", tt.ID).Updates(map[string]interface{}{
  539. "mtime": time.Now().Unix(),
  540. "deposit": tt.Deposit.Sub(history.Deposit),
  541. }).Error
  542. if err != nil {
  543. return err
  544. }
  545. up = map[string]interface{}{
  546. "mtime": time.Now().Unix(),
  547. "surplus_deposit": tt.Deposit.Sub(history.Deposit),
  548. "trial_status": 1,
  549. }
  550. } else {
  551. up = map[string]interface{}{
  552. "mtime": time.Now().Unix(),
  553. "trial_status": 2,
  554. }
  555. }
  556. //修改一条历史记录
  557. err = tx.Model(&models.DepositHistory{}).Where("id = ? and status = 1", id).Updates(up).Error
  558. return err
  559. }
  560. //退款删除
  561. func DeleteRefund(orgid, id int64) (err error) {
  562. //根据id查询历史记录
  563. history := models.DepositHistory{}
  564. err = XTReadDB().Model(&models.DepositHistory{}).Where("id = ?", id).Find(&history).Error
  565. if err != nil {
  566. return
  567. }
  568. //判断是否可以删除
  569. if history.TrialStatus == 1 || history.DepositStatus != 3 {
  570. err = fmt.Errorf("当前状态不可删除!")
  571. return
  572. }
  573. //删除
  574. err = XTWriteDB().Model(&models.DepositHistory{}).Where("id = ?", id).Updates(map[string]interface{}{
  575. "mtime": time.Now().Unix(),
  576. "status": 0,
  577. }).Error
  578. return
  579. }
  580. //更改退款申请
  581. func ChangeRefund(orgid, his_patient_id, trial_status, id int64, code string, deposit decimal.Decimal) (err error) {
  582. //开事务
  583. tx := XTWriteDB().Begin()
  584. defer func() {
  585. if err != nil {
  586. utils.ErrorLog("事务失败,原因为: %v", err.Error())
  587. tx.Rollback()
  588. } else {
  589. tx.Commit()
  590. }
  591. }()
  592. //获取患者当前余额
  593. tmp := models.Deposit{}
  594. err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", his_patient_id, orgid).Find(&tmp).Error
  595. if err != nil {
  596. return
  597. }
  598. //判断是否可以退款
  599. if tmp.Deposit.Cmp(deposit) == -1 {
  600. err = fmt.Errorf("退款金额不得大于押金余额!")
  601. return
  602. }
  603. up := make(map[string]interface{})
  604. //判断是否审核通过,通过更新当前押金,否则只新增一条历史记录
  605. if trial_status == 1 {
  606. //更新患者当前押金余额
  607. err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", tmp.ID).Updates(map[string]interface{}{
  608. "mtime": time.Now().Unix(),
  609. "deposit": tmp.Deposit.Sub(deposit),
  610. }).Error
  611. if err != nil {
  612. return
  613. }
  614. up = map[string]interface{}{
  615. "mtime": time.Now().Unix(),
  616. "deposit_code": code,
  617. "his_patient_id": his_patient_id,
  618. "surplus_deposit": tmp.Deposit.Sub(deposit),
  619. "deposit": deposit,
  620. "trial_status": trial_status,
  621. }
  622. } else {
  623. up = map[string]interface{}{
  624. "mtime": time.Now().Unix(),
  625. "deposit_code": code,
  626. "his_patient_id": his_patient_id,
  627. "deposit": deposit,
  628. "trial_status": trial_status,
  629. }
  630. }
  631. //更新一条历史记录
  632. err = tx.Model(&models.DepositHistory{}).Where("id = ? and status = 1", id).Updates(up).Error
  633. return
  634. }
  635. //退款分页
  636. func RefundList(page, limit, orgid, stime, etime, refundtype, examinetype int64, keyword string, slicekey []int64) (depo []models.RefundList, total int64, err error) {
  637. db := XTReadDB().Model(&models.DepositHistory{}).Where("status = 1 and user_org_id = ? ", orgid).Where("ctime >= ? and ctime <= ?", stime, etime)
  638. offset := (page - 1) * limit
  639. //退款类型
  640. switch refundtype {
  641. case 0:
  642. db = db.Where(" deposit_status in (3,4) ")
  643. case 3, 4:
  644. db = db.Where(" deposit_status = ? ", refundtype)
  645. default:
  646. err = fmt.Errorf("退款类型错误")
  647. return
  648. }
  649. //审核状态
  650. switch examinetype {
  651. case 0, 1, 2:
  652. db = db.Where(" trial_status = ? ", examinetype)
  653. case 3: //代表查询全部
  654. db = db
  655. default:
  656. err = fmt.Errorf("审核状态错误")
  657. return
  658. }
  659. if len(keyword) > 0 {
  660. tmp := "deposit_code like ? "
  661. if len(slicekey) > 0 {
  662. for i := 0; i < len(slicekey); i++ {
  663. tmp = tmp + " or his_patient_id = " + strconv.FormatInt(slicekey[i], 10)
  664. }
  665. }
  666. keyword = "%" + keyword + "%"
  667. db = db.Where(tmp, keyword)
  668. }
  669. err = db.Count(&total).Offset(offset).Order("ctime desc").Limit(limit).Find(&depo).Error
  670. return
  671. }
  672. //退回患者的押金,并生成一条退费历史记录
  673. //orgid 机构id
  674. //his_user_id 患者id
  675. //code 编号
  676. //deposit 本次退费的金额,支持float64、int、int64、decimal.Decimal类型
  677. func MoneyIncrease(orgid, his_user_id int64, code string, deposit interface{}) (err error) {
  678. tmp_deposit := decimal.Decimal{}
  679. tmp_deposit, err = ToDecimal(deposit)
  680. if err != nil {
  681. return
  682. }
  683. //开事务
  684. tx := XTWriteDB().Begin()
  685. defer func() {
  686. if err != nil {
  687. utils.ErrorLog("事务失败,原因为: %v", err.Error())
  688. tx.Rollback()
  689. } else {
  690. tx.Commit()
  691. }
  692. }()
  693. //获取当前患者押金
  694. tmp := models.Deposit{}
  695. err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", his_user_id, orgid).Find(&tmp).Error
  696. if err != nil {
  697. return
  698. }
  699. //退回患者押金
  700. err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", tmp.ID).Updates(map[string]interface{}{
  701. "mtime": time.Now().Unix(),
  702. "deposit": tmp.Deposit.Add(tmp_deposit),
  703. }).Error
  704. if err != nil {
  705. return
  706. }
  707. //生成一条退费记录
  708. dehistory := models.DepositHistory{
  709. UserOrgId: orgid,
  710. HisPatientId: his_user_id,
  711. DepositCode: code,
  712. Deposit: tmp_deposit,
  713. SurplusDeposit: tmp.Deposit.Add(tmp_deposit),
  714. DepositStatus: 4,
  715. Status: 1,
  716. CreateId: 0,
  717. Ctime: time.Now().Unix(),
  718. Mtime: time.Now().Unix(),
  719. TrialStatus: 1,
  720. }
  721. err = tx.Create(&dehistory).Error
  722. return
  723. }
  724. func GetorgName(orgid int64) (name string) {
  725. tmp := models.GetorgName{}
  726. XTReadDB().Model(&models.GetorgName{}).Where("id = ? ", orgid).Find(&tmp)
  727. return tmp.OrgName
  728. }
  729. //根据id查询就诊号
  730. func FindcodeToid(id int64) (code string) {
  731. tmp := models.GetMdtrtId{}
  732. XTReadDB().Model(&models.GetMdtrtId{}).Where("id = ?", id).Find(&tmp)
  733. return tmp.MdtrtId
  734. }
  735. //根据id查询就诊号
  736. func FindnumberToid(id int64) (number string) {
  737. tmp := models.GetMdtrtId{}
  738. XTReadDB().Model(&models.GetMdtrtId{}).Where("id = ?", id).Find(&tmp)
  739. return tmp.Number
  740. }
  741. //扣费明细列表,查看详情按钮是否显示,0隐藏,1显示
  742. func IsButtonShow(code string, orgid, his_patient_id int64) (tmp int64) {
  743. var total int
  744. 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)
  745. if total == 0 {
  746. return 1
  747. } else {
  748. //预防万一,多加一个验证
  749. if total%2 == 0 {
  750. return 1
  751. } else {
  752. return 0
  753. }
  754. }
  755. }
  756. //把其他的类型转换成decimal.Decimal类型
  757. func ToDecimal(i interface{}) (d decimal.Decimal, err error) {
  758. switch i.(type) {
  759. case float64:
  760. d = decimal.NewFromFloat(i.(float64))
  761. case decimal.Decimal:
  762. d = i.(decimal.Decimal)
  763. case int64:
  764. d = decimal.NewFromFloat(float64(i.(int64)))
  765. case int:
  766. d = decimal.NewFromFloat(float64(i.(int)))
  767. default:
  768. err = fmt.Errorf("类型解析错误")
  769. }
  770. return
  771. }
  772. func TypeConversion(tmp float64) (s string) {
  773. switch tmp {
  774. case 1:
  775. s = "g"
  776. case 2:
  777. s = "mg"
  778. case 3:
  779. s = "u"
  780. case 4:
  781. s = "ml"
  782. case 5:
  783. s = "万U"
  784. case 6:
  785. s = "枚"
  786. case 7:
  787. s = "粒"
  788. case 8:
  789. s = "片"
  790. case 9:
  791. s = "支"
  792. case 10:
  793. s = "μg"
  794. case 11:
  795. s = "iu"
  796. case 12:
  797. s = "包"
  798. case 13:
  799. s = "袋"
  800. case 14:
  801. s = "万"
  802. case 15:
  803. s = "万iu"
  804. case 16:
  805. s = "丸"
  806. case 17:
  807. s = "盒"
  808. case 18:
  809. s = "瓶"
  810. case 19:
  811. s = "瓶(袋)"
  812. case 20:
  813. s = "次"
  814. }
  815. return s
  816. }
  817. func TypeConversion02(tmp string) (s string) {
  818. switch tmp {
  819. case "1":
  820. s = "g"
  821. case "2":
  822. s = "mg"
  823. case "3":
  824. s = "u"
  825. case "4":
  826. s = "ml"
  827. case "5":
  828. s = "万U"
  829. case "6":
  830. s = "枚"
  831. case "7":
  832. s = "粒"
  833. case "8":
  834. s = "片"
  835. case "9":
  836. s = "支"
  837. case "10":
  838. s = "μg"
  839. case "11":
  840. s = "iu"
  841. case "12":
  842. s = "包"
  843. case "13":
  844. s = "袋"
  845. case "14":
  846. s = "万"
  847. case "15":
  848. s = "万iu"
  849. case "16":
  850. s = "丸"
  851. case "17":
  852. s = "盒"
  853. case "18":
  854. s = "瓶"
  855. case "19":
  856. s = "瓶(袋)"
  857. case "20":
  858. s = "次"
  859. default:
  860. s = tmp
  861. }
  862. return s
  863. }
  864. //获取本段时间内患者退款的总额
  865. //orgid:机构id,stime:开始时间,etime:结束时间,patient_id:患者id
  866. func RefundListSum(orgid, stime, etime, patient_id int64) (sum decimal.Decimal, err error) {
  867. var depo []models.RefundList
  868. err = XTReadDB().Model(&models.DepositHistory{}).Where("status = 1 and user_org_id = ? and trial_status = 1 and ctime >= ? and ctime <= ? and deposit_status = 3 and his_patient_id = ? ", orgid, stime, etime, patient_id).Find(&depo).Error
  869. if err != nil {
  870. return decimal.NewFromFloat(0), err
  871. }
  872. for i := 0; i < len(depo); i++ {
  873. sum = sum.Add(depo[i].Deposit)
  874. }
  875. return
  876. }