package svc

import (
	"applet/app/db"
	"applet/app/db/model"
	"applet/app/e"
	"applet/app/svc"
	"applet/app/utils"
	"github.com/gin-gonic/gin"
	"strings"
	"time"
)

func StoreWithdrawFlow(c *gin.Context) {
	var req map[string]string
	if err := c.ShouldBindJSON(&req); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	user := svc.GetUser(c)
	req["agent_uid"] = utils.IntToStr(user.Info.Uid)
	withdraw, total := db.GetStoreWithdraw(svc.MasterDb(c), req)
	list := make([]map[string]string, 0)
	if withdraw != nil {
		var stateList = []string{"审核中", "审核通过", "审核通过", "审核拒绝"}
		for _, v := range *withdraw {
			name := ""
			store := db.GetStoreIdEg(svc.MasterDb(c), utils.IntToStr(v.Uid))
			if store != nil {
				name = store.Name
			}
			tmp := map[string]string{
				"id":             utils.IntToStr(v.Id),
				"name":           name,
				"amount":         v.Amount,
				"alipay_account": v.WithdrawAccount,
				"alipay_name":    v.WithdrawName,
				"state_str":      stateList[v.State],
				"state":          utils.IntToStr(v.State),
				"time":           v.CreateAt.Format("2006-01-02 15:04:05"),
				"end_time":       "",
				"memo":           v.Memo,
			}
			if v.PaymentDate != "" && v.PaymentDate != "0000-00-00 00:00:00" {
				tmp["end_time"] = v.PaymentDate
			}
			list = append(list, tmp)
		}
	}
	res := map[string]interface{}{
		"total": total,
		"list":  list,
		"state": []map[string]string{
			{"name": "审核中", "value": "0"},
			{"name": "审核通过", "value": "1"},
			{"name": "审核拒绝", "value": "3"},
		},
	}
	e.OutSuc(c, res, nil)
	return
}
func StoreWithdrawTotal(c *gin.Context) {
	var req map[string]string
	if err := c.ShouldBindJSON(&req); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	user := svc.GetUser(c)
	var allAmount float64 = 0
	var waitAmount float64 = 0
	var failAmount float64 = 0
	req["agent_uid"] = utils.IntToStr(user.Info.Uid)
	allAmount, _ = db.CommWhere(svc.MasterDb(c), req).And("state=?", 1).Sum(&model.CommunityTeamStoreWithdrawApply{}, "amount")
	waitAmount, _ = db.CommWhere(svc.MasterDb(c), req).And("state=?", 0).Sum(&model.CommunityTeamStoreWithdrawApply{}, "amount")
	failAmount, _ = db.CommWhere(svc.MasterDb(c), req).And("state=?", 3).Sum(&model.CommunityTeamStoreWithdrawApply{}, "amount")
	res := []map[string]string{
		{"name": "累计提现金额(元):" + utils.Float64ToStr(allAmount), "tip": "统计提现成功的金额"},
		{"name": "审核中金额(元):" + utils.Float64ToStr(waitAmount), "tip": "统计正在审核中的金额"},
		{"name": "审核拒绝金额(元):" + utils.Float64ToStr(failAmount), "tip": "统计审核拒绝的金额"},
	}
	e.OutSuc(c, res, nil)
	return
}
func StoreWithdrawAudit(c *gin.Context) {
	var req map[string]string
	if err := c.ShouldBindJSON(&req); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	sess := svc.MasterDb(c).NewSession()
	defer sess.Close()
	sess.Begin()
	data := db.GetStoreWithdrawById(sess, req["id"])
	if data == nil {
		sess.Rollback()
		e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
		return
	}
	if data.State > 0 {
		sess.Rollback()
		e.OutErr(c, 400, e.NewErr(400, "记录已处理"))
		return
	}
	if req["state"] == "3" {
		bools := svc.MoneyCheck(c, sess, data.Uid, data.ParentUid, data.StoreType, 0, 2, utils.StrToFloat64(data.Amount), "提现审核拒绝退回", utils.StrToInt64(utils.OrderUUID(data.Uid)))
		if bools == false {
			sess.Rollback()
			e.OutErr(c, 400, e.NewErr(400, "审核失败"))
			return
		}
	}
	data.State = utils.StrToInt(req["state"])
	data.Memo = req["memo"]
	if data.State == 1 {
		data.PaymentDate = time.Now().Format("2006-01-02 15:04:05")
	}
	update, err := sess.Where("id=?", data.Id).Cols("state,payment_date,memo").Update(data)
	if update == 0 || err != nil {
		sess.Rollback()
		e.OutErr(c, 400, e.NewErr(400, "审核失败"))
		return
	}
	sess.Commit()
	e.OutSuc(c, "success", nil)
	return
}
func StoreWithdrawAuditAll(c *gin.Context) {
	var req map[string]string
	if err := c.ShouldBindJSON(&req); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	svc.MasterDb(c).In("id", strings.Split(req["ids"], ",")).And("state=0").Cols("state,payment_date").Update(&model.CommunityTeamStoreWithdrawApply{State: 1, PaymentDate: time.Now().Format("2006-01-02 15:04:05")})
	e.OutSuc(c, "success", nil)
	return
}
func StoreWithdrawOutPut(c *gin.Context) {
	var args map[string]string
	if err := c.ShouldBindJSON(&args); err != nil {
		e.OutErr(c, 200, e.ERR_INVALID_ARGS)
		return
	}
	name := "提现记录_" + args["p"]
	args["state"] = "0"
	args["size"] = "3000"
	if args["ids"] != "" {
		args["size"] = "0"
	}
	user := svc.GetUser(c)
	args["parent_uid"] = utils.IntToStr(user.Info.Uid)
	data, _ := db.GetStoreWithdraw(svc.MasterDb(c), args)
	file := utils.OutputSecond(c, name, data)
	filename := name + ".xls"
	r := map[string]string{
		"file":     file,
		"filename": filename,
	}
	e.OutSuc(c, r, nil)
	return
}
