package hdl

import (
	"applet/app/db"
	"applet/app/e"
	storeSvc "applet/app/store/svc"
	"applet/app/svc"
	"applet/app/utils"
	"fmt"
	"github.com/gin-gonic/gin"
)

func StoreIndexTotal(c *gin.Context) {
	var arg map[string]string
	if err := c.ShouldBindJSON(&arg); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	user := svc.GetUser(c)
	stime, etime := svc.GetDate(c, arg)
	sql := `select SUM(amount-agent_commission-platform_commission) AS money,SUM(amount) AS amount,SUM(commission) AS commission,SUM(IF(state=3,1,0)) as count,SUM(IF(state in(1,2),1,0)) as success_count,
	SUM(IF(state=1,1,0)) as wait_count
	from community_team_order 
	where %s
`
	where := "store_uid=" + utils.IntToStr(user.Info.Uid) + " and state>0"
	wherePay := where + " and create_at>='" + stime.Format("2006-01-02 15:04:05") + "' and create_at<'" + etime.Format("2006-01-02 15:04:05") + "'"
	sqlPay := fmt.Sprintf(sql, wherePay)
	nativeString, _ := db.QueryNativeString(svc.MasterDb(c), sqlPay)
	amount := "0"
	money := "0"
	commission := "0"
	count := "0"
	successCount := "0"
	waitCount := "0"
	for _, v := range nativeString {
		amount = v["amount"]
		money = v["money"]
		commission = v["commission"]
		count = v["count"]
		successCount = v["success_count"]
		waitCount = v["wait_count"]
	}
	sqlCodePay := `select SUM(amount-agent_commission-platform_commission) AS money,SUM(amount) AS amount,SUM(commission) AS commission,SUM(IF(state=3,1,0)) as count,SUM(IF(state in(1,2),1,0)) as success_count,
	SUM(IF(state=1,1,0)) as wait_count
	from community_team_pay_order 
	where %s
`
	whereCodePay := where + " and create_at>='" + stime.Format("2006-01-02 15:04:05") + "' and create_at<'" + etime.Format("2006-01-02 15:04:05") + "'"
	sqlCodePay = fmt.Sprintf(sqlCodePay, whereCodePay)
	nativeStringCodePay, _ := db.QueryNativeString(svc.MasterDb(c), sqlCodePay)
	for _, v := range nativeStringCodePay {
		amount = utils.Float64ToStr(utils.StrToFloat64(amount) + utils.StrToFloat64(v["amount"]))
		money = utils.Float64ToStr(utils.StrToFloat64(money) + utils.StrToFloat64(v["money"]))
		commission = utils.Float64ToStr(utils.StrToFloat64(commission) + utils.StrToFloat64(v["commission"]))
		count = utils.IntToStr(utils.StrToInt(count) + utils.StrToInt(v["count"]))
		successCount = utils.IntToStr(utils.StrToInt(successCount) + utils.StrToInt(v["success_count"]))
	}
	store := db.GetStoreIdEg(svc.MasterDb(c), utils.IntToStr(user.Info.Uid))
	tmp := []map[string]string{
		{"name": "营业总额", "value": svc.GetCommissionPrec(c, amount, "2", "1")},
	}
	if store != nil {
		if store.StoreType == 0 {
			tmp = append(tmp, map[string]string{"name": "佣金收益", "value": svc.GetCommissionPrec(c, commission, "2", "1")})
		}
		if store.StoreType > 0 {
			tmp = append(tmp, map[string]string{"name": "订单收益", "value": svc.GetCommissionPrec(c, money, "2", "1")})
		}
	} else {
		tmp = append(tmp, map[string]string{"name": "订单收益", "value": svc.GetCommissionPrec(c, money, "2", "1")})
	}
	tmp = append(tmp, map[string]string{"name": "已付款订单量", "value": utils.IntToStr(utils.StrToInt(successCount))})
	tmp = append(tmp, map[string]string{"name": "已取消订单量", "value": utils.IntToStr(utils.StrToInt(count))})
	tmp = append(tmp, map[string]string{"name": "待提货订单量", "value": utils.IntToStr(utils.StrToInt(waitCount))})
	e.OutSuc(c, tmp, nil)
	return
}

func StoreWithdrawBase(c *gin.Context) {
	user := svc.GetUser(c)
	var res = map[string]string{
		"is_bind":        "0",
		"amount":         "0",
		"alipay_account": user.Profile.AccAlipay,
		"alipay_name":    user.Profile.AccAlipayRealName,
		"info":           "",
	}
	if user.Profile.AccAlipay != "" {
		res["is_bind"] = "1"
	}

	store := db.GetStoreIdEg(svc.MasterDb(c), utils.IntToStr(user.Info.Uid))
	if store != nil {
		amountData := db.GetStoreAmountEg(svc.MasterDb(c), user.Info.Uid, store.ParentUid, store.StoreType)
		if amountData != nil {
			res["amount"] = amountData.Amount
		}
	}
	e.OutSuc(c, res, nil)
	return
}
func StoreWithdrawFlowCate(c *gin.Context) {
	res := []map[string]string{
		{"name": "全部", "value": ""},
		{"name": "订单", "value": "1"},
		{"name": "收款", "value": "4"},
		{"name": "提现", "value": "2"},
	}
	e.OutSuc(c, res, nil)
	return
}
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["store_uid"] = utils.IntToStr(user.Info.Uid)
	store := db.GetStoreIdEg(svc.MasterDb(c), utils.IntToStr(user.Info.Uid))
	if store != nil {
		req["parent_uid"] = utils.IntToStr(store.ParentUid)
		req["store_type"] = utils.IntToStr(store.StoreType)
	}
	withdraw, total := db.GetStoreFlow(svc.MasterDb(c), req)
	list := make([]map[string]string, 0)
	if withdraw != nil {
		var stateList = []string{"", "订单", "提现", "", "收款"}
		for _, v := range *withdraw {
			tmp := map[string]string{
				"amount":   v.Amount,
				"title":    v.Title,
				"ord_type": utils.IntToStr(v.OrdType),
				"label":    stateList[v.OrdType],
				"oid":      utils.Int64ToStr(v.Oid),
				"time":     v.CreateAt.Format("2006-01-02 15:04:05"),
			}
			if v.OrdType == 1 {
				ord := db.GetOrderInfoFirstEg(svc.MasterDb(c), utils.Int64ToStr(v.Oid))
				if ord != nil {
					tmp["title"] = ord.Title
				}
			}
			list = append(list, tmp)
		}
	}
	res := map[string]interface{}{
		"total": total,
		"list":  list,
	}
	e.OutSuc(c, res, nil)
	return
}
func StoreWithdrawDoing(c *gin.Context) {
	storeSvc.StoreWithdrawDoing(c)
}
