public async Task<string> UptGradItem(UptGradItemParm param) { var db = new EnergyNetEntities(); var r = new AjaxResult<IEnumerable<Graid_Detail>>(); try { var item1 = await db.ExamineReport_Master.FindAsync(new object[] { param.CustId, param.YY }); if (item1 == null) { r.message = "無申報完成項目記錄"; r.result = false; } var item2 = await db.ExamineReport_Record .Where(x => x.CustID == param.CustId && x.YY == param.YY).OrderByDescending(x => x.examine_report_record_id).FirstOrDefaultAsync(); if (item2 == null) { r.message = "無申報完成項明細目記錄"; r.result = false; } var examine_report_record_id = item2.examine_report_record_id; var item3 = await db.Grad.Where(x => x.CustId == param.CustId && x.YY == param.YY && x.examine_report_record_id == examine_report_record_id) .OrderByDescending(x => x.grad_id) .FirstOrDefaultAsync(); if (item3 == null) { r.message = "無申評核主檔"; r.result = false; } item3.checker = param.checkerid; item3.check_date = DateTime.Now; await db.SaveChangesAsync(); var master_id = item3.grad_id; var item4 = await db.Graid_Item .Where(x => x.graid_id == master_id && x.repord_id == param.pno && x.item == param.item) .ToListAsync(); if (item4.Count() > 0) { db.Graid_Item.RemoveRange(item4); } foreach (var item in param.md) { var md = new Graid_Item(); md.graid_id = master_id; md.repord_id = param.pno; md.opt_id = item.opt_id; md.item = item.item; md.mem = item.mem; db.Graid_Item.Add(md); } await db.SaveChangesAsync(); r.result = true; } catch (Exception ex) { r.result = false; r.message = getException(ex); throw; } finally { db.Dispose(); } return defJSON(r); }
public async Task<string> GetGradItem(string CustId, int YY, string pno, int item, string checkerid) { var db = new EnergyNetEntities(); var r = new AjaxResult<IEnumerable<Graid_Item>>(); try { #region Code var item1 = await db.ExamineReport_Master.FindAsync(new object[] { CustId, YY }); if (item1 == null) { r.message = "無申報完成項目記錄"; r.result = false; } var item2 = await db.ExamineReport_Record .Where(x => x.CustID == CustId && x.YY == YY).OrderByDescending(x => x.examine_report_record_id).FirstOrDefaultAsync(); if (item2 == null) { r.message = "無申報完成項明細目記錄"; r.result = false; } var examine_report_record_id = item2.examine_report_record_id; var item3 = await db.Grad.Where(x => x.CustId == CustId && x.YY == YY && x.examine_report_record_id == examine_report_record_id) .OrderByDescending(x => x.grad_id) .FirstOrDefaultAsync(); var master_id = 0; if (item3 == null) { var item_prv = await db.Grad.Where(x => x.CustId == CustId && x.YY == YY) .OrderByDescending(x => x.grad_id) .FirstOrDefaultAsync(); var md = new Grad() { CustId = CustId, YY = YY, check_date = DateTime.Now, examine_report_record_id = examine_report_record_id, checker = checkerid }; db.Grad.Add(md); await db.SaveChangesAsync(); master_id = md.grad_id; if (item_prv != null) //copy 上次資料 { var details = db.Graid_Item.Where(x => x.graid_id == item_prv.grad_id); foreach (var detail in details) { var mds = new Graid_Item(); mds.graid_id = master_id; mds.repord_id = detail.repord_id; mds.opt_id = detail.opt_id; mds.mem = detail.mem; mds.item = detail.item; db.Graid_Item.Add(mds); } await db.SaveChangesAsync(); } } else { master_id = item3.grad_id; } #endregion var item4 = await db.Graid_Item.Where(x => x.graid_id == master_id && x.repord_id == pno && x.item == item) .ToListAsync(); r.data = item4; r.result = true; } catch (Exception ex) { r.result = false; r.message = getException(ex); throw; } finally { db.Dispose(); } return defJSON(r); }