/// <summary> /// 新增一条记录 /// </summary> /// <param name="qcRecord">质检实体</param> /// <returns></returns> public static bool InsertOneQCRecord(WeighStrategy qcRecord) { bool rbool = true; using (DCCarManagementDataContext db = new DCCarManagementDataContext()) { try { rbool = LinQBaseDao.InsertOne(db, qcRecord); } catch { rbool = false; } finally { db.Connection.Close(); } } return(rbool); }
/// <summary> /// 向下移动 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnX_Click(object sender, EventArgs e) { if (dgvCount.SelectedRows.Count != 1) { MessageBox.Show("请选择一项"); return; } string WeighStratry_ID = dgvCount.SelectedRows[0].Cells["dgv_WeighStrategy_ID"].Value.ToString(); //策略编号 string Sort = dgvCount.SelectedRows[0].Cells["dgv_WeighStrategy_Sort"].Value.ToString(); //策略顺序号 string a = dgvCount.Rows[0].Cells["dgv_WeighStrategy_Sort"].Value.ToString(); string b = dgvCount.SelectedRows[0].Cells["dgv_WeighStrategy_Sort"].Value.ToString(); if (dgvCount.Rows[dgvCount.Rows.Count - 1].Cells["dgv_WeighStrategy_Sort"].Value.ToString() == dgvCount.SelectedRows[0].Cells["dgv_WeighStrategy_Sort"].Value.ToString()) { //判断当前值是否是排序第一位 MessageBox.Show("已经是最下级了!"); return; } //排序号替换 for (int i = 0; i < ListWeighStrategy.Count; i++) { if (ListWeighStrategy[i].WeighStrategy_Sort == Convert.ToInt32(dgvCount.SelectedRows[0].Cells["dgv_WeighStrategy_Sort"].Value.ToString())) { int Sorts = Convert.ToInt32(ListWeighStrategy[i].WeighStrategy_Sort); ListWeighStrategy[i].WeighStrategy_Sort = ListWeighStrategy[i + 1].WeighStrategy_Sort; ListWeighStrategy[i + 1].WeighStrategy_Sort = Sorts; } } //对集合进行冒泡排序 for (int i = 0; i < ListWeighStrategy.Count; i++) { for (int j = 0; j < ListWeighStrategy.Count; j++) { if (ListWeighStrategy[i].WeighStrategy_Sort < ListWeighStrategy[j].WeighStrategy_Sort) { WeighStrategy ws = ListWeighStrategy[j]; ListWeighStrategy[j] = ListWeighStrategy[i]; ListWeighStrategy[i] = ws; } } } dgvCount.DataSource = ListWeighStrategy; dgvCount.Refresh();//更新页面数据,重要 }
/// <summary> /// 设置有序 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnYouSort_Click(object sender, EventArgs e) { for (int i = 0; i < ListWeighStrategy.Count; i++) { ListWeighStrategy[i].WeighStrategy_Sort = i + 1; } //对集合进行冒泡排序 for (int i = 0; i < ListWeighStrategy.Count; i++) { for (int j = 0; j < ListWeighStrategy.Count; j++) { if (ListWeighStrategy[i].WeighStrategy_Sort < ListWeighStrategy[j].WeighStrategy_Sort) { WeighStrategy ws = ListWeighStrategy[j]; ListWeighStrategy[j] = ListWeighStrategy[i]; ListWeighStrategy[i] = ws; } } } dgvCount.DataSource = ListWeighStrategy; dgvCount.Refresh();//更新页面数据,重要 }
/// <summary> /// 刷新、绑定设置顺序数据 /// </summary> private void Shift() { //绑定 ListWeighStrategy = new List <WeighStrategy>(); WeighStrategy WeighSt = null; //没有策略 DataSet NoDs = LinQBaseDao.Query("select * from WeighStrategy where WeighStrategy_CarType_ID=" + cobCarInfo.SelectedValue); if (NoDs.Tables[0].Rows.Count == 0) { //无数据 新增 int maxsort = 1; foreach (TreeNode tnTemp in trWeightInfo_Name.Nodes) { if (tnTemp.Checked) { WeighSt = new WeighStrategy(); //当前最大排序号 WeighSt.WeighStrategy_WeighInfo_ID = Convert.ToInt32(tnTemp.Name); WeighSt.WeighStrategy_CarType_ID = Convert.ToInt32(cobCarInfo.SelectedValue);//车辆类型ID WeighSt.WeighStrategy_Sort = maxsort; WeighSt.WeighStrategy_Name = tnTemp.Text.Trim(); ListWeighStrategy.Add(WeighSt); maxsort++; } } //对集合进行冒泡排序 for (int i = 0; i < ListWeighStrategy.Count; i++) { for (int j = 0; j < ListWeighStrategy.Count; j++) { if (ListWeighStrategy[i].WeighStrategy_Sort < ListWeighStrategy[j].WeighStrategy_Sort) { WeighStrategy ws = ListWeighStrategy[j]; ListWeighStrategy[j] = ListWeighStrategy[i]; ListWeighStrategy[i] = ws; } } } dgvCount.DataSource = ListWeighStrategy; dgvCount.Refresh();//更新页面数据,重要 } else { foreach (TreeNode tnTemp in trWeightInfo_Name.Nodes) { if (tnTemp.Checked) { WeighSt = new WeighStrategy(); //当前最大排序号 DataSet maxsort = LinQBaseDao.Query("select MAX(WeighStrategy_Sort),MAX(WeighStrategy_ID) from WeighStrategy where WeighStrategy_CarType_ID=" + cobCarInfo.SelectedValue); DataSet ds = LinQBaseDao.Query("select * from WeighStrategy where WeighStrategy_CarType_ID=" + cobCarInfo.SelectedValue + " and WeighStrategy_WeighInfo_ID=" + tnTemp.Name); if (ds.Tables[0].Rows.Count > 0) { //有此策略 WeighSt.WeighStrategy_Sort = Convert.ToInt32(ds.Tables[0].Rows[0]["WeighStrategy_Sort"]); WeighSt.WeighStrategy_ID = Convert.ToInt32(ds.Tables[0].Rows[0]["WeighStrategy_ID"]); } else { //新策略 WeighSt.WeighStrategy_Sort = Convert.ToInt32(maxsort.Tables[0].Rows[0][0]) + 1; WeighSt.WeighStrategy_ID = Convert.ToInt32(maxsort.Tables[0].Rows[0][1]) + 1; } WeighSt.WeighStrategy_WeighInfo_ID = Convert.ToInt32(tnTemp.Name); WeighSt.WeighStrategy_CarType_ID = Convert.ToInt32(cobCarInfo.SelectedValue);//车辆类型ID WeighSt.WeighStrategy_Name = tnTemp.Text.Trim(); ListWeighStrategy.Add(WeighSt); } } //对集合进行冒泡排序 for (int i = 0; i < ListWeighStrategy.Count; i++) { for (int j = 0; j < ListWeighStrategy.Count; j++) { if (ListWeighStrategy[i].WeighStrategy_Sort < ListWeighStrategy[j].WeighStrategy_Sort) { WeighStrategy ws = ListWeighStrategy[j]; ListWeighStrategy[j] = ListWeighStrategy[i]; ListWeighStrategy[i] = ws; } } } dgvCount.DataSource = ListWeighStrategy; dgvCount.Refresh();//更新页面数据,重要 } }
/// <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(""); } }