package db

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

func GetStoreAmount(sess *xorm.Session, storeId int, agentUid, storeType int) *model.CommunityTeamStoreAmount {
	var data model.CommunityTeamStoreAmount
	get, err := sess.Where("uid=? and parent_uid=? and store_type=?", storeId, agentUid, storeType).Get(&data)
	if get == false || err != nil {
		return nil
	}
	return &data
}
func GetStoreAmountEg(eg *xorm.Engine, storeId int, agentUid, storeType int) *model.CommunityTeamStoreAmount {
	var data model.CommunityTeamStoreAmount
	get, err := eg.Where("uid=? and parent_uid=? and store_type=?", storeId, agentUid, storeType).Get(&data)
	if get == false || err != nil {
		return nil
	}
	return &data
}
func GetStoreAmountFlowList(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["parent_uid"] != "" {
		sess.And("parent_uid=?", arg["parent_uid"])
	}
	if arg["store_type"] != "" {
		sess.And("store_type=?", arg["store_type"])
	}
	if arg["title"] != "" {
		sess.And("title like ?", "%"+arg["title"]+"%")
	}
	if arg["oid"] != "" {
		sess.And("oid like ?", "%"+arg["oid"]+"%")
	}
	if arg["start_time"] != "" {
		sess.And("create_at>=?", arg["start_time"])
	}
	if arg["end_time"] != "" {
		sess.And("create_at<=?", arg["end_time"])
	}
	limit := utils.StrToInt(arg["size"])
	start := (utils.StrToInt(arg["p"]) - 1) * limit
	count, err := sess.Desc("id").Limit(limit, start).FindAndCount(&data)
	if err != nil {
		return nil, count
	}
	return &data, count
}
