package db

import (
	"applet/app/db/model"
	"applet/app/e"
	"applet/app/utils"
	"applet/app/utils/logx"
	"errors"
	"fmt"
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
	"github.com/tidwall/gjson"
	"xorm.io/xorm"
)

// 返回所有, 不管是否显示
func SysModFindAll(Db *xorm.Engine) (*[]model.SysModule, error) {
	var m []model.SysModule
	if err := Db.Find(&m); err != nil {
		return nil, err
	}
	return &m, nil
}

// 查找主模块数据
func SysModFindMain(Db *xorm.Engine) (*[]model.SysModule, error) {
	var m []model.SysModule
	if err := Db.Where("mod_pid = 0 AND state = 1 AND position = 'base'").
		Asc("sort").
		Find(&m); err != nil {
		return nil, err
	}
	return &m, nil
}

// 用父ID查找子模块数据
func SysModFindByPId(c *gin.Context, Db *xorm.Engine, id int) (*[]model.SysModule, error) {
	var m []model.SysModule
	if err := Db.Where("state = 1").Where("mod_pid = ?", id).
		Asc("sort").
		Find(&m); err != nil {
		return nil, err
	}
	mm, err := sysModFormat(c, &m)
	if err != nil {
		return nil, err
	}
	var ms []model.SysModule
	modname_list := []string{"product", "search_result_taobao_item", "hot_rank_tab_view"}
	for _, item := range *mm.(*[]model.SysModule) {
		if item.ModName == "product_detail_title" {
			if strings.Contains(item.Data, "tmall") == false {
				item.Data = strings.Replace(item.Data, "\"platform_css\":[", "\"platform_css\":[{\"name\":\"天猫\",\"type\":\"tmall\",\"text_color\":\"#FFFFFF\",\"bg_color\":\"#FF4242\"},", 1)
			}
			if strings.Contains(item.Data, "kuaishou") == false {
				item.Data = strings.Replace(item.Data, "\"platform_css\":[", "\"platform_css\":[{\"name\":\"快手\",\"type\":\"kuaishou\",\"text_color\":\"#FFFFFF\",\"bg_color\":\"#FF4242\"},", 1)
			}
			if strings.Contains(item.Data, "tikTok") == false {
				item.Data = strings.Replace(item.Data, "\"platform_css\":[", "\"platform_css\":[{\"name\":\"抖音\",\"type\":\"tikTok\",\"text_color\":\"#FFFFFF\",\"bg_color\":\"#FF4242\"},", 1)
			}
		}

		if strings.Contains(item.Data, "tmall") == false && utils.InArr(item.ModName, modname_list) {
			item.Data = strings.Replace(item.Data, "{\"index\":\"6\",\"type\":\"kaola\",\"platform_name\":\"考拉\",\"provider_name_color\":\"#FFFFFF\",\"provider_bg_color\":\"#FF4242\"}", "{\"index\":\"6\",\"type\":\"kaola\",\"platform_name\":\"考拉\",\"provider_name_color\":\"#FFFFFF\",\"provider_bg_color\":\"#FF4242\"},{\"index\":\"7\",\"type\":\"tmall\",\"platform_name\":\"天猫\",\"provider_name_color\":\"#FFFFFF\",\"provider_bg_color\":\"#FF4242\"}", 1)
			item.Data = strings.Replace(item.Data, "{\"type\":\"kaola\",\"provider_name_color\":\"#FFFFFF\",\"provider_bg_color\":\"#FF4242\"}", "{\"type\":\"kaola\",\"provider_name_color\":\"#FFFFFF\",\"provider_bg_color\":\"#FF4242\"},{\"type\":\"tmall\",\"provider_name_color\":\"#FFFFFF\",\"provider_bg_color\":\"#FF4242\"}", 1)
		}
		if strings.Contains(item.Data, "优惠卷") {
			item.Data = strings.Replace(item.Data, "优惠卷", "优惠券", -1)
		}
		item.Data = strings.ReplaceAll(item.Data, "\\/", "/")
		item.Data = strings.ReplaceAll(item.Data, "\\u0026", "&")

		ms = append(ms, item)
	}
	return &ms, nil
}

// 用父ID查找子模块数据
func SysModFindByPIds(c *gin.Context, Db *xorm.Engine, ids ...int) (*[]model.SysModule, error) {
	var m []model.SysModule
	if err := Db.In("mod_pid", ids).Where("state = 1").
		Asc("sort").
		Find(&m); err != nil {
		return nil, err
	}
	mm, err := sysModFormat(c, &m)
	if err != nil {
		return nil, err
	}
	var ms []model.SysModule
	for _, item := range *mm.(*[]model.SysModule) {
		//数据里面
		if strings.Contains(item.Data, "tmall") == false && item.ModName == "product" {
			item.Data = strings.Replace(item.Data, "{\"index\":\"6\",\"type\":\"kaola\",\"platform_name\":\"考拉\",\"provider_name_color\":\"#FFFFFF\",\"provider_bg_color\":\"#FF4242\"}", "{\"index\":\"6\",\"type\":\"kaola\",\"platform_name\":\"考拉\",\"provider_name_color\":\"#FFFFFF\",\"provider_bg_color\":\"#FF4242\"},{\"index\":\"7\",\"type\":\"tmall\",\"platform_name\":\"天猫\",\"provider_name_color\":\"#FFFFFF\",\"provider_bg_color\":\"#FF4242\"}", 1)
		}
		item = SysModDataByReplace(c, item)
		ms = append(ms, item)
	}

	return &ms, nil
}

// 用IDS找对应模块数据
func SysModFindByIds(Db *xorm.Engine, ids ...int) (*[]model.SysModule, error) {
	var m []model.SysModule
	if err := Db.In("mod_id", ids).Where("state = 1").
		Cols("mod_id,mod_pid,mod_name,position,skip_identifier,title,subtitle,url,margin,aspect_ratio,icon,img,font_color,bg_img,bg_color,bg_color_t,badge,path,data,sort").
		Asc("sort").Find(&m); err != nil {
		return nil, err
	}

	return &m, nil
}

// ID查找对应模块
func SysModFindById(c *gin.Context, Db *xorm.Engine, id string) (*model.SysModule, error) {
	var m model.SysModule
	if has, err := Db.Where("state = 1 AND mod_id = ?", id).
		Get(&m); err != nil || has == false {
		return nil, logx.Warn(err)
	}
	mm, err := sysModFormat(c, &m)
	if err != nil {
		return nil, err
	}

	return mm.(*model.SysModule), nil
}

// SysModFindByTmpId is 根据模板
func SysModFindByTmpId(c *gin.Context, Db *xorm.Engine, id string) (*model.SysModule, error) {
	var m model.SysModule
	if has, err := Db.Where("state = 1 AND template_id = ?", id).
		Get(&m); err != nil || has == false {
		return nil, logx.Warn(err)
	}
	mm, err := sysModFormat(c, &m)
	if err != nil {
		return nil, err
	}
	return mm.(*model.SysModule), nil
}

// Name查找对应模块
func SysModFindByName(c *gin.Context, Db *xorm.Engine, name string) (*model.SysModule, error) {
	var m model.SysModule
	if has, err := Db.Where("state = 1 AND mod_name = ?", name).
		Get(&m); err != nil || has == false {
		return nil, logx.Warn(err)
	}
	mm, err := sysModFormat(c, &m)
	if err != nil {
		return nil, err
	}
	return mm.(*model.SysModule), nil
}

// SysModFindByName is Name查找对应模块
func SysModFindByNames(names ...string) (*[]model.SysModule, error) {
	var m []model.SysModule
	if err := Db.In("mod_name", names).Where("state = 1").
		Cols("mod_id,mod_pid,mod_name,position,skip_identifier,title,subtitle,url,margin,aspect_ratio,icon,img,font_color,bg_img,bg_color,bg_color_t,badge,path,data,sort").
		Find(&m); err != nil {
		return nil, err
	}
	return &m, nil
}

// SysModFindByPosition is 根据位置查找对应模块
func SysModFindByPosition(Db *xorm.Engine, positions ...string) (*[]model.SysModule, error) {
	var m []model.SysModule
	if err := Db.In("position", positions).Where("state = 1").Find(&m); err != nil {
		return nil, err
	}
	return &m, nil
}

// 根据跳转标识 查找对应模块
func SysModFindBySkipIdentifier(c *gin.Context, Db *xorm.Engine, name string) (*model.SysModule, error) {
	var m model.SysModule
	if has, err := Db.Where("state = 1 AND skip_identifier = ?", name).
		Get(&m); err != nil || has == false {
		return nil, logx.Warn(err)
	}
	mm, err := sysModFormat(c, &m)
	if err != nil {
		return nil, err
	}
	return mm.(*model.SysModule), nil
}

func SysModFindBySkipIdentifierWithUid(c *gin.Context, Db *xorm.Engine, name string, uid int) (*model.SysModule, error) {
	var m model.SysModule
	if has, err := Db.Where("state = 1 AND skip_identifier = ? and uid=?", name, uid).
		Get(&m); err != nil || has == false {
		return nil, logx.Warn(err)
	}
	mm, err := sysModFormat(c, &m)
	if err != nil {
		return nil, err
	}
	return mm.(*model.SysModule), nil
}

// 根据跳转标识和位置 查找对应模块list
func SysModFindBySkipIdentifierAndPosition(c *gin.Context, Db *xorm.Engine, name string, position string) (*[]model.SysModule, error) {
	var m []model.SysModule
	if err := Db.Where("state = 1 AND skip_identifier = ? AND position = ?", name, position).
		Cols("mod_id,mod_pid,mod_name,position,skip_identifier,title,subtitle,url,margin,aspect_ratio,icon,img,font_color,bg_img,bg_color,bg_color_t,badge,path,data,sort").
		Asc("sort").Find(&m); err != nil {
		return nil, err
	}
	mm, err := sysModFormat(c, &m)
	if err != nil {
		return nil, err
	}
	return mm.(*[]model.SysModule), nil
}
func SysModFindByTempId(Db *xorm.Engine, ids []int) (*[]model.SysModule, error) {
	var m []model.SysModule
	if err := Db.Where("state = 1 ").In("template_id", ids).Asc("sort").Find(&m); err != nil {
		return nil, err
	}
	return &m, nil
}

// SysModFindByTemplateIDAndSkip is 根据模板id 查找对应模块
func SysModFindByTemplateIDAndSkip(Db *xorm.Engine, id interface{}, skip string) (*model.SysModule, error) {
	var m model.SysModule
	if has, err := Db.Where("state = 1 AND template_id = ? AND skip_identifier = ?", id, skip).
		Get(&m); err != nil || has == false {
		return nil, logx.Warn(err)
	}
	return &m, nil
}

// SysModFindByTemplateIDAndPID is 根据模板id 和pid =0 查找父模块
func SysModFindByTemplateIDAndPID(Db *xorm.Engine, id interface{}, pid interface{}) (*model.SysModule, error) {
	var m model.SysModule
	if has, err := Db.Where("state = 1 AND template_id = ? AND mod_pid = ?", id, pid).
		Get(&m); err != nil || has == false {
		return nil, logx.Warn(err)
	}

	return &m, nil
}

// SysModFindByTemplateIDAndModName is 根据模板id 和mod name 查找模块
func SysModFindByTemplateIDAndModName(Db *xorm.Engine, id interface{}, modName string) (*model.SysModule, error) {
	var m model.SysModule
	if has, err := Db.Where("state = 1 AND template_id = ? AND mod_name = ?", id, modName).
		Get(&m); err != nil || has == false {
		return nil, logx.Warn(err)
	}
	return &m, nil
}

func SysModFindByTemplateIDAndModNameWithIds(Db *xorm.Engine, ids []int, modName string) (*model.SysModule, error) {
	var m model.SysModule
	if has, err := Db.In("template_id", ids).And(" mod_name = ?", modName).
		Get(&m); err != nil || has == false {
		return nil, logx.Warn(err)
	}
	return &m, nil
}

// SysModFindNavIsUsed 查找正在使用的底部导航栏模板
func SysModFindNavIsUsedByPlatform(c *gin.Context, Db *xorm.Engine, platform string) (*model.SysModule, error) {
	var (
		tm model.SysTemplate
		m  model.SysModule
	)

	mid := c.GetString("mid")
	fmt.Println("===================================app_type", c.GetString("app_type"))
	if c.GetString("app_type") != "" && c.GetString("app_type") != "daogou" {
		var (
			tempType string
		)
		switch c.GetString("app_type") {
		case "o2o":
			tempType = "o2o_store_bottomNav"

		}

		switch platform {
		case "ios":
			if has, err := Db.Where("is_use = 1 AND type = ? AND platform = 2 ", tempType).
				Cols("id,uid,name,is_use,is_system").
				Get(&tm); err != nil || has == false {
				return nil, logx.Warn(err)
			}
			appVersion := GetCloudBuild(c, platform)
			if c.GetHeader("app_version_name") == appVersion && c.GetHeader("app_version_name") != "" {
				m, err := GetCloudBundleByVersion(Db, appVersion, 2, 1)
				if err != nil {
					return nil, logx.Warn(err)
				}
				tm.Id = int(gjson.Get(m.TemplateDuringAudit, "bottom").Int())
			}
		case "android":
			has, err := Db.Where("is_use = 1 AND type = ? AND platform = 2 ", tempType).Cols("id,uid,name,is_use,is_system").Get(&tm)
			if err != nil || has == false {
				return nil, logx.Warn(err)
			}
			fmt.Println("===================================app_type", tm)

			appVersion := GetCloudBuild(c, platform)
			fmt.Println("===================================app_type", appVersion)

			if appVersion != "" && c.GetHeader("app_version_name") == appVersion {
				m, err := GetCloudBundleByVersion(Db, appVersion, 1, 1)
				if err != nil {
					return nil, logx.Warn(err)
				}
				fmt.Println("===================================app_type", m)

				tm.Id = int(gjson.Get(m.TemplateDuringAudit, "bottom").Int())
			}
		case "wx_applet", "wap":
			if has, err := Db.Where("is_use = 1 AND type = ? AND platform = 4 ", tempType).
				Cols("id,uid,name,is_use,is_system").
				Get(&tm); err != nil || has == false {
				return nil, logx.Warn(err)
			}
		case "baidu_applet":
			if has, err := Db.Where("is_use = 1 AND type = ? AND platform = 4 ", tempType).
				Cols("id,uid,name,is_use,is_system").
				Get(&tm); err != nil || has == false {
				return nil, logx.Warn(err)
			}
		case "toutiao_applet":
			if has, err := Db.Where("is_use = 1 AND type = ? AND platform = 4 ", tempType).
				Cols("id,uid,name,is_use,is_system").
				Get(&tm); err != nil || has == false {
				return nil, logx.Warn(err)
			}
		case "alipay_applet":
			if has, err := Db.Where("is_use = 1 AND type = ? AND platform = 4 ", tempType).
				Cols("id,uid,name,is_use,is_system").
				Get(&tm); err != nil || has == false {
				return nil, logx.Warn(err)
			}
		default:
			return &m, errors.New("Platform not support")
		}
		if has, err := Db.Where("state = 1 AND template_id = ?", tm.Id).
			Get(&m); err != nil || has == false {
			return nil, logx.Warn(err)
		}

		fmt.Println("===================================app_type", m)
		if tempType == "o2o_store_bottomNav" {
			bottomMap := make(map[string]interface{})
			utils.Unserialize([]byte(m.Data), &bottomMap)
			list, ok := bottomMap["list"]
			if ok {
				m.Data = string(utils.MarshalJSONCamelCase2JsonSnakeCase(utils.SerializeStr(list)))
			}
		}
		mm, err := sysModFormat(c, &m)
		if err != nil {
			return nil, err
		}
		return mm.(*model.SysModule), nil
	}
	switch platform {
	case "ios":
		if has, err := Db.Where("is_use = 1 AND type = 'bottom' AND platform = 2 ").
			Cols("id,uid,name,is_use,is_system").
			Get(&tm); err != nil || has == false {
			return nil, logx.Warn(err)
		}
		appVersion := GetCloudBuild(c, platform)
		if c.GetHeader("app_version_name") == appVersion && c.GetHeader("app_version_name") != "" {
			m, err := GetCloudBundleByVersion(Db, appVersion, 2, 0)
			if err != nil {
				return nil, logx.Warn(err)
			}
			tm.Id = int(gjson.Get(m.TemplateDuringAudit, "bottom").Int())
		}

		if has, err := Db.Where("state = 1 AND template_id = ? AND mod_name = 'bottom_nav'", tm.Id).
			Get(&m); err != nil || has == false {
			return nil, logx.Warn(err)
		}
		mm, err := sysModFormat(c, &m)
		if err != nil {
			return nil, err
		}
		return mm.(*model.SysModule), nil
	case "android":
		has, err := Db.Where("is_use = 1 AND type = 'bottom' AND platform = 2 ").Cols("id,uid,name,is_use,is_system").Get(&tm)
		if err != nil || has == false {
			return nil, logx.Warn(err)
		}
		appVersion := GetCloudBuild(c, platform)
		if appVersion != "" && c.GetHeader("app_version_name") == appVersion {
			m, err := GetCloudBundleByVersion(Db, appVersion, 1, 0)
			if err != nil {
				return nil, logx.Warn(err)
			}
			tm.Id = int(gjson.Get(m.TemplateDuringAudit, "bottom").Int())
		}
		if has, err := Db.Where("state = 1 AND template_id = ? AND mod_name = 'bottom_nav'", tm.Id).
			Get(&m); err != nil || has == false {
			return nil, logx.Warn(err)
		}
		mm, err := sysModFormat(c, &m)
		if err != nil {
			return nil, err
		}
		return mm.(*model.SysModule), nil
	case "wx_applet", "wap":
		wxAppletCfg := GetAppletKey(c, Db)
		id := utils.StrToInt(wxAppletCfg["bottom_nav_css_id"])
		if id == 0 {
			return nil, e.NewErr(400, "找不到模板配置")
		}
		if has, err := Db.Where("state = 1 AND template_id = ? AND mod_name = 'bottom_nav'", id).
			Get(&m); err != nil || has == false {
			return nil, logx.Warn(err)
		}
		mm, err := sysModFormat(c, &m)
		if err != nil {
			return nil, err
		}
		return mm.(*model.SysModule), nil
	case "baidu_applet":
		if has, err := Db.Where("is_use = 1 AND type = 'bottom' AND platform = 4 ").
			Cols("id,uid,name,is_use,is_system").
			Get(&tm); err != nil || has == false {
			return nil, logx.Warn(err)
		}
		appVersion := SysCfgGetWithDb(Db, mid, "baidu_audit_version")

		if appVersion != "" && c.GetHeader("app_version_name") == appVersion {
			m := SysCfgGetWithDb(Db, mid, "baidu_audit_template")
			if m == "" {
				return nil, e.NewErr(400, "找不到模板配置")
			}
			tm.Id = int(gjson.Get(m, "bottom").Int())
		}
		if has, err := Db.Where("state = 1 AND template_id = ? AND mod_name = 'bottom_nav'", tm.Id).
			Get(&m); err != nil || has == false {
			return nil, logx.Warn(err)
		}
		mm, err := sysModFormat(c, &m)
		if err != nil {
			return nil, err
		}
		return mm.(*model.SysModule), nil
	case "toutiao_applet":
		if has, err := Db.Where("is_use = 1 AND type = 'bottom' AND platform = 4 ").
			Cols("id,uid,name,is_use,is_system").
			Get(&tm); err != nil || has == false {
			return nil, logx.Warn(err)
		}
		appVersion := SysCfgGetWithDb(Db, mid, "tt_audit_version")
		if appVersion != "" && c.GetHeader("app_version_name") == appVersion {
			m := SysCfgGetWithDb(Db, mid, "tt_audit_template")
			if m == "" {
				return nil, errors.New("找不到模板配置")
			}
			tm.Id = int(gjson.Get(m, "bottom").Int())
		}
		if has, err := Db.Where("state = 1 AND template_id = ? AND mod_name = 'bottom_nav'", tm.Id).
			Get(&m); err != nil || has == false {
			return nil, logx.Warn(err)
		}
		mm, err := sysModFormat(c, &m)
		if err != nil {
			return nil, err
		}
		return mm.(*model.SysModule), nil
	case "alipay_applet":
		if has, err := Db.Where("is_use = 1 AND type = 'bottom' AND platform = 4 ").
			Cols("id,uid,name,is_use,is_system").
			Get(&tm); err != nil || has == false {
			return nil, logx.Warn(err)
		}
		appVersion := SysCfgGetWithDb(Db, mid, "zfb_audit_version")
		if appVersion != "" && c.GetHeader("app_version_name") == appVersion {
			m := SysCfgGetWithDb(Db, mid, "zfb_audit_template")
			if m == "" {
				return nil, errors.New("找不到模板配置")
			}
			tm.Id = int(gjson.Get(m, "bottom").Int())
		}
		if has, err := Db.Where("state = 1 AND template_id = ? AND mod_name = 'bottom_nav'", tm.Id).
			Get(&m); err != nil || has == false {
			return nil, logx.Warn(err)
		}
		mm, err := sysModFormat(c, &m)
		if err != nil {
			return nil, err
		}
		return mm.(*model.SysModule), nil
	default:
		return &m, errors.New("Platform not support")
	}

}

// SysModFindBySkipIdentifierAndModName is 根据mod_name和位置 查找对应模块
func SysModFindBySkipIdentifierAndModName(c *gin.Context, Db *xorm.Engine, name string, modName string) (*model.SysModule, error) {
	var m model.SysModule
	if has, err := Db.Where("state = 1 AND skip_identifier = ? AND mod_name = ?", name, modName).Get(&m); err != nil || !has {
		return nil, logx.Warn(err)
	}
	mm, err := sysModFormat(c, &m)
	if err != nil {
		return nil, err
	}
	return mm.(*model.SysModule), nil
}

// SysModFindByModName is 根据mod_name和位置 查找对应模块
func SysModFindByModName(c *gin.Context, Db *xorm.Engine, modName string) (*model.SysModule, error) {
	var m model.SysModule
	if has, err := Db.Where("state = 1 AND mod_name = ?", modName).Get(&m); err != nil || !has {
		return nil, logx.Warn(err)
	}
	mm, err := sysModFormat(c, &m)
	if err != nil {
		return nil, err
	}
	return mm.(*model.SysModule), nil
}

// 根据跳转标识和平台类型查找
func SysModFindBySkipIdentifierAndPlatform(c *gin.Context, Db *xorm.Engine, name string, platform int) (*model.SysModule, error) {
	var m model.SysModule
	if has, err := Db.Where("state = 1 AND skip_identifier = ? AND platform = ?", name, platform).Get(&m); err != nil || !has {
		return nil, logx.Warn(err)
	}
	mm, err := sysModFormat(c, &m)
	if err != nil {
		return nil, err
	}
	return mm.(*model.SysModule), nil
}

// 公共处理modData的链接
func SysModDataByReplace(c *gin.Context, mod model.SysModule) model.SysModule {
	//替换链接的一些参数
	if strings.Contains(mod.Data, "[replace_APP_URL]") {
		mod.Data = strings.Replace(mod.Data, "[replace_APP_URL]", c.GetString("domain_wap_base"), -1)
	}
	if strings.Contains(mod.Data, "[replace_masterId]") {
		mod.Data = strings.Replace(mod.Data, "[replace_masterId]", c.GetString("mid"), -1)
	}
	if strings.Contains(mod.Data, "[replace_platform]") {
		mod.Data = strings.Replace(mod.Data, "[replace_platform]", c.GetHeader("Platform"), -1)
	}
	if strings.Contains(mod.Data, "优惠卷") {
		mod.Data = strings.Replace(mod.Data, "优惠卷", "优惠券", -1)
	}
	if strings.Contains(mod.Data, "[replace_uid]") {
		token := c.GetHeader("Authorization")
		// 按空格分割
		parts := strings.SplitN(token, " ", 2)
		if len(parts) == 2 && parts[0] == "Bearer" {
			// parts[1]是获取到的tokenString，我们使用之前定义好的解析JWT的函数来解析它
			mc, _ := utils.ParseToken(parts[1])
			mod.Data = strings.Replace(mod.Data, "[replace_uid]", strconv.Itoa(mc.UID), -1)
		}
	}
	//if strings.Contains(mod.Data, "\"child_category_id") && strings.Contains(mod.Data, "\"category_id") {
	//	//如果存在这两个字段，要换一下
	//	mod.Data = strings.ReplaceAll(mod.Data, "\"category_id", "\"null_category_id")
	//	mod.Data = strings.ReplaceAll(mod.Data, "\"child_category_id", "\"category_id")
	//}
	return mod
}

// 公共处理modData的链接
func StringByReplace(c *gin.Context, skip string) string {
	//替换链接的一些参数
	if strings.Contains(skip, "[replace_APP_URL]") {
		skip = strings.Replace(skip, "[replace_APP_URL]", c.GetString("domain_wap_base"), -1)
	}
	if strings.Contains(skip, "[replace_masterId]") {
		skip = strings.Replace(skip, "[replace_masterId]", c.GetString("mid"), -1)
	}
	if strings.Contains(skip, "[replace_platform]") {
		skip = strings.Replace(skip, "[replace_platform]", c.GetHeader("Platform"), -1)
	}
	if strings.Contains(skip, "优惠卷") {
		skip = strings.Replace(skip, "优惠卷", "优惠券", -1)
	}
	if strings.Contains(skip, "[replace_uid]") {
		token := c.GetHeader("Authorization")
		// 按空格分割
		parts := strings.SplitN(token, " ", 2)
		if len(parts) == 2 && parts[0] == "Bearer" {
			// parts[1]是获取到的tokenString，我们使用之前定义好的解析JWT的函数来解析它
			mc, _ := utils.ParseToken(parts[1])
			skip = strings.Replace(skip, "[replace_uid]", strconv.Itoa(mc.UID), -1)
		}
	}

	//if strings.Contains(mod.Data, "\"child_category_id") && strings.Contains(mod.Data, "\"category_id") {
	//	//如果存在这两个字段，要换一下
	//	mod.Data = strings.ReplaceAll(mod.Data, "\"category_id", "\"null_category_id")
	//	mod.Data = strings.ReplaceAll(mod.Data, "\"child_category_id", "\"category_id")
	//}
	return skip
}

// 公共处理modSkip的链接 首页弹窗
func SysModSkipByReplace(c *gin.Context, mod *model.SysPopup) *model.SysPopup {
	//替换链接的一些参数
	if strings.Contains(mod.Skip, "[replace_APP_URL]") {
		mod.Skip = strings.Replace(mod.Skip, "[replace_APP_URL]", c.GetString("domain_wap_base"), -1)
	}
	if strings.Contains(mod.Skip, "[replace_masterId]") {
		mod.Skip = strings.Replace(mod.Skip, "[replace_masterId]", c.GetString("mid"), -1)
	}
	if strings.Contains(mod.Skip, "[replace_platform]") {
		mod.Skip = strings.Replace(mod.Skip, "[replace_platform]", c.GetHeader("Platform"), -1)
	}
	if strings.Contains(mod.Skip, "优惠卷") {
		mod.Skip = strings.Replace(mod.Skip, "优惠卷", "优惠券", -1)
	}
	if strings.Contains(mod.Skip, "[replace_uid]") {
		token := c.GetHeader("Authorization")
		// 按空格分割
		parts := strings.SplitN(token, " ", 2)
		if len(parts) == 2 && parts[0] == "Bearer" {
			// parts[1]是获取到的tokenString，我们使用之前定义好的解析JWT的函数来解析它
			mc, _ := utils.ParseToken(parts[1])
			mod.Skip = strings.Replace(mod.Skip, "[replace_uid]", strconv.Itoa(mc.UID), -1)
		}
	}

	//if strings.Contains(mod.Data, "\"child_category_id") && strings.Contains(mod.Data, "\"category_id") {
	//	//如果存在这两个字段，要换一下
	//	mod.Data = strings.ReplaceAll(mod.Data, "\"category_id", "\"null_category_id")
	//	mod.Data = strings.ReplaceAll(mod.Data, "\"child_category_id", "\"category_id")
	//}
	return mod
}

// 公共处理modData的链接
func SysModDataByReplaceSecond(c *gin.Context, mod *model.SysModule) *model.SysModule {
	//替换链接的一些参数
	if strings.Contains(mod.Data, "[replace_APP_URL]") {
		mod.Data = strings.Replace(mod.Data, "[replace_APP_URL]", c.GetString("domain_wap_base"), -1)
	}
	if strings.Contains(mod.Data, "[replace_masterId]") {
		mod.Data = strings.Replace(mod.Data, "[replace_masterId]", c.GetString("mid"), -1)
	}
	if strings.Contains(mod.Data, "优惠卷") {
		mod.Data = strings.Replace(mod.Data, "优惠卷", "优惠券", -1)
	}
	if strings.Contains(mod.Data, "[replace_uid]") {
		token := c.GetHeader("Authorization")
		// 按空格分割
		parts := strings.SplitN(token, " ", 2)
		if len(parts) == 2 && parts[0] == "Bearer" {
			// parts[1]是获取到的tokenString，我们使用之前定义好的解析JWT的函数来解析它
			mc, _ := utils.ParseToken(parts[1])
			if mc == nil {
				mod.Data = strings.Replace(mod.Data, "[replace_uid]", "0", -1)
			} else {
				mod.Data = strings.Replace(mod.Data, "[replace_uid]", strconv.Itoa(mc.UID), -1)
			}
		}
	}

	if mod.ModName == "product" && strings.Contains(mod.Data, "product_3") {
		if strings.Contains(mod.Data, "second_kill_style") == false {
			mod.Data = strings.ReplaceAll(mod.Data, "\"coupon_commission\"", "\"second_kill_style\":{\"btn_bg_img\":\"http://ossn.izhim.net/gift.png\",\"left_stock_text_color\":\"#D59E21\",\"buy_now_text_color\":\"#D59E21\",\"is_show\":\"0\"},\"coupon_commission\"")
		}
	}
	mod.Data = strings.ReplaceAll(mod.Data, "\\/", "/")
	mod.Data = strings.ReplaceAll(mod.Data, "\\u0026", "&")

	//if strings.Contains(mod.Data, "\"child_category_id") && strings.Contains(mod.Data, "\"category_id") {
	//	//如果存在这两个字段，要换一下
	//	mod.Data = strings.ReplaceAll(mod.Data, "\"category_id", "\"null_category_id")
	//	mod.Data = strings.ReplaceAll(mod.Data, "\"child_category_id", "\"category_id")
	//}
	return mod
}
