private SMSResult checkContentFarmer(SMSContent paser) { var result = new SMSResult() { status = 1 }; try { // so var cInfo = db.CInfoCommons.Where(p => p.Phone == paser.phone).FirstOrDefault(); if (cInfo == null) { return new SMSResult { status = 1, message = "So dien thoai Quy Khach vua nhan tin chua dang ky tham gia Chuong trinh nhan tin cung voi Cong ty HAI. Chi tiet lien he NVTT hoac 1800577768" } } ; if (cInfo.CType != "FARMER") { return new SMSResult { status = 1, message = "So dien thoai Quy Khach vua nhan tin chua dang ky tham gia Chuong trinh nhan tin cung voi Cong ty HAI. Chi tiet lien he NVTT hoac 1800577768" } } ; // lay danh sach sự kiện phù hợp với user (theo khu vực) List <EventInfo> listEventUser = new List <EventInfo>(); //var eventArea = db.EventAreaFarmers.Where(p => p.AreaId == cInfo.AreaId && p.EventInfo.ESTT == 1).ToList(); var dateNow = DateTime.Now; var eventArea = (from log in db.EventAreaFarmers where log.AreaId == cInfo.AreaId && log.EventInfo.ESTT == 1 && (DbFunctions.TruncateTime(log.EventInfo.BeginTime) <= DbFunctions.TruncateTime(dateNow) && DbFunctions.TruncateTime(log.EventInfo.EndTime) >= DbFunctions.TruncateTime(dateNow)) select log).ToList(); foreach (var item in eventArea) { var cusJoin = db.EventCustomerFarmers.Where(p => p.EventId == item.EventId && p.CInfoCommon.AreaId == item.AreaId).ToList(); if (cusJoin.Count() > 0) { var cJoin = cusJoin.Where(p => p.CInfoId == cInfo.Id).FirstOrDefault(); if (cJoin != null) { listEventUser.Add(item.EventInfo); } } else { listEventUser.Add(item.EventInfo); } } if (listEventUser.Count() == 0) { return new SMSResult { status = 4, message = "Quy khach dang dung hang chinh hang. Cam on Quy khach da tin dung & ung ho hang cua cong ty CPND HAI" } } ; // kiem tra ma dung, ma sau List <ProductSeri> righCode = new List <ProductSeri>(); foreach (var item in paser.products) { // lây mã seri // eventCode or seri var productSeri = db.ProductSeris.Where(p => p.Code == item && p.IsUse == 0 && p.SeriType == 2).FirstOrDefault(); if (productSeri == null) { return(new SMSResult { status = 4, message = "Ma " + item + " khong dung. Vui long kiem tra lai ma so nhan tin. Xin cam on" }); } else if (productSeri.IsUse == 1) { return(new SMSResult { status = 4, message = "Ma " + item + " da duoc su dung vao (" + productSeri.ModifyTime + "), Vui long kiem tra lai ma so nhan tin. Xin cam on" }); } else { righCode.Add(productSeri); } } Hashtable map = new Hashtable(); List <string> productAttend = new List <string>(); result.status = 5; foreach (var item in righCode) { try { // cap nhat lịch su var hEvent = new MSGPoint() { Id = Guid.NewGuid().ToString(), AcceptTime = DateTime.Now, CInfoId = cInfo.Id, UserLogin = paser.code, ProductId = item.ProductId, Barcode = item.Code, MSGType = "SMS" }; List <MSGPointEvent> listPointEvent = new List <MSGPointEvent>(); foreach (var userEvent in listEventUser) { // kiem tra san pham co trong su kien nay ko var productEvent = userEvent.EventProducts.Where(p => p.ProductId == item.ProductId).FirstOrDefault(); if (productEvent != null) { var pointEvemt = new MSGPointEvent() { EventId = userEvent.Id, MSGPointId = hEvent.Id, Point = productEvent.Point }; listPointEvent.Add(pointEvemt); } } // if (listPointEvent.Count() > 0) { if (!productAttend.Contains(item.ProductId)) { productAttend.Add(item.ProductId); } item.IsUse = 1; item.ModifyTime = DateTime.Now; db.Entry(item).State = System.Data.Entity.EntityState.Modified; db.MSGPoints.Add(hEvent); db.SaveChanges(); foreach (var pevent in listPointEvent) { if (map.ContainsKey(pevent.EventId)) { var oldPoint = Convert.ToInt32(map[pevent.EventId]); map[pevent.EventId] = oldPoint + pevent.Point; } else { map.Add(pevent.EventId, Convert.ToInt32(pevent.Point)); } db.MSGPointEvents.Add(pevent); // luu diem cho khach hang var agencyPoint = db.AgencySavePoints.Where(p => p.EventId == pevent.EventId && p.CInfoId == cInfo.Id).FirstOrDefault(); if (agencyPoint == null) { var newAgencyPoint = new AgencySavePoint() { EventId = pevent.EventId, CInfoId = cInfo.Id, PointSave = pevent.Point, CreateTime = DateTime.Now }; db.AgencySavePoints.Add(newAgencyPoint); } else { var newPoint = agencyPoint.PointSave + pevent.Point; agencyPoint.PointSave = newPoint; agencyPoint.ModifyTime = DateTime.Now; db.Entry(agencyPoint).State = System.Data.Entity.EntityState.Modified; } db.SaveChanges(); } } } catch { } } // // phan qua string pointEvent = ""; int countPoint = 0; foreach (var item in map.Keys) { var eventInfo = db.EventInfoes.Find(item); var savePoint = eventInfo.AgencySavePoints.Where(p => p.CInfoCommon.CCode == paser.code).FirstOrDefault(); int?point = 0; if (savePoint != null) { point = savePoint.PointSave; } string aWard = ""; var listAward = eventInfo.AwardInfoes.OrderByDescending(p => p.Point).ToList(); foreach (var aWardItem in listAward) { if (aWardItem.Point <= point) { aWard = ConvertToUnsign3(aWardItem.Name); break; } } var nameEvent = ConvertToUnsign3(eventInfo.Name); if (!String.IsNullOrEmpty(aWard)) { pointEvent = " , " + aWard + " tu " + nameEvent; } countPoint += Convert.ToInt32(map[item]); } // string msgReturn = "Chuc mung Quy khach vua tich luy " + countPoint + " diem tu "; foreach (string item in productAttend) { var productCheck = db.ProductInfoes.Find(item); if (productCheck != null) { msgReturn += ConvertToUnsign3(productCheck.PName) + " ,"; } } msgReturn = msgReturn.Remove(msgReturn.Count() - 1, 1); msgReturn += ". Cam on Quy khach da tin dung & ung ho hang cua cong ty CPND HAI. "; if (!String.IsNullOrEmpty(pointEvent)) { pointEvent = pointEvent.Remove(0, 2); msgReturn += "Chuc mung Quy khach nhan duoc " + pointEvent + ". Cam on Quy khach da tin dung & ung ho hang cua cong ty CPND HAI."; } result.message = msgReturn; } catch (Exception e) { result.status = 0; result.message = e.Message; } return(result); }
public ResultCodeEvent SendCodeEvent() { // send code // /api/rest/sendcodeevent var history = new MongoHistoryAPI() { CreateTime = DateTime.Now, APIUrl = "/api/event/sendcodeevent", Sucess = 1 }; var result = new ResultCodeEvent() { id = "1" }; var requestContent = Request.Content.ReadAsStringAsync().Result; history.Content = requestContent; try { var jsonserializer = new JavaScriptSerializer(); var paser = jsonserializer.Deserialize <RequestCodeEvent>(requestContent); history.Content = new JavaScriptSerializer().Serialize(paser); if (!mongoHelper.checkLoginSession(paser.user, paser.token)) { throw new Exception("Tài khoản bạn đã đăng nhập ở thiết bị khác."); } var cInfo = db.CInfoCommons.Where(p => p.UserLogin == paser.user).FirstOrDefault(); if (cInfo == null) { throw new Exception("Chương trình chỉ dành cho khách hàng của HAI."); } if (cInfo.CType == "CI") { throw new Exception("Chương trình chỉ dành cho khách hàng là đại lý cấp 2, nông dân của HAI."); } var dateNow = DateTime.Now; // lay danh sach sự kiện phù hợp với user (theo khu vực) List <EventInfo> listEventUser = new List <EventInfo>(); if (cInfo.CType == "CII") { // var eventArea = db.EventAreas.Where(p => p.AreaId == cInfo.AreaId && p.EventInfo.ESTT == 1).ToList(); var eventArea = (from log in db.EventAreas where log.EventInfo.ESTT == 1 && (DbFunctions.TruncateTime(log.EventInfo.BeginTime) <= DbFunctions.TruncateTime(dateNow) && DbFunctions.TruncateTime(log.EventInfo.EndTime) >= DbFunctions.TruncateTime(dateNow)) select log).ToList(); foreach (var item in eventArea) { var cusJoin = db.EventCustomers.Where(p => p.EventId == item.EventId).ToList(); if (cusJoin.Count() > 0) { var cJoin = cusJoin.Where(p => p.CInfoId == cInfo.Id).FirstOrDefault(); if (cJoin != null) { listEventUser.Add(item.EventInfo); } } else { //var cJoin = cusJoin.Where(p => p.CInfoId == cInfo.Id && p.IsJoint == 0).FirstOrDefault(); // if (cJoin == null) listEventUser.Add(item.EventInfo); } } } else if (cInfo.CType == "FARMER") { // var eventArea = db.EventAreaFarmers.Where(p => p.AreaId == cInfo.AreaId && p.EventInfo.ESTT == 1).ToList(); var eventArea = (from log in db.EventAreaFarmers where log.EventInfo.ESTT == 1 && (DbFunctions.TruncateTime(log.EventInfo.BeginTime) <= DbFunctions.TruncateTime(dateNow) && DbFunctions.TruncateTime(log.EventInfo.EndTime) >= DbFunctions.TruncateTime(dateNow)) select log).ToList(); foreach (var item in eventArea) { var cusJoin = db.EventCustomerFarmers.Where(p => p.EventId == item.EventId).ToList(); if (cusJoin.Count() > 0) { var cJoin = cusJoin.Where(p => p.CInfoId == cInfo.Id).FirstOrDefault(); if (cJoin != null) { listEventUser.Add(item.EventInfo); } } else { listEventUser.Add(item.EventInfo); } } } if (listEventUser.Count() == 0) { throw new Exception("Không tìm thấy chương trình khuyến mãi nào phù hợp."); } List <GeneralInfo> listCodeReturn = new List <GeneralInfo>(); Hashtable map = new Hashtable(); foreach (var item in paser.codes) { try { // lây mã seri // eventCode or seri var product = GetProduct(item); if (product == null) { listCodeReturn.Add(new GeneralInfo() { name = "Không phải sản phẩm HAI", code = item, status = "Mã không hợp lệ" }); throw new Exception(); } var caseCode = item.Substring(0, 15); var boxCode = item.Substring(0, 16); // kiem tra la da nhap kho chua PHistory pHis = null; if (product.IsBox == 1) { pHis = db.PHistories.Where(p => p.BoxCode == boxCode && p.WCode == cInfo.CCode && p.PStatus == "NK").FirstOrDefault(); } else { pHis = db.PHistories.Where(p => p.CaseCode == caseCode && p.WCode == cInfo.CCode && p.PStatus == "NK").FirstOrDefault(); } if (pHis == null) { listCodeReturn.Add(new GeneralInfo() { name = product.PName, code = item, status = "Mã chưa nhập kho" }); throw new Exception(); } // kiem tra la da su dung chua var checkUse = db.MSGPoints.Where(p => p.Barcode == item).FirstOrDefault(); if (checkUse != null) { listCodeReturn.Add(new GeneralInfo() { name = product.PName, code = item, status = "Mã đã được sử dụng" }); throw new Exception(); } // cap nhat lịch su var hEvent = new MSGPoint() { Id = Guid.NewGuid().ToString(), AcceptTime = DateTime.Now, CInfoId = cInfo.Id, UserLogin = paser.user, ProductId = product.Id, Barcode = item, MSGType = "APP" }; List <MSGPointEvent> listPointEvent = new List <MSGPointEvent>(); foreach (var userEvent in listEventUser) { // kiem tra san pham co trong su kien nay ko var productEvent = userEvent.EventProducts.Where(p => p.ProductId == product.Id).FirstOrDefault(); if (productEvent != null) { var pointEvemt = new MSGPointEvent() { EventId = userEvent.Id, MSGPointId = hEvent.Id, Point = productEvent.Point }; listPointEvent.Add(pointEvemt); // tra thong tin ve client listCodeReturn.Add(new GeneralInfo() { code = item, name = product.PName, status = productEvent.EventInfo.Name + " + " + productEvent.Point + "***" }); } } // if (listPointEvent.Count() > 0) { db.MSGPoints.Add(hEvent); db.SaveChanges(); foreach (var pevent in listPointEvent) { if (map.ContainsKey(pevent.EventId)) { var oldPoint = Convert.ToInt32(map[pevent.EventId]); map[pevent.EventId] = oldPoint + pevent.Point; } else { map.Add(pevent.EventId, Convert.ToInt32(pevent.Point)); } db.MSGPointEvents.Add(pevent); // luu diem cho khach hang var agencyPoint = db.AgencySavePoints.Where(p => p.EventId == pevent.EventId && p.CInfoId == cInfo.Id).FirstOrDefault(); if (agencyPoint == null) { var newAgencyPoint = new AgencySavePoint() { EventId = pevent.EventId, CInfoId = cInfo.Id, PointSave = pevent.Point, CreateTime = DateTime.Now }; db.AgencySavePoints.Add(newAgencyPoint); } else { var newPoint = agencyPoint.PointSave + pevent.Point; agencyPoint.PointSave = newPoint; agencyPoint.ModifyTime = DateTime.Now; db.Entry(agencyPoint).State = System.Data.Entity.EntityState.Modified; } db.SaveChanges(); } } } catch { } } string pointEvent = ""; int countPoint = 0; foreach (var item in map.Keys) { var eventInfo = db.EventInfoes.Find(item); var savePoint = eventInfo.AgencySavePoints.Where(p => p.CInfoCommon.UserLogin == paser.user).FirstOrDefault(); int?point = 0; if (savePoint != null) { point = savePoint.PointSave; } string aWard = ""; var listAward = eventInfo.AwardInfoes.ToList(); foreach (var aWardItem in listAward) { if (aWardItem.Point <= point) { aWard += ", " + aWardItem.Name + "(" + aWardItem.Point + "điểm )"; } } var nameEvent = eventInfo.Name; pointEvent = pointEvent + ";" + nameEvent + " (" + map[item] + " điểm)"; if (!String.IsNullOrEmpty(aWard)) { aWard = aWard.Remove(0, 2); pointEvent = pointEvent + " - phần thưởng đổi: " + aWard; } else { pointEvent = pointEvent + "-chưa đủ điểm đổi quà"; } countPoint += Convert.ToInt32(map[item]); } string msgReturn = "Cộng " + countPoint; msgReturn = msgReturn + pointEvent; result.msg = msgReturn; result.codes = listCodeReturn; } catch (Exception e) { result.id = "0"; result.msg = e.Message; history.Sucess = 0; } mongoHelper.createHistoryAPI(history); return(result); }