namespace LSFileClient { using LSServerService; using LSUtils; using System; using System.ComponentModel; using System.Data.SqlClient; using System.Drawing; using System.IO; using System.Threading; using System.Windows.Forms; using System.Data; using CommonLib; using System.Diagnostics; using System.Drawing.Imaging; public partial class FrmBatchProgress : FrmBase { private bool _imageUpLoad; public FrmBatchProgress() : this(null, "", "","1") { } public FrmBatchProgress(LSClientMain _clientMain, string _serverIP, string _databaseName, string _isConsumer) : base(_clientMain, _serverIP, _databaseName, _isConsumer) { this.components = null; try { string file = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)), "SystemResources.ini"); string connectionStr = ""; if (File.Exists(file)) { string lastIPPort = string.Format("{0}:{1}", IniUtil.Read(file, "LocalLastIP"), IniUtil.Read(file, "LocalLastPort")); string downloadAESStrKey = string.Format("DownloadAESStr_{0}", lastIPPort); string[] downloadAESStrValue = IniUtil.Read(file, downloadAESStrKey).Split('^'); string downloadAESStr = downloadAESStrValue.Length == 2 ? downloadAESStrValue[1] : ""; string configFlag = IniUtil.Read(file, "FrmConfigFlag"); if (configFlag.Equals("1")) { //string args = AESUtil.Decrypt(File.ReadAllText(file)); if (!string.IsNullOrEmpty(downloadAESStr)) { string args = AESUtil.Decrypt(downloadAESStr); string[] controlAfgs = args.Split('^'); if (controlAfgs != null && controlAfgs.Length == 5) { connectionStr = "Server=" + controlAfgs[0] + ";Database=" + controlAfgs[1] + ";Persist Security Info=True;User ID=" + controlAfgs[2] + ";Password=" + controlAfgs[3] + ";Connection Timeout=5;MultipleActiveResultSets=true"; } } } else { connectionStr = DBConfig.Instance.GetConStr(); } } if (string.IsNullOrEmpty(connectionStr)) { connectionStr = DBConfig.Instance.GetConStr(); } DBConfig.Instance.CreateNewConnection(connectionStr); } catch (Exception ex) { MessageBox.Show(ex.Message); } this.isConsumer = _isConsumer; this.InitializeComponent(); } private void addFiles(string[] files) { for (int i = 0; i < files.Length; i++) { string path = files[i]; if (File.Exists(path)) { ListViewItem item = null; FrmBase.ListNodeData data = base.getDataByName(Path.GetFileName(path)); bool flag = data != null; if (data == null) { item = new ListViewItem(); data = new FrmBase.ListNodeData(); } else { if (MessageBox.Show("确定覆盖文件\"" + Path.GetFileName(path) + "\"吗?", "提示", MessageBoxButtons.OKCancel) == DialogResult.Cancel) { continue; } item = data.item; } data.selected = true; data.absoluteFileName = path; item.Text = Path.GetFileName(path); FileInfo info = new FileInfo(path); if (flag) { item.SubItems[1].Text = info.Length.ToString(); item.SubItems[2].Text = ""; item.SubItems[3].Text = ""; item.SubItems[4].Text = ""; item.SubItems[5].Text = ""; item.SubItems[6].Text = ""; } else { item.SubItems.Add(info.Length.ToString()); item.SubItems.Add(""); item.SubItems.Add(""); item.SubItems.Add(""); item.SubItems.Add(""); item.SubItems.Add(""); this.lstVFileList.Items.Add(item); data.item = item; base.m_listNode.Add(data); } if (LSParamsFormatter.paramsCount == 11 || LSParamsFormatter.paramsCount == 12) { item.Selected = true; } } } } private void batchUploadThread() { Thread.CurrentThread.IsBackground = true; try { for (int i = 0; i < base.m_listNode.Count; i++) { base.m_currentNodeData = base.m_listNode[i]; if (base.m_currentNodeData.selected) { FrmBase.SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.NOTIFY_READY, IntPtr.Zero, IntPtr.Zero); string absoluteFileName = base.m_currentNodeData.absoluteFileName; LSWriteLog.WriteLog("createdir.log", false, "准备创建文件《{0}》", new object[] { absoluteFileName }); FileInfo info = new FileInfo(absoluteFileName); int length = (int)info.Length; int directoryId = LSParamsFormatter.directoryId; string sName = _imageUpLoad ? Path.GetFileName(absoluteFileName) : base.m_currentNodeData.fileNo + "_" + Path.GetFileName(absoluteFileName); // 上传检查记录 if (this.AttachOperation(1, LSParamsFormatter.userId, "", base.m_currentNodeData.specieNo, 0, sName, length)) { if (base.clientMain.readyCreateFile(directoryId, sName, length, 0)) { LSGlobleVariable.notifyEvent.WaitOne(); } } else { } } } } catch (Exception exception) { PubUtils.WriteLog("frmFileManager.batchUploadThread errorcode: {0} ", new object[] { exception }); } FrmBase.SendMessage(LSParamsFormatter.mainHandle, LSCustomWinMsg.BATCHUPLOADCOMPLETED, IntPtr.Zero, IntPtr.Zero); FrmBase.SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.CLOSE_WINDOW, IntPtr.Zero, IntPtr.Zero); } private void beginUploadBatch() { LSGlobleVariable.notifyEvent.Reset(); new Thread(new ThreadStart(this.batchUploadThread)).Start(); } private void btnAddFiles_Click(object sender, EventArgs e) { if (this.openFileDialog.ShowDialog() == DialogResult.OK) { if (this.openFileDialog.FileNames.Length > 0) { this.addFiles(this.openFileDialog.FileNames); } } } private void btnCancel_Click(object sender, EventArgs e) { base.Close(); } private void btnDelete_Click(object sender, EventArgs e) { if (this.lstVFileList.SelectedItems.Count > 0) { ListViewItem item = this.lstVFileList.SelectedItems[0]; base.removeItem(item); if (this.editContainer.Visible) { this.txtFileName.Text = this.txtFileNo.Text = this.txtSpecieNo.Text = this.txtSpecieName.Text = ""; } this.lstVFileList.Items.Remove(item); } } private void btnDeleteFile_Click(object sender, EventArgs e) { if (this.lstVFileList.SelectedItems.Count <= 0) { MessageBox.Show("请选择一个文件删除"); } else if (MessageBox.Show("确定删除吗?", "提示", MessageBoxButtons.OKCancel) != DialogResult.Cancel) { try { ListViewItem item = this.lstVFileList.SelectedItems[0]; base.m_currentNodeData = base.getByItem(item); if (base.m_currentNodeData != null) { if (!(base.m_currentNodeData.isDownload || (base.m_currentNodeData.fileId != 0))) { MessageBox.Show("还未上传无需删除!"); } else { this.btnOK.Enabled = this.btnCancel.Enabled = this.btnRedo.Enabled = this.btnDelete.Enabled = this.btnAddFiles.Enabled = false; C2S_DeleteFile structObj = new C2S_DeleteFile(true) { dwFileId = base.m_currentNodeData.fileId }; base.clientMain.clientSocket.Send(structObj); FrmBase.SendMessage(LSParamsFormatter.mainHandle, LSCustomWinMsg.DELETECOMPLETED, IntPtr.Zero, IntPtr.Zero); } } } catch (Exception) { FrmBase.SendMessage(LSParamsFormatter.mainHandle, LSCustomWinMsg.DELETEERROR, IntPtr.Zero, IntPtr.Zero); } } } private void btnOK_Click(object sender, EventArgs e) { if (LSParamsFormatter.type == fmTypes.upByParentId) { if (base.m_listNode.Count <= 0) { MessageBox.Show("列表没有文件,请添加要上传的文件。"); } else { for (int i = 0; i < base.m_listNode.Count; i++) { FrmBase.ListNodeData data = base.m_listNode[i]; if (data.selected && ((data.specieName == "") || (data.fileNo == ""))) { MessageBox.Show("有文件没有关联到必须的信息."); return; } } this.btnRedo.Enabled = this.btnOK.Enabled = this.btnCancel.Enabled = this.btnDelete.Enabled = this.btnAddFiles.Enabled = false; this.beginUploadBatch(); } } else if (base.m_listNode.Count > 50) { MessageBox.Show(string.Format("最多支持{0}个下载", 50)); } else if (this.folderDialog.ShowDialog() == DialogResult.OK) { string str = this.folderDialog.SelectedPath.Trim(); if (str != "") { LSFileManage.init(str + @"\"); this.btnRedo.Enabled = this.btnOK.Enabled = this.btnCancel.Enabled = this.btnDelete.Enabled = this.btnAddFiles.Enabled = false; this.reqDownloadFiles(); } } } /// /// 说明:附件操作之前调用 /// 创建人:龚宇超 /// 创建日期:2017-09-28 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// 1=上传,2=下载,3=删除,4=锁定,5=解除锁定 必填 /// The user identifier. /// Name of the user. /// The file specis no. /// The filename. /// The filesize. /// true if XXXX, false otherwise. private bool AttachOperation(int operType, int userId, string userName, string fileSpecisNo, int fileid, string filename, int filesize) { try { SqlParameter pMsg = new SqlParameter("@msg", SqlDbType.VarChar, 2000); pMsg.Direction = ParameterDirection.Output; SqlParameter returnValue = new SqlParameter("@return", SqlDbType.Int, 4); returnValue.Direction = ParameterDirection.ReturnValue; SqlParameter[] param = { new SqlParameter("@opertype",SqlDbType.Int), new SqlParameter("@operatorid",SqlDbType.Int), new SqlParameter("@operatorname",SqlDbType.VarChar,50), new SqlParameter("@fileSpecisNo",SqlDbType.VarChar,50), new SqlParameter("@fileid",SqlDbType.Int), new SqlParameter("@filename",SqlDbType.VarChar,200), new SqlParameter("@filesize",SqlDbType.Int), pMsg, returnValue }; param[0].Value = operType; param[1].Value = userId; param[2].Value = userName; param[3].Value = fileSpecisNo; param[4].Value = fileid; param[5].Value = filename; param[6].Value = filesize; DataSet ds = SqlHelper.ExecuteDataSet(CommandType.StoredProcedure, "p_SystemAttachOperation", "attach", param); string msg = pMsg.Value.ToString(); int rVaule = Convert.ToInt32(returnValue.Value + ""); if (rVaule != 1) { MessageBox.Show(msg); return false; } return true; } catch (Exception ex) { MessageBox.Show(ex.Message); } return true; } private void btnRedo_Click(object sender, EventArgs e) { if (this.lstVFileList.SelectedItems.Count <= 0) { MessageBox.Show("请选择一个文件" + this.btnRedo.Text); } else if (MessageBox.Show("确定" + this.btnRedo.Text + "?", "提示", MessageBoxButtons.OKCancel) != DialogResult.Cancel) { ListViewItem item = this.lstVFileList.SelectedItems[0]; base.m_currentNodeData = base.getByItem(item); if (base.m_currentNodeData != null) { base.m_currentNodeData.selected = true; } this.btnOK_Click(sender, e); } } private void btnSet_Click(object sender, EventArgs e) { this.querySpeciesName(true); } private void FrmBatchProgress_Load(object sender, EventArgs e) { LSGlobleVariable.masterForm = this; LSGlobleVariable.hWndMain = base.Handle; FrmBase.SendMessage(LSParamsFormatter.mainHandle, 1298, base.Handle, IntPtr.Zero); LSParamsFormatter.setPosistion(this); switch (LSParamsFormatter.type) { case fmTypes.upByParentId: this.Text = "上传文件"; this.btnRedo.Text = "重新上传"; this.btnOK.Text = "确定上传"; this.btnCancel.Text = "退出上传"; this.openFileDialog.Multiselect = true; this.btnAddFiles.Visible = true; this.editContainer.Visible = true; this.splitContainer2.Panel1Collapsed = false; if (LSParamsFormatter.commonModel == 12) { this._imageUpLoad = true; this.addFiles(new string[] { LSParamsFormatter.filePath }); this.btnOK.PerformClick(); } break; case fmTypes.downByParentId: if (MessageBox.Show("确认下载?", "询问", MessageBoxButtons.YesNo) != DialogResult.No) { break; } base.Close(); return; case fmTypes.downByFileIds: this.Text = "下载文件"; this.reqIdsDetais(); break; } this.Text = this.Text + " V1011-0.4 域名解析版"; } private void lstVFileList_DragDrop(object sender, DragEventArgs e) { string[] data = (string[])e.Data.GetData(DataFormats.FileDrop); this.addFiles(data); } private void lstVFileList_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { e.Effect = DragDropEffects.Move | DragDropEffects.Copy | DragDropEffects.Scroll; } } private void lstVFileList_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) { if (this.editContainer.Visible) { string text = e.Item.Text; FrmBase.ListNodeData data = base.getDataByName(Path.GetFileName(text)); if (data == null) { this.txtFileName.Text = this.txtFileNo.Text = this.txtSpecieNo.Text = this.txtSpecieName.Text = ""; } else { this.txtFileName.Text = text; this.txtFileNo.Text = data.fileNo; this.txtSpecieNo.Text = data.specieNo; this.txtSpecieName.Text = data.specieName; this.txtFileNo.Focus(); if (LSParamsFormatter.paramsCount == 11 || LSParamsFormatter.paramsCount == 12) { this.txtSpecieNo.Text = LSParamsFormatter.specieNo; this.txtFileNo.Text = LSParamsFormatter.fileNumber; this.btnSet_Click(null, null); } } } } private void NotifyPercent(int nPercent) { this.proTranslate.Value = nPercent; } private void querySpeciesName(bool commit) { if (commit && (this.txtFileName.Text == "")) { MessageBox.Show("请选择要关联的文件"); } else { string str = this.txtFileNo.Text.Trim(); string str2 = this.txtSpecieNo.Text.Trim(); if (commit && (str == "")) { MessageBox.Show("请输入文件编码"); } else if (commit && (str2 == "")) { MessageBox.Show("请输入类别编码"); } else { string text = this.txtFileName.Text; FrmBase.ListNodeData data = base.getDataByName(Path.GetFileName(text)); if (data == null) { MessageBox.Show("没关联到文件"); } else { data.fileNo = str; data.specieNo = str2; this.txtSpecieName.Text = ""; //string connectionString = LSIniFile.ReadValue("/Root/ClientSettings", "connectionStr"); //if (connectionString == "") //{ string connectionString = string.Format("Server={0};Database={1};Persist Security Info=True;User ID=wlms;Password=mymsserver", base.m_serverIP, base.m_databaseName); //} try { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = new SqlCommand(string.Format("select speciesName from Bmp_productspeciestab where speciesNo='{0}' ", str2), connection); using (SqlDataReader reader = command.ExecuteReader()) { if (reader.Read()) { if (commit) { data.specieName = reader["speciesName"].ToString(); } this.txtSpecieName.Text = reader["speciesName"].ToString(); } reader.Close(); } command.Dispose(); connection.Close(); } } catch (Exception exception) { PubUtils.WriteLog("btnSet_Click error: {0}", new object[] { exception }); MessageBox.Show("连接服务器失败,请联系管理员."); } if (commit) { data.item.SubItems[4].Text = data.fileNo; data.item.SubItems[3].Text = data.specieName; } if (this.txtSpecieName.Text.Trim() == "") { MessageBox.Show("类别编码输入有错"); } } } } } private void reqDownloadFiles() { int index = 0; bool canDownLoad = false; int[] sourceArray = new int[50]; for (int i = 0; i < base.m_listNode.Count; i++) { FrmBase.ListNodeData data = base.m_listNode[i]; if (data.selected) { if (this.AttachOperation(2, LSParamsFormatter.userId, "", "", data.fileId, "", 0)) { canDownLoad = true; sourceArray[index] = data.fileId; index++; } } } if (canDownLoad) { C2S_DownloadFile reqDownload = new C2S_DownloadFile(true); Array.Copy(sourceArray, 0, reqDownload.dwFileIds, 0, (index > reqDownload.dwFileIds.Length) ? reqDownload.dwFileIds.Length : index); base.clientMain.requestDownloadFile(reqDownload); } } private void reqIdsDetais() { //string connectionString = LSIniFile.ReadValue("/Root/ClientSettings", "connectionStr"); //if (connectionString == "") //{ string connectionString = string.Format("Server={0};Database={1};Persist Security Info=True;User ID=wlms;Password=mymsserver", base.m_serverIP, base.m_databaseName); //} SqlConnection connection = new SqlConnection(connectionString); try { connection.Open(); int[] fileIds = LSParamsFormatter.fileIds; string str2 = "select fileId,sName,CreateTime,fileSize from p_fm_fileTab where fileId in("; for (int i = 0; i < fileIds.Length; i++) { str2 = str2 + fileIds[i].ToString() + ","; } SqlCommand command = new SqlCommand(str2.Substring(0, str2.Length - 1) + ")", connection); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { FrmBase.ListNodeData data = new FrmBase.ListNodeData { isDownload = true, selected = true, fileId = int.Parse(reader["fileId"].ToString()) }; ListViewItem item = new ListViewItem { Text = reader["sName"].ToString() }; item.SubItems.Add(reader["fileSize"].ToString()); item.SubItems.Add(reader["CreateTime"].ToString()); item.SubItems.Add(""); item.SubItems.Add(""); item.SubItems.Add(""); item.SubItems.Add(""); this.lstVFileList.Items.Add(item); data.item = item; base.m_listNode.Add(data); } reader.Close(); reader.Dispose(); reader = null; command.Dispose(); command = null; } catch (Exception exception) { PubUtils.WriteLog("reqIdsDetais error: {0}", new object[] { exception }); MessageBox.Show("连接服务器失败,请联系管理员."); } connection.Close(); connection = null; } private void txtFileNo_KeyPress(object sender, KeyPressEventArgs e) { if (((e.KeyChar == '\r') && (this.txtFileNo.Text.Trim() != "")) && (this.txtFileName.Text != "")) { this.txtSpecieNo.Focus(); } } private void txtSpecieNo_KeyPress(object sender, KeyPressEventArgs e) { if (((e.KeyChar == '\r') && (this.txtSpecieNo.Text.Trim() != "")) && (this.txtFileName.Text != "")) { this.querySpeciesName(false); } } protected override void WndProc(ref Message msg) { if (msg.Msg == LSCustomWinMsg.SUCCESS_MESSAGE) { base.setItemState(2); } else { int wParam; if (msg.Msg == LSCustomWinMsg.FAIL_MESSAGE) { wParam = (int)msg.WParam; if (wParam == ErrorType.FILE_BEEN_LOCKED) { base.setItemState(3, "附件已经被锁定,"); } else { base.setItemState(3); } } else if (msg.Msg == LSCustomWinMsg.SETPROGRESS) { this.NotifyPercent((int)msg.WParam); } else if (msg.Msg == LSCustomWinMsg.CLOSE_WINDOW) { this.btnRedo.Enabled = true; if (this.btnAddFiles.Visible) { this.btnOK.Enabled = this.btnAddFiles.Enabled = true; } this.btnCancel.Enabled = true; } else if (msg.Msg == LSCustomWinMsg.NOTIFY_READY) { if (base.m_currentNodeData != null) { base.m_currentNodeData.success = 1; base.m_currentNodeData.selected = false; base.m_currentNodeData.item.SubItems[5].Text = base.m_currentNodeData.isDownload ? "下载中.." : "上传中.."; } } else if (msg.Msg == LSCustomWinMsg.CONNECT_ERROR) { MessageBox.Show("连接服务器失败,请联系管理员!"); } else { if (msg.Msg == LSCustomWinMsg.DELETEFILE_MESSAGE) { this.btnOK.Enabled = this.btnCancel.Enabled = this.btnRedo.Enabled = this.btnDelete.Enabled = this.btnAddFiles.Enabled = true; wParam = (int)msg.WParam; int lParam = (int)msg.LParam; if (wParam > 0) { if (wParam == ErrorType.FILE_BEEN_LOCKED) { MessageBox.Show("附件已经被锁定,请联系管理员!"); } else { MessageBox.Show("删除出错!"); } } else { base.m_currentNodeData = base.getDataById(lParam); if (base.m_currentNodeData != null) { ListViewItem item = base.m_currentNodeData.item; base.removeItem(item); this.lstVFileList.Items.Remove(item); } MessageBox.Show("删除成功."); } } base.WndProc(ref msg); } } } private void button1_Click(object sender, EventArgs e) { try { string path = IniUtil.ReadFileConfig("GPYPath"); string psy_Images = Application.StartupPath + "/Psy_Images"; if (string.IsNullOrEmpty(path)) { MessageBox.Show("请先选择拍摄仪运行程序"); if (this.openFileDialog1.ShowDialog() == DialogResult.OK) { if (this.openFileDialog1.FileNames.Length > 0) { path = openFileDialog1.FileName; IniUtil.WriteFileConfig("GPYPath", path); } } } try { Directory.Delete(psy_Images, true); } catch (Exception) { } if (!Directory.Exists(psy_Images)) { Directory.CreateDirectory(psy_Images); } if (!string.IsNullOrEmpty(path)) { timer1.Start(); StartProcess(path); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } public static void StartProcess(string exeName, string[] args = null) { try { if (args == null) args = new string[] { }; string arg = string.Empty; foreach (string s in args) { arg += s + " "; } Process myprocess = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(exeName, arg.Trim()); myprocess.StartInfo = startInfo; myprocess.StartInfo.UseShellExecute = false; myprocess.Start(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void timer1_Tick(object sender, EventArgs e) { try { string psy_Images = Application.StartupPath + "/Psy_Images"; DirectoryInfo root = new DirectoryInfo(psy_Images); foreach (FileInfo f in root.GetFiles("*.bmp")) { string name = f.Name; string pngPath = Path.Combine(root.FullName, f.Name.Replace(".bmp", "") + ".png"); if (File.Exists(pngPath)) continue; ImageUtil.Format(f.FullName, pngPath, ImageFormat.Png, ImageUtil.ImgDeep.D8, 100, 100);//此图片作为判断依据 pngPath = Path.Combine(root.FullName, DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".png"); ImageUtil.Format(f.FullName, pngPath, ImageFormat.Png, ImageUtil.ImgDeep.D8, 1366, 1366);//此图片作为上传图片 if (File.Exists(pngPath)) { this.addFiles(new string[] { pngPath }, false); } } } catch (Exception) { } } private void addFiles(string[] files, bool isTip = true) { for (int i = 0; i < files.Length; i++) { string path = files[i]; if (File.Exists(path)) { ListViewItem item = null; FrmBase.ListNodeData data = base.getDataByName(Path.GetFileName(path)); bool flag = data != null; if (data == null) { item = new ListViewItem(); data = new FrmBase.ListNodeData(); } else { if (!isTip) continue; if (MessageBox.Show("确定覆盖文件\"" + Path.GetFileName(path) + "\"吗?", "提示", MessageBoxButtons.OKCancel) == DialogResult.Cancel) { continue; } item = data.item; } data.selected = true; data.absoluteFileName = path; item.Text = Path.GetFileName(path); FileInfo info = new FileInfo(path); if (flag) { item.SubItems[1].Text = info.Length.ToString(); item.SubItems[2].Text = ""; item.SubItems[3].Text = ""; item.SubItems[4].Text = ""; item.SubItems[5].Text = ""; item.SubItems[6].Text = ""; } else { item.SubItems.Add(info.Length.ToString()); item.SubItems.Add(""); item.SubItems.Add(""); item.SubItems.Add(""); item.SubItems.Add(""); item.SubItems.Add(""); this.lstVFileList.Items.Add(item); data.item = item; base.m_listNode.Add(data); } if (LSParamsFormatter.paramsCount == 11 || LSParamsFormatter.paramsCount == 12) { item.Selected = true; } } } } } }