public JsonResult Insert(MachineTypeFrequency machineTypeFrequency)
 {
     return(Json(machineTypeFrequencyDB.Insert(machineTypeFrequency), JsonRequestBehavior.AllowGet));
 }
Ejemplo n.º 2
0
        public JsonResult ImportFileMachineTypeMtnContent()
        {
            ReturnMachineMtnContentList returnTotal = new ReturnMachineMtnContentList();

            returnTotal.Code = "00";  // thanh cong.

            #region validate
            //store Frequency of MachineType.
            MachineTypeFrequency       machineTypeFrequency_Import = Newtonsoft.Json.JsonConvert.DeserializeObject <MachineTypeFrequency>(Request.Form[0]);
            ReturnMachineTypeFrequency _returnMachineMtnFrequency  = machineTypeFrequencyDB.Insert(machineTypeFrequency_Import);
            if (_returnMachineMtnFrequency.Code == "99")
            {
                returnTotal.Code    = "99";
                returnTotal.Message = _returnMachineMtnFrequency.Message;
                return(Json(returnTotal, JsonRequestBehavior.AllowGet));
            }
            //xu ly file nhap vao.
            string tenfile           = "";
            string fullPath_filename = "";
            string sheetName         = "";

            if (Request.Files.Count == 0)
            {
                returnTotal.Code    = "99";
                returnTotal.Message = "Hãy chọn file/Choose file.";
                return(Json(returnTotal, JsonRequestBehavior.AllowGet));
            }
            #endregion validate.

            #region Upload file to server.
            if (Request.Files.Count > 0)
            {
                try
                {
                    HttpPostedFileBase httpFile = Request.Files[0];

                    //Checking for Internet Explorer
                    if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
                    {
                        string[] testfiles = httpFile.FileName.Split(new char[] { '\\' });
                        fullPath_filename = testfiles[testfiles.Length - 1];
                    }
                    else
                    {
                        fullPath_filename = httpFile.FileName;
                    }
                    string strExtension  = Path.GetExtension(httpFile.FileName);
                    string strPrefixName = Path.GetFileNameWithoutExtension(httpFile.FileName);
                    //rename file.
                    strPrefixName += DateTime.Now.ToString("yyMMddHHmmss");
                    string fullName = strPrefixName + strExtension;

                    // Get the complete folder path and store the file inside it.
                    fullPath_filename = Path.Combine(Server.MapPath("~/Uploads/MachineMaintenance"), fullName);
                    httpFile.SaveAs(fullPath_filename);
                }
                catch (Exception ex)
                {
                    mylog4net.Error("ImportFileMachineTypeMtnContent", ex);

                    if (Request.Files.Count == 0)
                    {
                        returnTotal.Code    = "99";
                        returnTotal.Message = "Lỗi cập nhật file lên server./Failed to upload file to server.";
                        return(Json(returnTotal, JsonRequestBehavior.AllowGet));
                    }
                }
            }
            #endregion Upload file to server.

            #region extract excel to datatable.
            // Khởi tạo data table
            DataTable dtFromExcel;
            dtFromExcel = ExtractExcelToDataTable(fullPath_filename, sheetName);
            #endregion

            #region validate
            if (dtFromExcel == null || (dtFromExcel.Rows.Count == 0))
            {
                returnTotal.Code    = "99";
                returnTotal.Message = "Không có dữ liệu (hoặc lỗi) trong file. Hãy kiểm tra lại/Not exists data in file. Check again!";
                return(Json(returnTotal, JsonRequestBehavior.AllowGet));
            }
            #endregion

            #region import datatable to DB

            MachineMtnContentListDB machineMtnContentListDB = new MachineMtnContentListDB();
            MachineMtnContentList   machineMtnContentList   = new MachineMtnContentList();

            Import_MachineMtnType_ContentList import_aMachineMtnType_ContentList = new Import_MachineMtnType_ContentList();
            import_aMachineMtnType_ContentList.MachineTypeID = machineTypeFrequency_Import.MachineTypeID;
            import_aMachineMtnType_ContentList.FrequencyID   = machineTypeFrequency_Import.FrequencyID;

            ReturnMachineMtnContentList resultImport_aMachineMtnContentList;

            dtFromExcel.Columns.Add(new DataColumn("Status"));
            foreach (DataRow dr in dtFromExcel.Rows)
            {
                try
                {
                    if (dr[0].ToString().Trim() != "" && dr[1].ToString().Trim() != "")
                    {
                        import_aMachineMtnType_ContentList.MachinePart = dr[0].ToString().Trim(); //Item bộ phận bảo dưỡng.
                        import_aMachineMtnType_ContentList.ContentMtn  = dr[1].ToString().Trim(); //Content noi dung bao duong.
                        import_aMachineMtnType_ContentList.ToolMtn     = dr[2].ToString().Trim(); //Tool.
                        import_aMachineMtnType_ContentList.MethodMtn   = dr[3].ToString().Trim(); //Method.
                        import_aMachineMtnType_ContentList.Standard    = dr[4].ToString().Trim(); //Standard.
                        import_aMachineMtnType_ContentList.Min         = dr[5].ToString().Trim(); //Min.
                        import_aMachineMtnType_ContentList.Max         = dr[6].ToString().Trim(); //Max.
                        resultImport_aMachineMtnContentList            = Import_aMachineMtnContent_To_DB(import_aMachineMtnType_ContentList);

                        if (resultImport_aMachineMtnContentList.Code == "00")
                        {
                            dr["Status"] = "Thành công/Success.";
                        }
                        else if (resultImport_aMachineMtnContentList.Code == "99")
                        {
                            dr["Status"] = "Thất bại/Fail.";
                        }
                    }
                    else
                    {
                        dr["Status"] = "Thất bại/Fail.";
                    }
                }
                catch (Exception)
                {
                    dr["Status"] = "Thất bại/Fail.";
                }
            }
            #endregion

            //delete file after export.
            if (System.IO.File.Exists(fullPath_filename))
            {
                System.IO.File.Delete(fullPath_filename);
            }
            return(Json(returnTotal, JsonRequestBehavior.AllowGet));
        }