package db

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

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

func GetStore(eg *xorm.Engine, arg map[string]string) []map[string]string {
	lng := utils.StrToFloat64(arg["lng"])
	lat := utils.StrToFloat64(arg["lat"])
	str := "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) ) )"
	str = fmt.Sprintf(str, lng, lat, lng, lat, lat, lat)
	sel := ` *,%s AS km`
	sel = fmt.Sprintf(sel, str)
	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["uid"]
	} else {
		if arg["store_type"] == "3" {
			where += " and store_type in(1,2)"
		} else {
			where += " and store_type=" + arg["store_type"]
		}
	}
	if arg["store_id"] != "" {
		where += " and uid = '" + arg["store_id"] + "'"
	}
	if arg["work_state"] != "" {
		where += " and work_state = '" + arg["work_state"] + "'"
	}
	if arg["is_new"] != "" {
		where += " and is_new = '" + arg["is_new"] + "'"
	}

	if utils.StrToFloat64(arg["km"]) > 0 {
		where += " and " + str + "<=" + utils.Float64ToStr(utils.StrToFloat64(arg["km"])/1000)
	} else {
		if utils.StrToFloat64(arg["lat"]) > 0 && arg["city_id"] == "" {
			where += " and " + str + "<=1000"
		}
	}
	if arg["city"] != "" {
		where += " and city like '%" + arg["city"] + "%'"
	}
	if arg["province_id"] != "" {
		where += " and province_id = '" + arg["province_id"] + "'"
	}
	if arg["city_id"] != "" {
		where += " and city_id = '" + arg["city_id"] + "'"
	}
	if arg["district_id"] != "" {
		where += " and district_id = '" + arg["district_id"] + "'"
	}
	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"])
	str := "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) ) )"
	str = fmt.Sprintf(str, lng, lat, lng, lat, lat, lat)
	sel := ` cts.*,%s AS km`
	sel = fmt.Sprintf(sel, str)
	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 {
		if arg["store_type"] == "3" {
			where += " and cts.store_type in(1,2)"
		} else {
			where += " and cts.store_type=" + arg["store_type"]
		}
	}
	if utils.StrToFloat64(arg["km"]) > 0 {
		where += " and " + str + "<=" + utils.Float64ToStr(utils.StrToFloat64(arg["km"])/1000)
	}
	if arg["store_id"] != "" {
		where += " and cts.uid = '" + arg["store_id"] + "'"
	}
	//if arg["city"] != "" {
	//	where += " and cts.city='" + arg["city"] + "'"
	//}
	if arg["province_id"] != "" {
		where += " and cts.province_id = '" + arg["province_id"] + "'"
	}
	if arg["city_id"] != "" {
		where += " and cts.city_id = '" + arg["city_id"] + "'"
	}
	if arg["district_id"] != "" {
		where += " and cts.district_id = '" + arg["district_id"] + "'"
	}
	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
}
