/// <summary> /// 单击删除按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnDelete_Click(object sender, EventArgs e) { int index = 0; try { if (MessageBox.Show("删除,将会把所有关联删除,确定要删除吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) { int CarType_ID = Convert.ToInt32(dgvWeighStrategy.SelectedRows[0].Cells["WeighStrategy_CarType_ID"].Value.ToString()); DataSet ds = LinQBaseDao.Query("select WeighStrategy_ID from WeighStrategy where WeighStrategy_CarType_ID=" + CarType_ID); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { Expression <Func <WeighStrategy, bool> > funuserinfo = n => n.WeighStrategy_ID == int.Parse(ds.Tables[0].Rows[i][0].ToString()); if (WeighStrategyDAL.DeleteToMany(funuserinfo)) { index++; } else { index = 0; } } if (index > 0) { MessageBox.Show("删除成功"); Empty(); LogInfoLoad(""); } else { MessageBox.Show("删除失败"); } } } catch { Console.WriteLine(""); } }
/// <summary> /// 单击修改按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnUpdate_Click(object sender, EventArgs e) { try { if (dgvWeighStrategy.SelectedRows.Count < 1) { MessageBox.Show("请双击选中需要修改的数据"); return; } if (string.IsNullOrEmpty(txtWeighStrategy_Name.Text.Trim())) { MessageBox.Show("请选择地磅策略!"); return; } if (cobSate.Text.Trim() != "启动" && cobCarInfo.Text.Trim() != "") { //直接修改策略状态 string Sql = "select * from View_WeighStrategy where CarType_ID=" + CarType_IDs; DataSet ds = LinQBaseDao.Query(Sql); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { Expression <Func <WeighStrategy, bool> > func = n => n.WeighStrategy_ID == Convert.ToInt32(ds.Tables[0].Rows[i]["WeighStrategy_ID"]) && n.WeighStrategy_CarType_ID == Convert.ToInt32(CarType_IDs); Action <WeighStrategy> action = a => { a.WeighStrategy_State = cobSate.Text.Trim();//修改状态 a.WeighStrategy_Remark = txtRemark.Text; }; if (!WeighStrategyDAL.Update(func, action)) { MessageBox.Show("修改失败!"); return; } } } else if (cobCarInfo.Text.Trim() != "") { int demo = 0; List <string> test = WeighStrategyAgo; //备份新选的策略 int indexs = 0; for (int i = 0; i < WeighStrategyBack.Count; i++) //循环选中后的 { indexs = 0; for (int j = 0; j < WeighStrategyAgo.Count; j++)//循环选中前的 { if (WeighStrategyBack[i] == WeighStrategyAgo[j]) { //进入说明相等,不用操作 indexs++; test.RemoveAt(j); } } #region 说明选中的当前策略不相等,新增、修改为启动 if (indexs == 0)//选中不同时进入 { //说明选中的当前策略不相等,新增、修改为启动 DataSet ds = LinQBaseDao.Query("select * from View_WeighStrategy where CarType_ID=" + CarType_IDs + " and WeighInfo_ID=" + WeighStrategyBack[i]);//查询该车辆类型所有数据 //int Infoid = Convert.ToInt32(ds.Tables[0].Rows[0][1]); for:Infoid == Convert.ToInt32(WeighStrategyBack[i]) if (ds.Tables[0].Rows.Count > 0) { //说明选中的当前策略不相等,新增、修改为启动 Expression <Func <WeighStrategy, bool> > func = n => n.WeighStrategy_WeighInfo_ID == Convert.ToInt32(WeighStrategyBack[i]) && n.WeighStrategy_CarType_ID == Convert.ToInt32(CarType_IDs); Action <WeighStrategy> action = a => { a.WeighStrategy_State = "启动";//启动状态 a.WeighStrategy_Remark = txtRemark.Text; }; if (WeighStrategyDAL.Update(func, action)) { indexs++;//修改成功 } else { MessageBox.Show("修改失败!"); return; } } else { //新增 DataSet dsc = LinQBaseDao.Query("select * from WeighInfo where WeighInfo_ID=" + WeighStrategyBack[i]);//查询磅房名 DataSet dsmax = LinQBaseDao.Query("select MAX(WeighStrategy_Sort) from WeighStrategy where WeighStrategy_CarType_ID='" + CarType_IDs + "'"); weig = new WeighStrategy(); weig.WeighStrategy_WeighInfo_ID = Convert.ToInt32(WeighStrategyBack[i]); weig.WeighStrategy_CarType_ID = Convert.ToInt32(CarType_IDs); weig.WeighStrategy_Name = dsc.Tables[0].Rows[0][1].ToString(); weig.WeighStrategy_Sort = Convert.ToInt32(dsmax.Tables[0].Rows[0][0]) + 1; weig.WeighStrategy_State = "启动"; weig.WeighStrategy_CreatTime = CommonalityEntity.GetServersTime(); weig.WeighStrategy_Remark = txtRemark.Text.Trim(); if (!WeighStrategyDAL.InsertOneQCRecord(weig)) { MessageBox.Show("修改失败!");//新增失败 return; } } } else { //两次选中有相同策略时 } #endregion } #region 暂停多余数据 for (int i = 0; i < test.Count; i++) { DataSet ds = LinQBaseDao.Query("select * from View_WeighStrategy where CarType_ID=" + CarType_IDs + " and WeighInfo_ID=" + WeighStrategyAgo[i]);//查询该车辆类型所有数据 if (ds.Tables[0].Rows.Count > 0) { //修改多余数据 暂停 Expression <Func <WeighStrategy, bool> > func = n => n.WeighStrategy_WeighInfo_ID == Convert.ToInt32(WeighStrategyAgo[i]) && n.WeighStrategy_CarType_ID == Convert.ToInt32(CarType_IDs); Action <WeighStrategy> action = a => { a.WeighStrategy_State = "暂停";//启动状态 }; if (WeighStrategyDAL.Update(func, action)) { indexs++;//修改成功 } else { MessageBox.Show("修改失败!"); return; } } } #endregion } MessageBox.Show("修改成功"); } catch { Console.WriteLine(""); } finally { LogInfoLoad(""); btnUpdate.Enabled = false; BtnDelete.Enabled = false; btnAdd.Enabled = true; Empty(); } }
/// <summary> /// 单击保存按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAdd_Click(object sender, EventArgs e) { try { WeighStrategy weig; if (cobCarInfo.SelectedIndex < 0) { MessageBox.Show("请选择车辆类型"); return; } if (cobSate.SelectedIndex < 0) { MessageBox.Show("请选择启动状态"); return; } if (string.IsNullOrEmpty(txtWeighStrategy_Name.Text)) { MessageBox.Show("请选择过磅策略"); return; } if (txtRemark.Text.Trim().Length > 200) { MessageBox.Show("超过最大字符,限制字数200字内"); return; } string[] Name = txtWeighStrategy_Name.Text.Trim().Split(','); string[] ID = txtWeighStrategy_Name.Tag.ToString().Split(','); if (Name.Count() < 1) { MessageBox.Show("策略必须至少选择一项"); return; } DataSet ds = LinQBaseDao.Query("select * from WeighStrategy where WeighStrategy_CarType_ID=" + cobCarInfo.SelectedValue.ToString()); //修改所有未暂停 LinQBaseDao.Query("update WeighStrategy set WeighStrategy_State='暂停' where WeighStrategy_CarType_ID=" + cobCarInfo.SelectedValue.ToString()); //新增新数据,已存在修改为启动 for (int i = 0; i < ListWeighStrategy.Count; i++) { DataSet SelectDs = LinQBaseDao.Query("select * from WeighStrategy where WeighStrategy_CarType_ID=" + cobCarInfo.SelectedValue.ToString() + " and WeighStrategy_WeighInfo_ID=" + ListWeighStrategy[i].WeighStrategy_WeighInfo_ID.ToString()); if (SelectDs.Tables[0].Rows.Count > 0) { //存在 修改为启动 Expression <Func <WeighStrategy, bool> > funcs = n => n.WeighStrategy_CarType_ID == Convert.ToInt32(cobCarInfo.SelectedValue) && n.WeighStrategy_WeighInfo_ID == ListWeighStrategy[i].WeighStrategy_WeighInfo_ID; Action <WeighStrategy> actions = a => { a.WeighStrategy_State = "启动";//启动状态 a.WeighStrategy_Sort = ListWeighStrategy[i].WeighStrategy_Sort; }; if (!WeighStrategyDAL.Update(funcs, actions)) { MessageBox.Show("新增失败"); return; } } else { //不存在。新增 weig = new WeighStrategy(); weig.WeighStrategy_WeighInfo_ID = Convert.ToInt32(ListWeighStrategy[i].WeighStrategy_WeighInfo_ID); weig.WeighStrategy_CarType_ID = Convert.ToInt32(cobCarInfo.SelectedValue); weig.WeighStrategy_Name = ListWeighStrategy[i].WeighStrategy_Name; weig.WeighStrategy_Sort = ListWeighStrategy[i].WeighStrategy_Sort; weig.WeighStrategy_State = "启动"; weig.WeighStrategy_CreatTime = CommonalityEntity.GetServersTime(); weig.WeighStrategy_Remark = txtRemark.Text.Trim(); if (!WeighStrategyDAL.InsertOneQCRecord(weig)) { MessageBox.Show("保存失败!"); return; } CommonalityEntity.WriteLogData("新增", "新增过策略:" + Name[i].ToString(), CommonalityEntity.USERNAME);//添加操作日志 } } MessageBox.Show("保存成功!"); LogInfoLoad(""); Empty(); } catch { MessageBox.Show(""); } }