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

This commit is contained in:
wucongxing8150 2024-08-19 15:50:20 +08:00
parent 32fde535f5
commit d41f2399ad
5 changed files with 410 additions and 76 deletions

View File

@ -16,11 +16,11 @@ type UserInfoDto struct {
IsFamilyHistory *int `json:"is_family_history"` // 是否存在家族病史0:未知 1:是 2:否)
IsPregnant *int `json:"is_pregnant"` // 是否怀孕(1:无计划 2:计划中 3:已怀孕 4:家有宝宝)
ExpectedDate *string `json:"expected_date"` // 预产期
ProvinceId *int `json:"province_id"` // 省份id
ProvinceId *int64 `json:"province_id"` // 省份id
Province string `json:"province"` // 省份
CityId *int `json:"city_id"` // 城市id
CityId *int64 `json:"city_id"` // 城市id
City string `json:"city"` // 城市
CountyId *int `json:"county_id"` // 区县id
CountyId *int64 `json:"county_id"` // 区县id
County string `json:"county"` // 区县
CreatedAt model.LocalTime `json:"created_at"` // 创建时间
UpdatedAt model.LocalTime `json:"updated_at"` // 更新时间

View File

@ -16,11 +16,11 @@ type UserInfo struct {
IsFamilyHistory *int `gorm:"column:is_family_history;type:tinyint(1);comment:是否存在家族病史0:未知 1:是 2:否)" json:"is_family_history"`
IsPregnant *int `gorm:"column:is_pregnant;type:tinyint(1);comment:是否怀孕(1:无计划 2:计划中 3:已怀孕 4:家有宝宝)" json:"is_pregnant"`
ExpectedDate *LocalTime `gorm:"column:expected_date;type:datetime;comment:预产期" json:"expected_date"`
ProvinceId *int `gorm:"column:province_id;type:int(11);comment:省份id" json:"province_id"`
ProvinceId *int64 `gorm:"column:province_id;type:int(11);comment:省份id" json:"province_id"`
Province string `gorm:"column:province;type:varchar(40);comment:省份" json:"province"`
CityId *int `gorm:"column:city_id;type:int(11);comment:城市id" json:"city_id"`
CityId *int64 `gorm:"column:city_id;type:int(11);comment:城市id" json:"city_id"`
City string `gorm:"column:city;type:varchar(50);comment:城市" json:"city"`
CountyId *int `gorm:"column:county_id;type:int(11);comment:区县id" json:"county_id"`
CountyId *int64 `gorm:"column:county_id;type:int(11);comment:区县id" json:"county_id"`
County string `gorm:"column:county;type:varchar(255);comment:区县" json:"county"`
Model
}

View File

@ -7,20 +7,16 @@ type UserRequest struct {
// PutUser 修改用户数据-基本信息
type PutUser struct {
UserName string `json:"user_name" form:"user_name" label:"用户名称" validate:"required"`
RegisterSource int `json:"register_source" form:"register_source" label:"注册来源" validate:"required,oneof=1 2"`
Birthday string `json:"birthday" form:"birthday" label:"出生日期" validate:"required,min=1,max=120"`
Sex int `json:"sex" form:"sex" label:"性别" validate:"required,oneof=1 2"`
Sex int `json:"sex" form:"sex" label:"性别" validate:"omitempty,oneof=0 1 2"`
Avatar string `json:"avatar" form:"avatar" label:"头像"`
Height string `json:"height" form:"height" label:"身高cm"`
Weight string `json:"weight" form:"weight" label:"体重kg"`
NationId string `json:"nation_id" form:"nation_id" label:"民族id"` // 从int64转换为可选字符串
IsFamilyHistory *int `json:"is_family_history" form:"is_family_history" label:"是否存在家族病史" validate:"omitempty,oneof=0 1 2"`
IsPregnant *int `json:"is_pregnant" form:"is_pregnant" label:"是否怀孕"`
Height *int `json:"height" form:"height" label:"身高cm"`
Weight *int `json:"weight" form:"weight" label:"体重kg"`
NationId string `json:"nation_id" form:"nation_id" label:"民族id"` // 从int64转换为可选字符串
IsFamilyHistory *int `json:"is_family_history" form:"is_family_history" label:"是否存在家族病史" validate:"omitempty,oneof=0 1 2"` // 是否存在家族病史0:位置 1:是 2:否)
IsPregnant *int `json:"is_pregnant" form:"is_pregnant" label:"是否怀孕" validate:"omitempty,oneof=1 2 3 4"`
ExpectedDate string `json:"expected_date" form:"expected_date" label:"预产期"` // 假设转换为可选字符串
ProvinceId string `json:"province_id" form:"province_id" label:"省份id" validate:"required"` // 从int转换为可选字符串
Province string `json:"province" form:"province" label:"省份" validate:"required"`
CityId string `json:"city_id" form:"city_id" label:"城市id" validate:"required"` // 从int转换为可选字符串
City string `json:"city" form:"city" label:"城市" validate:"required"`
CountyId string `json:"county_id" form:"county_id" label:"区县id"` // 从int转换为可选字符串
County string `json:"county" form:"county" label:"区县"`
CityId string `json:"city_id" form:"city_id" label:"城市id" validate:"required"` // 从int转换为可选字符串
CountyId string `json:"county_id" form:"county_id" label:"区县id" validate:"required"` // 从int转换为可选字符串
}

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
}

View File

@ -18,6 +18,25 @@ type GetInfoByMobileRequest struct {
Timestamp string `json:"timestamp"` // 当前时间戳10位
}
// UpdateInfoRequest 修改用户信息-请求数据
type UpdateInfoRequest struct {
Birthday string `json:"birthday"` // 出生日期
IsPegnant *int `json:"isPegnant"` // 是否怀孕 1无计划 2计划中 3已怀孕 4家有宝宝
Sex *int `json:"sex"` // 性别 0男 1女
Weight *int `json:"weight"` // 体重 KG
ExpectedDateOfChildbirth string `json:"expectedDateOfChildbirth"` // 预产期
IsHbv *int `json:"isHbv"` // 市区 id
NationUuid string `json:"nationUuid"` // 民族 uuid
PatientUuid string `json:"patientUuid"` // 患者 uuid
Name string `json:"name"` // 姓名
ProvId *int64 `json:"provId"` // 省份 id
CityId *int64 `json:"cityId"` // 城市 id
CountyId *int64 `json:"countyId"` // 市区 id
Height *int `json:"height"` // 身高 cm
Platform string `json:"platform"` // 所属平台
Timestamp string `json:"timestamp"` // 当前时间戳10位
}
// GetInfoByMobileResponse 根据手机号获取用户信息-返回数据
type GetInfoByMobileResponse struct {
Code int `json:"code"` // 接口调用状态。200正常其它值调用出错
@ -27,6 +46,15 @@ type GetInfoByMobileResponse struct {
Message string `json:"message"`
}
// UpdateInfoResponse 修改用户信息-返回数据
type UpdateInfoResponse struct {
Code int `json:"code"` // 接口调用状态。200正常其它值调用出错
Msg string `json:"msg"` // 结果说明。如果接口调用出错,那么返回错误描述。成功则返回 ok
Data GetInfoByMobileData `json:"data"` // 接口返回结果,各个接口自定义,数据结构参考具体文档说明
Success bool `json:"success"`
Message string `json:"message"`
}
// GetInfoByMobileData 根据手机号获取用户信息-data详细数据
type GetInfoByMobileData struct {
Birthday string `json:"birthday" description:"出生日期"`
@ -35,14 +63,14 @@ type GetInfoByMobileData struct {
Mobile string `json:"mobile" description:"手机号"`
Photo string `json:"photo" description:"头像地址"`
Weight *int `json:"weight" description:"体重 KG"`
CityID *int `json:"cityId" description:"城市 id"`
CityID *int64 `json:"cityId" description:"城市 id"`
ExpectedDateOfChildbirth string `json:"expectedDateOfChildbirth" description:"预产期"`
CountyID *int `json:"countyId" description:"市区 id"`
CountyID *int64 `json:"countyId" description:"市区 id"`
IsHBV *int `json:"isHbv" description:"有无 肝硬化或肝癌家族史 0无1有2未知"`
NationUUID string `json:"nationUuid" description:"民族 uuid"`
PatientUUID string `json:"patientUuid" description:"患者 uuid"`
Name string `json:"name" description:"姓名"`
ProvinceID *int `json:"provId" description:"省份 id"`
ProvinceID *int64 `json:"provId" description:"省份 id"`
Height *int `json:"height" description:"身高 cm"`
OpenId string `json:"openid" description:"openid"`
UnionId string `json:"unionid" description:"unionid"`
@ -109,7 +137,7 @@ func GetInfoByMobile(mobile string) (g *GetInfoByMobileResponse, err error) {
// 检查响应状态码
if resp.StatusCode != 200 {
return g, errors.New("登陆失败")
return g, errors.New("失败")
}
err = json.Unmarshal(body, &g)
@ -124,7 +152,86 @@ func GetInfoByMobile(mobile string) (g *GetInfoByMobileResponse, err error) {
if g.Msg != "" {
return g, errors.New(g.Msg)
} else {
return g, errors.New("登陆失败")
return g, errors.New("失败")
}
}
return g, nil
}
// UpdateInfo 修改用户信息
func UpdateInfo(reqData UpdateInfoRequest) (g *UpdateInfoResponse, err error) {
reqData.Platform = platform
reqData.Timestamp = strconv.FormatInt(time.Now().Unix(), 10)
// 将 JSON 数据编码为字节数组
jsonData, err := json.Marshal(reqData)
if err != nil {
return g, err
}
maps := make(map[string]interface{})
err = json.Unmarshal(jsonData, &maps)
if err != nil {
return g, err
}
// 获取请求签名
sign, err := GenSignature(maps)
if err != nil {
return g, err
}
// 准备请求体
requestBody := bytes.NewBuffer(jsonData)
// 设置请求 URL
url := apiUrl + "/patient-api/updateInfo"
// 创建 POST 请求
req, err := http.NewRequest("POST", url, requestBody)
if err != nil {
return g, err
}
// 设置请求头
req.Header.Set("Content-Type", "application/json")
req.Header.Set("sign", sign)
// 发送请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return g, err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return g, err
}
// 检查响应状态码
if resp.StatusCode != 200 {
return g, errors.New("失败")
}
err = json.Unmarshal(body, &g)
if err != nil {
// json解析失败
return g, err
}
utils.LogJsonInfo("修改app数据返回", g)
if g.Code != 200 {
if g.Msg != "" {
return g, errors.New(g.Msg)
} else {
return g, errors.New("失败")
}
}