StockInQuiry/StockInQuiry/RawMaterialPriceInquiryForm.cs
bairubing ea326ec8ea 😎1.新增BOM对比页面
2.调整页面ui
3.调整url请求ip取值方式
2025-05-19 14:12:52 +08:00

176 lines
6.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Newtonsoft.Json;
using StockInQuiry.Dto;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace StockInQuiry
{
public partial class RawMaterialPriceInquiryForm : Form
{
private string _environment;
public RawMaterialPriceInquiryForm(string environment)
{
_environment = environment;
InitializeComponent();
FetchProductInventory();
}
private async void FetchProductInventory()
{
var materialCodeText = materialCodeTextBox.Text;
string url = _environment + "api/stockInquiry/getRawMaterial";
// 构建请求参数字典
var requestData = new
{
materialCode = materialCodeText
};
// 将对象序列化为JSON字符串
string json = JsonConvert.SerializeObject(requestData);
using (HttpClient client = new HttpClient())
{
try
{
// 创建HttpContent设置为JSON格式的内容
HttpContent content = new StringContent(json, Encoding.UTF8, "application/json");
// 发起POST请求
HttpResponseMessage response = await client.PostAsync(url, content);
string contentOutput = await response.Content.ReadAsStringAsync();
RawMaterialOutput result = JsonConvert.DeserializeObject<RawMaterialOutput>(contentOutput);
var data = result.result;
// 清空现有行
dataGridView1.Rows.Clear();
// 重新添加数据行
foreach (var item in data)
{
int index = dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells["物料编码"].Value = item._System_objNBS;
dataGridView1.Rows[index].Cells["物料名称"].Value = item._System_objDescription;
}
}
catch (HttpRequestException e)
{
MessageBox.Show($"Request failed: {e.Message}");
}
}
}
private void button1_Click(object sender, EventArgs e)
{
// 清空所有行,重新获取数据
dataGridView1.Rows.Clear();
FetchProductInventory();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// 确保点击的是按钮列
if (e.ColumnIndex == 5)
{
// 获取当前点击行的索引
int rowIndex = e.RowIndex;
// 获取当前行的数据(例如,你可以通过行索引获取单元格的值)
var cellValue1 = dataGridView1.Rows[rowIndex].Cells[0].Value; // 获取第1列的数据
GetRawMaterialPriceInformation(rowIndex, cellValue1.ToString());
}
}
private async void GetRawMaterialPriceInformation(int rowIndex, string materialCode)
{
string url = _environment + "api/stockInquiry/getPriceInquiry/" + materialCode;
using (HttpClient client = new HttpClient())
{
try
{
// Make the GET request
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
// Read the response content
string content = await response.Content.ReadAsStringAsync();
SapPriceInquiryOutput result = JsonConvert.DeserializeObject<SapPriceInquiryOutput>(content);
if (result.result.Code == "S")
{
dataGridView1.Rows[rowIndex].Cells["价格"].Value = result.result.Verpr;
dataGridView1.Rows[rowIndex].Cells["更新日期"].Value = result.result.DateUpdated;
dataGridView1.Rows[rowIndex].Cells["状态"].Value = "查询成功";
}
else
{
dataGridView1.Rows[rowIndex].Cells["状态"].Value = "查询失败";
}
}
catch (HttpRequestException e)
{
MessageBox.Show($"Request failed: {e.Message}");
}
}
}
private void button2_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(InExecutionlabel.Text))
{
dataGridView1.Rows.Clear();
GetAllRawMaterialPriceInformation();
}
else
{
MessageBox.Show("正在执行,请不要重复操作!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private async void GetAllRawMaterialPriceInformation()
{
InExecutionlabel.Text = "加载中,请勿操作.... ... ..";
using (HttpClient client = new HttpClient())
{
try
{
var url = _environment + "api/stockInquiry/rawMaterialPriceInquiry";
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
string content = await response.Content.ReadAsStringAsync();
BomPriceInquiryOutput result = JsonConvert.DeserializeObject<BomPriceInquiryOutput>(content);
var data = result.result;
for (var i = 0; i < data.Count; i++)
{
int index = dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells["物料编码"].Value = data[i].materialCode;
dataGridView1.Rows[index].Cells["物料名称"].Value = data[i].materialName;
dataGridView1.Rows[index].Cells["价格"].Value = data[i].Verpr;
if (data[i].DateUpdated != DateTime.MinValue)
{
dataGridView1.Rows[index].Cells["更新日期"].Value = data[i].DateUpdated;
}
dataGridView1.Rows[index].Cells["状态"].Value = data[i].State;
}
}
catch (HttpRequestException e)
{
MessageBox.Show($"Request failed: {e.Message}");
}
}
InExecutionlabel.Text = "";
}
}
}