SmartHouseAPI/Source/MyHouse/DbOp/FrmGoodsAdd.cs
zilinsoft f0ff641ed4 ## 2025-02-08 星期六更新
### MyHouse    V1.0.2502.0801
- *.[新增]适配新版接口。
### SmartHouseAPI    V1.0.2502.0801
- *.[新增]支持Docker部署,支持NAS。
2025-02-08 17:01:20 +08:00

340 lines
14 KiB
C#

using ryCommon;
using ryCommonDb;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
namespace MyHouse.DbOp
{
public partial class FrmGoodsAdd : RySkins.SKinForm
{
public ModalForm mr = null;
public int isAdd = 1;
public string selectId = "-1";
private string last_img = "";
private Dictionary<string, int> last_img2 = new Dictionary<string, int>();
public int LocationId { get; set; } = 0;
public FrmGoodsAdd()
{
InitializeComponent();
TxtGoodsSN.Text = GetNewId();
CbbUnit.SelectedIndex = 0;
}
/// <summary>
/// 修改前
/// </summary>
public string GetNewId()
{
var jsonResult = API.API.Get("Goods", "GetNewGoodsId");
if (jsonResult.IsOK)
{
return jsonResult.Get("datas","");
}
return "";
}
#region
public void GetByAdd()
{
var jsonResult = API.API.Post("Goods", "GetLocation","id="+ LocationId);
if (jsonResult.IsOK)
{
DataTable data = jsonResult.List;
if (data.Rows.Count > 0)
{
DataRow reader = data.Rows[0];
DtLocation.selectId = LocationId.ToString();
DtLocation.Text = "【" + reader["SN"].ToString() + "】" + reader["Name"].ToString();
}
}
}
public void GetInfo(string id)
{
selectId = id;
string post_str = "api=GetGoods&id=" + id;
var jsonResult = API.API.Post("Goods", "GetGoods", post_str);
if (jsonResult.IsOK)
{
DataTable data = jsonResult.List;
if (data.Rows.Count > 0)
{
DataRow reader = data.Rows[0];
#region
TxtBarCode.Text = reader["BarCode"].ToString();
txtName.Text = reader["Name"].ToString();
NumCount.Value = reader["iCount"].ToDecimal();
if (!CbbUnit.Items.Contains(reader["Unit"].ToString()))
{
CbbUnit.Items.Add(reader["Unit"].ToString());
}
CbbUnit.Text = reader["Unit"].ToString();
TxtDes.Text = reader["Des"].ToString();
NumCleanInterval.Value = reader["CleanInterval"].ToInt();
NumChargingInterval.Value = reader["ChargingInterval"].ToInt();
var OutTime = reader["OutTime"].ToInt64();
if (OutTime == 0) { DtOutTime.Checked = false; }
else
{
DtOutTime.Checked = true;
DtOutTime.Value = OutTime.ToDateTime().AddDays(-1);
}
if (isAdd == 0)
{
TxtGoodsSN.Text = reader["SN"].ToString();
NumCount.Enabled = false;
CbbUnit.Enabled = false;
last_img = reader["img"].ToString();
PicLogo.Image = API.WebDav.LoadPic(Public_Config.WebDav_VistorUrl + "/" + last_img);
var img2 = reader["img2"].ToString().Replace("\r", "").Split('\n');
for (int i = 0; i < img2.Length; i++)
{
if (img2[i].Length == 0) { continue; }
var image_item = API.WebDav.LoadPic(Public_Config.WebDav_VistorUrl + "/" + img2[i]);
if (image_item == null) { continue; }
Config.ImageInfo image = new Config.ImageInfo
{
Image = image_item,
Url = img2[i]
};
last_img2[img2[i]] = 1;
iconViewEx1.Items.Add("").Tag = image;
}
}
else
{ TxtGoodsSN.Text = GetNewId(); selectId = "0"; }
DtLocation.selectId = reader["LocationId"].ToString();
post_str = "api=GetLocation&id=" + reader["LocationId"].ToInt();
jsonResult = API.API.Post("Goods", "GetLocation", post_str);
if (jsonResult.IsOK)
{
DataTable data_loc = jsonResult.List;
if (data_loc.Rows.Count > 0)
{
DataRow row_loc = data_loc.Rows[0];
DtLocation.Text = "【" + row_loc["SN"].ToString() + "】" + row_loc["Name"].ToString();
}
else
{
DtLocation.Text = "";
}
}
else { DtLocation.Text = ""; }
ChkTipOn.Checked = reader["TipOn"].ToBool();
var TipCount = reader["TipCount"].ToDecimal();
if (TipCount < 0) { TipCount = 0; }
NumTipCount.Value = TipCount;
#endregion
}
}
}
private void BtnOK_Click(object sender, EventArgs e)
{
if (txtName.Text.Length == 0)
{
RySkins.Msg.ShowMsg("名称不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (DtLocation.selectId.Length==0)
{
RySkins.Msg.ShowMsg("请选择一个存储位置。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
RyQuickSQL mySQL = new RyQuickSQL("Goods");
mySQL.AddField("SN", TxtGoodsSN.Text);
mySQL.AddField("Name", txtName.Text);
mySQL.AddField("BarCode", TxtBarCode.Text);
mySQL.AddField("iCount", NumCount.Value);
mySQL.AddField("Unit", CbbUnit.Text);
mySQL.AddField("LocationId", LocationId);
mySQL.AddField("Des", TxtDes.Text);
mySQL.AddField("img", "");
mySQL.AddField("img2", "");
mySQL.AddField("OutTime", DtOutTime.Checked ? DtOutTime.Value.Date.AddDays(1).ToInt64() : 0);
mySQL.AddField("EditTime", DateTime.Now.ToInt64());
mySQL.AddField("idPath", Itrycn_Db.GetLocationIdPath(DtLocation.selectId.ToInt()));
mySQL.AddField("TipOn", ChkTipOn.Checked ? 1 : 0);
mySQL.AddField("TipCount", NumTipCount.Value);
mySQL.AddField("CleanInterval", NumCleanInterval.Value);
mySQL.AddField("ChargingInterval", NumChargingInterval.Value);
if (isAdd == 0)
{
mySQL.AddField("id", selectId.ToInt());
}
var jsonResult = API.API.Post("Goods", "SaveGoods", mySQL.GetPostData());
if (jsonResult.IsOK)
{
}
API.WebDav webdav = new API.WebDav(Public_Config.WebDav_Url, Public_Config.WebDav_UserId, Public_Config.WebDav_Pwd);
var dt_folder = DateTime.Now.ToString("yyyy-MM");
webdav.CreateDir(dt_folder);
if (select_img.Length >0)
{
if (last_img.Length > 0)
{
webdav.DelFile(last_img);
}
var file_url = dt_folder + "/"+ DateTime.Now.ToString("yyyyMMddHHmmss") + System.IO.Path.GetExtension(select_img);
webdav.Upload(file_url,select_img);
mySQL.AddField("img", file_url);
}
var img2 = "";
Dictionary<string, int> dict_img2 = new Dictionary<string, int>();
for (int i = 0; i < iconViewEx1.Items.Count; i++)
{
var item = (Config.ImageInfo)iconViewEx1.Items[i].Tag;
if(item.Path.Length>0)
{
var file_url = dt_folder + "/" + DateTime.Now.ToString("yyyyMMddHHmmss") +"_"+(i+1)+ System.IO.Path.GetExtension(item.Path);
webdav.Upload(file_url, item.Path);
item.Url = file_url;
}
img2 += item.Url + "\r\n";
dict_img2[item.Url] = 1;
}
foreach (var item in last_img2)
{
if(!dict_img2.ContainsKey(item.Key))
{
webdav.DelFile(item.Key);
}
}
mySQL.AddField("img2", img2.Trim());
mySQL.AddField("editTime", DateTime.Now.ToInt64());
if (isAdd >= 1)
{
mySQL.AddField("AddUser",Itrycn_Db.UserId);
mySQL.AddField("iCount", NumCount.Value);
mySQL.AddField("Unit", CbbUnit.Text);
mySQL.AddField("addTime",DateTime.Now.ToInt64());
}
else
{
//db.ExecuteNonQuery(mySQL.GetUpdateSQL() + " where id=" + selectId,mySQL);
}
ModalForm.SetDialogResult(this, mr, DialogResult.OK);
}
#endregion
private void BtnCancel_Click(object sender, EventArgs e)
{
ModalForm.SetDialogResult(this, mr, DialogResult.Cancel);
}
private void FrmAdd_Load(object sender, EventArgs e)
{
}
private void DtLocation_OnSelected(object sender, EventArgs e)
{
FrmLocationView frm=new FrmLocationView();
frm.IsSelectedMode = true;
if(frm.ShowDialog() == DialogResult.OK)
{
DtLocation.selectId = frm.SelectId.ToString();
DtLocation.SelectName = frm.SelectName;
}
frm.Dispose();
}
private string select_img = "";
private void PicLogo_Click(object sender, EventArgs e)
{
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
select_img = openFileDialog1.FileName;
PicLogo.Image = RyImage.LoadPic(select_img);
}
}
private void BtnCapture_Click(object sender, EventArgs e)
{
var capture = new ryCommon._SCREEN_CAPTURE.ScreenCapture();
Bitmap m = capture.StartCapture(false);
if (m != null)
{
long tick = DateTime.Now.Ticks;
Random ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
string path =Application.StartupPath+@"\UserDb\tmp\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ran.Next(1000, 9999) + ".png";
if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(path)))
{
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path));
}
RyImage.SaveHighQualityImage(m, path);
select_img = path;
PicLogo.Image = RyImage.LoadPic(select_img);
}
}
private void CbbUnit_SelectedIndexChanged(object sender, EventArgs e)
{
label8.Text = CbbUnit.Text + "进行提醒";
}
private void ChkTipOn_CheckedChanged(object sender, EventArgs e)
{
NumTipCount.Enabled = ChkTipOn.Checked;
}
private void CbbUnit_TextChanged(object sender, EventArgs e)
{
label8.Text = CbbUnit.Text + "进行提醒";
}
private void BtnCapture2_Click(object sender, EventArgs e)
{
var capture = new ryCommon._SCREEN_CAPTURE.ScreenCapture();
Bitmap m = capture.StartCapture(false);
if (m != null)
{
long tick = DateTime.Now.Ticks;
Random ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
string path = Application.StartupPath + @"\UserDb\tmp\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ran.Next(1000, 9999) + ".png";
if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(path)))
{
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path));
}
RyImage.SaveHighQualityImage(m, path);
Config.ImageInfo image = new Config.ImageInfo();
image.Image = RyImage.LoadPic(path);
image.Path = path;
iconViewEx1.Items.Add("").Tag = image;
}
}
private void BtnDel_Click(object sender, EventArgs e)
{
if (iconViewEx1.SelectedItems.Count == 0) { return; }
iconViewEx1.SelectedItems[0].Remove();
}
private void iconViewEx1_DoubleClick(object sender, EventArgs e)
{
if (iconViewEx1.SelectedItems.Count == 0) { return; }
Config.ImageInfo image= iconViewEx1.SelectedItems[0].Tag as Config.ImageInfo;
OTools.FrmPreview frm = new OTools.FrmPreview();
frm.pictureBox1.Image = image.Image;
frm.ShowDialog();
frm.Dispose();
}
private void BtnReadBarCode_Click(object sender, EventArgs e)
{
string post_str = "api=ReadBarCode";
var jsonResult = API.API.Post("goods.aspx", post_str);
if (jsonResult.IsOK)
{
TxtBarCode.Text = jsonResult.Get("datas", "");
}
}
}
}