package db

import (
	"applet/app/db/model"
	"applet/app/utils"
	"strings"
	"xorm.io/xorm"
)

func GetStoreWithdraw(eg *xorm.Engine, arg map[string]string) (*[]model.CommunityTeamStoreWithdrawApply, int64) {
	var data []model.CommunityTeamStoreWithdrawApply
	sess := CommWhere(eg, arg)
	limit := utils.StrToInt(arg["size"])
	start := (utils.StrToInt(arg["p"]) - 1) * limit
	if limit > 0 {
		sess.Limit(limit, start)
	}
	count, err := sess.Desc("id").FindAndCount(&data)
	if err != nil {
		return nil, count
	}
	return &data, count
}
func GetStoreFlow(eg *xorm.Engine, arg map[string]string) (*[]model.CommunityTeamStoreAmountFlow, int64) {
	var data []model.CommunityTeamStoreAmountFlow
	sess := eg.Where("1=1")
	if arg["store_uid"] != "" {
		sess.And("uid=?", arg["store_uid"])
	}
	if arg["ord_type"] != "" {
		sess.And("ord_type=?", arg["ord_type"])
	}
	limit := utils.StrToInt(arg["size"])
	start := (utils.StrToInt(arg["p"]) - 1) * limit
	if limit > 0 {
		sess.Limit(limit, start)
	}
	count, err := sess.Desc("id").FindAndCount(&data)
	if err != nil {
		return nil, count
	}
	return &data, count
}
func CommWhere(eg *xorm.Engine, arg map[string]string) *xorm.Session {
	sess := eg.Where("1=1")
	if arg["store_uid"] != "" {
		sess.And("uid=?", arg["store_uid"])
	}
	if arg["parent_uid"] != "" {
		sess.And("parent_uid=?", arg["parent_uid"])
	}
	if arg["store_type"] != "" {
		sess.And("store_type=?", arg["store_type"])
	}

	if arg["state"] != "" {
		sess.And("state=?", arg["state"])
	}
	if arg["ids"] != "" {
		sess.In("id", strings.Split(arg["ids"], ","))
	}
	if arg["alipay_account"] != "" {
		sess.And("withdraw_account like ?", "%"+arg["alipay_account"]+"%")
	}
	if arg["alipay_name"] != "" {
		sess.And("withdraw_name like ?", "%"+arg["alipay_name"]+"%")
	}
	if arg["store_name"] != "" {
		var data1 []model.CommunityTeamStore
		eg.Where("name like ?", "%"+arg["store_name"]+"%").Find(&data1)
		uid := []int{-1}
		for _, v := range data1 {
			uid = append(uid, v.Uid)
		}
		sess.In("uid", uid)
	}
	if arg["start_time"] != "" {
		sess.And("create_at>=?", arg["start_time"])
	}
	if arg["end_time"] != "" {
		sess.And("create_at<=?", arg["end_time"])
	}
	if arg["payment_start_time"] != "" {
		sess.And("payment_date>=?", arg["payment_start_time"])
	}
	if arg["payment_end_time"] != "" {
		sess.And("payment_date<=?", arg["payment_end_time"])
	}
	return sess
}
func GetStoreWithdrawById(sess *xorm.Session, id string) *model.CommunityTeamStoreWithdrawApply {
	var data model.CommunityTeamStoreWithdrawApply
	get, err := sess.Where("id=?", id).Get(&data)
	if get == false || err != nil {
		return nil
	}
	return &data
}
