示例#1
0
        public async Task AddAsync(PrintTemplateInputDto dto, IFormFile file = null)
        {
            var fileName = Guid.NewGuid().ToString() + ".txt";
            var destPath = $"{_templatesPath}{Path.DirectorySeparatorChar}{fileName}";

            if (file == null)
            {
                using (var writer = System.IO.File.CreateText(destPath))
                {
                    var scripts = Regex.Split(dto.Script, "Kira.LaconicInvoicingScript", RegexOptions.IgnoreCase);
                    foreach (var script in scripts)
                    {
                        await writer.WriteLineAsync(script);
                    }
                }
            }
            else
            {
                //var sourcePath = $"{directory}{Path.DirectorySeparatorChar}temp{dto.Path}";
                //if (!File.Exists(sourcePath))
                //{
                //    throw new BussinessException("无法找到模板文件");
                //}

                //File.Move(sourcePath, destPath);

                using (var stream = new FileStream(destPath, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    await file.CopyToAsync(stream);
                }
            }

            dto.Path = fileName;
            await _printRepo.InsertAsync(dto.MapTo <PrintTemplate>());
        }
示例#2
0
        public async Task UpdateAsync(PrintTemplateInputDto dto, IFormFile file = null)
        {
            if (file != null)
            {
                //var destPath = $"{directory}{Path.DirectorySeparatorChar}template{dto.Path}";
                //if (!File.Exists(destPath))
                //{
                //    var sourcePath = $"{directory}{Path.DirectorySeparatorChar}temp{dto.Path}";
                //    File.Move(sourcePath, destPath);
                //}

                //File.Delete($"{directory}{Path.DirectorySeparatorChar}template{printTemplate.Path}");

                var fileName = Guid.NewGuid().ToString() + ".txt";
                using (var stream = new FileStream(_templatesPath + Path.DirectorySeparatorChar + fileName, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    await file.CopyToAsync(stream);
                }

                dto.Path = fileName;
            }
            else
            {
                var destPath = $"{_templatesPath}{Path.DirectorySeparatorChar}{dto.Path}";
                using (var writer = new StreamWriter(System.IO.File.Open(destPath, FileMode.Truncate, FileAccess.Write)))
                {
                    var scripts = Regex.Split(dto.Script, "Kira.LaconicInvoicingScript", RegexOptions.IgnoreCase);
                    for (var i = 0; i < scripts.Length; i++)
                    {
                        if (i > 0)
                        {
                            await writer.WriteLineAsync("Kira.LaconicInvoicingScript" + scripts[i]);
                        }
                        else
                        {
                            await writer.WriteLineAsync(scripts[i]);
                        }
                    }
                }
            }

            await _printRepo.UpdateAsync(dto.MapTo <PrintTemplate>());
        }