智盟项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svc_withdrawal.go 12 KiB

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