public async Task <ApiResult> GetModuleByID(int id) { ApiResult ret = new ApiResult(); List <List <string> > showData = new List <List <string> >(); List <object> showMerge = new List <object>(); try { PMModule pMModule = await _repo.GetModuleByID(id); if (pMModule.Location != null && pMModule.LocationBy != null) { List <QueryItem> queryItems = await _importRepo.ListAllLocations(pMModule.Location, pMModule.LocationBy); pMModule.LocationName = queryItems.FirstOrDefault().Name; } using (FileStream file = new FileStream(pMModule.FilePath, FileMode.Open, FileAccess.Read)) { string fileType = Path.GetExtension(pMModule.FileName); ListShowModule(file, fileType, ref showData, ref showMerge); } ret.data = new { obj = pMModule, data = showData, mergeCells = showMerge }; } catch (Exception ex) { ret.code = Code.Failure; ret.msg = ex.Message; } return(ret); }
//[RequestSizeLimit(52428800)] public ActionResult ImportModule([FromForm] PMModule pMModule, IFormFile file) { var ret = _service.ImportModule(pMModule, file); return(Ok(ret.Result)); //return Ok(""); }
public async Task <ApiResult> GetEntityByID(int id, bool isUpdate) { ApiResult ret = new ApiResult(); List <List <string> > showData = new List <List <string> >(); List <object> showMerge = new List <object>(); PMModule module = null; List <ConstructionPlanMonthDetail> cpmd = null; PMEntity entity = null; try { if (isUpdate) { entity = await _repo.GetEntityByID(id); module = await _repo.GetModuleByID(entity.Module); cpmd = await _detailRepo.GetByIDs(await _repo.ListMonthDetail(id)); List <QueryItem> locations = await _importRepo.ListAllLocations(); foreach (var item in cpmd) { item.LocationName = locations.Where(a => a.LocationBy == item.LocationBy && a.ID == item.Location) .FirstOrDefault().Name; } } else { entity = await _repo.GetEntityDetailByID(id); entity.LocationName = (await _importRepo.ListAllLocations(entity.Location, entity.Locationby)) .FirstOrDefault().Name; } using (FileStream file = new FileStream(entity.FilePath, FileMode.Open, FileAccess.Read)) { string fileType = Path.GetExtension(entity.FilePath); ListShowModule(file, fileType, ref showData, ref showMerge); } if (isUpdate) { ret.data = new { entity.Eqp, module, cpmd, showData, showMerge } } ; else { ret.data = new { entity, showData, showMerge } }; } catch (Exception ex) { ret.code = Code.Failure; ret.msg = ex.Message; } return(ret); }
public async Task <int> SavePMModule(PMModule pmModule) { return(await WithConnection(async c => { string sql = " insert into pm_module " + " values (0,@Code,@Name,@FileName,@FilePath,@Major,@Line,@Location,@LocationBy," + " @LocationPath,@DeviceName,@KeyWord,@Department,@DeathYear,@Level,@CreatedBy,@CreatedTime); "; sql += "SELECT LAST_INSERT_ID() "; int newid = await c.QueryFirstOrDefaultAsync <int>(sql, pmModule); return newid; })); }
public async Task <ApiResult> ImportModule(PMModule pMModule, IFormFile file) { ApiResult ret = new ApiResult(); pMModule.CreatedTime = DateTime.Now; pMModule.CreatedBy = _userID; List <List <string> > showData = new List <List <string> >(); List <object> showMerge = new List <object>(); try { //if (file.Length > 0) { //利用IFormFile里面的OpenReadStream()方法直接读取文件流 Stream stream = file.OpenReadStream(); string fileType = Path.GetExtension(file.FileName); ListShowModule(stream, fileType, ref showData, ref showMerge); ret.data = new { data = showData, mergeCells = showMerge }; PDFHelper pdf = new PDFHelper(); string fileNameNew = Guid.NewGuid().ToString(); string[] fileName = file.FileName.Split('.'); string ext = "." + fileName[fileName.Length - 1]; pMModule.FilePath = FilePath.PMPATH.Replace('/', '\\') + "Module\\" + fileNameNew + ext; pMModule.FileName = file.FileName; await _repo.SavePMModule(pMModule); pdf.SaveFile(file, pMModule.FilePath); } return(ret); } catch (Exception ex) { ret.code = Code.Failure; ret.msg = ex.Message; return(ret); } }
public async Task <ApiResult> UpdatePMEntity(PMEntity pmEntity) { ApiResult ret = new ApiResult(); EqpHistory eqp = new EqpHistory(); DateTime dt = DateTime.Now; pmEntity.UpdatedBy = _userID; pmEntity.UpdatedTime = dt; pmEntity.Status = (int)PMStatus.Editing; if (pmEntity.IsFinished) { pmEntity.Status = (int)PMStatus.Finished; if (pmEntity.Eqp != null) { eqp.CreatedBy = _userID; eqp.CreatedTime = dt; eqp.EqpID = (int)pmEntity.Eqp; eqp.Type = (int)EqpHistoryType.Maintenance; eqp.ShowName = pmEntity.Title; eqp.WorkingOrder = pmEntity.ID; } } try { using (TransactionScope scope = new TransactionScope()) { //保存excel PMModule module = await _repo.GetModuleByIDEasy(pmEntity.Module); PMEntity oldEntity = await _repo.GetEntityByID(pmEntity.ID); string modulePath, entityPath, fileName; string fileType = Path.GetExtension(module.FileName); entityPath = oldEntity.FilePath; if (File.Exists(entityPath)) { File.Delete(entityPath); } if (pmEntity.Module != oldEntity.Module) { //复制模板为实例 modulePath = Path.GetDirectoryName(module.FilePath); entityPath = modulePath.Substring(0, modulePath.Length - 6) + "Entity\\"; fileName = pmEntity.Title + dt.ToString("yyyyMMdd"); fileType = Path.GetExtension(module.FileName); entityPath += fileName + fileType; } File.Copy(module.FilePath, entityPath); SaveExcelEntity(entityPath, fileType, pmEntity); if (pmEntity.Module != oldEntity.Module || pmEntity.IsPlanChanged || pmEntity.Status != oldEntity.Status) { pmEntity.FilePath = entityPath; ret.data = await _repo.UpdatePMEntity(pmEntity); } if (pmEntity.IsPlanChanged) { await _repo.DelPMEntityMonthDetail(new string[] { pmEntity.ID.ToString() }); List <PMEntityMonthDetail> details = new List <PMEntityMonthDetail>(); foreach (int item in pmEntity.PMMonthDetails) { PMEntityMonthDetail detail = new PMEntityMonthDetail(); detail.MonthDetail = item; detail.PMEntity = pmEntity.ID; details.Add(detail); } ret.data = await _repo.SavePMEntityMonthDetail(details); } if (pmEntity.IsFinished) { await _repo.SaveEqpHistory(eqp); } scope.Complete(); } return(ret); } catch (Exception ex) { ret.code = Code.Failure; ret.msg = ex.Message; return(ret); } }