UploadController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net.Http;
  8. using System.Net.Http.Headers;
  9. using System.Threading.Tasks;
  10. using Common.Model;
  11. using CoreEntity.DAL;
  12. using JCSoft.WX.Framework.Api;
  13. using LigerRM.Common;
  14. using Microsoft.AspNetCore.Hosting;
  15. using Microsoft.AspNetCore.Hosting.Server;
  16. using Microsoft.AspNetCore.Http;
  17. using Microsoft.AspNetCore.Mvc;
  18. using Microsoft.Extensions.Caching.Memory;
  19. using Newtonsoft.Json;
  20. using Newtonsoft.Json.Linq;
  21. using PublicLibrary.Model;
  22. using SupplierWeb.Codes.mvc;
  23. using ZcPeng.PublicLibrary;
  24. namespace SupplierWeb.Controllers
  25. {
  26. [Route("web/upload")]
  27. [ApiController]
  28. public class UploadController : BaseController
  29. {
  30. private readonly IHostingEnvironment _hostingEnvironment;
  31. public UploadController(IHostingEnvironment hostingEnvironment, IMemoryCache cache, IApiClient client) : base(cache, client)
  32. {
  33. _hostingEnvironment = hostingEnvironment;
  34. }
  35. [HttpPost]
  36. [Route("uploadprofile")]
  37. public JsonResult UploadPrfile()
  38. {
  39. //var contentType = Request.ContentType;
  40. //var s = Request.HttpContext.ToString();
  41. var files = Request.Form.Files;
  42. foreach (var file in files)
  43. {
  44. var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
  45. string filePath = _hostingEnvironment.WebRootPath ;
  46. if (!Directory.Exists(filePath+ Path.DirectorySeparatorChar + "UploadFiles"+ Path.DirectorySeparatorChar))
  47. {
  48. Directory.CreateDirectory(filePath + Path.DirectorySeparatorChar+"UploadFiles"+ Path.DirectorySeparatorChar);
  49. }
  50. var fileType = Path.GetExtension(fileName).Substring(1);
  51. fileName = Guid.NewGuid() + Path.GetExtension(fileName);
  52. var vpath = "/UploadFiles/" + fileName;
  53. string fileFullName = filePath + vpath;
  54. using (FileStream fs = System.IO.File.Create(fileFullName))
  55. {
  56. file.CopyTo(fs);
  57. fs.Flush();
  58. }
  59. var sql = $"insert into sup_uploadfiles (FileName, FileType, Path, UploadDate, Comment,InUse) values (@FileName, @FileType, @Path, @UploadDate, @Comment,1);select SCOPE_IDENTITY()";
  60. var param = new List<SqlParameter>();
  61. param.Add(new SqlParameter("FileName", fileName));
  62. param.Add(new SqlParameter("FileType", fileType));
  63. param.Add(new SqlParameter("Path", vpath));
  64. param.Add(new SqlParameter("UploadDate", DateTime.Now));
  65. param.Add(new SqlParameter("Comment", string.Empty));
  66. DataAccess.GetOneValue(sql, param.ToArray(), out var result, out _);
  67. return new JsonResult(new
  68. {
  69. success = true,
  70. //path = result
  71. path = vpath
  72. });
  73. }
  74. return new JsonResult(new
  75. {
  76. success = false,
  77. path = string.Empty
  78. });
  79. }
  80. [AuthPermission]
  81. [HttpPost]
  82. [Route("getdata")]
  83. public JsonResult getdata(string staffid, [FromBody] dynamic entity)
  84. {
  85. var userid = getStaffUserid(staffid);
  86. //判断参数是否合法
  87. if (string.IsNullOrEmpty(userid))
  88. {
  89. return new JsonResult(new
  90. {
  91. success = 0,
  92. msg = "没有登陆"
  93. });
  94. }
  95. List<QueryFilter> filterList = new List<QueryFilter>();
  96. int pageIndex = 1;
  97. int pageSize = 15;
  98. string sortField;
  99. int sortDirection = 0;
  100. //string[] sumFields;
  101. if (entity != null)
  102. {
  103. if (entity.filters.Count > 0)
  104. {
  105. var list = JsonConvert.SerializeObject(entity.filters);
  106. filterList = JsonConvert.DeserializeObject<List<QueryFilter>>(list);
  107. }
  108. pageIndex = entity.pageIndex;
  109. pageSize = entity.pageSize;
  110. sortField = entity.sortField;
  111. sortDirection = entity.sortDirection;
  112. }
  113. var isAce = sortDirection == 0;
  114. var queryCondition = string.Empty;
  115. var param = new List<SqlParameter>();
  116. var start = (pageIndex - 1) * pageSize;
  117. var end = (start + 1 + pageSize);
  118. var dt = new DataTable();
  119. if (filterList != null)
  120. {
  121. queryCondition = QueryFilter.getFilterSqlParam(filterList.ToArray(), out param,new Temp());
  122. }
  123. //var sql = $"select * from (select row_number() over (order by id) as rowNum,* from (select (select stuff((select ','+path from sup_UPLOADFILES where RelativeId=a.ID and RelativeTable='k_contactsp' and InUse = 1 FOR xml PATH('')), 1, 1, '')) as field_file,a.ID, a.GoodsId, b.Contact, b.UserId, (select GoodsName from GOODSDOC where A.GoodsId = GOODSDOC.GoodsId) as GoodsName, (select ApprovalNo from GOODSATTR where A.GoodsId = GOODSATTR.GoodsId) as ApprovalNo from k_contactsp A left join CONTACTDOC B on a.ContactId = b.ContactId where UserId = {userid}) R where 1=1 {queryCondition} ) RR where rowNum > {start} and rowNum < {end} ";
  124. var sql = $"select * from" +
  125. $"(select row_number() over(order by id) as rowNum," +
  126. $" * from(select(select stuff((select ',' + path from sup_UPLOADFILES where RelativeId = a.ID and RelativeTable = 'newgood' and InUse = 1 FOR xml PATH('')), 1, 1, '')) as field_file," +
  127. $" * from NEWGOOD A where UserId = {userid}) R where 1 = 1 {queryCondition} " +
  128. $") RR" +
  129. $" where rowNum > {start} and rowNum< {end}";
  130. DataAccess.GetValues(sql, ref dt, param.ToArray(), out _);
  131. IList<Temp> result = new List<Temp>();
  132. if (dt != null && dt.Rows.Count > 0)
  133. {
  134. result = ModelConvertHelper<Temp>.ConvertToModel(dt);
  135. }
  136. //var countSql = $"select count(1) from(select a.ID from k_contactsp A left join CONTACTDOC B on a.ContactId = b.ContactId where UserId = {userid} {queryCondition} ) R";
  137. var countSql = $"select count(1) from(select ID from newgood where UserId = {userid} {queryCondition} ) R";
  138. var count = DataAccess.GetRowCountDefine(countSql, param.ToArray(), out _);
  139. return Json(new
  140. {
  141. items = result,
  142. sum = new { },
  143. totalCount = count
  144. });
  145. }
  146. [AuthPermission]
  147. [HttpPost]
  148. [Route("getalldata")]
  149. public JsonResult getalldata(string staffid, [FromBody] dynamic entity)
  150. {
  151. var userid = getStaffUserid(staffid);
  152. var RoleId = getStaff("roleid");
  153. //判断参数是否合法
  154. if (string.IsNullOrEmpty(userid))
  155. {
  156. return Json(new
  157. {
  158. success = 0,
  159. msg = "没有登陆",
  160. timeout =1,
  161. });
  162. }
  163. List<QueryFilter> filterList = new List<QueryFilter>();
  164. int pageIndex = 1;
  165. int pageSize = 15;
  166. string sortField;
  167. int sortDirection = 0;
  168. //string[] sumFields;
  169. if (entity != null)
  170. {
  171. if (entity.filters.Count > 0)
  172. {
  173. var list = JsonConvert.SerializeObject(entity.filters);
  174. filterList = JsonConvert.DeserializeObject<List<QueryFilter>>(list);
  175. }
  176. pageIndex = entity.pageIndex;
  177. pageSize = entity.pageSize;
  178. sortField = entity.sortField;
  179. sortDirection = entity.sortDirection;
  180. }
  181. var isAce = sortDirection == 0;
  182. var queryCondition = string.Empty;
  183. var param = new List<SqlParameter>();
  184. var start = (pageIndex - 1) * pageSize;
  185. var end = (start + 1 + pageSize);
  186. var dt = new DataTable();
  187. if (filterList != null)
  188. {
  189. queryCondition = QueryFilter.getFilterSqlParam(filterList.ToArray(),out param, new Temp(), "R.");
  190. }
  191. //string StaffDocId = StaffDocDAL.GetStaffId(userid);
  192. //string filterPurRole = ((RoleId == "1" || RoleId == "6" || RoleId == "7" || RoleId == "8") ? "" :
  193. // (" and A0.userid in" +
  194. // " (select A1.UserId from K_SaleContact A left join ContactDoc A1 on A.K_ContactId = A1.ContactId " +
  195. // "where A.SaleManId = '" + StaffDocId + "' and A1.Beactive = 'Y') "));
  196. //var sql = $"select * from (select row_number() over (order by id) as rowNum,* from (select (select stuff((select ','+path from sup_UPLOADFILES where RelativeId=a.ID and RelativeTable='k_contactsp' and InUse = 1 FOR xml PATH('')), 1, 1, '')) as field_file,a.ID, a.GoodsId, b.Contact, b.UserId, (select GoodsName from GOODSDOC where A.GoodsId = GOODSDOC.GoodsId) as GoodsName, (select ApprovalNo from GOODSATTR where A.GoodsId = GOODSATTR.GoodsId) as ApprovalNo, case (select top 1 1 from sup_UPLOADFILES where RelativeId=a.ID and RelativeTable='k_contactsp' and InUse =1 ) when 1 then 0 else 1 end as state from k_contactsp A left join CONTACTDOC B on a.ContactId = b.ContactId) R where 1=1 {queryCondition} )RR where rowNum > {start} and rowNum < {end} ";
  197. var filter = FilterTranslator.ruleSql(ref param);
  198. var sql = $"select * from" +
  199. $"(select row_number() over(order by id) as rowNum," +
  200. $" * from" +
  201. $"(select(select stuff((select ',' + path from sup_UPLOADFILES where RelativeId = A0.ID and RelativeTable = 'newgood' and InUse = 1 FOR xml PATH('')), 1, 1, '')) as field_file," +
  202. $" * " +
  203. $"from NEWGOOD A0 where 1=1 " +
  204. filter +
  205. $") R where 1 = 1 {queryCondition} " +
  206. $") RR where rowNum > {start} and rowNum< {end}";
  207. DataAccess.GetValues(sql, ref dt, param.ToArray(), out _);
  208. IList<Temp> result = new List<Temp>();
  209. if (dt != null && dt.Rows.Count > 0)
  210. {
  211. result = ModelConvertHelper<Temp>.ConvertToModel(dt);
  212. }
  213. //var countSql = $"select count(1) from(select a.ID from k_contactsp A left join CONTACTDOC B on a.ContactId = b.ContactId where 1=1 {queryCondition} ) R";
  214. var countSql = $"select count(1) from newgood A0 " +
  215. $"where 1=1 {queryCondition} " +
  216. filter +
  217. $" ";
  218. var count = DataAccess.GetRowCountDefine(countSql, param.ToArray(), out var resultstr);
  219. return Json(new
  220. {
  221. items = result,
  222. sum = new { },
  223. totalCount = count,
  224. msg = resultstr
  225. });
  226. }
  227. [HttpPost]
  228. [Route("update")]
  229. public JsonResult update([FromServices]IHostingEnvironment env, string staffid, [FromBody] dynamic entity)
  230. {
  231. var userid = getStaffUserid(staffid);
  232. //判断参数是否合法
  233. if (string.IsNullOrEmpty(userid))
  234. {
  235. return Json(new
  236. {
  237. success = 0,
  238. msg = "没有登陆"
  239. });
  240. }
  241. JObject obj = JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeObject(entity));
  242. var flag = true;
  243. var msg = "";
  244. if (obj.TryGetValue("field_file", out var value))
  245. {
  246. var l = value.ToString().Split(',');
  247. var param0 = new List<SqlParameter>();
  248. var s = "update sup_UPLOADFILES set InUse =0 where RelativeId= @RelativeId and RelativeTable='newgood';";
  249. param0.Add(new SqlParameter("RelativeId", (int)entity.id));
  250. if (DataAccess.ExecuteCommand(s, param0.ToArray(), out msg) < 1)
  251. {
  252. flag = false;
  253. }
  254. if(value.ToString() == "")
  255. {
  256. }else
  257. foreach (var item in l)
  258. {
  259. var param = new List<SqlParameter>();
  260. var sql = $"update sup_UPLOADFILES set RelativeTable='newgood' ,RelativeId= @RelativeId,InUse=1 where path=@path";
  261. param.Add(new SqlParameter("RelativeId", (int)entity.id));
  262. var path = item.Replace("\\", "/");
  263. param.Add(new SqlParameter("path", path));
  264. if (DataAccess.ExecuteCommand(sql, param.ToArray(), out msg) < 1)
  265. {
  266. flag = false;
  267. }
  268. }
  269. //删除无用的文件
  270. var sqlDel = $"select path from sup_UPLOADFILES where RelativeTable='newgood' and RelativeId= @RelativeId and InUse=0;";
  271. var paramDel = new List<SqlParameter>();
  272. paramDel.Add(new SqlParameter("RelativeId", (int)entity.id));
  273. var dt = new DataTable();
  274. DataAccess.GetValues(sqlDel, ref dt, paramDel.ToArray(), out msg);
  275. if(dt != null && dt.Rows.Count > 0)
  276. {
  277. for(var k = 0; k < dt.Rows.Count; k++)
  278. {
  279. string path = (string)dt.Rows[k]["path"];
  280. path = path.ToString().Replace('/',Path.DirectorySeparatorChar);
  281. System.IO.File.Delete(env.WebRootPath+ path);
  282. }
  283. }
  284. }
  285. if (flag)
  286. {
  287. return Json(new
  288. {
  289. success = true
  290. });
  291. }
  292. return Json(new
  293. {
  294. success = false,
  295. msg
  296. });
  297. }
  298. [HttpPost]
  299. [Route("download")]
  300. public FileResult download(string filePath)
  301. {
  302. if (string.IsNullOrEmpty(filePath))
  303. {
  304. return null;
  305. }
  306. string path = _hostingEnvironment.WebRootPath + "/" + filePath;
  307. string fileName = " ";
  308. byte[] fileBytes = System.IO.File.ReadAllBytes(path);
  309. return File(fileBytes, "application/octet-stream", fileName);
  310. }
  311. public class Temp
  312. {
  313. private string _field_file;
  314. public int ID { get; set; }
  315. public string FileId { get; set; }
  316. public string GoodsId { get; set; }
  317. public string Contact { get; set; }
  318. public int UserId { get; set; }
  319. public string GoodsName { get; set; }
  320. public string ApprovalNo { get; set; }
  321. public int pageIndex { get; set; }
  322. public string sortField { get; set; }
  323. public int pageSize { get; set; }
  324. public int sortDirection { get; set; }
  325. //public dynamic filters { get; set; }
  326. public string field_file { get { return _field_file == null ? "" : _field_file; } set => _field_file = value; }
  327. public string name { get; set; }
  328. public string path { get; set; }
  329. public bool state { get; set; }
  330. public string UserName { get; set; }
  331. }
  332. }
  333. }