Beispiel #1
0
        /// <summary>
        /// 用户删除行时获取id并从数据库中移除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvDelItem_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            string delId = dgvDelItem.CurrentRow.Cells[0].Value.ToString();

            BeginInvoke(new MethodInvoker(
                            () =>
            {
                USDataAccess.Delete($"delete from [commodity] where [id] = {delId};");
                DGVDataSource = USDataAccess.Select("select id as 商品代码, name as 商品名, price as 价格, manufacturer as 生产厂家, productiondate as 生产日期, validuntil as 有效期, count(commodityId) as 库存数 from[terminal] as t1 join[commodity] as t2 on t1.commodityId = t2.id group by commodityId;");
            }
                            ));
        }
        /// <summary>
        /// 定时器,负责检查货柜上商品变动情况
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timerSerialPort_Tick(object sender, EventArgs e)
        {
            try
            {
                //读取数据,看看有没有新的RFID标签添加到货柜上
                foreach (var str in serialPort.ReadAllData().Split('\n'))
                {
                    if (str.Contains("addr:0x0001 read data:0x"))
                    {
                        //查询对应卡id的信息
                        string cardId = str.Substring(24, 4);
                        //判断rfid标签是否绑定
                        if (cardId.Equals("0000"))
                        {
                            return;
                        }

                        var dataTable = USDataAccess.Select($"select * from [terminal] where [cardId] like '{cardId}';");
                        if (dataTable.Rows.Count > 0)
                        {
                            //判断是添加商品到货柜还是拿走商品
                            if (int.Parse(dataTable.Rows[0][0].ToString()) == 0)
                            {
                                USDataAccess.Update($"update [terminal] set [tid] = {curTid} where [cardId] like '{cardId}';");
                            }
                            else
                            {
                                USDataAccess.Delete($"delete from [terminal] where [cardId] like '{cardId}';");
                                serialPort.Write("CM+WRITE -addr=0x01 -value=0x0000");
                            }
                            dgvDisplay.DataSource = USDataAccess.Select($"select tis as 所在货柜, name as 商品名, price as 价格, manufacturer as 生产厂家, productiondate as 生产日期, validuntil as 有效期, count(commodityId) as 库存数 from[terminal] as t1 join[commodity] as t2 on t1.commodityId = t2.id and tid = {curTid} group by commodityId;");
                        }
                    }
                }
                serialPort.Write("CM+READ -addr=0x01");
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("遇到了一些问题,请检查串口是否被占用!", "异常");
                return;
            }
        }
        public SmartShelvesTerminal()
        {
            InitializeComponent();

            //debug
            USDataAccess.Delete("delete from [terminal] where tid = 0;");

            //登记并显示货柜
            curTid = USDataAccess.Select("select distinct [tid] from [terminal] where [tid] != 0;").Rows.Count + 1;

            //相关事件
            tbQueryCondition.Click += (x, y) => tbQueryCondition.Text = "";
            this.Load += (x, y) =>
            {
                serialPort            = new PL2303();
                dgvDisplay.DataSource = USDataAccess.Select($"select tid as 所在货柜, name as 商品名, price as 价格, manufacturer as 生产厂家, productiondate as 生产日期, validuntil as 有效期, count(commodityId) as 库存数 from[terminal] as t1 join[commodity] as t2 on t1.commodityId = t2.id and tid = {curTid} group by commodityId;");
            };
            this.Activated  += (x, y) => { serialPort?.Open(); timerSerialPort.Enabled = true; };
            this.Deactivate += (x, y) => { serialPort?.Close(); timerSerialPort.Enabled = false; };
        }