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.
DS7/DSWeb/Areas/MvcShipping/Controllers/MsOpAirn_FreightController.cs

595 lines
21 KiB
C#

3 years ago
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using DSWeb.Areas.CommMng.DAL;
using DSWeb.Areas.RptMng.Comm;
using DSWeb.TruckMng.Comm.Cookie;
using DSWeb.TruckMng.Helper;
using DSWeb.TruckMng.Helper.Repository;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DSWeb.EntityDA;
using DSWeb.Areas.CommMng.Models;
using HcUtility.Comm;
using HcUtility.Core;
using System.IO;
using System.Data.OleDb;
using DSWeb.MvcShipping.Models.MsOpAirn;
using DSWeb.MvcShipping.DAL.MsOpAirnDAL;
using System.Web.Configuration;
namespace DSWeb.MvcShipping.Controllers
{
/// <summary>
/// 路单查询
/// </summary>
[JsonRequestBehavior]
public class MsOpAirn_FreightController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Edit()
{
return View();
}
public static string GetRangDAStr(string tb, string userid, string usercode, string orgcode)
{
string str = "";
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append(" VISIBLERANGE,OPERATERANGE ");
strSql.Append(" from VW_User_Authority ");
strSql.Append(" where [NAME]='modTruckBs' and USERID='" + userid + "' and ISDELETE=0");
string visiblerange = "4";
string operaterange = "4";
Database db = DatabaseFactory.CreateDatabase();
DataSet ds = new DataSet();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
visiblerange = Convert.ToString(reader["VISIBLERANGE"]);
operaterange = Convert.ToString(reader["OPERATERANGE"]);
break;
}
reader.Close();
}
if (visiblerange == "4")
{
str = "1=2";
}
else if (visiblerange == "3")
{
if (tb == "index")
{
str = " A.UserCode='" + usercode + "'";
}
else
{
str = " UPPER(A.OrgCode)='" + orgcode + "'";
}
}
else if (visiblerange == "2")
{
if (tb == "index")
{
var rangeDa = new RangeDA();
var companyid = rangeDa.GetCORPID(userid);
var deptname = rangeDa.GetDEPTNAME(userid);
var userstr = new StringBuilder();
userstr.Append(" select codename from [user] where GID in (select USERID from user_company where COMPANYID='" + companyid + "') and GID in (select userid from user_baseinfo where DEPTNAME='" + deptname + "')");
Database userdb = DatabaseFactory.CreateDatabase();
using (IDataReader reader = userdb.ExecuteReader(CommandType.Text, userstr.ToString()))
{
str = "";
while (reader.Read())
{
if (str == "")
{
str = "(A.usercode='" + Convert.ToString(reader["codename"]) + "'";
}
else
{
str = str+ " or A.usercode='" + Convert.ToString(reader["codename"]) + "'";
};
}
str =str+ ")";
reader.Close();
}
}
else
{
str = " UPPER(A.OrgCode)='" + orgcode + "'";
}
}
else if (visiblerange == "1")
{
str = " UPPER(A.OrgCode)='" + orgcode + "'";
}
return str;
}
public ContentResult GetDataList(int start, int limit, string sort, string condition)
{
var dataList = MsOpAirn_FreightDAL.GetDataList(condition, 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)
{
MsOpAirn_FreightPLANmb head = null;
if (handle == "add")
{
head = new MsOpAirn_FreightPLANmb();
head.GID = System.Guid.NewGuid().ToString();
head.ENTEROPERATOR = Convert.ToString(Session["SHOWNAME"]);
head.ENTERDATE = DateTime.Now.ToString();
}
else
{
var _t = "";
var list = MsOpAirn_FreightDAL.GetDataList(condition, _t);
if (list.Count > 0)
head = list[0];
if (head == null)
{
head = new MsOpAirn_FreightPLANmb();
}
}
var json = JsonConvert.Serialize(
new { Success = true, Message = "查询成功", data = head });
return new ContentResult() { Content = json };
}
public ContentResult GetBodyList ( int start, int limit, string sort, string condition )
{
var dataList = MsOpAirn_FreightDAL.GetBodyList(condition, 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 Save ( string opstatus, string data,
string CargoBody, string CargoDelBody )
{
var head = JsonConvert.Deserialize<MsOpAirn_FreightPLANmb>(data);
var CargobodyList = JsonConvert.Deserialize<List<MsOpAirn_Freight_Detailmb>>(CargoBody);
var CargodelbodyList = JsonConvert.Deserialize<List<MsOpAirn_Freight_Detailmb>>(CargoDelBody);
if (opstatus == "add")
{
head.DbOperationType = DbOperationType.DbotIns;
head.ModelUIStatus = "I";
//head.Mainstate = "0";
head.GID = System.Guid.NewGuid().ToString();
}
else if (opstatus == "edit")
{
head.DbOperationType = DbOperationType.DbotUpd;
head.ModelUIStatus = "E";
}
else
{
head.DbOperationType = DbOperationType.DbotDel;
}
var modb = new ModelObjectRepository();
DBResult result = modb.Save(head,
ModelObjectConvert<MsOpAirn_Freight_Detailmb>.ToModelObjectList(CargobodyList),
ModelObjectConvert<MsOpAirn_Freight_Detailmb>.ToModelObjectList(CargodelbodyList)
);
//刷新父窗口上的父节点
var jsonRespose = new JsonResponse
{
Success = result.Success,
Message = result.Message,
Data = MsOpAirn_FreightDAL.GetData("GID='" + head.GID + "'")
};
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
[HttpPost]
public ContentResult File2 ( )
{
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["file"];
var PLAN_GID = Request.Form["GID"].ToString().Trim();
var AIRLINES = Request.Form["AIRLINES"].ToString().Trim();
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) };
}
var ExcelExt = "";
try
{
ExcelExt = WebConfigurationManager.AppSettings["ExcelExt"].ToString();
}
catch
{
}
if (ExcelExt == "" || ExcelExt == null) ExcelExt = "Excel 8.0;HDR=Yes;IMEX=1";
List<string> sheets = ExcelSheetName(filename, ExcelExt);
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="+ ExcelExt;
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:X]", 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<MsOpAirn_Freight_Detailmb> headList;
//if (customerFieldName == "委托单位")
//{
var InsertCount = 0;
var UpdateCount = 0;
var UnKnowenTruckNo = "";
var isSucess = MsOpAirn_FreightDAL.ImportExcelData(Request, table, out message, out InsertCount, out UpdateCount, PLAN_GID, AIRLINES, out UnKnowenTruckNo, 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) + "条记录,其中新增" + Convert.ToString(InsertCount)
+ "个,更新" + Convert.ToString(UpdateCount) + "个。" + UnKnowenTruckNo,
data = headList.ToList()
});
return new ContentResult() { Content = json };
}
catch (Exception e)
{
jsonRespose.Success = false;
jsonRespose.Message = "读取Excel文件出错:<"+e.Message+">,请确认文件正确性";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
}
[HttpPost]
public ContentResult File()
{
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["file"];
var PLAN_GID = Request.Form["GID"].ToString().Trim();
var AIRLINES = Request.Form["AIRLINES"].ToString().Trim();
if (file == null)
{
jsonRespose.Success = false;
jsonRespose.Message = "上传文件发生未知错误,请重新上传";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
var path = Server.MapPath("../../UploadFiles/Bs");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
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) };
}
var ExcelExt = "";
try
{
ExcelExt = WebConfigurationManager.AppSettings["ExcelExt"].ToString();
}
catch
{
}
if (ExcelExt == "" || ExcelExt == null) ExcelExt = "Excel 8.0;HDR=Yes;IMEX=1";
List<string> sheets = ExcelSheetName(filename, ExcelExt);
if (sheets.Count == 0)
{
jsonRespose.Success = false;
jsonRespose.Message = filename + " 上传的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=" + ExcelExt;
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 + "A2:AA]", 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 || table.Rows.Count == 1)
{
jsonRespose.Success = false;
jsonRespose.Message = "上传的Excel不包含数据04";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
var message = string.Empty;
List<MsOpAirn_Freight_Detailmb> headList;
//if (customerFieldName == "委托单位")
//{
var InsertCount = 0;
var UpdateCount = 0;
var UnKnowenTruckNo = "";
var isSucess = MsOpAirn_FreightDAL.ImportExcelData(Request, table, out message, out InsertCount, out UpdateCount, PLAN_GID, AIRLINES, out UnKnowenTruckNo, 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) + "条记录,其中新增" + Convert.ToString(InsertCount)
+ "个,更新" + Convert.ToString(UpdateCount) + "个。" + UnKnowenTruckNo,
data = headList.ToList()
});
return new ContentResult() { Content = json };
}
catch (Exception e)
{
jsonRespose.Success = false;
jsonRespose.Message = "读取Excel文件出错 "+e.Message;
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
}
public List<string> ExcelSheetName ( string filepath,string excelext )
{
var al = new List<string>();
try
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties="+ excelext;
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 e)
{
BasicDataRefDAL.SaveLog(e.Message, "", "内贸空运", "导入费用方案");
return new List<string>();
}
return al;
}
public ContentResult Delete ( string data, string USERID )
{
var head = JsonConvert.Deserialize<MsOpAirn_FreightPLANmb>(data);
var modb = new ModelObjectDB();
DBResult result = modb.Delete(head, "delete from OpAirn_Freight_Detail where plan_gid='"+head.GID+"'");
//var ContractNo = head.ContractNo;
var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
//为了清除用证信息
//var _count = 0;
//_count = XXHDAL.DELUseApp(ContractNo);
//_count = _count + XXHDAL.ClearHTH(ContractNo);
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
public ContentResult COPYNEW ( string data )
{
var head = JsonConvert.Deserialize<MsOpAirn_FreightPLANmb>(data);
var OLDGID = head.GID;
head.DbOperationType = DbOperationType.DbotIns;
head.ModelUIStatus = "I";
head.GID = System.Guid.NewGuid().ToString();
head.ENTERDATE = System.DateTime.Now.ToString();
head.ENTEROPERATOR = Session["SHOWNAME"].ToString();
var modb = new ModelObjectRepository();
DBResult result = modb.Save(head
);
var _count = MsOpAirn_FreightDAL.COPYNEW(OLDGID,head.GID);
//刷新父窗口上的父节点
var jsonRespose = new JsonResponse
{
Success = result.Success,
Message = result.Message,
Data = MsOpAirn_FreightDAL.GetData("GID='" + head.GID + "'")
};
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
#region 参照部分
#endregion
}
}