private bool save() { this.dataGridView1.CurrentCell = null; if (this.dataGridView1.RowCount == 0) { MessageBox.Show("请添加物料!"); return(false); } else if (supplier == -1) { MessageBox.Show("请添加供应商!"); return(false); } orderParmas obj = new orderParmas(supplier, user, textBox1.Text, dateTimePicker1.Value, dateTimePicker2.Value); obj.listM = new List <ListModel>(); for (int i = this.dataGridView1.RowCount; i > 0; i--) { ListModel lm = new ListModel(); if (this.dataGridView1.Rows[i - 1].Cells["conversion"].Value != DBNull.Value) { lm.conversion = Math.Round(Convert.ToDouble(this.dataGridView1.Rows[i - 1].Cells["conversion"].Value), 2); } lm.price = Math.Round(Convert.ToDouble(this.dataGridView1.Rows[i - 1].Cells["price"].Value), 4); lm.line = i; lm.materiel = Convert.ToInt32(this.dataGridView1.Rows[i - 1].Cells["id"].Value); lm.quantity = Convert.ToDouble(this.dataGridView1.Rows[i - 1].Cells["quantity"].Value); if (this.dataGridView1.Rows[i - 1].Cells["summary"].Value != null) { lm.summary = this.dataGridView1.Rows[i - 1].Cells["summary"].Value.ToString(); } lm.tax = Convert.ToDouble(this.dataGridView1.Rows[i - 1].Cells["tax"].Value); lm.deliveryDate = Convert.ToDateTime(this.dataGridView1.Rows[i - 1].Cells["deliveryDate"].Value); if (lm.price == 0 || lm.quantity == 0) { MessageBox.Show("物料缺失价格或数量!"); return(false); } var num = 0; var attrnum = Convert.ToInt32(this.dataGridView1.Rows[i - 1].Cells["attrnum"].Value); if (mapAttr.Keys.Contains(lm.materiel)) { num = mapAttr[lm.materiel].Keys.Count; } if (num < attrnum) { MessageBox.Show("请添加辅助属性!"); return(false); } if (attrnum > 0) { lm.combination = sbctrl.getCombin(lm.materiel, mapAttr[lm.materiel]); } obj.listM.Add(lm); } var msg = octrl.add(obj); if (msg.Code == 0) { //this.Hide(); MessageBox.Show(msg.Msg); isSave = true; toolStripButton1.Enabled = false; toolStripButton2.Enabled = false; toolStripButton3.Enabled = false; dateTimePicker1.Enabled = false; dateTimePicker2.Enabled = false; button1.Enabled = false; textBox1.Enabled = false; dataGridView1.Enabled = false; DataTable dtorder = octrl.getlastinsert(PropertyClass.UserId); label9.Visible = true; label10.Text = dtorder.Rows[0]["num"].ToString(); double amount = 0; for (int i = 0; i < this.dataGridView1.RowCount; i++) { amount += Convert.ToDouble(this.dataGridView1.Rows[i].Cells["amount"].Value); } octrl.addMsg(label10.Text, textBox2.Text, dateTimePicker2.Value.ToString(), amount); return(true); } else { MessageBox.Show(msg.Msg); return(false); } }
public MessageModel add(orderParmas gm) { List <string> sqlList = new List <string>(); string num = "(SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_NAME = 'purchaseorder')"; string sqlOrder = "insert into purchaseorder (date,supplier,summary,user,deliveryDate,num) " + " values(@date, @supplier, @summary,@user,@deliveryDate, concat('PO',LPAD(" + num + ",9,'0')));"; string sqlMat = "insert into ordermateriel (purchaseorder,line,materiel,quantity,price,tax,amount,summary,deliveryDate,conversion,subquantity,combination) values "; string sqlTemp = "insert into supmaterielprice (supplier,materiel,price) values "; for (int i = 0; i < gm.listM.Count; i++) { if (i != 0) { sqlMat += ","; sqlTemp += ","; } sqlMat += " (last_insert_id(),'" + gm.listM[i].line + "','" + gm.listM[i].materiel + "' ,'" + gm.listM[i].quantity + "','" + gm.listM[i].price + "','" + gm.listM[i].tax + "','" + gm.listM[i].price * gm.listM[i].quantity + "','" + gm.listM[i].summary + "','" + gm.listM[i].deliveryDate + "'"; sqlTemp += "(@supplier,'" + gm.listM[i].materiel + "','" + gm.listM[i].price + "')"; if (gm.listM[i].conversion != null) { sqlMat += ",'" + gm.listM[i].conversion + "','" + gm.listM[i].conversion * gm.listM[i].quantity + "'"; } else { sqlMat += ", null,null"; } if (gm.listM[i].combination != null && gm.listM[i].combination > 0) { sqlMat += ",'" + gm.listM[i].combination + "'"; } else { sqlMat += ", null"; } sqlMat += ") "; } sqlList.Add(sqlOrder); sqlList.Add(sqlMat); sqlList.Add(sqlTemp); Dictionary <string, object> paras = new Dictionary <string, object>(); paras.Add("@date", gm.date); paras.Add("@supplier", gm.supplier); paras.Add("@summary", gm.summary); paras.Add("@user", gm.user); paras.Add("@deliveryDate", gm.deliveryDate); bool result = h.ExcuteTransaction(sqlList, paras); MessageModel msg = new MessageModel(); if (result == true) { msg = new MessageModel(0, "新建成功"); } else { msg = new MessageModel(10005, "新建失败"); } return(msg); }