public ResultInfo CreateAgencyC2() { var log = new MongoHistoryAPI() { APIUrl = "/api/agency/createagencyc2", CreateTime = DateTime.Now, Sucess = 1 }; var result = new ResultInfo() { id = "1", msg = "success" }; var requestContent = Request.Content.ReadAsStringAsync().Result; try { var jsonserializer = new JavaScriptSerializer(); var paser = jsonserializer.Deserialize <AgencyCreateRequest>(requestContent); log.Content = new JavaScriptSerializer().Serialize(paser); if (!mongoHelper.checkLoginSession(paser.user, paser.token)) { throw new Exception("Wrong token and user login!"); } var staff = db.HaiStaffs.Where(p => p.UserLogin == paser.user).FirstOrDefault(); if (staff == null) { throw new Exception("Chỉ nhân viên công ty mới được quyền tạo"); } /* * if (checkC1 == null) * checkC1 = db.C1Info.Where(p => p.Code == "0000000000").FirstOrDefault(); */ var agencyCode = GetAgencyCodeTemp(staff.HaiBranch.Code); CInfoCommon cInfo = new CInfoCommon() { Id = Guid.NewGuid().ToString(), AddressInfo = paser.address, BranchCode = staff.HaiBranch.Code, BusinessLicense = paser.businessLicense, CRank = paser.rank, CDeputy = paser.deputy, CName = paser.name, CreateTime = DateTime.Now, ProvinceName = paser.province, DistrictName = paser.district, Phone = paser.phone, IdentityCard = paser.identityCard, CType = "CII", TaxCode = paser.taxCode, Lat = paser.lat, Lng = paser.lng, CCode = agencyCode, WardName = paser.ward, Country = paser.country }; db.CInfoCommons.Add(cInfo); db.SaveChanges(); C2Info c2 = new C2Info() { Id = Guid.NewGuid().ToString(), StoreName = paser.name, Deputy = paser.deputy, IsActive = 0, Code = agencyCode, InfoId = cInfo.Id, CStatus = 1 }; db.C2Info.Add(c2); db.SaveChanges(); // db.Entry(staff).State = System.Data.Entity.EntityState.Modified; // db.SaveChanges(); var staffC2 = new StaffWithC2() { C2Id = c2.Id, StaffId = staff.Id, GroupChoose = paser.group }; db.StaffWithC2.Add(staffC2); db.SaveChanges(); var checkC1 = db.C1Info.Where(p => p.Code == paser.c1Id).FirstOrDefault(); // import c1 if (checkC1 != null) { var c2C1Add = new C2C1() { C1Code = checkC1.Code, C2Code = agencyCode, Id = Guid.NewGuid().ToString(), Priority = 1, ModifyDate = DateTime.Now }; db.C2C1.Add(c2C1Add); db.SaveChanges(); } // save info var agencyImage = new SaveAgencyShopImage() { Id = Guid.NewGuid().ToString(), AddressFull = paser.address, Cinfo = cInfo.Id, Country = paser.country, District = paser.district, Lat = paser.lat, Province = paser.province, CreateTime = DateTime.Now, StaffId = staff.Id, Lng = paser.lng, Ward = paser.ward, ImagePath = paser.image }; db.SaveAgencyShopImages.Add(agencyImage); } catch (Exception e) { result.id = "0"; result.msg = e.Message; log.Sucess = 0; } log.ReturnInfo = new JavaScriptSerializer().Serialize(result); mongoHelper.createHistoryAPI(log); return(result); }
public ResultInfo ModifyAgencyC2() { var log = new MongoHistoryAPI() { APIUrl = "/api/agency/modifyagencyc2", CreateTime = DateTime.Now, Sucess = 1 }; var result = new ResultInfo() { id = "1", msg = "success" }; var requestContent = Request.Content.ReadAsStringAsync().Result; try { var jsonserializer = new JavaScriptSerializer(); var paser = jsonserializer.Deserialize <AgencyModifyRequest>(requestContent); log.Content = new JavaScriptSerializer().Serialize(paser); if (!mongoHelper.checkLoginSession(paser.user, paser.token)) { throw new Exception("Wrong token and user login!"); } var staff = db.HaiStaffs.Where(p => p.UserLogin == paser.user).FirstOrDefault(); if (staff == null) { throw new Exception("Chỉ nhân viên công ty mới được quyền tạo"); } var checkC2 = db.C2Info.Find(paser.id); if (checkC2 == null) { throw new Exception("Sai thông tin khách hàng"); } checkC2.StoreName = paser.name; checkC2.Deputy = paser.deputy; CInfoCommon cinfo = checkC2.CInfoCommon; cinfo.CDeputy = paser.deputy; cinfo.CName = paser.name; // cinfo.Phone = paser.phone; //cinfo.IdentityCard = paser.identityCard; cinfo.BusinessLicense = paser.businessLicense; cinfo.TaxCode = paser.taxCode; cinfo.Country = paser.country; cinfo.ProvinceName = paser.province; cinfo.DistrictName = paser.district; cinfo.WardName = paser.ward; cinfo.AddressInfo = paser.address; if (paser.lat != 0 && paser.lng != 0) { // cho cap nhat toa do voi dk toa do duoi server dc reset if (cinfo.Lat == 0 || cinfo.Lng == 0 || cinfo.Lat == null || cinfo.Lng == null) { cinfo.Lat = paser.lat; cinfo.Lng = paser.lng; } else { throw new Exception("Tọa độ đã được cập nhật trước, nếu bạn muốn cập nhật lại, liên hệ ban quản trị"); } } db.Entry(checkC2).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); db.Entry(cinfo).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); var staffC2 = checkC2.StaffWithC2.Where(p => p.StaffId == staff.Id).FirstOrDefault(); if (staffC2 == null) { var staffC2Create = new StaffWithC2() { C2Id = checkC2.Id, StaffId = staff.Id, GroupChoose = paser.group }; db.StaffWithC2.Add(staffC2Create); db.SaveChanges(); } else { staffC2.GroupChoose = paser.group; db.Entry(staffC2).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } } catch (Exception e) { result.id = "0"; result.msg = e.Message; log.Sucess = 0; } log.ReturnInfo = new JavaScriptSerializer().Serialize(result); mongoHelper.createHistoryAPI(log); return(result); }
private void AddAgencyC2(HaiStaff staff, string AgencyId, HttpPostedFileBase files, int?group) { if (files != null && files.ContentLength > 0) { string extension = System.IO.Path.GetExtension(files.FileName); if (extension.Equals(".xlsx")) { string fileSave = "staffcii_" + DateTime.Now.ToString("ddMMyyyyhhmmss") + extension; string path = Server.MapPath("~/temp/" + fileSave); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } files.SaveAs(path); FileInfo newFile = new FileInfo(path); var package = new ExcelPackage(newFile); ExcelWorksheet sheet = package.Workbook.Worksheets[1]; int totalRows = sheet.Dimension.End.Row; int totalCols = sheet.Dimension.End.Column; for (int i = 2; i <= totalRows; i++) { string code = Convert.ToString(sheet.Cells[i, 1].Value); int groupNumber = Convert.ToInt32(sheet.Cells[i, 2].Value); var checkC2 = db.C2Info.Where(p => p.Code == code).FirstOrDefault(); if (checkC2 != null) { // staff.C2Info.Add(checkC2); var staffC2 = new StaffWithC2() { C2Id = checkC2.Id, StaffId = staff.Id, GroupChoose = groupNumber }; db.StaffWithC2.Add(staffC2); db.SaveChanges(); } } } } else { var checkC2 = db.C2Info.Where(p => p.Code == AgencyId).FirstOrDefault(); if (checkC2 != null) { var staffC2 = new StaffWithC2() { C2Id = checkC2.Id, StaffId = staff.Id, GroupChoose = group }; db.StaffWithC2.Add(staffC2); db.SaveChanges(); } } // db.Entry(staff).State = System.Data.Entity.EntityState.Modified; // db.SaveChanges(); }