2023-11-02 04:34:46 +08:00
package app
import (
"errors"
"github.com/duke-git/lancet/v2/datetime"
"go.uber.org/zap"
"gorm.io/gorm"
"miniapp/global"
"miniapp/model/app"
"miniapp/model/app/request"
"miniapp/model/app/response"
"miniapp/model/common"
2024-03-21 21:08:45 +08:00
"strings"
2023-11-02 04:34:46 +08:00
"time"
)
type UserService struct { }
func ( UserService ) GetUser ( user * app . User ) ( err error ) {
return global . GVA_DB . Take ( & user , user ) . Error
}
func ( UserService ) GetOrCreate ( user * app . User ) ( err error ) {
//err = global.GVA_DB.Take(&user, user).Error
err = global . GVA_DB . Model ( & user ) . Where ( "phone = ?" , user . Phone ) . First ( & user ) . Error
if err == nil {
return
}
// 如果是没查询到记录,则创建用户
if err == gorm . ErrRecordNotFound {
// 用户不存在,创建用户
if err = global . GVA_DB . Create ( & user ) . Error ; err != nil {
global . GVA_LOG . Error ( "账号创建失败: %v" , zap . Error ( err ) )
err = errors . New ( "登录失败" )
return
}
}
return
}
// CheckUnionIdIsExist 检查UnionId和OpenId是否存在
func ( UserService ) CheckUnionIdIsExist ( unionId , openId string ) bool {
var count int64
err := global . GVA_DB . Model ( & app . User { } ) . Where ( "wechat_union_id = ? and wechat_open_id = ?" , unionId , openId ) . Count ( & count ) . Error
if err != nil {
return false
}
return count > 0
}
// UpdateUserInfo 更新普通用户信息
func ( UserService ) UpdateUserInfo ( e * app . User ) ( err error ) {
2024-03-21 21:08:45 +08:00
tUser := app . User { }
err = global . GVA_DB . Where ( "id = ?" , e . ID ) . First ( & tUser ) . Error
err = global . GVA_DB . Updates ( & e ) . Error
if err != nil {
global . GVA_LOG . Error ( "更新用户信息失败" , zap . Error ( err ) )
return
}
2024-03-22 12:29:31 +08:00
if e . SurgeryTime != "" || e . SurgeryTime != tUser . SurgeryTime || e . HospitalId != tUser . HospitalId {
2024-03-21 21:08:45 +08:00
2024-03-22 12:29:31 +08:00
err = global . GVA_DB . Delete ( & common . UserTodo { } , "user_id = ?" , e . ID ) . Error
if err != nil {
global . GVA_LOG . Error ( "清除用户Todo列表失败" , zap . Error ( err ) )
return
}
var hospital common . Hospital
err = global . GVA_DB . Where ( "id = ?" , e . HospitalId ) . Preload ( "Todos" ) . First ( & hospital ) . Error
if err != nil {
global . GVA_LOG . Error ( "获取医院信息失败" , zap . Error ( err ) )
return
}
if len ( hospital . Todos ) != 0 {
var userTodos [ ] common . UserTodo
for _ , todo := range hospital . Todos {
for _ , s := range strings . Split ( todo . RemindTime , "," ) {
userTodos = append ( userTodos , common . UserTodo {
Content : todo . Content ,
UserId : int ( e . ID ) ,
IsFinish : 0 ,
RemindTime : s ,
RemindPeriod : todo . RemindPeriod ,
} )
}
2024-03-21 21:08:45 +08:00
}
2024-03-22 12:29:31 +08:00
err = global . GVA_DB . Create ( & userTodos ) . Error
2024-03-21 21:08:45 +08:00
if err != nil {
2024-03-22 12:29:31 +08:00
global . GVA_LOG . Error ( "创建用户Todo列表失败" , zap . Error ( err ) )
2024-03-21 21:08:45 +08:00
return
}
}
}
return err
2023-11-02 04:34:46 +08:00
}
// GetAverageUserList 查询普通用户列表
func ( UserService ) GetAverageUserList ( p request . GetUserList ) ( records [ ] response . UserItem , count int64 , err error ) {
tx := global . GVA_DB . Scopes ( Page ( p . Page , p . PageSize ) ) .
Table ( "`t_user` AS tu" ) .
Select ( "tu.id AS id,tu.avatar AS avatar,tu.phone AS phone,tu.nickname AS nickname,tu.sex AS sex,tu.created_at AS created_at,tu.last_login_at AS last_login_at,tu.`status` AS `status`" ) .
Order ( "tu.last_login_at DESC" )
if p . Phone != "" {
tx = tx . Where ( "tu.phone LIKE ?" , "%" + p . Phone + "%" )
}
//加一个姓名的模糊查询
if p . Name != "" {
tx = tx . Where ( "tu.nickname LIKE ?" , "&" + p . Name + "%" )
}
if p . Status != "" {
tx = tx . Where ( "tu.`status` = ?" , p . Status )
}
if p . StartAt != "" || p . EndAt != "" {
start := time . Date ( 2021 , 1 , 1 , 0 , 0 , 0 , 0 , time . Local )
end := time . Now ( )
// 处理数据
if p . StartAt != "" {
st , err := datetime . FormatStrToTime ( p . StartAt , "yyyy-mm-dd" )
if err != nil {
global . GVA_LOG . Error ( "解析开始时间异常" )
return nil , 0 , errors . New ( "解析开始时间异常" )
}
start = datetime . BeginOfDay ( st )
}
if p . EndAt != "" {
et , err := datetime . FormatStrToTime ( p . EndAt , "yyyy-mm-dd" )
if err != nil {
global . GVA_LOG . Error ( "解析结束时间异常" )
return nil , 0 , errors . New ( "解析结束时间异常" )
}
end = datetime . EndOfDay ( et )
}
// 处理查询条件
tx . Where ( "tor.pay_at BETWEEN ? AND ?" , start , end )
}
err = tx . Offset ( - 1 ) . Limit ( - 1 ) . Count ( & count ) . Find ( & records ) . Error
return
}
func ( UserService ) UpdateLastLoginInfo ( username int ) {
now := time . Now ( ) . Local ( )
global . GVA_DB . Model ( & app . User { } ) .
Where ( "id = ?" , username ) .
2024-01-11 16:03:51 +08:00
Updates ( app . User { LastLoginAt : & now } )
2023-11-02 04:34:46 +08:00
}
// UpdateUserHospital 更新用户医院信息
func ( UserService ) UpdateUserHospital ( r * request . ChangeUserHospital ) ( err error ) {
err = global . GVA_DB . Delete ( & common . UserTodo { } , "user_id = ?" , r . UserId ) . Error
if err != nil {
global . GVA_LOG . Error ( "清除用户Todo列表失败" , zap . Error ( err ) )
return
}
var hospital common . Hospital
err = global . GVA_DB . Where ( "id = ?" , r . HospitalId ) . Preload ( "Todos" ) . First ( & hospital ) . Error
if err != nil {
global . GVA_LOG . Error ( "获取医院信息失败" , zap . Error ( err ) )
return
}
2024-01-11 16:03:51 +08:00
if len ( hospital . Todos ) != 0 {
2023-11-02 04:34:46 +08:00
var userTodos [ ] common . UserTodo
for _ , todo := range hospital . Todos {
2024-03-21 21:08:45 +08:00
for _ , s := range strings . Split ( todo . RemindTime , "," ) {
userTodos = append ( userTodos , common . UserTodo {
Content : todo . Content ,
UserId : r . UserId ,
IsFinish : 0 ,
RemindTime : s ,
RemindPeriod : todo . RemindPeriod ,
} )
}
2023-11-02 04:34:46 +08:00
}
err = global . GVA_DB . Create ( & userTodos ) . Error
if err != nil {
global . GVA_LOG . Error ( "创建用户Todo列表失败" , zap . Error ( err ) )
return
}
}
2024-01-11 16:03:51 +08:00
2024-03-21 21:08:45 +08:00
//t, _ := time.Parse("2006-01-02", r.SurgeryTime)
2024-01-11 16:03:51 +08:00
2024-03-21 21:08:45 +08:00
err = global . GVA_DB . Table ( "t_user" ) . Where ( "id = ?" , r . UserId ) . Updates ( app . User { IsInfo : 1 , HospitalId : r . HospitalId , SurgeryTime : r . SurgeryTime } ) . Error
2024-01-11 16:03:51 +08:00
if err != nil {
global . GVA_LOG . Error ( "更新用户信息失败" , zap . Error ( err ) )
return
}
2023-11-02 04:34:46 +08:00
return
}
func ( s UserService ) GetUserInfo ( id string ) ( user response . UserVO , err error ) {
var u app . User
err = global . GVA_DB . Where ( "id = ?" , id ) . First ( & u ) . Error
user . ParseOrdinary ( u )
2024-01-11 16:03:51 +08:00
global . GVA_DB . Table ( "t_user_todo" ) . Where ( "user_id = ?" , id ) . Find ( & user . Todos )
var hid [ ] string
global . GVA_DB . Table ( "hospital_notes" ) . Where ( "hospital_id = ?" , user . HospitalId ) . Select ( "notes_id" ) . Find ( & hid )
global . GVA_DB . Table ( "sys_notes" ) . Where ( "id in (?)" , hid ) . Find ( & user . Notes )
2023-11-02 04:34:46 +08:00
if u . IsSurgery == 1 {
user . TimeNote = "距离下次复查时间还剩:"
} else {
user . TimeNote = "距离手术时间还剩:"
}
return
}