package hdl

import (
	"applet/app/db"
	"applet/app/db/model"
	"applet/app/e"
	"applet/app/md"
	"applet/app/svc"
	"applet/app/utils"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
)

// User 用户信息
// @Summary      用户信息
// @Tags         用户信息
// @Description  用户信息
// @Accept       json
// @Produce      json
// @Success      200   {string}   				""
// @Failure      400   {object}   md.Response              	"具体错误"
// @Router       /api/v1/communityTeam/ownStore/user [GET]
func User(c *gin.Context) {
	user := svc.GetUser(c)
	third_gd_web_app_key := db.SysCfgGet(c, "third_gd_web_app_key")
	res := map[string]string{
		"head_img":             user.Profile.AvatarUrl,
		"nickname":             user.Info.Nickname,
		"phone":                user.Info.Phone,
		"state":                "0",
		"info":                 "",
		"store_type":           "0",
		"third_gd_web_app_key": third_gd_web_app_key,
	}
	storeCheck := db.StoreAuditByUid(svc.MasterDb(c), user.Info.Uid)
	if storeCheck != nil {
		if storeCheck.State == 1 {
			res["state"] = "1"
		}
		if storeCheck.State == 3 {
			res["state"] = "3"
			res["info"] = storeCheck.Info
		}
	}
	userParty := db.GetStoreIdEg(svc.MasterDb(c), utils.IntToStr(user.Info.Uid))
	if userParty != nil {
		if userParty.State == 1 {
			res["state"] = "2"
		} else if storeCheck != nil && storeCheck.State == 2 {
			res["state"] = "3"
		}
		res["store_type"] = utils.IntToStr(userParty.StoreType)
	}
	db.GetStorePayQrcodeByUId(svc.MasterDb(c), user.Info.Uid)
	e.OutSuc(c, res, nil)
	return
}
func GdKey(c *gin.Context) {
	third_gd_web_app_key := db.SysCfgGet(c, "third_gd_js_app_key")
	third_gd_web_app_secret := db.SysCfgGet(c, "third_gd_web_app_secret")
	res := map[string]string{
		"third_gd_web_app_key":    third_gd_web_app_key,
		"third_gd_web_app_secret": third_gd_web_app_secret,
	}
	e.OutSuc(c, res, nil)
	return
}

func Base(c *gin.Context) {
	user := svc.GetUser(c)
	store := db.GetStoreIdEg(svc.MasterDb(c), utils.IntToStr(user.Info.Uid))

	var res = md.CommunityTeamStore{}
	if store != nil {
		copier.Copy(&res, &store)
		res.DistrictId = utils.IntToStr(store.DistrictId)
		res.CityId = utils.IntToStr(store.CityId)
		res.WorkState = utils.IntToStr(store.WorkState)
		res.ProvinceId = utils.IntToStr(store.ProvinceId)
	}
	e.OutSuc(c, res, nil)
	return
}
func BaseSet(c *gin.Context) {
	var arg md.CommunityTeamStore
	if err := c.ShouldBindJSON(&arg); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	user := svc.GetUser(c)
	store := db.GetStoreIdEg(svc.MasterDb(c), utils.IntToStr(user.Info.Uid))
	var storeData model.CommunityTeamStore
	copier.Copy(&storeData, &arg)
	storeData.DistrictId = utils.StrToInt(arg.DistrictId)
	storeData.CityId = utils.StrToInt(arg.CityId)
	storeData.WorkState = utils.StrToInt(arg.WorkState)
	storeData.ProvinceId = utils.StrToInt(arg.ProvinceId)
	svc.MasterDb(c).Where("id=?", store.Id).Cols("lat,lng,address,work_state,name,province,city,district,timer,phone,logo,province_id,city_id,district_id").Update(&storeData)
	e.OutSuc(c, "success", nil)
	return
}
