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.

563 lines
21 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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 DSWeb.Areas.TruckMng.Models.MsWlPc;
using DSWeb.Areas.TruckMng.Models.Comm;
using HcUtility.Comm;
using HcUtility.Core;
using DSWeb.Areas.TruckMng.DAL.MsWl_Haier;
using System.IO;
using System.Data.OleDb;
namespace DSWeb.Areas.TruckMng.Controllers
{
/// <summary>
/// 路单查询
/// </summary>
[JsonRequestBehavior]
public class MsWl_HaierMainController : Controller
{
//
// GET: /RptMng/MsRptPcHeadQry
public ActionResult Index()
{
return View();
}
//
// GET/RptMng/MsRptPcHeadQry/QryData
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 = 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 };
}
static public List<MsWl_HaierMain> GetDataList(string condition, string sort)
{
var strSql = new StringBuilder();
strSql.Append(" SELECT m.GID,ACCDATE,TruckNo,m.DrvCode,d.DrvName,TotalMil,FuelPrice,FuelAmount,XXF,EBF,DZXF,LTSH,LQF,LTKF,RGF,YLS ");
strSql.Append(" ,LCJL,Amount_1,Tax,ZFJL,ZCGL,YXCB,YSSR,CLBX,CLZJ,Amount_2,CLJL,CLSJ,AQKH,Amount_3 ");
strSql.Append(" from tmswl_haierMain m");
strSql.Append(" left join tmswldriver d on d.drvcode=m.drvcode");
if (!string.IsNullOrEmpty(condition))
{
strSql.Append(" where " + condition.Replace("~", ""));//str.Replace("b","**");
}
var sortstring = DatasetSort.Getsortstring(sort);
if (!string.IsNullOrEmpty(sortstring))
{
strSql.Append(" order by " + sortstring);
}
else
{
strSql.Append(" order by ACCDATE,TruckNo ");
}
return SetData(strSql);
}
/*
public ContentResult GetData(string condition)
{
MsWlPcHead head = null;
var _t = "";
var list = GetDataList(condition, _t);
if (list.Count > 0)
head = list[0];
if (head == null)
{
head = new MsWlPcHead();
}
var json = JsonConvert.Serialize(
new { Success = true, Message = "查询成功", data = head });
return new ContentResult() { Content = json };
}*/
static public MsWl_HaierMain GetData(string condition)
{
var _t = "";
var list = GetDataList(condition, _t);
if (list.Count > 0)
return list[0];
return new MsWl_HaierMain();
}
private static List<MsWl_HaierMain> SetData(StringBuilder strSql)
{
var headList = new List<MsWl_HaierMain>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
MsWl_HaierMain data = new MsWl_HaierMain();
#region Set DB data to Object
data.GID = Convert.ToString(reader["GID"]);
data.ACCDATE = Convert.ToString(reader["ACCDATE"]);
data.TruckNo = Convert.ToString(reader["TruckNo"]);
data.DrvCode = Convert.ToString(reader["DrvCode"]);
data.DrvName = Convert.ToString(reader["DrvName"]);
data.TotalMil = Convert.ToString(reader["TotalMil"]);
data.FuelPrice = Convert.ToString(reader["FuelPrice"]);
data.FuelAmount = Convert.ToString(reader["FuelAmount"]);
data.XXF = Convert.ToString(reader["XXF"]);
data.EBF = Convert.ToString(reader["EBF"]);
data.DZXF = Convert.ToString(reader["DZXF"]);
data.LTSH = Convert.ToString(reader["LTSH"]);
data.LQF = Convert.ToString(reader["LQF"]);
data.LTKF = Convert.ToString(reader["LTKF"]);
data.RGF = Convert.ToString(reader["RGF"]);
data.YLS = Convert.ToString(reader["YLS"]);
data.LCJL = Convert.ToString(reader["LCJL"]);
data.Amount_1 = Convert.ToString(reader["Amount_1"]);
data.Tax = Convert.ToString(reader["Tax"]);
data.ZFJL = Convert.ToString(reader["ZFJL"]);
data.ZCGL = Convert.ToString(reader["ZCGL"]);
data.YXCB = Convert.ToString(reader["YXCB"]);
data.YSSR = Convert.ToString(reader["YSSR"]);
data.CLBX = Convert.ToString(reader["CLBX"]);
data.CLZJ = Convert.ToString(reader["CLZJ"]);
data.Amount_2 = Convert.ToString(reader["Amount_2"]);
data.CLJL = Convert.ToString(reader["CLJL"]);
data.CLSJ = Convert.ToString(reader["CLSJ"]);
data.AQKH = Convert.ToString(reader["AQKH"]);
data.Amount_3 = Convert.ToString(reader["Amount_3"]);
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
/*
public ContentResult CreateHM(string data)
{
var head = JsonConvert.Deserialize<MsWl_HaierMain>(data);
head.DbOperationType = DbOperationType.DbotIns;
head.ModelUIStatus = "I";
head.BillNo = PubSysDAL.GetBillNo("0111"); //获取路单号
head.RefBillNo = head.BillNo;
if (!string.IsNullOrEmpty(head.ArriveDate))
{
head.ArriveDate = head.ArriveDate.Substring(0, 10);
}
//head.PcBillType = '0';
var modb = new ModelObjectRepository();
DBResult result = modb.Save(head);
//var USERID = Convert.ToString(CookieConfig.GetCookie_UserId(Request));
//var USERCODE = Convert.ToString(CookieConfig.GetCookie_UserCode(Request));
//var ORGCODE = Convert.ToString(CookieConfig.GetCookie_OrgCode(Request));
//刷新父窗口上的父节点
var _r = GetData(" billno= '" + head.BillNo + "'") ;
var jsonRespose = new JsonResponse
{
Success = result.Success,
Message = result.Message,
Data = _r
};
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
} * */
/*
public ContentResult DeleteHM(string data)
{
var head = JsonConvert.Deserialize<MsWlPcHead>(data);
head.DbOperationType = DbOperationType.DbotIns;
head.ModelUIStatus = "D";
var modb = new ModelObjectDB();
DBResult result = modb.Delete(head);
var jsonRespose = new JsonResponse
{
Success = result.Success,
Message = result.Message,
Data = null
};
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
} * */
/*
public ContentResult SaveHM(string data)
{
var head = JsonConvert.Deserialize<MsWlPcHead>(data);
head.DbOperationType = DbOperationType.DbotUpd;
head.ModelUIStatus = "E";
if (!string.IsNullOrEmpty(head.ArriveDate))
{
head.ArriveDate=head.ArriveDate.Substring(0,10);
}
var modb = new ModelObjectRepository();
DBResult result = modb.Save(head);
//var USERID = Convert.ToString(CookieConfig.GetCookie_UserId(Request));
//var USERCODE = Convert.ToString(CookieConfig.GetCookie_UserCode(Request));
//var ORGCODE = Convert.ToString(CookieConfig.GetCookie_OrgCode(Request));
//刷新父窗口上的父节点
var jsonRespose = new JsonResponse
{
Success = result.Success,
Message = result.Message,
Data = GetData(" billno= '" + head.BillNo + "'")
};
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
*/
public int SetFee(string GIDList,string FeeName,string Amount, string Type)
{
var strSql = new StringBuilder();
var userid = Convert.ToString(Session["USERID"]);
var _GIDList = JsonConvert.Deserialize<List<string>>(GIDList);
var _count = 0;
if (Type == "1")
{
foreach (var GID in _GIDList)
{
strSql.Append(" if ((select count(*) _count from ch_fee where FEETYPE=1 ");
strSql.Append(" and FEENAME='" + FeeName + "' and amount=" + Amount + " and bsno='" + GID + "')=0) ");
strSql.Append(" BEGIN ");
strSql.Append(" insert into ch_fee (gid,bsno,feetype,feename,unitprice,quantity,AMOUNT,customername,remark,enteroperator) ");
strSql.Append(" VALUES(newid(),'" + GID + "',1,'" + FeeName + "'," + Amount + ",1," + Amount + ",'华泰',dbo.trimdate(getdate())+'批量添加','" + userid + "') ");
strSql.Append(" END ");
_count = _count + DoSQL(strSql);
}
}
else if (Type == "0")
{
foreach (var GID in _GIDList)
{
strSql.Append(" delete from ch_fee where FEENAME='" + FeeName + "' and amount=" + Amount + " and bsno='" + GID + "' ");
_count = _count + DoSQL(strSql);
}
}
return _count;//ExecuteNonQuery
}
private static int DoSQL(StringBuilder strSql)
{
var _count = 0;
Database db = DatabaseFactory.CreateDatabase();
_count = db.ExecuteNonQuery(CommandType.Text, strSql.ToString());
return _count;
}
[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 ACCDATE = Request.Form["ACCDATE"].ToString().Trim();
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<string> sheets = ExcelSheetName(filename);
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=Excel 8.0;";
OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "A3: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 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<MsWl_HaierMain> headList;
//if (customerFieldName == "委托单位")
//{
var InsertCount = 0;
var UpdateCount = 0;
var UnKnowenTruckNo = "";
var isSucess = MsWl_HaierDAL.ImportExcelData(Request, table, out message, out InsertCount, out UpdateCount,ACCDATE, out UnKnowenTruckNo, out headList);
if (!isSucess)
{
jsonRespose.Success = false;
jsonRespose.Message = message;
return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
}
/*}
else
{
var isSucess = MsWl_HH_ZYDAL.ImportExcelUpdateData(Request, table, 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) + "条记录,其中新增" + 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)
{
var al = new List<string>();
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<string>();
}
return al;
}
#region 参照部分
#endregion
}
}