码迷,mamicode.com
首页 > 其他好文 > 详细

真爱生命,远离外包!哈哈

时间:2016-07-28 14:13:35      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

这外包的水平真是不敢恭维,只要达到效果从不考虑效率

看看这个代码:

        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model">退货申请实体</param>
        public int AddReturnOrder(www.bpj.com.webservice.Model.ReturnOrder model, int retractableDays)
        {
            // 获取订单ID
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select");
            strSql.Append("  OrderId");
            strSql.Append("  , datediff(day, CreateTime, getdate()) as days ");
            strSql.Append("from");
            strSql.Append("  Orders ");
            strSql.Append("where");
            strSql.Append("  OrderSn = @OrderSn ");
            strSql.Append("  and IsDeleted = 0 ");
            strSql.Append("  and UserDeleted = 0");

            SqlParameter[] parameters = { new SqlParameter("@OrderSn", SqlDbType.NVarChar, 50) };
            parameters[0].Value = model.OrderSn;

            DataSet dsOrder = SqlServerHelper.Query(strSql.ToString(), parameters);
            DataTable dtOrder = dsOrder.Tables[0];

            String strOrderId = string.Empty;
            if (dtOrder.Rows.Count == 0)
            {
                return -1;
            }
            else
            {
                int days = int.Parse(dtOrder.Rows[0]["days"].ToString());
                if (days >= retractableDays)
                {
                    return -2;
                }

                strOrderId = dtOrder.Rows[0]["OrderId"].ToString();
            }

            // 获取可退数量
            strSql = new StringBuilder();
            strSql.Append("select");
            strSql.Append("  OrderProductsId, RetractableCount ");
            strSql.Append("from");
            strSql.Append("  OrderProducts ");
            strSql.Append("where");
            strSql.Append("  OrderId = @OrderId ");
            strSql.Append("  and ProductSKUId = @ProductSKUId ");
            strSql.Append("  AND IsDeleted = 0");

            parameters = new SqlParameter[] { new SqlParameter("@OrderId", SqlDbType.Int, 4)
                , new SqlParameter("@ProductSKUId", SqlDbType.Int, 4) };
            parameters[0].Value = strOrderId;
            parameters[1].Value = model.ProductSKUId;

            DataSet dsOrderProduct = SqlServerHelper.Query(strSql.ToString(), parameters);
            DataTable dtOrderProduct = dsOrderProduct.Tables[0];

            String strOrderProductsId = string.Empty;
            if (dtOrderProduct.Rows.Count == 0)
            {
                return -1;
            }
            else
            {
                int retractableCount = int.Parse(dtOrderProduct.Rows[0]["RetractableCount"].ToString());
                if (retractableCount - model.ReturnCount < 0)
                {
                    return -3;
                }
                strOrderProductsId = dtOrderProduct.Rows[0]["OrderProductsId"].ToString();
            }

            // 订单商品表更新
            Dictionary<string, SqlParameter[]> dicSQLStringList = new Dictionary<string,SqlParameter[]>();
            strSql = new StringBuilder();
            strSql.Append("update OrderProducts set RetractableCount = RetractableCount - @ReturnCount from OrderProducts where OrderProductsId = @OrderProductsId");

            parameters = new SqlParameter[] { new SqlParameter("@ReturnCount", SqlDbType.Int, 4)
                , new SqlParameter("@OrderProductsId", SqlDbType.Int, 4) };
            parameters[0].Value = model.ReturnCount;
            parameters[1].Value = strOrderProductsId;

            dicSQLStringList.Add(strSql.ToString(), parameters);

            //SqlServerHelper.ExecuteSql(strSql.ToString(), parameters);

            strSql = new StringBuilder();
            strSql.Append("insert into ReturnOrder(");
            strSql.Append("IsDeleted,UserDeleted,OrderId,ProductSKUId,UserId,ReturnCount,ApplyReturnMoney,ReturnMoney,CreateTime,Status,ReturnOrderReason,ReturnDealResult,ReturnOrderSn,PayPic)");
            strSql.Append(" values (");
            strSql.Append("@IsDeleted,@UserDeleted,@OrderId,@ProductSKUId,@UserId,@ReturnCount,@ApplyReturnMoney,0,getdate(),1,@ReturnOrderReason,null,@ReturnOrderSn,null)");
            //strSql.Append(";select @id=@@IDENTITY");
            parameters = new SqlParameter[] {
					new SqlParameter("@IsDeleted", SqlDbType.Bit,1),
					new SqlParameter("@UserDeleted", SqlDbType.Int,4),
					new SqlParameter("@OrderId", SqlDbType.Int,4),
					new SqlParameter("@ProductSKUId", SqlDbType.Int,4),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@ReturnCount", SqlDbType.Int,4),
					new SqlParameter("@ApplyReturnMoney", SqlDbType.Decimal,9),
					new SqlParameter("@ReturnOrderReason", SqlDbType.NText),
                    new SqlParameter("@ReturnOrderSn", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.IsDeleted;
            parameters[1].Value = model.UserDeleted;
            parameters[2].Value = strOrderId;
            parameters[3].Value = model.ProductSKUId;
            parameters[4].Value = model.UserId;
            parameters[5].Value = model.ReturnCount;
            parameters[6].Value = model.ApplyReturnMoney;
            parameters[7].Value = model.ReturnOrderReason;
            parameters[8].Value = model.ReturnOrderSn;

            dicSQLStringList.Add(strSql.ToString(), parameters);

            SqlServerHelper.ExecuteSqlTran(dicSQLStringList);

            // 退货申请ID
            strSql = new StringBuilder();
            strSql.Append("select ReturnOrderId from ReturnOrder where ReturnOrderSn = @ReturnOrderSn");

            parameters = new SqlParameter[] { new SqlParameter("@ReturnOrderSn", SqlDbType.NVarChar, 50) };
            parameters[0].Value = model.ReturnOrderSn;

            DataSet dsRet = SqlServerHelper.Query(strSql.ToString(), parameters);
            int intId = -1;
            if (dsRet.Tables[0].Rows.Count > 0)
            {
                intId = int.Parse(dsRet.Tables[0].Rows[0]["ReturnOrderId"].ToString());
            }

            return intId;
        }

 这一个新增的方法连接5次数据库 大神啊 !你是BDQN毕业的吧!!!

哥还是优化下吧:

 public int Add(API.Model.MReturnOrder model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@ReturnOrderId", SqlDbType.Int,4),
					new SqlParameter("@ProductSKUId", SqlDbType.Int,4),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@ReturnCount", SqlDbType.Int,4),
					new SqlParameter("@ApplyReturnMoney", SqlDbType.Decimal,9),
					new SqlParameter("@ReturnOrderReason", SqlDbType.NText),
					new SqlParameter("@ReturnOrderSn", SqlDbType.NVarChar,50),
                                        new SqlParameter("@OrderSn", SqlDbType.NVarChar,50)
				};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.ProductSKUId;
            parameters[2].Value = model.UserId;
            parameters[3].Value = model.ReturnCount;
            parameters[4].Value = model.ApplyReturnMoney;
            parameters[5].Value = model.ReturnOrderReason;
            parameters[6].Value = DateTime.Now.ToString("yyyyMMddhhmmssfff") + model.UserId.PadLeft(5, ‘0‘) + "RO";
            DbHelperSQL.RunProcedure("api_ReturnOrder_ADD", parameters, out rowsAffected);
            return (int)parameters[0].Value;
        }

 存储过程:

CREATE PROCEDURE api_ReturnOrder_ADD
@ReturnOrderId int output,
@ProductSKUId int,
@UserId int,
@ReturnCount int,
@ApplyReturnMoney decimal(18,2),
@ReturnOrderReason ntext,
@ReturnOrderSn nvarchar(50),
@OrderSn nvarchar(50)
 AS 
    declare @OrderId int 
    declare @d int
    declare @OrderProductsId int
    declare @RetractableCount int 
    if exists(select OrderSn from Orders where OrderSn = @OrderSn and IsDeleted = 0 and UserDeleted = 0)
    begin
      select @OrderId=OrderId , @d=datediff(day, CreateTime, getdate()) from Orders where  OrderSn = @OrderSn and IsDeleted = 0 and UserDeleted = 0
      if @d>=7
      begin
          select @OrderProductsId=OrderProductsId, @RetractableCount=RetractableCount from OrderProducts where OrderId = @OrderId and ProductSKUId = @ProductSKUId AND IsDeleted = 0
          if @@ROWCOUNT>0
          begin
             if (@RetractableCount-@ReturnCount)<0
             begin
               set @ReturnOrderId = -3
             end
             begin
                update OrderProducts set RetractableCount = RetractableCount - @ReturnCount from OrderProducts where OrderProductsId = @OrderProductsId
                INSERT INTO [ReturnOrder]([IsDeleted],[UserDeleted],[OrderId],[ProductSKUId],[UserId],[ReturnCount],[ApplyReturnMoney],[ReturnMoney],[CreateTime],[Status],[ReturnOrderReason],[ReturnOrderSn],[RebateCalculatedStatus]
				)VALUES(0,0,@OrderId,@ProductSKUId,@UserId,@ReturnCount,@ApplyReturnMoney,0,GETDATE(),1,@ReturnOrderReason,@ReturnOrderSn,0)
				SET @ReturnOrderId = @@IDENTITY
             end
          end
          else
          begin
             set @ReturnOrderId=-1
          end
      end
      begin
        set @ReturnOrderId = -2
      end 
    end
    else
    begin
      set @ReturnOrderId=-1
    end
GO

 本文纯属吐糟 ,勿喷!

真爱生命,远离外包!哈哈

标签:

原文地址:http://www.cnblogs.com/jasonduan/p/5714388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!