/******************************
* 说明:下载进度窗口
* 创建人:龚宇超
* 创建日期:2018-02-09
* 修改人:
* 修改日期:
* 修改备注:
* 版本:1.0.0.0
******************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Net;
using Lskj.Model;
using Lskj.Core;
using Lskj.Business;
using Lskj.Control.BrowserSetting;
using Newtonsoft.Json.Linq;
namespace Lskj.BatchUpload
{
///
/// 下载进度窗口
///
public partial class FrmProgress : Form
{
public DynamicPowerModel Model;
///
/// 是否下载完成
///
private bool isFinished = false;
///
/// 上传总数
///
private long total = 0;
///
/// 当前下载的总数
///
private long nDownloadedTotal = 0;
///
/// 上传文件列表
///
private List UploadFileList = new List();
private delegate void SetProcessBarCallBack(int current, int total);
private delegate void ExitCallBack(bool success);
private delegate void ShowCurrentDownloadFileNameCallBack(string name);
public FrmProgress(List fileNames)
{
InitializeComponent();
total = fileNames.Count;
this.UploadFileList = fileNames;
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
this.lblAll.Text = string.Format("总进度(0/{0})", this.UploadFileList.Count);
}
///
/// 说明:窗口加载
/// 创建人:龚宇超
/// 创建日期:2018-02-23
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The sender.
/// The instance containing the event data.
private void OnFormLoad(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(this.ProcDownload));
}
///
/// 说明:下载文件
/// 创建人:龚宇超
/// 创建日期:2018-02-23
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The o.
private void ProcDownload(object o)
{
if (UploadFileList.Count == 0)
{
return;
}
string sql = @"if isnull((select 1 from Plm_MapNumberTab where plm_mn_dwgNo='{0}'),0)!=1
begin
insert into Plm_MapNumberTab(plm_mn_dwgNo,plm_mn_BillNo,plm_mn_operatorid,plm_mn_operatedate,plm_mn_modid) values('{0}','{1}','{2}',getdate(),'201041')
end";
string sql2 = "insert into Plm_FigBindingTab(plm_fb_dwgNo,plm_fb_billdocument_id,plm_fb_stepover,plm_fb_operatorid,plm_fb_operatedate,plm_fb_modid) values('{0}','{1}','1','{2}',getdate(),'201043')";
string sql3 = " update P_fm_FileTab set dwgNo='{0}' where fileId='{1}'";
//年份后2位+2位月+2位日
DateTime now = DateTime.Now;
string date = now.ToString("yyMMdd");
ERPInfo.Instance.UserId = DBConfig.Instance.NoticeUserID;
ERPInfo.Instance.UserName = DBConfig.Instance.NoticeUserName;
int i = 1;
foreach (string filePath in UploadFileList)
{
try
{
string MaximumValue = SqlHelper.ExecuteScalar("select MAX(plm_mn_id) AS max_value FROM Plm_MapNumberTab;") + "";//主表
string DetailsMaximumValue = SqlHelper.ExecuteScalar("select MAX(plm_fb_id) AS max_value FROM Plm_FigBindingTab;") + "";//明细表
int number = int.Parse(MaximumValue)+1;//最大id
int DetailsNumber = int.Parse(DetailsMaximumValue)+1;
string filename = Path.GetFileNameWithoutExtension(filePath);
string wjm = "TH" + date + number.ToString("D4");
string DetailsWjm = "TZ" + date + DetailsNumber.ToString("D4");
string ReturnID = string.Empty;
bool isUpdate = UploadFile(filePath, DetailsWjm, out string msg, out ReturnID);
if (isUpdate)
{
SqlHelper.ExecuteNonQuery(string.Format(sql, filename, wjm, ERPInfo.Instance.UserId));
SqlHelper.ExecuteNonQuery(string.Format(sql2, filename, DetailsWjm, ERPInfo.Instance.UserId));
SqlHelper.ExecuteNonQuery(string.Format("update Plm_FigBindingTab set plm_fb_assid=(select plm_mn_id from Plm_MapNumberTab where plm_mn_dwgNo='{0}') where plm_fb_dwgNo='{0}'", filename));
if (!string.IsNullOrWhiteSpace(ReturnID))SqlHelper.ExecuteNonQuery(string.Format(sql3, filename, ReturnID));
// SqlHelper.ExecuteNonQuery(string.Format(sql2, filename, "TZ" + date + i.ToString("D4"), ERPInfo.Instance.UserId));
}
//Thread.Sleep(500);
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
finally
{
this.lblAll.Text = string.Format("总进度({0}/{1})", i, this.UploadFileList.Count);
progressBarTotal.Value = (int)(i * 100 / total);
}
i++;
}
if (this.UploadFileList.Count == i)
Exit(true);
else
Exit(false);
}
///
/// 说明:显示当前上传文件
/// 创建人:龚宇超
/// 创建日期:2018-02-23
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The name.
private void ShowCurrentDownloadFileName(string name)
{
if (this.labelCurrentItem.InvokeRequired)
{
ShowCurrentDownloadFileNameCallBack cb = new ShowCurrentDownloadFileNameCallBack(ShowCurrentDownloadFileName);
this.Invoke(cb, new object[] { name });
this.labelCurrentItem.Text = "正在上传:" + name;
}
else
{
this.labelCurrentItem.Text = "正在上传:" + name;
}
}
///
/// 说明:退出升级程序
/// 创建人:龚宇超
/// 创建日期:2018-02-23
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// if set to true [success].
private void Exit(bool success)
{
if (this.InvokeRequired)
{
ExitCallBack cb = new ExitCallBack(Exit);
this.Invoke(cb, new object[] { success });
}
else
{
this.isFinished = success;
this.DialogResult = success ? DialogResult.OK : DialogResult.Cancel;
this.Close();
}
}
///
/// 取消
///
///
///
private void buttonOk_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
///
/// 上传文件
///
///
///
///
private bool UploadFile(string filePath,string wjm, out string msg, out string ReturnID)
{
bool isUpload = false;
string returnMsg = "";
string postUrl = "";
ReturnID = "";
try
{
FileInfo fileInfo = new FileInfo(filePath);
string fileName = Path.GetFileName(filePath);
byte[] fileBytes = ConvertFileToBytes(filePath);
string fileBase64 = Convert.ToBase64String(fileBytes);
//上传文件到服务器目录
postUrl = $"{SystemInfo.Instance.OAUrl}Api/FileUploadApi.ashx?moduleId={Model.ModuleCode}&idValue={wjm}&totsize={fileInfo.Length}&position=0&filename={fileName}&method=DoWebUpload";
HttpTools.setting("application/x-www-form-urlencoded", null, null);
string result = "";
CookieCollection cookieCollection = null;
HttpWebResponse webResponse = HttpTools.Post(postUrl, fileBase64, null, HttpTools.Method.POST, out cookieCollection, out result);
if (webResponse != null && !string.IsNullOrEmpty(result))
{
JObject jsonObject = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(result);
string isSuccess = jsonObject["success"] + "";
if (isSuccess.Equals("True", StringComparison.CurrentCultureIgnoreCase))
{
string fileId = jsonObject["other"] + "";
ReturnID = fileId;
string webpath = jsonObject["data"]["WebRelPath"] + "";
isUpload = true;
}
else
{
if (jsonObject.ContainsKey("msg"))
{
returnMsg = jsonObject["msg"] + "";
}
isUpload = false;
}
}
else
{
returnMsg = "上传请求失败";
isUpload = false;
}
}
catch (Exception ex)
{
returnMsg = ex.Message;
isUpload = false;
}
msg = returnMsg;
return isUpload;
}
///
/// 附件转换byte[]
///
/// 附件路径
/// 二进制
private byte[] ConvertFileToBytes(string filePath)
{
byte[] bytContent = null;
FileStream fs = null;
BinaryReader br = null;
try
{
fs = new FileStream(filePath, System.IO.FileMode.Open);
}
catch (Exception)
{
throw;
}
finally
{
if (fs != null)
br = new BinaryReader((Stream)fs);
if (br != null)
bytContent = br.ReadBytes((Int32)fs.Length);
if (fs != null)
fs.Dispose();
}
return bytContent;
}
}
}