public async Task<string> UptGradData(UptGradParm 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;
                }
                //item1.CheckDate = DateTime.Now; //最新審核日期

                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_Detail
                    .Where(x => x.graid_id == master_id && x.repord_id == param.pno)
                    .ToListAsync();

                if (item4.Count() > 0)
                {
                    db.Graid_Detail.RemoveRange(item4);
                }

                if (param.md != null)
                {
                    foreach (var item in param.md)
                    {
                        var md = new Graid_Detail();

                        md.graid_id = master_id;
                        md.repord_id = param.pno;
                        md.opt_id = item.opt_id;
                        md.mem = item.mem;
                        db.Graid_Detail.Add(md);
                    }
                }
                await db.SaveChangesAsync();

                r.result = true;
            }
            catch (DbEntityValidationException ex)
            {
                r.result = false;
                r.message = getDbEntityValidationException(ex);
            }
            catch (Exception ex)
            {
                r.result = false;
                r.message = getException(ex);
            }
            finally
            {
                db.Dispose();
            }

            return defJSON(r);
        }
        public async Task<string> GetGradList(string CustId, int YY)
        {
            var db = new EnergyNetEntities();
            var r = new AjaxResult<IList<IGrouping<string, Graid_Detail>>>();
            try
            {
                #region Code 檢查是否有申報完成
                var exam_record = await db.ExamineReport_Record
                    .Where(x => x.CustID == CustId && x.YY == YY)
                    .OrderByDescending(x => x.examine_report_record_id)
                    .FirstOrDefaultAsync();

                if (exam_record == null)
                {
                    r.message = "無申報完成項明細目記錄";
                    r.result = false;
                }

                var examine_report_record_id = exam_record.examine_report_record_id;

                var grad = await db.Grad.Where(x => x.examine_report_record_id == examine_report_record_id)
                    .OrderByDescending(x => x.grad_id)
                    .FirstOrDefaultAsync();

                var master_id = 0;
                if (grad == null)
                {
                    #region Code
                    var item_prv = await db.Grad.Where(x => x.CustId == CustId && x.YY == YY)
                        .OrderByDescending(x => x.grad_id)
                        .FirstOrDefaultAsync();

                    var checkerid = (await db.BuileBase.FindAsync(new object[] { CustId, YY })).CheckerId;

                    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_Detail.Where(x => x.graid_id == item_prv.grad_id);
                        foreach (var detail in details)
                        {
                            var mds = new Graid_Detail();
                            mds.graid_id = master_id;
                            mds.repord_id = detail.repord_id;
                            mds.opt_id = detail.opt_id;
                            mds.mem = detail.mem;
                            db.Graid_Detail.Add(mds);
                        }
                        await db.SaveChangesAsync();
                    }
                    #endregion
                }

                #endregion

                var master = await db.Grad.Where(x => x.examine_report_record_id == examine_report_record_id)
                    .OrderByDescending(x => x.grad_id)
                    .Take(1)
                    .FirstOrDefaultAsync();

                if (master != null)
                {
                    var items = await db.Graid_Detail
                        .Where(x => x.graid_id == master.grad_id)
                        .GroupBy(x => x.repord_id, (key, g) => new { repord_id = key, data = g })
                        .ToListAsync();

                    return defJSONNull(items);
                }
                else {
                    r.result = false;
                    return defJSONNull(null);
                }
            }
            catch (Exception ex)
            {
                r.result = false;
                r.message = getException(ex);
                return defJSONNull(null);
            }
            finally
            {
                db.Dispose();
            }
        }