package hdl

import (
	"applet/app/db"
	"applet/app/e"
	"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"]
	}
	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.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")})
	}
	tmp = append(tmp, map[string]string{"name": "已付款订单量", "value": successCount})
	tmp = append(tmp, map[string]string{"name": "已取消订单量", "value": count})
	tmp = append(tmp, map[string]string{"name": "待提货订单量", "value": waitCount})
	e.OutSuc(c, tmp, nil)
	return
}
