package svc

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

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

	store := db.GetStoreIdEg(svc.MasterDb(c), utils.IntToStr(user.Info.Uid))
	if store != nil {
		str := "commission"
		if store.ParentUid > 0 {
			str = "amount-agent_commission"
		}
		if store.StoreType == 1 {
			str = "amount-platform_commission"
		}
		amount, _ := svc.MasterDb(c).Where("store_uid=? and parent_uid=? and store_type=? and state=1", user.Info.Uid, store.ParentUid, store.StoreType).Sum(&model.CommunityTeamOrder{}, str)
		res["wait_amount"] = utils.Float64ToStr(amount)
		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 StoreAmountFlow(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.GetStoreAmountFlowList(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,
				"type_str":     stateList[v.Type],
				"type":         utils.IntToStr(v.Type),
				"title":        v.Title,
				"oid":          utils.Int64ToStr(v.Oid),
				"after_amount": v.AfterAmount,
				"time":         v.CreateAt.Format("2006-01-02 15:04:05"),
			}
			list = append(list, tmp)
		}
	}
	res := map[string]interface{}{
		"total": total,
		"list":  list,
	}
	e.OutSuc(c, res, nil)
	return
}
