public static sysGlobalClass.CResutlWebMethod SaveToDB(CSave_Data item, List <dataFileContactUs> objFile)
    {
        sysGlobalClass.CResutlWebMethod result = new sysGlobalClass.CResutlWebMethod();
        PTTGC_EPIEntities db  = new PTTGC_EPIEntities();
        DateTime          now = DateTime.Now;

        if (UserAcc.UserExpired())
        {
            result.Status = SystemFunction.process_SessionExpired;
        }
        else
        {
            UserAcc ua = UserAcc.GetObjUser();
            try
            {
                string     sPathSend = "";
                int        nID       = db.TContactUs.Any() ? db.TContactUs.Max(m => m.nContactID) + 1 : 1;
                TContactUs t         = new TContactUs();
                t.nContactID    = nID;
                t.sContactName  = item.sName;
                t.sContactEmail = item.sEmail;
                t.sContactTel   = item.sTel;
                t.sSubject      = item.sSubject;
                t.sDetail       = item.sDec;
                t.dCreate       = now;
                t.dUpdate       = now;
                t.cDel          = "N";
                if (objFile.Count > 0 && objFile != null)
                {
                    string sPathSave = string.Format(sFolderInPathSave, nID);
                    SystemFunction.CreateDirectory(sPathSave);
                    //string sPathSave = string.Format(sFolderInPathSave, nID);
                    //SystemFunction.CreateDirectory(sPathSave);
                    var    f = objFile.First();
                    string sSystemFileName = nID + "_" + "-" + DateTime.Now.ToString("ddMMyyyyHHmmss") + "." + SystemFunction.GetFileNameFromFileupload(f.sFileName, ""); //SystemFunction2.GetFileType(item.SaveToFileName);
                    SystemFunction.UpFile2Server(f.sPath, sPathSave, f.sSysFileName, sSystemFileName);

                    t.sContactFile    = f.sFileName;
                    t.sContactSysFile = sSystemFileName;
                    t.sContactPath    = sPathSave;
                    sPathSend         = HttpContext.Current.Server.MapPath("./") + sPathSave + sSystemFileName;
                }
                //t.cStatusAns = "NULL";
                db.TContactUs.Add(t);
                var Ispass = db.SaveChanges() > 0;

                if (Ispass)
                {
                    string From    = "";
                    var    lstUser = db.mTUser.FirstOrDefault(w => w.ID == ua.nUserID);
                    if (lstUser != null)
                    {
                        From = lstUser.Email;
                    }

                    // string From = item.sEmail;
                    #region Get_Email_Admin
                    string AdminContactmail = "";
                    var    lst_Email_Admin  = db.TMenu_Permission.Where(w => w.nRoleID == 1 && w.nMenuID == 65 && w.nPermission == 2).ToList();
                    if (lst_Email_Admin.Any())
                    {
                        lst_Email_Admin.ForEach(f =>
                        {
                            int nUserID   = f.nUserID;
                            var lst_Tuser = db.mTUser.FirstOrDefault(w => w.cActive == "Y" && w.ID == nUserID);
                            if (lst_Tuser != null)
                            {
                                AdminContactmail += "," + lst_Tuser.Email + " ";
                            }
                        });
                        AdminContactmail = AdminContactmail.Remove(0, 1);
                    }
                    else
                    {
                        AdminContactmail = WebConfigurationManager.AppSettings["ContactMail"] + "";
                    }
                    #endregion



                    string To       = "*****@*****.**";
                    string sSubject = SystemFunction.sAbbrSystem + " : Contact Us." + item.sSubject;
                    string sGurl    = SystemFunction.RequestUrl() + WebConfigurationManager.AppSettings["DefaultPage"] + "";
                    string sHtml    = @"
                    <table align='left'  border='0' cellspacing='2' cellpadding='3'>

                        <tr>
                            <td align='left'>Dear administrator: " + SystemFunction.SystemName + @" </td>
                        </tr>
                         <tr>
                           <td align='left'>Subject: " + item.sSubject + @"</td>
                        </tr>
                         <tr>
                           <td align='left'>Description: " + item.sDec + @"</td>
                        </tr>
                         <tr>
                           <td align='left'>From: " + item.sName + " E-mail: " + item.sEmail + " Contact phone number: " + item.sTel + @"</td>
                        </tr>
                        <tr>
                            <td align='left'><b>Check out more details.</b>: <a href='" + sGurl + @"' target='_blank'>Please click here</a></td>
                        </tr>
                       
                    </table>";
                    //sPathSend != "" ? sPathSend.Replace("/", "\\") : ""
                    Workflow.DataMail_log log = new Workflow.DataMail_log();
                    log           = SystemFunction.SendMailAll(From, AdminContactmail, "", "", sSubject, sHtml, sPathSend != "" ? sPathSend.Replace("/", "\\") : "");
                    log.nDataID   = SystemFunction.GetIntNullToZero(nID + "");
                    log.sPageName = "f_ContactUs.aspx";
                    new Workflow().SaveLogMail(log);
                    if (log.bStatus)
                    {
                        result.Status = SystemFunction.process_Success;
                    }
                }
            }
            catch (Exception e)
            {
                result.Msg    = e.Message;
                result.Status = SystemFunction.process_Failed;
            }

            result.Status = SystemFunction.process_Success;
        }

        return(result);
    }
    public static sysGlobalClass.TRetunrLoadData SaveData(IntensityClass it)
    {
        sysGlobalClass.TRetunrLoadData result = new sysGlobalClass.TRetunrLoadData();
        string sStatus     = it.rt.nStatusWF + "";
        string sStatusTemp = sStatus;
        int    nFormID     = 0;

        try
        {
            if (!UserAcc.UserExpired())
            {
                if (it.rt.lstIn != null)
                {
                    PTTGC_EPIEntities db = new PTTGC_EPIEntities();

                    var ef = db.TEPI_Forms.FirstOrDefault(w => w.IDIndicator == it.nIndicator && w.sYear == it.sYear && w.OperationTypeID == it.nOperationType && w.FacilityID == it.nFacility);

                    if (ef == null)
                    {
                        nFormID            = (db.TEPI_Forms.Any() ? db.TEPI_Forms.Max(m => m.FormID) : 0) + 1;
                        ef                 = new TEPI_Forms();
                        ef.FormID          = nFormID;
                        ef.sAddBy          = UserAcc.GetObjUser().nUserID;
                        ef.dAddDate        = DateTime.Now;
                        ef.sYear           = it.sYear;
                        ef.IDIndicator     = it.nIndicator;
                        ef.OperationTypeID = it.nOperationType;
                        ef.FacilityID      = it.nFacility;
                        db.TEPI_Forms.Add(ef);
                    }
                    else
                    {
                        nFormID = ef.FormID;
                    }

                    if (!SystemFunction.IsSuperAdmin())
                    {
                        ef.ResponsiblePerson = UserAcc.GetObjUser().nUserID;
                        ef.sUpdateBy         = UserAcc.GetObjUser().nUserID;
                        ef.dUpdateDate       = DateTime.Now;
                    }

                    var ot        = db.TIntensity_Other.Where(w => w.FormID == nFormID).ToList();
                    int nReportID = (db.TEPI_Workflow.Any() ? db.TEPI_Workflow.Max(m => m.nReportID) : 0) + 1;


                    if (sStatus == "24")
                    {
                        sStatus = "0";
                    }
                    List <string> lstMonthAdd = new List <string>();
                    for (int i = 1; i <= 12; i++)
                    {
                        lstMonthAdd.Add(i + "");
                    }
                    foreach (string i in lstMonthAdd)
                    {
                        int nM        = SystemFunction.GetIntNullToZero(i);
                        var ef_wf     = db.TEPI_Workflow.FirstOrDefault(w => w.FormID == nFormID && w.nMonth == nM);
                        int?nStatus   = 0;
                        int nStatusID = SystemFunction.GetIntNullToZero(sStatus);
                        if (ef_wf == null)
                        {
                            ef_wf           = new TEPI_Workflow();
                            ef_wf.nReportID = nReportID;
                            ef_wf.FormID    = nFormID;
                            ef_wf.nMonth    = nM;
                            db.TEPI_Workflow.Add(ef_wf);

                            if (!SystemFunction.IsSuperAdmin())
                            {
                                ef_wf.nStatusID = nStatus;
                                ef_wf.nActionBy = UserAcc.GetObjUser().nUserID;
                                ef_wf.dAction   = DateTime.Now;
                            }
                            nReportID++;
                        }
                    }
                    if (sStatusTemp != "24" && sStatusTemp != "2")
                    {
                        db.TIntensity_Other.RemoveRange(ot);
                        db.SaveChanges();
                        it.rt.lstIn.ForEach(f =>
                        {
                            var td       = db.TIntensityDominator.FirstOrDefault(w => w.FormID == nFormID && w.ProductID == f.ProductID);
                            int nVersion = (db.TIntensity_Remark.Any(w => w.FormID == nFormID && w.ProductID == f.ProductID) ? db.TIntensity_Remark.Where(w => w.FormID == nFormID && w.ProductID == f.ProductID).Max(m => m.nVersion) : 0) + 1;
                            if (td == null)
                            {
                                td           = new TIntensityDominator();
                                td.ProductID = f.ProductID;
                                td.FormID    = nFormID;
                                db.TIntensityDominator.Add(td);
                            }
                            td.UnitID = 0;
                            td.M1     = f.M1;
                            td.M2     = f.M2;
                            td.M3     = f.M3;
                            td.M4     = f.M4;
                            td.M5     = f.M5;
                            td.M6     = f.M6;
                            td.M7     = f.M7;
                            td.M8     = f.M8;
                            td.M9     = f.M9;
                            td.M10    = f.M10;
                            td.M11    = f.M11;
                            td.M12    = f.M12;
                            td.UnitID = f.UnitID;
                            if (!string.IsNullOrEmpty(f.nTotal))
                            {
                                td.nTotal = f.nTotal;
                            }
                            td.Target = f.Target;

                            var rm       = new TIntensity_Remark();
                            rm.FormID    = nFormID;
                            rm.ProductID = f.ProductID;
                            rm.nVersion  = nVersion;
                            rm.sRemark   = f.sRemark;
                            if (!SystemFunction.IsSuperAdmin())
                            {
                                rm.dAddDate = DateTime.Now;
                                rm.sAddBy   = UserAcc.GetObjUser().nUserID;
                            }
                            int nUnit = f.UnitID;
                            db.TIntensity_Remark.Add(rm);

                            f.lstarrDetail.Where(w => w.IsActive == true).ToList().ForEach(f2 =>
                            {
                                var to = db.TIntensity_Other.Where(w => w.FormID == nFormID && w.ProductID == f2.nProductID && w.UnderProductID == f2.UnderProductID).FirstOrDefault();
                                if (to == null)
                                {
                                    to                = new TIntensity_Other();
                                    to.ProductID      = f2.nProductID;
                                    to.FormID         = nFormID;
                                    to.UnderProductID = f2.UnderProductID;
                                    db.TIntensity_Other.Add(to);
                                }
                                to.UnitID      = nUnit;
                                to.UnitID      = 0;
                                to.M1          = f2.M1;
                                to.M2          = f2.M2;
                                to.M3          = f2.M3;
                                to.M4          = f2.M4;
                                to.M5          = f2.M5;
                                to.M6          = f2.M6;
                                to.M7          = f2.M7;
                                to.M8          = f2.M8;
                                to.M9          = f2.M9;
                                to.M10         = f2.M10;
                                to.M11         = f2.M11;
                                to.M12         = f2.M12;
                                to.Target      = f2.sTarget;
                                to.ProductName = f2.sIndicator;
                            });
                        });

                        #region SAVE FILE
                        if (it.rt.lstDataFile.Count > 0)
                        {
                            string sPathSave = string.Format(it.sFolderInPathSave, nFormID);
                            SystemFunction.CreateDirectory(sPathSave);

                            //ลบไฟล์เดิมที่เคยมีและกดลบจากหน้าเว็บ
                            var qDelFile = it.rt.lstDataFile.Where(w => w.IsNewFile == false && w.sDelete == "Y").ToList();
                            if (qDelFile.Any())
                            {
                                foreach (var qf in qDelFile)
                                {
                                    var query = db.TEPI_Forms_File.FirstOrDefault(w => w.FormID == nFormID && w.sSysFileName == qf.SaveToFileName);
                                    if (query != null)
                                    {
                                        new SystemFunction().DeleteFileInServer(query.sPath, query.sSysFileName);
                                        // new SystemFunction2().DeleteFileInServer(query.sSysPath, query.sSysFileName);
                                        db.TEPI_Forms_File.Remove(query);
                                        ///db.TAuditPlan_AttachFile.Remove(query);
                                    }
                                }
                                db.SaveChanges();
                            }

                            //Save New File Only
                            var lstSave = it.rt.lstDataFile.Where(w => w.sDelete == "N").ToList();

                            if (lstSave.Any())
                            {
                                int nFileID = db.TEPI_Forms_File.Where(w => w.FormID == nFormID).Any() ? db.TEPI_Forms_File.Where(w => w.FormID == nFormID).Max(m => m.nFileID) + 1 : 1;
                                lstSave.ForEach(f =>
                                {
                                    string sSystemFileName = "";
                                    if (f.IsNewFile == true)
                                    {
                                        sSystemFileName = nFormID + "_" + nFileID + "-" + DateTime.Now.ToString("ddMMyyyyHHmmss") + "." + SystemFunction.GetFileNameFromFileupload(f.SaveToFileName, "");
                                        SystemFunction.UpFile2Server(f.SaveToPath, sPathSave, f.SaveToFileName, sSystemFileName);
                                        f.ID = nFileID;
                                    }
                                    var t = db.TEPI_Forms_File.FirstOrDefault(w => w.nFileID == f.ID && w.FormID == nFormID);
                                    if (t == null)
                                    {
                                        t              = new TEPI_Forms_File();
                                        t.FormID       = nFormID;
                                        t.nFileID      = nFileID;
                                        t.sSysFileName = sSystemFileName;
                                        t.sFileName    = f.FileName;
                                        t.sPath        = sPathSave;
                                        db.TEPI_Forms_File.Add(t);
                                        nFileID++;
                                    }
                                    t.sDescription = f.sDescription;
                                    if (!SystemFunction.IsSuperAdmin())
                                    {
                                        t.dAdd   = DateTime.Now;
                                        t.nAddBy = UserAcc.GetObjUser().nUserID;
                                    }
                                });
                            }
                        }

                        #endregion
                    }

                    db.SaveChanges();

                    new EPIFunc().RecalculateMaterial(it.nOperationType, it.nFacility, it.sYear);
                    new EPIFunc().RecalculateWaste(it.nOperationType, it.nFacility, it.sYear);
                    new EPIFunc().RecalculateWater(it.nOperationType, it.nFacility, it.sYear);
                }

                if (sStatusTemp != "27")
                {
                    new Workflow().UpdateHistoryStatus(nFormID);
                }

                int nStatusTemp = SystemFunction.GetIntNullToZero(sStatusTemp);
                if (nStatusTemp != 0)
                {
                    string sMode = "";
                    switch (nStatusTemp)
                    {
                    case 1:
                        sMode = "SM";
                        break;

                    case 2:
                        sMode = "RQ";
                        break;

                    case 24:
                        sMode = "RC";
                        break;

                    case 27:
                        sMode = "APC";
                        break;
                    }
                    if (!string.IsNullOrEmpty(sMode))
                    {
                        var wf = new Workflow().WorkFlowAction(nFormID, it.lstMonth, sMode, UserAcc.GetObjUser().nUserID, UserAcc.GetObjUser().nRoleID, it.rt.sComment);
                        result.sStatus = wf.Status;
                        result.sMsg    = wf.Msg;
                    }
                    else
                    {
                        if (UserAcc.GetObjUser().nRoleID == 4)//ENVI Corporate (L2) >> Req.09.04.2019 Send email to L0 on L2 Modified data.
                        {
                            new Workflow().SendEmailToL0onL2EditData(it.sYear, it.nIndicator, it.nFacility, it.nOperationType);
                        }

                        result.sStatus = SystemFunction.process_Success;
                        result.sMsg    = "";
                    }
                }
                else
                {
                    result.sStatus = SystemFunction.process_Success;
                    result.sMsg    = "";
                }
            }
            else
            {
                result.sStatus = SystemFunction.process_SessionExpired;
                result.sMsg    = "Session Expired !!";
            }
        }
        catch (Exception ex)
        {
            result.sStatus = SystemFunction.process_Failed;
            result.sMsg    = ex.ToString();
        }
        string sPath = "#";
        switch (it.nOperationType)
        {
        case 4:
            sPath = "epi_input_intensity_1.aspx";
            ssTRetunrLoadData_1 = GetLoadData(it);
            break;

        case 11:
            sPath = "epi_input_intensity_2.aspx";
            ssTRetunrLoadData_2 = GetLoadData(it);
            break;

        case 13:
            sPath = "epi_input_intensity_3.aspx";
            ssTRetunrLoadData_3 = GetLoadData(it);
            break;

        case 14:
            sPath = "epi_input_intensity_4.aspx";
            ssTRetunrLoadData_4 = GetLoadData(it);
            break;
        }
        if (sPath != "#")
        {
            sPath = sPath + "?in=" + HttpUtility.UrlEncode(STCrypt.Encrypt(it.nIndicator + "")) + "&&ot=" + HttpUtility.UrlEncode(STCrypt.Encrypt(it.nOperationType + ""));
        }
        result.sPath = sPath;

        return(result);
    }
    public static sysGlobalClass.CResutlWebMethod SaveToDB(cDataSave arrData)
    {
        sysGlobalClass.CResutlWebMethod result = new sysGlobalClass.CResutlWebMethod();
        PTTGC_EPIEntities            db        = new PTTGC_EPIEntities();
        Func <List <string>, string> Sum       = (lstData) =>
        {
            string sReusltData = "";
            int    Total       = 0;
            foreach (var item in lstData)
            {
                if (item.Trim() != "" && item.ToLower().Trim() != "n/a")
                {
                    Total += int.Parse(item);
                }
            }
            return(sReusltData);
        };

        if (!UserAcc.UserExpired())
        {
            var  itemEPI_FORM = db.TEPI_Forms.FirstOrDefault(w => w.sYear == arrData.sYear && w.IDIndicator == arrData.nIndicatorID && w.OperationTypeID == arrData.nOperationID && w.FacilityID == arrData.nFacilityID);
            bool IsNew        = itemEPI_FORM != null ? false : true;
            int  FORM_ID      = itemEPI_FORM != null ? itemEPI_FORM.FormID : (db.TEPI_Forms.Any() ? db.TEPI_Forms.Max(m => m.FormID) + 1 : 1);

            #region EPI_FORM
            var EPI_FORM = db.TEPI_Forms.FirstOrDefault(w => w.FormID == FORM_ID);
            if (IsNew)
            {
                EPI_FORM                   = new TEPI_Forms();
                EPI_FORM.FormID            = FORM_ID;
                EPI_FORM.sYear             = arrData.sYear;
                EPI_FORM.IDIndicator       = arrData.nIndicatorID;
                EPI_FORM.OperationTypeID   = arrData.nOperationID;
                EPI_FORM.FacilityID        = arrData.nFacilityID;
                EPI_FORM.sAddBy            = UserAcc.GetObjUser().nUserID;
                EPI_FORM.dAddDate          = DateTime.Now;
                EPI_FORM.ResponsiblePerson = UserAcc.GetObjUser().nUserID;
                EPI_FORM.sUpdateBy         = UserAcc.GetObjUser().nUserID;
                EPI_FORM.dUpdateDate       = DateTime.Now;
                db.TEPI_Forms.Add(EPI_FORM);
            }
            else
            {
                if (!SystemFunction.IsSuperAdmin())
                {
                    EPI_FORM.ResponsiblePerson = UserAcc.GetObjUser().nUserID;
                    EPI_FORM.sUpdateBy         = UserAcc.GetObjUser().nUserID;
                    EPI_FORM.dUpdateDate       = DateTime.Now;
                }
            }
            db.SaveChanges();
            #endregion

            #region EPI Workflow
            int nWkFlowID = db.TEPI_Workflow.Any() ? db.TEPI_Workflow.Max(m => m.nReportID) + 1 : 1;
            for (int i = 1; i <= 12; i++)
            {
                int nStatus = 0;
                if (arrData.nStatus != 0)
                {
                    var itemData = arrData.lstMonthSubmit.FirstOrDefault(a => a == i);
                    if (itemData != null && itemData != 0)
                    {
                        nStatus = arrData.nStatus;
                    }
                }
                var wkflow = db.TEPI_Workflow.FirstOrDefault(w => w.FormID == FORM_ID && w.nMonth == i);
                if (wkflow == null)
                {
                    wkflow           = new TEPI_Workflow();
                    wkflow.nReportID = nWkFlowID;
                    wkflow.FormID    = FORM_ID;
                    wkflow.nMonth    = i;
                    wkflow.nStatusID = nStatus;
                    wkflow.nActionBy = UserAcc.GetObjUser().nUserID;
                    wkflow.dAction   = DateTime.Now;
                    db.TEPI_Workflow.Add(wkflow);
                    nWkFlowID++;
                }
            }
            db.SaveChanges();
            #endregion

            #region Product
            if (arrData.nStatus != 24)
            {
                if (!IsNew)
                {
                    db.TComplaint_Product.RemoveRange(db.TComplaint_Product.Where(w => w.FormID == FORM_ID));
                    db.TComplaint.RemoveRange(db.TComplaint.Where(w => w.FormID == FORM_ID));
                    db.TComplaint_Impact.RemoveRange(db.TComplaint_Impact.Where(w => w.FormID == FORM_ID));
                    db.SaveChanges();
                }

                #region TComplaint_Product
                TComplaint_Product dataComplaintPrd = new TComplaint_Product();
                dataComplaintPrd.FormID    = FORM_ID;
                dataComplaintPrd.ProductID = arrData.incData.ProductID;
                dataComplaintPrd.UnitID    = arrData.incData.nUnitID;
                dataComplaintPrd.Target    = arrData.incData.sTarget;
                dataComplaintPrd.M1        = arrData.incData.M1;
                dataComplaintPrd.M2        = arrData.incData.M2;
                dataComplaintPrd.M3        = arrData.incData.M3;
                dataComplaintPrd.M4        = arrData.incData.M4;
                dataComplaintPrd.M5        = arrData.incData.M5;
                dataComplaintPrd.M6        = arrData.incData.M6;
                dataComplaintPrd.M7        = arrData.incData.M7;
                dataComplaintPrd.M8        = arrData.incData.M8;
                dataComplaintPrd.M9        = arrData.incData.M9;
                dataComplaintPrd.M10       = arrData.incData.M10;
                dataComplaintPrd.M11       = arrData.incData.M11;
                dataComplaintPrd.M12       = arrData.incData.M12;

                dataComplaintPrd.IsCheckM1  = arrData.incData.IsCheckM1;
                dataComplaintPrd.IsCheckM2  = arrData.incData.IsCheckM2;
                dataComplaintPrd.IsCheckM3  = arrData.incData.IsCheckM3;
                dataComplaintPrd.IsCheckM4  = arrData.incData.IsCheckM4;
                dataComplaintPrd.IsCheckM5  = arrData.incData.IsCheckM5;
                dataComplaintPrd.IsCheckM6  = arrData.incData.IsCheckM6;
                dataComplaintPrd.IsCheckM7  = arrData.incData.IsCheckM7;
                dataComplaintPrd.IsCheckM8  = arrData.incData.IsCheckM8;
                dataComplaintPrd.IsCheckM9  = arrData.incData.IsCheckM9;
                dataComplaintPrd.IsCheckM10 = arrData.incData.IsCheckM10;
                dataComplaintPrd.IsCheckM11 = arrData.incData.IsCheckM11;
                dataComplaintPrd.IsCheckM12 = arrData.incData.IsCheckM12;

                List <string> lstForSum = new List <string> {
                    arrData.incData.M1, arrData.incData.M2, arrData.incData.M3, arrData.incData.M4, arrData.incData.M5,
                    arrData.incData.M6, arrData.incData.M7, arrData.incData.M8, arrData.incData.M9, arrData.incData.M10, arrData.incData.M11, arrData.incData.M12
                };
                dataComplaintPrd.nTotal = Sum(lstForSum);
                db.TComplaint_Product.Add(dataComplaintPrd);
                db.SaveChanges();
                #endregion

                #region TComplaint
                int nComplaint = 1;
                arrData.lstComplaint.ForEach(f =>
                {
                    if (f.IsShow && f.IsSubmited)
                    {
                        TComplaint data        = new TComplaint();
                        data.FormID            = FORM_ID;
                        data.nComplaintID      = nComplaint;
                        data.nComplaintTypeID  = f.nComplaintTypeID;
                        data.ComplaintDate     = DateTime.ParseExact(f.sIssueDate, "dd/MM/yyyy", null);
                        data.sIssueBy          = f.sIssueBy;
                        data.sSubject          = f.sSubject;
                        data.sDetail           = f.sDetail;
                        data.sCorrectiveAction = f.sCorrectiveAction;
                        data.nComplaintByID    = f.nComplaintByID;
                        data.sComplaintByOther = f.sComplaintByOther;
                        data.sStatus           = f.sStatus;
                        data.nAddBy            = UserAcc.GetObjUser().nUserID;
                        data.dAdd = DateTime.Now;
                        db.TComplaint.Add(data);

                        f.lstImpact.ForEach(f2 =>
                        {
                            TComplaint_Impact dataImpact = new TComplaint_Impact();
                            dataImpact.FormID            = FORM_ID;
                            dataImpact.nComplaintID      = nComplaint;
                            dataImpact.nImpactTypeID     = f2.nImpactTypeID;
                            dataImpact.sOther            = f2.sOther;
                            db.TComplaint_Impact.Add(dataImpact);
                        });
                        db.SaveChanges();

                        #region TComplaint_File
                        if (f.lstFile.Any())
                        {
                            string sPathSave = string.Format(sFolderInPathSave, FORM_ID);
                            SystemFunction.CreateDirectory(sPathSave);

                            //ลบไฟล์เดิมที่เคยมีและกดลบจากหน้าเว็บ
                            var qDelFile = f.lstFile.Where(w => w.IsNewFile == false && w.sDelete == "Y").ToList();
                            if (qDelFile.Any())
                            {
                                foreach (var qf in qDelFile)
                                {
                                    var query = db.TComplaint_File.FirstOrDefault(w => w.FormID == FORM_ID && w.nComplaintID == f.nComplaintID && w.sSysFileName == qf.SaveToFileName);
                                    if (query != null)
                                    {
                                        new SystemFunction().DeleteFileInServer(query.sPath, query.sSysFileName);
                                        // new SystemFunction2().DeleteFileInServer(query.sSysPath, query.sSysFileName);
                                        db.TComplaint_File.Remove(query);
                                        ///db.TAuditPlan_AttachFile.Remove(query);
                                    }
                                }
                                db.SaveChanges();
                            }
                            //Update Description
                            f.lstFile.Where(w => w.IsNewFile == false && w.sDelete == "N").ToList().ForEach(f2U =>
                            {
                                var data2Update = db.TComplaint_File.FirstOrDefault(w => w.FormID == FORM_ID && w.nComplaintID == f.nComplaintID && w.nFileID == f2U.ID);
                                if (data2Update != null)
                                {
                                    data2Update.sDescription = f2U.sDescription;
                                }
                            });

                            //Save New File Only
                            var lstSave = f.lstFile.Where(w => w.IsNewFile == true && w.sDelete == "N").ToList();
                            if (lstSave.Any())
                            {
                                int nFileID = db.TComplaint_File.Where(w => w.FormID == FORM_ID && w.nComplaintID == f.nComplaintID).Any() ? db.TComplaint_File.Where(w => w.FormID == FORM_ID && w.nComplaintID == f.nComplaintID).Max(m => m.nFileID) + 1 : 1;

                                foreach (var s in lstSave)
                                {
                                    string sSystemFileName = FORM_ID + "_" + f.nComplaintID + "_" + nFileID + "-" + DateTime.Now.ToString("ddMMyyyyHHmmss") + "." + SystemFunction.GetFileNameFromFileupload(s.SaveToFileName, ""); //+ "." + SystemFunction.GetFileNameFromFileupload(s.SaveToFileName, "") SystemFunction2.GetFileType(item.SaveToFileName);
                                    SystemFunction.UpFile2Server(s.SaveToPath, sPathSave, s.SaveToFileName, sSystemFileName);

                                    // SystemFunction2.UpFile2Server(item.SaveToPath, sPathSave, item.SaveToFileName, sSystemFileName);
                                    TComplaint_File t = new TComplaint_File();
                                    t.FormID          = FORM_ID;
                                    t.nComplaintID    = f.nComplaintID;
                                    t.nFileID         = nFileID;
                                    t.sSysFileName    = sSystemFileName;
                                    t.sFileName       = s.FileName;
                                    t.sPath           = sPathSave;
                                    t.sDescription    = s.sDescription;
                                    db.TComplaint_File.Add(t);
                                    nFileID++;
                                }
                            }
                            db.SaveChanges();
                        }
                        #endregion

                        nComplaint++;
                    }
                });
                #endregion

                new EPIFunc().RecalculateComplaint(arrData.nOperationID, arrData.nFacilityID, arrData.sYear);
            }
            #endregion

            if (arrData.nStatus != 27)
            {
                new Workflow().UpdateHistoryStatus(FORM_ID);
            }

            if (arrData.nStatus != 0 && arrData.nStatus != 9999)
            {
                string sMode = "";
                switch (arrData.nStatus)
                {
                case 1:
                    sMode = "SM";
                    break;

                case 2:
                    sMode = "RQ";
                    break;

                case 24:
                    sMode = "RC";
                    break;

                case 27:
                    sMode = "APC";
                    break;
                }
                result = new Workflow().WorkFlowAction(FORM_ID, arrData.lstMonthSubmit, sMode, UserAcc.GetObjUser().nUserID, UserAcc.GetObjUser().nRoleID, arrData.sRemarkRequestEdit);
            }
            else
            {
                if (arrData.nStatus != 0)
                {
                    if (UserAcc.GetObjUser().nRoleID == 4)//ENVI Corporate (L2) >> Req.09.04.2019 Send email to L0 on L2 Modified data.
                    {
                        new Workflow().SendEmailToL0onL2EditData(arrData.sYear, arrData.nIndicatorID, arrData.nFacilityID, arrData.nOperationID);
                    }
                }
                result.Status = SystemFunction.process_Success;
            }
        }
        else
        {
            result.Status = SystemFunction.process_SessionExpired;
        }
        return(result);
    }
    public static sysGlobalClass.CResutlWebMethod SaveToDB(string sDesc, string sID, List <dataFileContactUs> objFile)
    {
        sysGlobalClass.CResutlWebMethod result = new sysGlobalClass.CResutlWebMethod();
        PTTGC_EPIEntities db  = new PTTGC_EPIEntities();
        DateTime          now = DateTime.Now;

        if (UserAcc.UserExpired())
        {
            result.Status = SystemFunction.process_SessionExpired;
        }
        else
        {
            try
            {
                UserAcc ua = UserAcc.GetObjUser();
                if (!string.IsNullOrEmpty(sID))
                {
                    string sPathSend = "";
                    int    nID       = int.Parse(STCrypt.Decrypt(sID));
                    var    Query     = db.TContactUs.FirstOrDefault(w => w.cDel == "N" && w.nContactID == nID);
                    if (Query != null)
                    {
                        Query.sAnswer    = sDesc;
                        Query.cStatusAns = "2";
                        Query.dAnswer    = now;
                        Query.dUpdate    = now;
                        Query.nAnswerBy  = UserAcc.GetObjUser().nUserID;

                        if (objFile.Count > 0 && objFile != null)
                        {
                            string sPathSave = string.Format(sFolderInPathSave, nID);
                            SystemFunction.CreateDirectory(sPathSave);
                            //string sPathSave = string.Format(sFolderInPathSave, nID);
                            //SystemFunction.CreateDirectory(sPathSave);
                            var    f = objFile.First();
                            string sSystemFileName = nID + "_" + "-" + DateTime.Now.ToString("ddMMyyyyHHmmss") + "." + SystemFunction.GetFileNameFromFileupload(f.sFileName, ""); //SystemFunction2.GetFileType(item.SaveToFileName);
                            SystemFunction.UpFile2Server(f.sPath, sPathSave, f.sSysFileName, sSystemFileName);

                            Query.sAnsFile    = f.sFileName;
                            Query.sAnsSysFile = sSystemFileName;
                            Query.sAnsPath    = sPathSave;
                            sPathSend         = HttpContext.Current.Server.MapPath("./") + sPathSave + sSystemFileName;
                        }

                        var Ispass = db.SaveChanges() > 0;
                        if (Ispass)
                        {
                            string To = Query.sContactEmail;                                                   // Contact Mail
                            string AdminContactmail = WebConfigurationManager.AppSettings["ContactMail"] + ""; // Mail Admin
                            //string To1 = "*****@*****.**";

                            string sSubject           = SystemFunction.sAbbrSystem + " : Contact Us." + Query.sSubject;
                            string sGurl              = SystemFunction.RequestUrl() + WebConfigurationManager.AppSettings["DefaultPage"] + "";
                            string sHtml              = @"
                                    <table align='left'  border='0' cellspacing='2' cellpadding='3'>

                                        <tr>
                                            <td align='left'>Dear: " + Query.sContactName + @"</td>
                                        </tr>
                                         <tr>
                                           <td align='left'>Subject: " + Query.sSubject + @"</td>
                                        </tr>
                                         <tr>
                                           <td align='left'>Description: " + Query.sDetail + @"</td>
                                        </tr>
                                        <tr>
                                           <td align='left'>Answer: " + Query.sAnswer + @"</td>
                                        </tr>

                                    </table>";
                            Workflow.DataMail_log log = new Workflow.DataMail_log();

                            string ccMail  = "";
                            var    lstUser = db.mTUser.FirstOrDefault(w => w.ID == ua.nUserID);
                            if (lstUser != null)
                            {
                                ccMail = lstUser.Email;
                            }

                            log           = SystemFunction.SendMailAll(AdminContactmail, To, ccMail, "", sSubject, sHtml, sPathSend != "" ? sPathSend.Replace("/", "\\") : "");
                            log.nDataID   = SystemFunction.GetIntNullToZero(nID + "");
                            log.sPageName = "admin_ContactUs_update.aspx";
                            new Workflow().SaveLogMail(log);
                            if (log.bStatus)
                            {
                                result.Status = SystemFunction.process_Success;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                result.Msg    = e.Message;
                result.Status = SystemFunction.process_Failed;
            }

            result.Status = SystemFunction.process_Success;
        }

        return(result);
    }