public InsertOrUpdateBranchResponse InsertOrUpdateBranch(InsertOrUpdateBranchRequest request, int channelId, int?upperBranchId) { _context.Configuration.AutoDetectChangesEnabled = false; var response = new InsertOrUpdateBranchResponse(); Logger.Info(_logMsg.Clear().SetPrefixMsg("Insert or Update Branch").Add("BranchCode", request.BranchCode) .Add("BranchName", request.BranchName).Add("ChannelId", channelId).Add("UpperBranchId", upperBranchId).ToInputLogString()); try { #region "Comment out" // var result = new InsertOrUpdateBranchResponse(); //// var dbBranch = _context.TB_R_BRANCH.Where(x => x.BRANCH_CODE.Trim().ToUpper() == request.BranchCode.Trim().ToUpper()).FirstOrDefault(); // TB_R_BRANCH dbBranch; //// if (dbBranch == null) // if (!request.Command) // { // //add mode // dbBranch = new TB_R_BRANCH(); // result.IsNewBranch = true; // //check duplicate code // var count = _context.TB_R_BRANCH.Count(p => p.BRANCH_CODE == request.BranchCode); // if (count > 0) // { // result.IsSuccess = false; // result.ErrorCode = "6"; // result.ErrorMessage = string.Format("CSM : รหัสสาขา {0} มีในระบบแล้ว", request.BranchCode); // return result; // } // //check duplicate name // count = _context.TB_R_BRANCH.Count(p => p.BRANCH_NAME == request.BranchName); // if (count > 0) // { // result.IsSuccess = false; // result.ErrorCode = "6"; // result.ErrorMessage = string.Format("CSM : {0} มีในระบบ", request.BranchName); // return result; // } // } // else // { // //edit mode // result.IsNewBranch = false; // dbBranch = _context.TB_R_BRANCH.FirstOrDefault(x => x.BRANCH_CODE.Trim().ToUpper() == request.BranchCode.Trim().ToUpper()); // #region == Validate Branch Data == // if (dbBranch == null) // { // //create new branch at slm // dbBranch = new TB_R_BRANCH(); // result.IsNewBranch = true; // } // else // { // // IF (Change Branch) Or (Change Status 1 to 0) // if (dbBranch.STATUS == 1 && request.Status == 0) // { // var count = _context.TB_M_POOL_BRANCH.Count(x => x.BRANCH_ID == dbBranch.BRANCH_ID && x.STATUS == 1); // if (count > 0) // { // result.IsSuccess = false; // result.ErrorCode = "6"; // result.ErrorMessage = "ไม่สามารถอัพเดตข้อมูลเป็นปิดสาขาได้ เนื่องจาก มีการผูกสาขากับ Communication Pool"; // return result; // } // } // //duplicate code and name // var countDuplicate = _context.TB_R_BRANCH.Count(p => p.BRANCH_NAME == request.BranchName && p.BRANCH_CODE != request.BranchCode); // if (countDuplicate > 0) // { // result.IsSuccess = false; // result.ErrorCode = "6"; // result.ErrorMessage = string.Format("{0} มีในระบบแล้ว", request.BranchName); // return result; // } // //check recursive upper branch // if (!CheckRecursiveBranch(dbBranch.BRANCH_CODE, upperBranchId)) // { // result.IsSuccess = false; // result.ErrorCode = "6"; // result.ErrorMessage = "CSM : การบันทึกข้อมูลไม่สำเร็จเนื่องจากพบ Recursive Upper Branch"; // return result; // } // } // #endregion // } // dbBranch.CHANNEL_ID = channelId; // dbBranch.BRANCH_CODE = ValueOrDefault(request.BranchCode); // dbBranch.BRANCH_NAME = ValueOrDefault(request.BranchName); // dbBranch.STATUS = Convert.ToInt16(request.Status); // dbBranch.UPPER_BRANCH_ID = upperBranchId; // dbBranch.START_TIME_HOUR = Convert.ToInt16(request.StartTimeHour); // dbBranch.START_TIME_MINUTE = Convert.ToInt16(request.StartTimeMinute); // dbBranch.END_TIME_HOUR = Convert.ToInt16(request.EndTimeHour); // dbBranch.END_TIME_MINUTE = Convert.ToInt16(request.EndTimeMinute); // dbBranch.BRANCH_HOME_NO = ValueOrDefault(request.HomeNo); // dbBranch.BRANCH_MOO = ValueOrDefault(request.Moo); // dbBranch.BRANCH_BUILDING = ValueOrDefault(request.Building); // dbBranch.BRANCH_FLOOR = ValueOrDefault(request.Floor); // dbBranch.BRANCH_SOI = ValueOrDefault(request.Soi); // dbBranch.BRANCH_STREET = ValueOrDefault(request.Street); // dbBranch.BRANCH_PROVINCE = ValueOrDefault(request.Province); // dbBranch.BRANCH_AMPHUR = ValueOrDefault(request.Amphur); // dbBranch.BRANCH_TAMBOL = ValueOrDefault(request.Tambol); // dbBranch.BRANCH_ZIPCODE = ValueOrDefault(request.Zipcode); // var now = DateTime.Now; // if (result.IsNewBranch) // { // dbBranch.CREATE_USER = ValueOrDefault(request.ActionUsername); // dbBranch.UPDATE_USER = ValueOrDefault(request.ActionUsername); // dbBranch.CREATE_DATE = now; // dbBranch.UPDATE_DATE = now; // _context.TB_R_BRANCH.Add(dbBranch); // } // else // { // dbBranch.UPDATE_USER = ValueOrDefault(request.ActionUsername); // dbBranch.UPDATE_DATE = DateTime.Now; // SetEntryStateModified(dbBranch); // } // this.Save(); // result.IsSuccess = true; // return result; #endregion string logMsg; var today = DateTime.Now; string branchCode = request.BranchCode.NullSafeTrim(); var dbBranch = _context.TB_R_BRANCH.FirstOrDefault(x => x.BRANCH_CODE == branchCode); bool isNewBranch = (dbBranch == null); if (isNewBranch) { dbBranch = new TB_R_BRANCH(); } else { // เช็คเงื่อนไขการปิดสาขา if (dbBranch.STATUS == 1 && request.Status == 0) { if (_context.TB_M_POOL_BRANCH.Any(x => x.BRANCH_ID == dbBranch.BRANCH_ID && x.STATUS == Constants.ApplicationStatus.Active)) { response.IsSuccess = false; response.ErrorCode = "6"; response.ErrorMessage = "ไม่สามารถอัพเดตข้อมูลเป็นปิดสาขาได้ เนื่องจาก มีการผูกสาขากับ Communication Pool"; return(response); } } // Check recursive upper branch if (!CheckRecursiveBranch(dbBranch.BRANCH_CODE, upperBranchId)) { response.IsSuccess = false; response.ErrorCode = "6"; response.ErrorMessage = "CSM : การบันทึกข้อมูลไม่สำเร็จเนื่องจากพบ Recursive Upper Branch"; return(response); } } dbBranch.CHANNEL_ID = channelId; dbBranch.BRANCH_CODE = branchCode; dbBranch.BRANCH_NAME = request.BranchName.NullSafeTrim(); dbBranch.STATUS = request.Status; dbBranch.UPPER_BRANCH_ID = upperBranchId; dbBranch.START_TIME_HOUR = (short)request.StartTimeHour; dbBranch.START_TIME_MINUTE = (short)request.StartTimeMinute; dbBranch.END_TIME_HOUR = (short)request.EndTimeHour; dbBranch.END_TIME_MINUTE = (short)request.EndTimeMinute; dbBranch.BRANCH_HOME_NO = request.HomeNo.NullSafeTrim(); dbBranch.BRANCH_MOO = request.Moo.NullSafeTrim(); dbBranch.BRANCH_BUILDING = request.Building.NullSafeTrim(); dbBranch.BRANCH_FLOOR = request.Floor.NullSafeTrim(); dbBranch.BRANCH_SOI = request.Soi.NullSafeTrim(); dbBranch.BRANCH_STREET = request.Street.NullSafeTrim(); dbBranch.BRANCH_PROVINCE = request.Province.NullSafeTrim(); dbBranch.BRANCH_AMPHUR = request.Amphur.NullSafeTrim(); dbBranch.BRANCH_TAMBOL = request.Tambol.NullSafeTrim(); dbBranch.BRANCH_ZIPCODE = request.Zipcode.NullSafeTrim(); dbBranch.UPDATE_USER = request.ActionUsername.NullSafeTrim(); dbBranch.UPDATE_DATE = today; if (isNewBranch) { logMsg = "Insert Branch"; dbBranch.CREATE_USER = request.ActionUsername.NullSafeTrim(); dbBranch.CREATE_DATE = today; _context.TB_R_BRANCH.Add(dbBranch); } else { logMsg = "Update Branch"; SetEntryStateModified(dbBranch); } response.IsSuccess = (Save() > 0); if (response.IsSuccess) { Logger.Info(_logMsg.Clear().SetPrefixMsg(logMsg).ToSuccessLogString()); } else { Logger.Info(_logMsg.Clear().SetPrefixMsg(logMsg).Add("Error Message", "Failed to save data").ToFailLogString()); } return(response); } catch (Exception ex) { Logger.Error("Exception occur:\n", ex); Logger.Info(_logMsg.Clear().SetPrefixMsg("Insert or Update Branch").Add("Error Message", ex.Message).ToFailLogString()); response = new InsertOrUpdateBranchResponse { IsSuccess = false, ErrorCode = "1", ErrorMessage = ex.Message, }; return(response); } finally { _context.Configuration.AutoDetectChangesEnabled = false; } }
public InsertOrUpdateBranchResponse InsertOrUpdateBranch(InsertOrUpdateBranchRequest request, int channelId, int?upperBranchId) { _context.Configuration.AutoDetectChangesEnabled = false; try { var result = new InsertOrUpdateBranchResponse(); var dbBranch = _context.TB_R_BRANCH.Where(x => x.BRANCH_CODE.Trim().ToUpper() == request.BranchCode.Trim().ToUpper()).FirstOrDefault(); if (dbBranch == null) { dbBranch = new TB_R_BRANCH(); result.IsNewBranch = true; } else { result.IsNewBranch = false; // IF (Change Branch) Or (Change Status 1 to 0) if (dbBranch.STATUS == 1 && request.Status == 0) { #region == Validate Relation Between Branch to Pool == var count = _context.TB_M_POOL_BRANCH.Count(x => x.BRANCH_ID == dbBranch.BRANCH_ID && x.STATUS == 1); if (count > 0) { result.IsSuccess = false; result.ErrorCode = "6"; result.ErrorMessage = "ไม่สามารถอัพเดตข้อมูลเป็นปิดสาขาได้ เนื่องจาก มีการผูกสาขากับ Communication Pool"; return(result); } #endregion } } dbBranch.CHANNEL_ID = channelId; dbBranch.BRANCH_CODE = ValueOrDefault(request.BranchCode); dbBranch.BRANCH_NAME = ValueOrDefault(request.BranchName); dbBranch.STATUS = Convert.ToInt16(request.Status); dbBranch.UPPER_BRANCH_ID = upperBranchId; dbBranch.START_TIME_HOUR = Convert.ToInt16(request.StartTimeHour); dbBranch.START_TIME_MINUTE = Convert.ToInt16(request.StartTimeMinute); dbBranch.END_TIME_HOUR = Convert.ToInt16(request.EndTimeHour); dbBranch.END_TIME_MINUTE = Convert.ToInt16(request.EndTimeMinute); dbBranch.BRANCH_HOME_NO = ValueOrDefault(request.HomeNo); dbBranch.BRANCH_MOO = ValueOrDefault(request.Moo); dbBranch.BRANCH_BUILDING = ValueOrDefault(request.Building); dbBranch.BRANCH_FLOOR = ValueOrDefault(request.Floor); dbBranch.BRANCH_SOI = ValueOrDefault(request.Soi); dbBranch.BRANCH_STREET = ValueOrDefault(request.Street); dbBranch.BRANCH_PROVINCE = ValueOrDefault(request.Province); dbBranch.BRANCH_AMPHUR = ValueOrDefault(request.Amphur); dbBranch.BRANCH_TAMBOL = ValueOrDefault(request.Tambol); dbBranch.BRANCH_ZIPCODE = ValueOrDefault(request.Zipcode); var now = DateTime.Now; if (result.IsNewBranch) { dbBranch.CREATE_USER = ValueOrDefault(request.ActionUsername); dbBranch.UPDATE_USER = ValueOrDefault(request.ActionUsername); dbBranch.CREATE_DATE = now; dbBranch.UPDATE_DATE = now; _context.TB_R_BRANCH.Add(dbBranch); } else { dbBranch.UPDATE_USER = ValueOrDefault(request.ActionUsername); dbBranch.UPDATE_DATE = DateTime.Now; SetEntryStateModified(dbBranch); } this.Save(); result.IsSuccess = true; return(result); } catch (Exception ex) { Logger.Error("Exception occur:\n", ex); return(new InsertOrUpdateBranchResponse() { IsSuccess = false, ErrorCode = "1", ErrorMessage = ex.Message, }); } finally { _context.Configuration.AutoDetectChangesEnabled = false; } }