package db

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

func GetCoupon(eg *xorm.Engine, req map[string]string) *[]model.CommunityTeamCoupon {
	var data []model.CommunityTeamCoupon
	limit := 10
	start := (utils.StrToInt(req["p"]) - 1) * limit
	sess := eg.Where("store_type=0 and num>0 and state=1 and ((valid_time_end>? and valid_time_type=1) or valid_time_type=2)", time.Now().Format("2006-01-02 15:04:05")).OrderBy("sort desc,id desc").Limit(limit, start)
	if req["name"] != "" {
		sess.And("name like ?", "%"+req["name"]+"%")
	}
	if req["start_time"] != "" {
		sess.And("created_time>=?", req["start_time"])
	}
	if req["end_time"] != "" {
		sess.And("created_time<=?", req["end_time"])
	}
	err := sess.Find(&data)
	if err != nil {
		return nil
	}
	return &data
}
func GetCouponById(eg *xorm.Engine, id string) *model.CommunityTeamCoupon {
	var data model.CommunityTeamCoupon
	get, err := eg.Where("id=?", id).Get(&data)
	if get == false || err != nil {
		return nil
	}
	return &data
}
