Exemplo n.º 1
0
        public CommonServiceResult <long> GetFirstBuyableCommodityDetail(string commodityMasterCode, string batchCode, string buyerCode)
        {
            try
            {
                using (MyBuyingContext context = new MyBuyingContext())
                {
                    CommodityDetail detail = context.CommodityDetails.FirstOrDefault(p =>
                                                                                     p.CommodityMasterCode == commodityMasterCode &&
                                                                                     p.BatchCode == batchCode &&
                                                                                     p.CommodityStatus == CommodityDetail.COMMODITY_STATUS_IS_CREATED);

                    if (detail == null)
                    {
                        // 数据不存在.
                        return(CommonServiceResult <long> .DataNotFoundResult);
                    }

                    // 修改状态.
                    detail.CommodityStatus = CommodityDetail.COMMODITY_STATUS_IS_LOCKED;

                    // 购买人.
                    detail.BuyerCode = buyerCode;

                    context.SaveChanges();

                    return(CommonServiceResult <long> .CreateDefaultSuccessResult(detail.CommodityDetailID));
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
                return(new CommonServiceResult <long>(ex));
            }
        }
        /// <summary>
        /// 入队列.
        /// </summary>
        /// <param name="buyingData"></param>
        /// <returns></returns>
        public CommonServiceResult <int> Enqueue(BuyingData buyingData)
        {
            if (logger.IsDebugEnabled)
            {
                logger.DebugFormat("入队列: {0}", buyingData);
            }

            lock (locker)
            {
                if (this.userCodeSet.Contains(buyingData.BuyerCode))
                {
                    CommonServiceResult <int> errorResult = new CommonServiceResult <int>()
                    {
                        ResultCode    = "BUYER_CODE_HAD_EXISTS",
                        ResultMessage = "用户代码已经存在于队列中",
                    };
                    return(errorResult);
                }

                this.userCodeSet.Add(buyingData.BuyerCode);
                this.userCodeQueue.Enqueue(buyingData);

                var queueCount = this.userCodeQueue.Count();

                // 通知 其他等待的线程
                Monitor.Pulse(locker);

                return(CommonServiceResult <int> .CreateDefaultSuccessResult(queueCount));
            }
        }
        CommonServiceResult IJobProcessService.ExecuteJob(Job job)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(job.JobSetting))
                {
                    conn.Open();

                    // 创建一个 Command.
                    SqlCommand sqlCommand = conn.CreateCommand();

                    // 创建DataSet,用于存储数据.
                    DataSet resultDataSet = new DataSet();

                    // 创建一个适配器
                    SqlDataAdapter adapter = new SqlDataAdapter(job.JobCommand, conn);
                    // 执行查询,并将数据导入DataSet.
                    adapter.Fill(resultDataSet, "result_data");

                    // 取得 DataTable.
                    DataTable dt = resultDataSet.Tables["result_data"];

                    string json = JsonConvert.SerializeObject(dt);

                    // 将 DataTable 返回出去.
                    return(CommonServiceResult.CreateDefaultSuccessResult(json));
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
                return(new CommonServiceResult(ex));
            }
        }
        CommonServiceResult IJobProcessService.ExecuteJob(Job job)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(job.JobSetting))
                {
                    conn.Open();

                    // 创建一个 Command.
                    SqlCommand sqlCommand = conn.CreateCommand();

                    // 定义需要执行的SQL语句.
                    sqlCommand.CommandText = job.JobCommand;

                    // ExecuteNonQuery 方法,表明本次操作,不是一个查询的操作。将没有结果集合返回.
                    // 返回的数据,将是 被影响的记录数.
                    int rowCount = sqlCommand.ExecuteNonQuery();

                    // 将更新行数返回出去.
                    return(CommonServiceResult.CreateDefaultSuccessResult(rowCount));
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
                return(new CommonServiceResult(ex));
            }
        }
Exemplo n.º 5
0
        public CommonServiceResult <CommodityDetail> OrderPayed(long commodityDetailID)
        {
            try
            {
                using (MyBuyingContext context = new MyBuyingContext())
                {
                    CommodityDetail detail = context.CommodityDetails.Find(commodityDetailID);
                    if (detail == null)
                    {
                        // 数据不存在.
                        return(CommonServiceResult <CommodityDetail> .DataNotFoundResult);
                    }

                    if (detail.CommodityStatus != CommodityDetail.COMMODITY_STATUS_IS_PAYING)
                    {
                        CommonServiceResult <CommodityDetail> errorResult = new CommonServiceResult <CommodityDetail>()
                        {
                            ResultCode    = "COMMODITY_STATUS_ERROR",
                            ResultMessage = "状态数据无效!",
                        };
                        return(errorResult);
                    }

                    // 修改状态.
                    detail.CommodityStatus = CommodityDetail.COMMODITY_STATUS_IS_PAYED;

                    context.SaveChanges();


                    return(CommonServiceResult <CommodityDetail> .CreateDefaultSuccessResult(detail));
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
                return(new CommonServiceResult <CommodityDetail>(ex));
            }
        }