Example #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));
            }
        }
Example #2
0
        public CommonServiceResult CommodityDetailUsedByCode(string commodityMasterCode, string batchCode, string commodityDetailCode)
        {
            try
            {
                using (MyBuyingContext context = new MyBuyingContext())
                {
                    CommodityDetail detail = context.CommodityDetails.FirstOrDefault(p =>
                                                                                     p.CommodityMasterCode == commodityMasterCode &&
                                                                                     p.BatchCode == batchCode &&
                                                                                     p.CommodityDetailCode == commodityDetailCode);

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

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

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

                    context.SaveChanges();
                }

                return(CommonServiceResult.DefaultSuccessResult);
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
                return(new CommonServiceResult(ex));
            }
        }
Example #3
0
        public CommonServiceResult CreateOrder(long commodityDetailID, string buyerCode)
        {
            try
            {
                using (MyBuyingContext context = new MyBuyingContext())
                {
                    CommodityDetail detail = context.CommodityDetails.Find(commodityDetailID);
                    if (detail == null)
                    {
                        // 数据不存在.
                        return(CommonServiceResult.DataNotFoundResult);
                    }

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

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

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

                    context.SaveChanges();
                }

                return(CommonServiceResult.DefaultSuccessResult);
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
                return(new CommonServiceResult(ex));
            }
        }
Example #4
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));
            }
        }
Example #5
0
        /// <summary>
        /// 简单创建商品明细
        /// </summary>
        /// <param name="commodityMasterCode"></param>
        /// <param name="batchCode"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public CommonServiceResult CreateCommodityDetail(string commodityMasterCode, string batchCode, int count)
        {
            try
            {
                using (MyBuyingContext context = new MyBuyingContext())
                {
                    // 先查询 商品主表是否存在.
                    CommodityMaster commodityMaster = context.CommodityMasters.Find(commodityMasterCode);
                    if (commodityMaster == null)
                    {
                        // 数据不存在.
                        return(CommonServiceResult.DataNotFoundResult);
                    }

                    // 再查询, 相同的商品主表代码+批次, 是否已存在有数据.
                    int dbDataCount = context.CommodityDetails.Count(p => p.CommodityMasterCode == commodityMasterCode && p.BatchCode == batchCode);
                    if (dbDataCount > 0)
                    {
                        // 数据已存在.
                        return(CommonServiceResult.DataHadExistsResult);
                    }

                    if (count <= 0)
                    {
                        CommonServiceResult errorResult = new CommonServiceResult()
                        {
                            ResultCode    = "COUNT_MUST_MORE_THAN_ZERO",
                            ResultMessage = "数量必须大于零。",
                        };
                        return(errorResult);
                    }


                    HashSet <int> resultCodes = new HashSet <int>();
                    Random        random      = new Random();

                    while (resultCodes.Count < count)
                    {
                        int num = random.Next(1, 99999999);
                        if (!resultCodes.Contains(num))
                        {
                            resultCodes.Add(num);
                        }
                    }

                    foreach (int num in resultCodes)
                    {
                        CommodityDetail commodityDetail = new CommodityDetail()
                        {
                            // 主表代码.
                            CommodityMasterCode = commodityMasterCode,
                            // 批次.
                            BatchCode = batchCode,
                            // 明细代码.
                            CommodityDetailCode = num.ToString("00000000"),

                            // 商品状态 : 可用.
                            CommodityStatus = CommodityDetail.COMMODITY_STATUS_IS_CREATED,
                        };

                        context.CommodityDetails.Add(commodityDetail);
                    }

                    context.SaveChanges();
                }


                return(CommonServiceResult.DefaultSuccessResult);
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
                return(new CommonServiceResult(ex));
            }
        }
Example #6
0
        /// <summary>
        /// 根据模板创建商品明细
        /// </summary>
        /// <param name="commodityMasterCode"></param>
        /// <param name="batchCode"></param>
        /// <returns></returns>
        public CommonServiceResult CreateCommodityDetailByTemplate(string commodityMasterCode, string batchCode)
        {
            try
            {
                using (MyBuyingContext context = new MyBuyingContext())
                {
                    // 先查询 商品主表是否存在.
                    CommodityMaster commodityMaster = context.CommodityMasters.Find(commodityMasterCode);
                    if (commodityMaster == null)
                    {
                        // 数据不存在.
                        return(CommonServiceResult.DataNotFoundResult);
                    }

                    // 再获取模板数据.
                    List <CommodityDetailTemplate> templateDataList = context.CommodityDetailTemplates.Where(p => p.CommodityMasterCode == commodityMasterCode).ToList();
                    if (templateDataList.Count == 0)
                    {
                        CommonServiceResult errorResult = new CommonServiceResult()
                        {
                            ResultCode    = "WITHOUT_TEMPLATE_DATA",
                            ResultMessage = "未查询到模板数据",
                        };
                        return(errorResult);
                    }

                    // 再查询, 相同的商品主表代码+批次, 是否已存在有数据.
                    int dbDataCount = context.CommodityDetails.Count(p => p.CommodityMasterCode == commodityMasterCode && p.BatchCode == batchCode);
                    if (dbDataCount > 0)
                    {
                        // 数据已存在.
                        return(CommonServiceResult.DataHadExistsResult);
                    }


                    HashSet <int> resultCodes = new HashSet <int>();
                    Random        random      = new Random();

                    while (resultCodes.Count < templateDataList.Count)
                    {
                        int num = random.Next(1, 99999999);
                        if (!resultCodes.Contains(num))
                        {
                            resultCodes.Add(num);
                        }
                    }

                    // 索引.
                    var index = 0;
                    foreach (int num in resultCodes)
                    {
                        CommodityDetail commodityDetail = new CommodityDetail()
                        {
                            // 主表代码.
                            CommodityMasterCode = commodityMasterCode,
                            // 批次.
                            BatchCode = batchCode,
                            // 明细代码.
                            CommodityDetailCode = num.ToString("00000000"),

                            // 商品状态 : 可用.
                            CommodityStatus = CommodityDetail.COMMODITY_STATUS_IS_CREATED,

                            // 附加数据.
                            CommodityDetailExpData = templateDataList[index].CommodityDetailExpData,
                        };

                        context.CommodityDetails.Add(commodityDetail);
                        index++;
                    }

                    context.SaveChanges();
                }


                return(CommonServiceResult.DefaultSuccessResult);
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
                return(new CommonServiceResult(ex));
            }
        }
        private void btnRegister_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
                {
                    IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted,
                    Timeout = new TimeSpan(2, 0, 0)
                }))
                {
                    var commodity               = new Commodity();
                    var commodityBusiness       = Business.GetCommodityBusiness();
                    var commodityDetailBusiness = Business.GetCommodityDetailBusiness();
                    if (CommodityId.HasValue)
                    {
                        commodity = commodityBusiness.GetById(CommodityId.Value);
                    }

                    commodity.CName          = txtGoodTitle.Text;
                    commodity.COrderPoint    = txtCOrderPoint.Text.ToNullableInt();
                    commodity.COrderMax      = txtCOrderMax.Text.ToNullableInt();
                    commodity.COrderMin      = txtCOrderMin.Text.ToNullableInt();
                    commodity.CPointCritical = txtCPointCritical.Text.ToNullableInt();
                    commodity.CInventoryMax  = txtCInventoryMax.Text.ToNullableInt();
                    commodityBusiness.Save(commodity);
                    this.CommodityId = commodity.ID;

                    var mainGroup = commodityDetailBusiness.GetByName(txtMainGroup.Text, Constants.CodeTitle.CommodityMainGroup.ToGUID());
                    if (mainGroup == null)
                    {
                        mainGroup = new CommodityDetail
                        {
                            IDCommodity = commodity.ID,
                            IDCodeTitle = Constants.CodeTitle.CommodityMainGroup,
                            CDName      = txtMainGroup.Text,
                            CDIDIn      = Business.GetCommodityDetailBusiness().GetNewCode(Constants.CodeTitle.CommodityMainGroup, string.Empty, txtMainGroup.Text)
                        };
                    }
                    else if (mainGroup.IDCommodity == commodity.ID)
                    {
                        mainGroup.CDName = txtMainGroup.Text;
                    }

                    else
                    {
                        var newMainGroup = new CommodityDetail
                        {
                            IDCommodity = commodity.ID,
                            IDCodeTitle = Constants.CodeTitle.CommodityMainGroup,
                            CDName      = txtMainGroup.Text,
                            CDIDIn      = mainGroup.CDIDIn
                        };

                        mainGroup = newMainGroup;
                    }

                    commodityDetailBusiness.Save(mainGroup);

                    var subsidiaryGroup = commodityDetailBusiness.GetByName(txtSubsidiaryGroup.Text, Constants.CodeTitle.CommoditySubsidiaryGroup.ToGUID());
                    if (subsidiaryGroup == null)
                    {
                        subsidiaryGroup = new CommodityDetail()
                        {
                            IDCodeTitle = Constants.CodeTitle.CommoditySubsidiaryGroup,
                            IDCommodity = commodity.ID,
                            CDName      = txtSubsidiaryGroup.Text,
                            CDIDIn      = Business.GetCommodityDetailBusiness().GetNewCode(Constants.CodeTitle.CommoditySubsidiaryGroup, mainGroup.CDIDIn, txtSubsidiaryGroup.Text)
                        };
                    }
                    else if (subsidiaryGroup.IDCommodity == commodity.ID)
                    {
                        subsidiaryGroup.CDName = txtSubsidiaryGroup.Text;
                    }
                    else
                    {
                        var newSubsidiaryGroup = new CommodityDetail
                        {
                            IDCommodity = commodity.ID,
                            IDCodeTitle = Constants.CodeTitle.CommoditySubsidiaryGroup,
                            CDName      = txtSubsidiaryGroup.Text,
                            CDIDIn      = subsidiaryGroup.CDIDIn
                        };

                        subsidiaryGroup = newSubsidiaryGroup;
                    }

                    commodityDetailBusiness.Save(subsidiaryGroup);

                    var commodityDetail = commodityDetailBusiness.GetByName(txtGoodTitle.Text, Constants.CodeTitle.CommodityTitle.ToGUID());
                    if (commodityDetail == null)
                    {
                        commodityDetail = new CommodityDetail()
                        {
                            IDCodeTitle = Constants.CodeTitle.CommodityTitle,
                            IDCommodity = commodity.ID,
                            CDName      = txtGoodTitle.Text,
                            CDIDIn      = Business.GetCommodityDetailBusiness().GetNewCode(Constants.CodeTitle.CommodityTitle, subsidiaryGroup.CDIDIn, txtGoodTitle.Text)
                        };
                    }
                    else if (commodityDetail.IDCommodity == commodity.ID)
                    {
                        commodityDetail.CDName = txtGoodTitle.Text;
                    }
                    else
                    {
                        var newCommodityDetail = new CommodityDetail
                        {
                            IDCommodity = commodity.ID,
                            IDCodeTitle = Constants.CodeTitle.CommodityTitle,
                            CDName      = txtGoodTitle.Text,
                            CDIDIn      = commodityDetail.CDIDIn
                        };

                        commodityDetail = newCommodityDetail;
                    }
                    commodityDetailBusiness.Save(commodityDetail);

                    scope.Complete();
                }

                this.Close();
            }
            catch
            {
                throw;
            }
        }