using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Web.Mvc ;
using DSWeb.Areas.TruckMng.Models.Comm ;
using DSWeb.Areas.CommMng.DAL ;
using DSWeb.Areas.Import.DAL.XXH ;
using DSWeb.Areas.Import.Models.XXH ;
using DSWeb.TruckMng.Comm.Cookie ;
using DSWeb.TruckMng.Helper ;
using DSWeb.TruckMng.Helper.Repository ;
using DSWeb.Areas.TruckMng.DAL.Comm ;
using HcUtility.Comm ;
using HcUtility.Core ;
using System.Data ;
using System.Data.OleDb ;
using System.IO ;
using System.Text ;
using DSWeb.SoftMng.Filter ;
using System.Web.UI.MobileControls ;
namespace DSWeb.Areas.Import.Controllers
{
[JsonRequestBehavior]
public class XXH_singleController : Controller
{
/ /
// GET: /Import/XXH_single
public ActionResult Index ( )
{
return View ( ) ;
}
/ /
// GET: /Import/XXH/Edit
public ActionResult Edit ( )
{
return View ( ) ;
}
[SqlKeyWordsFilter(Type = "Action")] //sql 防注入过滤器
public ContentResult GetDataList ( int start , int limit , string sort , string condition )
{
string conditionStr = condition ;
string uid = Session [ "USERID" ] = = null ? "" : Session [ "USERID" ] . ToString ( ) ;
if ( uid ! = "" )
{
conditionStr + = " and (M.agent = (select COMPANYID from user_company where USERID = '" + uid + "') or (select c.CODENAME from user_company uc join company c on c.GID=uc.COMPANYID where USERID = '" + uid + "' )='XXH')" ;
}
int total = 0 ;
var dataList = XXHDAL . GetDataList_single ( conditionStr , sort , uid , start , limit , out total ) ;
//var list = dataList.Skip(start).Take(limit);
var json = JsonConvert . Serialize ( new { Success = true , Message = "查询成功" , totalCount = total , data = dataList . ToList ( ) } ) ;
return new ContentResult ( ) { Content = json } ;
}
[SqlKeyWordsFilter(Type = "Action")] //sql 防注入过滤器
public ContentResult GetData ( string handle , string condition )
{
XXH_singlemb head = null ;
string uid = Session [ "USERID" ] = = null ? "" : Session [ "USERID" ] . ToString ( ) ;
if ( handle = = "edit" )
{
var _t = "" ;
int total = 0 ;
var list = XXHDAL . GetDataList_single ( condition , _t , uid , 0 , 1 , out total ) ;
if ( list . Count > 0 )
head = list [ 0 ] ;
}
if ( head = = null )
{
head = new XXH_singlemb ( ) ;
}
var json = JsonConvert . Serialize (
new { Success = true , Message = "查询成功" , data = head } ) ;
return new ContentResult ( ) { Content = json } ;
}
public ContentResult Save ( string opstatus , string data ,
string Receiptbody , string Receiptdelbody ,
string KCBody , string KCDelBody , string chfeebody , string chfeedelbody )
{
var head = JsonConvert . Deserialize < XXH_singlemb > ( data ) ;
var ReceiptbodyList = JsonConvert . Deserialize < List < Receiptmb > > ( Receiptbody ) ;
var ReceiptdelbodyList = JsonConvert . Deserialize < List < Receiptmb > > ( Receiptdelbody ) ;
//var CargobodyList = JsonConvert.Deserialize<List<Cargomb>>(CargoBody);
//var CargodelbodyList = JsonConvert.Deserialize<List<Cargomb>>(CargoDelBody);
var KCBodyList = JsonConvert . Deserialize < List < KCmb > > ( KCBody ) ;
var KCDelBodyList = JsonConvert . Deserialize < List < KCmb > > ( KCDelBody ) ;
var chfeeBodyList = JsonConvert . Deserialize < List < MsChFee > > ( chfeebody ) ;
var chfeeBodyDelList = JsonConvert . Deserialize < List < MsChFee > > ( chfeedelbody ) ;
if ( chfeeBodyList ! = null )
{
foreach ( var _fee in chfeeBodyList )
{
_fee . MODIFIEDTIME = DateTime . Now . ToString ( ) ;
_fee . MODIFIEDUSER = Session [ "USERID" ] . ToString ( ) ;
}
}
if ( opstatus = = "add" )
{
head . DbOperationType = DbOperationType . DbotIns ;
head . ModelUIStatus = "I" ;
head . ContractNo = PubSysDAL . GetBillNo ( "0201" ) ; //获取合同号
//20131011 应客户要求,将合同号改为接单人手动填写的必填项目。不再自动生成合同号
/ * 自 动 填 写 接 单 审 单 人 id
headData . UserCode = CookieConfig . GetCookie_UserCode ( Request ) ;
headData . UserName = CookieConfig . GetCookie_UserName ( Request ) ;
headData . OrgCode = CookieConfig . GetCookie_OrgCode ( Request ) ;
headData . OrgName = CookieConfig . GetCookie_OrgName ( Request ) ; * /
//headData.LrDate = DateTime.Now;
}
else if ( opstatus = = "edit" )
{
head . DbOperationType = DbOperationType . DbotUpd ;
head . ModelUIStatus = "E" ;
}
else
{
head . DbOperationType = DbOperationType . DbotDel ;
}
var modb = new ModelObjectRepository ( ) ;
head . MODIFIEDUSER = Session [ "SHOWNAME" ] . ToString ( ) ;
head . MODIFIEDTIME = System . DateTime . Now . ToString ( ) ;
DBResult result = modb . Save ( head ,
ModelObjectConvert < Receiptmb > . ToModelObjectList ( ReceiptbodyList ) ,
ModelObjectConvert < Receiptmb > . ToModelObjectList ( ReceiptdelbodyList ) ,
//ModelObjectConvert<Cargomb>.ToModelObjectList(CargobodyList),
//ModelObjectConvert<Cargomb>.ToModelObjectList(CargodelbodyList),
ModelObjectConvert < KCmb > . ToModelObjectList ( KCBodyList ) ,
ModelObjectConvert < KCmb > . ToModelObjectList ( KCDelBodyList ) ,
ModelObjectConvert < MsChFee > . ToModelObjectList ( chfeeBodyList ) ,
ModelObjectConvert < MsChFee > . ToModelObjectList ( chfeeBodyDelList )
) ;
BasicDataRefDAL . SaveLog ( head , Session [ "USERID" ] . ToString ( ) , "进口贸易_天津共享" , "保存" ) ;
//刷新父窗口上的父节点
var jsonRespose = new JsonResponse
{
Success = result . Success ,
Message = result . Message ,
Data = XXHDAL . GetData ( "M.ContractNo='" + head . ContractNo + "'" , CookieConfig . GetCookie_UserCode ( Request ) , CookieConfig . GetCookie_UserName ( Request ) , CookieConfig . GetCookie_CompanyId ( Request ) )
} ;
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" ] ;
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 + "A1:L]" , 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 customerFieldName = table . Columns [ 0 ] . ColumnName ;
if ( customerFieldName ! = "订单列表" )
{
jsonRespose . Success = false ;
jsonRespose . Message = "上传的Excel不是标准的格式, 请重新选择" ;
return new ContentResult ( ) { Content = JsonConvert . Serialize ( jsonRespose ) } ;
} * /
var message = string . Empty ;
var headList = new List < String > ( ) ;
//if (customerFieldName == "委托单位")
//{
var InsertCount = 0 ;
var UpdateCount = 0 ;
var UnknownKF = "" ;
var isSucess = XXHDAL . ImportExcelData ( Request , table , out message , out InsertCount , out UpdateCount , out UnknownKF ) ;
if ( ! isSucess )
{
jsonRespose . Success = false ;
jsonRespose . Message = message ;
return new ContentResult ( ) { Content = JsonConvert . Serialize ( jsonRespose ) } ;
}
var msg = "上传成功,共处理" + Convert . ToString ( InsertCount + UpdateCount ) + "个托单,其中新增" + Convert . ToString ( InsertCount )
+ "个,更新" + Convert . ToString ( UpdateCount ) + "个。" ;
if ( ! string . IsNullOrEmpty ( UnknownKF ) ) {
msg = msg + UnknownKF ;
}
var json = JsonConvert . Serialize ( new
{
success = true ,
Message = msg
} ) ;
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;" ;
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 ;
}
}
}