智盟项目
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

svc_withdrawal.go 16 KiB

1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. package platform
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. offical "applet/app/db/official"
  6. "applet/app/e"
  7. "applet/app/utils"
  8. "applet/app/utils/cache"
  9. "encoding/json"
  10. "fmt"
  11. "github.com/gin-gonic/gin"
  12. "time"
  13. )
  14. func WithdrawalIncome(c *gin.Context) {
  15. args, mid, err := commArg(c)
  16. if err != nil {
  17. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  18. return
  19. }
  20. fmt.Println(args)
  21. fmt.Println(mid)
  22. if args["type"] == "" {
  23. args["type"] = "playlet"
  24. }
  25. amountMap := masterAmount(mid, args["type"])
  26. monthAmountMap := masterMonthAmount(mid)
  27. isNeedBingAlipay := "1"
  28. if amountMap["alipay"] != "" {
  29. isNeedBingAlipay = "0"
  30. }
  31. var res = make([]map[string]string, 0)
  32. if amountMap["is_show_official_amount"] == "1" {
  33. var tmp = map[string]string{"name": "账户一余额", "value": amountMap["amount"], "type": "amount", "tip": "", "alipay": amountMap["alipay"], "alipay_name": amountMap["alipay_name"], "is_need_bing_alipay": isNeedBingAlipay, "is_show_withdrawal": "1"}
  34. res = append(res, tmp)
  35. if amountMap["is_show_agent_amount"] == "1" {
  36. var tmpAgent = map[string]string{"name": "账户二余额", "value": amountMap["agent_amount"], "type": "agent_amount", "tip": "", "alipay": amountMap["alipay"], "alipay_name": amountMap["alipay_name"], "is_need_bing_alipay": isNeedBingAlipay, "is_show_withdrawal": "1"}
  37. res = append(res, tmpAgent)
  38. }
  39. } else {
  40. var tmpAgent = map[string]string{"name": "账户二余额", "value": amountMap["agent_amount"], "type": "agent_amount", "tip": "", "alipay": amountMap["alipay"], "alipay_name": amountMap["alipay_name"], "is_need_bing_alipay": isNeedBingAlipay, "is_show_withdrawal": "1"}
  41. res = append(res, tmpAgent)
  42. }
  43. res = append(res, map[string]string{"name": "上月预估收益", "value": monthAmountMap["last_month_amount"], "type": "last_month_amount", "tip": "", "is_need_bing_alipay": "0", "is_show_withdrawal": "0"})
  44. res = append(res, map[string]string{"name": "上月预估结算收益", "value": amountMap["last_month_settle_amount"], "type": "last_month_settle_amount", "tip": "", "is_need_bing_alipay": "0", "is_show_withdrawal": "0"})
  45. res = append(res, map[string]string{"name": "本月预估收益", "value": monthAmountMap["month_amount"], "type": "month_amount", "tip": "", "is_need_bing_alipay": "0", "is_show_withdrawal": "0"})
  46. res = append(res, map[string]string{"name": "本月预估结算收益", "value": monthAmountMap["month_settle_amount"], "type": "month_settle_amount", "tip": "", "is_need_bing_alipay": "0", "is_show_withdrawal": "0"})
  47. e.OutSuc(c, res, nil)
  48. return
  49. }
  50. func WithdrawalList(c *gin.Context) {
  51. args, mid, err := commArg(c)
  52. if err != nil {
  53. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  54. return
  55. }
  56. amountMap := masterAmount(mid, args["type"])
  57. masterWithdrawalFlowDb := db.MasterWithdrawalFlowDb{}
  58. masterWithdrawalFlowDb.Set()
  59. list, total := masterWithdrawalFlowDb.GetWithdrawalFlowListWithTotal(amountMap["id"], args)
  60. data := make([]map[string]string, 0)
  61. if list != nil {
  62. for _, v := range *list {
  63. var tmp = map[string]string{
  64. "id": utils.IntToStr(v.Id),
  65. "alipay": v.Alipay,
  66. "alipay_name": v.AlipayName,
  67. "amount": v.Amount,
  68. "real_amount": v.RealAmount,
  69. "fee": v.Fee,
  70. "time": v.Time.Format("2006-01-02 15:04:05"),
  71. "check_time": "",
  72. "status": v.Status,
  73. "remark": v.Remark,
  74. "reason": v.Reason,
  75. "img": v.Img,
  76. "is_need_upload_invoice": utils.IntToStr(v.HasInvoice),
  77. }
  78. if v.Img != "" {
  79. tmp["is_need_upload_invoice"] = "0"
  80. }
  81. if v.CheckTime.IsZero() == false {
  82. tmp["check_time"] = v.CheckTime.Format("2006-01-02 15:04:05")
  83. }
  84. data = append(data, tmp)
  85. }
  86. }
  87. statusList := []map[string]string{
  88. {"name": "提现审核", "value": "提现审核"},
  89. {"name": "提现成功", "value": "提现成功"},
  90. {"name": "提现失败", "value": "提现失败"},
  91. }
  92. var res = map[string]interface{}{
  93. "list": data, "total": total, "status_list": statusList,
  94. }
  95. e.OutSuc(c, res, nil)
  96. return
  97. }
  98. func WithdrawalDoing(c *gin.Context) {
  99. args, mid, err := commArg(c)
  100. if err != nil {
  101. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  102. return
  103. }
  104. if utils.StrToFloat64(args["amount"]) <= 0 {
  105. e.OutErr(c, 400, e.NewErr(400, "金额不正确"))
  106. return
  107. }
  108. masterdb := db.MasterDb{}
  109. masterdb.Set()
  110. master := masterdb.GetMaster(mid)
  111. if master == nil {
  112. e.OutErr(c, 400, e.NewErr(400, "用户不存在"))
  113. return
  114. }
  115. amountMap := masterAmount(mid, args["type"])
  116. puid := "0"
  117. if args["amount_type"] == "agent_amount" {
  118. amountMap["amount"] = amountMap["agent_amount"]
  119. amountMap["list_id"] = amountMap["agent_list_id"]
  120. puid = amountMap["puid"]
  121. }
  122. leaveAmount := utils.StrToFloat64(amountMap["amount"]) - utils.StrToFloat64(args["amount"])
  123. if leaveAmount < 0 {
  124. e.OutErr(c, 400, e.NewErr(400, "余额不足"))
  125. return
  126. }
  127. masterListCfgDb := db.MasterListCfgDb{}
  128. masterListCfgDb.Set()
  129. withdrawalBili := masterListCfgDb.MasterListCfgGetOneData(puid, "withdrawal_bili")
  130. invoiceBili := masterListCfgDb.MasterListCfgGetOneData(puid, "invoice_bili")
  131. //withdrawalDay := masterListCfgDb.MasterListCfgGetOneData(puid, "withdrawal_day")
  132. //if time.Now().Day() != utils.StrToInt(withdrawalDay) && utils.StrToInt(withdrawalDay) > 0 {
  133. // e.OutErr(c, 400, e.NewErr(400, "每月"+withdrawalDay+"号提现"))
  134. // return
  135. //}
  136. var fee float64 = 0
  137. if utils.StrToFloat64(withdrawalBili) > 0 {
  138. bili := utils.StrToFloat64(withdrawalBili) / 100
  139. var invoiceBiliMap = make([]string, 0)
  140. json.Unmarshal([]byte(invoiceBili), &invoiceBiliMap)
  141. if utils.InArr(args["invoice_bili"], invoiceBiliMap) == false && utils.StrToInt(args["has_invoice"]) == 1 {
  142. e.OutErr(c, 400, e.NewErr(400, "发票税率不正确"))
  143. return
  144. }
  145. //开了发票的话再扣掉对应的发票比例
  146. if utils.InArr(args["invoice_bili"], invoiceBiliMap) && utils.StrToInt(args["has_invoice"]) == 1 {
  147. bili -= utils.StrToFloat64(args["invoice_bili"]) / 100
  148. }
  149. fee = utils.StrToFloat64(args["amount"]) * bili
  150. }
  151. realAmount := utils.StrToFloat64(args["amount"]) - fee
  152. if amountMap["alipay"] == "" {
  153. e.OutErr(c, 400, e.NewErr(400, "未绑定支付宝"))
  154. return
  155. }
  156. mutexKey := fmt.Sprintf("withdrawal:%s", amountMap["id"])
  157. withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 30, "NX")
  158. if err != nil {
  159. e.OutErr(c, e.ERR, err)
  160. return
  161. }
  162. if withdrawAvailable != "OK" {
  163. e.OutErr(c, e.ERR, e.NewErr(400000, "操作过于频繁,请稍后再试"))
  164. return
  165. }
  166. sess := db.ZhimengDb.NewSession()
  167. err = sess.Begin()
  168. if err != nil {
  169. sess.Rollback()
  170. e.OutErr(c, 400, e.NewErr(400000, "请重试"))
  171. return
  172. }
  173. defer sess.Close()
  174. //先扣钱
  175. amountData := db.GetMasterAmountByListIdWithSess(sess, amountMap["list_id"])
  176. if amountData == nil {
  177. sess.Rollback()
  178. e.OutErr(c, e.ERR, e.NewErr(400000, "提现失败"))
  179. return
  180. }
  181. oldAmount := amountData.Amount
  182. leaveAmount = utils.StrToFloat64(amountData.Amount) - utils.StrToFloat64(args["amount"])
  183. if leaveAmount < 0 {
  184. e.OutErr(c, 400, e.NewErr(400, "余额不足"))
  185. return
  186. }
  187. amountData.Amount = utils.Float64ToStr(leaveAmount)
  188. update := db.MasterAmountUpdateWithSess(sess, amountData.Id, amountData)
  189. if update == false {
  190. e.OutErr(c, e.ERR, e.NewErr(400000, "提现失败"))
  191. return
  192. }
  193. //再写入明细
  194. var tmpFlow = model.MasterAmountFlow{
  195. Uid: amountMap["id"],
  196. Time: time.Now(),
  197. BeforeAmount: oldAmount,
  198. Amount: args["amount"],
  199. AfterAmount: amountData.Amount,
  200. Platform: args["type"],
  201. Oid: "",
  202. Title: "提现",
  203. FlowType: "withdrawal",
  204. ExtendUid: puid,
  205. }
  206. flowInsert := db.MasterAmountFlowInsertWithSess(sess, &tmpFlow)
  207. if flowInsert == false {
  208. e.OutErr(c, e.ERR, e.NewErr(400000, "提现失败"))
  209. return
  210. }
  211. var tmp = model.MasterWithdrawalFlow{
  212. Uid: amountMap["id"],
  213. Time: time.Now(),
  214. UpdateTime: time.Now(),
  215. Remark: args["remark"],
  216. Alipay: amountMap["alipay"],
  217. AlipayName: amountMap["alipay_name"],
  218. Amount: args["amount"],
  219. RealAmount: utils.Float64ToStr(realAmount),
  220. Fee: utils.Float64ToStr(fee),
  221. Reason: "",
  222. Status: "提现审核",
  223. HasInvoice: utils.StrToInt(args["has_invoice"]),
  224. InvoiceBili: args["invoice_bili"],
  225. ExtendUid: puid,
  226. }
  227. insert := db.MasterWithdrawalFlowInsertWithSess(sess, &tmp)
  228. if insert == false {
  229. e.OutErr(c, e.ERR, e.NewErr(400000, "提现失败"))
  230. return
  231. }
  232. sess.Commit()
  233. e.OutSuc(c, "success", nil)
  234. return
  235. }
  236. func WithdrawalOutput(c *gin.Context) {
  237. args, mid, err := commArg(c)
  238. if err != nil {
  239. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  240. return
  241. }
  242. args["size"] = "3000"
  243. amountMap := masterAmount(mid, args["type"])
  244. masterWithdrawalFlowDb := db.MasterWithdrawalFlowDb{}
  245. masterWithdrawalFlowDb.Set()
  246. list := masterWithdrawalFlowDb.GetWithdrawalFlowList(amountMap["id"], args)
  247. name := "订单_" + args["p"]
  248. //写入数据
  249. data := map[string]string{
  250. "A1": "提现支付宝账号",
  251. "B1": "提现支付宝姓名",
  252. "C1": "提现金额",
  253. "D1": "实际金额",
  254. "E1": "手续费",
  255. "F1": "提现状态",
  256. "G1": "申请时间",
  257. "H1": "审核时间",
  258. "I1": "备注",
  259. "J1": "失败原因",
  260. }
  261. if list != nil {
  262. for k, v := range *list {
  263. checkTime := ""
  264. if v.CheckTime.IsZero() == false {
  265. checkTime = v.CheckTime.Format("2006-01-02 15:04:05")
  266. }
  267. i := utils.IntToStr(k + 2)
  268. data["A"+i] = v.Alipay
  269. data["B"+i] = v.AlipayName
  270. data["C"+i] = v.Amount
  271. data["D"+i] = v.RealAmount
  272. data["E"+i] = v.Fee
  273. data["F"+i] = v.Status
  274. data["G"+i] = v.Time.Format("2006-01-02 15:04:05")
  275. data["H"+i] = checkTime
  276. data["I"+i] = v.Remark
  277. data["J"+i] = v.Reason
  278. }
  279. }
  280. file := utils.Output(c, name, data)
  281. filename := name + ".xlsx"
  282. r := map[string]string{
  283. "file": file,
  284. "filename": filename,
  285. }
  286. e.OutSuc(c, r, nil)
  287. return
  288. }
  289. func WithdrawalInvoiceImg(c *gin.Context) {
  290. var args map[string]string
  291. if err := c.ShouldBindJSON(&args); err != nil {
  292. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  293. return
  294. }
  295. masterWithdrawalFlowDb := db.MasterWithdrawalFlowDb{}
  296. masterWithdrawalFlowDb.Set()
  297. flow := masterWithdrawalFlowDb.MasterWithdrawalFlowById(args["id"])
  298. flow.Img = args["img"]
  299. update := masterWithdrawalFlowDb.MasterWithdrawalFlowInsertUpdate(flow)
  300. if update == false {
  301. e.OutErr(c, 400, e.NewErr(400, "上传失败"))
  302. return
  303. }
  304. e.OutSuc(c, "success", nil)
  305. return
  306. }
  307. func WithdrawalBindAlipay(c *gin.Context) {
  308. var args map[string]string
  309. if err := c.ShouldBindJSON(&args); err != nil {
  310. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  311. return
  312. }
  313. if args["alipay"] == "" || args["alipay_name"] == "" {
  314. e.OutErr(c, 400, e.NewErr(400, "支付宝信息不能为空"))
  315. return
  316. }
  317. masterId, _ := c.Get("master_id")
  318. mid := utils.AnyToString(masterId)
  319. masterDb := db.MasterDb{}
  320. masterDb.Set()
  321. master := masterDb.GetMaster(mid)
  322. master.AlipayName = args["alipay_name"]
  323. master.Alipay = args["alipay"]
  324. update := masterDb.MasterUpdate(master)
  325. if update == false {
  326. e.OutErr(c, 400, e.NewErr(400, "修改失败"))
  327. return
  328. }
  329. e.OutSuc(c, "success", nil)
  330. return
  331. }
  332. func commArg(c *gin.Context) (map[string]string, string, error) {
  333. masterId, _ := c.Get("master_id")
  334. mid := utils.AnyToString(masterId)
  335. fmt.Println(mid)
  336. var args map[string]string
  337. if err := c.ShouldBindJSON(&args); err != nil {
  338. return args, mid, err
  339. }
  340. return args, mid, nil
  341. }
  342. func masterInfo(mid string) map[string]string {
  343. masterDb := db.MasterDb{}
  344. masterDb.Set()
  345. master := masterDb.GetMaster(mid)
  346. res := make(map[string]string)
  347. if master != nil {
  348. res["id"] = utils.IntToStr(master.Id)
  349. res["alipay"] = master.Alipay
  350. res["alipay_name"] = master.AlipayName
  351. }
  352. return res
  353. }
  354. func masterAmount(mid, types string) map[string]string {
  355. masterInfos := masterInfo(mid)
  356. res := map[string]string{
  357. "amount": "0.00",
  358. "last_month_settle_amount": "0.00",
  359. }
  360. if masterInfos["id"] == "" {
  361. return res
  362. }
  363. puid := AppUserListPuid(mid)
  364. hwOwnOpen := offical.MasterListCfgGetOneData(puid, "hw_own_open")
  365. masterAmountDb := db.MasterAmountDb{}
  366. masterAmountDb.Set()
  367. masterAmounts := masterAmountDb.GetMasterAmountByExtendUid(masterInfos["id"], "0", types)
  368. if masterAmounts == nil {
  369. return res
  370. }
  371. res["agent_amount"] = "0"
  372. res["is_show_official_amount"] = "1"
  373. res["puid"] = puid
  374. res["is_show_agent_amount"] = "0"
  375. if utils.StrToInt(puid) > 0 && hwOwnOpen == "1" {
  376. res["is_show_agent_amount"] = "1"
  377. agentMasterAmounts := masterAmountDb.GetMasterAmountByExtendUid(masterInfos["id"], puid, types)
  378. if agentMasterAmounts != nil {
  379. if agentMasterAmounts.Amount != "" {
  380. res["agent_amount"] = agentMasterAmounts.Amount
  381. }
  382. res["agent_list_id"] = utils.IntToStr(agentMasterAmounts.Id)
  383. masterAmounts.LastMonthAmount = utils.Float64ToStr(utils.StrToFloat64(masterAmounts.LastMonthAmount) + utils.StrToFloat64(agentMasterAmounts.LastMonthAmount))
  384. }
  385. if utils.StrToFloat64(masterAmounts.Amount) == 0 {
  386. res["is_show_official_amount"] = "0"
  387. }
  388. }
  389. res["amount"] = masterAmounts.Amount
  390. if res["amount"] == "" {
  391. res["amount"] = "0"
  392. }
  393. res["id"] = masterAmounts.Uid
  394. res["list_id"] = utils.IntToStr(masterAmounts.Id)
  395. res["alipay"] = masterInfos["alipay"]
  396. res["alipay_name"] = masterInfos["alipay_name"]
  397. res["last_month_settle_amount"] = masterAmounts.LastMonthAmount
  398. if res["last_month_settle_amount"] == "" {
  399. res["last_month_settle_amount"] = "0"
  400. }
  401. return res
  402. }
  403. func masterMonthAmount(mid string) map[string]string {
  404. playletSaleOrder := db.PlayletSaleOrderDb{}
  405. playletSaleOrder.Set()
  406. lastMonthSum := playletSaleOrder.PlayletVideoOrderSum(mid, "", "last_month")
  407. monthSum := playletSaleOrder.PlayletVideoOrderSum(mid, "", "current_month")
  408. monthSettleSum := playletSaleOrder.PlayletVideoOrderSum(mid, "订单结算", "current_month")
  409. lastMonthTimeRange := utils.GetTimeRange("last_month")
  410. currentMonthTimeRange := utils.GetTimeRange("current_month")
  411. hwSum, _ := db.ZhimengDb.NotIn("status", []string{"创建订单", "订单退款", "订单失效"}).Where("uid=? and create_time>=? and create_time<?", mid, lastMonthTimeRange["start"], lastMonthTimeRange["end"]).Sum(&model.HwOrder{}, "commission")
  412. guideSum, _ := db.ZhimengDb.NotIn("status", []string{"创建订单", "订单退款", "订单失效"}).Where("uid=? and create_time>=? and create_time<?", mid, lastMonthTimeRange["start"], lastMonthTimeRange["end"]).Sum(&model.GuideOrder{}, "commission")
  413. lastMonthSum += hwSum + guideSum
  414. hwMonthSum, _ := db.ZhimengDb.NotIn("status", []string{"创建订单", "订单退款", "订单失效"}).Where("uid=? and create_time>=? and create_time<?", mid, currentMonthTimeRange["start"], currentMonthTimeRange["end"]).Sum(&model.HwOrder{}, "commission")
  415. guideMonthSum, _ := db.ZhimengDb.NotIn("status", []string{"创建订单", "订单退款", "订单失效"}).Where("uid=? and create_time>=? and create_time<?", mid, currentMonthTimeRange["start"], currentMonthTimeRange["end"]).Sum(&model.GuideOrder{}, "commission")
  416. monthSum += hwMonthSum + guideMonthSum
  417. hwMonthSettleSum, _ := db.ZhimengDb.In("status", []string{"订单结算"}).Where("uid=? and create_time>=? and create_time<?", mid, currentMonthTimeRange["start"], currentMonthTimeRange["end"]).Sum(&model.HwOrder{}, "commission")
  418. guideMonthSettleSum, _ := db.ZhimengDb.In("status", []string{"订单结算"}).Where("uid=? and create_time>=? and create_time<?", mid, currentMonthTimeRange["start"], currentMonthTimeRange["end"]).Sum(&model.GuideOrder{}, "real_commission")
  419. monthSettleSum += hwMonthSettleSum + guideMonthSettleSum
  420. res := map[string]string{
  421. "last_month_amount": utils.Float64ToStr(lastMonthSum),
  422. "month_amount": utils.Float64ToStr(monthSum),
  423. "month_settle_amount": utils.Float64ToStr(monthSettleSum),
  424. }
  425. return res
  426. }