智盟项目
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

373 righe
11 KiB

  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. amountMap := masterAmount(mid, args["type"])
  99. leaveAmount := utils.StrToFloat64(amountMap["amount"]) - utils.StrToFloat64(args["amount"])
  100. if leaveAmount < 0 {
  101. e.OutErr(c, 400, e.NewErr(400, "余额不足"))
  102. return
  103. }
  104. masterListCfgDb := db.MasterListCfgDb{}
  105. masterListCfgDb.Set()
  106. withdrawalBili := masterListCfgDb.MasterListCfgGetOneData("0", "withdrawal_bili")
  107. invoiceBili := masterListCfgDb.MasterListCfgGetOneData("0", "invoice_bili")
  108. var fee float64 = 0
  109. if utils.StrToFloat64(withdrawalBili) > 0 {
  110. bili := utils.StrToFloat64(withdrawalBili) / 100
  111. var invoiceBiliMap = make([]string, 0)
  112. json.Unmarshal([]byte(invoiceBili), &invoiceBiliMap)
  113. if utils.InArr(args["invoice_bili"], invoiceBiliMap) == false && utils.StrToInt(args["has_invoice"]) == 1 {
  114. e.OutErr(c, 400, e.NewErr(400, "发票税率不正确"))
  115. return
  116. }
  117. //开了发票的话再扣掉对应的发票比例
  118. if utils.InArr(args["invoice_bili"], invoiceBiliMap) && utils.StrToInt(args["has_invoice"]) == 1 {
  119. bili -= utils.StrToFloat64(args["invoice_bili"]) / 100
  120. }
  121. fee = utils.StrToFloat64(args["amount"]) * bili
  122. }
  123. realAmount := utils.StrToFloat64(args["amount"]) - fee
  124. if amountMap["alipay"] == "" {
  125. e.OutErr(c, 400, e.NewErr(400, "未绑定支付宝"))
  126. return
  127. }
  128. mutexKey := fmt.Sprintf("withdrawal:%s", amountMap["id"])
  129. withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 30, "NX")
  130. if err != nil {
  131. e.OutErr(c, e.ERR, err)
  132. return
  133. }
  134. if withdrawAvailable != "OK" {
  135. e.OutErr(c, e.ERR, e.NewErr(400000, "操作过于频繁,请稍后再试"))
  136. return
  137. }
  138. sess := db.ZhimengDb.NewSession()
  139. err = sess.Begin()
  140. if err != nil {
  141. sess.Rollback()
  142. e.OutErr(c, 400, e.NewErr(400000, "请重试"))
  143. return
  144. }
  145. defer sess.Close()
  146. //先扣钱
  147. amountData := db.GetMasterAmountByListIdWithSess(sess, amountMap["list_id"])
  148. if amountData == nil {
  149. sess.Rollback()
  150. e.OutErr(c, e.ERR, e.NewErr(400000, "提现失败"))
  151. return
  152. }
  153. oldAmount := amountData.Amount
  154. leaveAmount = utils.StrToFloat64(amountData.Amount) - utils.StrToFloat64(args["amount"])
  155. if leaveAmount < 0 {
  156. e.OutErr(c, 400, e.NewErr(400, "余额不足"))
  157. return
  158. }
  159. amountData.Amount = utils.Float64ToStr(leaveAmount)
  160. update := db.MasterAmountUpdateWithSess(sess, amountData.Id, amountData)
  161. if update == false {
  162. e.OutErr(c, e.ERR, e.NewErr(400000, "提现失败"))
  163. return
  164. }
  165. //再写入明细
  166. var tmpFlow = model.MasterAmountFlow{
  167. Uid: amountMap["id"],
  168. Time: time.Now(),
  169. BeforeAmount: oldAmount,
  170. Amount: args["amount"],
  171. AfterAmount: amountData.Amount,
  172. Platform: args["type"],
  173. Oid: "",
  174. Title: "提现",
  175. FlowType: "withdrawal",
  176. }
  177. flowInsert := db.MasterAmountFlowInsertWithSess(sess, &tmpFlow)
  178. if flowInsert == false {
  179. e.OutErr(c, e.ERR, e.NewErr(400000, "提现失败"))
  180. return
  181. }
  182. var tmp = model.MasterWithdrawalFlow{
  183. Uid: amountMap["id"],
  184. Time: time.Now(),
  185. UpdateTime: time.Now(),
  186. Remark: args["remark"],
  187. Alipay: amountMap["alipay"],
  188. AlipayName: amountMap["alipay_name"],
  189. Amount: args["amount"],
  190. RealAmount: utils.Float64ToStr(realAmount),
  191. Fee: utils.Float64ToStr(fee),
  192. Reason: "",
  193. Status: "提现审核",
  194. HasInvoice: utils.StrToInt(args["has_invoice"]),
  195. InvoiceBili: args["invoice_bili"],
  196. }
  197. insert := db.MasterWithdrawalFlowInsertWithSess(sess, &tmp)
  198. if insert == false {
  199. e.OutErr(c, e.ERR, e.NewErr(400000, "提现失败"))
  200. return
  201. }
  202. sess.Commit()
  203. e.OutSuc(c, "success", nil)
  204. return
  205. }
  206. func WithdrawalOutput(c *gin.Context) {
  207. args, mid, err := commArg(c)
  208. if err != nil {
  209. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  210. return
  211. }
  212. args["size"] = "3000"
  213. amountMap := masterAmount(mid, args["type"])
  214. masterWithdrawalFlowDb := db.MasterWithdrawalFlowDb{}
  215. masterWithdrawalFlowDb.Set()
  216. list := masterWithdrawalFlowDb.GetWithdrawalFlowList(amountMap["id"], args)
  217. name := "订单_" + args["p"]
  218. //写入数据
  219. data := map[string]string{
  220. "A1": "提现支付宝账号",
  221. "B1": "提现支付宝姓名",
  222. "C1": "提现金额",
  223. "D1": "实际金额",
  224. "E1": "手续费",
  225. "F1": "提现状态",
  226. "G1": "申请时间",
  227. "H1": "审核时间",
  228. "I1": "备注",
  229. "J1": "失败原因",
  230. }
  231. if list != nil {
  232. for k, v := range *list {
  233. checkTime := ""
  234. if v.CheckTime.IsZero() == false {
  235. checkTime = v.CheckTime.Format("2006-01-02 15:04:05")
  236. }
  237. i := utils.IntToStr(k + 2)
  238. data["A"+i] = v.Alipay
  239. data["B"+i] = v.AlipayName
  240. data["C"+i] = v.Amount
  241. data["D"+i] = v.RealAmount
  242. data["E"+i] = v.Fee
  243. data["F"+i] = v.Status
  244. data["G"+i] = v.Time.Format("2006-01-02 15:04:05")
  245. data["H"+i] = checkTime
  246. data["I"+i] = v.Remark
  247. data["J"+i] = v.Reason
  248. }
  249. }
  250. file := utils.Output(c, name, data)
  251. filename := name + ".xlsx"
  252. r := map[string]string{
  253. "file": file,
  254. "filename": filename,
  255. }
  256. e.OutSuc(c, r, nil)
  257. return
  258. }
  259. func WithdrawalInvoiceImg(c *gin.Context) {
  260. var args map[string]string
  261. if err := c.ShouldBindJSON(&args); err != nil {
  262. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  263. return
  264. }
  265. masterWithdrawalFlowDb := db.MasterWithdrawalFlowDb{}
  266. masterWithdrawalFlowDb.Set()
  267. flow := masterWithdrawalFlowDb.MasterWithdrawalFlowById(args["id"])
  268. flow.Img = args["img"]
  269. update := masterWithdrawalFlowDb.MasterWithdrawalFlowInsertUpdate(flow)
  270. if update == false {
  271. e.OutErr(c, 400, e.NewErr(400, "上传失败"))
  272. return
  273. }
  274. e.OutSuc(c, "success", nil)
  275. return
  276. }
  277. func WithdrawalBindAlipay(c *gin.Context) {
  278. var args map[string]string
  279. if err := c.ShouldBindJSON(&args); err != nil {
  280. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  281. return
  282. }
  283. if args["alipay"] == "" || args["alipay_name"] == "" {
  284. e.OutErr(c, 400, e.NewErr(400, "支付宝信息不能为空"))
  285. return
  286. }
  287. masterId, _ := c.Get("master_id")
  288. mid := utils.AnyToString(masterId)
  289. masterDb := db.MasterDb{}
  290. masterDb.Set()
  291. master := masterDb.GetMaster(mid)
  292. master.AlipayName = args["alipay_name"]
  293. master.Alipay = args["alipay"]
  294. update := masterDb.MasterUpdate(master)
  295. if update == false {
  296. e.OutErr(c, 400, e.NewErr(400, "修改失败"))
  297. return
  298. }
  299. e.OutSuc(c, "success", nil)
  300. return
  301. }
  302. func commArg(c *gin.Context) (map[string]string, string, error) {
  303. masterId, _ := c.Get("master_id")
  304. mid := utils.AnyToString(masterId)
  305. fmt.Println(mid)
  306. var args map[string]string
  307. if err := c.ShouldBindJSON(&args); err != nil {
  308. return args, mid, err
  309. }
  310. return args, mid, nil
  311. }
  312. func masterInfo(mid string) map[string]string {
  313. masterDb := db.MasterDb{}
  314. masterDb.Set()
  315. master := masterDb.GetMaster(mid)
  316. res := make(map[string]string)
  317. if master != nil {
  318. res["id"] = utils.IntToStr(master.Id)
  319. res["alipay"] = master.Alipay
  320. res["alipay_name"] = master.AlipayName
  321. }
  322. return res
  323. }
  324. func masterAmount(mid, types string) map[string]string {
  325. masterInfos := masterInfo(mid)
  326. res := map[string]string{
  327. "amount": "0.00",
  328. "last_month_settle_amount": "0.00",
  329. }
  330. if masterInfos["id"] == "" {
  331. return res
  332. }
  333. masterAmountDb := db.MasterAmountDb{}
  334. masterAmountDb.Set()
  335. masterAmounts := masterAmountDb.GetMasterAmount(masterInfos["id"], types)
  336. if masterAmounts == nil {
  337. return res
  338. }
  339. res["amount"] = masterAmounts.Amount
  340. if res["amount"] == "" {
  341. res["amount"] = "0"
  342. }
  343. res["id"] = masterAmounts.Uid
  344. res["list_id"] = utils.IntToStr(masterAmounts.Id)
  345. res["alipay"] = masterInfos["alipay"]
  346. res["alipay_name"] = masterInfos["alipay_name"]
  347. res["last_month_settle_amount"] = masterAmounts.LastMonthAmount
  348. if res["last_month_settle_amount"] == "" {
  349. res["last_month_settle_amount"] = "0"
  350. }
  351. return res
  352. }
  353. func masterMonthAmount(mid string) map[string]string {
  354. playletSaleOrder := db.PlayletSaleOrderDb{}
  355. playletSaleOrder.Set()
  356. lastMonthSum := playletSaleOrder.PlayletVideoOrderSum(mid, "", "last_month")
  357. monthSum := playletSaleOrder.PlayletVideoOrderSum(mid, "", "current_month")
  358. monthSettleSum := playletSaleOrder.PlayletVideoOrderSum(mid, "订单结算", "current_month")
  359. res := map[string]string{
  360. "last_month_amount": utils.Float64ToStr(lastMonthSum),
  361. "month_amount": utils.Float64ToStr(monthSum),
  362. "month_settle_amount": utils.Float64ToStr(monthSettleSum),
  363. }
  364. return res
  365. }