diff --git a/app/admin/hdl/hdl_user.go b/app/admin/hdl/hdl_user.go index 71a9399..9fddbb3 100644 --- a/app/admin/hdl/hdl_user.go +++ b/app/admin/hdl/hdl_user.go @@ -4,6 +4,7 @@ import ( "applet/app/admin/lib/validate" "applet/app/admin/md" "applet/app/admin/svc" + svc2 "applet/app/admin/svc/enterprise_manage" "applet/app/db" "applet/app/e" "applet/app/enum" @@ -80,6 +81,63 @@ func UserList(c *gin.Context) { return } +func CentralKitchenForSchoolUserList(c *gin.Context) { + var req md.EnterpriseUserListReq + err := c.ShouldBindJSON(&req) + if err != nil { + err = validate.HandleValidateErr(err) + err1 := err.(e.E) + e.OutErr(c, err1.Code, err1.Error()) + return + } + + resp, err := svc2.EnterpriseUserListByCentralKitchenForSchoolV2(req) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + e.OutSuc(c, resp, nil) + return +} + +func CentralKitchenForSchoolUserUpdate(c *gin.Context) { + var req md.CentralKitchenForSchoolUserUpdateReqV2 + err := c.ShouldBindJSON(&req) + if err != nil { + err = validate.HandleValidateErr(err) + err1 := err.(e.E) + e.OutErr(c, err1.Code, err1.Error()) + return + } + + err = svc2.CentralKitchenForSchoolUserUpdateV2(req) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + e.OutSuc(c, "success", nil) + return +} + +func CentralKitchenForSchoolUserDelete(c *gin.Context) { + var req md.CentralKitchenForSchoolUserDeleteReq + err := c.ShouldBindJSON(&req) + if err != nil { + err = validate.HandleValidateErr(err) + err1 := err.(e.E) + e.OutErr(c, err1.Code, err1.Error()) + return + } + + err = svc2.CentralKitchenForSchoolUserDeleteV2(req) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + e.OutSuc(c, "success", nil) + return +} + func UserUpdate(c *gin.Context) { var req md.UserList err := c.ShouldBindJSON(&req) diff --git a/app/admin/md/md_enterprise_manage.go b/app/admin/md/md_enterprise_manage.go index d92e63e..280a177 100644 --- a/app/admin/md/md_enterprise_manage.go +++ b/app/admin/md/md_enterprise_manage.go @@ -1,7 +1,7 @@ package md type EnterpriseUserListReq struct { - EnterpriseId int `json:"enterprise_id" binding:"required" label:"企业id"` + EnterpriseId int `json:"enterprise_id" label:"企业id"` Limit int `json:"limit" binding:"required"` Page int `json:"page" binding:"required"` Nickname string `json:"nickname" label:"支付宝昵称"` @@ -48,8 +48,22 @@ type CentralKitchenForSchoolUserUpdateReq struct { } `json:"user_identities" label:"身份列表"` } +type CentralKitchenForSchoolUserUpdateReqV2 struct { + Uid int `json:"uid" binding:"required" label:"用户id"` + Nickname string `json:"nickname" binding:"required" label:"支付宝昵称"` + Phone string `json:"phone" binding:"required" label:"手机号"` + BindUserIdentities []struct { + EnterpriseId int `json:"enterprise_id" label:"企业id"` + UserIdentityId int `json:"user_identity_id" label:"用户身份id"` + IdNo string `json:"id_no" label:"身份证号"` + Name string `json:"name" label:"姓名"` + GradeId int `json:"grade_id" label:"年级"` + ClassId int `json:"class_id" label:"班级"` + } `json:"user_identities" label:"身份列表"` +} + type CentralKitchenForSchoolUserDeleteReq struct { - EnterpriseId int `json:"enterprise_id" binding:"required" label:"企业id"` + EnterpriseId int `json:"enterprise_id" label:"企业id"` Uids []int `json:"uids" binding:"required" label:"用户id"` } diff --git a/app/admin/svc/enterprise_manage/svc_central_kitchen_for_school.go b/app/admin/svc/enterprise_manage/svc_central_kitchen_for_school.go index 3e78baf..1813f32 100644 --- a/app/admin/svc/enterprise_manage/svc_central_kitchen_for_school.go +++ b/app/admin/svc/enterprise_manage/svc_central_kitchen_for_school.go @@ -145,6 +145,139 @@ func CentralKitchenForSchoolUserUpdate(req md.CentralKitchenForSchoolUserUpdateR return session.Commit() } +func CentralKitchenForSchoolUserUpdateV2(req md.CentralKitchenForSchoolUserUpdateReqV2) (err error) { + //1、删除当前用户&&央厨学校下的所有身份对应的 `class_with_user` 记录 + var userIdentities []model.UserIdentity + err = db.Db.Where("uid =?", req.Uid).And("identity =? or identity =?", enum2.UserIdentityForCentralKitchenForTeacher, enum2.UserIdentityForCentralKitchenForStudent).Find(&userIdentities) + if err != nil { + return + } + var userIdentityIds []int + for _, v := range userIdentities { + userIdentityIds = append(userIdentityIds, v.Id) + } + _, err = db.Db.In("user_identity_id", userIdentityIds).Delete(model.ClassWithUser{}) + if err != nil { + return + } + + session := db.Db.NewSession() + defer session.Close() + session.Begin() + + //2、新增 / 编辑 数据 + now := time.Now() + userIdentityDb := db.UserIdentityDb{} + userIdentityDb.Set(req.Uid) + + classWithUserDb := db.ClassWithUserDb{} + classWithUserDb.Set() + + var newUserIdentityIds []string + for _, v := range req.BindUserIdentities { + if v.UserIdentityId == 0 { + // 新增 + if v.ClassId == -1 { + //TODO::为老师身份 + _, err3 := userIdentityDb.UserIdentityInsertBySession(session, &model.UserIdentity{ + Uid: req.Uid, + Name: v.Name, + IdNo: v.IdNo, + Kind: enum2.UserIdentityKindForCommon, + Identity: enum2.UserIdentityForCentralKitchenForTeacher, + EnterpriseId: v.EnterpriseId, + State: enum2.UserIdentityStateForNormal, + Memo: "", + CreateAt: now.Format("2006-01-02 15:04:05"), + UpdateAt: now.Format("2006-01-02 15:04:05"), + }) + if err3 != nil { + _ = session.Rollback() + return err3 + } + continue + } + + insertId, err1 := userIdentityDb.UserIdentityInsertBySession(session, &model.UserIdentity{ + Uid: req.Uid, + Name: v.Name, + IdNo: v.IdNo, + Kind: enum2.UserIdentityKindForCommon, + Identity: enum2.UserIdentityForCentralKitchenForStudent, + EnterpriseId: v.EnterpriseId, + State: enum2.UserIdentityStateForNormal, + Memo: "", + CreateAt: now.Format("2006-01-02 15:04:05"), + UpdateAt: now.Format("2006-01-02 15:04:05"), + }) + if err1 != nil { + _ = session.Rollback() + return err1 + } + + _, err2 := classWithUserDb.ClassWithUserInsertBySession(session, &model.ClassWithUser{ + UserIdentityId: insertId, + ClassId: v.ClassId, + CreateAt: now.Format("2006-01-02 15:04:05"), + UpdateAt: now.Format("2006-01-02 15:04:05"), + }) + if err2 != nil { + _ = session.Rollback() + return err1 + } + + newUserIdentityIds = append(newUserIdentityIds, utils.IntToStr(insertId)) + } else { + //编辑 + identity, err2 := userIdentityDb.GetUserIdentity(v.UserIdentityId) + if err != nil { + _ = session.Rollback() + return err2 + } + if identity == nil { + _ = session.Rollback() + return errors.New("未查询到对应的身份信息记录") + } + identity.IdNo = v.IdNo + identity.Name = v.Name + if v.ClassId == -1 { + //TODO::为老师身份 + identity.Identity = enum2.UserIdentityForCentralKitchenForTeacher + } else { + _, err3 := classWithUserDb.ClassWithUserInsertBySession(session, &model.ClassWithUser{ + UserIdentityId: v.UserIdentityId, + ClassId: v.ClassId, + CreateAt: now.Format("2006-01-02 15:04:05"), + UpdateAt: now.Format("2006-01-02 15:04:05"), + }) + if err3 != nil { + _ = session.Rollback() + return err3 + } + } + _, err3 := userIdentityDb.UserIdentityUpdateBySession(session, v.UserIdentityId, identity, "id_no", "name", "identity") + if err3 != nil { + _ = session.Rollback() + return err3 + } + + newUserIdentityIds = append(newUserIdentityIds, utils.IntToStr(v.UserIdentityId)) + } + } + + //3、删除需要删除的身份信息 + for _, v := range userIdentityIds { + if !utils.InArr(utils.IntToStr(v), newUserIdentityIds) { + _, err = session.Where("id = ?", v).Delete(model.UserIdentity{}) + if err != nil { + _ = session.Rollback() + return + } + } + } + return session.Commit() +} + func CentralKitchenForSchoolUserDelete(req md.CentralKitchenForSchoolUserDeleteReq) (err error) { //1、删除 class_with_user var userIdentities []model.UserIdentity @@ -169,6 +302,32 @@ func CentralKitchenForSchoolUserDelete(req md.CentralKitchenForSchoolUserDeleteR return } +func CentralKitchenForSchoolUserDeleteV2(req md.CentralKitchenForSchoolUserDeleteReq) (err error) { + //1、删除 class_with_user + var userIdentities []model.UserIdentity + err = db.Db.Where("identity =? or identity =?", enum2.UserIdentityForCentralKitchenForTeacher, enum2.UserIdentityForCentralKitchenForStudent). + In("uid", req.Uids).Find(&userIdentities) + if err != nil { + return + } + var userIdentityIds []int + for _, v := range userIdentities { + userIdentityIds = append(userIdentityIds, v.Id) + } + _, err = db.Db.In("user_identity_id", userIdentityIds).Delete(model.ClassWithUser{}) + if err != nil { + return + } + + //2、删除 user_identity + _, err = db.Db.Where("identity =? or identity =?", enum2.UserIdentityForCentralKitchenForTeacher, enum2.UserIdentityForCentralKitchenForStudent). + Delete(model.UserIdentity{}) + if err != nil { + return + } + return +} + func CentralKitchenForSchoolStudentList(req md.CentralKitchenForSchoolStudentListReq) (resp []md.CentralKitchenForSchoolStudentListResp, count int64, err error) { var classWithUserIdentityIdsOne []int var classWithUserIdentityIdsTwo []int diff --git a/app/admin/svc/enterprise_manage/svc_enterprise_manage.go b/app/admin/svc/enterprise_manage/svc_enterprise_manage.go index 5512204..f28cb4c 100644 --- a/app/admin/svc/enterprise_manage/svc_enterprise_manage.go +++ b/app/admin/svc/enterprise_manage/svc_enterprise_manage.go @@ -64,7 +64,6 @@ func EnterpriseUserListByCentralKitchenForSchool(req md.EnterpriseUserListReq) ( CreateAt: v.CreateAt, } for _, v1 := range userIdentitiesMap[utils.IntToStr(v.Id)] { - fmt.Println(">>>>>>>>>>>>", userIdentitiesMap[utils.IntToStr(v.Id)]) //TODO::判断是否为老师 if v1.Identity == enum2.UserIdentityForCentralKitchenForTeacher { temp.UserIdentities = append(temp.UserIdentities, struct { @@ -103,6 +102,89 @@ func EnterpriseUserListByCentralKitchenForSchool(req md.EnterpriseUserListReq) ( return } +func EnterpriseUserListByCentralKitchenForSchoolV2(req md.EnterpriseUserListReq) (resp md.EnterpriseUserListByCentralKitchenForSchoolResp, err error) { + sess := db.Db.Desc("user.id") + if req.Nickname != "" { + sess.And("user.nickname like ?", "%"+req.Nickname+"%") + } + if req.Phone != "" { + sess.And("user.phone like ?", "%"+req.Phone+"%") + } + if req.IsTeacher == 1 { + sess.And("user.phone like ?", "%"+req.Phone+"%") + } + if req.IsTeacher == 0 { + sess.And("user_identity.identity = ? or user_identity.identity = ?", enum2.UserIdentityForCentralKitchenForTeacher, enum2.UserIdentityForCentralKitchenForStudent) + } + if req.IsTeacher == 1 { + sess.And("user_identity.identity = ?", enum2.UserIdentityForCentralKitchenForTeacher) + } + if req.IsTeacher == 2 { + sess.And("user_identity.identity = ?", enum2.UserIdentityForCentralKitchenForStudent) + } + var m []*db.UserWithUserIdentity + count, err := sess. + Join("LEFT", "user_identity", "user.id = user_identity.uid"). + Limit(req.Limit, (req.Page-1)*req.Limit).FindAndCount(&m) + + var userIdentitiesMap = map[string][]db.UserIdentityWithClass{} + userIdentityDb := db.UserIdentityDb{} + for _, v := range m { + userIdentityDb.Set(v.User.Id) + userIdentities, err1 := userIdentityDb.FindUserIdentityWithClass() + if err1 != nil { + return resp, err1 + } + for _, v1 := range *userIdentities { + userIdentitiesMap[utils.IntToStr(v1.Uid)] = append(userIdentitiesMap[utils.IntToStr(v1.Uid)], v1) + } + } + fmt.Println("!!!!!!!!!") + resp.Total = count + classWithUserDb := db.ClassWithUserDb{} + classWithUserDb.Set() + for _, v := range m { + temp := md.EnterpriseUserListByCentralKitchenForSchoolStruct{ + Id: v.User.Id, + Nickname: v.Nickname, + Phone: v.Phone, + Avatar: v.Avatar, + IsTeacher: 0, + CreateAt: v.User.CreateAt, + } + for _, v1 := range userIdentitiesMap[utils.IntToStr(v.User.Id)] { + //TODO::判断是否为老师 + if v1.Identity == enum2.UserIdentityForCentralKitchenForTeacher { + temp.UserIdentities = append(temp.UserIdentities, struct { + Id int `json:"id" label:"身份id"` + IdNo string `json:"id_no" label:"身份证号"` + SchoolName string `json:"school_name" label:"学校名"` + Name string `json:"name" label:"姓名"` + Grade string `json:"grade" label:"年级"` + GradeId int `json:"grade_id" label:"年级id"` + Class string `json:"class" label:"班级"` + ClassId int `json:"class_id" label:"班级id"` + }{Id: v1.UserIdentity.Id, IdNo: v1.IdNo, SchoolName: "教师", Name: v1.UserIdentity.Name, Grade: "教师", Class: "教师", GradeId: 0, ClassId: 0}) + temp.IsTeacher = 1 + } + if v1.Identity == enum2.UserIdentityForCentralKitchenForStudent { + temp.UserIdentities = append(temp.UserIdentities, struct { + Id int `json:"id" label:"身份id"` + IdNo string `json:"id_no" label:"身份证号"` + SchoolName string `json:"school_name" label:"学校名"` + Name string `json:"name" label:"姓名"` + Grade string `json:"grade" label:"年级"` + GradeId int `json:"grade_id" label:"年级id"` + Class string `json:"class" label:"班级"` + ClassId int `json:"class_id" label:"班级id"` + }{Id: v1.UserIdentity.Id, IdNo: v1.UserIdentity.IdNo, SchoolName: v1.Enterprise.Name, Name: v1.UserIdentity.Name, Grade: v1.Grade.Name, Class: v1.Class.Name, GradeId: v1.Grade.Id, ClassId: v1.Class.Id}) + } + } + resp.List = append(resp.List, temp) + } + return +} + func EnterpriseUserListByNursingHome(req md.EnterpriseUserListReq) (resp md.EnterpriseUserListByNursingHomeResp, err error) { //1、判断是否过滤 "教师" userIdentityDb := db.UserIdentityDb{} @@ -140,7 +222,6 @@ func EnterpriseUserListByNursingHome(req md.EnterpriseUserListReq) (resp md.Ente CreateAt: v.CreateAt, } for _, v1 := range userIdentitiesMap[utils.IntToStr(v.Id)] { - fmt.Println(">>>>>>>>>>>>", userIdentitiesMap[utils.IntToStr(v.Id)]) temp.UserIdentities = append(temp.UserIdentities, struct { Id int `json:"id" label:"身份id"` IdNo string `json:"id_no" label:"身份证号"` diff --git a/app/db/db_user.go b/app/db/db_user.go index 47c95b6..7392934 100644 --- a/app/db/db_user.go +++ b/app/db/db_user.go @@ -89,3 +89,12 @@ func (userDb *UserDb) DeleteUser(id interface{}) (int64, error) { return userDb.Db.Where("enterprise_id = ?", id).Delete(model.User{}) } } + +type UserWithUserIdentity struct { + model.User `xorm:"extends"` + model.UserIdentity `xorm:"extends"` +} + +func (UserWithUserIdentity) TableName() string { + return "user" +} diff --git a/app/db/db_user_identity.go b/app/db/db_user_identity.go index 1b148a5..b0d0ee9 100644 --- a/app/db/db_user_identity.go +++ b/app/db/db_user_identity.go @@ -81,6 +81,20 @@ func (userIdentityDb *UserIdentityDb) FindUserIdentity() (*[]UserIdentityWithEnt return &m, nil } +func (userIdentityDb *UserIdentityDb) FindUserIdentityWithClass() (*[]UserIdentityWithClass, error) { + var m []UserIdentityWithClass + if err := userIdentityDb.Db. + Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id"). + Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id"). + Join("LEFT", "class", "class_with_user.class_id = class.id"). + Join("LEFT", "grade", "class.grade_id = grade.id"). + Where("user_identity.state =?", enum.UserIdentityStateForNormal). + And("user_identity.uid =?", userIdentityDb.Uid).Desc("user_identity.id").Find(&m); err != nil { + return nil, logx.Error(err) + } + return &m, nil +} + func (userIdentityDb *UserIdentityDb) UserIdentity(id int) (m *UserIdentityWithEnterprise, err error) { m = new(UserIdentityWithEnterprise) has, err := userIdentityDb.Db.Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id"). @@ -205,3 +219,15 @@ type UserIdentityWithEnterprise struct { func (UserIdentityWithEnterprise) TableName() string { return "user_identity" } + +type UserIdentityWithClass struct { + model.UserIdentity `xorm:"extends"` + model.Enterprise `xorm:"extends"` + model.ClassWithUser `xorm:"extends"` + model.Class `xorm:"extends"` + model.Grade `xorm:"extends"` +} + +func (UserIdentityWithClass) TableName() string { + return "user_identity" +} diff --git a/app/router/admin_router.go b/app/router/admin_router.go index 984fa51..7c203e0 100644 --- a/app/router/admin_router.go +++ b/app/router/admin_router.go @@ -114,7 +114,11 @@ func rDataStatistics(r *gin.RouterGroup) { } func rUser(r *gin.RouterGroup) { - r.POST("/list", hdl2.UserList) //列表 + r.POST("/list", hdl2.UserList) //列表 + r.POST("centralKitchenForSchool/list", hdl2.CentralKitchenForSchoolUserList) //央厨用户列表 + r.POST("/centralKitchenForSchool/userUpdate", hdl2.CentralKitchenForSchoolUserUpdate) //"央厨-学校"用户编辑 + r.POST("/centralKitchenForSchool/userDelete", hdl2.CentralKitchenForSchoolUserDelete) //"央厨-学校"用户删除 + r.POST("/update", hdl2.UserUpdate) //编辑 r.DELETE("/delete/:id", hdl2.UserDelete) //删除 }