/// <summary> /// 生成文件 /// </summary> /// <param name="batchId"></param> /// <param name="url"></param> /// <returns></returns> public async Task <byte[]> MakeCsvByte(int batchId, string url) { var single = await dbHelper.Single(x => x.id == batchId); if (single == null) { throw new Exception("批次ID有误"); } dbHelper.TranscationBegin(); try { var dbHelper_good = new DapperHelper <PsGoodsEntity>(dbHelper.GetConnection(), dbHelper.GetTransaction()); List <PsGoodsEntity> downList = new List <PsGoodsEntity>(); var opNum = 0; if (single.downNum == 0) { #region 首次下载,生成数据,并添加 for (int i = 0; i < single.goodsNum; i++) { downList.Add(new PsGoodsEntity { id = Guid.NewGuid().ToString("N"), code = $"{new Random(i).Next(10000000, 99999999)}{ new Random(i+single.goodsNum).Next(10000000, 99999999)}", batchId = batchId, proNum = i + 1, lookNum = 0, openid = "", confirmTime = 0 }); } opNum = await dbHelper_good.Saves(new DtoSave <List <PsGoodsEntity> > { data = downList }); if (opNum == 0) { dbHelper.TranscationRollback(); return(null); } #endregion } else { downList = new List <PsGoodsEntity>(await dbHelper_good.FindAll(x => x.batchId == batchId)); downList = downList.OrderBy(x => x.proNum).ToList(); } #region 更新下载次数 single.downNum = single.downNum + 1; opNum = await dbHelper.Update(new DtoSave <PsBatchEntity> { data = single, saveFieldList = new List <string> { "downNum" }, whereList = new List <string> { "id" } }); if (opNum == 0) { dbHelper.TranscationRollback(); return(null); } #endregion dbHelper.TranscationCommit(); #region 生成csv数据 List <byte> reEnt = new List <byte>(); reEnt.AddRange(Encoding.UTF8.GetBytes($"产品编号,防伪码,二维码地址\r\n")); foreach (var item in downList) { string proNumStr = $"0000000000{item.proNum}"; proNumStr = proNumStr.Substring(proNumStr.Length - 10); reEnt.AddRange(Encoding.UTF8.GetBytes($"{single.code.ToUpper()}{proNumStr},{item.code},{url}{item.id}\r\n")); } return(reEnt.ToArray()); #endregion } catch (Exception e) { dbHelper.TranscationRollback(); throw e; } }