/// <summary> /// 新建职位 /// </summary> /// <param name="position">职位实体对象</param> /// <param name="tran">事务对象</param> /// <returns>返回处理后的职位实体对象</returns> public Position Add(Position position, ICTransaction tran) { IList<Position> positionList = new List<Position>(); positionList.Add(position); positionList = this.Add(positionList, tran); return positionList == null ? null : positionList[0]; }
/// <summary> /// 更新职位数据 /// </summary> /// <param name="position">职位实体对象</param> /// <param name="tran">中间事务对象</param> public void Update(Position position, ICTransaction tran) { IList<Position> positionList = new List<Position>(); positionList.Add(position); this.Update(positionList, tran); }
/// <summary> /// 更新职位数据 /// </summary> /// <param name="position">职位实体对象</param> public void Update(Position position) { this.Update(position, null); }
/// <summary> /// 新建职位 /// </summary> /// <param name="position">职位实体对象</param> /// <returns>返回处理后的职位实体对象</returns> public Position Add(Position position) { return this.Add(position, null); }
public void TransactionRollBackTest() { ICTransaction tran = DalManager.DalFactory.BeginTransaction(); Employee existEmployee = OrgHandler.FindEmployeeById(QueryTest.EmployeeIdList[2]); Position newPosition = new Position(); newPosition.CurrCompany = existEmployee.CurrCompany; newPosition.CurrDepartment = existEmployee.CurrDepartment; newPosition.PositionCode = "3" + existEmployee.CurrCompany.CompanyCode + string.Format("{0:0000}", "13"); newPosition.CreaterId = "00000000000"; newPosition.UpdatorId = "00000000000"; newPosition.RVersion = 1; newPosition.Status = 1; OrgHandler.AddPosition(newPosition, tran); Employee newEmployee = new Employee(); newEmployee.Name = "Emp" + Guid.NewGuid().ToString().ToLower().Replace("-", "").Substring(0, 5); newEmployee.EmployeeCode = "5" + existEmployee.CurrCompany.CompanyCode + string.Format("{0:000000}", "159"); newEmployee.CurrCompany = existEmployee.CurrCompany; newEmployee.CurrDepartment = existEmployee.CurrDepartment; newEmployee.CurrPosition = newPosition; newEmployee.CreaterId = "00000000000"; newEmployee.UpdatorId = "00000000000"; newEmployee.RVersion = 1; newEmployee.Status = 1; newEmployee.Birthday = Convert.ToDateTime("1981-11-27"); newEmployee.Sex = 1; newEmployee.StartWorkDate = Convert.ToDateTime("2005-4-1"); newEmployee.JoinDate = Convert.ToDateTime("2005-4-1"); newEmployee.Rand = 10; OrgHandler.AddEmployee(newEmployee, tran); newEmployee = new Employee(); newEmployee.Name = "Emp" + Guid.NewGuid().ToString().ToLower().Replace("-", "").Substring(0, 5); newEmployee.EmployeeCode = "5" + existEmployee.CurrCompany.CompanyCode + string.Format("{0:000000}", "160"); newEmployee.CurrCompany = existEmployee.CurrCompany; newEmployee.CurrDepartment = existEmployee.CurrDepartment; newEmployee.CurrPosition = newPosition; newEmployee.CreaterId = "00000000000"; newEmployee.UpdatorId = "00000000000"; newEmployee.RVersion = 1; newEmployee.Status = 1; newEmployee.Birthday = Convert.ToDateTime("1981-12-02"); newEmployee.Sex = 1; newEmployee.StartWorkDate = Convert.ToDateTime("2005-4-3"); newEmployee.JoinDate = Convert.ToDateTime("2005-4-3"); newEmployee.Rand = 11; OrgHandler.AddEmployee(newEmployee, tran); ((HibernateTransaction)tran).CurrentTransaction.Rollback(); long allCountPosEx = 56; long allCountPos = OrgHandler.CountPosition(new PositionSearcher()); Assert.AreEqual(allCountPosEx, allCountPos); long allCountEmpEx = 782; long allCountEmp = OrgHandler.CountEmployee(new EmployeeSearcher()); Assert.AreEqual(allCountEmpEx, allCountEmp); }
public void AddDataTest() { #region Clear All Data OrgHandler.DeleteEmployee(new EmployeeSearcher()); OrgHandler.DeletePosition(new PositionSearcher()); OrgHandler.DeleteDepartment(new DepartmentSearcher()); OrgHandler.DeleteCompany(new CompanySearcher()); #endregion string companyName = " Com. Ltd."; Company company = null; Department department = null; Position position = null; Employee employee = null; int depCode = 1; int posCode = 1; int empCode = 1; int empCount = 30; Random rand = new Random(DateTime.Now.Hour); string[][] companyNamePool = new string[][] { new string[]{"ABC","10201"}, new string[]{"DEF","10301"}, new string[]{"GHI","10202"}, new string[]{"JKL","10203"}, new string[]{"MNO","10401"} }; string[] departmentNamePool = new string[]{ "GM", "Finance", "HR", "Sales", "Marketing", "Executive" }; string[] positionNamePool = new string[] { "GM", "Specialist" }; long allEmpCount = 780; foreach (string[] cName in companyNamePool) { company = new Company(); company.Name = cName[0] + companyName; company.CompanyCode = cName[1]; company.CreaterId = "00000000000"; company.UpdatorId = "00000000000"; company.RVersion = 1; company.Status = 1; OrgHandler.AddCompany(company); depCode = 1; empCode = 1; posCode = 1; foreach (string dName in departmentNamePool) { department = new Department(); department.CurrCompany = company; department.Name = dName; department.DepartmentCode = "2" + company.CompanyCode + string.Format("{0:0000}", depCode); department.CreaterId = "00000000000"; department.UpdatorId = "00000000000"; department.RVersion = 1; department.Status = 1; OrgHandler.AddDepartment(department); foreach (string pName in positionNamePool) { position = new Position(); position.CurrCompany = company; position.CurrDepartment = department; position.PositionCode = "3" + company.CompanyCode + string.Format("{0:0000}", posCode); position.CreaterId = "00000000000"; position.UpdatorId = "00000000000"; position.RVersion = 1; position.Status = 1; if (pName.Equals("GM")) { if (dName.Equals("GM")) { position.Name = pName; } else { position.Name = dName + " " + pName; } empCount = 1; } else { position.Name = dName + " " + pName; empCount = 30; } OrgHandler.AddPosition(position); for (int i = 1; i <= empCount; i++) { employee = new Employee(); employee.Name = "Emp" + Guid.NewGuid().ToString().ToLower().Replace("-", "").Substring(0, 5); employee.EmployeeCode = "5" + company.CompanyCode + string.Format("{0:000000}", empCode); employee.CurrCompany = company; employee.CurrDepartment = department; employee.CurrPosition = position; employee.CreaterId = "00000000000"; employee.UpdatorId = "00000000000"; employee.RVersion = 1; employee.Status = 1; employee.Birthday = Convert.ToDateTime("19" + rand.Next(70,90).ToString() + "-" + rand.Next(1, 9).ToString() + "-" + rand.Next(1, 28).ToString()); employee.Sex = rand.Next(1, 2); employee.StartWorkDate = Convert.ToDateTime(employee.Birthday).AddYears(20).AddMonths(rand.Next(1, 12)); employee.JoinDate = rand.Next(1, 10) > 6 ? employee.StartWorkDate : Convert.ToDateTime(employee.StartWorkDate).AddYears(rand.Next(1, 2)).AddMonths(rand.Next(1, 12)); if (dName.Equals("GM")) { employee.Rand = 20; } else if (pName.Equals("GM")) { employee.Rand = rand.Next(15, 19); } else { employee.Rand = rand.Next(5, 14); } OrgHandler.AddEmployee(employee); if(QueryTest.EmployeeIdList.Count < 10 && !dName.Equals("GM") && !pName.Equals("GM")) { QueryTest.EmployeeIdList.Add(employee.EmployeeId); QueryTest.EmployeeCodeList.Add(employee.EmployeeCode); } empCode++; } posCode++; if (dName.Equals("GM")) { break; } } depCode++; } } EmployeeSearcher empSearcher = new EmployeeSearcher(); long newDataCount = OrgHandler.CountEmployee(empSearcher); Assert.AreEqual(allEmpCount, newDataCount); }
/// <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; MySqlParameter[] paramCollection = new MySqlParameter[9]; paramCollection[0] = new MySqlParameter("@PositionId", MySqlDbType.String, 40); paramCollection[1] = new MySqlParameter("@PositionCode", MySqlDbType.String, 13); paramCollection[2] = new MySqlParameter("@CompanyId", MySqlDbType.String, 40); paramCollection[3] = new MySqlParameter("@DepartmentId", MySqlDbType.String, 40); paramCollection[4] = new MySqlParameter("@Name", MySqlDbType.String, 100); paramCollection[5] = new MySqlParameter("@RVersion", MySqlDbType.Int32); paramCollection[6] = new MySqlParameter("@Status", MySqlDbType.Int32); paramCollection[7] = new MySqlParameter("@UpdatorId", MySqlDbType.String, 40); paramCollection[8] = new MySqlParameter("@UpdateTime", MySqlDbType.DateTime); paramCollection[0].Value = position.PositionId; paramCollection[1].Value = position.PositionCode; paramCollection[2].Value = position.CompanyId; paramCollection[3].Value = position.DepartmentId; paramCollection[4].Value = position.Name; paramCollection[5].Value = position.RVersion; paramCollection[6].Value = position.Status; paramCollection[7].Value = position.UpdatorId; paramCollection[8].Value = position.UpdateTime; try { int effectCount = 0; if (position != null) { if (tran != null) { effectCount = MySqlHelper.ExecuteNonQuery((MySqlConnection)tran.Connection, query.ToString(), paramCollection); } else { effectCount = MySqlHelper.ExecuteNonQuery(this.CurrentConnectionString, 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="positionSearcher">职位查询对象</param> /// <param name="pager">分页对象</param> /// <param name="tran">中间事务对象</param> /// <returns>返回查找到的分页集合,包括按条件可查询到的所有记录数和当前分页的对象集合</returns> public PageList<Position> FindList(PositionSearcher positionSearcher, Pager pager, ICTransaction tran) { PageList<Position> resultList = new PageList<Position>(); PageDataTable pageDataTable = this.FindDataTable(positionSearcher, pager, tran); Position ele = null; if (pageDataTable != null) { resultList = new PageList<Position>(); resultList.PageIndex = pageDataTable.PageIndex; resultList.TotalCount = pageDataTable.TotalCount; if (pageDataTable.RecordList != null && pageDataTable.RecordList.Rows.Count > 0) { foreach (DataRow aRow in pageDataTable.RecordList.Rows) { ele = new Position(); if (!(aRow["PositionId"] is DBNull)) { ele.PositionId = aRow["PositionId"].ToString(); } if (!(aRow["CompanyId"] is DBNull)) { ele.CompanyId = aRow["CompanyId"].ToString(); } if (!(aRow["DepartmentId"] is DBNull)) { ele.DepartmentId = aRow["DepartmentId"].ToString(); } if (!(aRow["PositionCode"] is DBNull)) { ele.PositionCode = aRow["PositionCode"].ToString(); } if (!(aRow["CreaterId"] is DBNull)) { ele.CreaterId = aRow["CreaterId"].ToString(); } if (!(aRow["CreateTime"] is DBNull)) { ele.CreateTime = Convert.ToDateTime(aRow["CreateTime"]); } if (!(aRow["Name"] is DBNull)) { ele.Name = aRow["Name"].ToString(); } if (!(aRow["RVersion"] is DBNull)) { ele.RVersion = Convert.ToInt32(aRow["RVersion"]); } if (!(aRow["Status"] is DBNull)) { ele.Status = Convert.ToInt32(aRow["Status"]); } if (!(aRow["UpdateTime"] is DBNull)) { ele.UpdateTime = Convert.ToDateTime(aRow["UpdateTime"]); } if (!(aRow["UpdatorId"] is DBNull)) { ele.UpdatorId = aRow["UpdatorId"].ToString(); } resultList.RecordList.Add(ele); } } } return resultList; }
/// <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(@"); "); MySqlParameter[] paramCollection = new MySqlParameter[11]; paramCollection[0] = new MySqlParameter("@PositionId", MySqlDbType.String, 40); paramCollection[1] = new MySqlParameter("@PositionCode", MySqlDbType.String, 13); paramCollection[2] = new MySqlParameter("@CompanyId", MySqlDbType.String, 40); paramCollection[3] = new MySqlParameter("@DepartmentId", MySqlDbType.String, 40); paramCollection[4] = new MySqlParameter("@Name", MySqlDbType.String, 100); paramCollection[5] = new MySqlParameter("@RVersion", MySqlDbType.Int32); paramCollection[6] = new MySqlParameter("@Status", MySqlDbType.Int32); paramCollection[7] = new MySqlParameter("@CreaterId", MySqlDbType.String, 40); paramCollection[8] = new MySqlParameter("@CreateTime", MySqlDbType.DateTime); paramCollection[9] = new MySqlParameter("@UpdatorId", MySqlDbType.String, 40); paramCollection[10] = new MySqlParameter("@UpdateTime", MySqlDbType.DateTime); paramCollection[0].Value = position.PositionId; paramCollection[1].Value = position.PositionCode; paramCollection[2].Value = position.CompanyId; paramCollection[3].Value = position.DepartmentId; paramCollection[4].Value = position.Name; paramCollection[5].Value = position.RVersion; paramCollection[6].Value = position.Status; paramCollection[7].Value = position.CreaterId; paramCollection[8].Value = position.CreateTime; paramCollection[9].Value = position.UpdatorId; paramCollection[10].Value = position.UpdateTime; try { int effectCount = 0; if (tran != null) { effectCount = MySqlHelper.ExecuteNonQuery((MySqlConnection)tran.Connection, query.ToString(), paramCollection); } else { effectCount = MySqlHelper.ExecuteNonQuery(this.CurrentConnectionString, 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> /// 更新职位数据 /// </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; }