public ActionResult GetDistrictRoutes(string provinceId, string employeeId, string type) { var checkEmployee = db.BS_Employees.Find(employeeId); if (checkEmployee == null) { return(Json(new ResultInfo() { error = 1, msg = "Sai nhân viên" }, JsonRequestBehavior.AllowGet)); } var allDistrict = db.BS_Districts.Where(p => p.ProvinceID == provinceId).ToList(); List <ERouteInfo> districtRoutes = new List <ERouteInfo>(); foreach (var item in allDistrict) { var findStaffRoutes = db.BS_Routes.Where(p => p.ProvinceID == provinceId && p.DistrictID == item.DistrictID && p.Type == type).ToList(); var routeInfo = new ERouteInfo() { DistrictID = item.DistrictID, DistrictName = item.DistrictName, ProvinceID = provinceId, Staffs = new List <CommonData>(), Type = type, ISJoin = false }; foreach (var staff in findStaffRoutes) { var checkStaff = db.BS_Employees.Find(staff.EmployeeID); if (checkStaff != null && checkStaff.PostOfficeID == checkEmployee.PostOfficeID) { routeInfo.Staffs.Add(new CommonData() { code = checkStaff.EmployeeID, name = checkStaff.EmployeeName }); if (checkStaff.EmployeeID == employeeId) { routeInfo.ISJoin = true; } } } districtRoutes.Add(routeInfo); } return(Json(new ResultInfo() { error = 0, msg = "", data = districtRoutes }, JsonRequestBehavior.AllowGet)); }
public ActionResult AddDistrictRoutes(ERouteInfo info, string employeeId) { var getAll = db.BS_Routes.Where(p => p.ProvinceID == info.ProvinceID && p.Type == info.Type && p.DistrictID == info.DistrictID).ToList(); var check = getAll.Where(p => p.EmployeeID == employeeId).FirstOrDefault(); if (info.ISJoin) { // them if (check == null) { var routes = new BS_Routes() { RouteID = Guid.NewGuid().ToString(), DistrictID = info.DistrictID, EmployeeID = employeeId, IsDetail = getAll.Count() > 0?true:false, ProvinceID = info.ProvinceID, Type = info.Type }; db.BS_Routes.Add(routes); db.SaveChanges(); foreach (var item in getAll) { item.IsDetail = true; db.Entry(item).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } } } else { // remove if (check != null) { db.BS_Routes.Remove(check); var findWards = db.BS_RouteDetails.Where(p => p.RouteID == check.RouteID).ToList(); findWards.Clear(); db.SaveChanges(); getAll.Remove(check); if (getAll.Count() == 1) { var firstCheck = getAll[0]; firstCheck.IsDetail = false; db.Entry(firstCheck).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } } } return(Json(new ResultInfo() { error = 0, msg = "" }, JsonRequestBehavior.AllowGet)); }