新增修改用户数据-基本信息

This commit is contained in:
2024-08-19 15:50:20 +08:00
parent 32fde535f5
commit d41f2399ad
5 changed files with 410 additions and 76 deletions
+284 -53
View File
@@ -14,6 +14,7 @@ import (
"io"
"math/rand"
"net/http"
"strconv"
"time"
)
@@ -617,59 +618,263 @@ func (r *UserService) HandleAppUserCase(tx *gorm.DB, user *model.User) error {
// 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{})
//appUserData := make(map[string]interface{})
//
//// 用户名称
//if req.UserName != user.UserName {
// userData["user_name"] = req.UserName
// appUserData["name"] = req.UserName
//}
//
//// 注册来源
//if req.RegisterSource != user.RegisterSource {
// userData["register_source"] = req.UserName
//}
//
//// 出生日期
//birthday := time.Time(*user.Birthday).Format("2006-01-02")
//if req.Birthday != birthday {
// userData["birthday"] = req.Birthday
// appUserData["birthday"] = req.Birthday
//
// // 年龄
// age, err := utils.CalculateAge(req.Birthday)
// if err != nil {
// return false, errors.New("年龄错误")
// }
// userData["age"] = age
//}
//
//// 性别
//if req.Sex != *user.Sex {
// userData["sex"] = req.Sex
//
// if req.Sex == 1 {
// appUserData["sex"] = 0
// } else if req.Sex == 2 {
// appUserData["sex"] = 1
// }
//}
// 获取用户数据
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{})
appData := app.UpdateInfoRequest{
Birthday: req.Birthday,
IsPegnant: req.IsPregnant,
Sex: nil,
Weight: req.Weight,
ExpectedDateOfChildbirth: req.ExpectedDate,
IsHbv: nil,
NationUuid: "",
PatientUuid: "",
Name: req.UserName,
ProvId: nil,
CityId: nil,
CountyId: nil,
Height: req.Height,
}
// 用户名称
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 req.Sex == 1 {
sex := 0
appData.Sex = &sex
} else if req.Sex == 2 {
sex := 1
appData.Sex = &sex
}
if req.Sex != *user.Sex {
userData["sex"] = req.Sex
}
// 身高
if req.Height != nil {
height := fmt.Sprintf("%d", *req.Height)
if height != userInfo.Height {
userInfoData["height"] = req.Height
}
} else {
userInfoData["height"] = nil
}
// 体重
if req.Weight != nil {
weight := fmt.Sprintf("%d", *req.Weight)
if weight != userInfo.Weight {
userInfoData["weight"] = req.Weight
}
} else {
userInfoData["weight"] = nil
}
// 民族id
if req.NationId != "" {
if userInfo.NationId != nil {
nationId := fmt.Sprintf("%d", *userInfo.NationId)
if req.NationId != nationId {
userInfoData["nation_id"] = req.NationId
// 获取民族数据
baseNationDao := dao.BaseNationDao{}
maps := make(map[string]interface{})
maps["nation_id"] = req.NationId
baseNation, err := baseNationDao.GetBaseNation(maps)
if err != nil {
return false, errors.New("民族错误")
}
appData.NationUuid = baseNation.AppIden
}
} else {
userInfoData["nation_id"] = req.NationId
// 获取民族数据
baseNationDao := dao.BaseNationDao{}
maps := make(map[string]interface{})
maps["nation_id"] = req.NationId
baseNation, err := baseNationDao.GetBaseNation(maps)
if err != nil {
return false, errors.New("民族错误")
}
appData.NationUuid = baseNation.AppIden
}
} else {
if userInfo.NationId != nil {
userInfoData["nation_id"] = nil
appData.NationUuid = ""
}
}
// 是否存在家族病史
// 有无 肝硬化或肝癌家族史 0无1有2未知
// 是否存在家族病史(0:未知 1:是 2:否)
// 转换双方状态
var isHbv int
if *req.IsFamilyHistory == 0 {
isHbv = 2
} else if *req.IsFamilyHistory == 1 {
isHbv = 1
} else if *req.IsFamilyHistory == 2 {
isHbv = 0
}
appData.IsHbv = &isHbv
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("省份错误")
}
appData.ProvId = &provinceId
baseAreaDao := dao.BaseAreaDao{}
baseArea, err := baseAreaDao.GetBaseAreaById(provinceId)
if err != nil {
return false, errors.New("省份错误")
}
if userInfo.ProvinceId != nil {
if 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("城市错误")
}
appData.CityId = &cityId
baseArea, err = baseAreaDao.GetBaseAreaById(cityId)
if err != nil {
return false, errors.New("城市错误")
}
if userInfo.CityId != nil {
if cityId != *userInfo.CityId {
userInfoData["city_id"] = cityId
userInfoData["city"] = baseArea.Name
}
} else {
userInfoData["city_id"] = cityId
userInfoData["city"] = baseArea.Name
appData.CityId = &cityId
}
// 区县id
countyId, err := strconv.ParseInt(req.CountyId, 10, 64)
if err != nil {
return false, errors.New("城市错误")
}
appData.CountyId = &countyId
baseArea, err = baseAreaDao.GetBaseAreaById(countyId)
if err != nil {
return false, errors.New("城市错误")
}
if userInfo.CountyId != nil {
if countyId != *userInfo.CountyId {
userInfoData["county_id"] = countyId
userInfoData["county"] = baseArea.Name
}
} else {
userInfoData["county_id"] = countyId
userInfoData["county"] = baseArea.Name
appData.CountyId = &countyId
}
// 开始事务
tx := global.Db.Begin()
@@ -679,6 +884,32 @@ func (r *UserService) PutUser(userId int64, req requests.PutUser) (bool, error)
}
}()
// 用户数据
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
}
}
// app数据
appData.PatientUuid = user.AppIden
_, err = app.UpdateInfo(appData)
if err != nil {
tx.Rollback()
return false, err
}
tx.Commit()
return true, nil
}