package services import ( "encoding/base64" "fmt" "io/ioutil" "lzyd-user-api/common" "lzyd-user-api/models" "lzyd-user-api/utils" "math/rand" "os" "regexp" "strconv" "strings" "time" ) /** [Mysql]根据用户手机号去匹配用户信息 */ func GetOneByMobileOnly(mobile string) models.UserInfo { var model models.UserInfo sql := `SELECT user_id, user_code, username, nickname, avatar, mobile, password, is_authen, DATE_FORMAT(register_time, '%Y-%m-%d %H:%i:%S') as register_time FROM sports_user WHERE user_status IN (1, 4) AND mobile = ? AND is_delete = 0` common.DB.Raw(sql, mobile).First(&model) return model } /** [Mysql]检查用户名是否重复 */ func DuplicatedUserName(username string) bool { var count int sql := "SELECT COUNT(user_id) FROM sports_user WHERE username = ?" common.DB.Raw(sql, username).Count(&count) return count > 0 } /** 手机号+密码登录 */ func LoginWithMobileAndPass(mobile, password string) (models.UserInfo, int) { var user models.UserInfo sql := `SELECT user_id, user_code, username, nickname, avatar, mobile, password, is_authen, DATE_FORMAT(register_time, '%Y-%m-%d %H:%i:%S') as register_time FROM sports_user WHERE user_status IN (1, 4) AND mobile = ? AND password = ?` common.DB.Raw(sql, mobile, password).First(&user) if user.Id > 0 { return user, common.QueryStateSuccess } return user, common.QueryStateNone } /** 只用手机号登录 */ func LoginWithMobileOnly(mobile_phone string) (models.UserInfo, int) { var user models.UserInfo sql := `SELECT id, user_id, username, real_name, user_avatar, mobile_phone, app_id, DATE_FORMAT(register_time, '%Y-%m-%d %H:%i:%S') as register_time FROM lzyd_user WHERE mobile_phone = ?` common.DB.Raw(sql, mobile_phone).Limit(1).Find(&user) if user.Id > 0 { return user, common.QueryStateSuccess } return user, common.QueryStateNone } /** 根据用户id查询用户信息 */ func QueryUserByCode(code string) models.UserInfo { var user models.UserInfo var totalDistances []models.TotalDistances var marathons []models.TotalDistances var qualifying []models.TotalDistances var allCourse int // total_distance, times_marathon,times_qualifier, sql := `SELECT id, user_id, username, like_num, atten_num, real_name, user_avatar, mobile_phone, birthday, fans_num, user_point, app_id, height, weight, gender, address, area, DATE_FORMAT(register_time, '%Y-%m-%d %H:%i:%S') as register_time FROM lzyd_user WHERE user_id = ? AND is_delete = 0` common.DB.Raw(sql, code).Limit(1).Find(&user) sql1 := `SELECT course FROM lzyd_sport_log WHERE user_id = ?` common.DB.Raw(sql1, code).Find(&totalDistances) sql2 := `SELECT marathon_id FROM lzyd_marathon_user WHERE user_id = ? && is_cancel = 0` common.DB.Raw(sql2, code).Find(&marathons) sql3 := `SELECT qualifying_id FROM lzyd_qualifying_user WHERE user_id = ?` common.DB.Raw(sql3, code).Find(&qualifying) fmt.Println(totalDistances,marathons,qualifying) for i := 0; i < len(totalDistances); i++ { allCourse = totalDistances[i].TotalDistance + allCourse } user.TotalDistance = allCourse user.TimesMarathon = len(marathons) user.TimesQualifier = len(qualifying) //fmt.Println("你输入的是:", user) return user } /** 根据用户id查询用户赛事订单 */ func QueryCompetitionOrder(code string) []models.Competition { var order []models.Competition var goodsList []models.CompetitionGoodsList sql := `SELECT u.deal_sn,u.marathon_id,u.pay_type,u.order_money,u.recv_man,u.pay_money,u.deal_status,u.is_paied,v.name from lzyd_marathon_order_master u LEFT JOIN lzyd_marathon v ON u.marathon_id = v.marathon_id WHERE u.user_id = ? AND u.is_delete = 0` common.DB.Raw(sql, code).Find(&order) for i:=0;i< len(order); i++ { sql1 := `SELECT u.pkg_id,u.pkg_name,u.spec,u.price,u.amout,v.cover,u.spec_id from lzyd_marathon_order_detail u LEFT JOIN lzyd_marathon_pkg_spec x ON u.spec_id = x.spec_id LEFT JOIN lzyd_marathon_pkg_cert v ON v.pkg_id = x.pkg_id WHERE deal_sn = ?` common.DB.Raw(sql1,order[i].DealSn ).Find(&goodsList) order[i].List = goodsList } fmt.Println(order) //fmt.Println("你输入的是:", order) return order } /** 根据用户id查询用户排位赛信息 */ func QueryQualifyingList(code string) models.QualifyingList { var qualify models.QualifyingList var qualifying []models.QualifyingResp sql := `SELECT u.user_id, u.qualifying_id, v.name, u.rank, v.update_at FROM lzyd_qualifying_user u LEFT JOIN lzyd_qualifying v ON v.qualifying_id = u.qualifying_id WHERE u.user_id = ?` common.DB.Raw(sql, code).Find(&qualifying) qualify.List = qualifying var FirstCode = 0 var TopThreeCode = 0 var AverageCode = 0 var TenAverageCode = 0 fmt.Println(len(qualifying)) for i := 0; i < len(qualifying); i++ { ch := qualifying[i] fmt.Println(ch) Chint, err := strconv.Atoi(ch.Rank) fmt.Println(err) fmt.Println(111,Chint) if Chint == 1 { FirstCode += 1 } if Chint <= 3 && 0 < Chint { TopThreeCode += 1 } AverageCode += Chint if i <= 10{ TenAverageCode += Chint } } if len(qualifying) > 0 { AverageCode = AverageCode/len(qualifying) if len(qualifying) <= 10{ TenAverageCode = TenAverageCode/len(qualifying) }else { TenAverageCode = TenAverageCode/10 } }else { AverageCode = 0 TenAverageCode = 0 } qualify.First = FirstCode qualify.TopThree = TopThreeCode qualify.Average = AverageCode qualify.TenAverage = TenAverageCode //fmt.Println("你输入的是:", order) return qualify } /** 根据用户id查询用户黑名单 */ func QueryBlackByCode(code string,listType int) []models.BlackListReq { var user []models.BlackListReq sql := `select u.user_id,u.user_avatar,u.fans_num, u.username from lzyd_user u left join lzyd_video_attention v on v.attention_user_id=u.user_id where v.user_id= ? and v.status = ?` common.DB.Raw(sql, code, listType).Find(&user) for i := 0; i < len(user); i++ { var videoList []models.VideoListReq sqlVideo := `select user_id from lzyd_video_upload where user_id= ?` common.DB.Raw(sqlVideo, user[i].UserId).Find(&videoList) user[i].VideosNum = len(videoList) } //fmt.Println("你输入的是:", user) return user } /** [Mysql]根据OpenID去匹配用户信息 */ func QueryUserByOpenId(openId string) (models.UserInfo, int) { var user models.UserInfo sql := `SELECT user_id, user_code, username, nickname, avatar, mobile, password, is_authen, DATE_FORMAT(register_time, '%Y-%m-%d %H:%i:%S') as register_time FROM sports_user WHERE user_status IN (1, 4) AND wechat_id = ? AND is_delete = 0` common.DB.Raw(sql, openId).First(&user) if user.Id > 0 { return user, common.QueryStateSuccess } return user, common.QueryStateNone } /** [Mysql]获取第三方绑定信息 */ func GetBindInfoByUserId(uid string) models.UserInfo { var model models.UserInfo sql := "SELECT id, wxid FROM lzyd_user WHERE user_id = ? AND user_status IN (1, 4)" common.DB.Raw(sql, uid).First(&model) return model } // 判断手机号是否已存在 func ExistedMobile(mobile string) bool { var count int sql := "SELECT COUNT(user_id) FROM lzyd_user WHERE mobile_phone = ?" common.DB.Raw(sql, mobile).Count(&count) return count > 0 } /** 用户昵称唯一处理 */ func UniqueName(name string) string { name = strings.TrimSpace(name) if len(name) == 0 { return "悦动" + "_" + utils.GenUserIdentifier() } if DuplicatedUserName(name) { return name + "_" + utils.GenUserIdentifier() } return name } /** 查询是否关注 */ func UserRelation(userCode string,otherCode string) string { var noticeRead models.UserRelationResp //var noticeRead2 models.UserRelationResp //var noticeRead3 models.UserRelationResp //var noticeRead4 models.UserRelationResp // sql1与sql2 查询用户之前互相关注 sql := `SELECT attention_user_id FROM lzyd_video_attention where user_id = ? AND attention_user_id = ? AND status = 0` common.DB.Raw(sql,otherCode,userCode).Find(¬iceRead) //fmt.Println(noticeRead) //sql2 := `SELECT attention_user_id FROM lzyd_video_attention where user_id = ? AND attention_user_id = ? AND status = 0` //common.DB.Raw(sql2,bCreatorId,creatorId).Find(¬iceRead2) return noticeRead.AttentionUserId } //写入文件,保存 func WriteFile(path string, base64_image_content string) string { b, _ := regexp.MatchString(`^data:\s*image\/(\w+);base64,`, base64_image_content) if !b { } re, _ := regexp.Compile(`^data:\s*image\/(\w+);base64,`) allData := re.FindAllSubmatch([]byte(base64_image_content), 2) fileType := string(allData[0][1]) //png ,jpeg 后缀获取 base64Str := re.ReplaceAllString(base64_image_content, "") //date := time.Now().Format("20060102") //if ok := IsFileExist(path + "/" + date); !ok { // os.Mkdir(path+"/"+date, 0666) //} //+ "/" + date var file string = path + "/" + strconv.FormatInt(time.Now().Unix(),10) + strconv.Itoa(rand.Intn(999999-100000)+100000) + "." + fileType byte, _ := base64.StdEncoding.DecodeString(base64Str) err := ioutil.WriteFile(file, byte, 0666) if err != nil { return file } return file } //判断文件是否存在 func IsFileExist(filename string) bool { _, err := os.Stat(filename) if os.IsNotExist(err) { return false } return true }