智盟项目
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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