887 lines
21 KiB
Go
887 lines
21 KiB
Go
package service
|
||
|
||
import (
|
||
"errors"
|
||
"fmt"
|
||
"gorm.io/gorm"
|
||
"hepa-calc-api/api/dao"
|
||
"hepa-calc-api/api/model"
|
||
"hepa-calc-api/api/requests"
|
||
"hepa-calc-api/extend/aliyun"
|
||
"hepa-calc-api/extend/app"
|
||
"hepa-calc-api/global"
|
||
"hepa-calc-api/utils"
|
||
"io"
|
||
"math/rand"
|
||
"net/http"
|
||
"strconv"
|
||
"time"
|
||
)
|
||
|
||
type UserService struct {
|
||
}
|
||
|
||
// HandleUserImage 处理用户图片
|
||
func (r *UserService) HandleUserImage(wxAvatar string) (ossPath 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)
|
||
if t.Sub(time.Now()) < 0 {
|
||
t = time.Now()
|
||
}
|
||
}
|
||
|
||
userData["member_expire_date"] = 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
|
||
}
|
||
|
||
// GetAppUserInfo 查询app用户数据
|
||
func (r *UserService) GetAppUserInfo(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")
|
||
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 *appUserInfo.Data.Sex == 0 {
|
||
*appUserInfo.Data.Sex = 1
|
||
} else if *appUserInfo.Data.Sex == 1 {
|
||
*appUserInfo.Data.Sex = 2
|
||
}
|
||
|
||
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 && *appUserInfo.Data.Weight != 0 {
|
||
weight := fmt.Sprintf("%d", *appUserInfo.Data.Weight)
|
||
|
||
if userInfo.Weight != "" {
|
||
if weight != userInfo.Weight {
|
||
userInfoData["weight"] = weight
|
||
}
|
||
} else {
|
||
userInfoData["weight"] = weight
|
||
}
|
||
}
|
||
|
||
// 身高
|
||
if appUserInfo.Data.Height != nil && *appUserInfo.Data.Height != 0 {
|
||
height := fmt.Sprintf("%d", *appUserInfo.Data.Height)
|
||
|
||
if userInfo.Height != "" {
|
||
if height != userInfo.Height {
|
||
userInfoData["height"] = height
|
||
}
|
||
} else {
|
||
userInfoData["height"] = height
|
||
}
|
||
}
|
||
|
||
// 省份 id
|
||
if appUserInfo.Data.ProvinceID != nil {
|
||
// 获取省份数据
|
||
baseAreaDao := dao.BaseAreaDao{}
|
||
if userInfo.ProvinceId != "" {
|
||
provinceId := fmt.Sprintf("%d", *appUserInfo.Data.ProvinceID)
|
||
if provinceId != userInfo.ProvinceId {
|
||
userInfoData["province_id"] = *appUserInfo.Data.ProvinceID
|
||
|
||
baseArea, err := baseAreaDao.GetBaseAreaById(*appUserInfo.Data.ProvinceID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
userInfoData["province"] = baseArea.Name
|
||
}
|
||
} else {
|
||
userInfoData["province_id"] = *appUserInfo.Data.ProvinceID
|
||
|
||
baseArea, err := baseAreaDao.GetBaseAreaById(*appUserInfo.Data.ProvinceID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
userInfoData["province"] = baseArea.Name
|
||
}
|
||
}
|
||
|
||
// 城市 id
|
||
if appUserInfo.Data.CityID != nil {
|
||
// 获取城市数据
|
||
baseAreaDao := dao.BaseAreaDao{}
|
||
if userInfo.CityId != "" {
|
||
cityId := fmt.Sprintf("%d", *appUserInfo.Data.CityID)
|
||
if cityId != userInfo.CityId {
|
||
userInfoData["city_id"] = *appUserInfo.Data.CityID
|
||
|
||
baseArea, err := baseAreaDao.GetBaseAreaById(*appUserInfo.Data.CityID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
userInfoData["city"] = baseArea.Name
|
||
}
|
||
} else {
|
||
userInfoData["city_id"] = *appUserInfo.Data.CityID
|
||
|
||
baseArea, err := baseAreaDao.GetBaseAreaById(*appUserInfo.Data.CityID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
userInfoData["city"] = baseArea.Name
|
||
}
|
||
}
|
||
|
||
// 市区 id
|
||
if appUserInfo.Data.CountyID != nil {
|
||
// 获取区县数据
|
||
baseAreaDao := dao.BaseAreaDao{}
|
||
if userInfo.CountyId != "" {
|
||
countyId := fmt.Sprintf("%d", *appUserInfo.Data.CountyID)
|
||
if countyId != userInfo.CountyId {
|
||
userInfoData["county_id"] = *appUserInfo.Data.CountyID
|
||
|
||
baseArea, err := baseAreaDao.GetBaseAreaById(*appUserInfo.Data.CountyID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
userInfoData["county"] = baseArea.Name
|
||
}
|
||
} else {
|
||
userInfoData["county_id"] = *appUserInfo.Data.CountyID
|
||
|
||
baseArea, err := baseAreaDao.GetBaseAreaById(*appUserInfo.Data.CountyID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
userInfoData["county"] = baseArea.Name
|
||
}
|
||
}
|
||
|
||
// 家族病史
|
||
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 != "" {
|
||
// 获取民族数据
|
||
baseNationDao := dao.BaseNationDao{}
|
||
baseNation, err := baseNationDao.GetBaseNationByAppIden(appUserInfo.Data.NationUUID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
if userInfo.NationId != nil {
|
||
if baseNation.NationId != *userInfo.NationId {
|
||
userInfoData["nation_id"] = baseNation.NationId
|
||
}
|
||
} else {
|
||
userInfoData["nation_id"] = baseNation.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 == "" {
|
||
appUserInfo.Data.Name = "GDXZ_" + utils.GenerateRandomString(4)
|
||
}
|
||
if user.UserName != "" {
|
||
if appUserInfo.Data.Name != user.UserName {
|
||
userData["user_name"] = appUserInfo.Data.Name
|
||
}
|
||
} else {
|
||
userData["user_name"] = appUserInfo.Data.Name
|
||
}
|
||
|
||
// 头像
|
||
if appUserInfo.Data.Photo != "" && user.Avatar == "" {
|
||
// 处理app图片前缀
|
||
photo, err := app.HandleImagePrefix(appUserInfo.Data.Photo)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
if photo != "" {
|
||
// 处理用户图片,上传至oss
|
||
ossPath, err := r.HandleUserImage(appUserInfo.Data.Photo)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
userData["avatar"] = ossPath
|
||
}
|
||
}
|
||
|
||
// 修改用户数据
|
||
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
|
||
}
|
||
|
||
// GetAppUserCase 查询app用户病例数据
|
||
func (r *UserService) GetAppUserCase(tx *gorm.DB, user *model.User) error {
|
||
appUserCase, err := app.GetUserCaseByAppIden(user.AppIden)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
// 用户无病例数据
|
||
if appUserCase.Data == nil {
|
||
return nil
|
||
}
|
||
|
||
// 获取用户病例数据
|
||
userCaseDao := dao.UserCaseDao{}
|
||
userCase, _ := userCaseDao.GetUserCaseByUserId(user.UserId)
|
||
if userCase == nil {
|
||
// 未添加用户病例数据
|
||
userCase = &model.UserCase{}
|
||
userCase.UserId = user.UserId
|
||
|
||
// 是否过敏史
|
||
if appUserCase.Data.IsAllergy != nil {
|
||
userCase.IsAllergyHistory = appUserCase.Data.IsAllergy
|
||
|
||
// 过敏史描述
|
||
if appUserCase.Data.AllergyInfo != "" {
|
||
userCase.AllergyHistory = appUserCase.Data.AllergyInfo
|
||
}
|
||
}
|
||
|
||
// 是否服药
|
||
if appUserCase.Data.IsMedication != nil {
|
||
userCase.IsMedication = appUserCase.Data.IsMedication
|
||
|
||
// 服药名称
|
||
if appUserCase.Data.MedicationInfo != "" {
|
||
userCase.Medication = appUserCase.Data.MedicationInfo
|
||
}
|
||
}
|
||
|
||
// 是否去医院
|
||
if appUserCase.Data.IsHospital != nil {
|
||
userCase.IsHospital = appUserCase.Data.IsHospital
|
||
}
|
||
|
||
// 慢性疾病
|
||
if appUserCase.Data.OtherDisease != "" {
|
||
userCase.ChronicDisease = appUserCase.Data.OtherDisease
|
||
}
|
||
|
||
// 目前肝脏状态
|
||
if appUserCase.Data.LiverStatus != "" {
|
||
userCase.LiverStatus = appUserCase.Data.LiverStatus
|
||
}
|
||
|
||
userCase, err = userCaseDao.AddUserCase(tx, userCase)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
} else {
|
||
// 对比处理用户数据
|
||
userCaseData := make(map[string]interface{}) // 用户病例数据
|
||
|
||
// 是否过敏史
|
||
if appUserCase.Data.IsAllergy != nil {
|
||
if userCase.IsAllergyHistory != nil {
|
||
if *appUserCase.Data.IsAllergy != *userCase.IsAllergyHistory {
|
||
userCaseData["is_allergy_history"] = appUserCase.Data.IsAllergy
|
||
}
|
||
} else {
|
||
userCaseData["is_allergy_history"] = appUserCase.Data.IsAllergy
|
||
}
|
||
|
||
// 过敏史描述
|
||
if appUserCase.Data.AllergyInfo != "" {
|
||
if userCase.AllergyHistory != "" {
|
||
if appUserCase.Data.AllergyInfo != userCase.AllergyHistory {
|
||
userCaseData["allergy_history"] = appUserCase.Data.AllergyInfo
|
||
}
|
||
} else {
|
||
userCaseData["allergy_history"] = appUserCase.Data.AllergyInfo
|
||
}
|
||
}
|
||
}
|
||
|
||
// 是否服药
|
||
if appUserCase.Data.IsMedication != nil {
|
||
if userCase.IsMedication != nil {
|
||
if *appUserCase.Data.IsMedication != *userCase.IsMedication {
|
||
userCaseData["is_medication"] = appUserCase.Data.IsMedication
|
||
}
|
||
} else {
|
||
userCaseData["is_medication"] = appUserCase.Data.IsMedication
|
||
}
|
||
|
||
// 服药名称
|
||
if appUserCase.Data.MedicationInfo != "" {
|
||
if userCase.Medication != "" {
|
||
if appUserCase.Data.MedicationInfo != userCase.Medication {
|
||
userCaseData["medication"] = appUserCase.Data.MedicationInfo
|
||
}
|
||
} else {
|
||
userCaseData["medication"] = appUserCase.Data.MedicationInfo
|
||
}
|
||
}
|
||
}
|
||
|
||
// 是否去医院
|
||
if appUserCase.Data.IsHospital != nil {
|
||
if userCase.IsHospital != nil {
|
||
if *appUserCase.Data.IsHospital != *userCase.IsHospital {
|
||
userCaseData["is_hospital"] = appUserCase.Data.IsHospital
|
||
}
|
||
} else {
|
||
userCaseData["is_hospital"] = appUserCase.Data.IsHospital
|
||
}
|
||
}
|
||
|
||
// 慢性疾病
|
||
if appUserCase.Data.OtherDisease != "" {
|
||
if userCase.ChronicDisease != "" {
|
||
if appUserCase.Data.OtherDisease != userCase.ChronicDisease {
|
||
userCaseData["chronic_disease"] = appUserCase.Data.OtherDisease
|
||
}
|
||
} else {
|
||
userCaseData["chronic_disease"] = appUserCase.Data.OtherDisease
|
||
}
|
||
}
|
||
|
||
// 目前肝脏状态
|
||
if appUserCase.Data.LiverStatus != "" {
|
||
if userCase.LiverStatus != "" {
|
||
if appUserCase.Data.LiverStatus != userCase.LiverStatus {
|
||
userCaseData["liver_status"] = appUserCase.Data.LiverStatus
|
||
}
|
||
} else {
|
||
userCaseData["liver_status"] = appUserCase.Data.LiverStatus
|
||
}
|
||
}
|
||
|
||
// 修改用户病例数据
|
||
if len(userCaseData) > 0 {
|
||
err := userCaseDao.EditUserCaseById(tx, userCase.UserCaseId, userCaseData)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
}
|
||
|
||
if len(appUserCase.Data.DiseasesList) > 0 {
|
||
userCaseDiseaseItemDao := dao.UserCaseDiseaseItemDao{}
|
||
|
||
// 删除所患疾病列表
|
||
maps := make(map[string]interface{})
|
||
maps["user_case_id"] = userCase.UserCaseId
|
||
err := userCaseDiseaseItemDao.DeleteUserCaseDiseaseItem(tx, maps)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
}
|
||
}
|
||
|
||
// 所患疾病列表
|
||
if len(appUserCase.Data.DiseasesList) > 0 {
|
||
for _, data := range appUserCase.Data.DiseasesList {
|
||
userCaseDiseaseItemDao := dao.UserCaseDiseaseItemDao{}
|
||
baseDiseaseClassDao := dao.BaseDiseaseClassDao{}
|
||
|
||
// 获取基础数据-疾病分类
|
||
baseDiseaseClass, err := baseDiseaseClassDao.GetBaseDiseaseClassByAppIden(data.UUID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
// 新增所患疾病列表
|
||
userCaseDiseaseItem := &model.UserCaseDiseaseItem{
|
||
UserCaseId: userCase.UserCaseId,
|
||
DiseaseClassId: baseDiseaseClass.DiseaseClassId,
|
||
AppIden: data.UUID,
|
||
Duration: data.Year,
|
||
Genotype: data.Info,
|
||
}
|
||
|
||
userCaseDiseaseItem, err = userCaseDiseaseItemDao.AddUserCaseDiseaseItem(tx, userCaseDiseaseItem)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
}
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// PutUser 修改用户数据-基本信息
|
||
func (r *UserService) PutUser(userId int64, req requests.PutUser) (bool, error) {
|
||
// 获取用户数据
|
||
userDao := dao.UserDao{}
|
||
user, err := userDao.GetUserById(userId)
|
||
if err != nil {
|
||
return false, errors.New("用户数据错误")
|
||
}
|
||
|
||
// 获取用户数据
|
||
userInfoDao := dao.UserInfoDao{}
|
||
userInfo, err := userInfoDao.GetUserInfoByUserId(userId)
|
||
if err != nil {
|
||
return false, errors.New("用户数据错误")
|
||
}
|
||
|
||
userData := make(map[string]interface{})
|
||
userInfoData := make(map[string]interface{})
|
||
|
||
// 用户名称
|
||
if req.UserName != user.UserName {
|
||
userData["user_name"] = req.UserName
|
||
}
|
||
|
||
// 出生日期
|
||
if user.Birthday != nil {
|
||
birthday := time.Time(*user.Birthday).Format("2006-01-02")
|
||
if req.Birthday != birthday {
|
||
userData["birthday"] = req.Birthday
|
||
|
||
// 年龄
|
||
age, err := utils.CalculateAge(req.Birthday)
|
||
if err != nil {
|
||
return false, errors.New("年龄错误")
|
||
}
|
||
userData["age"] = age
|
||
}
|
||
} else {
|
||
userData["birthday"] = req.Birthday
|
||
userData["age"] = nil
|
||
}
|
||
|
||
if user.Sex != nil {
|
||
if req.Sex != *user.Sex {
|
||
userData["sex"] = req.Sex
|
||
}
|
||
} else {
|
||
userData["sex"] = req.Sex
|
||
}
|
||
|
||
// 头像
|
||
if req.Avatar != "" {
|
||
avatar := utils.RemoveOssDomain(req.Avatar)
|
||
if avatar != user.Avatar {
|
||
userData["avatar"] = avatar
|
||
}
|
||
} else {
|
||
if user.Avatar != "" {
|
||
userData["avatar"] = ""
|
||
}
|
||
}
|
||
|
||
// 身高
|
||
if req.Height != nil {
|
||
height := fmt.Sprintf("%d", *req.Height)
|
||
if height != userInfo.Height {
|
||
userInfoData["height"] = req.Height
|
||
}
|
||
} else {
|
||
if userInfo.Height != "" {
|
||
userInfoData["height"] = nil
|
||
}
|
||
}
|
||
|
||
// 体重
|
||
if req.Weight != nil {
|
||
weight := fmt.Sprintf("%d", *req.Weight)
|
||
if weight != userInfo.Weight {
|
||
userInfoData["weight"] = req.Weight
|
||
}
|
||
} else {
|
||
if userInfo.Weight != "" {
|
||
userInfoData["weight"] = nil
|
||
}
|
||
}
|
||
|
||
// 民族id
|
||
if req.NationId != "" {
|
||
if userInfo.NationId != nil {
|
||
nationId := fmt.Sprintf("%d", *userInfo.NationId)
|
||
fmt.Println(req.NationId)
|
||
if req.NationId != nationId {
|
||
userInfoData["nation_id"] = req.NationId
|
||
}
|
||
} else {
|
||
userInfoData["nation_id"] = req.NationId
|
||
}
|
||
} else {
|
||
if userInfo.NationId != nil {
|
||
userInfoData["nation_id"] = nil
|
||
}
|
||
}
|
||
|
||
if req.IsFamilyHistory != nil {
|
||
if userInfo.IsFamilyHistory != nil {
|
||
if *req.IsFamilyHistory != *userInfo.IsFamilyHistory {
|
||
userInfoData["is_family_history"] = req.IsFamilyHistory
|
||
}
|
||
} else {
|
||
userInfoData["is_family_history"] = req.IsFamilyHistory
|
||
}
|
||
} else {
|
||
if userInfo.IsFamilyHistory != nil {
|
||
userInfoData["is_family_history"] = nil
|
||
}
|
||
}
|
||
|
||
// 是否怀孕 (1:无计划 2:计划中 3:已怀孕 4:家有宝宝)'
|
||
if req.IsPregnant != nil {
|
||
if userInfo.IsPregnant != nil {
|
||
if *req.IsPregnant != *userInfo.IsPregnant {
|
||
userInfoData["is_pregnant"] = *req.IsPregnant
|
||
}
|
||
} else {
|
||
userInfoData["is_pregnant"] = *req.IsPregnant
|
||
}
|
||
} else {
|
||
if userInfo.IsPregnant != nil {
|
||
userInfoData["is_pregnant"] = nil
|
||
}
|
||
}
|
||
|
||
// 预产期
|
||
if req.ExpectedDate != "" {
|
||
if userInfo.ExpectedDate != nil {
|
||
expectedDate := time.Time(*userInfo.ExpectedDate).Format("2006-01-02")
|
||
if req.ExpectedDate != expectedDate {
|
||
userInfoData["expected_date"] = req.ExpectedDate
|
||
}
|
||
} else {
|
||
userInfoData["expected_date"] = req.ExpectedDate
|
||
}
|
||
} else {
|
||
if userInfo.ExpectedDate != nil {
|
||
userInfoData["expected_date"] = nil
|
||
}
|
||
}
|
||
|
||
// 省份id
|
||
provinceId, err := strconv.ParseInt(req.ProvinceId, 10, 64)
|
||
if err != nil {
|
||
return false, errors.New("省份错误")
|
||
}
|
||
|
||
baseAreaDao := dao.BaseAreaDao{}
|
||
baseArea, err := baseAreaDao.GetBaseAreaById(provinceId)
|
||
if err != nil {
|
||
return false, errors.New("省份错误")
|
||
}
|
||
|
||
if userInfo.ProvinceId != "" {
|
||
if req.ProvinceId != userInfo.ProvinceId {
|
||
userInfoData["province_id"] = provinceId
|
||
userInfoData["province"] = baseArea.Name
|
||
}
|
||
} else {
|
||
userInfoData["province_id"] = provinceId
|
||
userInfoData["province"] = baseArea.Name
|
||
}
|
||
|
||
// 城市id
|
||
cityId, err := strconv.ParseInt(req.CityId, 10, 64)
|
||
if err != nil {
|
||
return false, errors.New("城市错误")
|
||
}
|
||
|
||
baseArea, err = baseAreaDao.GetBaseAreaById(cityId)
|
||
if err != nil {
|
||
return false, errors.New("城市错误")
|
||
}
|
||
|
||
if userInfo.CityId != "" {
|
||
if req.CityId != userInfo.CityId {
|
||
userInfoData["city_id"] = cityId
|
||
|
||
userInfoData["city"] = baseArea.Name
|
||
}
|
||
} else {
|
||
userInfoData["city_id"] = cityId
|
||
userInfoData["city"] = baseArea.Name
|
||
}
|
||
|
||
// 区县id
|
||
countyId, err := strconv.ParseInt(req.CountyId, 10, 64)
|
||
if err != nil {
|
||
return false, errors.New("区县错误")
|
||
}
|
||
|
||
baseArea, err = baseAreaDao.GetBaseAreaById(countyId)
|
||
if err != nil {
|
||
return false, errors.New("城市错误")
|
||
}
|
||
|
||
if userInfo.CountyId != "" {
|
||
if req.CountyId != userInfo.CountyId {
|
||
userInfoData["county_id"] = countyId
|
||
|
||
userInfoData["county"] = baseArea.Name
|
||
}
|
||
} else {
|
||
userInfoData["county_id"] = countyId
|
||
userInfoData["county"] = baseArea.Name
|
||
}
|
||
|
||
// 开始事务
|
||
tx := global.Db.Begin()
|
||
defer func() {
|
||
if r := recover(); r != nil {
|
||
tx.Rollback()
|
||
}
|
||
}()
|
||
|
||
// 用户数据
|
||
if len(userData) > 0 {
|
||
err = userDao.EditUserById(tx, userId, userData)
|
||
if err != nil {
|
||
tx.Rollback()
|
||
return false, err
|
||
}
|
||
}
|
||
|
||
// 用户详情数据
|
||
if len(userInfoData) > 0 {
|
||
err = userInfoDao.EditUserInfoById(tx, userInfo.UserInfoId, userInfoData)
|
||
if err != nil {
|
||
tx.Rollback()
|
||
return false, err
|
||
}
|
||
}
|
||
|
||
tx.Commit()
|
||
return true, nil
|
||
}
|
||
|
||
// CheckUserInfo 检测用户信息是否补全
|
||
func (r *UserService) CheckUserInfo(userInfo *model.UserInfo) bool {
|
||
if userInfo == nil {
|
||
return false
|
||
}
|
||
|
||
if userInfo.IsFamilyHistory == nil {
|
||
return false
|
||
}
|
||
|
||
if userInfo.ProvinceId == "" {
|
||
return false
|
||
}
|
||
|
||
return true
|
||
}
|