his_deposit_service.go 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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 = readDb.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 = readDb.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 = readDb.Create(&dehistory).Error
  413. return
  414. }
  415. func SpendDepositTwo(orgid, his_user_id, create_id int64, code string, deposit interface{}, ctime int64) (err error) {
  416. tmp_deposit := decimal.Decimal{} //本次患者使用的押金
  417. tmp_deposit, err = ToDecimal(deposit)
  418. if err != nil {
  419. return
  420. }
  421. ////开事务
  422. //tx := XTWriteDB().Begin()
  423. //defer func() {
  424. // if err != nil {
  425. // utils.ErrorLog("事务失败,原因为: %v", err.Error())
  426. // tx.Rollback()
  427. // } else {
  428. // tx.Commit()
  429. // }
  430. //}()
  431. //查询患者押金
  432. tmp := models.Deposit{}
  433. err = readDb.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", his_user_id, orgid).Find(&tmp).Error
  434. if err != nil {
  435. err = fmt.Errorf("押金余额不足")
  436. return
  437. }
  438. //判断能否扣除
  439. if tmp.Deposit.Cmp(tmp_deposit) == -1 {
  440. err = fmt.Errorf("押金余额不足")
  441. return
  442. }
  443. //扣除患者押金
  444. err = readDb.Model(&models.Deposit{}).Where("id = ? and status = 1", tmp.ID).Updates(map[string]interface{}{
  445. "mtime": time.Now().Unix(),
  446. "deposit": tmp.Deposit.Sub(tmp_deposit),
  447. }).Error
  448. if err != nil {
  449. return
  450. }
  451. //生成一条历史记录
  452. dehistory := models.DepositHistory{
  453. UserOrgId: orgid,
  454. HisPatientId: his_user_id,
  455. DepositCode: code,
  456. Deposit: tmp_deposit,
  457. SurplusDeposit: tmp.Deposit.Sub(tmp_deposit),
  458. DepositStatus: 2,
  459. Status: 1,
  460. CreateId: create_id,
  461. Ctime: ctime,
  462. Mtime: time.Now().Unix(),
  463. TrialStatus: 1,
  464. }
  465. err = readDb.Create(&dehistory).Error
  466. return
  467. }
  468. // 新增一条退款申请
  469. func RefundApplication(orgid, his_patient_id, trial_status, createid int64, code string, deposit decimal.Decimal) (err error) {
  470. //开事务
  471. tx := XTWriteDB().Begin()
  472. defer func() {
  473. if err != nil {
  474. utils.ErrorLog("事务失败,原因为: %v", err.Error())
  475. tx.Rollback()
  476. } else {
  477. tx.Commit()
  478. }
  479. }()
  480. //获取患者当前余额
  481. tmp := models.Deposit{}
  482. err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", his_patient_id, orgid).Find(&tmp).Error
  483. if err != nil {
  484. if err.Error() == "record not found" {
  485. err = fmt.Errorf("没有可退押金,保存失败")
  486. }
  487. return
  488. }
  489. //判断是否可以退款
  490. if tmp.Deposit.Cmp(deposit) == -1 {
  491. err = fmt.Errorf("退款金额不得大于押金余额!")
  492. return
  493. }
  494. s_deposit := tmp.Deposit
  495. //判断是否审核通过,通过更新当前押金,否则只新增一条历史记录
  496. if trial_status == 1 {
  497. //更新患者当前押金余额
  498. err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", tmp.ID).Updates(map[string]interface{}{
  499. "mtime": time.Now().Unix(),
  500. "deposit": tmp.Deposit.Sub(deposit),
  501. }).Error
  502. if err != nil {
  503. return
  504. }
  505. s_deposit = tmp.Deposit.Sub(deposit)
  506. }
  507. //生成一条历史记录
  508. dehistory := models.DepositHistory{
  509. UserOrgId: orgid,
  510. HisPatientId: his_patient_id,
  511. DepositCode: code,
  512. Deposit: deposit,
  513. SurplusDeposit: s_deposit,
  514. DepositStatus: 3, //3退款,4退费
  515. Status: 1,
  516. CreateId: createid,
  517. Ctime: time.Now().Unix(),
  518. Mtime: time.Now().Unix(),
  519. TrialStatus: trial_status,
  520. }
  521. err = tx.Create(&dehistory).Error
  522. return
  523. }
  524. // 退款审核通过/拒绝批量处理
  525. func RefundReviewMore(orgid, trial_status int64, ids string) (err error) {
  526. //处理下字符串
  527. t_ids := ""
  528. if ids[len(ids)-1] == 44 {
  529. t_ids = ids[:len(ids)-1]
  530. } else {
  531. t_ids = ids
  532. }
  533. tmp_id := strings.Split(t_ids, ",")
  534. //开事务
  535. tx := XTWriteDB().Begin()
  536. defer func() {
  537. if err != nil {
  538. utils.ErrorLog("事务失败,原因为: %v", err.Error())
  539. tx.Rollback()
  540. } else {
  541. tx.Commit()
  542. }
  543. }()
  544. //循环处理id
  545. for i := 0; i < len(tmp_id); i++ {
  546. var id int64
  547. id, err = strconv.ParseInt(tmp_id[i], 10, 64)
  548. if err != nil {
  549. return err
  550. }
  551. err = RefundReview(orgid, id, trial_status, tx)
  552. if err != nil {
  553. return err
  554. }
  555. }
  556. return err
  557. }
  558. // 退款审核通过/拒绝
  559. func RefundReview(orgid, id, trial_status int64, tx *gorm.DB) (err error) {
  560. //根据id查询该条历史记录
  561. history := models.DepositHistory{}
  562. err = tx.Model(&models.DepositHistory{}).Where("id = ? and status = 1", id).Find(&history).Error
  563. if err != nil {
  564. return err
  565. }
  566. //判断状态是否为未审核
  567. if history.TrialStatus != 0 {
  568. err = fmt.Errorf("所选单据中包含了无需审核的单据")
  569. return err
  570. }
  571. //判断类型是否为3,3代表退款,4为退费
  572. if history.DepositStatus != 3 {
  573. err = fmt.Errorf("所选单据中包含了无需审核的单据")
  574. return err
  575. }
  576. up := make(map[string]interface{})
  577. //通过
  578. if trial_status == 1 {
  579. //拿患者的id和机构id在去查患者的余额
  580. tt := models.Deposit{}
  581. err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", history.HisPatientId, orgid).Find(&tt).Error
  582. if err != nil {
  583. return err
  584. }
  585. //判断是否可以修改患者押金
  586. if tt.Deposit.Cmp(history.Deposit) == -1 {
  587. err = fmt.Errorf("当前押金余额小于退款金额,无法完成退款")
  588. return err
  589. }
  590. //修改患者押金
  591. err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", tt.ID).Updates(map[string]interface{}{
  592. "mtime": time.Now().Unix(),
  593. "deposit": tt.Deposit.Sub(history.Deposit),
  594. }).Error
  595. if err != nil {
  596. return err
  597. }
  598. up = map[string]interface{}{
  599. "mtime": time.Now().Unix(),
  600. "surplus_deposit": tt.Deposit.Sub(history.Deposit),
  601. "trial_status": 1,
  602. }
  603. } else {
  604. up = map[string]interface{}{
  605. "mtime": time.Now().Unix(),
  606. "trial_status": 2,
  607. }
  608. }
  609. //修改一条历史记录
  610. err = tx.Model(&models.DepositHistory{}).Where("id = ? and status = 1", id).Updates(up).Error
  611. return err
  612. }
  613. // 退款删除
  614. func DeleteRefund(orgid, id int64) (err error) {
  615. //根据id查询历史记录
  616. history := models.DepositHistory{}
  617. err = XTReadDB().Model(&models.DepositHistory{}).Where("id = ?", id).Find(&history).Error
  618. if err != nil {
  619. return
  620. }
  621. //判断是否可以删除
  622. if history.TrialStatus == 1 || history.DepositStatus != 3 {
  623. err = fmt.Errorf("当前状态不可删除!")
  624. return
  625. }
  626. //删除
  627. err = XTWriteDB().Model(&models.DepositHistory{}).Where("id = ?", id).Updates(map[string]interface{}{
  628. "mtime": time.Now().Unix(),
  629. "status": 0,
  630. }).Error
  631. return
  632. }
  633. // 更改退款申请
  634. func ChangeRefund(orgid, his_patient_id, trial_status, id int64, code string, deposit decimal.Decimal) (err error) {
  635. //开事务
  636. tx := XTWriteDB().Begin()
  637. defer func() {
  638. if err != nil {
  639. utils.ErrorLog("事务失败,原因为: %v", err.Error())
  640. tx.Rollback()
  641. } else {
  642. tx.Commit()
  643. }
  644. }()
  645. //获取患者当前余额
  646. tmp := models.Deposit{}
  647. err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", his_patient_id, orgid).Find(&tmp).Error
  648. if err != nil {
  649. return
  650. }
  651. //判断是否可以退款
  652. if tmp.Deposit.Cmp(deposit) == -1 {
  653. err = fmt.Errorf("退款金额不得大于押金余额!")
  654. return
  655. }
  656. up := make(map[string]interface{})
  657. //判断是否审核通过,通过更新当前押金,否则只新增一条历史记录
  658. if trial_status == 1 {
  659. //更新患者当前押金余额
  660. err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", tmp.ID).Updates(map[string]interface{}{
  661. "mtime": time.Now().Unix(),
  662. "deposit": tmp.Deposit.Sub(deposit),
  663. }).Error
  664. if err != nil {
  665. return
  666. }
  667. up = map[string]interface{}{
  668. "mtime": time.Now().Unix(),
  669. "deposit_code": code,
  670. "his_patient_id": his_patient_id,
  671. "surplus_deposit": tmp.Deposit.Sub(deposit),
  672. "deposit": deposit,
  673. "trial_status": trial_status,
  674. }
  675. } else {
  676. up = map[string]interface{}{
  677. "mtime": time.Now().Unix(),
  678. "deposit_code": code,
  679. "his_patient_id": his_patient_id,
  680. "deposit": deposit,
  681. "trial_status": trial_status,
  682. }
  683. }
  684. //更新一条历史记录
  685. err = tx.Model(&models.DepositHistory{}).Where("id = ? and status = 1", id).Updates(up).Error
  686. return
  687. }
  688. // 退款分页
  689. func RefundList(page, limit, orgid, stime, etime, refundtype, examinetype int64, keyword string, slicekey []int64) (depo []models.RefundList, total int64, err error) {
  690. db := XTReadDB().Model(&models.DepositHistory{}).Where("status = 1 and user_org_id = ? ", orgid).Where("ctime >= ? and ctime <= ?", stime, etime)
  691. offset := (page - 1) * limit
  692. //退款类型
  693. switch refundtype {
  694. case 0:
  695. db = db.Where(" deposit_status in (3,4) ")
  696. case 3, 4:
  697. db = db.Where(" deposit_status = ? ", refundtype)
  698. default:
  699. err = fmt.Errorf("退款类型错误")
  700. return
  701. }
  702. //审核状态
  703. switch examinetype {
  704. case 0, 1, 2:
  705. db = db.Where(" trial_status = ? ", examinetype)
  706. case 3: //代表查询全部
  707. db = db
  708. default:
  709. err = fmt.Errorf("审核状态错误")
  710. return
  711. }
  712. if len(keyword) > 0 {
  713. tmp := "deposit_code like ? "
  714. if len(slicekey) > 0 {
  715. for i := 0; i < len(slicekey); i++ {
  716. tmp = tmp + " or his_patient_id = " + strconv.FormatInt(slicekey[i], 10)
  717. }
  718. }
  719. keyword = "%" + keyword + "%"
  720. db = db.Where(tmp, keyword)
  721. }
  722. err = db.Count(&total).Offset(offset).Order("ctime desc").Limit(limit).Find(&depo).Error
  723. return
  724. }
  725. // 退回患者的押金,并生成一条退费历史记录
  726. // orgid 机构id
  727. // his_user_id 患者id
  728. // code 编号
  729. // deposit 本次退费的金额,支持float64、int、int64、decimal.Decimal类型
  730. func MoneyIncrease(orgid, his_user_id int64, code string, deposit interface{}) (err error) {
  731. tmp_deposit := decimal.Decimal{}
  732. tmp_deposit, err = ToDecimal(deposit)
  733. if err != nil {
  734. return
  735. }
  736. //开事务
  737. tx := XTWriteDB().Begin()
  738. defer func() {
  739. if err != nil {
  740. utils.ErrorLog("事务失败,原因为: %v", err.Error())
  741. tx.Rollback()
  742. } else {
  743. tx.Commit()
  744. }
  745. }()
  746. //获取当前患者押金
  747. tmp := models.Deposit{}
  748. err = tx.Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", his_user_id, orgid).Find(&tmp).Error
  749. if err != nil {
  750. return
  751. }
  752. //退回患者押金
  753. err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", tmp.ID).Updates(map[string]interface{}{
  754. "mtime": time.Now().Unix(),
  755. "deposit": tmp.Deposit.Add(tmp_deposit),
  756. }).Error
  757. if err != nil {
  758. return
  759. }
  760. //生成一条退费记录
  761. dehistory := models.DepositHistory{
  762. UserOrgId: orgid,
  763. HisPatientId: his_user_id,
  764. DepositCode: code,
  765. Deposit: tmp_deposit,
  766. SurplusDeposit: tmp.Deposit.Add(tmp_deposit),
  767. DepositStatus: 4,
  768. Status: 1,
  769. CreateId: 0,
  770. Ctime: time.Now().Unix(),
  771. Mtime: time.Now().Unix(),
  772. TrialStatus: 1,
  773. }
  774. err = tx.Create(&dehistory).Error
  775. return
  776. }
  777. func GetorgName(orgid int64) (name string) {
  778. tmp := models.GetorgName{}
  779. XTReadDB().Model(&models.GetorgName{}).Where("id = ? ", orgid).Find(&tmp)
  780. return tmp.OrgName
  781. }
  782. // 根据id查询就诊号
  783. func FindcodeToid(id int64) (code string) {
  784. tmp := models.GetMdtrtId{}
  785. XTReadDB().Model(&models.GetMdtrtId{}).Where("id = ?", id).Find(&tmp)
  786. return tmp.MdtrtId
  787. }
  788. // 根据id查询就诊号
  789. func FindnumberToid(id int64) (number string) {
  790. tmp := models.GetMdtrtId{}
  791. XTReadDB().Model(&models.GetMdtrtId{}).Where("id = ?", id).Find(&tmp)
  792. return tmp.Number
  793. }
  794. // 扣费明细列表,查看详情按钮是否显示,0隐藏,1显示
  795. func IsButtonShow(code string, orgid, his_patient_id int64) (tmp int64) {
  796. var total int
  797. 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)
  798. if total == 0 {
  799. return 1
  800. } else {
  801. //预防万一,多加一个验证
  802. if total%2 == 0 {
  803. return 1
  804. } else {
  805. return 0
  806. }
  807. }
  808. }
  809. // 把其他的类型转换成decimal.Decimal类型
  810. func ToDecimal(i interface{}) (d decimal.Decimal, err error) {
  811. switch i.(type) {
  812. case float64:
  813. d = decimal.NewFromFloat(i.(float64))
  814. case decimal.Decimal:
  815. d = i.(decimal.Decimal)
  816. case int64:
  817. d = decimal.NewFromFloat(float64(i.(int64)))
  818. case int:
  819. d = decimal.NewFromFloat(float64(i.(int)))
  820. default:
  821. err = fmt.Errorf("类型解析错误")
  822. }
  823. return
  824. }
  825. func TypeConversion(tmp float64) (s string) {
  826. switch tmp {
  827. case 1:
  828. s = "g"
  829. case 2:
  830. s = "mg"
  831. case 3:
  832. s = "u"
  833. case 4:
  834. s = "ml"
  835. case 5:
  836. s = "万U"
  837. case 6:
  838. s = "枚"
  839. case 7:
  840. s = "粒"
  841. case 8:
  842. s = "片"
  843. case 9:
  844. s = "支"
  845. case 10:
  846. s = "μg"
  847. case 11:
  848. s = "iu"
  849. case 12:
  850. s = "包"
  851. case 13:
  852. s = "袋"
  853. case 14:
  854. s = "万"
  855. case 15:
  856. s = "万iu"
  857. case 16:
  858. s = "丸"
  859. case 17:
  860. s = "盒"
  861. case 18:
  862. s = "瓶"
  863. case 19:
  864. s = "瓶(袋)"
  865. case 20:
  866. s = "次"
  867. }
  868. return s
  869. }
  870. func TypeConversion02(tmp string) (s string) {
  871. switch tmp {
  872. case "1":
  873. s = "g"
  874. case "2":
  875. s = "mg"
  876. case "3":
  877. s = "u"
  878. case "4":
  879. s = "ml"
  880. case "5":
  881. s = "万U"
  882. case "6":
  883. s = "枚"
  884. case "7":
  885. s = "粒"
  886. case "8":
  887. s = "片"
  888. case "9":
  889. s = "支"
  890. case "10":
  891. s = "μg"
  892. case "11":
  893. s = "iu"
  894. case "12":
  895. s = "包"
  896. case "13":
  897. s = "袋"
  898. case "14":
  899. s = "万"
  900. case "15":
  901. s = "万iu"
  902. case "16":
  903. s = "丸"
  904. case "17":
  905. s = "盒"
  906. case "18":
  907. s = "瓶"
  908. case "19":
  909. s = "瓶(袋)"
  910. case "20":
  911. s = "次"
  912. default:
  913. s = tmp
  914. }
  915. return s
  916. }
  917. // 获取本段时间内患者退款的总额
  918. // orgid:机构id,stime:开始时间,etime:结束时间,patient_id:患者id
  919. func RefundListSum(orgid, stime, etime, patient_id int64) (sum decimal.Decimal, err error) {
  920. var depo []models.RefundList
  921. 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
  922. if err != nil {
  923. return decimal.NewFromFloat(0), err
  924. }
  925. for i := 0; i < len(depo); i++ {
  926. sum = sum.Add(depo[i].Deposit)
  927. }
  928. return
  929. }