/// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="connection">数据连接</param>
 public BArrangeBillDetailBC(SqlConnection connection)
 {
     this.connection = connection;
     this.arrangeBillDetailBB = new BArrangeBillDetailBB(this.connection);
     this.errorDiaryBB = new SErrorDiaryBB(this.connection);
     this.operatDiaryBB = new SOperatDiaryBB(this.connection);
 }
 /// <summary>
 /// 构造函数
 /// </summary>
 public BArrangeBillDetailBC()
 {
     this.selfConn = true;
     this.connection = new SqlConnection(HS.Config.SqlDataObject.GetSqlConnectionString);
     this.connection.Open();
     this.arrangeBillDetailBB = new BArrangeBillDetailBB(this.connection);
     this.errorDiaryBB = new SErrorDiaryBB(this.connection);
     this.operatDiaryBB = new SOperatDiaryBB(this.connection);
 }
        //*****************************************************************************
        //do it later      do it later      do it later
        //*****************************************************************************
        /// <summary>
        /// 保存排托单信息
        /// </summary>
        /// <param name="arrangeBillModel">排托单实例</param>
        /// <param name="dtArrange">排托明细数据源</param>
        /// <param name="isSubmit">是否提交</param>
        /// <returns></returns>
        public bool SaveBArrangeBill(BArrangeBillData arrangeBillModel, DataTable dtArrange, bool isSubmit)
        {
            bool ret = false;
            BArrangeBillBB arrangeBillBB = new BArrangeBillBB(connection);
            BArriveBillBB arriveBillBB = new BArriveBillBB(connection);
            BArrangeBillDetailBB arrangeBillDetailBB = new BArrangeBillDetailBB(connection);
            SqlTransaction trans = null;

            try
            {
                if (this.transaction == null)
                {
                    trans = this.connection.BeginTransaction("TranSave");
                    arrangeBillBB.Transaction = trans;
                    arriveBillBB.Transaction = trans;
                    arrangeBillDetailBB.Transaction = trans;
                }
                else
                {
                    arrangeBillDetailBB.Transaction = this.transaction;
                    arriveBillBB.Transaction = this.transaction;
                    arrangeBillBB.Transaction = this.transaction;
                }

                BArriveBillData arriveBillModel = arriveBillBB.GetModel(arrangeBillModel.arriveBillNo);

                if (arrangeBillModel.id == 0)//新增排托单
                {
                    #region 新增排托单

                    if (isSubmit)//提交
                    {
                        arriveBillModel.instantState = "03";//排托单已提交
                        arriveBillModel.commitDt = arrangeBillModel.commitDt;
                        arriveBillModel.commitEmpId = arrangeBillModel.commitEmpId;
                    }
                    else//未提交
                    {
                        arriveBillModel.instantState = "02";//已生成排托单
                    }

                    //更改到货单状态
                    arriveBillBB.ModifyRecord(arriveBillModel);

                    //新增排托单信息
                    arrangeBillBB.AddRecord(arrangeBillModel);

                    foreach (DataRow dr in dtArrange.Rows)
                    {
                        if (dr["isdel"].ToString() == "0")//未删除
                        {
                            BArrangeBillDetailData model = new BArrangeBillDetailData();

                            model.arrangeBillNo = dr["arrangeBillNo"].ToString();
                            model.arriveBillNo = dr["arriveBillNo"].ToString();
                            model.boxNum = Convert.ToInt32(dr["boxNum"]);
                            model.financeBillNo = dr["financeBillNo"].ToString();
                            model.isrtDt = arrangeBillModel.isrtDt;
                            model.instantState = "01";
                            model.isrtEmpId = arrangeBillModel.isrtEmpId;
                            model.materialNo = dr["materialNo"].ToString();
                            model.palletIndex = Convert.ToInt32(dr["palletIndex"]);
                            model.region = Convert.ToInt32(dr["region"].ToString());

                            arrangeBillDetailBB.AddRecord(model);
                        }
                    }

                    #endregion 新增排托单
                }
                else
                {
                    #region 修改排托单信息

                    if (isSubmit)//提交
                    {
                        arriveBillModel.instantState = "03";//排托单已提交
                        arriveBillModel.commitDt = arrangeBillModel.commitDt;
                        arriveBillModel.commitEmpId = arrangeBillModel.commitEmpId;
                    }
                    else//未提交
                    {
                        arriveBillModel.instantState = "02";//已生成排托单
                    }

                    //更改到货单状态
                    arriveBillBB.ModifyRecord(arriveBillModel);

                    //修改排托单信息
                    arrangeBillBB.ModifyRecord(arrangeBillModel);

                    foreach (DataRow dr in dtArrange.Rows)
                    {
                        if (Convert.ToInt32(dr["id"]) > 0 && dr["isdel"].ToString() == "1")//删除方法
                        {
                            arrangeBillDetailBB.DeleteRecord(Convert.ToInt32(dr["id"]));
                        }
                        else if (Convert.ToInt32(dr["id"]) > 0 && dr["isdel"].ToString() == "0")//修改
                        {
                            BArrangeBillDetailData model = arrangeBillDetailBB.GetModel(Convert.ToInt32(dr["id"]));

                            model.boxNum = Convert.ToInt32(dr["boxNum"]);
                            model.region = Convert.ToInt32(dr["region"]);
                            model.updtDt = DateTime.Now.ToString();
                            model.updtEmpId = arrangeBillModel.updtEmpId;

                            arrangeBillDetailBB.ModifyRecord(model);
                        }
                        else if (Convert.ToInt32(dr["id"]) == 0 && dr["isdel"].ToString() == "0")//添加
                        {
                            BArrangeBillDetailData model = new BArrangeBillDetailData();

                            model.arrangeBillNo = dr["arrangeBillNo"].ToString();
                            model.arriveBillNo = dr["arriveBillNo"].ToString();
                            model.boxNum = Convert.ToInt32(dr["boxNum"]);
                            model.financeBillNo = dr["financeBillNo"].ToString();
                            model.isrtDt = arrangeBillModel.isrtDt;
                            model.instantState = "01";
                            model.isrtEmpId = arrangeBillModel.isrtEmpId;
                            model.materialNo = dr["materialNo"].ToString();
                            model.palletIndex = Convert.ToInt32(dr["palletIndex"]);
                            model.region = Convert.ToInt32(dr["region"].ToString());

                            arrangeBillDetailBB.AddRecord(model);
                        }
                    }

                    #endregion 修改排托单信息
                }

                if (this.transaction == null) trans.Commit();
                ret = true;
            }
            catch
            {
                if (transaction == null) trans.Rollback("TranSave");
                throw new Exception("新增排拖单出错了!");
            }
            finally
            {
                arrangeBillBB.Dispose();
                arriveBillBB.Dispose();
                arrangeBillDetailBB.Dispose();
            }

            return ret;
        }