1. 程式人生 > WINDOWS開發 >abp(net core)+easyui+efcore實現倉儲管理系統——ABP WebAPI與EasyUI結合增刪改查之九(三十五)

abp(net core)+easyui+efcore實現倉儲管理系統——ABP WebAPI與EasyUI結合增刪改查之九(三十五)

abp(net core)+easyui+efcore實現倉儲管理系統目錄

abp(net core)+easyui+efcore實現倉儲管理系統——ABP總體介紹(一) abp(net core)+easyui+efcore實現倉儲管理系統——解決方案介紹(二) abp(net core)+easyui+efcore實現倉儲管理系統——領域層建立實體(三) abp(net core)+easyui+efcore實現倉儲管理系統——定義倉儲並實現 (四)

abp(net core)+easyui+efcore實現倉儲管理系統——建立應用服務(五)

abp(net core)+easyui+efcore實現倉儲管理系統——展現層實現增刪改查之控制器(六) abp(net core)+easyui+efcore實現倉儲管理系統——展現層實現增刪改查之列表檢視(七) abp(net core)+easyui+efcore實現倉儲管理系統——展現層實現增刪改查之增刪改檢視(八) abp(net core)+easyui+efcore實現倉儲管理系統——展現層實現增刪改查之選單與測試(九) abp(net core)+easyui+efcore實現倉儲管理系統——多語言(十)
abp(net core)+easyui+efcore實現倉儲管理系統——使用 WEBAPI實現CURD (十一) abp(net core)+easyui+efcore實現倉儲管理系統——選單-上 (十六)

abp(net core)+easyui+efcore實現倉儲管理系統——EasyUI前端頁面框架 (十八)

abp(net core)+easyui+efcore實現倉儲管理系統——EasyUI之貨物管理一 (十九) abp(net core)+easyui+efcore實現倉儲管理系統——EasyUI之貨物管理七(二十五)
abp(net core)+easyui+efcore實現倉儲管理系統——EasyUI之貨物管理八(二十六) abp(net core)+easyui+efcore實現倉儲管理系統——ABP WebAPI與EasyUI結合增刪改查之一(二十七) abp(net core)+easyui+efcore實現倉儲管理系統——ABP WebAPI與EasyUI結合增刪改查之二(二十八) abp(net core)+easyui+efcore實現倉儲管理系統——ABP WebAPI與EasyUI結合增刪改查之三(二十九) abp(net core)+easyui+efcore實現倉儲管理系統——ABP WebAPI與EasyUI結合增刪改查之四(三十) abp(net core)+easyui+efcore實現倉儲管理系統——ABP WebAPI與EasyUI結合增刪改查之五(三十一) abp(net core)+easyui+efcore實現倉儲管理系統——ABP WebAPI與EasyUI結合增刪改查之六(三十二) abp(net core)+easyui+efcore實現倉儲管理系統——ABP WebAPI與EasyUI結合增刪改查之七(三十三) abp(net core)+easyui+efcore實現倉儲管理系統——ABP WebAPI與EasyUI結合增刪改查之八(三十四) 在上面abp(net core)+easyui+efcore實現倉儲管理系統——ABP WebAPI與EasyUI結合增刪改查之八(三十四) 文章的學習之後。我們通過前面的八篇文章已經學習了通過WebAPI介面與控制器去實現新增、刪除與修改功能。接下來,我們要在控制器中實現查詢功能。

、查詢組織資訊

1. 在Visual Studio 2017的“解決方案資源管理器”中,右鍵單擊在領域層“ABP.TPLMS.Web.Mvc”專案中的Views\Orgs目錄。 找到Index.cshmtl檔案,新增一個查詢條件相關程式碼。如下圖。

技術分享圖片

具體程式碼如下:

 <div id="dg-button">
            <form name="searchform" method="post" action="" id="searchform">             

                <label for="Name">組織名稱:</label>
                <input name="Name" id="Name" class="easyui-validatebox" data-options="width:200" />
                <label for="Code">組織程式碼:</label>
                <input name="Code" id="Code" class="easyui-validatebox" data-options="width:150" />
                <label for="CustomCode">海關程式碼:</label>

                <input name="CustomCode" id="CustomCode" class="easyui-validatebox" data-options="width:100" />
                <a href="#" id="search" class="easyui-linkbutton" data-options="iconCls:‘icon-search‘" onclick="Search()">查詢</a>               
            </form>
        </div>

2.Visual Studio 2017的“解決方案資源管理器”中,右鍵單擊“ABP.TPLMS.Application”專案的 “Orgs”資料夾中,找到PagedOrgResultRequestDto.cs檔案,新增查詢條件屬性。程式碼如下。

public class PagedOrgResultRequestDto : PagedResultRequestDto
    {

        public string Keyword { get; set; }
        public string OrgName { get; set; }
        public string OrgCode { get; set; }
        public string CustomCode { get; set; }
    }
}

3. 在Visual Studio 2017的“解決方案資源管理器”中,右鍵單擊在領域層“ABP.TPLMS.Web.Mvc”專案中的Controller目錄。找到“OrgsController.cs”檔案。如下圖。

技術分享圖片

具體程式碼如下:

   [DontWrapResult]
        [HttpPost]
        public string List()
        {           

            PagedOrgResultRequestDto paged = new PagedOrgResultRequestDto();
            paged.MaxResultCount = MAX_COUNT;
            paged.OrgName = Request.Form["Name"].ToString();
            paged.OrgCode = Request.Form["Code"].ToString();
            paged.CustomCode = Request.Form["CustomCode"].ToString();

            var userList = _orgAppService.GetAll(paged).GetAwaiter().GetResult().Items;

            int total = userList.Count;
            var json = JsonEasyUI(userList,total);
            return json;
        }

4.Visual Studio 2017的“解決方案資源管理器”中,找到領域層“ABP.TPLMS.Web.Mvc”專案中的wwwroot目錄下的view-resources\Orgs資料夾下,找到Index.js檔案,在工具欄(toolbar)中新增查詢條件。如下圖畫框處

技術分享圖片

5.在Index.js檔案新增一個查詢方法Search,程式碼如下。

function Search() {
    var _$form = $(form[name=searchform]);
    var params = _$form.serializeFormToObject();
    $(#dgOrg).treegrid({ queryParams: params });
}
6.在Visual Studio 2017中按F5執行應用程式。

7.在瀏覽器中的位址列中輸入“http://localhost:5000/”,然後輸入管理員使用者名稱進行登入。

8.在主介面的選單中,選擇“Business->組織管理”選單項,瀏覽器中呈現一個貨物資訊列表與四個按鈕。如下圖。 技術分享圖片

9. 接下來,我們要實現根據查詢條件進行查詢的功能。Visual Studio 2017的“解決方案資源管理器”中,右鍵單擊“ABP.TPLMS.Application”專案的 “Orgs”資料夾中,找到OrgAppService.cs檔案。重寫CreateFilteredQuery方法。程式碼如下。關於GetParentOrgs方法,在這裡要說明一點,這個方法的具體作用是找到我們查詢到的組織資訊的所有上級組織。這個方法通過遞迴演算法來查詢條件當前查詢條件的組織資訊中的第一條的所有上級組織資訊。如果這個方法有缺陷,可以自行修正。如果不呼叫GetParentOrgs方法,會出現什麼結果,可以自行試驗。

  protected override IQueryable<Org> CreateFilteredQuery(PagedOrgResultRequestDto input)
        {
            var qry= base.CreateFilteredQuery(input)
                .Where(t=>t.Name.Contains(input.OrgName))
                .Where(t => t.BizCode.Contains(input.OrgCode))
                .Where(t => t.CustomCode.Contains(input.CustomCode));

            List<Org> list = qry.ToList<Org>();

            var qry1 = base.CreateFilteredQuery(input);
            List<Org> listParent = new List<Org>();
            GetParentOrgs(listParent,list[0].ParentId,qry1);
            return qry.Union<Org>(listParent);
        }

        private void GetParentOrgs(List<Org> orgs,int ParentId,IQueryable<Org> listOrgs)
        {
            List<Org> liOrgs = listOrgs.Where(x => x.Id == ParentId).ToList();
            if (liOrgs == null || liOrgs .Count <= 0)
            {

                return;
            }
            else
            {
                for (int i = 0; i < liOrgs.Count; i++)
                {
                    var org = liOrgs[i];
                    if (!orgs.Contains(org))
                    {
                        orgs.Add(org);
                    }                    
                   GetParentOrgs(orgs,org.ParentId,listOrgs);
                }
            }
        }

10.重複上面的第6、7、8步。然後在“組織名稱”查詢條件中輸入“北”,然後點選“查詢”按鈕,然而查詢出所有貨物程式碼中有“北”的貨物資訊。如下圖。

技術分享圖片

11.“海關程式碼”查詢條件中輸入“1111”,然後點選“查詢”按鈕,然而查詢出所有“海關程式碼”中有“1111”的相關組織資訊。如下圖。 技術分享圖片

12.“組織程式碼”查詢條件中輸入“B”,然後點選“查詢”按鈕,然而查詢出所有“組織程式碼”中有“B”的相關組織資訊。如下圖。 技術分享圖片