using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using DSWeb.MvcShipping.Models.OpFenDetail;
using DSWeb.MvcShipping.DAL.MsOpFenDetail;
using DSWeb.MvcShipping.Helper;
using DSWeb.TruckMng.Comm.Cookie;
using HcUtility.Comm;
using HcUtility.Core;
namespace DSWeb.MvcShipping.Controllers
{
[JsonRequestBehavior]
public class MsOpFenDetailController : Controller
{
///
/// 视图
///
///
public ActionResult Index()
{
return View();
}
///
/// 获取分页列表
///
///
///
///
///
///
public ContentResult GetList(int start, int limit, string condition, string sort)
{
var dataList = MsOpFenDetailDAL.GetListByPage(condition, sort, start, start + limit).OrderByDescending(p=>p.FXCOLNO).ThenBy(p=>p.FXNO).ToList();
int recordCount = MsOpFenDetailDAL.GetRecordCount(condition);
var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = recordCount, data = dataList.ToList() });
return new ContentResult { Content = json };
}
///
/// 获取分页列表
///
///
public JsonResult GetListAll()
{
var dataList = MsOpFenDetailDAL.GetListByPage("GID IN (SELECT MIN(GID) FROM op_ctn_fendetail WHERE IOMARK=1 GROUP BY FXCOLNO)", "FXCOLNO DESC", 0,2000).OrderByDescending(p => p.FXCOLNO).ThenBy(p => p.FXNO).ToList();
return Json(new { success = true, data = dataList });
}
///
/// 获取实例
///
///
///
///
public ContentResult GetData(string handle, string id)
{
var model = MsOpFenDetailDAL.GetData(id);
var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", data = model });
return new ContentResult { Content = json };
}
///
/// 批量删除
///
///
///
public ContentResult Delete(string data)
{
var datalist = JsonConvert.Deserialize>(data);
var modb = new ModelObjectDB();
DBResult result = new DBDataSetResult();
var jsonRespose = new JsonResponse { Success = false, Message = "删除失败" };
foreach (var item in datalist)
{
result = modb.Delete(item);
if(!result.Success)//有失败直接返回结果
return new ContentResult { Content = JsonConvert.Serialize(jsonRespose) };
}
jsonRespose = new JsonResponse { Success = true, Message = "删除成功" };
return new ContentResult { Content = JsonConvert.Serialize(jsonRespose) };
}
///
/// 批量保存
///
///
///
public ContentResult Save(string body)
{
var bodyList = JsonConvert.Deserialize>(body);
foreach (var item in bodyList)
{
item.OPERUSER = Session["SHOWNAME"].ToString();//最近操作人
item.OPERTIME = DateTime.Now;
if (String.IsNullOrEmpty(item.GID))
{
item.GID = Guid.NewGuid().ToString();
MsOpFenDetailDAL.Add(item);
}
else
MsOpFenDetailDAL.Update(item);
}
var jsonRespose = new JsonResponse { Success = true, Message = "保存成功" };
return new ContentResult { Content = JsonConvert.Serialize(jsonRespose) };
}
///
/// 导入
///
///
public ContentResult FileUpload()
{
var jsonRespose = JsonConvert.Serialize(new { success = false ,message="未上传文件"});
try
{
var file = Request.Files["file"];//获取文件
if (file == null)//未上传文件
return new ContentResult { Content = jsonRespose };
var path = Server.MapPath("../../UploadFiles/CtnTkDetail");//获取物理路径
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
var name = Path.GetFileName(file.FileName);
var usercode = CookieConfig.GetCookie_UserCode(Request);
string filepath = path + "\\" + usercode + DateTime.Now.ToString("yyyyMMddHHmmssfff") + name;
if (System.IO.File.Exists(filepath))
System.IO.File.Delete(filepath);
file.SaveAs(filepath);
DataSet ds = GetExcelDs(filepath);
if (ds.Tables.Count > 0)
{
DataTable dt = ds.Tables[0];
//修改导入的列名为字段名
dt.Columns[0].ColumnName = "COILNO";
dt.Columns[1].ColumnName = "SIZE";
dt.Columns[2].ColumnName = "COLOUR";
dt.Columns[3].ColumnName = "NWKGS";
dt.Columns[4].ColumnName = "GWKGS";
dt.Columns[5].ColumnName = "NLENGTH";
dt.Columns.Add(new DataColumn() {ColumnName = "OPERTIME" });
var list = ToJson(dt);
jsonRespose = JsonConvert.Serialize(new
{
success = true,
data = list,
totalCount = dt.Rows.Count,
message = "导入成功"
});
}
return new ContentResult { Content = jsonRespose };
}
catch (Exception se)
{
jsonRespose = JsonConvert.Serialize(new
{
success = false,
message = se.Message
});
return new ContentResult { Content = jsonRespose };
}
}
///
/// 获取DataSet
///
///
///
public DataSet GetExcelDs(string filepath)
{
try
{
var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
if (filepath.ToLower().IndexOf(".xlsx", StringComparison.Ordinal) > 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 dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
conn.Close();
if (dt != null)
{
OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + dt.Rows[0][2] + "]", strConn);
DataSet ds = new DataSet();
oada.Fill(ds);
return ds;
}
return new DataSet();
}
catch (Exception)
{
return new DataSet();
}
}
///
/// DataTable 对象 转换为Json 字符串
///
///
///
public static string ToJson(DataTable dt)
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = new ArrayList();
foreach (DataRow dataRow in dt.Rows)
{
Dictionary dictionary = new Dictionary();//实例化一个参数集合
foreach (DataColumn dataColumn in dt.Columns)
{
if (dataColumn.ColumnName == "OPERTIME")
dataRow[dataColumn.ColumnName] = DateTime.Now.ToString();
dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToString());
}
arrayList.Add(dictionary); //ArrayList集合中添加键值
}
return javaScriptSerializer.Serialize(arrayList);//返回一个json字符串
}
///
/// 分箱操作(加分箱批号)
///
///
///
///
public ContentResult FenXiangOpera(string data, decimal limit,string xmodel)
{
JsonResponse json;
try
{
List datalist = JsonConvert.Deserialize>(data);
var groupItems = datalist.GroupBy(p => p.COLOUR).ToList();//按颜色分组
int recordCount = MsOpFenDetailDAL.GetRecordCount("gid in(select min(gid) from op_ctn_fendetail where fxmark=1 group by FXCOLNO)");//统计已分箱的业务编号
var nowDate = DateTime.Now;//当前时间
var fxcolno = "FX" + nowDate.Year.ToString().Substring(2, 2) + nowDate.Month.ToString().PadLeft(2, '0') + (recordCount + 1).ToString().PadLeft(4, '0');//业务编号(BSNO)/分箱批次号
int fxno = 0;//分箱编号
List notGroupList = new List();//未分箱集合
//遍历分组后的泛型(按颜色分组)
foreach (var item in groupItems)
{
var readyList = RecuFx(item.ToList(),limit,fxno, fxcolno,xmodel,0);
notGroupList.AddRange(readyList.Values.First());//不够一箱的添加到未分箱集合中,下次分箱
fxno = readyList.Keys.First();
}
var linqList = notGroupList.GroupBy(p => p.COLOUR).Select(t => new
{
key=t.Key,
list=t.ToList(),
count = t.Count()
}).OrderByDescending(q => q.count).ToList();//p:按颜色排序;t:实例;q:按数量排序
//未分箱的集合递归继续分箱
notGroupList=new List();
foreach (var item in linqList)
notGroupList.AddRange(item.list);
//杂色分箱处理
RecuFx(notGroupList, limit, fxno, fxcolno, xmodel,1);
json = new JsonResponse { Success = true, Message = "分箱成功" };
return new ContentResult { Content = JsonConvert.Serialize(json) };
}
catch (Exception e)
{
json = new JsonResponse { Success = false, Message = e.Message };
return new ContentResult { Content = JsonConvert.Serialize(json) };
}
}
///
/// 分箱功能
///
///
///
///
///
///
/// 类型0:按颜色分箱,1:混色分箱
///
public Dictionary> RecuFx(List rlist,decimal limit,int fxno,string fxcolno,string xmodel,int type)
{
decimal? sumWeight = 0.000M;//当前钢卷总毛重
List readyList = new List();//待分箱集合
//遍历某种颜色集合
foreach (var item1 in rlist)
{
readyList.Add(item1);//添加到待分箱集合中
//在误差范围内,执行分箱操作
if (limit >= sumWeight && limit <= item1.GWKGS + sumWeight)
{
if (limit < item1.GWKGS + sumWeight)
readyList.Remove(item1);
fxno++;//分箱编号进1位
//遍历待分箱集合进行分箱操作
foreach (var item2 in readyList)
{
item2.FXNO = fxno.ToString().PadLeft(2, '0') + "#";//分箱编号
item2.FXMARK = 1;//是否分箱标识
item2.XMODEL = xmodel;//箱型
item2.LIKGS = limit; //实际限重
item2.FXCOLNO = fxcolno;//分箱批次号
if (String.IsNullOrEmpty(item2.GID))
{
item2.GID = Guid.NewGuid().ToString();
MsOpFenDetailDAL.Add(item2);
}
else
MsOpFenDetailDAL.Update(item2);
}
readyList = new List();
if (limit < item1.GWKGS + sumWeight)
readyList.Add(item1);
sumWeight = 0.000M;//初始化钢卷总毛重
}
sumWeight += item1.GWKGS;//累加钢卷毛重
}
if (type == 1)//混色分箱最后不够一箱的分到一箱
{
fxno++;//分箱编号进1位
//遍历待分箱集合进行分箱操作
foreach (var item2 in readyList)
{
item2.FXNO = fxno.ToString().PadLeft(2, '0') + "#";//分箱编号
item2.FXMARK = 1;//是否分箱标识
item2.XMODEL = xmodel;//箱型
item2.LIKGS = limit; //实际限重
item2.FXCOLNO = fxcolno;//分箱批次号
if (String.IsNullOrEmpty(item2.GID))
{
item2.GID = Guid.NewGuid().ToString();
MsOpFenDetailDAL.Add(item2);
}
else
MsOpFenDetailDAL.Update(item2);
}
}
//公共部分
Dictionary < Int32, List < OpFenDetail >> dict=new Dictionary>();
dict.Add(fxno, readyList);
return dict;
}
///
/// 更新业务编号
///
///
public void UpdateImport(string bsno)
{
MsOpFenDetailDAL.UpdatebyBsno(bsno);
}
}
}