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.
 
 
 
 
 
 

145 lines
4.5 KiB

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace GroupBuyParser
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void btnPathBrowser_Click(object sender, EventArgs e)
{
if (dlgOpen.ShowDialog() == DialogResult.OK)
{
txtPath.Text = dlgOpen.FileName;
rdoPath.Checked = true;
}
}
private void btnType1_Click(object sender, EventArgs e)
{
txtName.Text = "A";
txtLocation.Text = "蘆洲,三重";
btnProcess.PerformClick();
}
private void btnType2_Click(object sender, EventArgs e)
{
txtName.Text = "A,B,C,D,E,F,G,H,I,J";
txtLocation.Text = "蘆洲,三重";
btnProcess.PerformClick();
}
private void btnType3_Click(object sender, EventArgs e)
{
txtName.Text = "A:M:L:XL:2XL:3XL:4XL,B:M:L:XL:2XL:3XL:4XL,C:M:L:XL:2XL:3XL:4XL,D:M:L:XL:2XL:3XL:4XL,E:M:L:XL:2XL:3XL:4XL,F:M:L:XL:2XL:3XL:4XL";
txtLocation.Text = "蘆洲,三重";
btnProcess.PerformClick();
}
private void ParseProcess(ParserSettings settings)
{
if (rdoPath.Checked && string.IsNullOrEmpty(txtPath.Text))
{
MessageBox.Show("Please select text file first", "Error");
return;
}
var parser = new GroupBuyParser
{
Settings = settings
};
var data = GetParserData();
parser.Parse(data);
var successData = data.Where(x => x.Success).Select(x => $"{x.Text} => {x.NormalizeText}");
var errorData = data.Where(x => !x.Success).Select(x => $"{x.Text} ({x.ErrorMessage})");
UpdateListBox(successData, errorData);
}
private void UpdateListBox(IEnumerable<string> successData, IEnumerable<string> errorData)
{
lstSuccess.BeginUpdate();
lstSuccess.Items.Clear();
lstSuccess.Items.AddRange(successData.ToArray());
lstSuccess.EndUpdate();
lstError.BeginUpdate();
lstError.Items.Clear();
lstError.Items.AddRange(errorData.ToArray());
lstError.EndUpdate();
lblListCount.Text = $"成功 {lstSuccess.Items.Count} 筆,失敗 {lstError.Items.Count} 筆";
}
private List<ParserData> GetParserData()
{
if (rdoPath.Checked)
{
IEnumerable<string> comments;
var fileContent = System.IO.File.ReadAllText(txtPath.Text);
if (fileContent.StartsWith("["))
{
comments = JsonConvert.DeserializeObject<List<string>>(fileContent);
}
else
{
comments = GroupBuyParser.Arrange(fileContent);
}
var data = comments.Select(x => new ParserData { Text = x });
return data.ToList();
}
else
{
return new List<ParserData> { new ParserData { Text = txtInput.Text } };
}
}
private void btnProcess_Click(object sender, EventArgs e)
{
var settings = new ParserSettings
{
Products = txtName.Text.Split(',').Select(x => {
// 如果項目有多筆,後面的當作規格輸入
// Ex. "A" => 無規格 "B:M:L:XL" => M/L/XL
var s = x.Split(':').ToList();
var name = s[0];
s.RemoveAt(0);
var specs = s.Count >= 1 ? s.ToArray() : null;
return new ProductInfo
{
Name = name,
Specifications = specs
};
}).ToList(),
Branches = GetSplitStrings(txtLocation)
};
ParseProcess(settings);
}
private List<string> GetSplitStrings(TextBox textBox)
{
return textBox.Text.Trim().Split(',', StringSplitOptions.RemoveEmptyEntries).ToList();
}
private void txtInput_KeyDown(object sender, KeyEventArgs e)
{
rdoInput.Checked = true;
}
}
}