using NFine.Application.WMS;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NFine.Code;
using System.Data.SqlClient;
using NFine.Data.Extensions;
using System.Data.OleDb;
using System.Configuration;
using ICS.Application.Entity;

namespace NFine.Web.Areas.WMS.Controllers
{
    public class HomeWorkController : ControllerBase
    {

        HomeWorkApp INVApp = new HomeWorkApp();
        // GET: WMS/HomeWork
        public ActionResult InitialImport()
        {
            return View();
        }
        public ActionResult InitialImportUpLoad()
        {
            return View();
        }

        [HttpPost]
        /// <summary>
        /// 文件上传到本地
        /// </summary>
        public string UploadFile()
        {
            try
            {
                string str_Year = Request.Form["txt_Year"];
                String UPLoadType = Request.Form["UPLoadType"];
                HttpFileCollection hpFiles = System.Web.HttpContext.Current.Request.Files;
                if (hpFiles != null && hpFiles.Count > 0)
                {

                    string IsXls = System.IO.Path.GetExtension(hpFiles[0].FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名
                    if (IsXls != ".xls" && IsXls != ".xlsx")
                    {

                        return "只可以选择Excel(.xls .xlsx)文件";//当选择的不是Excel文件时,返回
                    }
                    string filename = DateTime.Now.ToString("yyyyMMddhhmmss") + UPLoadType + IsXls;             //获取Execle文件名 DateTime日期函数
                    var folder = System.Web.HttpContext.Current.Server.MapPath("~\\File\\UPLoadFile");   //Server.MapPath 获得虚拟服务器相对路径
                    if(!Directory.Exists(folder))
                        Directory.CreateDirectory(folder);
                    string savePath = Path.Combine(folder, filename);



                    int iLen = hpFiles[0].ContentLength;
                    if (Directory.Exists(savePath)) return "文件已存在";
                    byte[] bData = new byte[iLen];
                    hpFiles[0].InputStream.Read(bData, 0, iLen);


                    FileStream newFile = new FileStream(savePath, FileMode.OpenOrCreate);
                    newFile.Write(bData, 0, bData.Length);
                    newFile.Flush();
                    int _FileSizeTemp = hpFiles[0].ContentLength;

                    newFile.Close();
                    newFile.Dispose();
                    //bool del = false;
                    string mess = "";
                    mess = INVApp.SetData_PR(savePath, str_Year);
                    if (System.IO.File.Exists(savePath))//删除文件
                    {

                        System.IO.File.Delete(savePath);
                    }
                    return mess;

                }
                else
                {
                    return "获取文件失败";
                }
            }
            catch (Exception ex)
            {

                return ex.ToString();
            }
        }




    }
}