user.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package services
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "io/ioutil"
  6. "lzyd-user-api/common"
  7. "lzyd-user-api/models"
  8. "lzyd-user-api/utils"
  9. "math/rand"
  10. "os"
  11. "regexp"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. /**
  17. [Mysql]根据用户手机号去匹配用户信息
  18. */
  19. func GetOneByMobileOnly(mobile string) models.UserInfo {
  20. var model models.UserInfo
  21. sql := `SELECT user_id, user_code, username, nickname, avatar, mobile, password, is_authen,
  22. DATE_FORMAT(register_time, '%Y-%m-%d %H:%i:%S') as register_time
  23. FROM sports_user
  24. WHERE user_status IN (1, 4) AND mobile = ? AND is_delete = 0`
  25. common.DB.Raw(sql, mobile).First(&model)
  26. return model
  27. }
  28. /**
  29. [Mysql]检查用户名是否重复
  30. */
  31. func DuplicatedUserName(username string) bool {
  32. var count int
  33. sql := "SELECT COUNT(user_id) FROM sports_user WHERE username = ?"
  34. common.DB.Raw(sql, username).Count(&count)
  35. return count > 0
  36. }
  37. /**
  38. 手机号+密码登录
  39. */
  40. func LoginWithMobileAndPass(mobile, password string) (models.UserInfo, int) {
  41. var user models.UserInfo
  42. sql := `SELECT user_id, user_code, username, nickname, avatar, mobile, password, is_authen,
  43. DATE_FORMAT(register_time, '%Y-%m-%d %H:%i:%S') as register_time
  44. FROM sports_user
  45. WHERE user_status IN (1, 4) AND mobile = ? AND password = ?`
  46. common.DB.Raw(sql, mobile, password).First(&user)
  47. if user.Id > 0 {
  48. return user, common.QueryStateSuccess
  49. }
  50. return user, common.QueryStateNone
  51. }
  52. /**
  53. 只用手机号登录
  54. */
  55. func LoginWithMobileOnly(mobile_phone string) (models.UserInfo, int) {
  56. var user models.UserInfo
  57. sql := `SELECT id, user_id, username, real_name, user_avatar, mobile_phone, app_id,
  58. DATE_FORMAT(register_time, '%Y-%m-%d %H:%i:%S') as register_time
  59. FROM lzyd_user
  60. WHERE mobile_phone = ?`
  61. common.DB.Raw(sql, mobile_phone).Limit(1).Find(&user)
  62. if user.Id > 0 {
  63. return user, common.QueryStateSuccess
  64. }
  65. return user, common.QueryStateNone
  66. }
  67. /**
  68. 根据用户id查询用户信息
  69. */
  70. func QueryUserByCode(code string) models.UserInfo {
  71. var user models.UserInfo
  72. var totalDistances []models.TotalDistances
  73. var marathons []models.TotalDistances
  74. var qualifying []models.TotalDistances
  75. var allCourse int
  76. // total_distance, times_marathon,times_qualifier,
  77. sql := `SELECT id, user_id, username, like_num, atten_num, real_name, user_avatar, mobile_phone, birthday, fans_num, user_point, app_id,
  78. height, weight, gender, address, area,
  79. DATE_FORMAT(register_time, '%Y-%m-%d %H:%i:%S') as register_time
  80. FROM lzyd_user
  81. WHERE user_id = ? AND is_delete = 0`
  82. common.DB.Raw(sql, code).Limit(1).Find(&user)
  83. sql1 := `SELECT course FROM lzyd_sport_log WHERE user_id = ?`
  84. common.DB.Raw(sql1, code).Find(&totalDistances)
  85. sql2 := `SELECT marathon_id FROM lzyd_marathon_user WHERE user_id = ? && is_cancel = 0`
  86. common.DB.Raw(sql2, code).Find(&marathons)
  87. sql3 := `SELECT qualifying_id FROM lzyd_qualifying_user WHERE user_id = ?`
  88. common.DB.Raw(sql3, code).Find(&qualifying)
  89. fmt.Println(totalDistances,marathons,qualifying)
  90. for i := 0; i < len(totalDistances); i++ {
  91. allCourse = totalDistances[i].TotalDistance + allCourse
  92. }
  93. user.TotalDistance = allCourse
  94. user.TimesMarathon = len(marathons)
  95. user.TimesQualifier = len(qualifying)
  96. //fmt.Println("你输入的是:", user)
  97. return user
  98. }
  99. /**
  100. 根据用户id查询用户赛事订单
  101. */
  102. func QueryCompetitionOrder(code string) []models.Competition {
  103. var order []models.Competition
  104. var goodsList []models.CompetitionGoodsList
  105. 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
  106. from lzyd_marathon_order_master u
  107. LEFT JOIN lzyd_marathon v ON u.marathon_id = v.marathon_id
  108. WHERE u.user_id = ? AND u.is_delete = 0`
  109. common.DB.Raw(sql, code).Find(&order)
  110. for i:=0;i< len(order); i++ {
  111. sql1 := `SELECT u.pkg_id,u.pkg_name,u.spec,u.price,u.amout,v.cover,u.spec_id
  112. from lzyd_marathon_order_detail u
  113. LEFT JOIN lzyd_marathon_pkg_spec x ON u.spec_id = x.spec_id
  114. LEFT JOIN lzyd_marathon_pkg_cert v ON v.pkg_id = x.pkg_id
  115. WHERE deal_sn = ?`
  116. common.DB.Raw(sql1,order[i].DealSn ).Find(&goodsList)
  117. order[i].List = goodsList
  118. }
  119. fmt.Println(order)
  120. //fmt.Println("你输入的是:", order)
  121. return order
  122. }
  123. /**
  124. 根据用户id查询用户排位赛信息
  125. */
  126. func QueryQualifyingList(code string) models.QualifyingList {
  127. var qualify models.QualifyingList
  128. var qualifying []models.QualifyingResp
  129. sql := `SELECT u.user_id, u.qualifying_id, v.name, u.rank, v.update_at
  130. FROM lzyd_qualifying_user u
  131. LEFT JOIN lzyd_qualifying v ON v.qualifying_id = u.qualifying_id
  132. WHERE u.user_id = ?`
  133. common.DB.Raw(sql, code).Find(&qualifying)
  134. qualify.List = qualifying
  135. var FirstCode = 0
  136. var TopThreeCode = 0
  137. var AverageCode = 0
  138. var TenAverageCode = 0
  139. fmt.Println(len(qualifying))
  140. for i := 0; i < len(qualifying); i++ {
  141. ch := qualifying[i]
  142. fmt.Println(ch)
  143. Chint, err := strconv.Atoi(ch.Rank)
  144. fmt.Println(err)
  145. fmt.Println(111,Chint)
  146. if Chint == 1 {
  147. FirstCode += 1
  148. }
  149. if Chint <= 3 && 0 < Chint {
  150. TopThreeCode += 1
  151. }
  152. AverageCode += Chint
  153. if i <= 10{
  154. TenAverageCode += Chint
  155. }
  156. }
  157. if len(qualifying) > 0 {
  158. AverageCode = AverageCode/len(qualifying)
  159. if len(qualifying) <= 10{
  160. TenAverageCode = TenAverageCode/len(qualifying)
  161. }else {
  162. TenAverageCode = TenAverageCode/10
  163. }
  164. }else {
  165. AverageCode = 0
  166. TenAverageCode = 0
  167. }
  168. qualify.First = FirstCode
  169. qualify.TopThree = TopThreeCode
  170. qualify.Average = AverageCode
  171. qualify.TenAverage = TenAverageCode
  172. //fmt.Println("你输入的是:", order)
  173. return qualify
  174. }
  175. /**
  176. 根据用户id查询用户黑名单
  177. */
  178. func QueryBlackByCode(code string,listType int) []models.BlackListReq {
  179. var user []models.BlackListReq
  180. sql := `select u.user_id,u.user_avatar,u.fans_num, u.username from lzyd_user u
  181. left join lzyd_video_attention v on v.attention_user_id=u.user_id
  182. where v.user_id= ? and v.status = ?`
  183. common.DB.Raw(sql, code, listType).Find(&user)
  184. for i := 0; i < len(user); i++ {
  185. var videoList []models.VideoListReq
  186. sqlVideo := `select user_id from lzyd_video_upload where user_id= ?`
  187. common.DB.Raw(sqlVideo, user[i].UserId).Find(&videoList)
  188. user[i].VideosNum = len(videoList)
  189. }
  190. //fmt.Println("你输入的是:", user)
  191. return user
  192. }
  193. /**
  194. [Mysql]根据OpenID去匹配用户信息
  195. */
  196. func QueryUserByOpenId(openId string) (models.UserInfo, int) {
  197. var user models.UserInfo
  198. sql := `SELECT user_id, user_code, username, nickname, avatar, mobile, password, is_authen,
  199. DATE_FORMAT(register_time, '%Y-%m-%d %H:%i:%S') as register_time
  200. FROM sports_user
  201. WHERE user_status IN (1, 4) AND wechat_id = ? AND is_delete = 0`
  202. common.DB.Raw(sql, openId).First(&user)
  203. if user.Id > 0 {
  204. return user, common.QueryStateSuccess
  205. }
  206. return user, common.QueryStateNone
  207. }
  208. /**
  209. [Mysql]获取第三方绑定信息
  210. */
  211. func GetBindInfoByUserId(uid string) models.UserInfo {
  212. var model models.UserInfo
  213. sql := "SELECT id, wxid FROM lzyd_user WHERE user_id = ? AND user_status IN (1, 4)"
  214. common.DB.Raw(sql, uid).First(&model)
  215. return model
  216. }
  217. // 判断手机号是否已存在
  218. func ExistedMobile(mobile string) bool {
  219. var count int
  220. sql := "SELECT COUNT(user_id) FROM lzyd_user WHERE mobile_phone = ?"
  221. common.DB.Raw(sql, mobile).Count(&count)
  222. return count > 0
  223. }
  224. /**
  225. 用户昵称唯一处理
  226. */
  227. func UniqueName(name string) string {
  228. name = strings.TrimSpace(name)
  229. if len(name) == 0 {
  230. return "悦动" + "_" + utils.GenUserIdentifier()
  231. }
  232. if DuplicatedUserName(name) {
  233. return name + "_" + utils.GenUserIdentifier()
  234. }
  235. return name
  236. }
  237. /**
  238. 查询是否关注
  239. */
  240. func UserRelation(userCode string,otherCode string) string {
  241. var noticeRead models.UserRelationResp
  242. //var noticeRead2 models.UserRelationResp
  243. //var noticeRead3 models.UserRelationResp
  244. //var noticeRead4 models.UserRelationResp
  245. // sql1与sql2 查询用户之前互相关注
  246. sql := `SELECT attention_user_id FROM lzyd_video_attention where user_id = ? AND attention_user_id = ? AND status = 0`
  247. common.DB.Raw(sql,otherCode,userCode).Find(&noticeRead)
  248. //fmt.Println(noticeRead)
  249. //sql2 := `SELECT attention_user_id FROM lzyd_video_attention where user_id = ? AND attention_user_id = ? AND status = 0`
  250. //common.DB.Raw(sql2,bCreatorId,creatorId).Find(&noticeRead2)
  251. return noticeRead.AttentionUserId
  252. }
  253. //写入文件,保存
  254. func WriteFile(path string, base64_image_content string) string {
  255. b, _ := regexp.MatchString(`^data:\s*image\/(\w+);base64,`, base64_image_content)
  256. if !b {
  257. }
  258. re, _ := regexp.Compile(`^data:\s*image\/(\w+);base64,`)
  259. allData := re.FindAllSubmatch([]byte(base64_image_content), 2)
  260. fileType := string(allData[0][1]) //png ,jpeg 后缀获取
  261. base64Str := re.ReplaceAllString(base64_image_content, "")
  262. //date := time.Now().Format("20060102")
  263. //if ok := IsFileExist(path + "/" + date); !ok {
  264. // os.Mkdir(path+"/"+date, 0666)
  265. //}
  266. //+ "/" + date
  267. var file string = path + "/" + strconv.FormatInt(time.Now().Unix(),10) + strconv.Itoa(rand.Intn(999999-100000)+100000) + "." + fileType
  268. byte, _ := base64.StdEncoding.DecodeString(base64Str)
  269. err := ioutil.WriteFile(file, byte, 0666)
  270. if err != nil {
  271. return file
  272. }
  273. return file
  274. }
  275. //判断文件是否存在
  276. func IsFileExist(filename string) bool {
  277. _, err := os.Stat(filename)
  278. if os.IsNotExist(err) {
  279. return false
  280. }
  281. return true
  282. }