202 lines
7.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using WaterCloud.Code;
using WaterCloud.Domain.MaterialManage;
using WaterCloud.Service;
using Microsoft.AspNetCore.Authorization;
using WaterCloud.Service.MaterialManage;
using System.Net.Http.Headers;
using System.IO;
using Newtonsoft.Json;
namespace WaterCloud.Web.Areas.MaterialManage.Controllers
{
/// <summary>
/// 创 建:超级管理员
/// 日 期2020-12-01 14:31
/// 描 述:物料管理控制器类
/// </summary>
[Area("MaterialManage")]
public class MaterialController : BaseController
{
public MaterialService _service {get;set;}
[HttpGet]
[ServiceFilter(typeof(HandlerAuthorizeAttribute))]
public virtual ActionResult Import()
{
return View();
}
[HttpGet]
public virtual ActionResult AddForm()
{
return View();
}
#region
[HandlerAjaxOnly]
[IgnoreAntiforgeryToken]
public async Task<ActionResult> GetGridJson(SoulPage<MaterialEntity> pagination, string keyword,string ids,int? type)
{
if (string.IsNullOrEmpty(pagination.field))
{
pagination.field = "F_MaterialType,F_MaterialCode";
pagination.order = "asc";
}
var data = await _service.GetLookList(pagination,keyword, ids ,type);
return Content(pagination.setData(data).ToJson());
}
[HttpGet]
[HandlerAjaxOnly]
public async Task<ActionResult> GetParentJson(Pagination pagination, string keyword)
{
pagination.sort = "F_CreatorTime";
pagination.order = "desc";
var data = await _service.GetLookList(pagination, keyword);
return Success(pagination.records,data);
}
[HttpGet]
[HandlerAjaxOnly]
public async Task<ActionResult> GetListJson(string keyword)
{
var data = await _service.GetList(keyword);
return Content(data.ToJson());
}
[HttpGet]
[HandlerAjaxOnly]
public async Task<ActionResult> GetFormJson(string keyValue)
{
var data = await _service.GetLookForm(keyValue);
return Content(data.ToJson());
}
#endregion
#region
[HttpPost]
[HandlerAjaxOnly]
public async Task<ActionResult> SubmitForm(MaterialEntity entity, string keyValue)
{
try
{
await _service.SubmitForm(entity, keyValue);
return await Success("操作成功。", "", keyValue);
}
catch (Exception ex)
{
return await Error(ex.Message, "", keyValue);
}
}
[HttpPost]
[HandlerAjaxOnly]
[ServiceFilter(typeof(HandlerAuthorizeAttribute))]
public async Task<ActionResult> DeleteForm(string keyValue)
{
try
{
//todo 加删除限制
await _service.DeleteForm(keyValue);
return await Success("操作成功。", "", keyValue, DbLogType.Delete);
}
catch (Exception ex)
{
return await Error(ex.Message, "", keyValue, DbLogType.Delete);
}
}
[HttpPost]
[HandlerAjaxOnly]
public async Task<ActionResult> CheckFile()
{
try
{
//获取文件参数,创建临时文件,使用完成就删除
var files = HttpContext.Request.Form.Files;
long size = files.Sum(f => f.Length);
if (size > 104857600)
{
throw new Exception("文件大小必须小于100M");
}
var file = files.FirstOrDefault();
var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
if (string.IsNullOrEmpty(fileName))
{
throw new Exception("文件不存在");
}
if (!FileHelper.IsExcel(fileName))
{
throw new Exception("请上传Excel");
}
string filePath = GlobalContext.HostingEnvironment.WebRootPath + $@"/" + "file" + $@"/";
fileName = Utils.CreateNo() + fileName.Substring(fileName.LastIndexOf("."));
string fileFullName = filePath + fileName;
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
using (FileStream fs = System.IO.File.Create(fileFullName))
{
file.CopyTo(fs);
fs.Flush();
}
var data = await _service.CheckFile(fileFullName);
return Content(new { code = 0, msg = "操作成功", data = data }.ToJson());
}
catch (Exception ex)
{
return Content(new { code = 400, msg = "操作失败," + ex.Message }.ToJson());
}
}
[HttpPost]
[HandlerAjaxOnly]
public async Task<ActionResult> ImportForm(string listData)
{
var filterList = JsonConvert.DeserializeObject<List<MaterialEntity>>(listData);
if (filterList == null || filterList.Count == 0)
{
return Error("导入数据不存在!");
}
if (filterList.Where(a => a.F_EnabledMark == false).Count() > 0)
{
return Error("导入数据存在错误!");
}
try
{
await _service.ImportForm(filterList);
return await Success("导入成功。", "", "");
}
catch (Exception ex)
{
return await Error("导入失败," + ex.Message, "", "");
}
}
[HttpGet]
public async Task<FileResult> Download()
{
string fileName = "物料导入模板.xlsx";
string fileValue = "model";
string filePath = GlobalContext.HostingEnvironment.WebRootPath + $@"/" + fileValue + $@"/" + fileName;
if (!FileHelper.IsExistFile(filePath))
{
throw new Exception("文件不存在");
}
///定义并实例化一个内存流,以存放图片的字节数组。
MemoryStream ms = new MemoryStream();
///图片读入FileStream
FileStream f = new FileStream(filePath, FileMode.Open);
///把FileStream写入MemoryStream
ms.SetLength(f.Length);
f.Read(ms.GetBuffer(), 0, (int)f.Length);
ms.Flush();
f.Close();
var contentType = MimeMapping.GetMimeMapping(fileName);
return File(ms, contentType, fileName);
}
#endregion
}
}