package svc

import (
	"applet/app/db"
	"applet/app/db/model"
	"applet/app/e"
	"applet/app/utils"
	"github.com/gin-gonic/gin"
	"time"
)

func BankStore(c *gin.Context) {
	var arg map[string]string
	if err := c.ShouldBindJSON(&arg); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	arg["store_type"] = "0"
	user, _ := GetDefaultUser(c, c.GetHeader("Authorization"))
	var store = make([]map[string]string, 0)
	if arg["cid"] == "2" {
		store = db.GetStoreLike(MasterDb(c), arg)
	} else {
		store = db.GetStore(MasterDb(c), arg)
	}
	storeList := make([]map[string]interface{}, 0)
	for _, v := range store {
		km := v["km"]
		if utils.StrToFloat64(v["km"]) < 1 {
			v["km"] = utils.IntToStr(int(utils.StrToFloat64(v["km"])*1000)) + "m"
		} else {
			v["km"] = GetCommissionPrec(c, v["km"], "2", "1")
			v["km"] += "km"
		}
		if utils.StrToFloat64(km) <= 0 || utils.StrToFloat64(v["lat"]) == 0 || utils.StrToFloat64(v["lng"]) == 0 {
			v["km"] = "-"
		}
		tmp := map[string]interface{}{
			"lat":      v["lat"],
			"lng":      v["lng"],
			"address":  v["address"],
			"name":     v["name"],
			"id":       v["id"],
			"km":       v["km"],
			"time_str": v["timer"],
			"uid":      v["uid"],
			"phone":    v["phone"],
			"logo":     v["logo"],
			"is_like":  "0",
			"fan":      "",
		}
		if user != nil {
			count, _ := MasterDb(c).Where("uid=? and store_id=?", user.Info.Uid, v["id"]).Count(&model.CommunityTeamStoreLike{})
			if count > 0 {
				tmp["is_like"] = "1"
			}
		}
		storeList = append(storeList, tmp)
	}
	e.OutSuc(c, storeList, nil)
	return
}
func Store(c *gin.Context) {
	var arg map[string]string
	if err := c.ShouldBindJSON(&arg); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	arg["store_type"] = "0"

	user, _ := GetDefaultUser(c, c.GetHeader("Authorization"))
	store := db.GetStore(MasterDb(c), arg)
	storeList := make([]map[string]interface{}, 0)
	for _, v := range store {
		km := v["km"]
		if utils.StrToFloat64(v["km"]) < 1 {
			v["km"] = utils.IntToStr(int(utils.StrToFloat64(v["km"])*1000)) + "m"
		} else {
			v["km"] = GetCommissionPrec(c, v["km"], "2", "1")
			v["km"] += "km"
		}
		if utils.StrToFloat64(km) <= 0 || utils.StrToFloat64(v["lat"]) == 0 || utils.StrToFloat64(v["lng"]) == 0 {
			v["km"] = "-"
		}
		label := make([]string, 0)
		tmp := map[string]interface{}{
			"lat":        v["lat"],
			"lng":        v["lng"],
			"address":    v["address"],
			"work_state": v["work_state"],
			"name":       v["name"],
			"id":         v["id"],
			"uid":        v["uid"],
			"km":         v["km"],
			"time_str":   v["timer"],
			"phone":      v["phone"],
			"label":      label,
			"is_like":    "0",
		}
		if user != nil {
			count, _ := MasterDb(c).Where("uid=? and store_id=?", user.Info.Uid, v["id"]).Count(&model.CommunityTeamStoreLike{})
			if count > 0 {
				tmp["is_like"] = "1"
			}
		}

		storeList = append(storeList, tmp)
	}
	e.OutSuc(c, storeList, nil)
	return
}
func StoreAddLike(c *gin.Context) {
	var arg map[string]string
	if err := c.ShouldBindJSON(&arg); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	user := GetUser(c)
	count, _ := MasterDb(c).Where("uid=? and store_id=?", user.Info.Uid, arg["id"]).Count(&model.CommunityTeamStoreLike{})
	if count > 0 {
		e.OutErr(c, 400, e.NewErr(400, "已收藏"))
		return
	}
	var data = model.CommunityTeamStoreLike{
		Uid:     user.Info.Uid,
		StoreId: utils.StrToInt(arg["id"]),
		Time:    time.Now(),
	}
	MasterDb(c).Insert(&data)
	e.OutSuc(c, "success", nil)
	return
}

func StoreCancelLike(c *gin.Context) {
	var arg map[string]string
	if err := c.ShouldBindJSON(&arg); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	user := GetUser(c)
	MasterDb(c).Where("uid=? and store_id=?", user.Info.Uid, arg["id"]).Delete(&model.CommunityTeamStoreLike{})
	e.OutSuc(c, "success", nil)
	return
}
