/// <summary> /// 执行删除数据并处理相关数据一致性 /// </summary> /// <param name="e">传入的带有数据的事件参数</param> /// <returns></returns> private bool DeleteData(ListViewDeleteEventArgs e) { //当前用户输入的id号 Int64 id = Convert.ToInt64(e.Keys[0]); //数据适配器 //当前数据库连接 //当前更新语句对象 using (var da = new t_product_record_kailiaoTableAdapter()) using (var cmd = da.Adapter.DeleteCommand) using (var conn = cmd.Connection) { //打开数据库连接 conn.Open(); //开启事务 using (var tran = conn.BeginTransaction()) { //试运行 try { //设置事务 da.Transaction = tran; //获取数据 using (var tab = da.GetDataById(id)) { //检查是否获取到行 if (tab.Rows.Count == 0) { //显示失败 throw new Exception("当前生产记录已经被其他用户删除!"); } else { //首行 var row = (DataSetProductRecord.t_product_record_kailiaoRow)tab.Rows[0]; //部门名称 string deptName = row.dept_name; //下部门名称 string nextDeptName = row.next_dept_name; //lot卡号 string lotId = row.lot_id; //pnl出数 Int16 pnlQty = row.pnl_qty; //pcs出数 int pcsQty = row.pcs_qty; //检测是否超过下部门余数 if (ydOperateBalanceLotCrad.IsOutstripDeptBalance( tran, deptName, nextDeptName, lotId, pnlQty, pcsQty, true )) { return false; } //修改下部门lot卡余数 if (!ydOperateBalanceLotCrad.UpdateBalanceAfterInsert( tran, deptName, nextDeptName, lotId, pnlQty, pcsQty, true )) { return false; } //目标数据库表名 string toTabName = "[t_dept_lot_card_balance]"; //添加到部门结存lot卡 if (!ydOperateBalanceLotCrad.InsertOneRecordToLotCardBalance( tran, ydOperateBalanceLotCrad.GetTabNameByDeptName(deptName), deptName, toTabName, id )) { return false; } //从生产记录删除数据 if (!ydOperateBalanceLotCrad.DeleteOneRecordFromProductOrScrapRecord(cmd, id)) { return false; } } } //提交事务 tran.Commit(); //返回成功 return true; } catch (Exception ex) { //回滚事务 tran.Rollback(); //非数字返回失败 throw new Exception(ex.Message); } } } }
/// <summary> /// 根据输入的参数值来执行更新数据 /// </summary> /// <param name="e">传入的带有数据的事件参数</param> /// <returns></returns> private bool UpdateData(FormViewUpdateEventArgs e) { //检测是否更改了需要更新到其他部门的数据内容 if (e.OldValues["lot_id"].Equals(e.NewValues["lot_id"]) && e.OldValues["product_num"].Equals(e.NewValues["product_num"]) && e.OldValues["pnl_qty"].Equals(e.NewValues["pnl_qty"]) && e.OldValues["next_dept_name"].Equals(e.NewValues["next_dept_name"]) ) { //执行只更新本生产记录 e.Cancel = false; //更新事件继续执行 return false; } //数据适配器 //当前添加语句对象 //当前数据库连接 using (var da = new t_product_record_kailiaoTableAdapter()) using (var cmd = da.Adapter.UpdateCommand) using (var conn = cmd.Connection) { //打开数据库连接 conn.Open(); //开启事务 using (var tran = conn.BeginTransaction()) { //设置事务 da.Transaction = tran; //试运行 try { //部门名称 string deptName = e.OldValues["dept_name"].ToString(); //当前行id号 Int64 id = Convert.ToInt64(e.Keys["id"]); //获取数据 using (var tab = da.GetDataById(id)) { //检查是否获取到行 if (tab.Rows.Count == 0) { //显示失败 throw new Exception("当前生产记录已经被其他用户删除!"); } else { //首行 var row = (DataSetProductRecord.t_product_record_kailiaoRow)tab.Rows[0]; //下部门名称 string nextDeptName = row.next_dept_name; //lot卡号 string lotId = row.lot_id; //pnl出数 Int16 pnlQty = row.pnl_qty; //pcs出数 int pcsQty = row.pcs_qty; //检测是否超过下部门余数 if (ydOperateBalanceLotCrad.IsOutstripDeptBalance( tran, deptName, nextDeptName, lotId, pnlQty, pcsQty, true )) { return false; } //修改下部门lot卡余数 if (!ydOperateBalanceLotCrad.UpdateBalanceAfterInsert( tran, deptName, nextDeptName, lotId, pnlQty, pcsQty, true )) { return false; } //目标数据库表名 string toTabName = "[t_dept_lot_card_balance]"; //添加到部门结存lot卡 if (!ydOperateBalanceLotCrad.InsertOneRecordToLotCardBalance( tran, ydOperateBalanceLotCrad.GetTabNameByDeptName(deptName), deptName, toTabName, id )) { return false; } } } //部门名称 deptName = e.NewValues["dept_name"].ToString(); //保存一条记录到数据库 if (!ydOperateBalanceLotCrad.UpdateOneRecord( cmd, e, string.Empty, deptName )) { //失败就直接返回 return false; } //提交事务 tran.Commit(); //返回成功 return true; } catch (Exception ex) { //回滚事务 tran.Rollback(); //非数字返回失败 throw new Exception(ex.Message); } } } }
//导出数据到Excel protected void lBtnToExcel_Click(object sender, EventArgs e) { //获取数据并填充到数据表 using (var da = new t_product_record_kailiaoTableAdapter()) { //检测session中保存的查询条件 object obj = Session["product_record_kailiao_where_content"]; string str = string.Empty; if (obj != null && obj.ToString().Length > 0) { str = obj.ToString(); } //获取前1万行数据 var tab = da.GetPagedData(0, 10000, str, "order by [id] desc"); //设置列标题 string[] titleCol = ("序号,生产日期,部门名称,lot卡号,生产编号,pnl出数,pcs出数,下部门,备注,录入员,录取时间,修改时间").Split(','); //设置表名称 string titleTab = "开料部门生产记录"; //临时文件名称 string fileName = titleTab + DateTime.Now.ToString("yyMMddHHmmss") + ".xls"; //执行导出数据到excel bool isSuccess = ydPublicMethod.DataTable2Excel(tab, titleCol, ref fileName, titleTab); //如果成功则跳转到指定页面 if (isSuccess) { Response.Redirect(fileName); //停止加载后续内容 Response.End(); } } }
/// <summary> /// 根据输入的LOT格式来保存多条数据到数据库 /// </summary> /// <param name="e">传入的带有数据的事件参数</param> /// <param name="deptName">当前部门名称</param> /// <returns></returns> private bool InsertData(FormViewInsertEventArgs e, string deptName) { //当前用户输入的lot卡号 string strLotIdBegin = e.Values["lot_id"].ToString(); //当前用户输入的结束lot卡号 string strLotIdEnd = ((TextBox)fvProductRecordAdd.FindControl("txtLotIdEnd")).Text.Trim(); //数据适配器 //当前添加语句对象 //当前数据库连接 using (var da = new t_product_record_kailiaoTableAdapter()) using (var cmd = da.Adapter.InsertCommand) using (var conn = cmd.Connection) { //打开数据库连接 conn.Open(); //开启事务 using (var tran = conn.BeginTransaction()) { //设置事务 da.Transaction = tran; //试运行 try { //检测是否有结束lot卡号 if (strLotIdEnd.Length == 0) { //检测lot卡是否正确 //正则表达式 string strReg = @"^[1-9]\d(0[1-9]|1[012])-S?[1-9]\d{0,4}"; //检测是否正确lot卡号 if (!Regex.IsMatch(strLotIdBegin, strReg)) { //非数字返回失败 throw new Exception("您输入了一个不合格的lot卡号 " + strLotIdBegin + " !"); } //检测是否最小lot卡号 if (!ydPublicMethod.IsMinLotId(tran, deptName, strLotIdBegin)) { //直接返回 return false; } //保存一条记录到数据库 if (!ydOperateBalanceLotCrad.InsertOneRecord(cmd, strLotIdBegin, e)) { //失败就直接返回 return false; } } else { //正则表达式 string strReg = @"^[1-9]\d{0,4}$"; //检测是否正确lot卡号 if (!Regex.IsMatch(strLotIdEnd, strReg)) { //非数字返回失败 throw new Exception("您输入了一个不合格的结束lot卡号 " + strLotIdEnd + " !"); } //检测是否样板 bool isSample = strLotIdBegin.ToUpper().Contains("S"); //lot卡中的年份月份和数字索引号 string strYearMonth; int iLotIdStart; int iLotIdEnd = int.Parse(strLotIdEnd); if (!isSample) { strYearMonth = strLotIdBegin.Substring(0, 5); iLotIdStart = int.Parse(strLotIdBegin.Substring(5)); } else { strYearMonth = strLotIdBegin.Substring(0, 6); iLotIdStart = int.Parse(strLotIdBegin.Substring(6)); } //检测起始和结束lot卡号,让起始lot卡号最小 if (iLotIdStart > iLotIdEnd) { int i = iLotIdStart; iLotIdStart = iLotIdEnd; iLotIdEnd = i; } //lot卡不能太多,不允许超过10张 if (iLotIdEnd - iLotIdStart > 10) { //不允许超过10张 throw new Exception( string.Format( "您输入lot卡号段 {0} 到 {1}{2} 的lot卡太多,不允许超过10张!", strLotIdBegin, strYearMonth, strLotIdEnd ) ); } //区分生产和样板来执行保存操作 if (!isSample) { //获取订单序号的数据适配器 using (var daGetOrderId = new t_ppc_lot_cardTableAdapter()) { //设置数据库连接 daGetOrderId.Connection = conn; //设置事务 daGetOrderId.Transaction = tran; //订单序号 string orderId = string.Empty; //检测输入的lot卡号段是否在同一个订单序号中 for (int i = iLotIdStart; i <= iLotIdEnd; i++) { //获取 var tab = daGetOrderId.GetDataByLotId(strYearMonth + i.ToString()); //检查结果 if (tab.Rows.Count == 0) { //报告失败 throw new Exception( string.Format( "您输入lot卡号段 {0} 到 {1}{2} 不存在!", strLotIdBegin, strYearMonth, strLotIdEnd ) ); } else { //首张lot卡号只获取不比较 if (i == iLotIdStart) { //取得订单序号 orderId = ((DataSetPpcLotCardMgr.t_ppc_lot_cardRow)tab.Rows[0]).order_id; } else if (orderId != ((DataSetPpcLotCardMgr.t_ppc_lot_cardRow)tab.Rows[0]).order_id) { //报告失败 throw new Exception( string.Format( "您输入lot卡号段 {0} 到 {1}{2} 中的{1}{3}跟其之前的lot卡不是一张订单!", strLotIdBegin, strYearMonth, strLotIdEnd, i ) ); } } } //当前报数的pnl数以及pcs数 Int16 pnlQty = Convert.ToInt16(e.Values["pnl_qty"]); int pcsQty = Convert.ToInt32(e.Values["pcs_qty"]); //最后lot卡号 string strLotEnd = strYearMonth + iLotIdEnd.ToString(); //部门结余pnl总数及pcs总数 Int16 totalPnlQty = 0; int totalPcsQty = 0; //检测最后一LOT板的数量必须为本部门结存的全部数量 ydOperateBalanceLotCrad.GetDeptBalanceQty( tran, string.Empty, deptName, strLotEnd, out totalPnlQty, out totalPcsQty, true ); //检测是否不等于该数量 if (pnlQty == totalPnlQty && pcsQty == totalPcsQty) { } else { //是否一样 bool isSame = false; if (pnlQty != totalPnlQty && pcsQty != totalPcsQty) { //获取pnl含pcs数 Int16 pnlCountPcs = ydOperateBalanceLotCrad.GetPnlCountPcs(tran, strLotEnd); //检测是否一样 if (totalPnlQty * pnlCountPcs == pcsQty) { isSame = true; } } //不一样 if (!isSame) { //抛出错误 if (totalPnlQty > 0) { throw new Exception( string.Format( "如果有报结束lot卡号,则结束lot卡号({0})的数量必须为本部门总结存数 {1} pnl!", strLotEnd, totalPnlQty ) ); } else { throw new Exception( string.Format( "如果有报结束lot卡号,则结束lot卡号({0})的数量必须为本部门总结存数 {1} pcs!", strLotEnd, totalPcsQty ) ); } } } //执行保存 for (int i = iLotIdStart; i <= iLotIdEnd; i++) { //当前待保存的lot卡号 string curLotId = strYearMonth + i.ToString(); //检测是否最小lot卡号 if (!ydPublicMethod.IsMinLotId(tran, deptName, curLotId)) { //直接返回 return false; } //保存一条记录到数据库 if (!ydOperateBalanceLotCrad.InsertOneRecord(cmd, curLotId, e)) { //失败就直接返回 return false; } } } } else { //获取样板订单序号的数据适配器 using (var daGetOrderId = new t_sample_lot_cardTableAdapter()) { //设置数据库连接 daGetOrderId.Connection = conn; //设置事务 daGetOrderId.Transaction = tran; //样板订单序号 string orderId = string.Empty; //检测输入的lot卡号段是否在同一个订单序号中 for (int i = iLotIdStart; i <= iLotIdEnd; i++) { //获取 var tab = daGetOrderId.GetDataByLotId(strYearMonth + i.ToString()); //检查结果 if (tab.Rows.Count == 0) { //报告失败 throw new Exception( string.Format( "您输入的样板lot卡号段 {0} 到 {1}{2} 不存在!", strLotIdBegin, strYearMonth, strLotIdEnd ) ); } else { //首张lot卡号只获取不比较 if (i == iLotIdStart) { //取得订单序号 orderId = ((DataSetSampleLotCardMgr.t_sample_lot_cardRow)tab.Rows[0]).order_id; } else if (orderId != ((DataSetSampleLotCardMgr.t_sample_lot_cardRow)tab.Rows[0]).order_id) { //报告失败 throw new Exception( string.Format( "您输入的样板lot卡号段 {0} 到 {1}{2} 中的{1}{3}跟其之前的lot卡不是一张订单!", strLotIdBegin, strYearMonth, strLotIdEnd, i ) ); } } } //当前报数的pnl数以及pcs数 Int16 pnlQty = Convert.ToInt16(e.Values["pnl_qty"]); int pcsQty = Convert.ToInt32(e.Values["pcs_qty"]); //最后lot卡号 string strLotEnd = strYearMonth + iLotIdEnd.ToString(); //部门结余pnl总数及pcs总数 Int16 totalPnlQty = 0; int totalPcsQty = 0; //检测最后一LOT板的数量必须为本部门结存的全部数量 ydOperateBalanceLotCrad.GetDeptBalanceQty( tran, string.Empty, deptName, strLotEnd, out totalPnlQty, out totalPcsQty, true ); //检测是否不等于该数量 if (pnlQty == totalPnlQty && pcsQty == totalPcsQty) { } else { //是否一样 bool isSame = false; if (pnlQty != totalPnlQty && pcsQty != totalPcsQty) { //获取pnl含pcs数 Int16 pnlCountPcs = ydOperateBalanceLotCrad.GetPnlCountPcs(tran, strLotEnd); //检测是否一样 if (totalPnlQty * pnlCountPcs == pcsQty) { isSame = true; } } //不一样 if (!isSame) { //抛出错误 if (totalPnlQty > 0) { throw new Exception( string.Format( "如果有报结束lot卡号,则结束lot卡号({0})的数量必须为本部门总结存数 {1} pnl!", strLotEnd, totalPnlQty ) ); } else { throw new Exception( string.Format( "如果有报结束lot卡号,则结束lot卡号({0})的数量必须为本部门总结存数 {1} pcs!", strLotEnd, totalPcsQty ) ); } } } //执行保存 for (int i = iLotIdStart; i <= iLotIdEnd; i++) { //当前待保存的lot卡号 string curLotId = strYearMonth + i.ToString(); //检测是否最小lot卡号 if (!ydPublicMethod.IsMinLotId(tran, deptName, curLotId)) { //直接返回 return false; } //保存一条记录到数据库 if (!ydOperateBalanceLotCrad.InsertOneRecord(cmd, curLotId, e)) { //失败就直接返回 return false; } } } } } //提交事务 tran.Commit(); //返回成功 return true; } catch (Exception ex) { //回滚事务 tran.Rollback(); //非数字返回失败 throw new Exception(ex.Message); } } } }