You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1141 lines
45 KiB
C#

12 months ago
using System;
using System.Linq;
using System.Web.Mvc;
using DSWeb.MvcContainer.Models.MsCodeCtnTk;
using DSWeb.MvcContainer.DAL.MsCodeCtnTk;
using DSWeb.MvcShipping.Helper;
using DSWeb.MvcShipping.Comm.Cookie;
using System.Collections.Generic;
using HcUtility.Comm;
using HcUtility.Core;
using System.IO;
using System.Data;
using System.Data.OleDb;
using DSWeb.EntityDA;
using DSWeb.Areas.CommMng.DAL;
using DSWeb.MvcShipping.DAL.MsOpReceiptDAL;
using DSWeb.Areas.Import.DAL.ReceiptDoc;
using ICSharpCode.SharpZipLib.Zip;
namespace DSWeb.MvcContainer.Controllers
{
[JsonRequestBehavior]
public class MsCodeCtnTkController : Controller
{
//
// GET:
public ActionResult Index()
{
return View();
}
//
// GET: /
public ActionResult Edit()
{
return View();
}
public ActionResult AddIndex()
{
return View();
}
public ActionResult RptDateIndex()
{
return View();
}
public ActionResult CtnView()
{
return View();
}
[HttpGet]
public ActionResult UploadEdiFile()
{
return View();
}
//
// GET
public ContentResult GetDataList(int start, int limit, string sort, string condition)
{
int total = 0;
var dataList = MsCodeCtnTkDAL.GetDataList(start,limit,condition, Convert.ToString(Session["COMPANYID"]),out total,sort);
var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = total, data = dataList.ToList() });
return new ContentResult() { Content = json };
}
//罐箱超期提醒 列表方法
public ContentResult GetDatadDateList(int start, int limit, string sort, string condition)
{
var dataList = MsCodeCtnTkDAL.GetDataDateList(condition, 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 GetDatadDateListStr(string sort, string condition)
{
var dataListStr = MsCodeCtnTkDAL.GetDataDateListStr(condition, Convert.ToString(Session["COMPANYID"]), sort);
var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", data = dataListStr });
return new ContentResult() { Content = json };
}
public ContentResult GetDataListStr(string sort, string condition)
{
var dataListStr = MsCodeCtnTkDAL.GetDataListStr(condition, Convert.ToString(Session["COMPANYID"]), sort);
var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", data = dataListStr });
return new ContentResult() { Content = json };
}
//
// GET/TruckMng/MsWlTyreAcc/GetData/
public ContentResult GetData(string handle, string condition)
{
CodeCtnTk head = null;
int total = 0;
if (handle == "edit")
{
var list = MsCodeCtnTkDAL.GetDataList(0,1,condition, Convert.ToString(Session["COMPANYID"]),out total);
if (list.Count > 0)
head = list[0];
}
if (head == null)
{
head = new CodeCtnTk();
head.COMPANYID = Convert.ToString(Session["COMPANYID"]);
}
var json = JsonConvert.Serialize(
new { Success = true, Message = "查询成功", data = head });
return new ContentResult() { Content = json };
}
public ContentResult Save(string opstatus, string data, string body, string Inspectbody, string Insurancebody, string beforebody)
{
var headData = JsonConvert.Deserialize<CodeCtnTk>(data);
var bodyList = JsonConvert.Deserialize<List<CodeCtnTkNoLaneItems>>(body);
var InspectbodyList = JsonConvert.Deserialize<List<CodeCtnTkInspect>>(Inspectbody);
var InsurancebodyList = JsonConvert.Deserialize<List<CodeCtnTkInsurance>>(Insurancebody);
var BeforebodyList = JsonConvert.Deserialize<List<CodeCtnTkBefore>>(beforebody);
var errorstr = "";
var isPost = true;
var ct = MsCodeCtnTkDAL.GetRdCount("GID<>'" + headData.GID + "' AND CTNNO='" + headData.CTNNO + "' and CTNNO<>''");
if (ct != 0)
{
isPost = false;
errorstr = "罐号重复!";
}
var isnotctnno = MsCodeCtnTkDAL.ValidateContainerNumber(headData.CTNNO);
if (!isnotctnno)
{
isPost = false;
errorstr = "罐号验证不通过!";
}
if (opstatus == "add")
{
headData.DbOperationType = DbOperationType.DbotIns;
headData.COMPANYID = Convert.ToString(Session["COMPANYID"]);
headData.INPUTBY = Convert.ToString(Session["USERID"]);
headData.INPUTTIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
else if (opstatus == "edit")
{
headData.DbOperationType = DbOperationType.DbotUpd;
headData.ModelUIStatus = "E";
}
else
{
headData.DbOperationType = DbOperationType.DbotDel;
}
if (headData.MADEDATE == "") headData.MADEDATE = null;
var GID = headData.GID;
if (InsurancebodyList != null)
{
var insurance = InsurancebodyList.Find(x => x.LINKGID == "*");
if (insurance!=null&&insurance.LINKGID == "*")
{
foreach (var enumValue in InsurancebodyList)
{
if (enumValue.GID != insurance.GID)
{
if ((Convert.ToDateTime(insurance.BEGINDATE) >= Convert.ToDateTime(enumValue.BEGINDATE))
&& (Convert.ToDateTime(insurance.BEGINDATE) <= Convert.ToDateTime(enumValue.ENDDATE))
|| ((Convert.ToDateTime(insurance.ENDDATE) >= Convert.ToDateTime(enumValue.BEGINDATE))
&& (Convert.ToDateTime(insurance.ENDDATE) <= Convert.ToDateTime(enumValue.ENDDATE))))
{
isPost = false;
var jsonRespose = new JsonResponse { Success = false, Message = "保险信息存在重叠区间,不允许保存!" };
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
}
}
}
}
if (isPost)
{
var modb = new ModelObjectDB();
var result = modb.Save(headData);
if (result.Success == true)
{
result = MsCodeCtnTkDAL.SaveCodeCtnTkNoLaneItems(bodyList, GID, Convert.ToString(Session["USERID"]));
result = MsCodeCtnTkDAL.SaveCodeCtnTkINSPECTItems(InspectbodyList, GID, Convert.ToString(Session["USERID"]));
result = MsCodeCtnTkDAL.SaveCodeCtnTkInsuranceItems(InsurancebodyList, GID, Convert.ToString(Session["USERID"]));
result = MsCodeCtnTkDAL.SaveCodeCtnTkBeforeItems(BeforebodyList, GID, Convert.ToString(Session["USERID"]));
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
string blUpSQL = "update op_ctnbscard set CTNPROP='"+headData.CTNPROP+ "' where CTNGID='" + headData.GID + "'";
bool bl = T_ALL_DA.GetExecuteSqlCommand(blUpSQL);
}
var jsonRespose = new JsonResponse
{
Success = result.Success,
Message = result.Message,
Data = MsCodeCtnTkDAL.GetData(" c.GID='" + GID + "'", Convert.ToString(Session["COMPANYID"]))
};
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
else
{
var jsonRespose = new JsonResponse { Success = false, Message = errorstr };
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
}
public ContentResult Delete(string data)
{
var headData = JsonConvert.Deserialize<List<CodeCtnTk>>(data);
var ctnnos = "";
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
if (headData != null)
{
foreach (var enumValue in headData)
{
var BLCOUNT2 = T_ALL_DA.GetStrSQL("BLCOUNT", "select count(*) BLCOUNT from op_ctnbscard WHERE CTNNO='" + enumValue.CTNNO + "'");
if (BLCOUNT2 != "0")
{
ctnnos = ctnnos + enumValue.CTNNO + " ";
}
}
}
if (ctnnos != "") {
var jsonRespose2 = new JsonResponse { Success =false, Message ="箱号:"+ctnnos+" 已使用,不允许删除!" };
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose2) };
}
DBResult result = MsCodeCtnTkDAL.DeleteCodeCtnTk(headData);
var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
#region 不去航线
public ContentResult GetCodeCtnTkNoLaneItemsList(string condition, string sort)
{
var dataList = MsCodeCtnTkDAL.GetCodeCtnTkNoLaneItemsList(condition, sort);
var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = dataList.ToList() });
return new ContentResult() { Content = json };
}
public ContentResult SaveCodeCtnTkNoLaneItems(string body, string PID)
{
//
var bodyList = JsonConvert.Deserialize<List<CodeCtnTkNoLaneItems>>(body);
DBResult result = MsCodeCtnTkDAL.SaveCodeCtnTkNoLaneItems(bodyList, PID, Convert.ToString(Session["USERID"]));
var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
public ContentResult DeleteCodeCtnTkNoLaneItems(string data)
{
var headData = JsonConvert.Deserialize<List<CodeCtnTkNoLaneItems>>(data);
DBResult result = MsCodeCtnTkDAL.DeleteCodeCtnTkNoLaneItems(headData);
var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
#endregion
#region 年检
public ContentResult GetCodeCtnTkInspectItemsList(string condition, string sort)
{
var dataList = MsCodeCtnTkDAL.GetCodeCtnTkInspectItemsData(condition, sort);
var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = dataList.ToList() });
return new ContentResult() { Content = json };
}
public ContentResult SaveCodeCtnTkInspectItems(string body, string PID)
{
//
var bodyList = JsonConvert.Deserialize<List<CodeCtnTkInspect>>(body);
DBResult result = MsCodeCtnTkDAL.SaveCodeCtnTkINSPECTItems(bodyList, PID, Convert.ToString(Session["USERID"]));
var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
public ContentResult LockCodeCtnTkINSPECTItems(string data, int islock)
{
//
var bodyList = JsonConvert.Deserialize<List<CodeCtnTkInspect>>(data);
DBResult result = MsCodeCtnTkDAL.LockCodeCtnTkINSPECTItems(bodyList,islock);
var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
public ContentResult DeleteCodeCtnTkInspectItems(string data)
{
var headData = JsonConvert.Deserialize<List<CodeCtnTkInspect>>(data);
DBResult result = MsCodeCtnTkDAL.DeleteCodeCtnTkINSPECTItems(headData);
var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
#endregion
public ContentResult GetCodeCtnTkBeforeItemsData(string condition, string sort)
{
var dataList = MsCodeCtnTkDAL.GetCodeCtnTkBeforeItemsData(condition, sort);
var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = dataList.ToList() });
return new ContentResult() { Content = json };
}
public ContentResult DeleteCodeCtnTkBeforeItems(string data)
{
var headData = JsonConvert.Deserialize<List<CodeCtnTkBefore>>(data);
DBResult result = MsCodeCtnTkDAL.DeleteCodeCtnTkBeforeItems(headData);
var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
#region 保险
public ContentResult GetCodeCtnTkInsuranceItemsList(string condition, string sort)
{
var dataList = MsCodeCtnTkDAL.GetCodeCtnTkInsuranceItemdata(condition, sort);
var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = dataList.ToList() });
return new ContentResult() { Content = json };
}
public ContentResult SaveCodeCtnTkInsuranceItems(string body, string PID)
{
//
var bodyList = JsonConvert.Deserialize<List<CodeCtnTkInsurance>>(body);
DBResult result = MsCodeCtnTkDAL.SaveCodeCtnTkInsuranceItems(bodyList, PID, Convert.ToString(Session["USERID"]));
var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
public ContentResult DeleteCodeCtnTkInsuranceItems(string data)
{
var headData = JsonConvert.Deserialize<List<CodeCtnTkInsurance>>(data);
DBResult result = MsCodeCtnTkDAL.DeleteCodeCtnTkInsuranceItems(headData);
var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
public ContentResult LockCodeCtnTkInsuranceItems(string data, int islock)
{
//
var bodyList = JsonConvert.Deserialize<List<CodeCtnTkInsurance>>(data);
DBResult result = MsCodeCtnTkDAL.LockCodeCtnTkInsuranceItems(bodyList, islock);
var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
public ContentResult LockCodeCtnTkInsurance(string data)
{
DBResult result = MsCodeCtnTkDAL.LockCodeCtnTkInsurance(data);
var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
public ContentResult modify(string data, string modifydata)
{
var bodyList = JsonConvert.Deserialize<List<CodeCtnTk>>(data);
var amodifydata = JsonConvert.Deserialize<CodeCtnTk>(modifydata);
var result = new DBResult();
if (bodyList != null)
{
result = MsCodeCtnTkDAL.Modify(bodyList, amodifydata, Convert.ToString(Session["COMPANYID"]), Convert.ToString(Session["USERID"]));
}
var jsonRespose = new JsonResponse
{
Success = result.Success,
Message = result.Message,
};
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
#region 用excel批量上传罐箱保险信息
public ContentResult ImportCtnInsurance()
{
var jsonRespose = new JsonResponse { Success = false, Message = "" };
var dt = new DataTable();
var _r = BasicDataRefDAL.RequestExceltoDatatable(Request, out dt);
var bsno = Request.Form["bsno"];
if (_r.Success)
{
var _rn = MsCodeCtnTkDAL.InsertCtnInsurance(bsno,Session["SHOWNAME"].ToString(), dt);
if (_rn >= 0) {
jsonRespose.Success = true;
jsonRespose.Message = $"成功上传{ _rn.ToString()}条保险数据";
}
}
else
{
jsonRespose = new JsonResponse(_r);
}
return jsonRespose.getContentReult();
}
#endregion
#region 用excel批量上传罐箱年检信息
[HttpPost]
public ContentResult InsertCtnInspect()
{
var jsonRespose = new JsonResponse { Success = false, Message = "" };
var dt = new DataTable();
var _r = BasicDataRefDAL.RequestExceltoDatatable(Request, out dt);
var bsno = Request.Form["bsno"];
if (_r.Success)
{
var _rn = MsCodeCtnTkDAL.InsertCtnInspect(bsno, Convert.ToString(Session["USERID"]), dt);
if (_rn >= 0)
{
jsonRespose.Success = true;
jsonRespose.Message = $"成功上传{ _rn.ToString()}条年检数据";
}
}
else
{
jsonRespose = new JsonResponse(_r);
}
return jsonRespose.getContentReult();
}
[HttpPost]
public ContentResult ImportCtnTk()
{
var jsonRespose = new JsonResponse { Success = false, Message = "" };
var dt = new DataTable();
var _r = BasicDataRefDAL.RequestExceltoDatatable(Request, out dt);
if (_r.Success)
{
var _rn = MsCodeCtnTkDAL.InsertCtnTk(dt, Convert.ToString(Session["USERID"]),Convert.ToString(Session["COMPANYID"]));
jsonRespose.Success = true;
jsonRespose.Message = _rn.Message;
}
else
{
jsonRespose = new JsonResponse(_r);
}
return jsonRespose.getContentReult();
}
#endregion
#endregion
#region EXCEL引入
[HttpPost]
public ContentResult ImportCtnTkInsurance()
{
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["LoadExcel"];
if (file == null)
{
jsonRespose.Success = false;
jsonRespose.Message = "上传文件发生未知错误,请重新上传";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
var path = Server.MapPath("../../UploadFiles/CtnTkDetail");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
var size = file.ContentLength;
var name = Path.GetFileName(file.FileName);
var bsno = Request.Form["bsno"];
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) };
}
List<string> sheets = ExcelSheetName(filename);
if (sheets.Count == 0)
{
jsonRespose.Success = false;
jsonRespose.Message = "上传的Excel不包含数据02";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
try
{
var sheetname = sheets[0];
string excelConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename +
";Extended Properties=Excel 8.0;";
if (filename.ToLower().IndexOf(".xlsx") > 0)
{
excelConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename +
";Extended Properties=\"Excel 12.0 Xml;HDR=Yes\"";
}
OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "A1:N]", excelConn);
DataSet ds = new DataSet();
oada.Fill(ds);
if (ds.Tables.Count == 0)
{
jsonRespose.Success = false;
jsonRespose.Message = "上传的Excel不包含数据03";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
var table = ds.Tables[0];
if (table.Rows.Count == 0)
{
jsonRespose.Success = false;
jsonRespose.Message = "上传的Excel不包含数据04";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
var message = string.Empty;
List<CodeCtnTkInsurance> headList;
bool isSucess = false;
isSucess = MsCodeCtnTkDAL.ImportCtnTkInsurance(bsno, Request, table, Convert.ToString(Session["USERID"]), out message, out headList);
if (!isSucess)
{
jsonRespose.Success = false;
jsonRespose.Message = message;
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
var json = JsonConvert.Serialize(new { success = true, Message = "上传成功,共生成" + Convert.ToString(headList.Count) + "个托单", data = headList.ToList() });
return new ContentResult() { Content = json };
}
catch (Exception)
{
jsonRespose.Success = false;
jsonRespose.Message = "读取Excel文件出错请确认文件正确性";
return jsonRespose.getContentReult();
}
}
[HttpPost]
public ContentResult ImportCtnTkInspect()
{
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["LoadExcel"];
if (file == null)
{
jsonRespose.Success = false;
jsonRespose.Message = "上传文件发生未知错误,请重新上传";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
var path = Server.MapPath("../../UploadFiles/CtnTkDetail");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
var size = file.ContentLength;
var name = Path.GetFileName(file.FileName);
var bsno = Request.Form["bsno"];
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) };
}
List<string> sheets = ExcelSheetName(filename);
if (sheets.Count == 0)
{
jsonRespose.Success = false;
jsonRespose.Message = "上传的Excel不包含数据02";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
try
{
var sheetname = sheets[0];
string excelConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename +
";Extended Properties=Excel 8.0;";
if (filename.ToLower().IndexOf(".xlsx") > 0)
{
excelConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename +
";Extended Properties=\"Excel 12.0 Xml;HDR=Yes\"";
}
OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "A1:N]", excelConn);
DataSet ds = new DataSet();
oada.Fill(ds);
if (ds.Tables.Count == 0)
{
jsonRespose.Success = false;
jsonRespose.Message = "上传的Excel不包含数据03";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
var table = ds.Tables[0];
if (table.Rows.Count == 0)
{
jsonRespose.Success = false;
jsonRespose.Message = "上传的Excel不包含数据04";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
var message = string.Empty;
List<CodeCtnTkInspect> headList;
bool isSucess = false;
isSucess = MsCodeCtnTkDAL.ImportCtnTkInspect(bsno, Request, table, Convert.ToString(Session["USERID"]), out message, out headList);
if (!isSucess)
{
jsonRespose.Success = false;
jsonRespose.Message = message;
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
var json = JsonConvert.Serialize(new { success = true, Message = "上传成功,共生成" + Convert.ToString(headList.Count) + "个托单", data = headList.ToList() });
return new ContentResult() { Content = json };
}
catch (Exception)
{
jsonRespose.Success = false;
jsonRespose.Message = "读取Excel文件出错请确认文件正确性";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
}
public List<string> ExcelSheetName(string filepath)
{
var al = new List<string>();
try
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
if (filepath.ToLower().IndexOf(".xlsx") > 0)
{
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath +
";Extended Properties=\"Excel 12.0 Xml;HDR=Yes\"";
}
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
System.Data.DataTable sheetNames = conn.GetOleDbSchemaTable
(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
conn.Close();
foreach (DataRow dr in sheetNames.Rows)
{
al.Add(dr[2].ToString());
}
}
catch (Exception)
{
return new List<string>();
}
return al;
}
[HttpPost]
public ContentResult ImportCtnTkInsurance_List()
{
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["LoadExcel"];
if (file == null)
{
jsonRespose.Success = false;
jsonRespose.Message = "上传文件发生未知错误,请重新上传";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
var path = Server.MapPath("../../UploadFiles/CtnTkDetail");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
var size = file.ContentLength;
var name = Path.GetFileName(file.FileName);
var bsno = Request.Form["bsno"];
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) };
}
List<string> sheets = ExcelSheetName(filename);
if (sheets.Count == 0)
{
jsonRespose.Success = false;
jsonRespose.Message = "上传的Excel不包含数据02";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
try
{
var sheetname = sheets[0];
string excelConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename +
";Extended Properties=Excel 8.0;";
if (filename.ToLower().IndexOf(".xlsx") > 0)
{
excelConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename +
";Extended Properties=\"Excel 12.0 Xml;HDR=Yes\"";
}
OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "A1:N]", excelConn);
DataSet ds = new DataSet();
oada.Fill(ds);
if (ds.Tables.Count == 0)
{
jsonRespose.Success = false;
jsonRespose.Message = "上传的Excel不包含数据03";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
var table = ds.Tables[0];
if (table.Rows.Count == 0)
{
jsonRespose.Success = false;
jsonRespose.Message = "上传的Excel不包含数据04";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
var message = string.Empty;
List<CodeCtnTkInsurance> headList;
bool isSucess = false;
isSucess = MsCodeCtnTkDAL.ImportCtnTkInsurance_List(bsno, Request, table, Convert.ToString(Session["USERID"]), out message, out headList);
if (!isSucess)
{
jsonRespose.Success = false;
jsonRespose.Message = message;
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
var json = JsonConvert.Serialize(new { success = true, Message = "上传成功,共生成" + Convert.ToString(headList.Count) + "个托单", data = headList.ToList() });
return new ContentResult() { Content = json };
}
catch (Exception)
{
jsonRespose.Success = false;
jsonRespose.Message = "读取Excel文件出错请确认文件正确性";
return jsonRespose.getContentReult();
}
}
#endregion
[HttpPost]
public ActionResult UpEdiFileParse()
{
DBResult resp = new DBResult();
if (Request.Files.Count > 0)
{
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
var filelist = "";
for (int idx = 0; idx < Request.Files.Count; idx++)
{
var file = Request.Files.Get(idx);
var ctnno = Path.GetFileNameWithoutExtension(file.FileName);
var name = Path.GetFileName(file.FileName);
var bsno = T_ALL_DA.GetStrSQL("GID", "select top 1 GID from [Code_CtnTkInspect] where CTNNO='" + ctnno + "'");
if (bsno != null && bsno.Trim() != "")
{
var path = Server.MapPath("../../UploadFiles/MsOpFiles/" + ctnno);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
var usercode = Convert.ToString(Session["USERCODE"]);
var partname = usercode + DateTime.Now.ToString("yyyyMMddHHmmssfff") + name;
string filename = path + "\\" + partname;
string filenameURL = "../../UploadFiles/MsOpFiles/" + ctnno + "/" + partname;
if (System.IO.File.Exists(filename))
{
System.IO.File.Delete(filename);
}
file.SaveAs(filename);
var isSucess = MsOpReceiptDAL.UpdateFileData(filenameURL, name, bsno, Convert.ToString(Session["USERID"]),"罐证","","");
if (isSucess.Success)
{
}
else
{
filelist = filelist + name + ";";
}
}
else {
filelist = filelist + name+";";
}
}
if (filelist.Trim() != "") filelist = "其中文件名:" + filelist + "罐号未匹配未导入(文件名必须和罐号一致)";
resp.Success = true;
resp.Message = "导入成功"+ filelist;
}
return Json(resp);
}
public ActionResult DownloadFileList(string data)
{
var headData = JsonConvert.Deserialize<List<CodeCtnTk>>(data);
var ctnnos = "";
var usercode = CookieConfig.GetCookie_UserCode(Request);
var pathstr = "../../UploadFiles/ViewTmp/" + usercode + DateTime.Now.ToString("yyyyMMddHHmmssfff");
var filePath = Server.MapPath(pathstr);
var zipfilename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".zip";
var destFile = filePath + "\\" + zipfilename;
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
if (headData != null)
{
using (ZipFile zipFile = ZipFile.Create(destFile))
{
zipFile.BeginUpdate();
foreach (var enumValue in headData)
{
var doc = ReceiptDocDAL.GetDoc("BSNO=(select top 1 GID FROM Code_CtnTkInspect WHERE CTNNO='" + enumValue.CTNNO + "') and (RECEIPTTYPE='罐证' or isnull(RECEIPTTYPE,'')='') ");
//foreach (var doc in dataList)
//{
if (!string.IsNullOrEmpty(doc.Driect_URL))
{
var tmpfile = Server.MapPath(doc.Driect_URL);
if (System.IO.File.Exists(tmpfile))
{
FileInfo fileInfo2 = new FileInfo(tmpfile);
fileInfo2.CopyTo(filePath + "\\" + doc.URL);
zipFile.Add(filePath + "\\" + doc.URL, doc.URL);
}
}
else {
doc = ReceiptDocDAL.GetDoc("BSNO='" + enumValue.GID + "' and (RECEIPTTYPE='罐证' or isnull(RECEIPTTYPE,'')='') ");
if (!string.IsNullOrEmpty(doc.Driect_URL))
{
var tmpfile = Server.MapPath(doc.Driect_URL);
if (System.IO.File.Exists(tmpfile))
{
FileInfo fileInfo2 = new FileInfo(tmpfile);
fileInfo2.CopyTo(filePath + "\\" + doc.URL);
zipFile.Add(filePath + "\\" + doc.URL, doc.URL);
}
}
}
//}
}
zipFile.CommitUpdate();
}
}
//FileStream fs = new FileStream(destFile, FileMode.Open);
//byte[] datazip = new byte[fs.Length];
//fs.Read(datazip, 0, datazip.Length);
//fs.Close();
//MemoryStream ms = new MemoryStream(datazip);
//Response.AddHeader("Content-Disposition", "attachment;fileName=" + zipfilename);
//Response.BinaryWrite(ms.ToArray());
FileInfo fileInfo = new FileInfo(destFile);
Response.Clear(); //清除缓冲区流中的所有内容输出
Response.ClearContent(); //清除缓冲区流中的所有内容输出
Response.ClearHeaders(); //清除缓冲区流中的所有头
Response.Buffer = true; //该值指示是否缓冲输出,并在完成处理整个响应之后将其发送
Response.AddHeader("Content-Disposition", "attachment;filename=" + zipfilename);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/unknow"; //获取或设置输出流的 HTTP MIME 类型
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); //获取或设置输出流的 HTTP 字符集
Response.TransmitFile(fileInfo.FullName);
Response.End();
var json = JsonConvert.Serialize(new { success = true, Message = "下载完成"});
return new ContentResult() { Content = json };
}
public ActionResult DownloadFileList2(string data)
{
var headData = JsonConvert.Deserialize<List<CodeCtnTk>>(data);
var ctnnos = "";
var usercode = CookieConfig.GetCookie_UserCode(Request);
var pathstr = "../../UploadFiles/ViewTmp/" + usercode + DateTime.Now.ToString("yyyyMMddHHmmssfff");
var filePath = Server.MapPath(pathstr);
var zipfilename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".zip";
var destFile = filePath + "\\" + zipfilename;
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
if (headData != null)
{
using (ZipFile zipFile = ZipFile.Create(destFile))
{
zipFile.BeginUpdate();
foreach (var enumValue in headData)
{
var doc = ReceiptDocDAL.GetDoc("BSNO=(select top 1 GID FROM Code_CtnTkInspect WHERE CTNNO='" + enumValue.CTNNO + "') and (RECEIPTTYPE='罐证' or isnull(RECEIPTTYPE,'')='') ");
//foreach (var doc in dataList)
//{
if (!string.IsNullOrEmpty(doc.Driect_URL))
{
var tmpfile = Server.MapPath(doc.Driect_URL);
if (System.IO.File.Exists(tmpfile))
{
FileInfo fileInfo2 = new FileInfo(tmpfile);
fileInfo2.CopyTo(filePath + "\\" + doc.URL);
zipFile.Add(filePath + "\\" + doc.URL, doc.URL);
}
}
else
{
doc = ReceiptDocDAL.GetDoc("BSNO=(SELECT TOP 1 GID FROM CODE_CTNTK WHERE CTNNO='" + enumValue.CTNNO + "') and (RECEIPTTYPE='罐证' or isnull(RECEIPTTYPE,'')='') ");
if (!string.IsNullOrEmpty(doc.Driect_URL))
{
var tmpfile = Server.MapPath(doc.Driect_URL);
if (System.IO.File.Exists(tmpfile))
{
FileInfo fileInfo2 = new FileInfo(tmpfile);
fileInfo2.CopyTo(filePath + "\\" + doc.URL);
zipFile.Add(filePath + "\\" + doc.URL, doc.URL);
}
}
}
//}
}
zipFile.CommitUpdate();
}
}
//FileStream fs = new FileStream(destFile, FileMode.Open);
//byte[] datazip = new byte[fs.Length];
//fs.Read(datazip, 0, datazip.Length);
//fs.Close();
//MemoryStream ms = new MemoryStream(datazip);
//Response.AddHeader("Content-Disposition", "attachment;fileName=" + zipfilename);
//Response.BinaryWrite(ms.ToArray());
FileInfo fileInfo = new FileInfo(destFile);
Response.Clear(); //清除缓冲区流中的所有内容输出
Response.ClearContent(); //清除缓冲区流中的所有内容输出
Response.ClearHeaders(); //清除缓冲区流中的所有头
Response.Buffer = true; //该值指示是否缓冲输出,并在完成处理整个响应之后将其发送
Response.AddHeader("Content-Disposition", "attachment;filename=" + zipfilename);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/unknow"; //获取或设置输出流的 HTTP MIME 类型
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); //获取或设置输出流的 HTTP 字符集
Response.TransmitFile(fileInfo.FullName);
Response.End();
var json = JsonConvert.Serialize(new { success = true, Message = "下载完成" });
return new ContentResult() { Content = json };
}
#region 参照部分
#endregion
}
}