using System; using System.Linq; using System.Data; using System.Data.OleDb; using System.IO; using System.Web.Mvc; using DSWeb.MvcShipping.DAL.MsOpBulkDAL; using DSWeb.MvcShipping.Models.MsOpBulk; using DSWeb.MvcShipping.Helper; using DSWeb.MvcShipping.Comm.Cookie; using HcUtility.Comm; using HcUtility.Core; using System.Collections.Generic; using DSWeb.MvcShipping.DAL.MsBaseInfoDAL; namespace DSWeb.MvcShipping.Controllers { [JsonRequestBehavior] public class MsOpBulkController : Controller { // // GET: public ActionResult Index() { return View(); } public ActionResult HistryFeeIndex() { return View(); } // // GET: / public ActionResult Edit() { return View(); } public ActionResult StationKc() { return View(); } public ActionResult ApplyFeeView() { return View(); } public ActionResult StationKcDetail() { return View(); } public ActionResult StationKcModify() { return View(); } public ActionResult ReceiptEdit() { return View(); } public ActionResult PiLiangFeeEdit() { return View(); } public ActionResult FeeAmendEdit() { return View(); } // // GET: public ContentResult GetDataList(int start, int limit, string sort, string condition) { var dataList = MsOpBulkDAL.GetDataList(condition, Convert.ToString(Session["USERID"]), Convert.ToString(Session["SHOWNAME"]), 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 GetDataListStr(string sort, string condition) { var dataListStr = MsOpBulkDAL.GetDataListStr(condition, Convert.ToString(Session["USERID"]), Convert.ToString(Session["SHOWNAME"]), 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) { MsOpBulk head = null; if (handle == "edit") { head = MsOpBulkDAL.GetData(condition); } if (head == null) { head = new MsOpBulk(); head.OP = Convert.ToString(Session["SHOWNAME"]); } var json = JsonConvert.Serialize( new { Success = true, Message = "查询成功", data = head }); return new ContentResult() { Content = json }; } public ContentResult GetBodyList(string condition, string sort) { var dataList = MsOpBulkDAL.GetBodyList(condition,sort); var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = dataList.ToList() }); return new ContentResult() { Content = json }; } public ContentResult GetBodyListTrain(string condition, string sort) { var dataList = MsOpBulkDAL.GetBodyListTrain(condition, sort); var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = dataList.ToList() }); return new ContentResult() { Content = json }; } public ContentResult GetStationKc (string condition) { var data = MsOpBulkDAL.GetStationKc(condition); var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = 1, data = data }); return new ContentResult() { Content = json }; } public ContentResult GetStationKcList(string condition) { var data = MsOpBulkDAL.GetStationKcList(condition); var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = 1, data = data }); return new ContentResult() { Content = json }; } public ContentResult GetStationKcDetail(string condition, string sort) { var dataList = MsOpBulkDAL.GetStationKcDetail(condition, sort); var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = dataList.ToList() }); return new ContentResult() { Content = json }; } public ContentResult Save(string opstatus, string data, string data2) { var headData = JsonConvert.Deserialize(data.Replace("}", ",") + data2.Replace("{", "")); var isPost = true; var errorstr = ""; if(headData.BSNO=="*"){ headData.BSNO = Guid.NewGuid().ToString(); } if (headData.CUSTNO =="") { var isuse=MsBaseInfoDAL.GetRuleUse("委托编号","6"); if (isuse) { headData.CUSTNO = MsOpBulkDAL.getCodeRule("委托编号", "CUSTNO", headData.BSDATE.ToString().Trim(), headData.ACCDATE.ToString().Trim(), Convert.ToString(Session["USERID"]), Convert.ToString(Session["COMPANYID"])); } } if (headData.CORPID == ""||headData.CORPID ==null) { headData.CORPID = Convert.ToString(Session["COMPANYID"]); } if (opstatus == "add") { headData.INPUTBY = Convert.ToString(Session["SHOWNAME"]); headData.BSDATE = DateTime.Now; headData.DbOperationType = DbOperationType.DbotIns; } else if (opstatus == "edit") { headData.DbOperationType = DbOperationType.DbotUpd; headData.ModelUIStatus = "E"; } else { headData.DbOperationType = DbOperationType.DbotDel; } if (headData.ORDERNO != "" && headData.ORDERNO != null) { var ct = MsOpBulkDAL.GetRdCount("BSNO<>'" + headData.BSNO + "' AND ORDERNO='" + headData.ORDERNO + "'"); if (ct != 0) { isPost = false; errorstr = "订舱编号"; } } if (isPost) { var modb = new ModelObjectDB(); DBResult result = modb.Save(headData); var BSNO = headData.BSNO; if (result.Success == true) { // result = MsOpBulkDAL.SaveDetail(headData, bodyList, Convert.ToString(Session["USERID"])); } var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message, Data = MsOpBulkDAL.GetData("BSNO='" + BSNO + "'") }; 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 SaveDetail(string bsno, string body) { var bodyList = JsonConvert.Deserialize>(body); var result = new DBResult(); if (bodyList != null) { result = MsOpBulkDAL.SaveDetail(bsno, bodyList,false); if (result.Success) { MsOpBulkDAL.p_update_OVERKGS(bsno); } } var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message, // Data = MsChFeeDAL.GetDataList(headData.BillNo, Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), CookieConfig.GetCookie_OrgCode(Request)) }; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } public ContentResult AuditDetail(string bsno,bool audit) { var result = new DBResult(); result = MsOpBulkDAL.AuditDetail(bsno, audit); if (result.Success) { MsOpBulkDAL.p_update_OVERKGS(bsno); } var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message, // Data = MsChFeeDAL.GetDataList(headData.BillNo, Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), CookieConfig.GetCookie_OrgCode(Request)) }; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } public ContentResult DeleteDetail(string bsno,string body) { var bodyList = JsonConvert.Deserialize>(body); var result = new DBResult(); if (bodyList != null) { result = MsOpBulkDAL.DeleteDetail(bodyList); if (result.Success) { MsOpBulkDAL.p_update_OVERKGS(bsno); } } var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message, // Data = MsChFeeDAL.GetDataList(headData.BillNo, Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), CookieConfig.GetCookie_OrgCode(Request)) }; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } public ContentResult SaveTrainDetail(string bsno, string body) { var bodyList = JsonConvert.Deserialize>(body); var result = new DBResult(); if (bodyList != null) { result = MsOpBulkDAL.SaveTrainDetail(bsno, bodyList,false); if (result.Success) { MsOpBulkDAL.p_update_OVERKGS(bsno); } } var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message, // Data = MsChFeeDAL.GetDataList(headData.BillNo, Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), CookieConfig.GetCookie_OrgCode(Request)) }; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } public ContentResult AuditTrainDetail(string bsno, bool audit) { var result = new DBResult(); result = MsOpBulkDAL.AuditTrainDetail(bsno, audit); if (result.Success) { MsOpBulkDAL.p_update_OVERKGS(bsno); } var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message, // Data = MsChFeeDAL.GetDataList(headData.BillNo, Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), CookieConfig.GetCookie_OrgCode(Request)) }; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } public ContentResult DeleteTrainDetail(string bsno,string body) { var bodyList = JsonConvert.Deserialize>(body); var result = new DBResult(); if (bodyList != null) { result = MsOpBulkDAL.DeleteTrainDetail(bodyList); if (result.Success) { MsOpBulkDAL.p_update_OVERKGS(bsno); } } var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message, // Data = MsChFeeDAL.GetDataList(headData.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 isfee = MsOpBulkDAL.GetFeeCount(head.BSNO); if (isfee) { var jsonRespose = new JsonResponse { Success = false, Message ="此票业务存在费用,请先删除费用才能删除此票业务!" }; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } else { var modb = new ModelObjectDB(); DBResult result = modb.Delete(head); if (result.Success == true) { MsOpBulkDAL.DeleteDetail(head); } var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message }; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } } public ContentResult GetKcModifyData(string handle, string condition) { MsOpBulkKcModify head = null; if (handle == "edit") { head = MsOpBulkDAL.GetKcModifyData(condition); } if (head == null) { head = new MsOpBulkKcModify(); head.OP = Convert.ToString(Session["SHOWNAME"]); } var json = JsonConvert.Serialize( new { Success = true, Message = "查询成功", data = head }); return new ContentResult() { Content = json }; } public ContentResult SaveKcModify(string opstatus, string data) { var headData = JsonConvert.Deserialize(data); if (opstatus == "add") { headData.OP = Convert.ToString(Session["SHOWNAME"]); headData.DbOperationType = DbOperationType.DbotIns; } var modb = new ModelObjectDB(); DBResult result = modb.Save(headData); var BSNO = headData.GID; var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message, Data = MsOpBulkDAL.GetKcModifyData("GID='" + BSNO + "'") }; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } public ContentResult DeleteKcModify(string data) { var bodyList = JsonConvert.Deserialize(data); var result = new DBResult(); if (bodyList != null) { result = MsOpBulkDAL.DeleteKcModifyDetail(bodyList); } var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message, // Data = MsChFeeDAL.GetDataList(headData.BillNo, Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), CookieConfig.GetCookie_OrgCode(Request)) }; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } [HttpPost] public ContentResult ImportDetail() { 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) }; } 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/BulkDetail"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var size = file.ContentLength; var name = Path.GetFileName(file.FileName); var bsno = Request.Form["bsno"]; var updatetype=Request.Form["updatetype"]; 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;HDR=Yes;IMEX=1'"; 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 || 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 != "日期") //{ // jsonRespose.Success = false; // jsonRespose.Message = "上传的Excel不是标准的格式,请重新选择"; // return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; //} var message = string.Empty; List headList; bool isSucess = false; if (updatetype == "1") { isSucess = MsOpBulkDAL.ImportDetailData(bsno, Request, table, out message, out headList); } else if (updatetype == "2") { isSucess = MsOpBulkDAL.ImportTruckDetailData(bsno, Request, table, out message, out headList); } else { isSucess = MsOpBulkDAL.ImportTrainDetailData(bsno, 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;HDR=Yes;IMEX=1'"; 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(); 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; } #region 参照部分 #endregion } }