username@email.com
2025-01-21 957344a47100f58ac7ac046ec5b2d5201a599d5c
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
using CY.Infrastructure.Logging;
using CY.Model.OA;
using CY.SQLDAL;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
 
namespace CY.WebForm.Helper
{
    public class upLoadLiuyangFile
    {
 
 
 
        /// <summary>
        /// 上传一个文件页面,并保存,包括修修
        /// </summary>
        /// <param name="filel">postFileUI对象</param>
        /// <param name="Contract">客户文件表关联 </param>
        /// <param name="CountractFileKeiId">合同文件ID</param>
        /// <remarks>先判决是否存在CountractFileKeiId 如果不存在,则根据CorporateClientKeyid 新建 这两个ID不能同时为null </remarks>
        public void SavAndUpDataContractFile(HttpPostedFile filel, EC_OrderLiuyang Contract = null, Guid? CountractFileKeiId = null)
        {
            if (Contract == null && CountractFileKeiId == null)
            {
                throw new Exception("订单ID和留样ID同时为空,不能修改或者更新留样");
 
            }
            Database DC = new Database();
            string FilePath = "";
            try
            {
                String sqlStr = "";
 
                if (CountractFileKeiId != null)
                {
                    //修改文件
                    sqlStr = string.Format(@"SELECT   [Keyid],[orderid],[FileName],[FilePath],[Remark],[Creater],[CreateTime],[Updater],[LastUpdateTime] FROM [ECTEST].[dbo].[EC_OrderLiuyang] where Keyid='{0}'", CountractFileKeiId.ToString());
                    //开始修改文件
                    SqlCommand myCmd = new SqlCommand(sqlStr, DC.Connection);
 
                    var a = myCmd.ExecuteReader();
 
                    EC_OrderLiuyang Contract1 = new EC_OrderLiuyang();
                    while (a.Read())
                    {   //注意如何将每次读取到的记录添加到listbox1中(因为第一次做时出错了,嘿嘿)
                        Contract1.Keyid = a.GetGuid(0);
                        Contract1.orderid = a.GetInt32(1);
                        Contract1.FileName = a.GetString(2);
                        Contract1.FilePath = a.GetString(3);
                        Contract1.Remark = a.GetString(4);
                    }
                
                    a.Close();     //关闭sqldatareader
 
                    UploadFileTIhuan(filel, Contract1.FilePath);  //直接替换文件                
                 
                    
 
 
                }
                else
                {
                    //不是修改,则增加
 
                    //修改文件
 
                    if(Contract.orderid==0)
                    {
                        throw new Exception("订单ID为空,不能留样");
                    }
                    //开始上传合同
                    try
                    {
                        FilePath= UploadFile(filel, "LiuyangIMG", Contract.FileName);  //上传文件
                    }
                    catch (Exception e)
                    {
 
                        throw e;
                    }
                    //写入数据库
                    sqlStr = string.Format(@"  INSERT INTO [dbo].[EC_OrderLiuyang]           ( [Keyid],[orderid],[FileName],[FilePath],[Remark],[Creater],[CreateTime]      )     VALUES('{0}'    ,'{1}'       ,'{2}'           ,'{3}'           ,'{4}'           ,'{5}'           ,'{6}'          )", Guid.NewGuid().ToString(), Contract.orderid , Contract.FileName, FilePath, Contract.Remark, Contract.Creater,DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    try
                    {
                        SqlCommand myCmd = new SqlCommand(sqlStr, DC.Connection);
 
                        var a = myCmd.ExecuteNonQuery();
                        if (a == 1)
                        {
                            sqlStr = string.Format(@"  update [EC_OrderBasic]        set [LiuyangStatus] =     1  where [Keyid]=  {0}   ", Contract.orderid);
                            myCmd = new SqlCommand(sqlStr, DC.Connection);
                            a = myCmd.ExecuteNonQuery();
                            return;
                        } 
                        else
                        {
                            //删除文件?
                        }
 
                    }
                    catch (Exception e)
                    {
                        //删除文件?
                        throw e;
                    }
                    //开始修改文件
                 
 
                    
 
                  
                }
 
            }
            catch (Exception ee)
            {
                new Log4NetAdapter().Log("文件上传 错误:" + ee.Message);
 
            }
            finally
            {
                if (DC.Connection.State != System.Data.ConnectionState.Closed)
                    DC.Connection.Close();
            }
        }
 
 
 
 
 
        /// <summary>
        /// 直接替换文件
        /// </summary>
        /// <param name="filel"> postFileUI对象</param>
        /// <param name="Filepath"> /UpFile+文件路径</param>
        /// <param name="fileName"> 文件名</param>
        /// <returns></returns>
        public string UploadFileTIhuan(HttpPostedFile filel,  string fileName)
        {
            try
            {
                string DirPath = System.Web.HttpContext.Current.Request.MapPath(fileName);
 
               
              
             
 
                filel.SaveAs(DirPath );
 
                return fileName; //返回相对路径
 
            }
            catch (Exception e)
            {
 
                throw e;
            }
 
        }
 
 
 
 
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="filel"> postFileUI对象</param>
        /// <param name="Filepath"> /UpFile+文件路径</param>
        /// <param name="fileName"> 文件名</param>
        /// <returns></returns>
        public string     UploadFile(HttpPostedFile filel, string Filepath,string fileName)
        {
            try
            {
                string DirPath = System.Web.HttpContext.Current.Request.MapPath("/UpFile");
 
                if (Directory.Exists(DirPath))
                {
                    //文件夹已经存在                   
                }
                else
                {
                    try
                    {
                        Directory.CreateDirectory(DirPath);
                        //创建成功
                    }
                    catch (Exception ex)
                    {
                        throw ex;
 
                    }
                }
                //创建第二级
                if (Directory.Exists(DirPath+"/"+ Filepath))
                {
                    //文件夹已经存在                   
                }
                else
                {
                    try
                    {
                        Directory.CreateDirectory(DirPath + "/" + Filepath);
                        //创建成功
                    }
                    catch (Exception ex)
                    {
                        throw ex;
 
                    }
                }
                string File = Filepath + "/" + DateTime.Now.ToString("yyyy_MM_dd_hh_mm_ss") + fileName;
 
                filel.SaveAs(DirPath+ "/"+File);
 
                return "/UpFile/"+File; //返回相对路径
 
            }
            catch (Exception e)
            {
 
                throw e;
            }
            
        }
 
    }
}