package utils

import (
	"bytes"
	"crypto/hmac"
	"crypto/md5"
	"crypto/sha1"
	"encoding/base64"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"image"
	"image/png"
	"math"
	"math/rand"
	"net"
	"net/http"
	"os"
	"os/exec"
	"regexp"
	"strconv"
	"strings"
	"time"
)

// 随机数种子
var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))

func GetRandString(size int) string {
	allLetterDigit := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "!", "@", "#", "$", "%", "^", "&", "*"}
	randomSb := ""
	digitSize := len(allLetterDigit)
	for i := 0; i < size; i++ {
		randomSb += allLetterDigit[rnd.Intn(digitSize)]
	}
	return randomSb
}

func GetRandStringNoSpecialChar(size int) string {
	allLetterDigit := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
	randomSb := ""
	digitSize := len(allLetterDigit)
	for i := 0; i < size; i++ {
		randomSb += allLetterDigit[rnd.Intn(digitSize)]
	}
	return randomSb
}

func StringsToJSON(str string) string {
	rs := []rune(str)
	jsons := ""
	for _, r := range rs {
		rint := int(r)
		if rint < 128 {
			jsons += string(r)
		} else {
			jsons += "\\u" + strconv.FormatInt(int64(rint), 16) // json
		}
	}
	return jsons
}

// 序列化
func ToString(v interface{}) string {
	data, _ := json.Marshal(v)
	return string(data)
}

// md5加密
func MD5(data string) string {
	m := md5.Sum([]byte(data))
	return hex.EncodeToString(m[:])
}

// HmacMd5 HmacMd5加密
func HmacMd5(key, data string) string {
	h := hmac.New(md5.New, []byte(key))
	h.Write([]byte(data))
	return hex.EncodeToString(h.Sum([]byte("")))
}

// 获取数字随机字符
func GetRandDigit(n int) string {
	return fmt.Sprintf("%0"+strconv.Itoa(n)+"d", rnd.Intn(int(math.Pow10(n))))
}

// 获取随机数
func GetRandNumber(n int) int {
	return rnd.Intn(n)
}

func GetRandInt(min, max int) int {
	if min >= max || min == 0 || max == 0 {
		return max
	}
	return rand.Intn(max-min) + min
}

func GetToday(format string) string {
	today := time.Now().Format(format)
	return today
}

// 获取今天剩余秒数
func GetTodayLastSecond() time.Duration {
	today := GetToday(FormatDate) + " 23:59:59"
	end, _ := time.ParseInLocation(FormatDateTime, today, time.Local)
	return time.Duration(end.Unix()-time.Now().Local().Unix()) * time.Second
}

// 处理出生日期函数
func GetBrithDate(idcard string) string {
	l := len(idcard)
	var s string
	if l == 15 {
		s = "19" + idcard[6:8] + "-" + idcard[8:10] + "-" + idcard[10:12]
		return s
	}
	if l == 18 {
		s = idcard[6:10] + "-" + idcard[10:12] + "-" + idcard[12:14]
		return s
	}
	return GetToday(FormatDate)
}

// 处理性别
func WhichSexByIdcard(idcard string) string {
	var sexs = [2]string{"女", "男"}
	length := len(idcard)
	if length == 18 {
		sex, _ := strconv.Atoi(string(idcard[16]))
		return sexs[sex%2]
	} else if length == 15 {
		sex, _ := strconv.Atoi(string(idcard[14]))
		return sexs[sex%2]
	}
	return "男"
}

// 截取小数点后几位
func SubFloatToString(f float64, m int) string {
	n := strconv.FormatFloat(f, 'f', -1, 64)
	if n == "" {
		return ""
	}
	if m >= len(n) {
		return n
	}
	newn := strings.Split(n, ".")
	if m == 0 {
		return newn[0]
	}
	if len(newn) < 2 || m >= len(newn[1]) {
		return n
	}
	return newn[0] + "." + newn[1][:m]
}

// 截取小数点后几位
func SubFloatToFloat(f float64, m int) float64 {
	newn := SubFloatToString(f, m)
	newf, _ := strconv.ParseFloat(newn, 64)
	return newf
}

// 获取相差时间-年
func GetYearDiffer(start_time, end_time string) int {
	t1, _ := time.ParseInLocation("2006-01-02", start_time, time.Local)
	t2, _ := time.ParseInLocation("2006-01-02", end_time, time.Local)
	age := t2.Year() - t1.Year()
	if t2.Month() < t1.Month() || (t2.Month() == t1.Month() && t2.Day() < t1.Day()) {
		age--
	}
	return age
}

// 获取相差时间-秒
func GetSecondDifferByTime(start_time, end_time time.Time) int64 {
	diff := end_time.Unix() - start_time.Unix()
	return diff
}

func FixFloat(f float64, m int) float64 {
	newn := SubFloatToString(f+0.00000001, m)
	newf, _ := strconv.ParseFloat(newn, 64)
	return newf
}

// 将字符串数组转化为逗号分割的字符串形式  ["str1","str2","str3"] >>> "str1,str2,str3"
func StrListToString(strList []string) (str string) {
	if len(strList) > 0 {
		for k, v := range strList {
			if k == 0 {
				str = v
			} else {
				str = str + "," + v
			}
		}
		return
	}
	return ""
}

// Token
func GetToken() string {
	randStr := GetRandString(64)
	token := MD5(randStr + Md5Key)
	tokenLen := 64 - len(token)
	return strings.ToUpper(token + GetRandString(tokenLen))
}

// 数据没有记录
func ErrNoRow() string {
	return "<QuerySeter> no row found"
}

// 校验邮箱格式
func ValidateEmailFormatat(email string) bool {
	reg := regexp.MustCompile(RegularEmail)
	return reg.MatchString(email)
}

// 验证是否是手机号
func ValidateMobileFormatat(mobileNum string) bool {
	reg := regexp.MustCompile(RegularMobile)
	return reg.MatchString(mobileNum)
}

// 判断文件是否存在
func FileIsExist(filePath string) bool {
	_, err := os.Stat(filePath)
	return err == nil || os.IsExist(err)
}

// 获取图片扩展名
func GetImgExt(file string) (ext string, err error) {
	var headerByte []byte
	headerByte = make([]byte, 8)
	fd, err := os.Open(file)
	if err != nil {
		return "", err
	}
	defer fd.Close()
	_, err = fd.Read(headerByte)
	if err != nil {
		return "", err
	}
	xStr := fmt.Sprintf("%x", headerByte)
	switch {
	case xStr == "89504e470d0a1a0a":
		ext = ".png"
	case xStr == "0000010001002020":
		ext = ".ico"
	case xStr == "0000020001002020":
		ext = ".cur"
	case xStr[:12] == "474946383961" || xStr[:12] == "474946383761":
		ext = ".gif"
	case xStr[:10] == "0000020000" || xStr[:10] == "0000100000":
		ext = ".tga"
	case xStr[:8] == "464f524d":
		ext = ".iff"
	case xStr[:8] == "52494646":
		ext = ".ani"
	case xStr[:4] == "4d4d" || xStr[:4] == "4949":
		ext = ".tiff"
	case xStr[:4] == "424d":
		ext = ".bmp"
	case xStr[:4] == "ffd8":
		ext = ".jpg"
	case xStr[:2] == "0a":
		ext = ".pcx"
	default:
		ext = ""
	}
	return ext, nil
}

// 保存图片
func SaveImage(path string, img image.Image) (err error) {
	//需要保持的文件
	imgfile, err := os.Create(path)
	defer imgfile.Close()
	// 以PNG格式保存文件
	err = png.Encode(imgfile, img)
	return err
}

// 保存base64数据为文件
func SaveBase64ToFile(content, path string) error {
	data, err := base64.StdEncoding.DecodeString(content)
	if err != nil {
		return err
	}
	f, err := os.Create(path)
	defer f.Close()
	if err != nil {
		return err
	}
	f.Write(data)
	return nil
}

func SaveBase64ToFileBySeek(content, path string) (err error) {
	data, err := base64.StdEncoding.DecodeString(content)
	exist, err := PathExists(path)
	if err != nil {
		return
	}
	if !exist {
		f, err := os.Create(path)
		if err != nil {
			return err
		}
		n, _ := f.Seek(0, 2)
		// 从末尾的偏移量开始写入内容
		_, err = f.WriteAt([]byte(data), n)
		defer f.Close()
	} else {
		f, err := os.OpenFile(path, os.O_WRONLY, 0644)
		if err != nil {
			return err
		}
		n, _ := f.Seek(0, 2)
		// 从末尾的偏移量开始写入内容
		_, err = f.WriteAt([]byte(data), n)
		defer f.Close()
	}

	return nil
}

func PathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return false, err
}

func StartIndex(page, pagesize int) int {
	if page > 1 {
		return (page - 1) * pagesize
	}
	return 0
}
func PageCount(count, pagesize int) int {
	if count%pagesize > 0 {
		return count/pagesize + 1
	} else {
		return count / pagesize
	}
}

func TrimHtml(src string) string {
	//将HTML标签全转换成小写
	re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
	src = re.ReplaceAllStringFunc(src, strings.ToLower)

	re, _ = regexp.Compile("\\<img[\\S\\s]+?\\>")
	src = re.ReplaceAllString(src, "[图片]")

	re, _ = regexp.Compile("class[\\S\\s]+?>")
	src = re.ReplaceAllString(src, "")
	re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
	src = re.ReplaceAllString(src, "")
	return strings.TrimSpace(src)
}

//1556164246  ->  2019-04-25 03:50:46 +0000
//timestamp

func TimeToTimestamp() {
	fmt.Println(time.Unix(1556164246, 0).Format("2006-01-02 15:04:05"))
}

func ToUnicode(text string) string {
	textQuoted := strconv.QuoteToASCII(text)
	textUnquoted := textQuoted[1 : len(textQuoted)-1]
	return textUnquoted
}

func VersionToInt(version string) int {
	version = strings.Replace(version, ".", "", -1)
	n, _ := strconv.Atoi(version)
	return n
}
func IsCheckInList(list []int, s int) bool {
	for _, v := range list {
		if v == s {
			return true
		}
	}
	return false

}

func round(num float64) int {
	return int(num + math.Copysign(0.5, num))
}

func toFixed(num float64, precision int) float64 {
	output := math.Pow(10, float64(precision))
	return float64(round(num*output)) / output
}

// GetWilsonScore returns Wilson Score
func GetWilsonScore(p, n float64) float64 {
	if p == 0 && n == 0 {
		return 0
	}

	return toFixed(((p+1.9208)/(p+n)-1.96*math.Sqrt(p*n/(p+n)+0.9604)/(p+n))/(1+3.8416/(p+n)), 2)
}

// 将中文数字转化成数字,比如 第三百四十五章,返回第345章 不支持一亿及以上
func ChangeWordsToNum(str string) (numStr string) {
	words := ([]rune)(str)
	num := 0
	n := 0
	for i := 0; i < len(words); i++ {
		word := string(words[i : i+1])
		switch word {
		case "万":
			if n == 0 {
				n = 1
			}
			n = n * 10000
			num = num*10000 + n
			n = 0
		case "千":
			if n == 0 {
				n = 1
			}
			n = n * 1000
			num += n
			n = 0
		case "百":
			if n == 0 {
				n = 1
			}
			n = n * 100
			num += n
			n = 0
		case "十":
			if n == 0 {
				n = 1
			}
			n = n * 10
			num += n
			n = 0
		case "一":
			n += 1
		case "二":
			n += 2
		case "三":
			n += 3
		case "四":
			n += 4
		case "五":
			n += 5
		case "六":
			n += 6
		case "七":
			n += 7
		case "八":
			n += 8
		case "九":
			n += 9
		case "零":
		default:
			if n > 0 {
				num += n
				n = 0
			}
			if num == 0 {
				numStr += word
			} else {
				numStr += strconv.Itoa(num) + word
				num = 0
			}
		}
	}
	if n > 0 {
		num += n
		n = 0
	}
	if num != 0 {
		numStr += strconv.Itoa(num)
	}
	return
}

func Sha1(data string) string {
	sha1 := sha1.New()
	sha1.Write([]byte(data))
	return hex.EncodeToString(sha1.Sum([]byte("")))
}

func GetWeekDay() (weekStr string) {
	nowWeek := time.Now().Weekday().String()
	switch nowWeek {
	case "Monday":
		weekStr = "周一"
		break
	case "Tuesday":
		weekStr = "周二"
		break
	case "Wednesday":
		weekStr = "周三"
		break
	case "Thursday":
		weekStr = "周四"
		break
	case "Friday":
		weekStr = "周五"
		break
	case "Saturday":
		weekStr = "周六"
		break
	case "Sunday":
		weekStr = "周日"
		break
	default:
		weekStr = ""
		break
	}
	return
}

// GetNowWeekMonday 获取本周周一的时间
func GetNowWeekMonday() time.Time {
	offset := int(time.Monday - time.Now().Weekday())
	if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
		offset = -6
	}
	mondayTime := time.Now().AddDate(0, 0, offset)
	mondayTime = time.Date(mondayTime.Year(), mondayTime.Month(), mondayTime.Day(), 0, 0, 0, 0, mondayTime.Location())
	return mondayTime
}

// GetLastWeekMonday 获取上周周一的时间
func GetLastWeekMonday() time.Time {
	offset := int(time.Monday - time.Now().Weekday())
	if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
		offset = -6
	}
	mondayTime := time.Now().AddDate(0, 0, offset-7)
	mondayTime = time.Date(mondayTime.Year(), mondayTime.Month(), mondayTime.Day(), 0, 0, 0, 0, mondayTime.Location())
	return mondayTime
}

// GetNowWeekTuesday 获取本周周二的时间
func GetNowWeekTuesday() time.Time {
	offset := int(time.Tuesday - time.Now().Weekday())
	if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
		offset = -6
	}
	mondayTime := time.Now().AddDate(0, 0, offset)
	mondayTime = time.Date(mondayTime.Year(), mondayTime.Month(), mondayTime.Day(), 0, 0, 0, 0, mondayTime.Location())
	return mondayTime
}

// GetLastWeekTuesday 获取上周周二的时间
func GetLastWeekTuesday() time.Time {
	offset := int(time.Tuesday - time.Now().Weekday())
	if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
		offset = -6
	}
	mondayTime := time.Now().AddDate(0, 0, offset-7)
	mondayTime = time.Date(mondayTime.Year(), mondayTime.Month(), mondayTime.Day(), 0, 0, 0, 0, mondayTime.Location())
	return mondayTime
}

// GetNowWeekFriday 获取本周周四的时间
func GetNowWeekThursday() time.Time {
	offset := int(time.Thursday - time.Now().Weekday())
	if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
		offset = -6
	}
	fridayTime := time.Now().AddDate(0, 0, offset)
	fridayTime = time.Date(fridayTime.Year(), fridayTime.Month(), fridayTime.Day(), 0, 0, 0, 0, fridayTime.Location())
	return fridayTime
}

// GetLastWeekFriday 获取上周周四的时间
func GetLastWeekThursday() time.Time {
	offset := int(time.Thursday - time.Now().Weekday())
	if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
		offset = -6
	}
	fridayTime := time.Now().AddDate(0, 0, offset-7)
	fridayTime = time.Date(fridayTime.Year(), fridayTime.Month(), fridayTime.Day(), 0, 0, 0, 0, fridayTime.Location())
	return fridayTime
}

// GetNowWeekFriday 获取本周周五的时间
func GetNowWeekFriday() time.Time {
	offset := int(time.Friday - time.Now().Weekday())
	if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
		offset = -6
	}
	fridayTime := time.Now().AddDate(0, 0, offset)
	fridayTime = time.Date(fridayTime.Year(), fridayTime.Month(), fridayTime.Day(), 0, 0, 0, 0, fridayTime.Location())
	return fridayTime
}

// GetLastWeekFriday 获取上周周五的时间
func GetLastWeekFriday() time.Time {
	offset := int(time.Friday - time.Now().Weekday())
	if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
		offset = -6
	}
	fridayTime := time.Now().AddDate(0, 0, offset-7)
	fridayTime = time.Date(fridayTime.Year(), fridayTime.Month(), fridayTime.Day(), 0, 0, 0, 0, fridayTime.Location())
	return fridayTime
}

// GetNowWeekLastDay 获取本周最后一天的时间
func GetNowWeekLastDay() time.Time {
	offset := int(time.Monday - time.Now().Weekday())
	if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
		offset = -6
	}
	firstDayTime := time.Now().AddDate(0, 0, offset)
	firstDayTime = time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 0, 0, 0, 0, firstDayTime.Location()).AddDate(0, 0, 6)
	lastDayTime := time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 23, 59, 59, 0, firstDayTime.Location())

	return lastDayTime
}

// GetNowMonthFirstDay 获取本月第一天的时间
func GetNowMonthFirstDay() time.Time {
	nowMonthFirstDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location())
	return nowMonthFirstDay
}

// GetNowMonthLastDay 获取本月最后一天的时间
func GetNowMonthLastDay() time.Time {
	nowMonthLastDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
	nowMonthLastDay = time.Date(nowMonthLastDay.Year(), nowMonthLastDay.Month(), nowMonthLastDay.Day(), 23, 59, 59, 0, nowMonthLastDay.Location())
	return nowMonthLastDay
}

// GetNowQuarterFirstDay 获取本季度第一天的时间
func GetNowQuarterFirstDay() time.Time {
	month := int(time.Now().Month())
	var nowQuarterFirstDay time.Time
	if month >= 1 && month <= 3 {
		//1月1号
		nowQuarterFirstDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
	} else if month >= 4 && month <= 6 {
		//4月1号
		nowQuarterFirstDay = time.Date(time.Now().Year(), 4, 1, 0, 0, 0, 0, time.Now().Location())
	} else if month >= 7 && month <= 9 {
		nowQuarterFirstDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
	} else {
		nowQuarterFirstDay = time.Date(time.Now().Year(), 10, 1, 0, 0, 0, 0, time.Now().Location())
	}
	return nowQuarterFirstDay
}

// GetNowQuarterLastDay 获取本季度最后一天的时间
func GetNowQuarterLastDay() time.Time {
	month := int(time.Now().Month())
	var nowQuarterLastDay time.Time
	if month >= 1 && month <= 3 {
		//03-31 23:59:59
		nowQuarterLastDay = time.Date(time.Now().Year(), 3, 31, 23, 59, 59, 0, time.Now().Location())
	} else if month >= 4 && month <= 6 {
		//06-30 23:59:59
		nowQuarterLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
	} else if month >= 7 && month <= 9 {
		//09-30 23:59:59
		nowQuarterLastDay = time.Date(time.Now().Year(), 9, 30, 23, 59, 59, 0, time.Now().Location())
	} else {
		//12-31 23:59:59
		nowQuarterLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
	}
	return nowQuarterLastDay
}

// GetNowHalfYearFirstDay 获取当前半年的第一天的时间
func GetNowHalfYearFirstDay() time.Time {
	month := int(time.Now().Month())
	var nowHalfYearLastDay time.Time
	if month >= 1 && month <= 6 {
		//03-31 23:59:59
		nowHalfYearLastDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
	} else {
		//12-31 23:59:59
		nowHalfYearLastDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
	}
	return nowHalfYearLastDay
}

// GetNowHalfYearLastDay 获取当前半年的最后一天的时间
func GetNowHalfYearLastDay() time.Time {
	month := int(time.Now().Month())
	var nowHalfYearLastDay time.Time
	if month >= 1 && month <= 6 {
		//03-31 23:59:59
		nowHalfYearLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
	} else {
		//12-31 23:59:59
		nowHalfYearLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
	}
	return nowHalfYearLastDay
}

// GetNowYearFirstDay 获取当前年的最后一天的时间
func GetNowYearFirstDay() time.Time {
	//12-31 23:59:59
	nowYearFirstDay := time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
	return nowYearFirstDay
}

// GetNowYearLastDay 获取当前年的最后一天的时间
func GetNowYearLastDay() time.Time {
	//12-31 23:59:59
	nowYearLastDay := time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
	return nowYearLastDay
}

// SubStr 截取字符串(中文)
func SubStr(str string, subLen int) string {
	strRune := []rune(str)
	bodyRuneLen := len(strRune)
	if bodyRuneLen > subLen {
		bodyRuneLen = subLen
	}
	str = string(strRune[:bodyRuneLen])
	return str
}

// InArrayByInt php中的in_array(判断Int类型的切片中是否存在该int值)
func InArrayByInt(idIntList []int, searchId int) (has bool) {
	for _, id := range idIntList {
		if id == searchId {
			has = true
			return
		}
	}
	return
}

// InArrayByStr php中的in_array(判断String类型的切片中是否存在该string值)
func InArrayByStr(idStrList []string, searchId string) (has bool) {
	for _, id := range idStrList {
		if id == searchId {
			has = true
			return
		}
	}
	return
}

func GetLocalIP() (ip string, err error) {
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		return
	}
	for _, addr := range addrs {
		ipAddr, ok := addr.(*net.IPNet)
		if !ok {
			continue
		}
		if ipAddr.IP.IsLoopback() {
			continue
		}
		if !ipAddr.IP.IsGlobalUnicast() {
			continue
		}
		return ipAddr.IP.String(), nil
	}
	return
}

// 富文本字段过滤处理
func GetRichText(content string) (contentSub string) {
	contentSub = strings.Replace(content, "<p data-f-id=\"pbf\" style=\"text-align: center; font-size: 14px; margin-top: 30px; opacity: 0.65; font-family: sans-serif;\">Powered by <a href=\"https://www.froala.com/wysiwyg-editor?pb=1\" title=\"Froala Editor\">Froala Editor</a></p>", "", -1)
	return
}

// GetOrmInReplace 获取orm的in查询替换?的方法
func GetOrmInReplace(num int) string {
	template := make([]string, num)
	for i := 0; i < num; i++ {
		template[i] = "?"
	}
	return strings.Join(template, ",")
}

func GetDaysBetween2Date(format, date1Str, date2Str string) (int, error) {
	// 将字符串转化为Time格式
	date1, err := time.ParseInLocation(format, date1Str, time.Local)
	if err != nil {
		return 0, err
	}
	// 将字符串转化为Time格式
	date2, err := time.ParseInLocation(format, date2Str, time.Local)
	if err != nil {
		return 0, err
	}
	//计算相差天数
	return int(date1.Sub(date2).Hours() / 24), nil
}

// SubFloatToFloatStr 截取小数点后几位
func SubFloatToFloatStr(f float64, m int) string {
	newn := SubFloatToString(f, m)
	return newn
}

func GetVideoPlaySeconds(videoPath string) (playSeconds float64, err error) {
	cmd := `ffmpeg -i ` + videoPath + `  2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//`
	out, err := exec.Command("bash", "-c", cmd).Output()
	if err != nil {
		return
	}
	outTimes := string(out)
	fmt.Println("outTimes:", outTimes)
	if outTimes != "" {
		timeArr := strings.Split(outTimes, ":")
		h := timeArr[0]
		m := timeArr[1]
		s := timeArr[2]
		hInt, err := strconv.Atoi(h)
		if err != nil {
			return playSeconds, err
		}

		mInt, err := strconv.Atoi(m)
		if err != nil {
			return playSeconds, err
		}
		s = strings.Trim(s, " ")
		s = strings.Trim(s, "\n")
		sInt, err := strconv.ParseFloat(s, 64)
		if err != nil {
			return playSeconds, err
		}
		playSeconds = float64(hInt)*3600 + float64(mInt)*60 + float64(sInt)
	}
	return
}

// 随机手机号
const letterBytes = "0123456789"
const (
	letterIdxBits = 4
	letterIdxMask = 1<<letterIdxBits - 1
)
var src = rand.NewSource(time.Now().UnixNano())

var headerNums = [...]string{"139", "138", "137", "136", "135", "134", "159", "158", "157", "150", "151", "152", "188", "187", "182", "183", "184", "178", "130", "131", "132", "156", "155", "186", "185", "176", "133", "153", "189", "180", "181", "177"}
var headerNumsLen = len(headerNums)
const (
	headerIdxBits = 6
	headerIdxMask = 1<<headerIdxBits - 1
)

func getHeaderIdx(cache int64) int {
	for cache > 0{
		idx := int(cache & headerIdxMask)
		if idx < headerNumsLen{
			return idx
		}
		cache >>= headerIdxBits
	}
	return rand.Intn(headerNumsLen)
}

func RandomPhone() string {
	b := make([]byte, 12)
	cache := src.Int63()
	headerIdx := getHeaderIdx(cache)
	for i := 0; i < 3; i++{
		b[i] = headerNums[headerIdx][i]
	}
	for i := 3; i < 12 ; {
		if cache == 0{
			cache = src.Int63()
		}
		if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
			b[i] = letterBytes[idx]
			i++
		}
		cache >>= letterIdxBits
	}
	return string(b)
}

// makePostRequest 发起POST请求并返回响应
func MakePostRequest(url string, body []byte, headers map[string]string) (*http.Response, error) {
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
	if err != nil {
		return nil, err
	}

	// 设置自定义头部
	for key, value := range headers {
		req.Header.Set(key, value)
	}

	// 发起请求
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}

	return resp, nil
}