using System;
using System.Linq;
using System.Web.Mvc;
using DSWeb.Areas.Account.DAL.Chfee_Jinzhang;
using DSWeb.Areas.Account.Models.Chfee_jinzhang;
using DSWeb.MvcShipping.Helper;
using DSWeb.MvcShipping.Comm.Cookie;
using DSWeb.Areas.CommMng.DAL;
using System.Collections.Generic;
using HcUtility.Comm;
using HcUtility.Core;
using DSWeb.EntityDA;
using DSWeb.Areas.Account.Models.Chfee_Exrate;
using System.Data;
using DSWeb.Areas.Account.Models.Chfee_Bankdata;
using DSWeb.Areas.Account.DAL.Chfee_bankdata;
using System.IO;

namespace DSWeb.Areas.Account.Controllers
{
    [JsonRequestBehavior]
    public class Chfee_bankdataController : Controller
    {
        //
        // GET: 
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /
        public ActionResult Edit()
        {
            return View();
        }



        //
        // GET:

        #region 
        public ContentResult GetDataList(int start, int limit, string sort, string condition)
        {
            var dataList = ChBankdataDAL.GetDataList(condition, Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), Convert.ToString(Session["COMPANYID"]), sort);
            var list = dataList.Skip(start).Take(limit);

            var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = list.ToList() });
            return new ContentResult() { Content = json };
        }


        public ContentResult GetData(string handle, string condition)
        {
            ChBankdata head = null;

            if (handle == "edit")
            {
                var list = ChBankdataDAL.GetDataList(condition, Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), Convert.ToString(Session["COMPANYID"]));
                if (list.Count > 0)
                    head = list[0];
            }

            if (head == null)
            {
                head = new ChBankdata();
            }

            if (handle == "add")
            {
                head.CREATEUSER = Convert.ToString(Session["USERID"]);
                head.CREATEUSERREF = Convert.ToString(Session["SHOWNAME"]);
                head.CORPID = Convert.ToString(Session["COMPANYID"]);
            }



            var json = JsonConvert.Serialize(
               new { Success = true, Message = "查询成功", data = head });
            return new ContentResult() { Content = json };
        }



         //


         public ContentResult Delete(string data)
         {
             var headData = JsonConvert.Deserialize<ChBankdata>(data);
             DBResult result;
                     var modb = new ModelObjectDB();
                     result=modb.Delete(headData);


             var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
             return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
         }




        [HttpPost]
        public ContentResult ImpExcel()
        {
            if (Convert.ToString(Session["COMPANYID"]).ToString().Trim() == "" || Convert.ToString(Session["USERID"]).ToString().Trim() == "" || Convert.ToString(Session["CODENAME"]).ToString().Trim() == "" || Convert.ToString(Session["SHOWNAME"]).ToString().Trim() == "" || Convert.ToString(Session["DEPTNAME"]).ToString().Trim() == "")
            {
                var jsonRespose2 = new JsonResponse { Success = false, Message = "登录超时,请退出系统重新登录!" };
                return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose2) };
            }
            var jsonRespose = new JsonResponse { Success = false, Message = "" };

            if (Request.Files.Count != 1)
            {
                jsonRespose.Success = false;
                jsonRespose.Message = "请选择上传的文件";
                return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
            }

            var file = Request.Files["DsLoadExcel"];

            if (file == null)
            {
                jsonRespose.Success = false;
                jsonRespose.Message = "上传文件发生未知错误,请重新上传";
                return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
            }

            var path = Server.MapPath("../../UploadFiles/ImpBsExcel");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            var imptype = Request.Form["imptype"];
            var size = file.ContentLength;
            var name = Path.GetFileName(file.FileName);

            var usercode = CookieConfig.GetCookie_UserCode(Request);
            string filename = path + "\\" + usercode + DateTime.Now.ToString("yyyyMMddHHmmssfff") + name;

            if (System.IO.File.Exists(filename))
            {
                System.IO.File.Delete(filename);
            }
            file.SaveAs(filename);

            if (!System.IO.File.Exists(filename))
            {
                jsonRespose.Success = false;
                jsonRespose.Message = "上传的Excel不包含数据01";
                return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
            }


            try
            {
                string ext = Path.GetExtension(file.FileName).ToLower();
                if (ext != ".xls" && ext != ".xlsx" && ext != ".csv")
                {
                    jsonRespose.Success = false;
                    jsonRespose.Message = "上传的文件不是Excel文件";
                    return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
                }
                var strMsg = "";
                System.Data.DataTable table = ChBankdataDAL.CSV2DataTable(filename, out strMsg);



                if (!string.IsNullOrEmpty(strMsg))
                {
                    jsonRespose.Success = false;
                    jsonRespose.Message = strMsg;
                    return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
                }

                if (table.Rows.Count > 0)
                {
                    ChBankdataDAL.ImpBankData(table, "1", Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), Convert.ToString(Session["COMPANYID"]));
                }
                else
                {
                    jsonRespose.Success = false;
                    jsonRespose.Message = "excel无数据!";
                    return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
                }


                var result = new DBResult();
                //if (customerFieldName == "委托单位")
                //{
                var userid = Session["USERID"].ToString();
                var username = Session["SHOWNAME"].ToString();
                var companyid = Session["COMPANYID"].ToString();
                //if (imptype == "2")
                //{
                //    result = MsOp_InternalTradeDAL.UpdateOpInternalTrade(table, userid, username, companyid);

                //}
                //else
                //{
                //    result = MsOp_InternalTradeDAL.ImpOpInternalTrade(table, userid, username, companyid);
                //}
                var json = JsonConvert.Serialize(new
                {
                    success = result.Success,
                    Message = result.Message
                });
                return new ContentResult() { Content = json };
            }
            catch (Exception e)
            {

                jsonRespose.Success = false;
                jsonRespose.Message = "读取Excel文件出错,请确认文件正确性" + e.Message;
                return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
            }
        }





        #endregion



        #region 参照部分

        #endregion

    }
}