using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DAL;
using Model;
using System.Data.SqlClient;
using System.Threading;
using System.IO;
using System.Xml;
namespace CEDAS_AppDBManager.UserControl
{
public partial class ucColor : DevExpress.XtraEditors.XtraUserControl
{
int dbID;
private Thread exportThread;
string sFileName;
Guid importPK;
private delegate void SetControlStatusDelegate();
private delegate void RefreshGridViewDelegate();
private delegate void InvokeProgressDelegate(int position,int count);
private delegate void InvokeExportCompleteDelegate(bool success, Exception ex);
public ucColor(int iDBID)
{
InitializeComponent();
dbID = iDBID;
InitDataSource();
}
private void InitDataSource()
{
DAL.ColorConfig color = new DAL.ColorConfig();
DataTable dt = color.GetColorSchemeByState(dbID);
gridScheme.DataSource = dt;
}
private void viewScheme_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
{
if (e.Info.IsRowIndicator && e.RowHandle >= 0) e.Info.DisplayText = (e.RowHandle + 1).ToString().Trim();
}
//新建
private void btnNew_Click(object sender, EventArgs e)
{
ColourScheme c = new ColourScheme();
c.fdDatabaseID = dbID;
frmBlack fm = new frmBlack();
fm.Show(this);
frmColor frmColor = new frmColor(c);
frmColor.ShowDialog(this);
fm.Close();
fm.Dispose();
if (frmColor.DialogResult == DialogResult.OK)
{
frmColor.Close();
frmColor.Dispose();
InitDataSource();
viewScheme.SelectRow(viewScheme.RowCount-1);
}
}
private void ExportColorAsync()
{
bool success = false;
Exception exportException = null; ;
InvokeSetControlStatus();
FileStream fs = null;
StreamWriter sw = null;
try
{
string jsonResult = "<?xml version='1.0' encoding='gb2312'?><Schemes>";
string schemeFormat = "<Scheme><fdSchemeID value='{0}'></fdSchemeID>" +
"<fdDatabaseID value='{1}'></fdDatabaseID>" +
"<fdShowName value='{2}'></fdShowName>" +
"<fdSchemeName value='{3}'></fdSchemeName>" +
"<fdIsRegiste value='{4}'></fdIsRegiste>" +
"<fdState value='{5}'></fdState>" +
"<fdUpdateTime value='{6}'></fdUpdateTime>" + "{7}</Scheme>";
DataTable dtScheme = (DataTable)gridScheme.DataSource;
for (int i = 0; i < dtScheme.Rows.Count; i++)
{
DataRow dr = dtScheme.Rows[i];
string schemeInfo = string.Format(schemeFormat, dr["fdSchemeID"].ToString(), dr["fdDatabaseID"].ToString(),
Convert.ToBase64String(Encoding.Unicode.GetBytes(dr["fdShowName"].ToString())), dr["fdSchemeName"].ToString(),
dr["fdIsRegiste"].ToString(), dr["fdState"].ToString(), dr["fdUpdateTime"].ToString(),
GetColorJsonBySchemeID(new Guid(dr["fdSchemeID"].ToString())));
jsonResult += schemeInfo;
this.Invoke(new InvokeProgressDelegate(UpdateProgress), new object[] { i, dtScheme.Rows.Count });
}
jsonResult += "</Schemes>";
fs = new FileStream(sFileName, FileMode.OpenOrCreate);
sw = new StreamWriter(fs);
sw.Write(jsonResult);
success = true;
}
catch (Exception ex)
{
exportException = ex;
}
finally
{
sw.Flush();
sw.Close();
fs.Close();
this.Invoke(new InvokeExportCompleteDelegate(UpdateExportCompleted), new object[] { success, exportException });
}
}
private string GetColorJsonBySchemeID(Guid schemeID)
{
DataTable dt = new ColorConfig().GetColorsByScheme(schemeID);
string jsonResult = "<Colors>";
string colorFormat = "<Color><fdSchemeID value='{0}'></fdSchemeID><fdRGB value='{1}'></fdRGB><fdXIndex value='{2}'></fdXIndex></Color>";
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
string colorInfo = string.Format(colorFormat, dr["fdSchemeID"].ToString(), dr["fdRGB"].ToString(), dr["fdXIndex"].ToString());
jsonResult += colorInfo;
}
jsonResult += "</Colors>";
return jsonResult;
}
private void InvokeSetControlStatus()
{
if (this.InvokeRequired)
{
this.Invoke(new SetControlStatusDelegate(SetControlStatus), null);
}
else
{
SetControlStatus();
}
}
private void SetControlStatus()
{
panelExport.Visible = !panelExport.Visible;
labelExport.Text = "正在导出" + "......";
int iX = (Screen.PrimaryScreen.Bounds.Width - panelExport.Width) / 2;
if (iX < 0) iX = 0;
panelExport.Location = new Point(iX, (Screen.PrimaryScreen.Bounds.Height - panelExport.Height) / 2 - 160);
}
private void InvokeSetControlStatusImport()
{
if (this.InvokeRequired)
{
this.Invoke(new SetControlStatusDelegate(SetControlStatusImport), null);
}
else
{
SetControlStatus();
}
}
private void SetControlStatusImport()
{
panelExport.Visible = !panelExport.Visible;
labelExport.Text = "正在导入" + "......";
int iX = (Screen.PrimaryScreen.Bounds.Width - panelExport.Width) / 2;
if (iX < 0) iX = 0;
panelExport.Location = new Point(iX, (Screen.PrimaryScreen.Bounds.Height - panelExport.Height) / 2 - 160);
}
private void UpdateProgress(int position,int count)
{
this.pBarExport.Position = (100 / count) * position;
this.pBarExport.Invalidate();
}
private void UpdateExportCompleted(bool success, Exception ex)
{
InvokeSetControlStatus();
string msg = (success) ? "成功地导出!"
: (ex != null) ? "错误原因:" + ex.Message
: "由于出现未知情况发生导致导出结束!";
if (!success) XtraMessageBox.Show(this, msg, "导出情况", MessageBoxButtons.OK, MessageBoxIcon.Information);
else XtraMessageBox.Show(this, "导出成功!", "导出", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void UpdateImportCompleted(bool success, Exception ex)
{
InvokeSetControlStatus();
string msg = (success) ? "成功地导入!"
: (ex != null) ? "错误原因:" + ex.Message
: "由于出现未知情况发生导致导出结束!";
if (!success) XtraMessageBox.Show(this, msg, "导入情况", MessageBoxButtons.OK, MessageBoxIcon.Information);
else XtraMessageBox.Show(this, "导入成功!", "导入", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void btnImport_Click(object sender, EventArgs e)
{
}
private void ImportColorAsync()
{
bool success = false;
Exception exportException = null; ;
InvokeSetControlStatusImport();
try
{
string jsonResult = "";
XmlDocument dom = new XmlDocument();
dom.Load(sFileName);
Hashtable ht = new Hashtable();
string sql;
Guid fdSchemeID = Guid.Empty;
int j = 0;
int m = 0;
foreach(XmlElement schemes in dom.DocumentElement.ChildNodes) {
j++;
SqlParameter[] parametersScheme = {
new SqlParameter("@fdSchemeID", SqlDbType.UniqueIdentifier),
new SqlParameter("@fdDatabaseID", SqlDbType.Int),
new SqlParameter("@fdShowName", SqlDbType.VarChar,256),
new SqlParameter("@fdSchemeName", SqlDbType.VarChar,256),
new SqlParameter("@fdThumbnail", SqlDbType.Image),
new SqlParameter("@fdIsRegiste", SqlDbType.Bit),
new SqlParameter("@fdState", SqlDbType.Int),
new SqlParameter("@fdXIndex"+m, SqlDbType.Int),
new SqlParameter("@fdUpdateID", SqlDbType.Char,32),
new SqlParameter("@fdUpdateName", SqlDbType.NVarChar,32),
new SqlParameter("@fdUpdateTime", SqlDbType.DateTime)
};
List<int> intRGB = new List<int>();
importPK = Guid.NewGuid();
//新增色系
fdSchemeID = importPK;// new Guid(schemes.ChildNodes[0].Attributes["value"].Value.ToString());
parametersScheme[0].Value = fdSchemeID;
parametersScheme[1].Value = dbID;// int.Parse(schemes.ChildNodes[1].Attributes["value"].Value.ToString());
parametersScheme[2].Value = new ColorConfig().GetShowNameByImport(Encoding.Unicode.GetString(Convert.FromBase64String(schemes.ChildNodes[2].Attributes["value"].Value.ToString())), dbID);// "复件" + Encoding.Unicode.GetString(Convert.FromBase64String(schemes.ChildNodes[2].Attributes["value"].Value.ToString())); ;
parametersScheme[3].Value = schemes.ChildNodes[3].Attributes["value"].Value.ToString();
//parametersScheme[4].Value = ConvertToIcon();
parametersScheme[5].Value = schemes.ChildNodes[4].Attributes["value"].Value.ToString();
parametersScheme[6].Value = schemes.ChildNodes[5].Attributes["value"].Value.ToString();
parametersScheme[7].Value = DbHelperSQL.GetMaxID("fdXIndex", "tbColourScheme")+m;
parametersScheme[8].Value = AppCommonValue.AdminID;
parametersScheme[9].Value = AppCommonValue.AdminName;
parametersScheme[10].Value = DateTime.Now;
//新增颜色
foreach (XmlElement colors in schemes.ChildNodes[7].ChildNodes)
{
int i = 0;
SqlParameter[] parametersColor = {
new SqlParameter("@fdSchemeID", SqlDbType.UniqueIdentifier),
new SqlParameter("@fdRGB", SqlDbType.Int),
new SqlParameter("@fdXIndex"+j, SqlDbType.Int)
};
parametersColor[0].Value = importPK;// new Guid(colors.ChildNodes[0].Attributes["value"].Value.ToString());
parametersColor[1].Value = int.Parse(colors.ChildNodes[1].Attributes["value"].Value.ToString());
parametersColor[2].Value = int.Parse(colors.ChildNodes[2].Attributes["value"].Value.ToString());
sql = "insert into tbColourConfig(fdSchemeID,fdRGB,fdXIndex) values (@fdSchemeID,@fdRGB,@fdXIndex" + j + ")";
ht.Add(sql, parametersColor);
intRGB.Add(int.Parse(colors.ChildNodes[1].Attributes["value"].Value.ToString()));
i++;
j++;
}
if (intRGB.Count != 0)
{
parametersScheme[4].Value = ConvertToIcon(intRGB);
}
sql = "insert into tbColourScheme(fdSchemeID,fdDatabaseID,fdShowName,fdSchemeName,fdThumbnail,fdIsRegiste,fdState,fdXIndex,fdUpdateID,fdUpdateName,fdUpdateTime) values(@fdSchemeID,@fdDatabaseID,@fdShowName,@fdSchemeName,@fdThumbnail,@fdIsRegiste,@fdState,@fdXIndex"+m+",@fdUpdateID,@fdUpdateName,@fdUpdateTime)";
ht.Add(sql, parametersScheme);
m++;
}
DbHelperSQL.ExecuteSqlTran(ht);
success = true;
}
catch (Exception ex)
{
exportException = ex;
}
finally
{
this.Invoke(new InvokeExportCompleteDelegate(UpdateImportCompleted), new object[] { success, exportException });
InvokeRefreshGridView();
}
}
private void InvokeRefreshGridView()
{
if (this.InvokeRequired)
{
this.Invoke(new RefreshGridViewDelegate(InitDataSource), null);
}
else
{
InitDataSource();
}
}
private byte[] ConvertToIcon(List<int> intRGB)
{
int imageWidth = intRGB.Count > 10 ? 250 : intRGB.Count * 25;
Bitmap bt = new Bitmap(imageWidth, 25,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Stream imageStream = new MemoryStream();
Graphics g = Graphics.FromImage(bt);
g.Clear(Color.White);
int positionTemp = 0;
foreach (int c in intRGB)
{
g.DrawRectangle(new Pen(Color.FromArgb(c)), positionTemp * 25, 0, 25, 25);
g.FillRectangle(new SolidBrush(Color.FromArgb(c)), positionTemp * 25, 0, 25, 25);
positionTemp++;
}
bt.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] photo = new byte[(int)imageStream.Length];
imageStream.Position = 0;
imageStream.Read(photo, 0, (int)imageStream.Length);
//byte[] photo = br.ReadBytes((int)imageStream.Length);
g.Dispose();
//释放位图缓存
bt.Dispose();
imageStream.Close();
return photo;
}
private void repositoryItemButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
{
int row = viewScheme.FocusedRowHandle ;
ColourScheme c = new ColourScheme();
c.fdSchemeID = new Guid(viewScheme.GetRowCellValue(row,"fdSchemeID").ToString());
c.fdShowName =viewScheme.GetRowCellValue(row,"fdShowName").ToString();
c.fdDatabaseID = dbID;
if (e.Button.Caption == "编辑")
{
frmBlack fm = new frmBlack();
fm.Show(this);
frmColor frmColor = new frmColor(c);
frmColor.ShowDialog(this);
fm.Close();
fm.Dispose();
if (frmColor.DialogResult == DialogResult.OK)
{
frmColor.Close();
frmColor.Dispose();
InitDataSource();
viewScheme.SelectRow(row);
}
}
else
{
DialogResult dr = XtraMessageBox.Show("您确认删除色系【" + c.fdShowName + "】? 引用该色系的监测报表将使用默认色系!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dr == DialogResult.OK)
{
try
{
new ColorConfig().DeleteSchemeAndColor(c.fdSchemeID);
InitDataSource();
}
catch (Exception ex)
{
XtraMessageBox.Show("删除失败!原因是:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void btnUp_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
int temp = viewScheme.FocusedRowHandle - 1;
DataRow curRow = viewScheme.GetDataRow(viewScheme.FocusedRowHandle);
DataRow upRow = viewScheme.GetDataRow(viewScheme.FocusedRowHandle - 1);
if (curRow == null)
{
XtraMessageBox.Show(this, "选中要上移的色系!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
if (upRow == null) return;
new ColorConfig().UpdateColorFdXIndex(new Guid(curRow["fdSchemeID"].ToString()), int.Parse(curRow["fdXIndex"].ToString()),
new Guid(upRow["fdSchemeID"].ToString()), int.Parse(upRow["fdXIndex"].ToString()));
InitDataSource();
viewScheme.FocusedRowHandle = temp;
viewScheme.SelectRow(temp);
}
private void btnDown_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
int temp = viewScheme.FocusedRowHandle + 1;
DataRow curRow = viewScheme.GetDataRow(viewScheme.FocusedRowHandle);
DataRow downRow = viewScheme.GetDataRow(viewScheme.FocusedRowHandle + 1);
if (curRow == null)
{
XtraMessageBox.Show(this, "选中要下移的色系!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
if (downRow == null) return;
new ColorConfig().UpdateColorFdXIndex(new Guid(curRow["fdSchemeID"].ToString()), int.Parse(curRow["fdXIndex"].ToString()),
new Guid(downRow["fdSchemeID"].ToString()), int.Parse(downRow["fdXIndex"].ToString()));
InitDataSource();
viewScheme.FocusedRowHandle = temp;
viewScheme.SelectRow(temp);
}
private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "导入色系";
openFileDialog.Filter = "色系文件(*.color)|*.color";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
sFileName = openFileDialog.FileName;
pBarExport.Position = 0;
this.exportThread = new Thread(new ThreadStart(ImportColorAsync));
this.exportThread.Start();
}
}
private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
if (((DataTable)gridScheme.DataSource).Rows.Count == 0)
{
XtraMessageBox.Show("当前列表中没有色系,无法导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Title = "导出色系";
saveFileDialog.Filter = "色系文件(*.color)|*.color";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
sFileName = saveFileDialog.FileName;
pBarExport.Position = 0;
this.exportThread = new Thread(new ThreadStart(ExportColorAsync));
this.exportThread.Start();
}
}
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
DialogResult dr = XtraMessageBox.Show("您确认删除清理色系?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dr == DialogResult.OK)
{
try
{
new ColorConfig().ClearSchemeOverThreeMonths(dbID);
XtraMessageBox.Show("清理成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
XtraMessageBox.Show("清理失败!原因是:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}