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;
|
}
|
}
|
}
|