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.

350 lines
15 KiB
C#

9 months ago
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
{
/// <summary>
/// 视图
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
return View();
}
/// <summary>
/// 获取分页列表
/// </summary>
/// <param name="start"></param>
/// <param name="limit"></param>
/// <param name="condition"></param>
/// <param name="sort"></param>
/// <returns></returns>
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 };
}
/// <summary>
/// 获取分页列表
/// </summary>
/// <returns></returns>
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 });
}
/// <summary>
/// 获取实例
/// </summary>
/// <param name="handle"></param>
/// <param name="id"></param>
/// <returns></returns>
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 };
}
/// <summary>
/// 批量删除
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public ContentResult Delete(string data)
{
var datalist = JsonConvert.Deserialize<List<OpFenDetail>>(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) };
}
/// <summary>
/// 批量保存
/// </summary>
/// <param name="body"></param>
/// <returns></returns>
public ContentResult Save(string body)
{
var bodyList = JsonConvert.Deserialize<List<OpFenDetail>>(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) };
}
/// <summary>
/// 导入
/// </summary>
/// <returns></returns>
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 };
}
}
/// <summary>
/// 获取DataSet
/// </summary>
/// <param name="filepath"></param>
/// <returns></returns>
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();
}
}
/// <summary>
/// DataTable 对象 转换为Json 字符串
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
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<string, object> dictionary = new Dictionary<string, object>();//实例化一个参数集合
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字符串
}
/// <summary>
/// 分箱操作(加分箱批号)
/// </summary>
/// <param name="data"></param>
/// <param name="limit"></param>
/// <returns></returns>
public ContentResult FenXiangOpera(string data, decimal limit,string xmodel)
{
JsonResponse json;
try
{
List<OpFenDetail> datalist = JsonConvert.Deserialize<List<OpFenDetail>>(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<OpFenDetail> notGroupList = new List<OpFenDetail>();//未分箱集合
//遍历分组后的泛型(按颜色分组)
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<OpFenDetail>();
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) };
}
}
/// <summary>
/// 分箱功能
/// </summary>
/// <param name="rlist"></param>
/// <param name="limit"></param>
/// <param name="fxno"></param>
/// <param name="fxcolno"></param>
/// <param name="xmodel"></param>
/// <param name="type">类型0按颜色分箱1混色分箱</param>
/// <returns></returns>
public Dictionary<Int32, List<OpFenDetail>> RecuFx(List<OpFenDetail> rlist,decimal limit,int fxno,string fxcolno,string xmodel,int type)
{
decimal? sumWeight = 0.000M;//当前钢卷总毛重
List<OpFenDetail> readyList = new List<OpFenDetail>();//待分箱集合
//遍历某种颜色集合
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<OpFenDetail>();
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<int, List<OpFenDetail>>();
dict.Add(fxno, readyList);
return dict;
}
/// <summary>
/// 更新业务编号
/// </summary>
/// <param name="bsno"></param>
public void UpdateImport(string bsno)
{
MsOpFenDetailDAL.UpdatebyBsno(bsno);
}
}
}