예제 #1
0
파일: Add.ashx.cs 프로젝트: uvbs/mmp
        public void ProcessRequest(HttpContext context)
        {
            RequestModel requestModel = new RequestModel();

            try
            {
                requestModel = JsonConvert.DeserializeObject <RequestModel>(context.Request["data"]);
            }
            catch (Exception ex)
            {
                resp.errcode = (int)APIErrCode.OperateFail;
                resp.errmsg  = ex.Message;
                bllLottery.ContextResponse(context, resp);
                return;
            }
            if (string.IsNullOrWhiteSpace(requestModel.lottery_name))
            {
                resp.errcode = (int)APIErrCode.PrimaryKeyIncomplete;
                resp.errmsg  = "请输入活动名称";
                bllLottery.ContextResponse(context, resp);
                return;
            }
            if (string.IsNullOrEmpty(requestModel.lottery_type))
            {
                resp.errcode = (int)APIErrCode.PrimaryKeyIncomplete;
                resp.errmsg  = "请输入活动类型";
                bllLottery.ContextResponse(context, resp);
                return;
            }
            if (requestModel.awards == null || requestModel.awards.Count <= 0)
            {
                resp.errcode = (int)APIErrCode.PrimaryKeyIncomplete;
                resp.errmsg  = "至少添加一个奖项";
                bllLottery.ContextResponse(context, resp);
                return;
            }
            foreach (var item in requestModel.awards)
            {
                if (string.IsNullOrEmpty(item.prize_name))
                {
                    resp.errcode = (int)APIErrCode.PrimaryKeyIncomplete;
                    resp.errmsg  = "奖项名称不能为空";
                    bllLottery.ContextResponse(context, resp);
                    return;
                }
                if (string.IsNullOrEmpty(item.value))
                {
                    resp.errcode = (int)APIErrCode.PrimaryKeyIncomplete;
                    resp.errmsg  = "value为必填项,请检查";
                    bllLottery.ContextResponse(context, resp);
                    return;
                }
            }

            if (requestModel.awards.Sum(p => p.probability) > 100)
            {
                resp.errcode = (int)APIErrCode.PrimaryKeyIncomplete;
                resp.errmsg  = "中奖比例之和不能大于100";
                bllLottery.ContextResponse(context, resp);
                return;
            }
            BLLTransaction tran = new BLLTransaction();

            try
            {
                WXLotteryV1 lotteryModel = new WXLotteryV1();
                lotteryModel.InsertDate      = DateTime.Now;
                lotteryModel.LotteryID       = int.Parse(bllLottery.GetGUID(TransacType.AddLottery));
                lotteryModel.BackGroundColor = requestModel.background_color;
                lotteryModel.LotteryType     = requestModel.lottery_type;
                lotteryModel.LotteryName     = requestModel.lottery_name;
                lotteryModel.LotteryContent  = requestModel.lottery_content;
                lotteryModel.MaxCount        = requestModel.max_count;
                lotteryModel.Status          = requestModel.status;
                lotteryModel.ThumbnailsPath  = requestModel.thumbnails_path;
                lotteryModel.WebsiteOwner    = bllLottery.WebsiteOwner;
                if (requestModel.start_time > 0)
                {
                    lotteryModel.StartTime = bllLottery.GetTime(requestModel.start_time);
                }
                lotteryModel.ShareImg             = requestModel.share_img;
                lotteryModel.ShareDesc            = requestModel.share_desc;
                lotteryModel.IsGetPrizeFromMobile = requestModel.is_get_prize_from_mobile;
                lotteryModel.ToolbarButton        = requestModel.toolbar_button;
                if (requestModel.end_time > 0)
                {
                    lotteryModel.EndTime = bllLottery.GetTime(requestModel.end_time);
                }
                lotteryModel.LuckLimitType = requestModel.limit_type;
                lotteryModel.UsePoints     = requestModel.use_points;
                lotteryModel.WinLimitType  = requestModel.win_limit_type;

                if (!bllLottery.Add(lotteryModel, tran))
                {
                    tran.Rollback();
                    resp.errcode = (int)APIErrCode.PrimaryKeyIncomplete;
                    resp.errmsg  = "添加活动失败";
                    bllLottery.ContextResponse(context, resp);
                    return;
                }

                foreach (var item in requestModel.awards)
                {
                    WXAwardsV1 award = new WXAwardsV1();
                    award.LotteryId   = lotteryModel.LotteryID;
                    award.PrizeCount  = item.prize_count;
                    award.PrizeName   = item.prize_name;
                    award.Probability = item.probability;
                    award.Img         = item.img;
                    award.AwardsType  = item.awards_type;
                    award.Value       = item.value;
                    award.Description = item.description;

                    if (!bllLottery.Add(award, tran))
                    {
                        tran.Rollback();
                        resp.errcode = (int)APIErrCode.PrimaryKeyIncomplete;
                        resp.errmsg  = "添加奖项失败";
                        bllLottery.ContextResponse(context, resp);
                        return;
                    }
                }
                tran.Commit();
                resp.isSuccess = true;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                resp.errcode = (int)APIErrCode.OperateFail;
                resp.errmsg  = ex.Message;
            }
            bllLottery.ContextResponse(context, resp);
        }
예제 #2
0
        public void ProcessRequest(HttpContext context)
        {
            //force_delete等于1时,进行强制删除,会清除原有抽奖
            string       force_delete = context.Request["force_delete"];
            RequestModel requestModel = new RequestModel();

            try
            {
                requestModel = JsonConvert.DeserializeObject <RequestModel>(context.Request["data"]);
            }
            catch (Exception ex)
            {
                resp.errcode = (int)APIErrCode.OperateFail;
                resp.errmsg  = ex.Message;
                bllLottery.ContextResponse(context, resp);
                return;
            }

            if (requestModel.id == 0)
            {
                resp.errcode = (int)APIErrCode.PrimaryKeyIncomplete;
                resp.errmsg  = "请输入Id";
                bllLottery.ContextResponse(context, resp);
                return;
            }
            WXLotteryV1 lotteryModel = bllLottery.GetByKey <WXLotteryV1>("LotteryID", requestModel.id.ToString());

            if (lotteryModel == null)
            {
                resp.errcode = (int)APIErrCode.OperateFail;
                resp.errmsg  = "活动没有找到";
                bllLottery.ContextResponse(context, resp);
                return;
            }
            if (string.IsNullOrWhiteSpace(requestModel.lottery_name))
            {
                resp.errcode = (int)APIErrCode.PrimaryKeyIncomplete;
                resp.errmsg  = "请输入活动名称";
                bllLottery.ContextResponse(context, resp);
                return;
            }
            if (requestModel.awards == null || requestModel.awards.Count <= 0)
            {
                resp.errcode = (int)APIErrCode.PrimaryKeyIncomplete;
                resp.errmsg  = "至少添加一个奖项";
                bllLottery.ContextResponse(context, resp);
                return;
            }
            foreach (var item in requestModel.awards)
            {
                if (string.IsNullOrEmpty(item.prize_name))
                {
                    resp.errcode = (int)APIErrCode.PrimaryKeyIncomplete;
                    resp.errmsg  = "奖项名称不能为空";
                    bllLottery.ContextResponse(context, resp);
                    return;
                }
                if (string.IsNullOrEmpty(item.value))
                {
                    resp.errcode = (int)APIErrCode.PrimaryKeyIncomplete;
                    resp.errmsg  = "value为必填项,请检查";
                    bllLottery.ContextResponse(context, resp);
                    return;
                }
            }
            if (requestModel.awards.Sum(p => p.probability) > 100)
            {
                resp.errcode = (int)APIErrCode.PrimaryKeyIncomplete;
                resp.errmsg  = "中奖比例之和不能大于100";
                bllLottery.ContextResponse(context, resp);
                return;
            }


            List <RequestAwardModel> AddRequestAwardList    = requestModel.awards.Where(p => p.id == 0).ToList();
            List <RequestAwardModel> UpdateRequestAwardList = requestModel.awards.Where(p => p.id > 0).ToList();

            List <WXAwardsV1> DeleteAwardList = new List <WXAwardsV1>();
            List <WXAwardsV1> AddAwardList    = new List <WXAwardsV1>();
            List <WXAwardsV1> UpdateAwardList = new List <WXAwardsV1>();

            #region 参数对应 检查奖项

            lotteryModel.BackGroundColor      = requestModel.background_color;
            lotteryModel.LotteryName          = requestModel.lottery_name;
            lotteryModel.LotteryType          = requestModel.lottery_type;
            lotteryModel.LotteryContent       = requestModel.lottery_content;
            lotteryModel.MaxCount             = requestModel.max_count;
            lotteryModel.Status               = requestModel.status;
            lotteryModel.ThumbnailsPath       = requestModel.thumbnails_path;
            lotteryModel.StartTime            = requestModel.start_time;
            lotteryModel.ShareImg             = requestModel.share_img;
            lotteryModel.ShareDesc            = requestModel.share_desc;
            lotteryModel.IsGetPrizeFromMobile = requestModel.is_get_prize_from_mobile;
            lotteryModel.ToolbarButton        = requestModel.toolbar_button;
            lotteryModel.EndTime              = requestModel.end_time;
            lotteryModel.LuckLimitType        = requestModel.limit_type;
            lotteryModel.UsePoints            = requestModel.use_points;
            lotteryModel.WinLimitType         = requestModel.win_limit_type;

            List <WXAwardsV1> OldAwardList = bllLottery.GetListByKey <WXAwardsV1>("LotteryId", lotteryModel.LotteryID.ToString());//旧奖项
            foreach (var item in OldAwardList)
            {
                RequestAwardModel nAward = UpdateRequestAwardList.FirstOrDefault(p => p.id == item.AutoID);
                if (nAward == null)//该奖项被删除了
                {
                    if (force_delete != "1")
                    {
                        //检查该奖项是否有中奖记录,有的话不可以删除
                        int recordcount = bllLottery.GetCountByKey <WXLotteryRecordV1>("WXAwardsId", item.AutoID.ToString());
                        if (recordcount > 0)
                        {
                            resp.errcode = (int)APIErrCode.OperateFail;
                            resp.errmsg  = string.Format("{0}已经有人中奖,不能删除", item.PrizeName);
                            bllLottery.ContextResponse(context, resp);
                            return;
                        }
                        else
                        {
                            DeleteAwardList.Add(item);
                        }
                    }
                    else
                    {
                        DeleteAwardList.Add(item);
                    }
                }
                else
                {
                    item.PrizeName   = nAward.prize_name;
                    item.PrizeCount  = nAward.prize_count;
                    item.Probability = nAward.probability;
                    item.Img         = nAward.img;
                    item.AwardsType  = nAward.awards_type;
                    item.Value       = nAward.value;
                    item.Description = nAward.description;
                    UpdateAwardList.Add(item);
                }
            }

            foreach (var item in AddRequestAwardList)
            {
                WXAwardsV1 award = new WXAwardsV1();
                award.LotteryId   = lotteryModel.LotteryID;
                award.PrizeCount  = item.prize_count;
                award.PrizeName   = item.prize_name;
                award.Probability = item.probability;
                award.Img         = item.img;
                award.AwardsType  = item.awards_type;
                award.Value       = item.value;
                award.Description = item.description;
                AddAwardList.Add(award);
            }
            #endregion

            BLLTransaction tran = new BLLTransaction();
            try
            {
                if (!bllLottery.Update(lotteryModel, tran))
                {
                    tran.Rollback();
                    resp.errcode = (int)APIErrCode.PrimaryKeyIncomplete;
                    resp.errmsg  = "更新活动失败";
                    bllLottery.ContextResponse(context, resp);
                    return;
                }

                if (DeleteAwardList.Count > 0)
                {
                    foreach (var item in DeleteAwardList)
                    {
                        if (bllLottery.Delete(item, tran) <= 0)
                        {
                            tran.Rollback();
                            resp.errcode = (int)APIErrCode.OperateFail;
                            resp.errmsg  = "删除旧奖项失败";
                            bllLottery.ContextResponse(context, resp);
                            return;
                        }
                        else
                        {
                            if (bllLottery.DeleteByKey <WXLotteryRecordV1>("WXAwardsId", item.AutoID.ToString(), tran) < 0)
                            {
                                tran.Rollback();
                                resp.errcode = (int)APIErrCode.OperateFail;
                                resp.errmsg  = "清除中奖记录失败";
                                bllLottery.ContextResponse(context, resp);
                                return;
                            }
                        }
                    }
                }
                if (UpdateAwardList.Count > 0)
                {
                    foreach (var item in UpdateAwardList)
                    {
                        if (!bllLottery.Update(item, tran))
                        {
                            tran.Rollback();
                            resp.errcode = (int)APIErrCode.OperateFail;
                            resp.errmsg  = "更新奖项失败";
                            bllLottery.ContextResponse(context, resp);
                            return;
                        }
                    }
                }
                if (AddAwardList.Count > 0)
                {
                    foreach (var item in AddAwardList)
                    {
                        if (!bllLottery.Add(item, tran))
                        {
                            tran.Rollback();
                            resp.errcode = (int)APIErrCode.OperateFail;
                            resp.errmsg  = "新增奖项失败";
                            bllLottery.ContextResponse(context, resp);
                            return;
                        }
                    }
                }

                tran.Commit();
                resp.errcode   = (int)APIErrCode.IsSuccess;
                resp.isSuccess = true;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                resp.errcode = (int)APIErrCode.OperateFail;
                resp.errmsg  = ex.Message;
            }
            bllLottery.ContextResponse(context, resp);
        }