/// <summary> /// 更新员工数据 /// </summary> /// <param name="employee">员工实体对象</param> /// <param name="tran">中间事务对象</param> public void Update(Employee employee, ICTransaction tran) { Employee oldEmployee = this.FindSingle(employee.EmployeeId, tran); int updateColCount = 0; if (employee == null) { throw new ArgumentException("employee"); } if (oldEmployee == null) { throw new ResponseException((int)ResultCode.NoDataExists, employee.EmployeeCode); } if (employee.RVersion != oldEmployee.RVersion) { throw new ResponseException((int)ResultCode.VersionChanged, oldEmployee.RVersion.ToString()); } StringBuilder query = new StringBuilder(); query.AppendLine(@"UPDATE "); query.AppendLine(@" [Employee]"); query.AppendLine(@"SET "); query.AppendLine(@" [EmployeeId] = @EmployeeId "); if ((!string.IsNullOrEmpty(employee.EmployeeCode) && !employee.EmployeeCode.Equals(oldEmployee.EmployeeCode)) || (!string.IsNullOrEmpty(oldEmployee.EmployeeCode) && !oldEmployee.EmployeeCode.Equals(employee.EmployeeCode))) { updateColCount++; query.AppendLine(@" ,[EmployeeCode] = @EmployeeCode "); } if ((!string.IsNullOrEmpty(employee.Name) && !employee.Name.Equals(oldEmployee.Name)) || (!string.IsNullOrEmpty(oldEmployee.Name) && !oldEmployee.Name.Equals(employee.Name))) { updateColCount++; query.AppendLine(@" ,[Name] = @Name "); } if (((DateTime)oldEmployee.Birthday).CompareTo(employee.Birthday) != 0 && employee.Birthday != DateTime.MinValue) { updateColCount++; query.AppendLine(@" ,[Birthday] = @Birthday "); } if (employee.Sex != oldEmployee.Sex) { updateColCount++; query.AppendLine(@" ,[Sex] = @Sex "); } if ((!string.IsNullOrEmpty(employee.CompanyId) && !employee.CompanyId.Equals(oldEmployee.CompanyId)) || (!string.IsNullOrEmpty(oldEmployee.CompanyId) && !oldEmployee.CompanyId.Equals(employee.CompanyId))) { updateColCount++; query.AppendLine(@" ,[CompanyId] = @CompanyId "); } if ((!string.IsNullOrEmpty(employee.DepartmentId) && !employee.DepartmentId.Equals(oldEmployee.DepartmentId)) || (!string.IsNullOrEmpty(oldEmployee.DepartmentId) && !oldEmployee.DepartmentId.Equals(employee.DepartmentId))) { updateColCount++; query.AppendLine(@" ,[DepartmentId] = @DepartmentId "); } if ((!string.IsNullOrEmpty(employee.PositionId) && !employee.PositionId.Equals(oldEmployee.PositionId)) || (!string.IsNullOrEmpty(oldEmployee.PositionId) && !oldEmployee.PositionId.Equals(employee.PositionId))) { updateColCount++; query.AppendLine(@" ,[PositionId] = @PositionId "); } if (employee.Rand != oldEmployee.Rand) { updateColCount++; query.AppendLine(@" ,[Rand] = @Rand "); } if (employee.Status != oldEmployee.Status) { updateColCount++; query.AppendLine(@" ,[Status] = @Status "); } if ((!string.IsNullOrEmpty(employee.CreaterId) && !employee.CreaterId.Equals(oldEmployee.CreaterId)) || (!string.IsNullOrEmpty(oldEmployee.CreaterId) && !oldEmployee.CreaterId.Equals(employee.CreaterId))) { updateColCount++; query.AppendLine(@" ,[CreaterId] = @CreaterId "); } if (oldEmployee.CreateTime.CompareTo(employee.CreateTime) != 0 && employee.CreateTime != DateTime.MinValue) { updateColCount++; query.AppendLine(@" ,[CreateTime] = @CreateTime "); } if ((!string.IsNullOrEmpty(employee.UpdatorId) && !employee.UpdatorId.Equals(oldEmployee.UpdatorId)) || (!string.IsNullOrEmpty(oldEmployee.UpdatorId) && !oldEmployee.UpdatorId.Equals(employee.UpdatorId))) { updateColCount++; query.AppendLine(@" ,[UpdatorId] = @UpdatorId "); } if (oldEmployee.UpdateTime.CompareTo(employee.UpdateTime) != 0 && employee.UpdateTime != DateTime.MinValue) { updateColCount++; query.AppendLine(@" ,[UpdateTime] = @UpdateTime "); } if (((DateTime)oldEmployee.StartWorkDate).CompareTo(employee.StartWorkDate) != 0 && employee.StartWorkDate != DateTime.MinValue) { updateColCount++; query.AppendLine(@" ,[StartWorkDate] = @StartWorkDate "); } if (((DateTime)oldEmployee.JoinDate).CompareTo(employee.JoinDate) != 0 && employee.JoinDate != DateTime.MinValue) { updateColCount++; query.AppendLine(@" ,[JoinDate] = @JoinDate "); } query.AppendLine(@" ,[RVersion] = @RVersion "); query.AppendLine(@"WHERE "); query.AppendLine(@" [EmployeeId] = @EmployeeId "); if (updateColCount == 0) { return; } employee.UpdateTime = DateTime.Now; DBParamCollection <DBParam> paramCollection = new DBParamCollection <DBParam>(); paramCollection.Add(new DBParam("@EmployeeId", employee.EmployeeId, DbType.String, 40)); paramCollection.Add(new DBParam("@EmployeeCode", employee.EmployeeCode, DbType.String, 15)); paramCollection.Add(new DBParam("@Name", employee.Name, DbType.String, 50)); paramCollection.Add(new DBParam("@Birthday", employee.Birthday, DbType.DateTime)); paramCollection.Add(new DBParam("@Sex", employee.Sex, DbType.Int32)); paramCollection.Add(new DBParam("@CompanyId", employee.CompanyId, DbType.String, 40)); paramCollection.Add(new DBParam("@DepartmentId", employee.DepartmentId, DbType.String, 40)); paramCollection.Add(new DBParam("@PositionId", employee.PositionId, DbType.String, 40)); paramCollection.Add(new DBParam("@Rand", employee.Rand, DbType.Int32)); paramCollection.Add(new DBParam("@RVersion", employee.RVersion, DbType.Int32)); paramCollection.Add(new DBParam("@Status", employee.Status, DbType.Int32)); paramCollection.Add(new DBParam("@CreaterId", employee.CreaterId, DbType.String, 40)); paramCollection.Add(new DBParam("@CreateTime", employee.CreateTime, DbType.DateTime)); paramCollection.Add(new DBParam("@UpdatorId", employee.UpdatorId, DbType.String, 40)); paramCollection.Add(new DBParam("@UpdateTime", employee.UpdateTime, DbType.DateTime)); paramCollection.Add(new DBParam("@StartWorkDate", employee.StartWorkDate, DbType.DateTime)); paramCollection.Add(new DBParam("@JoinDate", employee.JoinDate, DbType.DateTime)); try { int effectCount = 0; if (employee != null) { if (tran != null) { DbTransaction dbTran = ((MssqlTransaction)tran).CurrentTransaction; effectCount = MssqlHelper.ExecuteNonQuery(dbTran, CommandType.Text, query.ToString(), paramCollection); } else { effectCount = MssqlHelper.ExecuteNonQuery(this.CurrentConnectionString, CommandType.Text, query.ToString(), paramCollection); } } // 抛出一个异常 if (effectCount == 0) { throw new ResponseException((int)ResultCode.NoDataUpdate, employee.EmployeeCode); } } catch (Exception ex) { throw new Exception(ex.Message, ex); } }
/// <summary> /// 新建员工 /// </summary> /// <param name="employee">员工实体对象</param> /// <param name="tran">中间事务对象</param> /// <returns>返回处理后的员工实体对象</returns> public Employee Add(Employee employee, ICTransaction tran) { if (employee == null) { throw new ArgumentNullException("employee"); } employee.EmployeeId = KeyGenerator.GenNewGuidKey(); StringBuilder query = new StringBuilder(); query.AppendLine(@"INSERT INTO "); query.AppendLine(@" [Employee] ( "); query.AppendLine(@" [EmployeeId] "); query.AppendLine(@" ,[EmployeeCode] "); query.AppendLine(@" ,[Name] "); query.AppendLine(@" ,[Birthday] "); query.AppendLine(@" ,[Sex] "); query.AppendLine(@" ,[CompanyId] "); query.AppendLine(@" ,[DepartmentId] "); query.AppendLine(@" ,[PositionId] "); query.AppendLine(@" ,[Rand] "); query.AppendLine(@" ,[RVersion] "); query.AppendLine(@" ,[Status] "); query.AppendLine(@" ,[CreaterId] "); query.AppendLine(@" ,[CreateTime] "); query.AppendLine(@" ,[UpdatorId] "); query.AppendLine(@" ,[UpdateTime] "); query.AppendLine(@" ,[StartWorkDate] "); query.AppendLine(@" ,[JoinDate] "); query.AppendLine(@" ) "); query.AppendLine(@"VALUES ("); query.AppendLine(@" @EmployeeId "); query.AppendLine(@" ,@EmployeeCode "); query.AppendLine(@" ,@Name "); query.AppendLine(@" ,@Birthday "); query.AppendLine(@" ,@Sex "); query.AppendLine(@" ,@CompanyId "); query.AppendLine(@" ,@DepartmentId "); query.AppendLine(@" ,@PositionId "); query.AppendLine(@" ,@Rand "); query.AppendLine(@" ,@RVersion "); query.AppendLine(@" ,@Status "); query.AppendLine(@" ,@CreaterId "); query.AppendLine(@" ,@CreateTime "); query.AppendLine(@" ,@UpdatorId "); query.AppendLine(@" ,@UpdateTime "); query.AppendLine(@" ,@StartWorkDate "); query.AppendLine(@" ,@JoinDate "); query.AppendLine(@"); "); DBParamCollection <DBParam> paramCollection = new DBParamCollection <DBParam>(); paramCollection.Add(new DBParam("@EmployeeId", employee.EmployeeId, DbType.String, 40)); paramCollection.Add(new DBParam("@EmployeeCode", employee.EmployeeCode, DbType.String, 15)); paramCollection.Add(new DBParam("@Name", employee.Name, DbType.String, 50)); paramCollection.Add(new DBParam("@Birthday", employee.Birthday, DbType.DateTime)); paramCollection.Add(new DBParam("@Sex", employee.Sex, DbType.Int32)); paramCollection.Add(new DBParam("@CompanyId", employee.CompanyId, DbType.String, 40)); paramCollection.Add(new DBParam("@DepartmentId", employee.DepartmentId, DbType.String, 40)); paramCollection.Add(new DBParam("@PositionId", employee.PositionId, DbType.String, 40)); paramCollection.Add(new DBParam("@Rand", employee.Rand, DbType.Int32)); paramCollection.Add(new DBParam("@RVersion", employee.RVersion, DbType.Int32)); paramCollection.Add(new DBParam("@Status", employee.Status, DbType.Int32)); paramCollection.Add(new DBParam("@CreaterId", employee.CreaterId, DbType.String, 40)); paramCollection.Add(new DBParam("@CreateTime", employee.CreateTime, DbType.DateTime)); paramCollection.Add(new DBParam("@UpdatorId", employee.UpdatorId, DbType.String, 40)); paramCollection.Add(new DBParam("@UpdateTime", employee.UpdateTime, DbType.DateTime)); paramCollection.Add(new DBParam("@StartWorkDate", employee.StartWorkDate, DbType.DateTime)); paramCollection.Add(new DBParam("@JoinDate", employee.JoinDate, DbType.DateTime)); try { int effectCount = 0; if (tran != null) { DbTransaction dbTran = ((MssqlTransaction)tran).CurrentTransaction; effectCount = MssqlHelper.ExecuteNonQuery(dbTran, CommandType.Text, query.ToString(), paramCollection); } else { effectCount = MssqlHelper.ExecuteNonQuery(this.CurrentConnectionString, CommandType.Text, query.ToString(), paramCollection); } if (effectCount == 0) { employee.EmployeeId = string.Empty; throw new ResponseException((int)ResultCode.NoDataInsert, employee.EmployeeCode); } } catch (Exception ex) { employee.EmployeeId = string.Empty; throw new Exception(ex.Message, ex); } return(employee); }
/// <summary> /// 根据指定条件查找员工分页集合 /// </summary> /// <param name="employeeSearcher">员工查询对象</param> /// <param name="pager">分页对象</param> /// <param name="tran">中间事务对象</param> /// <returns>返回查找到的分页集合,包括按条件可查询到的所有记录数和当前分页的DataTable数据</returns> public PageDataTable FindDataTable(EmployeeSearcher employeeSearcher, Pager pager, ICTransaction tran) { EmployeeSearcher querySearcher = null; MssqlQueryParser queryParser = new MssqlQueryParser(); PageDataTable pDataTable = new PageDataTable(); DataSet resultSet = null; StringBuilder query = new StringBuilder(); StringBuilder joinQuery = new StringBuilder(); StringBuilder conditionQuery = new StringBuilder(); StringBuilder sortQuery = new StringBuilder(); if (employeeSearcher != null) { querySearcher = (EmployeeSearcher)employeeSearcher.Clone(); querySearcher.TableName = "E"; if (querySearcher.CurrCompany != null) { querySearcher.CurrCompany.TableName = "C"; joinQuery.AppendLine(@"LEFT JOIN "); joinQuery.AppendLine(@" [Company] C ON(C.CompanyId = E.CompanyId) "); } if (querySearcher.CurrDepartment != null) { querySearcher.CurrDepartment.TableName = "D"; joinQuery.AppendLine(@"LEFT JOIN "); joinQuery.AppendLine(@" [Department] D ON(D.DepartmentId = E.DepartmentId) "); } if (querySearcher.CurrPosition != null) { querySearcher.CurrPosition.TableName = "P"; joinQuery.AppendLine(@"LEFT JOIN "); joinQuery.AppendLine(@" [Position] P ON(P.PositionId = E.PositionId) "); } } queryParser.SearcherParse(querySearcher); if (!string.IsNullOrEmpty(queryParser.ConditionString)) { conditionQuery.AppendLine(@"WHERE "); conditionQuery.AppendLine(@" " + queryParser.ConditionString); } if (!string.IsNullOrEmpty(queryParser.SortString)) { sortQuery.AppendLine(@"ORDER BY "); sortQuery.AppendLine(@" " + queryParser.SortString); } query.AppendLine(@"SELECT "); query.AppendLine(@" E.[EmployeeId] "); query.AppendLine(@" ,E.[EmployeeCode] "); query.AppendLine(@" ,E.[Name] "); query.AppendLine(@" ,E.[Birthday] "); query.AppendLine(@" ,E.[Sex] "); query.AppendLine(@" ,E.[CompanyId] "); query.AppendLine(@" ,E.[DepartmentId] "); query.AppendLine(@" ,E.[PositionId] "); query.AppendLine(@" ,E.[Rand] "); query.AppendLine(@" ,E.[RVersion] "); query.AppendLine(@" ,E.[Status] "); query.AppendLine(@" ,E.[CreaterId] "); query.AppendLine(@" ,E.[CreateTime] "); query.AppendLine(@" ,E.[UpdatorId] "); query.AppendLine(@" ,E.[UpdateTime] "); query.AppendLine(@" ,E.[StartWorkDate] "); query.AppendLine(@" ,E.[JoinDate] "); query.AppendLine(@"FROM "); query.AppendLine(@" [Employee] E "); query.AppendLine(joinQuery.ToString()); query.AppendLine(conditionQuery.ToString()); query.AppendLine(sortQuery.ToString()); query.AppendLine(@"; "); if (tran != null) { DbTransaction dbTran = ((MssqlTransaction)tran).CurrentTransaction; if (pager != null) { resultSet = MssqlHelper.ExecuteDataSet(dbTran, CommandType.Text, query.ToString(), pager, "Employee", queryParser.ParamCollection); } else { resultSet = MssqlHelper.ExecuteDataSet(dbTran, CommandType.Text, query.ToString(), queryParser.ParamCollection); } } else { if (pager != null) { resultSet = MssqlHelper.ExecuteDataSet(this.CurrentConnectionString, CommandType.Text, query.ToString(), pager, "Employee", queryParser.ParamCollection); } else { resultSet = MssqlHelper.ExecuteDataSet(this.CurrentConnectionString, CommandType.Text, query.ToString(), queryParser.ParamCollection); } } if (resultSet != null) { if (pager != null) { pDataTable.PageIndex = pager.CurrentPage; } pDataTable.TotalCount = this.Count(employeeSearcher, tran); pDataTable.RecordList = resultSet.Tables[0]; } return(pDataTable); }
/// <summary> /// 根据指定条件删除员工 /// </summary> /// <param name="employeeSearcher">员工查询对象</param> /// <param name="tran">中间事务对象</param> public void Delete(EmployeeSearcher employeeSearcher, ICTransaction tran) { int effectCount = 0; EmployeeSearcher querySearcher = null; MssqlQueryParser queryParser = new MssqlQueryParser(); StringBuilder query = new StringBuilder(); query.AppendLine(@"DELETE FROM "); query.AppendLine(@" Employee "); query.AppendLine(@"WHERE "); query.AppendLine(@" EmployeeId IN ("); query.AppendLine(@" SELECT "); query.AppendLine(@" E.EmployeeId "); query.AppendLine(@" FROM "); query.AppendLine(@" Employee E "); if (employeeSearcher != null) { querySearcher = (EmployeeSearcher)employeeSearcher.Clone(); querySearcher.TableName = "E"; if (querySearcher.CurrCompany != null) { querySearcher.CurrCompany.TableName = "C"; query.AppendLine(@"LEFT JOIN "); query.AppendLine(@" [Company] C ON(C.CompanyId = E.CompanyId) "); } if (querySearcher.CurrDepartment != null) { querySearcher.CurrDepartment.TableName = "D"; query.AppendLine(@"LEFT JOIN "); query.AppendLine(@" [Department] D ON(D.DepartmentId = E.DepartmentId) "); } if (querySearcher.CurrPosition != null) { querySearcher.CurrPosition.TableName = "P"; query.AppendLine(@"LEFT JOIN "); query.AppendLine(@" [Position] P ON(P.PositionId = E.PositionId) "); } } queryParser.SearcherParse(querySearcher); if (!string.IsNullOrEmpty(queryParser.ConditionString)) { query.AppendLine(@"WHERE "); query.AppendLine(@" " + queryParser.ConditionString); } query.AppendLine(@"); "); try { if (tran != null) { DbTransaction dbTran = ((MssqlTransaction)tran).CurrentTransaction; effectCount = MssqlHelper.ExecuteNonQuery(dbTran, CommandType.Text, query.ToString(), queryParser.ParamCollection); } else { effectCount = MssqlHelper.ExecuteNonQuery(this.CurrentConnectionString, CommandType.Text, query.ToString(), queryParser.ParamCollection); } } catch (SqlException sex) { if (sex.ErrorCode == (int)ResultCode.FKError) { throw new ResponseException((int)ResultCode.FKError, "DELETE Position"); } } }
public async Task <DataResult <int> > HealthStaffImportAsync(QueryData <HealthStaffImportQuery> query) { var result = new DataResult <int>(); string sqlsi = @"insert into [dbo].[health_staff]([StaffNo],[StaffName],[GroupType],[GroupLeader],[GroupLeaderNo],[AggLeader],[AggLeaderNo],[CommandLeader],[CommondLeaderNo],[HrLeader],[HrLeaderNo]) values(@StaffNo,@StaffName,@GroupType,@GroupLeader,@GroupLeaderNo,@AggLeader,@AggLeaderNo,@CommandLeader,@CommondLeaderNo,@HrLeader,@HrLeaderNo)"; string sqlui = @"insert into dbo.health_user_staff([UserNo],[StaffNo],[Creator],[CreateName],[CreateTime]) values(@UserNo,@StaffNo,@Creator,@CreateName,getdate())"; string sqlss = @"select * from [dbo].[health_staff] where [StaffNo]=@StaffNo"; using (IDbConnection dbConn = MssqlHelper.OpenMsSqlConnection(MssqlHelper.GetConn)) { IDbTransaction transaction = dbConn.BeginTransaction(); try { foreach (var item in query.Criteria.LstStaff)//保存员工信息 { result.Data = await MssqlHelper.QueryCountAsync(dbConn, sqlss, new { StaffNo = item.StaffNo }, transaction); if (result.Data > 0) { transaction.Rollback(); result.SetErr(string.Format("员工工号{0}已存在,请重新检查!", item.StaffNo), -101); return(result); } result.Data = await MssqlHelper.ExecuteSqlAsync(dbConn, sqlsi, item, transaction); if (result.Data <= 0) { transaction.Rollback(); result.SetErr(string.Format("员工工号{0}保存失败!", item.StaffNo), -101); return(result); } } #region 添加组长权限 foreach (var item in query.Criteria.LstGroupStaff) { result.Data = await MssqlHelper.ExecuteSqlAsync(dbConn, sqlui, item, transaction); if (result.Data <= 0) { transaction.Rollback(); result.SetErr(string.Format("组长权限{0}|{1}保存失败!", item.UserNo, item.StaffNo), -101); return(result); } } #endregion #region 添加集合组组长权限 foreach (var item in query.Criteria.LstAggStaff) { result.Data = await MssqlHelper.ExecuteSqlAsync(dbConn, sqlui, item, transaction); if (result.Data <= 0) { transaction.Rollback(); result.SetErr(string.Format("集合组长权限{0}|{1}保存失败!", item.UserNo, item.StaffNo), -101); return(result); } } #endregion #region 添加HR负责人权限 foreach (var item in query.Criteria.LstHrStaff) { result.Data = await MssqlHelper.ExecuteSqlAsync(dbConn, sqlui, item, transaction); if (result.Data <= 0) { transaction.Rollback(); result.SetErr(string.Format("HR负责人权限{0}|{1}保存失败!", item.UserNo, item.StaffNo), -101); return(result); } } #endregion transaction.Commit(); } catch (Exception ex) { result.SetErr(ex, -500); result.Data = -1; } } return(result); }
/// <summary> /// 更新职位数据 /// </summary> /// <param name="position">职位实体对象</param> /// <param name="tran">中间事务对象</param> public void Update(Position position, ICTransaction tran) { Position oldPosition = this.FindSingle(position.PositionId, tran); int updateColCount = 0; if (position == null) { throw new ArgumentException("position"); } if (oldPosition == null) { throw new ResponseException((int)ResultCode.NoDataExists, position.PositionCode); } if (position.RVersion != oldPosition.RVersion) { throw new ResponseException((int)ResultCode.VersionChanged, oldPosition.RVersion.ToString()); } StringBuilder query = new StringBuilder(); query.AppendLine(@"UPDATE "); query.AppendLine(@" [Position] "); query.AppendLine(@"SET "); query.AppendLine(@" [PositionId] = @PositionId "); if ((!string.IsNullOrEmpty(position.PositionCode) && !position.PositionCode.Equals(oldPosition.PositionCode)) || (!string.IsNullOrEmpty(oldPosition.PositionCode) && !oldPosition.PositionCode.Equals(position.PositionCode))) { updateColCount++; query.AppendLine(@" ,[PositionCode] = @PositionCode "); } if ((!string.IsNullOrEmpty(position.CompanyId) && !position.CompanyId.Equals(oldPosition.CompanyId)) || (!string.IsNullOrEmpty(oldPosition.CompanyId) && !oldPosition.CompanyId.Equals(position.CompanyId))) { updateColCount++; query.AppendLine(@" ,[CompanyId] = @CompanyId "); } if ((!string.IsNullOrEmpty(position.DepartmentId) && !position.DepartmentId.Equals(oldPosition.DepartmentId)) || (!string.IsNullOrEmpty(oldPosition.DepartmentId) && !oldPosition.DepartmentId.Equals(position.DepartmentId))) { updateColCount++; query.AppendLine(@" ,[DepartmentId] = @DepartmentId "); } if ((!string.IsNullOrEmpty(position.Name) && !position.Name.Equals(oldPosition.Name)) || (!string.IsNullOrEmpty(oldPosition.Name) && !oldPosition.Name.Equals(position.Name))) { updateColCount++; query.AppendLine(@" ,[Name] = @Name "); } if (position.Status != oldPosition.Status) { updateColCount++; query.AppendLine(@" ,[Status] = @Status "); } if ((!string.IsNullOrEmpty(position.PositionCode) && !position.PositionCode.Equals(oldPosition.PositionCode)) || (!string.IsNullOrEmpty(oldPosition.PositionCode) && !oldPosition.PositionCode.Equals(position.PositionCode))) { updateColCount++; query.AppendLine(@" ,[CreaterId] = @CreaterId "); } if (oldPosition.CreateTime.CompareTo(position.CreateTime) != 0 && position.CreateTime != DateTime.MinValue) { updateColCount++; query.AppendLine(@" ,[CreateTime] = @CreateTime "); } if ((!string.IsNullOrEmpty(position.PositionCode) && !position.PositionCode.Equals(oldPosition.PositionCode)) || (!string.IsNullOrEmpty(oldPosition.PositionCode) && !oldPosition.PositionCode.Equals(position.PositionCode))) { updateColCount++; query.AppendLine(@" ,[UpdatorId] = @UpdatorId "); } if (oldPosition.UpdateTime.CompareTo(position.UpdateTime) != 0 && position.UpdateTime != DateTime.MinValue) { updateColCount++; query.AppendLine(@" ,[UpdateTime] = @UpdateTime "); } query.AppendLine(@" ,[RVersion] = @RVersion "); query.AppendLine(@"WHERE "); query.AppendLine(@" [PositionId] = @PositionId "); if (updateColCount == 0) { return; } position.UpdateTime = DateTime.Now; DBParamCollection <DBParam> paramCollection = new DBParamCollection <DBParam>(); paramCollection.Add(new DBParam("@PositionId", position.PositionId, DbType.String, 40)); paramCollection.Add(new DBParam("@PositionCode", position.PositionCode, DbType.String, 13)); paramCollection.Add(new DBParam("@CompanyId", position.CompanyId, DbType.String, 40)); paramCollection.Add(new DBParam("@DepartmentId", position.DepartmentId, DbType.String, 40)); paramCollection.Add(new DBParam("@Name", position.Name, DbType.String, 100)); paramCollection.Add(new DBParam("@RVersion", position.RVersion, DbType.Int32)); paramCollection.Add(new DBParam("@Status", position.Status, DbType.Int32)); paramCollection.Add(new DBParam("@CreaterId", position.CreaterId, DbType.String, 40)); paramCollection.Add(new DBParam("@CreateTime", position.CreateTime, DbType.DateTime)); paramCollection.Add(new DBParam("@UpdatorId", position.UpdatorId, DbType.String, 40)); paramCollection.Add(new DBParam("@UpdateTime", position.UpdateTime, DbType.DateTime)); try { int effectCount = 0; if (position != null) { if (tran != null) { DbTransaction dbTran = ((MssqlTransaction)tran).CurrentTransaction; effectCount = MssqlHelper.ExecuteNonQuery(dbTran, CommandType.Text, query.ToString(), paramCollection); } else { effectCount = MssqlHelper.ExecuteNonQuery(this.CurrentConnectionString, CommandType.Text, query.ToString(), paramCollection); } } // 抛出一个异常 if (effectCount == 0) { throw new ResponseException((int)ResultCode.NoDataUpdate, position.PositionCode); } } catch (Exception ex) { throw new Exception(ex.Message, ex); } }
/// <summary> /// 新建职位 /// </summary> /// <param name="position">职位实体对象</param> /// <param name="tran">中间事务对象</param> /// <returns>返回处理后的职位实体对象</returns> public Position Add(Position position, ICTransaction tran) { if (position == null) { throw new ArgumentNullException("position"); } position.PositionId = KeyGenerator.GenNewGuidKey(); StringBuilder query = new StringBuilder(); query.AppendLine(@"INSERT INTO "); query.AppendLine(@" [Position] ( "); query.AppendLine(@" [PositionId] "); query.AppendLine(@" ,[PositionCode] "); query.AppendLine(@" ,[CompanyId] "); query.AppendLine(@" ,[DepartmentId] "); query.AppendLine(@" ,[Name] "); query.AppendLine(@" ,[RVersion] "); query.AppendLine(@" ,[Status] "); query.AppendLine(@" ,[CreaterId] "); query.AppendLine(@" ,[CreateTime] "); query.AppendLine(@" ,[UpdatorId] "); query.AppendLine(@" ,[UpdateTime] "); query.AppendLine(@" ) "); query.AppendLine(@"VALUES ( "); query.AppendLine(@" @PositionId "); query.AppendLine(@" ,@PositionCode "); query.AppendLine(@" ,@CompanyId "); query.AppendLine(@" ,@DepartmentId "); query.AppendLine(@" ,@Name "); query.AppendLine(@" ,@RVersion "); query.AppendLine(@" ,@Status "); query.AppendLine(@" ,@CreaterId "); query.AppendLine(@" ,@CreateTime "); query.AppendLine(@" ,@UpdatorId "); query.AppendLine(@" ,@UpdateTime "); query.AppendLine(@"); "); DBParamCollection <DBParam> paramCollection = new DBParamCollection <DBParam>(); paramCollection.Add(new DBParam("@PositionId", position.PositionId, DbType.String, 40)); paramCollection.Add(new DBParam("@PositionCode", position.PositionCode, DbType.String, 13)); paramCollection.Add(new DBParam("@CompanyId", position.CompanyId, DbType.String, 40)); paramCollection.Add(new DBParam("@DepartmentId", position.DepartmentId, DbType.String, 40)); paramCollection.Add(new DBParam("@Name", position.Name, DbType.String, 100)); paramCollection.Add(new DBParam("@RVersion", position.RVersion, DbType.Int32)); paramCollection.Add(new DBParam("@Status", position.Status, DbType.Int32)); paramCollection.Add(new DBParam("@CreaterId", position.CreaterId, DbType.String, 40)); paramCollection.Add(new DBParam("@CreateTime", position.CreateTime, DbType.DateTime)); paramCollection.Add(new DBParam("@UpdatorId", position.UpdatorId, DbType.String, 40)); paramCollection.Add(new DBParam("@UpdateTime", position.UpdateTime, DbType.DateTime)); try { int effectCount = 0; if (tran != null) { DbTransaction dbTran = ((MssqlTransaction)tran).CurrentTransaction; effectCount = MssqlHelper.ExecuteNonQuery(dbTran, CommandType.Text, query.ToString(), paramCollection); } else { effectCount = MssqlHelper.ExecuteNonQuery(this.CurrentConnectionString, CommandType.Text, query.ToString(), paramCollection); } if (effectCount == 0) { position.PositionId = string.Empty; throw new ResponseException((int)ResultCode.NoDataInsert, position.PositionCode); } } catch (Exception ex) { position.PositionId = string.Empty; throw new Exception(ex.Message, ex); } return(position); }
/// <summary> /// 得到最大ID /// </summary> public int GetMaxId() { return(MssqlHelper.GetMaxID("Id", "Auth_RoleMenu")); }