public static string checkEnoughData(int chuki, DateTime dt) { try { string congThuc = CongThucDAO.getCongThuc(dt); if (congThuc == "failed") { return("failed"); } string[] divided = congThuc.Split(' ');// tach cac phan tu trong cong thuc List <PhanTu> listPhanTu = new List <PhanTu>(); for (int i = 0; i < divided.Length; i++) { if (divided[i].Length > 1) // divice la phan tu { string[] temp = divided[i].Split('@'); if (temp.Length > 1) { var tenDiemDo = temp[0]; var tenKenh = temp[1]; PhanTu pt = new PhanTu(); pt.DiemDoID = db.DiemDoes.Where(x => x.TenDiemDo == tenDiemDo).Select(x => x.ID).FirstOrDefault(); if (pt.DiemDoID == 0) { return("Điểm đo trong Công thức không tồn tại !!!"); } pt.KenhID = db.Kenhs.Where(x => x.Ten == tenKenh).Select(x => x.ID).FirstOrDefault(); if (pt.KenhID == 0) { return("Kênh trong Công thức không tồn tại !!!"); } pt.index = i; listPhanTu.Add(pt); } } } foreach (var item in listPhanTu) { var rs = db.SanLuongs.Where(x => x.ChuKy == chuki && x.DiemDoID == item.DiemDoID && x.KenhID == item.KenhID && x.Ngay == dt).FirstOrDefault(); if (rs == null) { return("failed"); } } return("success"); } catch { return("failed"); } }
public static string Calculator(int chuki, DateTime dt) { try { var enoughData = TongSanLuong_NgayDAO.checkEnoughData(chuki, dt); if (enoughData == "success") { string congThuc = CongThucDAO.getCongThuc(dt); int congThucID = db.CongThucTongSanLuongs.Where(x => x.ThoiGianBatDau <dt && x.ThoiGianKetThuc> dt).Select(x => x.ID).First(); string[] divided = congThuc.Split(' '); List <MyCustom> list = new List <MyCustom>(); List <string> charList = new List <string>(); var diemDo = db.DiemDoes.ToList(); var kenh = db.Kenhs.ToList(); for (int i = 0; i < divided.Length; i++) { if (divided[i].Length > 1) { string[] temp = divided[i].Split('@'); if (temp.Length > 1) { var temDiemDo = temp[0]; var tenKenh = temp[1]; var tempDD = diemDo.Where(x => x.TenDiemDo == temDiemDo).Select(x => x.ID).First(); var tempKenh = kenh.Where(x => x.Ten == tenKenh).Select(x => x.ID).First(); list.Add(new MyCustom() { DiemDoID = tempDD, KenhID = tempKenh, index = i }); } else { list.Add(new MyCustom() { index = i, value = Convert.ToDouble(temp[0]) }); } } } var tongSanLuongNgay = new List <TongSanLuong_Ngay>(); DateTime ngayTinh = dt; var listTemp = db.SanLuongs.Where(x => x.ChuKy == chuki && x.Ngay == ngayTinh).ToList(); string formula = ""; for (int j = 0; j < list.Count; j++) { var diemDoTemp = list[j].DiemDoID; var kenhTemp = list[j].KenhID; if (diemDoTemp != 0) { divided[list[j].index] = listTemp.Where(x => x.DiemDoID == diemDoTemp && x.KenhID == kenhTemp).Select(x => x.GiaTri).FirstOrDefault().ToString(); } else { divided[list[j].index] = list[j].value.ToString(); } } for (int stringBlank = 0; stringBlank < divided.Length; stringBlank++) { if (divided[stringBlank] == "" && stringBlank != divided.Length - 1) { formula += "0 "; } else { formula += divided[stringBlank] + ' '; } } string rpn = toRPN(formula.Trim()).Trim(); var value = CalculateRPN(rpn); TongSanLuong_Ngay tslng = new TongSanLuong_Ngay(); tslng.ChuKy = chuki; tslng.CongThucID = congThucID; tslng.Ngay = dt; tslng.GiaTri = (double)value; var rs = Insert(tslng); if (rs != "success") { return(rs); } } else { return(enoughData); } return("success"); } catch { return("failed"); } }