package db

import (
	"applet/app/db/model"
	"applet/app/md"
	"applet/app/utils"
	"github.com/gin-gonic/gin"
	"xorm.io/xorm"
)

func GetOrderByOid(eg *xorm.Engine, oid string) *model.CommunityTeamOrder {
	var data model.CommunityTeamOrder
	get, err := eg.Where("oid=?", oid).Get(&data)
	if get == false || err != nil {
		return nil
	}
	return &data
}
func GetOrderEg(eg *xorm.Engine, oid string) *model.CommunityTeamOrder {
	var data model.CommunityTeamOrder
	get, err := eg.Where("oid=?", oid).Get(&data)
	if get == false || err != nil {
		return nil
	}
	return &data
}
func GetOrderPayEg(eg *xorm.Engine, oid string) *model.CommunityTeamPayOrder {
	var data model.CommunityTeamPayOrder
	get, err := eg.Where("oid=?", oid).Get(&data)
	if get == false || err != nil {
		return nil
	}
	return &data
}
func GetOrderPay(sess *xorm.Session, oid string) *model.CommunityTeamPayOrder {
	var data model.CommunityTeamPayOrder
	get, err := sess.Where("oid=?", oid).Get(&data)
	if get == false || err != nil {
		return nil
	}
	return &data
}
func GetOrder(sess *xorm.Session, oid string) *model.CommunityTeamOrder {
	var data model.CommunityTeamOrder
	get, err := sess.Where("oid=?", oid).Get(&data)
	if get == false || err != nil {
		return nil
	}
	return &data
}
func GetOrderInfo(sess *xorm.Session, oid string) *[]model.CommunityTeamOrderInfo {
	var data []model.CommunityTeamOrderInfo
	err := sess.Where("oid=?", oid).Find(&data)
	if err != nil {
		return nil
	}
	return &data
}
func GetOrderInfoAllEg(eg *xorm.Engine, oid string) *[]model.CommunityTeamOrderInfo {
	var data []model.CommunityTeamOrderInfo
	err := eg.Where("oid=?", oid).Find(&data)
	if err != nil {
		return nil
	}
	return &data
}
func GetOrderInfoFirstEg(eg *xorm.Engine, oid string) *model.CommunityTeamOrderInfo {
	var data model.CommunityTeamOrderInfo
	get, err := eg.Where("oid=?", oid).Get(&data)
	if err != nil || get == false {
		return nil
	}
	return &data
}
func GetOrderInfoEg(eg *xorm.Engine, oid string) *model.CommunityTeamOrderInfo {
	var data model.CommunityTeamOrderInfo
	get, err := eg.Where("oid=?", oid).Asc("id").Get(&data)
	if get == false || err != nil {
		return nil
	}
	return &data
}

func GetOrderList(c *gin.Context, eg *xorm.Engine, arg map[string]string) *[]model.CommunityTeamOrder {
	var data []model.CommunityTeamOrder
	sess := eg.Where("1=1")
	if arg["uid"] != "" {
		sess.And("uid=?", arg["uid"])
	}
	if arg["state"] != "" {
		sess.And("state=?", arg["state"])
	}
	if arg["store_uid"] != "" {
		sess.And("store_uid=?", arg["store_uid"])
	}
	if arg["code"] != "" {
		sess.And("code=?", arg["code"])
	}
	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
	err := sess.OrderBy("id desc").Limit(limit, start).Find(&data)
	if err != nil {
		return nil
	}
	return &data
}
func GetStoreOrderList(eg *xorm.Engine, req md.StoreOrder, parent_uid int) (*[]model.CommunityTeamOrder, int64, error) {
	var data []model.CommunityTeamOrder
	sess := eg.Where("1=1")
	if req.Oid != "" {
		sess.And("oid like ?", "%"+req.Oid+"%")
	}
	if req.StoreName != "" {
		var store []model.CommunityTeamStore
		eg.Where("name like ?", "%"+req.StoreName+"%").Find(&store)
		ids := []int{-1}
		for _, v := range store {
			ids = append(ids, v.Id)
		}
		sess.In("store_uid", ids)
	}
	if req.Phone != "" {
		var user []model.User
		eg.Where("phone like ?", "%"+req.Phone+"%").Find(&user)
		ids := []int{-1}
		for _, v := range user {
			ids = append(ids, v.Uid)
		}
		sess.In("uid", ids)
	}
	if req.Nickname != "" {
		var user []model.User
		eg.Where("nickname like ?", "%"+req.Nickname+"%").Find(&user)
		ids := []int{-1}
		for _, v := range user {
			ids = append(ids, v.Uid)
		}
		sess.In("uid", ids)
	}
	if req.StoreUid != "" {
		sess.And("store_uid=?", req.StoreUid)
	}
	if req.StoreType != "" {
		sess.And("store_type=?", req.StoreType)
	}
	if parent_uid > 0 {
		sess.And("parent_uid=?", parent_uid)
	}

	if req.StartTime != "" {
		sess.And("create_at>=?", req.StartTime)
	}
	if req.EndTime != "" {
		sess.And("create_at<=?", req.EndTime)
	}
	if req.ConfirmStartTime != "" {
		sess.And("confirm_at>=?", req.ConfirmStartTime)
	}
	if req.ConfirmEndTime != "" {
		sess.And("confirm_at<=?", req.ConfirmEndTime)
	}
	size := utils.StrToInt(req.Size)
	start := (utils.StrToInt(req.P) - 1) * size
	count, err := sess.Limit(size, start).OrderBy("create_at desc,id desc").FindAndCount(&data)
	if err != nil {
		return nil, 0, err
	}
	return &data, count, nil
}
