示例#1
0
        /// <summary>
        /// 规则修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task <bool> UpdateAsync(RuleReplyDto model)
        {
            using (var tran = databaseFixture.Db.BeginTransaction())
            {
                try
                {
                    var dbrule = await databaseFixture.Db.WxRule.FindByIdAsync(model.Id);

                    dbrule.ResponseMsgType = model.ResponseMsgType;
                    dbrule.RuleName        = model.RuleName;
                    dbrule.RuleType        = model.RuleType;
                    switch ((WxRuleType)model.RuleType)
                    {
                    case WxRuleType.Normal:
                        dbrule.RequestMsgType = (int)RequestMsgType.Text;
                        break;

                    case WxRuleType.Subscribe:
                        dbrule.RequestMsgType = (int)RequestMsgType.Event;
                        break;

                    case WxRuleType.Unmatched:
                        dbrule.RequestMsgType = model.RequestMsgType;
                        break;
                    }
                    await databaseFixture.Db.WxRule.UpdateAsync(dbrule, tran);

                    switch ((RequestMsgType)dbrule.RequestMsgType)
                    {
                    case RequestMsgType.Text:    //关键词
                        var dbkeywords = await databaseFixture.Db.WxKeyword.FindAllAsync(m => m.RuleId == dbrule.Id);

                        foreach (var item in dbkeywords)
                        {
                            await databaseFixture.Db.WxKeyword.DeleteAsync(item, tran);
                        }
                        foreach (var item in model.Keywords)
                        {
                            WxKeyword keyword = new WxKeyword
                            {
                                RuleId  = dbrule.Id,
                                Keyword = item
                            };
                            await databaseFixture.Db.WxKeyword.InsertAsync(keyword, tran);
                        }
                        break;

                    case RequestMsgType.Image:
                        break;

                    case RequestMsgType.Voice:
                        break;

                    case RequestMsgType.Video:
                        break;

                    case RequestMsgType.Location:
                        break;

                    case RequestMsgType.Link:
                        break;

                    case RequestMsgType.ShortVideo:
                        break;

                    case RequestMsgType.Event:
                        break;

                    default:
                        break;
                    }
                    switch ((ResponseMsgType)model.ResponseMsgType)
                    {
                    case ResponseMsgType.Text:
                        var dbtexts = await databaseFixture.Db.WxTextResponse.FindAllAsync(m => m.RuleId == dbrule.Id);

                        foreach (var item in dbtexts)
                        {
                            await databaseFixture.Db.WxTextResponse.DeleteAsync(item, tran);
                        }
                        foreach (var item in model.ResponseText)
                        {
                            WxTextResponse textResponse = new WxTextResponse
                            {
                                Content = item,
                                RuleId  = dbrule.Id
                            };
                            await databaseFixture.Db.WxTextResponse.InsertAsync(textResponse, tran);
                        }
                        break;

                    case ResponseMsgType.Image:
                        break;

                    case ResponseMsgType.Voice:
                        break;

                    case ResponseMsgType.Video:
                        break;

                    case ResponseMsgType.Music:
                        break;

                    case ResponseMsgType.News:
                        break;

                    case ResponseMsgType.Transfer_Customer_Service:
                        break;

                    default:
                        break;
                    }

                    tran.Commit();
                    return(true);
                }
                catch (System.Exception ex)
                {
                    tran.Rollback();
                    return(false);
                }
            }
        }
示例#2
0
        /// <summary>
        /// 规则新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task <bool> AddAsync(RuleReplyDto model)
        {
            using (var tran = databaseFixture.Db.BeginTransaction())
            {
                try
                {
                    WxRule rule = new WxRule
                    {
                        RuleName        = model.RuleName,
                        RuleType        = model.RuleType,
                        ResponseMsgType = model.ResponseMsgType
                    };
                    switch ((WxRuleType)model.RuleType)
                    {
                    case WxRuleType.Normal:
                        rule.RequestMsgType = (int)RequestMsgType.Text;
                        break;

                    case WxRuleType.Subscribe:
                        rule.RequestMsgType = (int)RequestMsgType.Event;
                        break;

                    case WxRuleType.Unmatched:
                        rule.RequestMsgType = model.RequestMsgType;
                        break;
                    }
                    int ruleid = await databaseFixture.Db.WxRule.InsertReturnIdAsync(rule, tran);

                    switch ((RequestMsgType)rule.RequestMsgType)
                    {
                    case RequestMsgType.Text:    //关键字请求
                        foreach (var item in model.Keywords)
                        {
                            WxKeyword keyword = new WxKeyword
                            {
                                Keyword = item,
                                RuleId  = ruleid
                            };
                            await databaseFixture.Db.WxKeyword.InsertAsync(keyword, tran);
                        }
                        break;

                    case RequestMsgType.Image:
                        break;

                    case RequestMsgType.Voice:
                        break;

                    case RequestMsgType.Video:
                        break;

                    case RequestMsgType.Location:
                        break;

                    case RequestMsgType.Link:
                        break;

                    case RequestMsgType.ShortVideo:
                        break;

                    case RequestMsgType.Event:
                        break;

                    default:
                        break;
                    }

                    switch ((ResponseMsgType)model.ResponseMsgType)
                    {
                    case ResponseMsgType.Text:
                        foreach (var item in model.ResponseText)
                        {
                            WxTextResponse textResponse = new WxTextResponse
                            {
                                Content = item,
                                RuleId  = ruleid
                            };
                            await databaseFixture.Db.WxTextResponse.InsertAsync(textResponse, tran);
                        }
                        break;

                    case ResponseMsgType.Image:
                        break;

                    case ResponseMsgType.Voice:
                        break;

                    case ResponseMsgType.Video:
                        break;

                    case ResponseMsgType.Music:
                        break;

                    case ResponseMsgType.News:
                        break;

                    case ResponseMsgType.Transfer_Customer_Service:
                        break;

                    default:
                        break;
                    }

                    tran.Commit();
                    return(true);
                }
                catch (System.Exception ex)
                {
                    tran.Rollback();
                    return(false);
                }
            }
        }