using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.UI.MobileControls;
using DSWeb.Areas.CommMng.DAL;
using DSWeb.Areas.TruckMng.DAL.MsWlBs_HT;
using DSWeb.Areas.TruckMng.Models.Comm;
using DSWeb.Areas.TruckMng.Models.MsWlBs;
using DSWeb.TruckMng.Comm.Cookie;
using DSWeb.TruckMng.Helper;
using DSWeb.TruckMng.Helper.Repository;
using HcUtility.Comm;
using HcUtility.Core;
namespace DSWeb.Areas.TruckMng.Controllers
{
///
/// 托运单
///
[JsonRequestBehavior]
public class MsWlBs_HTController : Controller
{
//
// GET: /TruckMng/WlBsCard/
public ActionResult Index()
{
return View();
}
//
// GET: /TruckMng/WlBsCard/Edit
public ActionResult Edit()
{
return View();
}
//
// GET:/TruckMng/WlTruck/GetDataList
public ContentResult GetDataList(int start, int limit, string sort, string condition)
{
var dataList = MsWlBs_HTDAL.GetDataList(condition, Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), CookieConfig.GetCookie_OrgCode(Request), 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 };
}
//
// GET:/TruckMng/WlBsCard/GetData/
public ContentResult GetData(string handle, string condition)
{
MsWlBsHead head = null;
if (handle == "edit")
{
var list = MsWlBs_HTDAL.GetDataList(condition, Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), CookieConfig.GetCookie_OrgCode(Request));
if (list.Count > 0)
head = list[0];
}
if (head == null)
{
head = new MsWlBsHead();
head.DispatchCode = CookieConfig.GetCookie_UserCode(Request);
head.DispatchName=CookieConfig.GetCookie_UserName(Request);
head.DispatchCode_Ref = head.DispatchCode + "-" + head.DispatchName;
}
var json = JsonConvert.Serialize(
new { Success = true, Message = "查询成功", data = head });
return new ContentResult() { Content = json };
}
public ContentResult GetIsPrint(string billno,string Gid)
{
var IsPring = true;
IsPring = MsWlBs_HTDAL.GetBsIsPrint(billno, Gid);
var json = JsonConvert.Serialize(
new { Success = IsPring, Message = "此托单下所有费用都审核通过后,才可以打印托单!" });
return new ContentResult() { Content = json };
}
//
// GET:/TruckMng/WlBsCard/Save
public ContentResult Save(string opstatus, string data, string ctnbody, string ctndelbody,
string fixbody, string fixdelbody, string chfeebody, string chfeedelbody)
{
var head = JsonConvert.Deserialize(data);
var ctnBodyList = JsonConvert.Deserialize>(ctnbody);
var ctnBodyDelList = JsonConvert.Deserialize>(ctndelbody);
var fixBodyList = JsonConvert.Deserialize>(fixbody);
var fixBodyDelList = JsonConvert.Deserialize>(fixdelbody);
var chfeeBodyList = JsonConvert.Deserialize>(chfeebody);
var chfeeBodyDelList = JsonConvert.Deserialize>(chfeedelbody);
if (opstatus == "add")
{
head.BillNo = "HT"+PubSysDAL.GetBillNo("0112"); //获取物流号
head.DbOperationType = DbOperationType.DbotIns;
head.ModelUIStatus = "I";
head.UserCode = CookieConfig.GetCookie_UserCode(Request);
head.UserName = CookieConfig.GetCookie_UserName(Request);
head.OrgCode = CookieConfig.GetCookie_OrgCode(Request);
head.OrgName = CookieConfig.GetCookie_OrgName(Request);
head.LrDate = DateTime.Now;
}
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.ToModelObjectList(ctnBodyList),
ModelObjectConvert.ToModelObjectList(ctnBodyDelList),
ModelObjectConvert.ToModelObjectList(fixBodyList),
ModelObjectConvert.ToModelObjectList(fixBodyDelList),
ModelObjectConvert.ToModelObjectList(chfeeBodyList),
ModelObjectConvert.ToModelObjectList(chfeeBodyDelList));
var jsonRespose = new JsonResponse
{
Success = result.Success,
Message = result.Message,
Data = MsWlBs_HTDAL.GetHeadDataByBillNo(head.BillNo, Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), CookieConfig.GetCookie_OrgCode(Request))
};
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
public ContentResult Delete(string data)
{
var head = JsonConvert.Deserialize(data);
var modb = new ModelObjectDBBill();
DBResult result = modb.Delete(head,
"delete from tMsWlBsCtn where BillNo='" + head.BillNo + "'",
"delete from tMsWlBsFixed where BillNo='" + head.BillNo + "'",
"delete from ch_fee where BsNo='"+head.GId+"'");
var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
#region 明细表部分
public JsonResult GetCtnBodyList(string billno)
{
var condition = " BillNo='" + billno + "'";
List list = MsWlBs_HTDAL.GetCtnBodyList(condition);
return Json(new { Success = true, Message = "查询成功", totalCount = list.Count, data = list.ToList() });
}
public JsonResult GetFeeBodyList(string billno)
{
var condition = " BillNo='" + billno + "'";
List list = MsWlBs_HTDAL.GetBodyList(condition);
return Json(new { Success = true, Message = "查询成功", totalCount = list.Count, data = list.ToList() });
}
#endregion
#region 相关联部分
#endregion
#region 参照部分
#endregion
//TruckMng/MsWlBs/File
[HttpPost]
public ActionResult FileBak()
{
if (Request.Files.Count != 1)
{
return Json(new
{
success = false,
msg = "请选择上传的文件"
}, "text/html");
}
var file = Request.Files["file"];
if (file == null)
{
return Json(new
{
success = false,
msg = "上传文件发生未知错误,请重新上传"
}, "text/html");
}
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) };
}
string ext = Path.GetExtension(file.FileName).ToLower();
if (ext == ".asp" || ext == ".aspx")
{
return Json(new
{
success = false,
msg = "上传文件发生未知错误,请重新上传"
}, "text/html");
}
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))
{
return Json(new
{
success = false,
msg = "上传的Excel不包含数据"
}, "text/html");
}
List sheets = ExcelSheetName(filename);
if (sheets.Count == 0)
{
return Json(new
{
success = false,
msg = "上传的Excel不包含数据"
}, "text/html");
}
try
{
var sheetname = sheets[0];
string excelConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename +
";Extended Properties=Excel 8.0;";
OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "]", excelConn);
DataSet ds = new DataSet();
oada.Fill(ds);
if (ds.Tables.Count == 0)
{
return Json(new
{
success = false,
msg = "上传的Excel不包含数据"
}, "text/html");
}
var table = ds.Tables[0];
if (table.Rows.Count == 0 || table.Rows.Count == 1)
{
return Json(new
{
success = false,
msg = "上传的Excel不包含数据"
}, "text/html");
}
var customerFieldName = table.Columns[0].ColumnName;
if (customerFieldName != "委托单位")
{
return Json(new
{
success = false,
msg = "上传的Excel不是标准的格式,请重新选择"
}, "text/html");
}
var message = string.Empty;
List headList;
var isSucess = MsWlBs_HTDAL.ImportExcelData(Request, table, out message, out headList);
if (!isSucess)
{
return Json(new
{
success = false,
msg = message
}, "text/html");
}
return Json(new
{
success = true,
msg = "上传成功,共生成" + Convert.ToString(headList.Count)+"个托单",
dataList = headList.ToList()
}, "text/html");
}
catch (Exception)
{
return Json(new
{
success = false,
msg = "读取Excel文件出错,请确认文件正确性"
}, "text/html");
}
}
//TruckMng/MsWlBs/File
[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"];
if (file == null)
{
jsonRespose.Success = false;
jsonRespose.Message = "上传文件发生未知错误,请重新上传";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
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) };
}
string ext = Path.GetExtension(file.FileName).ToLower();
if (ext == ".asp" || ext == ".aspx")
{
jsonRespose.Success = false;
jsonRespose.Message = "不允许上传ASP或ASPX文件";
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) };
}
List 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;";
OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "]", 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 customerFieldName = table.Columns[0].ColumnName;
if ((customerFieldName != "委托单位") && (customerFieldName != "运单号"))
{
jsonRespose.Success = false;
jsonRespose.Message = "上传的Excel不是标准的格式,请重新选择";
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
var message = string.Empty;
List headList;
if (customerFieldName == "委托单位")
{
var isSucess = MsWlBs_HTDAL.ImportExcelData(Request, table, out message, out headList);
if (!isSucess)
{
jsonRespose.Success = false;
jsonRespose.Message = message;
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
}
else
{
var isSucess = MsWlBs_HTDAL.ImportExcelUpdateData(Request, table, out message, out headList);
if (!isSucess)
{
jsonRespose.Success = false;
jsonRespose.Message = message;
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
}
//jsonRespose.Success = true;
//jsonRespose.Message = "上传成功,共生成" + Convert.ToString(headList.Count) + "个托单";
//jsonRespose.Data = headList.ToList();
//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 ExcelSheetName(string filepath)
{
var al = new List();
try
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
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();
}
return al;
}
}
}