using iWareCommon.Utils;
using IWareDataAccess.EF;
using IWareDataAccess.Entity.Base;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace IWareDataAccess.Helper
{
public static class Helper
{
///
/// 删除托盘和物料绑定关系 记录日志
///
///
public static void LogRemoveBASE_CONTAINER_VS_ITEM(List cviList, string funName, string remark)
{
foreach (var item in cviList)
{
LogRemoveBASE_CONTAINER_VS_ITEM(item, funName, remark);
}
}
///
/// 删除托盘和物料绑定关系 记录日志
///
///
public static void LogRemoveBASE_CONTAINER_VS_ITEM(BASE_CONTAINER_VS_ITEM item, string funName, string remark)
{
var logTitle = "方法名:" + funName + ",删除原因:" + remark + ",";
try
{
//记录删除托盘和物料绑定关系日志 【Editby shaoc,2023-03-07】
Log4NetHelper.WriteInfoLog(LogType.RemoveBASE_CONTAINER_VS_ITEM,
logTitle +
"删除托盘和物料绑定关系 成功,cvi_id:" + item.ID + ",ITEMID:" + item.ITEMID + ",CONTAINERID:" + item.CONTAINERID
+ ",CONTAINERNAME:" + item.BASE_CONTAINER.CONTAINERNAME
+ ",ITEMNAME:" + item.BASE_ITEM.ITEMNAME
);
}
catch (Exception ex)
{
Log4NetHelper.WriteErrorLog(LogType.RemoveBASE_CONTAINER_VS_ITEM, logTitle + "记录删除托盘和物料关系失败:" + ex.Message + ",cvi_id:" + item.ID, ex);
}
}
///
/// 计算 冲压计划单号的数量
///
///
///
///
public static void CalcInOrderNumPub(string oldCviCode, Model edm, BASE_CONTAINER_VS_ITEM cvi)
{
//计算 【EditBy shaocx,2022-11-08】
if (string.IsNullOrEmpty(oldCviCode))
{
if (!string.IsNullOrEmpty(cvi.CVICODE))
{
CalcInOrderNum(edm, cvi.CVICODE);
}
}
else
{//原先有 计划单号
if (string.IsNullOrEmpty(cvi.CVICODE))
{
CalcInOrderNum(edm, oldCviCode);
}
else
{
if (oldCviCode != cvi.CVICODE)
{
CalcInOrderNum(edm, oldCviCode);
CalcInOrderNum(edm, cvi.CVICODE);
}
}
}
}
///
/// 计算 冲压计划单号的数量
///
///
///
private static void CalcInOrderNum(Model edm, string cviCode)
{
List curCviList = edm.BASE_CONTAINER_VS_ITEM.Where(x => x.CVICODE == cviCode).ToList();
//重新计算冲压计划的数量
int doneNum = curCviList.Sum(x => x.ITEMNUM == null ? 0 : Convert.ToInt32(x.ITEMNUM));
var order = edm.ORDER_INORDER.Where(x => x.INORDERCODE == cviCode).FirstOrDefault();
if (order != null)
{
order.DONENUM = doneNum;
order.AllFinishedNum = (order.DONENUM ?? 0) + (order.UNLINENUM ?? 0);
}
}
///
/// 获取Dictionary值
///
///
///
///
///
public static object GetValue(Dictionary dic, string Type, string key)
{
string value;
if (dic.TryGetValue(key, out value))
{
if (Type == "string")
{
return value;
}
else if (Type == "int")
{
int intValue = 0;
if (int.TryParse(value, out intValue))
{
return intValue;
}
else
{
return null;
}
}
else if (Type == "datetime")
{
DateTime dateValue;
if (DateTime.TryParse(value, out dateValue))
{
return dateValue;
}
else
{
return null;
}
}
else if (Type == "decimal")
{
decimal decimalValue;
if (decimal.TryParse(value, out decimalValue))
{
return decimalValue;
}
else
{
return null;
}
}
}
return null;
}
///
/// 实体转键值对
///
/// 泛型
///
///
public static Dictionary EntityToDictionary(T obj) where T : class
{
//初始化定义一个键值对,注意最后的括号
Dictionary dic = new Dictionary();
//返回当前 Type 的所有公共属性Property集合
PropertyInfo[] props = typeof(T).GetProperties();
foreach (PropertyInfo p in props)
{
var property = obj.GetType().GetProperty(p.Name);//获取property对象
var value = p.GetValue(obj);//获取属性值
dic.Add(p.Name, Helper.valueOf(value));
}
return dic;
}
///
/// 避免空值
///
///
///
public static String valueOf(Object obj)
{
return (obj == null) ? null : obj.ToString();
}
///
/// 密码转换成MD5
///
///
///
public static string ParseMd5(string pwd)
{
var md5 = new MD5CryptoServiceProvider();
if (string.IsNullOrEmpty(pwd))
{
pwd = "";
}
var bytes = Encoding.UTF8.GetBytes(pwd);
bytes = md5.ComputeHash(bytes);
md5.Clear();
string ret = "";
for (int i = 0; i < bytes.Length; i++)
{
ret += Convert.ToString(bytes[i], 16).PadLeft(2, '0');
}
return ret.PadLeft(32, '0');
}
///
/// 获取属性值
///
///
///
public static object GetFieldValueByName(Object obj, String fieldName)
{
try
{
// 获取obj类的字节文件对象
Type c = obj.GetType();
// 获取该类的成员变量
PropertyInfo f = c.GetProperty(fieldName);
if (f == null)
{
f = c.GetProperty(fieldName.ToUpper());//尝试大写
}
// 给变量赋值
//f.SetValue(obj, value);
object v = f.GetValue(obj);
return v;
}
catch
{
return null;
}
}
///
/// 流转文件
///
///
///
public static void StreamToFile(Stream stream, string fileName)
{
if (!Directory.Exists(fileName))
{
Directory.CreateDirectory(fileName);
}
// 把 Stream 转换成 byte[]
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
// 把 byte[] 写入文件
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(bytes);
bw.Close();
fs.Close();
}
///获取时间戳的方法
public static string GetTimeStamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalMilliseconds).ToString();
}
/////
///// 设置值,空值保持原样
/////
//public static object SetValueWithOutNull(object old,object newValue)
//{
// object value = (newValue != null) ? newValue : old;
// return value;
//}
}
}