344 lines
7.9 KiB
Go
344 lines
7.9 KiB
Go
package service
|
||
|
||
import (
|
||
"errors"
|
||
"fmt"
|
||
"gorm.io/gorm"
|
||
"hepa-calc-api/api/dao"
|
||
"hepa-calc-api/api/model"
|
||
"hepa-calc-api/extend/aliyun"
|
||
"hepa-calc-api/extend/app"
|
||
"hepa-calc-api/utils"
|
||
"io"
|
||
"math/rand"
|
||
"net/http"
|
||
"time"
|
||
)
|
||
|
||
type UserService struct {
|
||
}
|
||
|
||
// HandleUserAvatar 处理用户头像
|
||
func (r *UserService) HandleUserAvatar(wxAvatar string) (avatar string, err error) {
|
||
if wxAvatar == "" {
|
||
return "", nil
|
||
}
|
||
|
||
// 发送GET请求
|
||
resp, err := http.Get(wxAvatar)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
|
||
defer func(Body io.ReadCloser) {
|
||
_ = Body.Close()
|
||
}(resp.Body)
|
||
|
||
if resp.StatusCode != 200 {
|
||
return "", errors.New("请求失败")
|
||
}
|
||
|
||
// 读取响应体
|
||
respBody, err := io.ReadAll(resp.Body)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
|
||
// 设置文件名字
|
||
now := time.Now()
|
||
dateTimeString := now.Format("20060102150405") // 当前时间字符串
|
||
rand.New(rand.NewSource(time.Now().UnixNano())) // 设置随机数
|
||
ossPath := "user/avatar/" + dateTimeString + fmt.Sprintf("%d", rand.Intn(9000)+1000) + ".png"
|
||
|
||
// 上传oss
|
||
_, err = aliyun.PutObjectByte(ossPath, respBody)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
|
||
ossPath = "/" + ossPath
|
||
|
||
return ossPath, nil
|
||
}
|
||
|
||
// CheckUserMember 检测用户会员
|
||
func (r *UserService) CheckUserMember(user *model.User) bool {
|
||
if user.IsMember == 0 {
|
||
return false
|
||
}
|
||
|
||
now := time.Now()
|
||
t := time.Time(*user.MemberExpireDate)
|
||
if t.Before(now) {
|
||
return false
|
||
}
|
||
|
||
return true
|
||
}
|
||
|
||
// CheckUserBuyMember 检测用户是否购买过会员
|
||
func (r *UserService) CheckUserBuyMember(userId int64) bool {
|
||
orderMemberDao := dao.OrderMemberDao{}
|
||
orderMember, _ := orderMemberDao.GetUserFirstTimeBuyOrderMember(userId)
|
||
if orderMember == nil {
|
||
return false
|
||
}
|
||
|
||
return true
|
||
}
|
||
|
||
// CheckUserBuySingle 检测用户是否购买过单项产品
|
||
func (r *UserService) CheckUserBuySingle(userId int64) bool {
|
||
orderSingleDao := dao.OrderSingleDao{}
|
||
orderSingle, _ := orderSingleDao.GetUserFirstTimeBuyOrderSingle(userId)
|
||
if orderSingle == nil {
|
||
return false
|
||
}
|
||
|
||
return true
|
||
}
|
||
|
||
// AddUserMemberValidDate 增加用户会员过期时间
|
||
func (r *UserService) AddUserMemberValidDate(tx *gorm.DB, user *model.User, d int) bool {
|
||
userData := make(map[string]interface{})
|
||
if user.IsMember == 0 {
|
||
userData["is_member"] = 1
|
||
}
|
||
|
||
var t time.Time
|
||
if user.MemberExpireDate == nil {
|
||
t = time.Now()
|
||
} else {
|
||
t = time.Time(*user.MemberExpireDate)
|
||
}
|
||
userData["is_member"] = t.Add(time.Duration(d) * 24 * time.Hour)
|
||
|
||
userDao := dao.UserDao{}
|
||
err := userDao.EditUserById(tx, user.UserId, userData)
|
||
if err != nil {
|
||
return false
|
||
}
|
||
|
||
return true
|
||
}
|
||
|
||
// AddUserSingleSubmitCount 增加用户单项提交次数
|
||
func (r *UserService) AddUserSingleSubmitCount(tx *gorm.DB, userId int64) (bool, error) {
|
||
userDao := dao.UserDao{}
|
||
err := userDao.Inc(tx, userId, "single_submit_count", 1)
|
||
if err != nil {
|
||
return false, err
|
||
}
|
||
|
||
return true, nil
|
||
}
|
||
|
||
// AddUserSinglePayCount 增加用户单项支付次数
|
||
func (r *UserService) AddUserSinglePayCount(tx *gorm.DB, userId int64) (bool, error) {
|
||
userDao := dao.UserDao{}
|
||
err := userDao.Inc(tx, userId, "single_pay_count", 1)
|
||
if err != nil {
|
||
return false, err
|
||
}
|
||
|
||
return true, nil
|
||
}
|
||
|
||
// AddUserMemberBuyCount 增加用户会员购买次数
|
||
func (r *UserService) AddUserMemberBuyCount(tx *gorm.DB, userId int64) (bool, error) {
|
||
userDao := dao.UserDao{}
|
||
err := userDao.Inc(tx, userId, "member_buy_count", 1)
|
||
if err != nil {
|
||
return false, err
|
||
}
|
||
|
||
return true, nil
|
||
}
|
||
|
||
// HandleAppUserInfo 处理app用户数据
|
||
func (r *UserService) HandleAppUserInfo(tx *gorm.DB, user *model.User, userInfo *model.UserInfo) error {
|
||
appUserInfo, err := app.GetInfoByMobile(user.Mobile)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
// 对比处理用户数据
|
||
userData := make(map[string]interface{}) // 用户数据
|
||
userInfoData := make(map[string]interface{}) // 用户详情数据
|
||
|
||
// 出生日期/年龄
|
||
if appUserInfo.Data.Birthday != "" {
|
||
if user.Birthday != nil {
|
||
birthday := time.Time(*user.Birthday).Format("2006-01-02 15:04:05")
|
||
if appUserInfo.Data.Birthday != birthday {
|
||
userData["birthday"] = appUserInfo.Data.Birthday
|
||
}
|
||
} else {
|
||
userData["birthday"] = appUserInfo.Data.Birthday
|
||
}
|
||
|
||
// 年龄
|
||
age, err := utils.CalculateAge(appUserInfo.Data.Birthday)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
userData["age"] = age
|
||
}
|
||
|
||
// 是否怀孕 1无计划 2计划中 3已怀孕 4家有宝宝
|
||
if appUserInfo.Data.IsPregnant != nil {
|
||
if userInfo.IsPregnant != nil {
|
||
if *appUserInfo.Data.IsPregnant != *userInfo.IsPregnant {
|
||
userInfoData["is_pregnant"] = appUserInfo.Data.IsPregnant
|
||
}
|
||
} else {
|
||
userInfoData["is_pregnant"] = appUserInfo.Data.IsPregnant
|
||
}
|
||
|
||
// 预产期
|
||
if appUserInfo.Data.ExpectedDateOfChildbirth != "" {
|
||
userInfoData["expected_date"] = appUserInfo.Data.ExpectedDateOfChildbirth
|
||
}
|
||
}
|
||
|
||
// 性别
|
||
if appUserInfo.Data.Sex != nil {
|
||
if user.Sex != nil {
|
||
if *appUserInfo.Data.Sex != *user.Sex {
|
||
userData["sex"] = appUserInfo.Data.Sex
|
||
}
|
||
} else {
|
||
userData["sex"] = appUserInfo.Data.Sex
|
||
}
|
||
}
|
||
|
||
// 体重
|
||
if appUserInfo.Data.Weight != nil {
|
||
weight := fmt.Sprintf("%f", appUserInfo.Data.Weight)
|
||
|
||
if userInfo.Weight != "" {
|
||
if weight != userInfo.Weight {
|
||
userInfoData["weight"] = weight
|
||
}
|
||
} else {
|
||
userInfoData["weight"] = weight
|
||
}
|
||
}
|
||
|
||
// 省份 id
|
||
if appUserInfo.Data.ProvinceID != nil {
|
||
// 获取省份数据
|
||
|
||
if userInfo.ProvinceId != nil {
|
||
if *appUserInfo.Data.ProvinceID != *userInfo.ProvinceId {
|
||
userInfoData["province_id"] = appUserInfo.Data.ProvinceID
|
||
}
|
||
} else {
|
||
userInfoData["province_id"] = appUserInfo.Data.ProvinceID
|
||
}
|
||
}
|
||
|
||
// 城市 id
|
||
if appUserInfo.Data.CityID != nil {
|
||
// 获取城市数据
|
||
|
||
if userInfo.CityId != nil {
|
||
if *appUserInfo.Data.CityID != *userInfo.CityId {
|
||
userInfoData["city_id"] = appUserInfo.Data.CityID
|
||
}
|
||
} else {
|
||
userInfoData["city_id"] = appUserInfo.Data.CityID
|
||
}
|
||
}
|
||
|
||
// 市区 id
|
||
if appUserInfo.Data.CountyID != nil {
|
||
// 获取区县数据
|
||
|
||
if userInfo.CountyId != nil {
|
||
if *appUserInfo.Data.CountyID != *userInfo.CountyId {
|
||
userInfoData["county_id"] = appUserInfo.Data.CountyID
|
||
}
|
||
} else {
|
||
userInfoData["county_id"] = appUserInfo.Data.CountyID
|
||
}
|
||
}
|
||
|
||
// 家族病史
|
||
if appUserInfo.Data.IsHBV != nil {
|
||
// 有无 肝硬化或肝癌家族史 0无1有2未知
|
||
// 是否存在家族病史(0:未知 1:是 2:否)
|
||
// 转换双方状态
|
||
if *appUserInfo.Data.IsHBV == 0 {
|
||
*appUserInfo.Data.IsHBV = 2
|
||
} else if *appUserInfo.Data.IsHBV == 1 {
|
||
*appUserInfo.Data.IsHBV = 1
|
||
} else if *appUserInfo.Data.IsHBV == 2 {
|
||
*appUserInfo.Data.IsHBV = 0
|
||
}
|
||
|
||
if userInfo.IsFamilyHistory != nil {
|
||
if *appUserInfo.Data.IsHBV != *userInfo.IsFamilyHistory {
|
||
userInfoData["is_family_history"] = appUserInfo.Data.IsHBV
|
||
}
|
||
} else {
|
||
userInfoData["is_family_history"] = appUserInfo.Data.IsHBV
|
||
}
|
||
}
|
||
|
||
// 民族
|
||
if appUserInfo.Data.NationUUID != "" {
|
||
// 获取民族数据
|
||
var nationId int64
|
||
if userInfo.NationId != nil {
|
||
if nationId != *userInfo.NationId {
|
||
userInfoData["nation_id"] = nationId
|
||
}
|
||
} else {
|
||
userInfoData["nation_id"] = nationId
|
||
}
|
||
}
|
||
|
||
// 患者 uuid
|
||
if appUserInfo.Data.PatientUUID != "" {
|
||
if user.AppIden != "" {
|
||
if appUserInfo.Data.PatientUUID != user.AppIden {
|
||
userData["app_iden"] = appUserInfo.Data.PatientUUID
|
||
}
|
||
} else {
|
||
userData["app_iden"] = appUserInfo.Data.PatientUUID
|
||
}
|
||
}
|
||
|
||
// 姓名
|
||
if appUserInfo.Data.Name != "" {
|
||
if user.UserName != "" {
|
||
if appUserInfo.Data.Name != user.UserName {
|
||
userData["user_name"] = appUserInfo.Data.Name
|
||
}
|
||
} else {
|
||
userData["user_name"] = appUserInfo.Data.Name
|
||
}
|
||
}
|
||
|
||
// 修改用户数据
|
||
if len(userData) > 0 {
|
||
userDao := dao.UserDao{}
|
||
err := userDao.EditUserById(tx, user.UserId, userData)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
}
|
||
|
||
// 修改用户详情数据
|
||
if len(userInfoData) > 0 {
|
||
userInfoDao := dao.UserInfoDao{}
|
||
err := userInfoDao.EditUserInfoById(tx, userInfo.UserInfoId, userInfoData)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
}
|
||
|
||
return nil
|
||
}
|