package hdl

import (
	"applet/app/db"
	"applet/app/db/model"
	"applet/app/e"
	"applet/app/md"
	"applet/app/svc"
	"applet/app/utils"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/tidwall/gjson"
	"time"
)

func StyleStoreScan(c *gin.Context) {
	Db := svc.MasterDb(c)
	skipIdentifier := "pub.flutter.community_team_store_scan_ord"
	moduleCfg, err := db.SysModFindBySkipIdentifier(c, Db, skipIdentifier)
	if err != nil {
		e.OutErr(c, e.ERR_FILE_SAVE, nil)
		return
	}
	var O2OScanOrdModule md.ModuleByO2oCByScanOrdData
	if moduleCfg != nil {
		bytes := utils.MarshalJSONCamelCase2JsonSnakeCase(moduleCfg.Data)
		utils.Unserialize(bytes, &O2OScanOrdModule)
		e.OutSuc(c, &O2OScanOrdModule, nil)
		return
	}
	setting := md.InitO2OScanOrdModule
	utils.Unserialize([]byte(setting), &O2OScanOrdModule)
	e.OutSuc(c, &O2OScanOrdModule, nil)
	return
}
func StoreScan(c *gin.Context) {
	svc.StoreScan(c)
}
func StorePayScan(c *gin.Context) {
	svc.StorePayScan(c)
}
func StorePayInfo(c *gin.Context) {
	svc.StorePayInfo(c)
}
func City(c *gin.Context) {
	var arg = make(map[string]string)
	c.ShouldBindJSON(&arg)
	sql := `select IF(city.name='省直辖县级行政区划' or city.name='市辖区',province.name,city.name) as newname from city
LEFT JOIN province on province.id=city.province_id
where %s
order by CONVERT(newname USING gbk)`
	str := "1=1"
	if arg["name"] != "" {
		str += " and newname like '%" + arg["name"] + "%'"
	}
	sql = fmt.Sprintf(sql, str)
	nativeString, _ := db.QueryNativeString(svc.MasterDb(c), sql)
	nodeList := make([]map[string]string, 0)
	for _, item := range nativeString {
		tmp := map[string]string{
			"name": item["newname"],
		}
		nodeList = append(nodeList, tmp)
	}
	e.OutSuc(c, nodeList, nil)
	return
}
func BankStoreCate(c *gin.Context) {
	var res = []map[string]string{
		{"name": "全部网点", "value": ""},
		{"name": "附近网点", "value": "1"},
		{"name": "关注网点", "value": "2"},
	}
	e.OutSuc(c, res, nil)
	return
}
func BankStoreNewCate(c *gin.Context) {
	second := []map[string]string{
		{"type": "km", "value": "500", "name": "附近 500m"},
		{"type": "km", "value": "1000", "name": "附近 1km"},
		{"type": "km", "value": "2000", "name": "附近 2km"},
		{"type": "km", "value": "5000", "name": "附近 5km"},
	}
	var res = []map[string]interface{}{
		{"name": "附近网点", "type": "km", "value": "", "has_second": "1", "second": second},
		{"name": "营业中", "type": "work_state", "value": "0", "has_second": "0", "second": []map[string]string{}},
		{"name": "新网点", "type": "is_new", "value": "1", "has_second": "0", "second": []map[string]string{}},
		{"name": "关注网点", "type": "is_like", "value": "1", "has_second": "0", "second": []map[string]string{}},
	}
	e.OutSuc(c, res, nil)
	return
}
func BankStore(c *gin.Context) {
	svc.BankStore(c)
}
func BankActivity(c *gin.Context) {
	svc.BankActivity(c)
}
func BankCard(c *gin.Context) {
	svc.BankCard(c)
}
func BankCardDetail(c *gin.Context) {
	svc.BankCardDetail(c)
}
func BankNotice(c *gin.Context) {
	svc.BankNotice(c)
}
func BankUserInfo(c *gin.Context) {
	user := svc.GetUser(c)
	res := map[string]string{
		"head_img":   user.Profile.AvatarUrl,
		"nickname":   user.Info.Nickname,
		"phone":      user.Info.Phone,
		"level_name": user.Level.LevelName,
		"amount":     svc.GetCommissionPrec(c, user.Profile.FinValid, "2", "1"),
	}
	e.OutSuc(c, res, nil)
	return
}
func NewStoreCate(c *gin.Context) {
	var res = []map[string]string{
		{"name": "全部店铺", "value": ""},
		{"name": "附近店铺", "value": "1"},
		{"name": "关注店铺", "value": "2"},
	}
	e.OutSuc(c, res, nil)
	return
}
func NewStore(c *gin.Context) {
	svc.NewStore(c)
}
func Store(c *gin.Context) {
	svc.Store(c)
}
func StoreLike(c *gin.Context) {
	svc.StoreLike(c)
}
func StoreAddLike(c *gin.Context) {
	svc.StoreAddLike(c)
}
func StoreCancelLike(c *gin.Context) {
	svc.StoreCancelLike(c)
}
func User(c *gin.Context) {
	user, _ := svc.GetDefaultUser(c, c.GetHeader("Authorization"))
	res := map[string]string{
		"head_img":     "",
		"nickname":     "",
		"coupon_str":   "优惠券:",
		"coupon":       "0",
		"integral_str": "积分:",
		"integral":     "0",
	}
	if user != nil && user.Info.Uid > 0 {
		res["head_img"] = user.Profile.AvatarUrl
		res["nickname"] = user.Info.Nickname
		now := time.Now().Format("2006-01-02 15:04:05")
		count, _ := svc.MasterDb(c).Table("act_coupon_user").
			Where("store_type=? and uid = ? AND is_use = ? AND (valid_time_start < ? AND valid_time_end > ?)", 0,
				user.Info.Uid, 0, now, now).Count(&model.CommunityTeamCouponUser{})
		res["coupon"] = utils.Int64ToStr(count)
		mod, _ := db.SysModFindBySkipIdentifier(c, svc.MasterDb(c), "pub.flutter.community_team_store_index")
		if mod != nil {
			integralCoinId := gjson.Get(mod.Data, "integralCoinId").String()
			if utils.StrToInt(integralCoinId) > 0 {
				coin, _ := db.VirtualCoinGetOneById(svc.MasterDb(c), integralCoinId)
				if coin != nil {
					amount, _ := db.GetUserVirtualAmountOneEg(svc.MasterDb(c), user.Info.Uid, utils.StrToInt(integralCoinId))
					res["integral_str"] = coin.Name + ":"
					if amount != nil {
						res["integral"] = svc.GetCommissionPrec(c, amount.Amount, svc.SysCfgGet(c, "integral_prec"), "0")
					}
				}
			}
		}
	}
	e.OutSuc(c, res, nil)
	return
}
