如何用设计模式去除ifelse多重嵌套?

jlennonxzy jlennonxzy 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
winform程序,感觉ifelse嵌套有点多了,如何用状态模式去除ifelse嵌套,请高手指教,谢谢。
        private void UploadBtn_Click(object sender, EventArgs e) {
if (string.IsNullOrWhiteSpace(ScanTbx.Text)) return;
string text = ScanTbx.Text.Trim();
using (var connection = SqlCon.TestDbConnection()) {
/***********************************************************************/
if (!next) {
if (LuhnAlgorithm.IsValidNumber(text)) {
string imei = $"select * from Binding where IMEI='{text}'";
var imeiQuery = connection.Query<Binding>(imei).ToList();
if ((imeiQuery.Count == 0) || cbxEdit865Code.Checked) {
lblIMEI.Text = text;
next = !next;
label1.Text = "请扫描小条码";
}
else {
MessageBox.Show("IMEI已扫描", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
}
else {
MessageBox.Show("IMEI错误", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
}
/***********************************************************************/
else {
if (cbxEditCode.Checked) {
string editSN = $"update Binding set SN='{text}' where IMEI='{lblIMEI.Text}'";
var sqlUpdateSN = connection.Execute(editSN);
if (sqlUpdateSN > 0) {
next = !next;
label1.Text = "请扫描大I条码";
lblIMEI.Text = "";
cbxEdit865Code.Checked = false;
/*********************************/
string query = "select * from Binding order by BindingDate desc";
var sqlQuery = connection.Query<Binding>(query).ToList();
dataGridView1.DataSource = sqlQuery;
}
else {
MessageBox.Show("修改失败", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
}
else {
string sn = $"select * from Binding where SN='{text}'";
var snQuery = connection.Query<Binding>(sn).ToList();
if (snQuery.Count == 0) {
string insert = $"insert into Binding (IMEI,SN) values(" +
$"'{lblIMEI.Text}','{text}')";
var sqlInsert = connection.Execute(insert);
if (sqlInsert > 0) {
next = !next;
label1.Text = "请扫描大条码";
lblIMEI.Text = "";
/*********************************/
string query = "select * from Binding order by BindingDate desc";
var sqlQuery = connection.Query<Binding>(query).ToList();
dataGridView1.DataSource = sqlQuery;
}
else {
MessageBox.Show("绑定失败", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
}
else {
MessageBox.Show("小条码已扫描", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
}
}
}
ScanTbx.Clear();
ScanTbx.Focus();
return;
}
...全文

相关推荐

评论 11
收藏 已收藏
分享
返回顶部
jlennonxzy
C# 111,129 成员 642,538 内容