his_deposit_controller.go 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. package controllers
  2. import (
  3. "XT_New/enums"
  4. "XT_New/models"
  5. "XT_New/service"
  6. "XT_New/utils"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/astaxie/beego"
  10. "github.com/shopspring/decimal"
  11. "strings"
  12. "time"
  13. )
  14. type HisDepositApiController struct {
  15. BaseAuthAPIController
  16. }
  17. func HisDepositApiRegistRouters() {
  18. beego.Router("/api/his/ttt", &HisDepositApiController{}, "get:TTT") //测试接口
  19. beego.Router("/api/his/gethisuser", &HisDepositApiController{}, "get:GetHisUser") //获取病例中心,有效患者名称
  20. beego.Router("/api/his/adddeposit", &HisDepositApiController{}, "post:AddDeposit") //新增押金
  21. beego.Router("/api/his/getdepositcode", &HisDepositApiController{}, "get:GetDepositCode") //获取新增押金编号
  22. beego.Router("/api/his/getdeletecode", &HisDepositApiController{}, "get:GetDeleteCode") //获取退款编号
  23. beego.Router("/api/his/rechargedetails", &HisDepositApiController{}, "get:RechargeDetails") //充值明细列表
  24. beego.Router("/api/his/updeposit", &HisDepositApiController{}, "get:UpDeposit") //审核
  25. beego.Router("/api/his/deletehistory", &HisDepositApiController{}, "get:DeleteHistory") //删除
  26. beego.Router("/api/his/rechargesummary", &HisDepositApiController{}, "get:RechargeSummary") //充值汇总列表
  27. beego.Router("/api/his/getuserlist", &HisDepositApiController{}, "get:GetUserList") //获取患者押金列表
  28. beego.Router("/api/his/depositflow", &HisDepositApiController{}, "get:DepositFlow") //根据患者id获取押金流水
  29. }
  30. func (this *HisDepositApiController) TTT() {
  31. id, _ := this.GetInt64("id")
  32. err := service.DelDecimalHistory(id)
  33. this.ServeSuccessJSON(map[string]interface{}{
  34. "list": err,
  35. })
  36. return
  37. }
  38. //获取病例中心,有效患者名称
  39. func (this *HisDepositApiController) GetHisUser() {
  40. orgid := this.GetAdminUserInfo().CurrentOrgId
  41. list, err := service.GetHisUser(orgid)
  42. if err != nil {
  43. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  44. return
  45. }
  46. this.ServeSuccessJSON(map[string]interface{}{
  47. "list": list,
  48. })
  49. return
  50. }
  51. //新增押金
  52. func (this *HisDepositApiController) AddDeposit() {
  53. orgid := this.GetAdminUserInfo().CurrentOrgId
  54. dataBody := make(map[string]interface{}, 0)
  55. err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  56. if err != nil {
  57. utils.ErrorLog(err.Error())
  58. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  59. return
  60. }
  61. switch {
  62. case dataBody["code"] == nil:
  63. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "押金编号不能为空")
  64. return
  65. case dataBody["his_patient_id"] == nil:
  66. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "患者名称不能为空")
  67. return
  68. case dataBody["deposit"] == nil:
  69. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "押金金额不能为空")
  70. return
  71. case dataBody["trial_status"] == nil:
  72. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "审核状态不能为空")
  73. return
  74. }
  75. code := dataBody["code"].(string) //押金编号
  76. his_patient_id := int64(dataBody["his_patient_id"].(float64)) //患者编号
  77. deposit := decimal.NewFromFloat(dataBody["deposit"].(float64)) //押金金额
  78. var remarks string
  79. if dataBody["remarks"] == nil {
  80. remarks = ""
  81. } else {
  82. remarks = dataBody["remarks"].(string) //备注
  83. }
  84. trial_status := int64(dataBody["trial_status"].(float64)) //审核状态
  85. createid := this.GetAdminUserInfo().AdminUser.Id
  86. pp := this.GetAdminUserInfo()
  87. fmt.Println(pp)
  88. err = service.UpDeposit(code, remarks, his_patient_id, orgid, trial_status, createid, deposit)
  89. if err != nil {
  90. utils.ErrorLog(err.Error())
  91. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  92. return
  93. }
  94. this.ServeSuccessJSON(map[string]interface{}{
  95. "list": "添加成功",
  96. })
  97. return
  98. }
  99. //获取新增押金编号
  100. func (this *HisDepositApiController) GetDepositCode() {
  101. orgid := this.GetAdminUserInfo().CurrentOrgId
  102. var code string
  103. for a := true; a == true; {
  104. code = service.CreateCPCode("CP")
  105. tmp := service.FindDecimalCode(orgid, code)
  106. if tmp == false {
  107. a = false
  108. }
  109. }
  110. this.ServeSuccessJSON(map[string]interface{}{
  111. "list": code,
  112. })
  113. return
  114. }
  115. //获取退款编号
  116. func (this *HisDepositApiController) GetDeleteCode() {
  117. orgid := this.GetAdminUserInfo().CurrentOrgId
  118. var code string
  119. for a := true; a == true; {
  120. code = service.CreateCPCode("AF")
  121. tmp := service.FindDecimalCode(orgid, code)
  122. if tmp == false {
  123. a = false
  124. }
  125. }
  126. this.ServeSuccessJSON(map[string]interface{}{
  127. "list": code,
  128. })
  129. return
  130. }
  131. //充值明细列表
  132. func (this *HisDepositApiController) RechargeDetails() {
  133. orgid := this.GetAdminUserInfo().CurrentOrgId
  134. timeLayout := "2006-01-02"
  135. loc, _ := time.LoadLocation("Local")
  136. keyword := this.GetString("keyword")
  137. start_time := this.GetString("start_time")
  138. end_time := this.GetString("end_time")
  139. var stime int64 //开始时间
  140. var etime int64 //结束时间
  141. namemap := make(map[int64]string)
  142. slicekey := make([]int64, 0)
  143. if len(keyword) > 0 {
  144. list, _ := service.GetHisUser(orgid)
  145. for _, v := range list {
  146. namemap[v.ID] = v.Name
  147. }
  148. for k, v := range namemap {
  149. res := strings.Contains(v, keyword)
  150. if res == true {
  151. slicekey = append(slicekey, k)
  152. }
  153. }
  154. }
  155. if start_time == "" && end_time == "" {
  156. stime, etime = service.GetMondayOfWeek()
  157. } else {
  158. stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  159. etmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  160. stime = stmp.Unix()
  161. etime = etmp.Unix()
  162. }
  163. list, err := service.DetailsList(orgid, stime, etime, keyword, slicekey)
  164. if err != nil {
  165. utils.ErrorLog(err.Error())
  166. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  167. return
  168. }
  169. var sum decimal.Decimal
  170. for i := 0; i < len(list); i++ {
  171. if list[i].TrialStatus == 1 {
  172. sum = sum.Add(list[i].Deposit)
  173. }
  174. list[i].Name = service.GetCreateidName(list[i].CreateId)
  175. }
  176. this.ServeSuccessJSON(map[string]interface{}{
  177. "list": list,
  178. "sum": sum,
  179. })
  180. return
  181. }
  182. //充值汇总列表
  183. func (this *HisDepositApiController) RechargeSummary() {
  184. orgid := this.GetAdminUserInfo().CurrentOrgId
  185. timeLayout := "2006-01-02"
  186. loc, _ := time.LoadLocation("Local")
  187. keyword := this.GetString("keyword")
  188. start_time := this.GetString("start_time")
  189. end_time := this.GetString("end_time")
  190. var stime int64 //开始时间
  191. var etime int64 //结束时间
  192. namemap := make(map[int64]string)
  193. slicekey := make([]int64, 0)
  194. lists, _ := service.GetHisUser(orgid)
  195. for _, v := range lists {
  196. namemap[v.ID] = v.Name
  197. }
  198. if len(keyword) > 0 {
  199. for k, v := range namemap {
  200. res := strings.Contains(v, keyword)
  201. if res == true {
  202. slicekey = append(slicekey, k)
  203. }
  204. }
  205. }
  206. if start_time == "" && end_time == "" {
  207. stime, etime = service.GetMondayOfWeek()
  208. } else {
  209. stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  210. etmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  211. stime = stmp.Unix()
  212. etime = etmp.Unix()
  213. }
  214. list, err := service.SummaryList(orgid, stime, etime, keyword, slicekey)
  215. if err != nil {
  216. utils.ErrorLog(err.Error())
  217. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  218. return
  219. }
  220. //list为详细数据,把list整理合并为maplist输出
  221. maplist := make(map[int64]models.Summary)
  222. Finlist := []models.Summary{}
  223. tmpslice := make([]int64, 0)
  224. var sum decimal.Decimal
  225. for i := 0; i < len(list); i++ {
  226. if list[i].TrialStatus == 1 {
  227. if k, ok := maplist[list[i].HisPatientId]; ok {
  228. k.SumDecimal = k.SumDecimal.Add(list[i].Deposit)
  229. maplist[list[i].HisPatientId] = models.Summary{
  230. namemap[list[i].HisPatientId],
  231. k.SumDecimal,
  232. service.GetUserMoney(list[i].HisPatientId, orgid),
  233. }
  234. } else {
  235. maplist[list[i].HisPatientId] = models.Summary{
  236. namemap[list[i].HisPatientId],
  237. list[i].Deposit,
  238. service.GetUserMoney(list[i].HisPatientId, orgid),
  239. }
  240. tmpslice = append(tmpslice, list[i].HisPatientId)
  241. }
  242. sum = sum.Add(list[i].Deposit)
  243. }
  244. }
  245. //maplist虽然满足接口要求,但格式不行,整理为Finlist输出
  246. for i := 0; i < len(tmpslice); i++ {
  247. Finlist = append(Finlist, maplist[tmpslice[i]])
  248. }
  249. this.ServeSuccessJSON(map[string]interface{}{
  250. "list": Finlist,
  251. "sum": sum,
  252. })
  253. return
  254. }
  255. //审核
  256. func (this *HisDepositApiController) UpDeposit() {
  257. id, _ := this.GetInt64("id", 0)
  258. if id == 0 {
  259. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "id不能为空")
  260. return
  261. }
  262. tmp, err := service.GetDecimalHistoryOne(id)
  263. if err != nil {
  264. utils.ErrorLog("无法查询该记录信息", err.Error())
  265. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "无法查询该记录信息")
  266. return
  267. }
  268. if tmp.TrialStatus == 0 {
  269. err = service.UpDecimalHistory(id)
  270. if err != nil {
  271. utils.ErrorLog("更新押金历史表出错,原因为:", err.Error())
  272. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "系统错误")
  273. return
  274. }
  275. } else {
  276. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "当前状态不可审核")
  277. return
  278. }
  279. this.ServeSuccessJSON(map[string]interface{}{
  280. "list": "审核通过",
  281. })
  282. return
  283. }
  284. func (this *HisDepositApiController) DeleteHistory() {
  285. id, _ := this.GetInt64("id", 0)
  286. if id == 0 {
  287. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "id不能为空")
  288. return
  289. }
  290. tmp, err := service.GetDecimalHistoryOne(id)
  291. if err != nil {
  292. utils.ErrorLog("无法查询该记录信息", err.Error())
  293. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "无法查询该记录信息")
  294. return
  295. }
  296. if tmp.TrialStatus == 0 {
  297. err = service.DelDecimalHistory(id)
  298. if err != nil {
  299. utils.ErrorLog("删除历史记录出错,原因为:", err.Error())
  300. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "系统错误")
  301. return
  302. }
  303. } else {
  304. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "已审核的单据不能进行删除操作")
  305. return
  306. }
  307. this.ServeSuccessJSON(map[string]interface{}{
  308. "list": "删除成功",
  309. })
  310. return
  311. }
  312. //根据id获取押金流水
  313. func (this *HisDepositApiController) DepositFlow() {
  314. orgid := this.GetAdminUserInfo().CurrentOrgId
  315. check := map[string][]string{
  316. "id": {"must", "int", "id"},
  317. "deposit_status": {"must", "int", "deposit_status"},
  318. }
  319. _, err := checks(this, &check)
  320. if err != nil {
  321. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  322. return
  323. }
  324. id, _ := this.GetInt64("id") //患者id
  325. //获取当前患者的姓名
  326. tmp, _ := service.GetHisUserName(orgid, id)
  327. name := tmp.Name
  328. deposit_status, _ := this.GetInt64("deposit_status", 0) //押金类型
  329. if deposit_status > 3 {
  330. utils.ErrorLog("押金类型错误,deposit_status:", deposit_status)
  331. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "押金类型错误")
  332. return
  333. }
  334. start_time := this.GetString("start_time", "") //开始时间
  335. end_time := this.GetString("end_time", "") //结束时间
  336. timeLayout := "2006-01-02"
  337. loc, _ := time.LoadLocation("Local")
  338. var stime int64 //开始时间
  339. var etime int64 //结束时间
  340. if start_time == "" && end_time == "" {
  341. stime, etime = service.GetMonth()
  342. } else {
  343. stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  344. etmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  345. stime = stmp.Unix()
  346. etime = etmp.Unix()
  347. }
  348. //获取该角色当前时间段的余额
  349. decimal := service.GetMoneyforTime(id, orgid, etime)
  350. //获取列表
  351. deposirhistory, errs := service.GetFlowList(id, orgid, stime, etime, deposit_status)
  352. if errs != nil {
  353. utils.ErrorLog("获取列表失败,原因为:", errs.Error())
  354. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, errs.Error())
  355. return
  356. }
  357. this.ServeSuccessJSON(map[string]interface{}{
  358. "list": deposirhistory,
  359. "name": name,
  360. "decimal": decimal,
  361. })
  362. return
  363. }
  364. //
  365. func (this *HisDepositApiController) GetUserList() {
  366. orgid := this.GetAdminUserInfo().CurrentOrgId
  367. keyword := this.GetString("keyword")
  368. page, _ := this.GetInt64("page") //页码
  369. limit, _ := this.GetInt64("limit") //每一页查出来的条数
  370. check := map[string][]string{
  371. "page": {"must", "string", "page"},
  372. "limit": {"must", "string", "limit"},
  373. }
  374. _, err := checks(this, &check)
  375. if err != nil {
  376. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  377. return
  378. }
  379. namemap := make(map[int64]string)
  380. slicekey := make([]int64, 0)
  381. lists, _ := service.GetHisUser(orgid)
  382. for _, v := range lists {
  383. namemap[v.ID] = v.Name
  384. }
  385. if len(keyword) > 0 {
  386. for k, v := range namemap {
  387. res := strings.Contains(v, keyword)
  388. if res == true {
  389. slicekey = append(slicekey, k)
  390. }
  391. }
  392. }
  393. list, total, errs := service.GetUserList(page, limit, orgid, keyword, slicekey)
  394. if errs != nil {
  395. utils.ErrorLog(errs.Error())
  396. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  397. return
  398. }
  399. for i := 0; i < len(list); i++ {
  400. list[i].HisPatientName = namemap[list[i].HisPatientId]
  401. }
  402. this.ServeSuccessJSON(map[string]interface{}{
  403. "list": list,
  404. "total": total,
  405. })
  406. return
  407. }
  408. //判断前端参数是否为空
  409. func checks(this *HisDepositApiController, m *map[string][]string) (map[string]string, error) {
  410. tmp := make(map[string]string)
  411. for k, v := range *m {
  412. t := this.GetString(k)
  413. if v[0] == "must" && t == "" {
  414. return nil, fmt.Errorf(v[2] + "不能为空")
  415. }
  416. tmp[k] = t
  417. }
  418. return tmp, nil
  419. }