10、ABPZero系列教程之拼多多賣家工具 拼團提醒邏輯功能實現

来源:https://www.cnblogs.com/shensigzs/archive/2018/01/17/8304039.html
-Advertisement-
Play Games

上篇文章已經封裝好了類庫,現在繼續實現功能,在ABPZero框架的基礎上來實現一個完整的功能。 Redis緩存 編寫功能前先在本機安裝好Redis,需要用到Redis做緩存,以下分享2個Windows安裝Redis的教程 博客園:http://www.cnblogs.com/mzws/p/redis ...


  上篇文章已經封裝好了類庫,現在繼續實現功能,在ABPZero框架的基礎上來實現一個完整的功能。

Redis緩存

編寫功能前先在本機安裝好Redis,需要用到Redis做緩存,以下分享2個Windows安裝Redis的教程

博客園:http://www.cnblogs.com/mzws/p/redis1.html

我的筆記:http://note.youdao.com/noteshare?id=a25fc319c5a38285ab7cab2e81857b31&sub=675165188B214E6CA0660B8EEB0A1C35(請記得及時收藏,可能不知哪天就失效了)

Core項目

在Core項目下新建Pdd目錄,繼續在Pdd目錄下新建Entities、IRepositories目錄,建完如下圖所示:

 

接著在Entities目錄下新建PddMall實體類,代碼如下:

/// <summary>
    /// 店鋪
    /// </summary>
    public class PddMall : FullAuditedEntity
    {
        /// <summary>
        /// 店鋪id
        /// </summary>
        public string MallId { get; set; }

        /// <summary>
        /// 店鋪名稱
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// logo
        /// </summary>
        public string Logo { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string Desc { get; set; }

        /// <summary>
        /// 退款地址
        /// </summary>
        public string RefundAddress { get; set; }

        /// <summary>
        /// 銷售量
        /// </summary>
        public long Sales { get; set; }

        /// <summary>
        /// 商品數量
        /// </summary>
        public int GoodNum { get; set; }
    }

 

繼續在IRepositories目錄下新建IMallRepository倉儲介面,代碼如下:

public interface IMallRepository : IRepository<PddMall>
{
}

 

EntityFramework項目

打開AbpZeroTemplateDbContext.cs文件,添加如下代碼:

文件路徑:D:\abpweb\PddSellerAssistant\PddSellerAssistant.EntityFramework\EntityFramework\AbpZeroTemplateDbContext.cs

/************拼多多相關*********************************/
public virtual IDbSet<PddMall> PddMalls { get; set; }
/************拼多多相關*********************************/

打開VS的包管理控制台,併在包管理控制臺中選擇 .EntityFramework 項目作為預設項目。然後在控制臺中執行下麵命令:

Add-Migration "Add_PddMall"

 

看到上圖黃色提示說明創建遷移文件成功

同時Migrations目錄多了一個文件,這個就是剛剛創建的遷移文件。

現在你可以使用下麵命令來創建資料庫:

Update-Database

 

命令執行成功,查看資料庫也創建了對應的表(如下):

 

再EntityFramework項目下新建Pdd目錄,接著再創建Repositories目錄,結構如下:

Repositories目錄下新建MallRepository倉儲實現類,代碼如下:

public class MallRepository : AbpZeroTemplateRepositoryBase<PddMall>, IMallRepository
    {
        public MallRepository(IDbContextProvider<AbpZeroTemplateDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }
    }

 

 

Application項目

首先引用類庫PddTool

 

同樣新建Pdd目錄,此目錄下再新建MallApp、ProductApp,這兩個目錄分別再創建Dto目錄,效果如下:

 

MallApp目錄下新建IMallAppService介面,代碼如下:

public interface IMallAppService : IApplicationService
    {
        void CreateByMallId(CreateMallInput input);
        /// <summary>
        /// 獲取店鋪信息,返回店鋪編號、店鋪名稱
        /// 
        /// </summary>
        /// <returns></returns>
        MallOutput GetMallInfo(int id);

        /// <summary>
        /// 獲取店鋪列表
        /// </summary>
        /// <returns></returns>
        GetMallsOutput GetMalls();

        
    }

 

 

此時,Input、Output類會報錯,接著在Dto目錄下再創建這幾個類,分別如下:

public class CreateMallInput
    {
        [Required]
        public string MallId { get; set; }
    }

 

 

public class MallOutput
    {
        public int Id { get; set; }

        /// <summary>
        /// 店鋪名稱
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 店鋪id
        /// </summary>
        public string MallId { get; set; }
    }

 

 

public class GetMallsOutput
    {
        public List<MallOutput> Items { get; set; }
    }

 

 

MallApp目錄下再新建MallAppService類,代碼如下:

public class MallAppService : AbpZeroTemplateAppServiceBase, IMallAppService
    {
        private readonly IMallRepository _mallRepository;
        private readonly ICacheManager _cacheManager;
        public MallAppService(IMallRepository mallRepository, ICacheManager cacheManager)
        {
            _mallRepository = mallRepository;
            _cacheManager = cacheManager;
        }

        /// <summary>
        /// 獲取店鋪信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public MallOutput GetMallInfo(int id)
        {

            var mall = _mallRepository.Get(id);
            return new MallOutput()
            {
                Id = mall.Id,
                MallId = mall.MallId,
                Name = mall.Name
            };
        }

        /// <summary>
        /// 添加店鋪
        /// </summary>
        /// <param name="input"></param>
        public void CreateByMallId(CreateMallInput input)
        {
            try
            {
                var mall = MallTool.GetInfo(input.MallId);
                var entity = new PddMall()
                {
                    Name = mall.mall_name,
                    Desc = mall.mall_desc,
                    GoodNum = mall.goods_num,
                    Logo = mall.logo,
                    MallId = input.MallId,
                    RefundAddress = mall.refund_address,
                    Sales = mall.mall_sales,
                };
                //按店鋪id查詢店鋪資料
                var count = _mallRepository.Count(a => a.MallId.Equals(input.MallId) && a.CreatorUserId == AbpSession.UserId);
                if (count != 0)
                {
                    //資料庫存在則更新
                    var m = _mallRepository.Single(a => a.MallId.Equals(input.MallId) && a.CreatorUserId == AbpSession.UserId);
                    m.Name = entity.Name;
                    m.Desc = entity.Desc;
                    m.GoodNum = entity.GoodNum;
                    m.Logo = entity.Logo;
                    m.MallId = entity.MallId;
                    m.RefundAddress = entity.RefundAddress;
                    m.Sales = entity.Sales;
                    _mallRepository.Update(m);
                }
                else
                {
                    //資料庫不存在此店鋪則使用API獲取
                    _mallRepository.Insert(entity);
                }

            }
            catch (Exception ex)
            {
                throw new UserFriendlyException(ex.Message);
            }
        }

        /// <summary>
        /// 從資料庫中獲取店鋪列表
        /// </summary>
        /// <returns></returns>
        public GetMallsOutput GetMalls()
        {
            var list = _mallRepository.GetAllList(a => a.CreatorUserId == AbpSession.UserId);
            //創建映射
            return new GetMallsOutput()
            {
                Items = Mapper.Map<List<MallOutput>>(list)
            };
        }
        
    }

 

 

目錄結構如下:

 

ProductApp目錄下繼續新建IProductAppService介面,代碼如下:

public interface IProductAppService : IApplicationService
    {
        
        Task<PagedResultDto<KaiTuanProductOutput>> GetKaiTuanProductsAsync(GetProductsInput input);

        /// <summary>
        /// 根據商品id獲取此商品所有拼團信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        //[HttpGet]
        PagedResultDto<ProductOutput> GetAllKaiTuansByGoodId(GetAllKaiTuansInput input);
    }

 

 

接著再新建ProductAppService類,代碼如下:

public class ProductAppService : AbpZeroTemplateAppServiceBase, IProductAppService
    {
        /// <summary>
        /// 緩存管理
        /// </summary>
        private readonly ICacheManager _cacheManager;
        private string key;    //緩存key
        public ProductAppService(ICacheManager cacheManager)
        {
            _cacheManager = cacheManager;
        }
        /// <summary>
        ///根據店鋪id, 獲取有開團的商品
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<KaiTuanProductOutput>> GetKaiTuanProductsAsync(GetProductsInput input)
        {
            //獲取所有商品的開團人數
            var list = MallTool.GetKaiTuanList(input.MallId);
            //清除緩存
            key = string.Format("{0}_{1}_KaiTuan", AbpSession.UserId, input.MallId);
            _cacheManager.GetCache(key).Clear();
            #region 數據轉換
            var items = new List<KaiTuanProductOutput>();
            foreach (var localGroupItem in list)
            {
                var item = new KaiTuanProductOutput()
                {
                    GoodId = localGroupItem.GoodId,
                    Name = localGroupItem.Name,
                    KaiTuanCount = localGroupItem.KaiTuanCount,
                    Img = localGroupItem.Img,

                };
                items.Add(item);
            }
            #endregion
            int totalCount = list.Count;

            #region 處理排序
            if (input.Sorting.Equals("goodId ASC"))
            {
                items = items.OrderBy(a => a.GoodId).ToList();
            }
            else if (input.Sorting.Equals("goodId DESC"))
            {
                items = items.OrderByDescending(a => a.GoodId).ToList();
            }
            if (input.Sorting.Equals("name ASC"))
            {
                items = items.OrderBy(a => a.Name).ToList();
            }
            else if (input.Sorting.Equals("name DESC"))
            {
                items = items.OrderByDescending(a => a.Name).ToList();
            }
            if (input.Sorting.Equals("kaiTuanCount ASC"))
            {
                items = items.OrderBy(a => a.KaiTuanCount).ToList();
            }
            else if (input.Sorting.Equals("kaiTuanCount DESC"))
            {
                items = items.OrderByDescending(a => a.KaiTuanCount).ToList();
            }
            #endregion

            return new PagedResultDto<KaiTuanProductOutput>(totalCount, items);
        }

        /// <summary>
        /// 根據商品id,獲取此商品所有開團
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public PagedResultDto<ProductOutput> GetAllKaiTuansByGoodId(GetAllKaiTuansInput input)
        {
            #region 使用redis緩存
            key = string.Format("{0}_{1}_KaiTuan", AbpSession.UserId, input.MallId);
            var list = _cacheManager.GetCache<string, List<KaiTuan>>(key).Get("GetAllKaiTuanByGoodId." + input.GoodId, () => MallTool.GetAllKaiTuanByGoodId(input.MallId, input.GoodId));

            #endregion
            #region 數據轉換
            List<ProductOutput> productOutputs = Mapper.Map<List<ProductOutput>>(list);

            #endregion
            #region 處理排序
            if (input.Sorting.Equals("timeLeft ASC"))
            {
                productOutputs = productOutputs.OrderBy(a => a.TimeLeft).ToList();
            }
            else if (input.Sorting.Equals("timeLeft DESC"))
            {
                productOutputs = productOutputs.OrderByDescending(a => a.TimeLeft).ToList();
            }
            #endregion

            int total = productOutputs.Count;
            #region 處理分頁
            productOutputs = productOutputs.Skip(input.SkipCount).Take(input.MaxResultCount).ToList();
            #endregion
            return new PagedResultDto<ProductOutput>(total, productOutputs);
        }
    }

 

同樣Input、Output報錯,在Dto分別創建如下類即可:

public class KaiTuanProductOutput
    {
        public int Id { get; set; }

        public int GoodId { get; set; }

        /// <summary>
        /// 商品名稱
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 商品圖片
        /// </summary>
        public string Img { get; set; }

        /// <summary>
        /// 開團人數
        /// </summary>
        public int KaiTuanCount { get; set; }
    }

 

 

public class GetProductsInput : PagedAndSortedInputDto, IShouldNormalize
    {
        /// <summary>
        /// 店鋪id
        /// </summary>
        public string MallId { get; set; }

        /// <summary>
        /// 提醒間隔(分鐘)
        /// </summary>
        public int Interval { get; set; }
        public void Normalize()
        {
            if (string.IsNullOrEmpty(Sorting))
            {
                Sorting = "Name";
            }
        }
    }

 

 

public class ProductOutput
    {
        /// <summary>
        /// 商品id
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 昵稱
        /// </summary>
        public string  NickName { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string SKU { get; set; }

        /// <summary>
        /// 訂單號
        /// </summary>
        public string OrderNum { get; set; }

        /// <summary>
        /// 剩餘時間
        /// </summary>
        public double TimeLeft { get; set; }

        /// <summary>
        /// 開團單號
        /// </summary>
        public string KaiTuanOrderNum { get; set; }
    }

 

 

public class GetAllKaiTuansInput : PagedAndSortedInputDto, IShouldNormalize
    {
        /// <summary>
        /// 店鋪id
        /// </summary>
        public int MallId { get; set; }
        /// <summary>
        /// 商品id
        /// </summary>
        public int GoodId { get; set; }

public void Normalize() { if (string.IsNullOrEmpty(Sorting)) { Sorting = "timeLeft ASC"; } } }

 

 

再打開CustomDtoMapper.cs,添加如下代碼(約定映射):

文件路徑:D:\abp version\aspnet-zero-3.4.0\aspnet-zero-3.4.0\src\MyCompanyName.AbpZeroTemplate.Application\CustomDtoMapper.cs

private static void CreateMappingsInternal(IMapperConfigurationExpression mapper)
        {
            mapper.CreateMap<User, UserEditDto>()
                .ForMember(dto => dto.Password, options => options.Ignore())
                .ReverseMap()
                .ForMember(user => user.Password, options => options.Ignore());
            /**********************拼多多相關********************************/
            mapper.CreateMap<PddMall, MallOutput>();
            mapper.CreateMap<KaiTuan, ProductOutput>();
        }

 

 

以上整個拼團提醒業務邏輯就完成了,最終Application項目中Pdd目錄結構如下:

 

生成解決方案,瀏覽器打開框架後臺登錄。

瀏覽器再打開http://localhost:8088/swagger/ui/index,進行api測試。

按上一篇提到獲取拼多多店鋪編號的方法,找一個店鋪編號:1227314,對剛剛編寫的功能進行測試。

目前只有店鋪資料會保存到資料庫,商品信息或拼團信息保存到緩存。

本篇內容比較多,頁面實現移到下一篇。

 

 返回總目錄

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一、需求 後臺使用orcale資料庫,mybatis做持久層,前臺搜索功能,根據類型搜索,但是資料庫中沒有類型欄位, 所以需要在where條件語句中進行判斷,當type == x1 時和type == x2時where中的判斷條件不同 二、解決 <select id = "" resultMap = ...
  • 一、什麼是依賴註入(Denpendency Injection) 這也是個老身常談的問題,到底依賴註入是什麼? 為什麼要用它? 初學者特別容易對控制反轉IOC(Iversion of Control),DI等概念搞暈。 1.1依賴 當一個類需要另一個類協作來完成工作的時候就產生了依賴。比如我們在Ac ...
  • 【轉】【完全開源】微信客戶端.NET版 目錄 說明 功能 原理步驟 一些參考 說明 前兩天比較閑,研究了一下web版微信。因為之前看過一篇博客講微信web協議的,後來嘗試分析了一下,半途中發現其實沒什麼意義,但又不想半途而廢,所以最後做出了一個這樣子的demo。功能比較少,跟官方客戶端功能差不多(其 ...
  • 【轉】【完全開源】百度地圖Web service API C#.NET版,帶地圖顯示控制項、導航控制項、POI查找控制項 目錄 概述 功能 如何使用 參考幫助 概述 源代碼主要包含三個項目,BMap.NET、BMap.NET.WindowsForm以及BMap.NET.WinformDemo。 BMap. ...
  • 類庫下載 I add a wiki page that explains how to use the NFS Client c# .net library in your project. NekoDrive uses a Library written in C# on .NET 2.0. th ...
  • 前面的幾篇文章<<.NET 中的阻塞隊列BlockingCollection的正確打開方式>><<項目開發中應用如何併發處理的一二事>>從代碼以及理論角度,充分的利用了微軟提供的BlockingCollection的屬性IsComplete以及CompleteAdding完成了併發的設計,這次我們單 ...
  • Quickfix 是開源的FIX引擎,支持JAVA, C#等語言 官網地址:http://quickfixn.org/tutorial/creating-an-application.html 閱讀下麵文字之前假設用戶已經看過了Quickfix官網並且已經對fix協議的基本內容有了瞭解。 下麵主要介 ...
  • 來源http://blog.csdn.net/u010705091/article/details/75212724 echarts折線圖的數據視圖樣式重寫 在echarts.js中,點擊折線圖的數據試圖按鈕,會以表格table的形式展示折線圖中的數據,但是此時的table格式比較亂。如下圖: 所以 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...