1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| public interface IBatJobService : ISyncJobService { void SyncProcess(CommCode jobSetting); }
public class BatJobService : IBatJobService { private readonly string _connStr; private readonly IBusinessDateService _businessDateService; public NXBatJobService(IConfiguration config , IBusinessDateService businessDateService) { _connStr = Encoding.UTF8.GetString(Convert.FromBase64String(config.GetConnectionString("ConnStrKeyword"))); _businessDateService = businessDateService; }
public void SyncProcess(CommCode jobSetting) { using (var _conn = new SqlConnection(_connStr)) { var batchId = getAutoId(); var batSettings = jobSetting.CodeVal2.Split(";"); var dirPath = _conn.Query<CommCode>(@"SELECT * FROM CommCode WHERE CodeType = 'DirName'") .FirstOrDefault(); var batFilePath = batSettings[0].Replace("@DirName", dirPath.CodeName); var batLog = new BatchLog() { BatchId = batchId, BatFilePath = batFilePath, BatFileName = batSettings[1], ResultFilePath = batSettings[2], ResultFileName = batSettings[3], DataStartRow = string.IsNullOrEmpty(batSettings[4]) ? default(int) : int.Parse(batSettings[4]), CapPublishName = "", CapPublishParaFname = batSettings[5], CapPublishParaDataDate = _businessDateService.GetBusinessDateWithParameter( (batSettings[6], "yyyy/MM/dd")), Result = "N", ErrorMessage = "", AuditUser = "SYS", AuditTime = DateTime.Now }; var insertCount = _conn.Execute(@"INSERT INTO BatchLog Values(@BatchId, @BatFilePath, @BatFileName, @ResultFilePath, @ResultFileName , @DataStartRow, @CapPublishName, @CapPublishParaFname, @CapPublishParaDataDate, @Result , @ErrorMessage, @AuditUser, @AuditTime)" , batLog);
var timeoutSecs = 10; int.TryParse(batSettings[7], out timeoutSecs); if (PoolingService.RetryUntilSuccessOrTimeout(() => { var currBatLog = _conn.Query<BatchLog>( @"SELECT * FROM BatchLog WHERE BatchId = @BatchId AND Result = @Result" , new { BatchId = batchId, Result = "Y" }).FirstOrDefault(); return currBatLog != null; }, TimeSpan.FromSeconds(timeoutSecs))) { var spOrNot = batSettings.Length > 8 && string.IsNullOrEmpty(batSettings[8]) == false; if (spOrNot) { var spSql = _businessDateService.GetBusinessDateWithParameter( (batSettings[8], "yyyy/MM/dd")); _conn.Execute(spSql); } } else { throw new Exception("Bat執行未成功或等待時間已過!"); } } } }
|