public CommonResponse AddUpdateEmployeeShift(AddUpdateOperatorShift data) { CommonResponse obj = new CommonResponse(); try { DateTime fdate = Convert.ToDateTime(data.fromDate).Date; DateTime tdate = Convert.ToDateTime(data.toDate).Date; var check = db.UnitworkccsTblemployeeshiftdetails.Where(m => m.Id == data.id && m.EmployeeId == data.employeeId && (m.FromDate.Date <= fdate && m.ToDate.Date >= tdate)).FirstOrDefault(); if (check == null) { UnitworkccsTblemployeeshiftdetails tblOperatorDetailsShift = new UnitworkccsTblemployeeshiftdetails(); tblOperatorDetailsShift.EmployeeId = Convert.ToInt32(db.UnitworkccsTbloperatordetails.Where(m => m.OpId == data.employeeId).Select(m => m.OpNo).FirstOrDefault()); tblOperatorDetailsShift.Shift = data.shift; tblOperatorDetailsShift.MachineIds = data.machineIds; tblOperatorDetailsShift.FromDate = fdate; tblOperatorDetailsShift.ToDate = tdate; tblOperatorDetailsShift.IsDeleted = 0; tblOperatorDetailsShift.CreatedOn = DateTime.Now; tblOperatorDetailsShift.CreatedBy = 1; db.UnitworkccsTblemployeeshiftdetails.Add(tblOperatorDetailsShift); db.SaveChanges(); obj.isStatus = true; obj.response = ResourceResponse.AddedSuccessMessage; } else { check.Shift = data.shift; check.MachineIds = data.machineIds; // check.FromDate = fdate; // check.ToDate = tdate; check.IsDeleted = 0; check.ModifiedOn = DateTime.Now; check.ModifiedBy = 2; db.SaveChanges(); obj.isStatus = true; obj.response = ResourceResponse.UpdatedSuccessMessage; } } catch (Exception e) { obj.isStatus = false; obj.response = ResourceResponse.FailureMessage; } return(obj); }
/// <summary> /// Upload Operators /// </summary> /// <param name="data"></param> /// <returns></returns> /// //public CommonResponse UploadOperatorsShift(List<AddUpdateOperatorShiftExcel> data) //{ // CommonResponse obj = new CommonResponse(); // try // { // string connectionString1 = Path.Combine(appSettings.DefaultConnection); // using (SqlConnection sqlConn = new SqlConnection(connectionString1)) // { // string sql = "delete from [unitworksccs].[unitworkccs].[tblEmployeeShiftDetails]"; // using (SqlCommand sqlCmd = new SqlCommand(sql, sqlConn)) // { // sqlConn.Open(); // sqlCmd.ExecuteNonQuery(); // } // } // foreach (var item in data) // { // DateTime fdate = Convert.ToDateTime(item.fromDate).Date; // DateTime tdate = Convert.ToDateTime(item.toDate).Date; // var check = db.TblEmployeeShiftDetails.Where(m => m.EmployeeId == item.employeeId && m.FromDate <= fdate.Date && m.ToDate >= tdate.Date).FirstOrDefault(); // if (check == null) // { // TblEmployeeShiftDetails tblOperatorDetailsShift = new TblEmployeeShiftDetails(); // tblOperatorDetailsShift.Shift = item.shift; // tblOperatorDetailsShift.EmployeeId = item.employeeId; // tblOperatorDetailsShift.FromDate = fdate; // tblOperatorDetailsShift.ToDate = tdate; // if (item.machines != null) // { // string[] machineNames = item.machines.Split(','); // foreach (var items in machineNames) // { // var dbCheck = db.Tblmachinedetails.Where(m => m.MachineName == items).Select(m => m.MachineId).FirstOrDefault(); // #region // if (dbCheck != null) // { // tblOperatorDetailsShift.MachineIds = tblOperatorDetailsShift.MachineIds + "," + dbCheck + ","; // tblOperatorDetailsShift.MachineIds = tblOperatorDetailsShift.MachineIds.TrimEnd(','); // tblOperatorDetailsShift.MachineIds = tblOperatorDetailsShift.MachineIds.TrimStart(','); // } // #endregion // } // } // else // { // tblOperatorDetailsShift.MachineIds = " "; // } // tblOperatorDetailsShift.IsDeleted = 0; // tblOperatorDetailsShift.CreatedOn = DateTime.Now; // tblOperatorDetailsShift.CreatedBy = 1; // db.TblEmployeeShiftDetails.Add(tblOperatorDetailsShift); // db.SaveChanges(); // obj.isStatus = true; // obj.response = ResourceResponse.AddedSuccessMessage; // } // else // { // check.Shift = item.shift; // // check.FromDate = fdate; // // check.ToDate = tdate; // if (item.machines != null) // { // string[] machineNames = item.machines.Split(','); // foreach (var items in machineNames) // { // var dbCheck = db.Tblmachinedetails.Where(m => m.MachineName == items).Select(m => m.MachineId).FirstOrDefault(); // #region // check.MachineIds = check.MachineIds + "," + dbCheck + ","; // check.MachineIds = check.MachineIds.TrimEnd(','); // check.MachineIds = check.MachineIds.TrimStart(','); // #endregion // } // } // check.IsDeleted = 0; // check.ModifiedOn = DateTime.Now; // check.ModifiedBy = 2; // db.SaveChanges(); // obj.isStatus = true; // obj.response = ResourceResponse.UpdatedSuccessMessage; // } // } // } // catch (Exception e) // { // obj.isStatus = false; // obj.response = ResourceResponse.FailureMessage; // } // return obj; //} public CommonResponse UploadOperatorsShift(AddUpdateOperatorShiftExcel data) { CommonResponse obj = new CommonResponse(); try { var dbCheckDelete = db.UnitworkccsTblemployeeshiftdetails.Where(m => m.IsDeleted == 0).ToList(); db.UnitworkccsTblemployeeshiftdetails.RemoveRange(dbCheckDelete); db.SaveChanges(); DateTime fdate = Convert.ToDateTime(data.fromDate).Date; DateTime tdate = Convert.ToDateTime(data.toDate).Date; foreach (var list in data.employeeList) { var check = db.UnitworkccsTblemployeeshiftdetails.Where(m => m.EmployeeId == list.employeeId && m.FromDate <= fdate.Date && m.ToDate >= tdate.Date).FirstOrDefault(); if (check == null) { UnitworkccsTblemployeeshiftdetails tblOperatorDetailsShift = new UnitworkccsTblemployeeshiftdetails(); tblOperatorDetailsShift.Shift = list.shift; tblOperatorDetailsShift.EmployeeId = list.employeeId; tblOperatorDetailsShift.FromDate = fdate; tblOperatorDetailsShift.ToDate = tdate; if (list.machines != null) { string[] machineNames = list.machines.Split(','); foreach (var items in machineNames) { var dbCheck = db.UnitworkccsTblmachinedetails.Where(m => m.MachineName == items).Select(m => m.MachineId).FirstOrDefault(); #region if (dbCheck != 0) { tblOperatorDetailsShift.MachineIds = tblOperatorDetailsShift.MachineIds + "," + dbCheck + ","; tblOperatorDetailsShift.MachineIds = tblOperatorDetailsShift.MachineIds.TrimEnd(','); tblOperatorDetailsShift.MachineIds = tblOperatorDetailsShift.MachineIds.TrimStart(','); } #endregion } } else { tblOperatorDetailsShift.MachineIds = ""; } tblOperatorDetailsShift.IsDeleted = 0; tblOperatorDetailsShift.CreatedOn = DateTime.Now; tblOperatorDetailsShift.CreatedBy = 1; db.UnitworkccsTblemployeeshiftdetails.Add(tblOperatorDetailsShift); db.SaveChanges(); obj.isStatus = true; obj.response = ResourceResponse.AddedSuccessMessage; } else { check.Shift = list.shift; // check.FromDate = fdate; // check.ToDate = tdate; if (list.machines != null) { string[] machineNames = list.machines.Split(','); foreach (var items in machineNames) { var dbCheck = db.UnitworkccsTblmachinedetails.Where(m => m.MachineName == items).Select(m => m.MachineId).FirstOrDefault(); #region check.MachineIds = check.MachineIds + "," + dbCheck + ","; check.MachineIds = check.MachineIds.TrimEnd(','); check.MachineIds = check.MachineIds.TrimStart(','); #endregion } } check.IsDeleted = 0; check.ModifiedOn = DateTime.Now; check.ModifiedBy = 2; db.SaveChanges(); obj.isStatus = true; obj.response = ResourceResponse.UpdatedSuccessMessage; } } } catch (Exception e) { obj.isStatus = false; obj.response = ResourceResponse.FailureMessage; } return(obj); }