package controllers import ( "XT_New/enums" "XT_New/models" "XT_New/service" "XT_New/utils" "encoding/json" "fmt" "github.com/astaxie/beego" "github.com/shopspring/decimal" "strings" "time" ) type HisDepositApiController struct { BaseAuthAPIController } func HisDepositApiRegistRouters() { beego.Router("/api/his/ttt", &HisDepositApiController{}, "get:TTT") //测试接口 beego.Router("/api/his/gethisuser", &HisDepositApiController{}, "get:GetHisUser") //获取病例中心,有效患者名称 beego.Router("/api/his/adddeposit", &HisDepositApiController{}, "post:AddDeposit") //新增押金 beego.Router("/api/his/getdepositcode", &HisDepositApiController{}, "get:GetDepositCode") //获取新增押金编号 beego.Router("/api/his/getdeletecode", &HisDepositApiController{}, "get:GetDeleteCode") //获取退款编号 beego.Router("/api/his/rechargedetails", &HisDepositApiController{}, "get:RechargeDetails") //充值明细列表 beego.Router("/api/his/updeposit", &HisDepositApiController{}, "get:UpDeposit") //审核 beego.Router("/api/his/deletehistory", &HisDepositApiController{}, "get:DeleteHistory") //删除 beego.Router("/api/his/rechargesummary", &HisDepositApiController{}, "get:RechargeSummary") //充值汇总列表 beego.Router("/api/his/getuserlist", &HisDepositApiController{}, "get:GetUserList") //获取患者押金列表 beego.Router("/api/his/depositflow", &HisDepositApiController{}, "get:DepositFlow") //根据患者id获取押金流水 } func (this *HisDepositApiController) TTT() { id, _ := this.GetInt64("id") err := service.DelDecimalHistory(id) this.ServeSuccessJSON(map[string]interface{}{ "list": err, }) return } //获取病例中心,有效患者名称 func (this *HisDepositApiController) GetHisUser() { orgid := this.GetAdminUserInfo().CurrentOrgId list, err := service.GetHisUser(orgid) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } this.ServeSuccessJSON(map[string]interface{}{ "list": list, }) return } //新增押金 func (this *HisDepositApiController) AddDeposit() { orgid := this.GetAdminUserInfo().CurrentOrgId dataBody := make(map[string]interface{}, 0) err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody) if err != nil { utils.ErrorLog(err.Error()) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } switch { case dataBody["code"] == nil: this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "押金编号不能为空") return case dataBody["his_patient_id"] == nil: this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "患者名称不能为空") return case dataBody["deposit"] == nil: this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "押金金额不能为空") return case dataBody["trial_status"] == nil: this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "审核状态不能为空") return } code := dataBody["code"].(string) //押金编号 his_patient_id := int64(dataBody["his_patient_id"].(float64)) //患者编号 deposit := decimal.NewFromFloat(dataBody["deposit"].(float64)) //押金金额 var remarks string if dataBody["remarks"] == nil { remarks = "" } else { remarks = dataBody["remarks"].(string) //备注 } trial_status := int64(dataBody["trial_status"].(float64)) //审核状态 createid := this.GetAdminUserInfo().AdminUser.Id pp := this.GetAdminUserInfo() fmt.Println(pp) err = service.UpDeposit(code, remarks, his_patient_id, orgid, trial_status, createid, deposit) if err != nil { utils.ErrorLog(err.Error()) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } this.ServeSuccessJSON(map[string]interface{}{ "list": "添加成功", }) return } //获取新增押金编号 func (this *HisDepositApiController) GetDepositCode() { orgid := this.GetAdminUserInfo().CurrentOrgId var code string for a := true; a == true; { code = service.CreateCPCode("CP") tmp := service.FindDecimalCode(orgid, code) if tmp == false { a = false } } this.ServeSuccessJSON(map[string]interface{}{ "list": code, }) return } //获取退款编号 func (this *HisDepositApiController) GetDeleteCode() { orgid := this.GetAdminUserInfo().CurrentOrgId var code string for a := true; a == true; { code = service.CreateCPCode("AF") tmp := service.FindDecimalCode(orgid, code) if tmp == false { a = false } } this.ServeSuccessJSON(map[string]interface{}{ "list": code, }) return } //充值明细列表 func (this *HisDepositApiController) RechargeDetails() { orgid := this.GetAdminUserInfo().CurrentOrgId timeLayout := "2006-01-02" loc, _ := time.LoadLocation("Local") keyword := this.GetString("keyword") start_time := this.GetString("start_time") end_time := this.GetString("end_time") var stime int64 //开始时间 var etime int64 //结束时间 namemap := make(map[int64]string) slicekey := make([]int64, 0) if len(keyword) > 0 { list, _ := service.GetHisUser(orgid) for _, v := range list { namemap[v.ID] = v.Name } for k, v := range namemap { res := strings.Contains(v, keyword) if res == true { slicekey = append(slicekey, k) } } } if start_time == "" && end_time == "" { stime, etime = service.GetMondayOfWeek() } else { stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc) etmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc) stime = stmp.Unix() etime = etmp.Unix() } list, err := service.DetailsList(orgid, stime, etime, keyword, slicekey) if err != nil { utils.ErrorLog(err.Error()) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } var sum decimal.Decimal for i := 0; i < len(list); i++ { if list[i].TrialStatus == 1 { sum = sum.Add(list[i].Deposit) } list[i].Name = service.GetCreateidName(list[i].CreateId) } this.ServeSuccessJSON(map[string]interface{}{ "list": list, "sum": sum, }) return } //充值汇总列表 func (this *HisDepositApiController) RechargeSummary() { orgid := this.GetAdminUserInfo().CurrentOrgId timeLayout := "2006-01-02" loc, _ := time.LoadLocation("Local") keyword := this.GetString("keyword") start_time := this.GetString("start_time") end_time := this.GetString("end_time") var stime int64 //开始时间 var etime int64 //结束时间 namemap := make(map[int64]string) slicekey := make([]int64, 0) lists, _ := service.GetHisUser(orgid) for _, v := range lists { namemap[v.ID] = v.Name } if len(keyword) > 0 { for k, v := range namemap { res := strings.Contains(v, keyword) if res == true { slicekey = append(slicekey, k) } } } if start_time == "" && end_time == "" { stime, etime = service.GetMondayOfWeek() } else { stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc) etmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc) stime = stmp.Unix() etime = etmp.Unix() } list, err := service.SummaryList(orgid, stime, etime, keyword, slicekey) if err != nil { utils.ErrorLog(err.Error()) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } //list为详细数据,把list整理合并为maplist输出 maplist := make(map[int64]models.Summary) Finlist := []models.Summary{} tmpslice := make([]int64, 0) var sum decimal.Decimal for i := 0; i < len(list); i++ { if list[i].TrialStatus == 1 { if k, ok := maplist[list[i].HisPatientId]; ok { k.SumDecimal = k.SumDecimal.Add(list[i].Deposit) maplist[list[i].HisPatientId] = models.Summary{ namemap[list[i].HisPatientId], k.SumDecimal, service.GetUserMoney(list[i].HisPatientId, orgid), } } else { maplist[list[i].HisPatientId] = models.Summary{ namemap[list[i].HisPatientId], list[i].Deposit, service.GetUserMoney(list[i].HisPatientId, orgid), } tmpslice = append(tmpslice, list[i].HisPatientId) } sum = sum.Add(list[i].Deposit) } } //maplist虽然满足接口要求,但格式不行,整理为Finlist输出 for i := 0; i < len(tmpslice); i++ { Finlist = append(Finlist, maplist[tmpslice[i]]) } this.ServeSuccessJSON(map[string]interface{}{ "list": Finlist, "sum": sum, }) return } //审核 func (this *HisDepositApiController) UpDeposit() { id, _ := this.GetInt64("id", 0) if id == 0 { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "id不能为空") return } tmp, err := service.GetDecimalHistoryOne(id) if err != nil { utils.ErrorLog("无法查询该记录信息", err.Error()) this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "无法查询该记录信息") return } if tmp.TrialStatus == 0 { err = service.UpDecimalHistory(id) if err != nil { utils.ErrorLog("更新押金历史表出错,原因为:", err.Error()) this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "系统错误") return } } else { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "当前状态不可审核") return } this.ServeSuccessJSON(map[string]interface{}{ "list": "审核通过", }) return } func (this *HisDepositApiController) DeleteHistory() { id, _ := this.GetInt64("id", 0) if id == 0 { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "id不能为空") return } tmp, err := service.GetDecimalHistoryOne(id) if err != nil { utils.ErrorLog("无法查询该记录信息", err.Error()) this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "无法查询该记录信息") return } if tmp.TrialStatus == 0 { err = service.DelDecimalHistory(id) if err != nil { utils.ErrorLog("删除历史记录出错,原因为:", err.Error()) this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "系统错误") return } } else { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "已审核的单据不能进行删除操作") return } this.ServeSuccessJSON(map[string]interface{}{ "list": "删除成功", }) return } //根据id获取押金流水 func (this *HisDepositApiController) DepositFlow() { orgid := this.GetAdminUserInfo().CurrentOrgId check := map[string][]string{ "id": {"must", "int", "id"}, "deposit_status": {"must", "int", "deposit_status"}, } _, err := checks(this, &check) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } id, _ := this.GetInt64("id") //患者id //获取当前患者的姓名 tmp, _ := service.GetHisUserName(orgid, id) name := tmp.Name deposit_status, _ := this.GetInt64("deposit_status", 0) //押金类型 if deposit_status > 3 { utils.ErrorLog("押金类型错误,deposit_status:", deposit_status) this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "押金类型错误") return } start_time := this.GetString("start_time", "") //开始时间 end_time := this.GetString("end_time", "") //结束时间 timeLayout := "2006-01-02" loc, _ := time.LoadLocation("Local") var stime int64 //开始时间 var etime int64 //结束时间 if start_time == "" && end_time == "" { stime, etime = service.GetMonth() } else { stmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc) etmp, _ := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc) stime = stmp.Unix() etime = etmp.Unix() } //获取该角色当前时间段的余额 decimal := service.GetMoneyforTime(id, orgid, etime) //获取列表 deposirhistory, errs := service.GetFlowList(id, orgid, stime, etime, deposit_status) if errs != nil { utils.ErrorLog("获取列表失败,原因为:", errs.Error()) this.ServeFailJsonSend(enums.ErrorCodeParamWrong, errs.Error()) return } this.ServeSuccessJSON(map[string]interface{}{ "list": deposirhistory, "name": name, "decimal": decimal, }) return } // func (this *HisDepositApiController) GetUserList() { orgid := this.GetAdminUserInfo().CurrentOrgId keyword := this.GetString("keyword") page, _ := this.GetInt64("page") //页码 limit, _ := this.GetInt64("limit") //每一页查出来的条数 check := map[string][]string{ "page": {"must", "string", "page"}, "limit": {"must", "string", "limit"}, } _, err := checks(this, &check) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } namemap := make(map[int64]string) slicekey := make([]int64, 0) lists, _ := service.GetHisUser(orgid) for _, v := range lists { namemap[v.ID] = v.Name } if len(keyword) > 0 { for k, v := range namemap { res := strings.Contains(v, keyword) if res == true { slicekey = append(slicekey, k) } } } list, total, errs := service.GetUserList(page, limit, orgid, keyword, slicekey) if errs != nil { utils.ErrorLog(errs.Error()) this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error()) return } for i := 0; i < len(list); i++ { list[i].HisPatientName = namemap[list[i].HisPatientId] } this.ServeSuccessJSON(map[string]interface{}{ "list": list, "total": total, }) return } //判断前端参数是否为空 func checks(this *HisDepositApiController, m *map[string][]string) (map[string]string, error) { tmp := make(map[string]string) for k, v := range *m { t := this.GetString(k) if v[0] == "must" && t == "" { return nil, fmt.Errorf(v[2] + "不能为空") } tmp[k] = t } return tmp, nil }