package db

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

func GetStore(eg *xorm.Engine, arg map[string]string) []map[string]string {
	lng := utils.StrToFloat64(arg["lng"])
	lat := utils.StrToFloat64(arg["lat"])
	sel := ` *,sqrt( ( (( %f - lng)*PI()*12656*cos((( %f +lat)/2)*PI()/180)/180) * (( %f - lng)*PI()*12656*cos (((%f+lat)/2)*PI()/180)/180) ) + ( ((%f-lat)*PI()*12656/180) * ((%f-lat)*PI()*12656/180) ) ) AS km`
	sel = fmt.Sprintf(sel, lng, lat, lng, lat, lat, lat)
	sql := `select %s from community_team_store where %s %s`
	where := "state=1"
	if arg["parent_uid"] != "" {
		where += " and store_type=2 and parent_uid=" + arg["parent_uid"]
	} else if arg["uid"] != "" {
		where += " and store_type=1 and uid=" + arg["parent_uid"]
	} else {
		where += " and store_type=" + arg["store_type"]
	}
	if arg["city"] != "" {
		where += " and address like '%" + arg["city"] + "%'"
	}
	if arg["name"] != "" {
		where += " and name like '%" + arg["name"] + "'"
	}
	start := (utils.StrToInt(arg["p"]) - 1) * utils.StrToInt(arg["size"])
	group := " order by %s limit " + utils.IntToStr(start) + "," + arg["size"]
	groupStr := "fan desc,km asc,id asc"
	if arg["cid"] == "1" {
		groupStr = "km asc,id asc"
	}
	group = fmt.Sprintf(group, groupStr)
	sql = fmt.Sprintf(sql, sel, where, group)
	fmt.Println(sql)
	nativeString, _ := QueryNativeString(eg, sql)
	return nativeString
}
func GetStoreLike(eg *xorm.Engine, arg map[string]string) []map[string]string {
	lng := utils.StrToFloat64(arg["lng"])
	lat := utils.StrToFloat64(arg["lat"])
	sel := ` cts.*,sqrt( ( (( %f - cts.lng)*PI()*12656*cos((( %f +cts.lat)/2)*PI()/180)/180) * (( %f - cts.lng)*PI()*12656*cos (((%f+cts.lat)/2)*PI()/180)/180) ) + ( ((%f-cts.lat)*PI()*12656/180) * ((%f-cts.lat)*PI()*12656/180) ) ) AS km`
	sel = fmt.Sprintf(sel, lng, lat, lng, lat, lat, lat)
	sql := `select %s from community_team_store_like ctsl
	left join community_team_store cts on ctsl.store_id=cts.id
where %s %s`
	where := "cts.state=1"
	if arg["parent_uid"] != "" {
		where += " and cts.store_type=2 and cts.parent_uid=" + arg["parent_uid"]
	} else if arg["uid"] != "" {
		where += " and cts.store_type=1 and cts.uid=" + arg["parent_uid"]
	} else {
		where += " and cts.store_type=" + arg["store_type"]
	}
	if arg["city"] != "" {
		where += " and cts.city='" + arg["city"] + "'"
	}
	if arg["name"] != "" {
		where += " and cts.name like '%" + arg["name"] + "'"
	}
	start := (utils.StrToInt(arg["p"]) - 1) * utils.StrToInt(arg["size"])
	group := " order by km asc,cts.id asc limit " + utils.IntToStr(start) + "," + arg["size"]
	sql = fmt.Sprintf(sql, sel, where, group)
	fmt.Println(sql)
	nativeString, _ := QueryNativeString(eg, sql)
	return nativeString
}
func GetStoreId(sess *xorm.Session, id string) *model.CommunityTeamStore {
	var data model.CommunityTeamStore
	get, err := sess.Where("uid=?", id).Get(&data)
	if get == false || err != nil {
		return nil
	}
	return &data
}

func GetStoreIdEg(eg *xorm.Engine, id string) *model.CommunityTeamStore {
	var data model.CommunityTeamStore
	get, err := eg.Where("uid=?", id).Get(&data)
	if get == false || err != nil {
		return nil
	}
	return &data
}
