新框架PC后端代码(祈禧6月初版本)
南骏 池
3 天以前 72449a1b8699b65712e57fba8abce5a8240e9465
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using System.Data;
using System.Data.SqlClient;
using System.Dynamic;
using System.Text;
using Gs.Toolbox;
using Gs.Toolbox.ApiCore.Abstract.Mvc;
using Gs.Toolbox.ApiCore.Common.Mvc;
using Gs.Toolbox.ApiCore.Group;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
 
namespace Gs.Report;
 
[ApiGroup(ApiGroupNames.Report)]
public class ReportController : IRomteService
{
    private readonly IHttpContextAccessor _http;
    private readonly string _userCode, _userGuid, _orgFids;
 
    public ReportController(IHttpContextAccessor httpContextAccessor)
    {
        _http = httpContextAccessor;
        (_userCode, _userGuid, _orgFids) =
            UtilityHelper.GetUserGuidAndOrgGuid(_http);
    }
 
    /// <summary>
    ///     根据报表参数读取报表名字
    /// </summary>
    /// <returns></returns>
    private string _rptGetParameterName(string str)
    {
        var _ary = str.Split('{');
        if (_ary.Length > 0)
        {
            var rptParameter = _ary[0];
            return rptParameter;
        }
        return str;
    }
    /// <summary>
    /// 根据报表参数读取报表参数
    /// </summary>
    /// <param name=""></param>
    /// <returns></returns>
    public static (string?, string?, string?, string?, string?, string?) _rptGetParameterP(string str)
    {
        string s0 = "", s1 = "", s2 = "", s3 = "", s4 = "", s5 = "";
        var _ary = str.Split('{');
        if (_ary.Length > 0)
        {
            var rptParameter = _ary[1].Replace("}", "");
            string[] _pAry = rptParameter.Split(',');
            if (_pAry.Length > 0)
                s0 = _pAry[0];
            if (_pAry.Length > 1)
                s1 = _pAry[1];
            if (_pAry.Length > 2)
                s2 = _pAry[2];
            if (_pAry.Length > 3)
                s3 = _pAry[3];
            if (_pAry.Length > 4)
                s4 = _pAry[4];
            if (_pAry.Length > 5)
                s5 = _pAry[5];
        }
        return (s0, s1, s2, s3, s4, s5);
    }
 
    /// <summary>
    ///     读取要打印的数据
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<ExpandoObject> GetRptData([FromBody] dynamic model)
    {
        string guid = model.guid;
        int isDesign = model.isDesign;
        string _rptParameter = model.rptParameter;
        string rptParameterName = _rptGetParameterName(_rptParameter);
        string p1, p2, p3, p4, p5, p6;
        (p1, p2, p3, p4, p5, p6) = _rptGetParameterP(_rptParameter);
        dynamic m = new ExpandoObject();
        m.reportTemplateData = "";
        m.reportCol = new List<dynamic>();
        m.zb = new List<dynamic>();
        m.mx1 = new List<dynamic>();
        m.mx2 = new List<dynamic>();
        var sbSql = new StringBuilder();
        var _blDate = false;
        try
        {
            using (var connection = new SqlConnection(DbHelperSQL.strConn))
            {
                var command = new SqlCommand(
                    "select * from [dbo].[sys_report_Template] where report_Type='" +
                    rptParameterName + "'", connection);
                connection.Open();
                var reader = command.ExecuteReader();
                if (reader.HasRows)
                    while (reader.Read())
                    {
                        var buffer = (byte[])reader["report_Data"];
                        m.guid = Guid.Parse(reader["guid"].ToString());
                        m.reportTemplateData = buffer;
                        _blDate = true;
                    }
                reader.Close();
            }
            if (_blDate == false)
                return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Default,
                    "读取失败,没有初始化模板!");
            //读数据
            var dset = new DataSet();
            using (var conn = new SqlConnection(DbHelperSQL.strConn))
            {
                conn.Open();
                using (var comm = new SqlCommand(rptParameterName, conn))
                {
                    comm.CommandType = CommandType.StoredProcedure;
                    SqlParameter[] parameters =
                    {
                       new("@inEdtUserGuid", _userGuid),
                       new("@inOrderGuid",guid),
                       new("@isDesign",isDesign),
                       new("@in1", p1),
                       new("@in2", p2),
                       new("@in3", string.IsNullOrEmpty(p3)?DBNull.Value:p3),
                       new("@in4", p4),
                       new("@in5", p5),
                      new("@in6", p6)
                    };
                    foreach (var parameter in parameters)
                        comm.Parameters.Add(parameter);
                    using (var dt = new SqlDataAdapter(comm))
                    {
                        dt.Fill(dset, "0");
                    }
                }
                conn.Close();
            }
            if (isDesign == 1)
            {
                return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Success, "读取成功!");
            }
            //0是状态,1是主表,2是明细表,3是明细表
            if (dset.Tables[0].Rows[0]["msgState"].ToString() == "-1")
            {
                return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Default, dset.Tables[0].Rows[0]["msgTxt"].ToString());
            }
            var _zb1 = dset.Tables[1].TableToDynamicList();
            var _mx1 = dset.Tables[2].TableToDynamicList();
            var _mx2 = dset.Tables[3].TableToDynamicList();
            m.zb = _zb1;
            m.mx1 = _mx1;
            m.mx2 = _mx2;
            return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Success, "读取成功!");
        }
        catch (Exception ex)
        {
            LogHelper.Debug(ToString(), "GetReport error:" + ex.Message);
            return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Default,
                "读取失败," + ex.Message);
        }
    }
 
    /// <summary>
    ///     保存模版
    /// </summary>
    /// <param name="mode"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<ExpandoObject> EdtTemplate([FromBody] dynamic mode)
    {
        string rptParameter = mode.rptParameter;
        rptParameter = _rptGetParameterName(rptParameter);
        byte[] rptData = mode.rptData;
        dynamic m = new ExpandoObject();
        m.outGuid = "";
        m.outMsg = "";
        using (var conn = new SqlConnection(DbHelperSQL.strConn))
        {
            using (var cmd = new SqlCommand("[prc_report_edt]", conn))
            {
                try
                {
                    conn.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter[] parameters =
                    {
                        new("@outGuid", SqlDbType.NVarChar, 100),
                        new("@outMsg", SqlDbType.NVarChar, 300),
                        new("@edtUserGuid", _userGuid),
                        new("@rptData", rptData),
                        new("@rptType", rptParameter)
                    };
                    parameters[0].Direction = ParameterDirection.Output;
                    parameters[1].Direction = ParameterDirection.Output;
                    foreach (var parameter in parameters)
                        cmd.Parameters.Add(parameter);
                    cmd.ExecuteNonQuery();
                    m.outOrderGuid = parameters[0].Value.ToString();
                    m.outMsg = parameters[1].Value.ToString();
                }
                catch (Exception ex)
                {
                    m.outOrderGuid = "";
                    m.outMsg = ex.Message;
                    LogHelper.Debug(ToString(),
                        "EdtReport error:" + ex.Message);
                    return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Default,
                        "操作失败!");
                }
                finally
                {
                    conn.Close();
                }
            }
        }
 
        return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Success, "操作成功!");
    }
 
    /// <summary>
    ///    
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<List<dynamic>> GetTemplateList([FromBody] dynamic model)
    {
        List<dynamic> lst = new List<dynamic>();
        System.Text.StringBuilder sbSql = new StringBuilder();
        sbSql.Append("select  [GUID],[REPORT_TYPE],[REPORT_NAME] from [dbo].[SYS_REPORT_TEMPLATE] order by [REPORT_NAME] asc");
        var dset = new DataSet();
        try
        {
            dset = DbHelperSQL.Query(sbSql.ToString());
            lst = dset.Tables[0].TableToDynamicList();
        }
        catch (Exception ex)
        {
            LogHelper.Debug(ToString(), "GetTemplateList error:" + ex.Message);
            return ReturnDto<List<dynamic>>.QuickReturn(lst,
                ReturnCode.Exception, "读取失败!");
        }
        return ReturnDto<List<dynamic>>.QuickReturn(lst, ReturnCode.Success,
            "读取成功!");
    }
}