智盟项目
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.

448 rivejä
16 KiB

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