Form1.cs
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
namespace RepairItemInfoTool
{
public partial class FrmMain : Form
{
///
/// 消息暂存器
///
private readonly StringBuilder _builder = new StringBuilder();
///
/// 商品重复过滤器
///
private readonly List _itemList=new List();
///
/// 文件暂存器
///
private string _file = null;
///
/// 构造方法
///
public FrmMain()
{
InitializeComponent();
progressBar1.Visible = false;
txtFilePath.ReadOnly = true;
}
///
/// 选择文件
///
///
///
private void btnSelectFile_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog
{
Multiselect = false,
Title = @"请选择文件",
Filter = @"所有文件(*txt*)|*.txt*"
};
if (fileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = fileDialog.FileName;
txtFilePath.Text = filePath;
}
}
///
/// 查询错误商品点击事件
///
///
///
private void btnExecute_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtFilePath.Text) || !File.Exists(txtFilePath.Text))
{
ShowMessage(@"该文件不存在!");
return;
}
_file = File.ReadAllText(txtFilePath.Text, Encoding.Default);
ShowProgressBar(_file.Length);
bw.RunWorkerAsync(10);
}
///
/// 处理查询错误商品
///
///
///
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
if (string.IsNullOrEmpty(_file))
{
ShowMessage("文件为空!");
return;
}
MatchCollection match = GetMatchCollection("{\"flow_id\":|\"out_qty_four_week\":\\d*}", _file);
if (match.Count % 2 != 0)
{
ShowMessage(@"文件内容不合法!");
return;
}
FindIllegalItemInfoFromMatchCollection(match,_file);
}
///
/// 进度条改变事件
///
///
///
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
///
/// 查找操作执行完成后事件处理
///
///
///
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
ShowMessage("任务取消!");
else if (e.Error != null)
ShowMessage("出现异常: " + e.Error);
else
ShowMessage("查找完成!");
rtxShowResult.AppendText(_builder.ToString());
progressBar1.Visible = false;
}
///
/// 显示提示信息
///
///
private void ShowMessage(string message)
{
MessageBox.Show(message, @"提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
///
/// 配置并且显示进度条
///
///
private void ShowProgressBar(int length)
{
progressBar1.Value = 0;
progressBar1.Maximum = length;
progressBar1.Visible = true;
}
///
/// 获取正则匹配
///
///
///
///
private MatchCollection GetMatchCollection(string pattern,string str)
{
Regex regex = new Regex(pattern);
MatchCollection match = regex.Matches(str);
return match;
}
///
/// 在MatchCollection中查找不匹配的商品
///
///
///
private void FindIllegalItemInfoFromMatchCollection(MatchCollection match, string text)
{
int beginIndex = -1;
Stack stack = new Stack();
for (int i = 0; i < match.Count; i++)
{
if (stack.Count == 0)
{
if (match[i].Value.StartsWith("{"))
{
beginIndex = match[i].Index;
stack.Push("{");
}
else
{
MessageBox.Show(@"不合法的括号匹配,请检查日志文件!");
return;
}
}
else
{
if (match[i].Value.StartsWith("{"))
stack.Push("{");
else if (match[i].Value.EndsWith("}"))
{
if (stack.Peek().Equals("{"))
{
stack.Pop();
if (stack.Count == 0)
{
int endIndex = match[i].Index;
endIndex = text.IndexOf('}', endIndex);
string jsonStr = text.Substring(beginIndex, endIndex - beginIndex + 1);
if (jsonStr.Contains("item_no") && jsonStr.Contains("valid_price")
&& jsonStr.Trim().StartsWith("{")
&& jsonStr.Trim().EndsWith("}"))
{
Model model = JsonToModel(jsonStr);
if (model.valid_price > 10000000 || model.sub_amt > 10000000)
{
if (!_itemList.Contains(model.item_no))
{
_builder.AppendLine("item_no:" + model.item_no?.Trim() + ",valid_price:" + model.valid_price + ",sub_amt:" + model.sub_amt);
_itemList.Add(model.item_no);
}
}
}
bw.ReportProgress(endIndex);
}
}
}
}
}
}
///
/// Json转Model
///
///
///
private Model JsonToModel(string jsonStr)
{
try
{
Model model = JsonConvert.DeserializeObject(jsonStr);
return model;
}
catch (Exception e)
{
throw new Exception("出现了不正确的json格式串:" + jsonStr);
}
}
Model.cs
using System;
namespace RepairItemInfoTool
{
[Serializable]
public class Model
{
// public int flow_id { get; set; }
public string item_no { get; set; }
public string item_subno { get; set; }
//public decimal large_qty { get; set; }
//public decimal real_qty { get; set; }
public long valid_price { get; set; }
public long sub_amt { get; set; }
//public string memo { get; set; }
//public int orgi_price { get; set; }
//public int num1 { get; set; }
//public int num2 { get; set; }
//public int num3 { get; set; }
//public int route_qty { get; set; }
//public int yh_qty { get; set; }
//public int order_qty { get; set; }
//public string valid_date { get; set; }
//public string expire_date { get; set; }
//public int gift_qty { get; set; }
//public int ret_qty { get; set; }
// "item_stock_type":"0",
// "batch_number":"",
// "item_barcode":"",
// "color_code":"",
// "size_code":"",
// "color_name":"",
// "size_name":"",
// "is_changed":0,
// "sale_min_price":0,
// "price":0,
// "cost_price":0,
// "sale_price":0,
// "valid_days":0,
// "week_sum_qty":0,
// "month_sum_qty":0,
// "item_clsno":"030402 ",
// "out_qty_one_week":0,
// "out_qty_four_week":0
}
}