From 9a3a6bbb2e1c656130bbb831c7d8e5c1ec5a414a Mon Sep 17 00:00:00 2001 From: wucongxing8150 <815046773@qq.com> Date: Thu, 15 Aug 2024 15:47:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E6=89=8B=E6=9C=BA?= =?UTF-8?q?=E5=8F=B7=E7=99=BB=E9=99=861?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/dao/BaseArea.go | 108 +++++++++++++++++++++++++++++++++++++ api/dao/BaseNation.go | 117 ++++++++++++++++++++++++++++++++++++++++ api/model/BaseArea.go | 38 +++++++++++++ api/model/BaseNation.go | 33 ++++++++++++ api/service/User.go | 55 ++++++++++++++++--- 5 files changed, 344 insertions(+), 7 deletions(-) create mode 100644 api/dao/BaseArea.go create mode 100644 api/dao/BaseNation.go create mode 100644 api/model/BaseArea.go create mode 100644 api/model/BaseNation.go diff --git a/api/dao/BaseArea.go b/api/dao/BaseArea.go new file mode 100644 index 0000000..a4f6c5f --- /dev/null +++ b/api/dao/BaseArea.go @@ -0,0 +1,108 @@ +package dao + +import ( + "gorm.io/gorm" + "gorm.io/gorm/clause" + "hepa-calc-api/api/model" + "hepa-calc-api/global" +) + +type BaseAreaDao struct { +} + +// GetBaseAreaById 获取数据-id +func (r *BaseAreaDao) GetBaseAreaById(id int64) (m *model.BaseArea, err error) { + err = global.Db.First(&m, id).Error + if err != nil { + return nil, err + } + return m, nil +} + +// GetBaseAreaPreloadById 获取数据-加载全部关联-id +func (r *BaseAreaDao) GetBaseAreaPreloadById(id int64) (m *model.BaseArea, err error) { + err = global.Db.Preload(clause.Associations).First(&m, id).Error + if err != nil { + return nil, err + } + return m, nil +} + +// DeleteBaseArea 删除 +func (r *BaseAreaDao) DeleteBaseArea(tx *gorm.DB, maps interface{}) error { + err := tx.Where(maps).Delete(&model.BaseArea{}).Error + if err != nil { + return err + } + return nil +} + +// DeleteBaseAreaById 删除-id +func (r *BaseAreaDao) DeleteBaseAreaById(tx *gorm.DB, id int64) error { + if err := tx.Delete(&model.BaseArea{}, id).Error; err != nil { + return err + } + return nil +} + +// EditBaseArea 修改 +func (r *BaseAreaDao) EditBaseArea(tx *gorm.DB, maps interface{}, data interface{}) error { + err := tx.Model(&model.BaseArea{}).Where(maps).Updates(data).Error + if err != nil { + return err + } + return nil +} + +// EditBaseAreaById 修改-id +func (r *BaseAreaDao) EditBaseAreaById(tx *gorm.DB, id int64, data interface{}) error { + err := tx.Model(&model.BaseArea{}).Where("id = ?", id).Updates(data).Error + if err != nil { + return err + } + return nil +} + +// GetBaseAreaList 获取列表 +func (r *BaseAreaDao) GetBaseAreaList(maps interface{}) (m []*model.BaseArea, err error) { + err = global.Db.Where(maps).Find(&m).Error + if err != nil { + return nil, err + } + return m, nil +} + +// GetBaseAreaCount 获取数量 +func (r *BaseAreaDao) GetBaseAreaCount(maps interface{}) (total int64, err error) { + err = global.Db.Model(&model.BaseArea{}).Where(maps).Count(&total).Error + if err != nil { + return 0, err + } + return total, nil +} + +// GetBaseAreaListRand 获取列表-随机 +func (r *BaseAreaDao) GetBaseAreaListRand(maps interface{}, limit int) (m []*model.BaseArea, err error) { + err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error + if err != nil { + return nil, err + } + return m, nil +} + +// AddBaseArea 新增 +func (r *BaseAreaDao) AddBaseArea(tx *gorm.DB, model *model.BaseArea) (*model.BaseArea, error) { + if err := tx.Create(model).Error; err != nil { + return nil, err + } + return model, nil +} + +// GetBaseArea 获取 +func (r *BaseAreaDao) GetBaseArea(maps interface{}) (m *model.BaseArea, err error) { + err = global.Db.Where(maps).First(&m).Error + if err != nil { + return nil, err + } + return m, nil +} diff --git a/api/dao/BaseNation.go b/api/dao/BaseNation.go new file mode 100644 index 0000000..755828c --- /dev/null +++ b/api/dao/BaseNation.go @@ -0,0 +1,117 @@ +package dao + +import ( + "gorm.io/gorm" + "gorm.io/gorm/clause" + "hepa-calc-api/api/model" + "hepa-calc-api/global" +) + +type BaseNationDao struct { +} + +// GetBaseNationById 获取数据-id +func (r *BaseNationDao) GetBaseNationById(id int64) (m *model.BaseNation, err error) { + err = global.Db.First(&m, id).Error + if err != nil { + return nil, err + } + return m, nil +} + +// GetBaseNationPreloadById 获取数据-加载全部关联-id +func (r *BaseNationDao) GetBaseNationPreloadById(id int64) (m *model.BaseNation, err error) { + err = global.Db.Preload(clause.Associations).First(&m, id).Error + if err != nil { + return nil, err + } + return m, nil +} + +// GetBaseNationByAppIden 获取数据-app_iden +func (r *BaseNationDao) GetBaseNationByAppIden(appIden string) (m *model.BaseNation, err error) { + err = global.Db.Where("app_iden = ?", appIden).First(&m).Error + if err != nil { + return nil, err + } + return m, nil +} + +// DeleteBaseNation 删除 +func (r *BaseNationDao) DeleteBaseNation(tx *gorm.DB, maps interface{}) error { + err := tx.Where(maps).Delete(&model.BaseNation{}).Error + if err != nil { + return err + } + return nil +} + +// DeleteBaseNationById 删除-id +func (r *BaseNationDao) DeleteBaseNationById(tx *gorm.DB, id int64) error { + if err := tx.Delete(&model.BaseNation{}, id).Error; err != nil { + return err + } + return nil +} + +// EditBaseNation 修改 +func (r *BaseNationDao) EditBaseNation(tx *gorm.DB, maps interface{}, data interface{}) error { + err := tx.Model(&model.BaseNation{}).Where(maps).Updates(data).Error + if err != nil { + return err + } + return nil +} + +// EditBaseNationById 修改-id +func (r *BaseNationDao) EditBaseNationById(tx *gorm.DB, id int64, data interface{}) error { + err := tx.Model(&model.BaseNation{}).Where("id = ?", id).Updates(data).Error + if err != nil { + return err + } + return nil +} + +// GetBaseNationList 获取列表 +func (r *BaseNationDao) GetBaseNationList(maps interface{}) (m []*model.BaseNation, err error) { + err = global.Db.Where(maps).Find(&m).Error + if err != nil { + return nil, err + } + return m, nil +} + +// GetBaseNationCount 获取数量 +func (r *BaseNationDao) GetBaseNationCount(maps interface{}) (total int64, err error) { + err = global.Db.Model(&model.BaseNation{}).Where(maps).Count(&total).Error + if err != nil { + return 0, err + } + return total, nil +} + +// GetBaseNationListRand 获取列表-随机 +func (r *BaseNationDao) GetBaseNationListRand(maps interface{}, limit int) (m []*model.BaseNation, err error) { + err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error + if err != nil { + return nil, err + } + return m, nil +} + +// AddBaseNation 新增 +func (r *BaseNationDao) AddBaseNation(tx *gorm.DB, model *model.BaseNation) (*model.BaseNation, error) { + if err := tx.Create(model).Error; err != nil { + return nil, err + } + return model, nil +} + +// GetBaseNation 获取 +func (r *BaseNationDao) GetBaseNation(maps interface{}) (m *model.BaseNation, err error) { + err = global.Db.Where(maps).First(&m).Error + if err != nil { + return nil, err + } + return m, nil +} diff --git a/api/model/BaseArea.go b/api/model/BaseArea.go new file mode 100644 index 0000000..7f2875d --- /dev/null +++ b/api/model/BaseArea.go @@ -0,0 +1,38 @@ +package model + +import ( + "gorm.io/gorm" + "hepa-calc-api/global" + "time" +) + +type BaseArea struct { + Id int64 `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"` + CreateDate time.Time `gorm:"column:create_date;type:datetime;NOT NULL" json:"create_date"` + ModifyDate time.Time `gorm:"column:modify_date;type:datetime;NOT NULL" json:"modify_date"` + Orders int `gorm:"column:orders;type:int(11)" json:"orders"` + FullName string `gorm:"column:full_name;type:longtext;NOT NULL" json:"full_name"` + Name string `gorm:"column:name;type:varchar(100);NOT NULL" json:"name"` + TreePath string `gorm:"column:tree_path;type:varchar(255);NOT NULL" json:"tree_path"` + Parent int64 `gorm:"column:parent;type:bigint(20)" json:"parent"` + CanClinic int `gorm:"column:can_clinic;type:int(11);default:1" json:"can_clinic"` + Model +} + +func (m *BaseArea) TableName() string { + return "base_area" +} + +func (m *BaseArea) BeforeCreate(tx *gorm.DB) error { + if m.Id == 0 { + m.Id = global.Snowflake.Generate().Int64() + } + + m.CreatedAt = LocalTime(time.Now()) + tx.Statement.SetColumn("CreatedAt", m.CreatedAt) + + m.UpdatedAt = LocalTime(time.Now()) + tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt) + + return nil +} diff --git a/api/model/BaseNation.go b/api/model/BaseNation.go new file mode 100644 index 0000000..a71848b --- /dev/null +++ b/api/model/BaseNation.go @@ -0,0 +1,33 @@ +package model + +import ( + "gorm.io/gorm" + "hepa-calc-api/global" + "time" +) + +// BaseNation 基础-民族表 +type BaseNation struct { + NationId int64 `gorm:"column:nation_id;type:bigint(19);primary_key;comment:主键id" json:"nation_id"` + AppIden string `gorm:"column:app_iden;type:varchar(255);comment:app唯一标识" json:"app_iden"` + NationName string `gorm:"column:nation_name;type:varchar(255);comment:民族名称" json:"nation_name"` + Model +} + +func (m *BaseNation) TableName() string { + return "base_nation" +} + +func (m *BaseNation) BeforeCreate(tx *gorm.DB) error { + if m.NationId == 0 { + m.NationId = global.Snowflake.Generate().Int64() + } + + m.CreatedAt = LocalTime(time.Now()) + tx.Statement.SetColumn("CreatedAt", m.CreatedAt) + + m.UpdatedAt = LocalTime(time.Now()) + tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt) + + return nil +} diff --git a/api/service/User.go b/api/service/User.go index 0bb125a..2a301e4 100644 --- a/api/service/User.go +++ b/api/service/User.go @@ -228,39 +228,75 @@ func (r *UserService) HandleAppUserInfo(tx *gorm.DB, user *model.User, userInfo // 省份 id if appUserInfo.Data.ProvinceID != nil { // 获取省份数据 - + baseAreaDao := dao.BaseAreaDao{} if userInfo.ProvinceId != nil { if *appUserInfo.Data.ProvinceID != *userInfo.ProvinceId { userInfoData["province_id"] = appUserInfo.Data.ProvinceID + + baseArea, err := baseAreaDao.GetBaseAreaById(int64(*appUserInfo.Data.ProvinceID)) + if err != nil { + return err + } + userInfoData["province_name"] = baseArea.Name } } else { userInfoData["province_id"] = appUserInfo.Data.ProvinceID + + baseArea, err := baseAreaDao.GetBaseAreaById(int64(*appUserInfo.Data.ProvinceID)) + if err != nil { + return err + } + userInfoData["province_name"] = baseArea.Name } } // 城市 id if appUserInfo.Data.CityID != nil { // 获取城市数据 - + baseAreaDao := dao.BaseAreaDao{} if userInfo.CityId != nil { if *appUserInfo.Data.CityID != *userInfo.CityId { userInfoData["city_id"] = appUserInfo.Data.CityID + + baseArea, err := baseAreaDao.GetBaseAreaById(int64(*appUserInfo.Data.ProvinceID)) + if err != nil { + return err + } + userInfoData["city_name"] = baseArea.Name } } else { userInfoData["city_id"] = appUserInfo.Data.CityID + + baseArea, err := baseAreaDao.GetBaseAreaById(int64(*appUserInfo.Data.ProvinceID)) + if err != nil { + return err + } + userInfoData["city_name"] = baseArea.Name } } // 市区 id if appUserInfo.Data.CountyID != nil { // 获取区县数据 - + baseAreaDao := dao.BaseAreaDao{} if userInfo.CountyId != nil { if *appUserInfo.Data.CountyID != *userInfo.CountyId { userInfoData["county_id"] = appUserInfo.Data.CountyID + + baseArea, err := baseAreaDao.GetBaseAreaById(int64(*appUserInfo.Data.ProvinceID)) + if err != nil { + return err + } + userInfoData["county_name"] = baseArea.Name } } else { userInfoData["county_id"] = appUserInfo.Data.CountyID + + baseArea, err := baseAreaDao.GetBaseAreaById(int64(*appUserInfo.Data.ProvinceID)) + if err != nil { + return err + } + userInfoData["county_name"] = baseArea.Name } } @@ -289,13 +325,18 @@ func (r *UserService) HandleAppUserInfo(tx *gorm.DB, user *model.User, userInfo // 民族 if appUserInfo.Data.NationUUID != "" { // 获取民族数据 - var nationId int64 + baseNationDao := dao.BaseNationDao{} + baseNation, err := baseNationDao.GetBaseNationByAppIden(appUserInfo.Data.NationUUID) + if err != nil { + return err + } + if userInfo.NationId != nil { - if nationId != *userInfo.NationId { - userInfoData["nation_id"] = nationId + if baseNation.NationId != *userInfo.NationId { + userInfoData["nation_id"] = baseNation.NationId } } else { - userInfoData["nation_id"] = nationId + userInfoData["nation_id"] = baseNation.NationId } }