年度变更建库软件5.0版本
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

546 lines
26 KiB

6 months ago
using DevExpress.Spreadsheet;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using KGIS.Framework.Maps;
using KGIS.Framework.Platform;
using KGIS.Framework.Platform.Helper;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.ExtensionMethod;
using KGIS.Framework.Utils.Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using System.Xml.Linq;
using KUI.Windows;
using Kingo.PluginServiceInterface.Model;
namespace Kingo.Plugin.BGSetting.View
{
/// <summary>
/// FrmKCDLXSWH.xaml 的交互逻辑
/// </summary>
public partial class FrmKCDLXSWH : BaseWindow
{
private List<GDEntity> GDEntitys;
private XDocument xDoc;
IFeatureCursor pTT1FeatureCursor;
IFeature pTT1Feature;
IQueryFilter pQueryFilterTT1 = new QueryFilter();
string GDLX = null;
string GDPDJB = null;
object obj = null;
public FrmKCDLXSWH()
{
InitializeComponent();
InitGD();
}
public void InitGD()
{
try
{
GDEntitys = new List<GDEntity>();
string strPath = SysAppPath.GetBGGDLXConfigPath();
xDoc = XDocument.Load(strPath);
foreach (XElement xElement in xDoc.Descendants("Item"))
{
GDEntity model = new GDEntity();
model.GDLX = xElement.Attributes("GDLX").Single().Value;
model.GDPDJB = xElement.Attributes("GDPDJB").Single().Value;
model.GDPDJBMC = xElement.Attributes("GDPDJBMC").Single().Value;
decimal kcdlxs = 0;
if (decimal.TryParse(xElement.Attributes("KCDLXS").Single().Value, out kcdlxs))
{
model.KCDLXS = kcdlxs;
}
else
{
model.KCDLXS = 0;
}
GDEntitys.Add(model);
}
this.dgData.ItemsSource = GDEntitys;
}
catch (Exception ex)
{
MessageHelper.ShowTips("界面初始化失败,可能的原因是:" + ex.Message);
}
}
/// <summary>
/// 导入扣除系数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnImport_Click(object sender, RoutedEventArgs e)
{
try
{
string excelfilePath = string.Empty;
KGIS.Framework.Utils.Dialog.OpenFileDialog openFileDialog = new KGIS.Framework.Utils.Dialog.OpenFileDialog();
openFileDialog.Title = "导入扣除系数表EXCEL";
openFileDialog.Filter = "Excel Files(*.xlsx)| *.xlsx";
openFileDialog.FileName = string.Empty;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog())
{
excelfilePath = openFileDialog.FileName;
if (FileIsUsed(excelfilePath))
{
MessageHelper.ShowWarning("文件被占用,请关闭占用文件的相关程序,或者选择其他文件!");
return;
}
if (MessageHelper.ShowYesNoAndTips("确定要导入吗?导入将覆盖现有的扣除系数!") == System.Windows.Forms.DialogResult.Yes)
{
Workbook workbook = new Workbook();
using (FileStream stream = new FileStream(excelfilePath, FileMode.Open))
{
workbook.LoadDocument(stream, DocumentFormat.OpenXml);
var worksheet = workbook.Worksheets[0];
bool exitload = false;
var rowindex = 0;
Dictionary<string, decimal> kcxsDic = new Dictionary<string, decimal>();
while (!exitload)
{
if (string.IsNullOrWhiteSpace(worksheet.Rows[rowindex][0].DisplayText))
{
if (rowindex < 5)
{
rowindex++;
continue;
}
exitload = true;
break;
}
if (rowindex == 0)
{
if (worksheet.Rows[rowindex][2].DisplayText != "耕地坡度级别" || worksheet.Rows[rowindex][3].DisplayText != "梯田田坎系数" || worksheet.Rows[rowindex][4].DisplayText != "坡地田坎系数")
{
MessageHelper.ShowError("文件解析错误,请选择正确的田坎系数表!");
exitload = true;
break;
}
}
if (rowindex > 0)
{
string pdjb = worksheet.Rows[rowindex][2].Value.ToString();
string ttkcxs = worksheet.Rows[rowindex][3].Value.ToString();
string pdkcxs = worksheet.Rows[rowindex][4].Value.ToString();
if (!string.IsNullOrWhiteSpace(pdjb) && !string.IsNullOrWhiteSpace(ttkcxs) && (string.IsNullOrWhiteSpace(pdkcxs) || pdkcxs == "0"))
{
if (!kcxsDic.ContainsKey("TT-" + pdjb))
kcxsDic.Add("TT-" + pdjb, ttkcxs.ToDecimal());
}
else if (!string.IsNullOrWhiteSpace(pdjb) && !string.IsNullOrWhiteSpace(pdkcxs) && (string.IsNullOrWhiteSpace(ttkcxs) || ttkcxs == "0"))
{
if (!kcxsDic.ContainsKey("PD-" + pdjb))
kcxsDic.Add("PD-" + pdjb, pdkcxs.ToDecimal());
}
}
rowindex++;
}
if (kcxsDic.Count != 0)
{
List<GDEntity> Entitys = this.dgData.ItemsSource as List<GDEntity>;
if (Entitys != null && Entitys.Count > 0)
{
foreach (GDEntity model in Entitys)
{
string key = string.Format("{0}-{1}", model.GDLX, model.GDPDJB);
if (this.cbIgnore.IsChecked == true && model.KCDLXS != 0) continue;
if (kcxsDic.ContainsKey(key))
model.KCDLXS = kcxsDic[key];
else
model.KCDLXS = 0;
}
}
this.dgData.RefreshData();
MessageHelper.Show("扣除系数导入成功!");
}
}
}
}
}
catch (Exception ex)
{
LogAPI.Debug("扣除地类系数导入失败:" + ex.Message);
MessageHelper.ShowError("扣除地类系数导入失败:" + ex.Message);
}
}
/// <summary>
/// 返回指示文件是否已被其它程序使用的布尔值
/// </summary>
/// <param name="fileFullName">文件的完全限定名,例如:“C:\MyFile.txt”。</param>
/// <returns>如果文件已被其它程序使用,则为 true;否则为 false。</returns>
public Boolean FileIsUsed(string fileFullName)
{
Boolean result = false;
//判断文件是否存在,如果不存在,直接返回 false
if (!System.IO.File.Exists(fileFullName))
{
result = false;
}
else
{
//如果文件存在,则继续判断文件是否已被其它程序使用
//逻辑:尝试执行打开文件的操作,如果文件已经被其它程序使用,则打开失败,抛出异常,根据此类异常可以判断文件是否已被其它程序使用。
FileStream fileStream = null;
try
{
fileStream = File.Open(fileFullName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
result = false;
}
catch (IOException)
{
result = true;
}
catch (Exception)
{
result = true;
}
finally
{
if (fileStream != null)
{
fileStream.Close();
}
}
}
return result;
}
/// <summary>
/// 保存配置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnApple_Click(object sender, RoutedEventArgs e)
{
try
{
List<GDEntity> Entitys = GDEntitys.ToList();
if (Entitys != null && Entitys.Count > 0)
{
foreach (GDEntity model in Entitys)
{
xDoc.Descendants("Item").Where(p => p.Attribute("GDLX").Value.Equals(model.GDLX) && p.Attribute("GDPDJB").Value.Equals(model.GDPDJB)).Single().Attribute("KCDLXS").SetValue(model.KCDLXS.ToString());
}
xDoc.Save(SysAppPath.GetBGGDLXConfigPath());
if (e != null)
{
MessageHelper.ShowTips("保存成功!");
this.Close();
}
}
else
{
MessageHelper.ShowTips("请选择要应用的耕地类型!");
}
}
catch (Exception ex)
{
LogAPI.Debug("赋值失败,可能的原因是:" + ex.Message);
MessageHelper.ShowError("赋值失败,可能的原因是:" + ex.Message);
}
}
private void Save()
{
List<GDEntity> Entitys = GDEntitys.ToList();
foreach (GDEntity model1 in Entitys)
{
if (model1.GDLX == GDLX && model1.GDPDJB == GDPDJB)
{
if (this.cbIgnore.IsChecked == true && model1.KCDLXS != 0) continue;
xDoc.Descendants("Item").Where(p => p.Attribute("GDLX").Value.Equals(model1.GDLX) && p.Attribute("GDPDJB").Value.Equals(model1.GDPDJB)).Single().Attribute("KCDLXS").SetValue(obj.ToString());
}
}
xDoc.Save(SysAppPath.GetBGGDLXConfigPath());
}
private void ClearData()
{
obj = "";
List<GDEntity> Entitys = GDEntitys.ToList();
foreach (GDEntity model1 in Entitys)
{
if (this.cbIgnore.IsChecked == true && model1.KCDLXS != 0) continue;
xDoc.Descendants("Item").Where(p => p.Attribute("GDLX").Value.Equals(model1.GDLX) && p.Attribute("GDPDJB").Value.Equals(model1.GDPDJB)).Single().Attribute("KCDLXS").SetValue(obj.ToString());
}
xDoc.Save(SysAppPath.GetBGGDLXConfigPath());
}
/// <summary>
/// 提取扣除地类系数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExtract_Click(object sender, RoutedEventArgs e)
{
//忽略已填写的,则不清除数据
if (this.cbIgnore.IsChecked == false) ClearData();
//保存已输入的数据
if (this.cbIgnore.IsChecked == true) btnApple_Click(sender, null);
try
{
DialogResult result = System.Windows.Forms.MessageBox.Show("是否根据基础库中数据提取相应的扣除地类系数", "提示", MessageBoxButtons.YesNo);
if (result == System.Windows.Forms.DialogResult.Yes)
{
//提取数据
IFeatureLayer JC_layer = MapsManager.Instance.MapService.GetFeatureLayerByLayerName("地类图斑");
if (JC_layer == null || JC_layer.FeatureClass == null) return;
IFeatureClass pFeatureClass = JC_layer.FeatureClass as IFeatureClass;
ITable pTable = pFeatureClass as ITable;
this.ShowLoading("正在提取数据……", 0, 0);
#region 梯田扣除地类系数
string TempFH = "%";
if (Path.GetExtension((pFeatureClass as FeatureClass).Workspace.PathName).ToLower().Contains(".mdb"))
TempFH = "*";
for (int i = 0; i < pTable.Fields.FieldCount; i++)
{
IField pField = pFeatureClass.Fields.get_Field(i);
if (pField.AliasName == "扣除地类系数")
{
for (int j = 1; j < 6; j++)
{
if (j == 1)
{
pQueryFilterTT1.WhereClause = string.Format(" KCXS >= 0 AND GDLX = 'TT' AND DLBM LIKE '01{1}' AND GDPDJB = '{0}' ", j, TempFH);
pTT1FeatureCursor = pFeatureClass.Search(pQueryFilterTT1, true);
pTT1Feature = pTT1FeatureCursor.NextFeature();
while (pTT1Feature != null)
{
obj = pTT1Feature.get_Value(i);
GDLX = "TT";
GDPDJB = "1";
Save();
pTT1Feature = pTT1FeatureCursor.NextFeature();
}
}
else if (j == 2)
{
pQueryFilterTT1.WhereClause = string.Format(" KCXS >= 0 AND GDLX = 'TT' AND DLBM LIKE '01{1}' AND GDPDJB = '{0}' ", j, TempFH);
pTT1FeatureCursor = pFeatureClass.Search(pQueryFilterTT1, true);
pTT1Feature = pTT1FeatureCursor.NextFeature();
while (pTT1Feature != null)
{
obj = pTT1Feature.get_Value(i);
GDLX = "TT";
GDPDJB = "2";
Save();
pTT1Feature = pTT1FeatureCursor.NextFeature();
}
}
else if (j == 3)
{
pQueryFilterTT1.WhereClause = string.Format(" KCXS >= 0 AND GDLX = 'TT' AND DLBM LIKE '01{1}' AND GDPDJB = '{0}' ", j, TempFH);
pTT1FeatureCursor = pFeatureClass.Search(pQueryFilterTT1, true);
pTT1Feature = pTT1FeatureCursor.NextFeature();
while (pTT1Feature != null)
{
obj = pTT1Feature.get_Value(i);
GDLX = "TT";
GDPDJB = "3";
Save();
pTT1Feature = pTT1FeatureCursor.NextFeature();
}
}
else if (j == 4)
{
pQueryFilterTT1.WhereClause = string.Format(" KCXS >= 0 AND GDLX = 'TT' AND DLBM LIKE '01{1}' AND GDPDJB = '{0}' ", j, TempFH);
pTT1FeatureCursor = pFeatureClass.Search(pQueryFilterTT1, true);
pTT1Feature = pTT1FeatureCursor.NextFeature();
while (pTT1Feature != null)
{
obj = pTT1Feature.get_Value(i);
GDLX = "TT";
GDPDJB = "4";
Save();
pTT1Feature = pTT1FeatureCursor.NextFeature();
}
}
else if (j == 5)
{
pQueryFilterTT1.WhereClause = string.Format(" KCXS >= 0 AND GDLX = 'TT' AND DLBM LIKE '01{1}' AND GDPDJB = '{0}' ", j, TempFH);
pTT1FeatureCursor = pFeatureClass.Search(pQueryFilterTT1, true);
pTT1Feature = pTT1FeatureCursor.NextFeature();
while (pTT1Feature != null)
{
obj = pTT1Feature.get_Value(i);
GDLX = "TT";
GDPDJB = "5";
Save();
pTT1Feature = pTT1FeatureCursor.NextFeature();
}
}
}
}
}
#endregion
#region 坡地扣除地类系数
for (int k = 0; k < pTable.Fields.FieldCount; k++)
{
IField pField = pFeatureClass.Fields.get_Field(k);
if (pField.AliasName == "扣除地类系数")
{
for (int j = 1; j < 6; j++)
{
if (j == 1)
{
pQueryFilterTT1.WhereClause = string.Format(" KCXS >= 0 AND GDLX = 'TT' AND DLBM LIKE '01{1}' AND GDPDJB = '{0}' ", j, TempFH);
pTT1FeatureCursor = pFeatureClass.Search(pQueryFilterTT1, true);
pTT1Feature = pTT1FeatureCursor.NextFeature();
while (pTT1Feature != null)
{
obj = pTT1Feature.get_Value(k);
GDLX = "PD";
GDPDJB = "1";
Save();
pTT1Feature = pTT1FeatureCursor.NextFeature();
}
}
else if (j == 2)
{
pQueryFilterTT1.WhereClause = string.Format(" KCXS >= 0 AND GDLX = 'PD' AND DLBM LIKE '01{1}' AND GDPDJB = '{0}' ", j, TempFH);
pTT1FeatureCursor = pFeatureClass.Search(pQueryFilterTT1, true);
pTT1Feature = pTT1FeatureCursor.NextFeature();
while (pTT1Feature != null)
{
obj = pTT1Feature.get_Value(k);
GDLX = "PD";
GDPDJB = "2";
Save();
pTT1Feature = pTT1FeatureCursor.NextFeature();
}
}
else if (j == 3)
{
pQueryFilterTT1.WhereClause = string.Format(" KCXS >= 0 AND GDLX = 'PD' AND DLBM LIKE '01{1}' AND GDPDJB = '{0}' ", j, TempFH);
pTT1FeatureCursor = pFeatureClass.Search(pQueryFilterTT1, true);
pTT1Feature = pTT1FeatureCursor.NextFeature();
while (pTT1Feature != null)
{
obj = pTT1Feature.get_Value(k);
GDLX = "PD";
GDPDJB = "3";
Save();
pTT1Feature = pTT1FeatureCursor.NextFeature();
}
}
else if (j == 4)
{
pQueryFilterTT1.WhereClause = string.Format(" KCXS >= 0 AND GDLX = 'PD' AND DLBM LIKE '01{1}' AND GDPDJB = '{0}' ", j, TempFH);
pTT1FeatureCursor = pFeatureClass.Search(pQueryFilterTT1, true);
pTT1Feature = pTT1FeatureCursor.NextFeature();
while (pTT1Feature != null)
{
obj = pTT1Feature.get_Value(k);
GDLX = "PD";
GDPDJB = "4";
Save();
pTT1Feature = pTT1FeatureCursor.NextFeature();
}
}
else if (j == 5)
{
pQueryFilterTT1.WhereClause = string.Format(" KCXS >= 0 AND GDLX = 'PD' AND DLBM LIKE '01{1}' AND GDPDJB = '{0}' ", j, TempFH);
pTT1FeatureCursor = pFeatureClass.Search(pQueryFilterTT1, true);
pTT1Feature = pTT1FeatureCursor.NextFeature();
while (pTT1Feature != null)
{
obj = pTT1Feature.get_Value(k);
GDLX = "PD";
GDPDJB = "5";
Save();
pTT1Feature = pTT1FeatureCursor.NextFeature();
}
}
}
}
}
#endregion
}
else
{
return;
}
this.dgData.ItemsSource = null;
InitGD();
this.dgData.ItemsSource = GDEntitys;
btnApple_Click(sender, null);
this.CloseLoading();
System.Windows.Forms.MessageBox.Show("提取成功", "提示");
}
catch (Exception ex)
{
this.CloseLoading();
System.Windows.Forms.MessageBox.Show("提取失败", "提示");
LogAPI.Debug(ex);
}
finally
{
this.CloseLoading();
}
}
/// <summary>
/// 确定逻辑
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnConfirm_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
/// <summary>
/// 双击模板下载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
try
{
string savepaths = string.Empty;
SaveFileDialog dialog = new SaveFileDialog();
dialog.DefaultExt = "xlsx";
dialog.Filter = "Excel Files(*.xlsx)|*.xlsx";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
savepaths = dialog.FileName;
else
return;
if (string.IsNullOrWhiteSpace(savepaths))
{
MessageHelper.Show("未获取到文件路径!");
return;
}
if (File.Exists(savepaths))
{
var result = MessageHelper.ShowYesNoAndTips("文件已存在,是否覆盖?");
if (result == System.Windows.Forms.DialogResult.No) return;
}
string strPath = AppDomain.CurrentDomain.BaseDirectory + @"工作空间\模板\字典模板\田坎系数表模板.xlsx";
File.Copy(strPath, savepaths, true);
MessageHelper.Show("模板下载已完成!");
}
catch (Exception ex)
{
LogAPI.Debug("下载失败信息:" + ex.Message);
MessageHelper.ShowError("田坎系数表模板下载失败!");
}
}
}
}