chenzx
2024-12-18 cb55bfc387d604618fe37b93b89b60c7e83d5846
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
using CommonHelper;
using GasolineBlend.BLL;
using GasolineBlend.Entity;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Razor.Tokenizer.Symbols;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml;
using System.Net.Http;
using OfficeOpenXml;
using WebGrease.Activities;
using ClosedXML.Excel;
 
 
namespace GasolineBlend.Controllers
{
    public class SubsidyDataController : BaseController
    {
        private SubsidyDataBLL _acc = new SubsidyDataBLL();
        /// <summary>
        ///获取补贴列表数据
        /// </summary>
        /// <param name="PageNumber"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetSubsidyDataList(int PageNumber,int PageSize,string Keyword,string CompanyName) 
        {
            try
            {
                var list = _acc.GetSubsidyDataList(PageNumber, PageSize, Keyword, CompanyName);
                if (list == null)
                {
                    return SuccessNoShow(data:"暂无此公司");
                }
                var response = new
                {
                    Data = list.Data,
                    TotalCount = list.TotalCount,
                    TotalPages = list.TotalPages
                };
                return SuccessNoShow(data: response);
            }
            catch (Exception e)
            {
                LogHelper.Write(Level.Error, "获取政策列表数据 GetSubsidyDataList", e, OperatorProvider.Instance.Current == null ? "GuestEx" : OperatorProvider.Instance.Current.LoginName);
                return Error();
            }
        }
 
        [HttpPost]
        public ActionResult UploadFile(HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                try
                {
                    // 获取文件名
                    string fileName = Path.GetFileName(file.FileName);
 
                    // 设置文件保存路径
                    string path = Path.Combine(Server.MapPath("~/HaoshuRobot"), fileName);
                    // 保存文件到服务器
                    file.SaveAs(path);
 
                    // 返回成功消息
                    return Json(new { success = true, message = "文件上传成功。", filePath = path }, JsonRequestBehavior.AllowGet);
                }
                catch (Exception ex)
                {
                    // 记录错误信息
                    // LogHelper.Write(Level.Error, "上传文件时发生错误", ex);
                    return Json(new { success = false, message = "文件上传失败:" + ex.Message }, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                return Json(new { success = false, message = "请选择一个文件上传。" }, JsonRequestBehavior.AllowGet);
            }
        }
        /// <summary>
        ///访问Moonshot
        /// </summary>
        /// <param name="CompanyName"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetPolicyApplied(string CompanyName)
        {
            try
            {
                var list = _acc.GetPolicyApplied(CompanyName);
                if (list == null) {
                    return SuccessNoShow(data: null);
                }
                var response = new
                {
                    Data = list.Data,
                };
                return SuccessNoShow(data: response);
            }
            catch (Exception e)
            {
                LogHelper.Write(Level.Error, "访问Moonshot GetPolicyApplied", e, OperatorProvider.Instance.Current == null ? "GuestEx" : OperatorProvider.Instance.Current.LoginName);
                return Error();
            }
        }
        /// <summary>
        ///访问Moonshot
        /// </summary>
        /// <param name="CompanyName"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetPolicyAnswer(string CompanyName)
        {
            try
            {
                var id = OperatorProvider.Instance.Current.UserId;
                if (id == 0)
                {
                    return Error(message: "账户未登录!请登录后查询", data: "-1");
                }
                string list = _acc.GetPolicyAnswer(id, CompanyName);
 
                if (list==null) {
                    return Error();
                }
                if (list == "-1") {
                    return Error(message: "今日政策洞察额度已经用完,如有需要请联系管理员!", data: "-1");
                }
                var response = new
                {
                    Data = list,
                };
                return SuccessNoShow(data: response);
            }
            catch (Exception e)
            {
                LogHelper.Write(Level.Error, "访问Moonshot GetPolicyAnswer", e, OperatorProvider.Instance.Current == null ? "GuestEx" : OperatorProvider.Instance.Current.LoginName);
                return Error();
            }
        }
        [HttpPost]
        public ActionResult GetSaveExcel(string data)
        {
            try
            {
                var filePath = SaveDataToExcel(data);
                var fileBytes = System.IO.File.ReadAllBytes(filePath);
                return File(fileBytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "评分标准.xlsx");
            }
            catch (Exception e)
            {
                LogHelper.Write(Level.Error, "GetSaveExcel请求过程中发生错误", e, OperatorProvider.Instance.Current == null ? "GuestEx" : OperatorProvider.Instance.Current.LoginName);
                return Error();
            }
        }
 
        public static string SaveDataToExcel(string data)
        {
            // 解析传输的数据
            var dataDict = ParseData(data);
 
            // 创建或打开Excel文件
            //var filePath = "E:\\.NETproject\\RRkl\\RR_PolicyControl_MVC\\RR_PolicyControl_MVC\\GasolineBlend\\评分标准.xlsx";
            var filePath = "C:\\inetpub\\RRPolicy\\HaoshuRobot\\评分标准.xlsx";
            var workbook = new XLWorkbook(filePath);
            var worksheet = workbook.Worksheet(1) ?? workbook.AddWorksheet("Sheet1");
 
            // 将数据填入F列
            int rowIndex = 1;
            foreach (var entry in dataDict)
            {
                worksheet.Cell(rowIndex, 6).Value = entry.Key;
                worksheet.Cell(rowIndex, 7).Value = entry.Value;
                rowIndex++;
            }
 
            // 保存文件
            workbook.Save();
 
            return filePath;
        }
 
        private static Dictionary<string, int> ParseData(string data)
        {
            if (!string.IsNullOrEmpty(data) && data.EndsWith(":"))
            {
                data = data.Substring(0, data.Length - 1);
            }
            var dataDict = new Dictionary<string, int>();
            var entries = data.Split(',');
            foreach (var entry in entries)
            {
                var parts = entry.Split(':');
                if (parts.Length == 2 && int.TryParse(parts[1], out int value))
                {
                    dataDict[parts[0].Trim()] = value;
                }
            }
 
            return dataDict;
        }
    }
}