add
yirongjin
2025-07-23 d518ecbcbc853e891c389c422d1470b028ed1dc6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
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
    {
        /// <summary>
        /// 删除托盘和物料绑定关系 记录日志
        /// </summary>
        /// <param name="cviList"></param>
        public static void LogRemoveBASE_CONTAINER_VS_ITEM(List<BASE_CONTAINER_VS_ITEM> cviList, string funName, string remark)
        {
            foreach (var item in cviList)
            {
                LogRemoveBASE_CONTAINER_VS_ITEM(item, funName, remark);
            }
        }
 
        /// <summary>
        /// 删除托盘和物料绑定关系 记录日志
        /// </summary>
        /// <param name="cviList"></param>
        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);
            }
        }
 
        /// <summary>
        ///  计算 冲压计划单号的数量
        /// </summary>
        /// <param name="oldCviCode"></param>
        /// <param name="edm"></param>
        /// <param name="cvi"></param>
        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);
                    }
                }
            }
        }
 
        /// <summary>
        /// 计算 冲压计划单号的数量
        /// </summary>
        /// <param name="edm"></param>
        /// <param name="cviCode"></param>
        private static void CalcInOrderNum(Model edm, string cviCode)
        {
            List<BASE_CONTAINER_VS_ITEM> 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);
            }
        }
 
        /// <summary>
        /// 获取Dictionary值
        /// </summary>
        /// <param name="dic"></param>
        /// <param name="Type"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object GetValue(Dictionary<string, string> 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;
        }
 
        /// <summary>
        /// 实体转键值对
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static Dictionary<string, string> EntityToDictionary<T>(T obj) where T : class
        {
            //初始化定义一个键值对,注意最后的括号
            Dictionary<string, string> dic = new Dictionary<string, string>();
            //返回当前 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;
        }
 
        /// <summary>
        /// 避免空值
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static String valueOf(Object obj)
        {
            return (obj == null) ? null : obj.ToString();
        }
 
        /// <summary>
        /// 密码转换成MD5
        /// </summary>
        /// <param name="pwd"></param>
        /// <returns></returns>
        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');
        }
 
        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="fieldName"></param>
        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;
            }
 
        }
 
        /// <summary>
        /// 流转文件
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="fileName"></param>
        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();
        }
 
        ///// <summary>
        ///// 设置值,空值保持原样
        ///// </summary>
        //public static object SetValueWithOutNull(object old,object newValue)
        //{
        //    object value = (newValue != null) ? newValue : old;
        //    return value;
        //}
    }
 
}