qinchulong
2025-03-29 039a4a5433e7f80adc88b491b549e5d9486e4f9a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using WIDESEA_Core.Dapper;
using WIDESEA_Core.DBManager;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Core.Extensions;
using WIDESEA_Entity.DomainModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace WIDESEA_Core.BaseProvider.DictionaryComponent
{
    /// <summary>
    /// 组件视图,参照:https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-2.1
    /// 与Controller命名一样必须以ViewComponent结尾
    /// </summary>
    public class DictionaryViewComponent : ViewComponent
    {
        public async Task<IViewComponentResult> InvokeAsync(string dropDownIds)
        {
            if (string.IsNullOrEmpty(dropDownIds))
                return null;
 
            string[] dicNos = dropDownIds.Split(',');
            StringBuilder stringBuilder = new StringBuilder();
            VOLContext context = DBServerProvider.GetEFDbContext();
            var dicData =await (from d in context.Set<Sys_Dictionary>()
                           join list in context.Set<Sys_DictionaryList>()
                           on d.Dic_ID equals list.Dic_ID
                           into t
                           from list in t.DefaultIfEmpty()
                           where dicNos.Contains(d.DicNo)
                           select new { list.DicValue, list.DicName, d.Config, d.DbSql, list.OrderNo, d.DicNo }).ToListAsync();
 
            foreach (var item in dicData.GroupBy(x => x.DicNo))
            {
                stringBuilder.AppendLine($" var optionConfig{item.Key} = {item.Select(x => x.Config).FirstOrDefault()}");
 
                string dbSql = item.Select(s => s.DbSql).FirstOrDefault();
 
                stringBuilder.AppendLine($@" var dataSource{item.Key} = {
                    (!string.IsNullOrEmpty(dbSql)
                    ? DBServerProvider.GetSqlDapper().QueryList<object>(dbSql, null).Serialize()
                    : item.OrderByDescending(o => o.OrderNo).
                            Select(s => new { s.DicName, s.DicValue }).ToList()
                            .Serialize())
                     }.convertToValueText(optionConfig{item.Key})");
                stringBuilder.AppendLine($" optionConfig{item.Key}.data = dataSource{item.Key};");
            }
            ViewBag.Dic = stringBuilder.ToString();
            return View("~/Views/Shared/Dictionary.cshtml");
        }
 
    }
}