diff --git a/dsBaseRpc/RpcService/BaseOrganization/BaseOrganizationService/BaseOrganizationService.go b/dsBaseRpc/RpcService/BaseOrganization/BaseOrganizationService/BaseOrganizationService.go index 5bb8787b..5c6cb51a 100644 --- a/dsBaseRpc/RpcService/BaseOrganization/BaseOrganizationService/BaseOrganizationService.go +++ b/dsBaseRpc/RpcService/BaseOrganization/BaseOrganizationService/BaseOrganizationService.go @@ -16,6 +16,7 @@ import ( "dsBaseRpc/RpcService/SysLoginperson/SysLoginpersonService" "dsBaseRpc/Utils/CommonUtil" "dsBaseRpc/Utils/DateUtil" + "dsBaseRpc/Utils/DbUtil" "dsBaseRpc/Utils/LogUtil" "dsBaseRpc/Utils/PinYinUtil" "dsBaseRpc/Utils/SqlKit" @@ -741,12 +742,19 @@ func UpdateManager(bureauId string, bUse int32) error { model.Lxdh = "18888888888" model.Dzxx = "xx@dsideal.com" model.BUse = 1 - _, err := BaseTeacherDao.AddBaseTeacher(*model) + var db = DbUtil.Engine + var session = db.NewSession() + _, err := BaseTeacherDao.AddBaseTeacher(session, *model) if err != nil { + session.Rollback() return err } //生成人员的组织机构数据 - BaseTeacherDao.AddTeacherOrgInfo([]string{personId}, bureauId, 1, 1) + _, _, _, err = BaseTeacherDao.AddTeacherOrgInfo(session, []string{personId}, bureauId, 1, 1) + if err != nil { + session.Rollback() + return err + } //(2)生成登录名和密码 accountArray := SysLoginpersonService.GenerateLoginAccount(1, 1) modelLoginPerson := new(models.TSysLoginperson) @@ -758,10 +766,13 @@ func UpdateManager(bureauId string, bUse int32) error { modelLoginPerson.PersonId = personId modelLoginPerson.PersonName = model.Xm modelLoginPerson.BUse = 1 - _, err = SysLoginpersonDao.AddSysLoginPerson(*modelLoginPerson) + _, err = SysLoginpersonDao.AddSysLoginPerson(session, *modelLoginPerson) if err != nil { + session.Rollback() return err } + session.Commit() + //(3)、t_base_role modelBaseRolePerson := new(models.TBaseRolePerson) modelBaseRolePerson.Id = CommonUtil.GetUUID() diff --git a/dsBaseRpc/RpcService/BaseTeacher/BaseTeacherDao/BaseTeacherDao.go b/dsBaseRpc/RpcService/BaseTeacher/BaseTeacherDao/BaseTeacherDao.go index 5dc61c7a..c3e3d800 100644 --- a/dsBaseRpc/RpcService/BaseTeacher/BaseTeacherDao/BaseTeacherDao.go +++ b/dsBaseRpc/RpcService/BaseTeacher/BaseTeacherDao/BaseTeacherDao.go @@ -22,6 +22,7 @@ import ( "github.com/360EntSecGroup-Skylar/excelize/v2" "github.com/pkg/errors" "github.com/xormplus/builder" + "github.com/xormplus/xorm" "strconv" ) @@ -65,9 +66,9 @@ func GetMaxSortId(OrgId string) int64 { } //增加 -func AddBaseTeacher(model models.TBaseTeacher) (int64, error) { +func AddBaseTeacher(session *xorm.Session, model models.TBaseTeacher) (int64, error) { //插入基本表 - count, err := db.Insert(model) + count, err := session.Insert(model) return count, err } diff --git a/dsBaseRpc/RpcService/BaseTeacher/BaseTeacherService/BaseTeacherService.go b/dsBaseRpc/RpcService/BaseTeacher/BaseTeacherService/BaseTeacherService.go index 15be538f..b378ecb5 100644 --- a/dsBaseRpc/RpcService/BaseTeacher/BaseTeacherService/BaseTeacherService.go +++ b/dsBaseRpc/RpcService/BaseTeacher/BaseTeacherService/BaseTeacherService.go @@ -12,6 +12,7 @@ import ( "dsBaseRpc/RpcService/SysLoginperson/SysLoginpersonService" "dsBaseRpc/Utils/CommonUtil" "dsBaseRpc/Utils/DateUtil" + "dsBaseRpc/Utils/DbUtil" "dsBaseRpc/Utils/ExcelUtil" "dsBaseRpc/Utils/IdCardUtil" "dsBaseRpc/Utils/LogUtil" @@ -156,9 +157,14 @@ func (s *Rpc) AddBaseTeacher(ctx context.Context, in *BaseTeacherProto.ModelArg) //教师的身份ID model.IdentityId = 2 + //创建事务会话 + var db = DbUtil.Engine + var session = db.NewSession() //调用DAO保存 - _, err := BaseTeacherDao.AddBaseTeacher(*model) + _, err := BaseTeacherDao.AddBaseTeacher(session, *model) if err != nil { + //事务回滚 + session.Rollback() reply.Success = false reply.Message = Const.DataBaseActionError LogUtil.Error(ErrorConst.SqlUpdateError, "执行AddBaseTeacher时发生严重错误:"+err.Error()) @@ -173,18 +179,36 @@ func (s *Rpc) AddBaseTeacher(ctx context.Context, in *BaseTeacherProto.ModelArg) } else { fromSortId = in.SortId } - _, bureauId, _, _ := BaseTeacherDao.AddTeacherOrgInfo(personIds, in.OrgId, 1, fromSortId) + _, bureauId, _, err := BaseTeacherDao.AddTeacherOrgInfo(personIds, in.OrgId, 1, fromSortId) + if err != nil { + reply.Success = false + reply.Message = Const.DataBaseActionError + LogUtil.Error(ErrorConst.SqlUpdateError, "执行AddTeacherOrgInfo时发生严重错误:"+err.Error()) + return &reply, err + } //记录教师在此单位下的职务信息 - BaseTeacherDao.AddTeacherPosition(personIds, bureauId, in.PositionIds) - + _, _, err = BaseTeacherDao.AddTeacherPosition(personIds, bureauId, in.PositionIds) + if err != nil { + //事务回滚 + session.Rollback() + reply.Success = false + reply.Message = Const.DataBaseActionError + LogUtil.Error(ErrorConst.SqlUpdateError, "执行AddLoginperson时发生严重错误:"+err.Error()) + return &reply, err + } // 生成教师账号 - _, err = SysLoginpersonService.AddLoginperson(2, model.PersonId, model.Xm) + _, err = SysLoginpersonService.AddLoginperson(session, 2, model.PersonId, model.Xm) if err != nil { + //事务回滚 + session.Rollback() reply.Success = false reply.Message = Const.DataBaseActionError LogUtil.Error(ErrorConst.SqlUpdateError, "执行AddLoginperson时发生严重错误:"+err.Error()) return &reply, err } + //事务提交 + session.Commit() + //记录日志 BaseTeacherDao.ActionLog([]models.TBaseTeacher{*model}, Const.ActionInsert, in.ActionPersonId, in.ActionIpAddress) diff --git a/dsBaseRpc/RpcService/SysLoginperson/SysLoginpersonDao/SysLoginpersonDao.go b/dsBaseRpc/RpcService/SysLoginperson/SysLoginpersonDao/SysLoginpersonDao.go index ac6aa93a..ebec4fca 100644 --- a/dsBaseRpc/RpcService/SysLoginperson/SysLoginpersonDao/SysLoginpersonDao.go +++ b/dsBaseRpc/RpcService/SysLoginperson/SysLoginpersonDao/SysLoginpersonDao.go @@ -13,13 +13,14 @@ import ( "errors" "fmt" "github.com/xormplus/builder" + "github.com/xormplus/xorm" ) var db = DbUtil.Engine //增加 -func AddSysLoginPerson(model models.TSysLoginperson) (int64, error) { - return db.Insert(model) +func AddSysLoginPerson(session *xorm.Session, model models.TSysLoginperson) (int64, error) { + return session.Insert(model) } //修改 diff --git a/dsBaseRpc/RpcService/SysLoginperson/SysLoginpersonService/SysLoginpersonBusinessService.go b/dsBaseRpc/RpcService/SysLoginperson/SysLoginpersonService/SysLoginpersonBusinessService.go index 5a18fa9d..50678957 100644 --- a/dsBaseRpc/RpcService/SysLoginperson/SysLoginpersonService/SysLoginpersonBusinessService.go +++ b/dsBaseRpc/RpcService/SysLoginperson/SysLoginpersonService/SysLoginpersonBusinessService.go @@ -5,6 +5,7 @@ import ( "dsBaseRpc/Utils/CommonUtil" "dsBaseRpc/Utils/LdapUtil" "dsBaseRpc/models" + "github.com/xormplus/xorm" "time" ) @@ -54,7 +55,7 @@ func GenerateLoginAccount(identityId int32, count int64) []LoginAccount { } //增加登录人员 -func AddLoginperson(identityId int32, personId string, personName string) (int64, error) { +func AddLoginperson(session *xorm.Session, identityId int32, personId string, personName string) (int64, error) { //生成登录名和密码 accountArray := GenerateLoginAccount(identityId, 1) //调用dao @@ -69,11 +70,10 @@ func AddLoginperson(identityId int32, personId string, personName string) (int64 model.PersonId = personId model.PersonName = personName model.BUse = 1 - count, err := SysLoginpersonDao.AddSysLoginPerson(*model) + count, err := SysLoginpersonDao.AddSysLoginPerson(session, *model) return count, err } - /** 功能:传入一个列表,判断密码是不是修改过了,扩展change_pwd列,通过Golang的指针进行修改 作者:黄海