Ejemplo n.º 1
0
        /// <summary>
        /// 当用户改变了单元格的值时对数据库进行更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvMgrItem_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            string id           = dgvMgrItem.Rows[e.RowIndex].Cells[0].Value.ToString();
            string newCellValue = dgvMgrItem.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();

            //USDataAccess.Update("update [commodity] set [xx] = xxxx where [xxx] = 'xxx';");
            USDataAccess.Update($"update [commodity] set {USDataAccess.CommodityColumns[e.ColumnIndex]} = '{newCellValue}' where [id] = {id};");
            BeginInvoke(new MethodInvoker(() => 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;
            }
        }