ReportViewApp.cs 3.07 KB
using Infrastructure;
using MySql.Data.MySqlClient;
using Newtonsoft.Json.Linq;
using System;
using System.Data.SqlClient;
using WebRepository;

namespace WebApp
{
    /// <summary>
    /// 
    /// </summary>

    public class ReportViewApp
    {
        private IUnitWork _unitWork;

        public ReportViewApp(IUnitWork unitWork)
        {
            _unitWork = unitWork;
        }

        public TableData QueryData(int ReportId, string Filter)
        {
            var result = new TableData();

            try
            {
                ReportDesign excelreport = _unitWork.FindSingle<ReportDesign>(u => u.Id == ReportId);
                var sql = excelreport.Sql;
                var sfilter = excelreport.Params;

                if (!string.IsNullOrEmpty(sfilter))
                {
                    if (string.IsNullOrEmpty(Filter))
                    {
                        throw new Exception("请填写查询条件!");
                    }
                    JObject json = null;
                    json = JObject.Parse(Filter);

                    JObject jsondefault = null;
                    jsondefault = JObject.Parse(sfilter);
                    foreach (var item in jsondefault)
                    {
                        var key = item.Key;
                        if (!json.ContainsKey(key))
                        {
                            throw new Exception("查询条件不正确!");
                        }
                    }

                    foreach (var item in json)
                    {
                        sql = sql.Replace(item.Key, item.Value.ToString());
                    }
                }

                var config = AppSettingsJson.GetAppSettings();
                var ConnString = config.GetSection("ConnectionStrings:BaseDbContext").Value;

                try
                {
                    SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder(ConnString);
                    connectionStringBuilder.Password = Encryption.Decrypt(connectionStringBuilder.Password);
                    ConnString = connectionStringBuilder.ConnectionString;

                    var ds = new SqlServerHelp(ConnString).SelectGet(sql);
                    result.data = ds.Tables[0];
                    result.count = ds.Tables[0].Rows.Count;
                }
                catch
                {
                    MySqlConnectionStringBuilder connectionStringBuilder = new MySqlConnectionStringBuilder(ConnString);
                    connectionStringBuilder.Password = Encryption.Decrypt(connectionStringBuilder.Password);
                    ConnString = connectionStringBuilder.ConnectionString;

                    var ds = new MySqlHelp(ConnString).SelectGet(sql);
                    result.data = ds.Tables[0];
                    result.count = ds.Tables[0].Rows.Count;
                }
            }
            catch (Exception ex)
            {
                result.code = 500;
                result.msg = ExceptionHelp.GetAllMessage(ex);
            }
            return result;
        }
    }
}