public async Task <JsonResult> CreateTask([FromBody] TaskOutDto task) { CheckApiKey(); using (var sqlR = new DeviceTaskRepository(_logger)) { var ret = new InsertResDto(); try { await sqlR.CreateTask(task); ret.IsSuccess = true; } catch (Exception e) { ret.Error = e.Message; if (e is SqlException sqle) { if (sqle.Number == 2601 && sqle.Message.ToLower().Contains("refid")) { ret.AlreadyExist = true; ret.Error = "already exist"; } } } return(Json(ret)); } }
//с ящика public async Task CreateTask(TaskOutDto task) { using (var transaction = new TransactionScope(asyncFlowOption: TransactionScopeAsyncFlowOption.Enabled)) { using (var conn = new SqlConnection(AppSettings.ConnectionString)) { var SqlCreateRemoteTask = "INSERT INTO [DeviceTasks] ([DeviceId],[UserId],[DeviceFaultId],[CreateDate],[RefId]) VALUES (@DeviceId,@UserId,@DeviceFaultId,@CreateDate,@RefId) " + " SELECT SCOPE_IDENTITY()"; var taskId = await conn.ExecuteScalarAsync <int>(SqlCreateRemoteTask, new { task.DeviceId, task.UserId, task.DeviceFaultId, task.CreateDate, task.RefId }); var SqlCreateRemoteTaskComment = "INSERT INTO [DeviceTaskComments] ([DeviceTaskId],[UserId],[Status],[Date]) VALUES (@DeviceTaskId,@UserId,@Status,@Date)"; await conn.ExecuteAsync(SqlCreateRemoteTaskComment, new { DeviceTaskId = taskId, UserId = task.UserId, Status = TaskStatus.Created, Date = task.CreateDate }); transaction.Complete(); } } }
private int ProcessOutTasks() { var sentCount = 0; var outTasks = _localConnection.Query <TaskOut>(SqlGetUnsentTasks).OrderBy(o => o.CreateDate).ToList(); if (outTasks.Any()) { _logger.Debug($"ProcessOutTasks total tasks to send: {outTasks.Count}"); foreach (var item in outTasks) { //отправляем задачу в удаленную базу try { var taskDto = new TaskOutDto { DeviceId = item.DeviceId, UserId = item.UserId, DeviceFaultId = item.DeviceFaultId, CreateDate = item.CreateDate, RefId = item.RefId }; _logger.Debug($"ProcessOutTasks taskDto: DeviceId={taskDto.DeviceId}, DeviceFaultId={taskDto.DeviceFaultId}, UserId={taskDto.UserId}, CreateDate={taskDto.CreateDate}, RefId={taskDto.RefId}"); var response = _remoteClient.PostAsJsonAsync("/api/TabletIssueStationSync/CreateTask", taskDto) .Result; var error = false; if (response.IsSuccessStatusCode) { var result = response.Content.ReadAsAsync <InsertResDto>().Result; if (result.IsSuccess == false && result.AlreadyExist == false) { //какая-то ошибка и это не повтор refid _logger.Error($"ProcessOutTasks request error: {result.Error}"); error = true; } } else { _logger.Error($"ProcessOutTasks non-success request status code: {response.StatusCode}"); error = true; } if (error) { throw new Exception(); } //если успешно - помечаем задачу как отправленную sentCount++; _localConnection.Execute(SqlMarkSentTask, new { item.Id }); } catch (Exception e) { _logger.Error(e, "ProcessOutTasks error"); } } } return(sentCount); }
private List <UserMedalOutDto> AddUserMedal(TaskOutDto dto, WorkUser workUser) { List <UserMedalOutDto> medals = new List <UserMedalOutDto>(); //var rep = ContainerManager.Resolve<IEfRepository>(); using (var tran = _rep.Database.BeginTransaction()) { try { var recordData = _rep.FirstOrDefault <BotSaleUserTotalRecord>(o => o != null && o.IsDeleted != 1 && o.SaleUserId == workUser.WxSaleUser.Id); if (recordData == null) { recordData = new BotSaleUserTotalRecord { Id = Guid.NewGuid().ToString(), SaleUserId = workUser.WxSaleUser.Id, Total = 1, CreateTime = DateTime.Now, CreateUser = workUser.WxSaleUser.Id, }; _rep.Insert(recordData); } else { if (dto.IsNewTask) { recordData.Total += 1; recordData.UpdateTime = DateTime.Now; recordData.UpdateUser = workUser.WxSaleUser.Id; _rep.Update(recordData); } } //按次数 var standardDatas = _rep.Where <BotMedalStandardConfigure>(o => o != null && o.IsDeleted != 1 && o.Ruletotal == recordData.Total && o.KBSBotId == dto.BotManageId).ToList(); if (standardDatas != null && standardDatas.Count > 0) { foreach (var item in standardDatas) { var model = _rep.FirstOrDefault <BotSaleUserMedalInfo>(o => o != null && o.IsDeleted != 1 && o.SaleUserId == workUser.WxSaleUser.Id && o.BotMedalRuleId == item.Id); if (model == null) { _rep.Insert(new BotSaleUserMedalInfo { Id = Guid.NewGuid().ToString(), BotMedalRuleId = item.Id, SaleUserId = workUser.WxSaleUser.Id, MedalSrc = item.MedalYSrc, MedalName = item.MedalName, MedalType = (int)SaleUserMedalType.Number, CreateTime = DateTime.Now, CreateUser = workUser.WxSaleUser.Id, }); medals.Add(new UserMedalOutDto { content = $"您已经和我对话{recordData.Total}次啦,搬个奖章给您", url = item.MedalYSrc, name = item.MedalName, }); } } } //按激活知识包 var businesModel = _rep.FirstOrDefault <BotMedalBusinessConfigure>(o => o != null && o.IsDeleted != 1 && o.FaqPackageId == dto.FaqPackageId && o.KBSBotId == dto.BotManageId); if (businesModel != null) { var userMedal = _rep.FirstOrDefault <BotSaleUserMedalInfo>(o => o != null && o.IsDeleted != 1 && o.SaleUserId == workUser.WxSaleUser.Id && o.BotMedalRuleId == businesModel.Id); if (userMedal == null) { _rep.Insert(new BotSaleUserMedalInfo { Id = Guid.NewGuid().ToString(), BotMedalRuleId = businesModel.Id, SaleUserId = workUser.WxSaleUser.Id, MedalSrc = businesModel.MedalYSrc, MedalName = businesModel.MedalName, MedalType = (int)SaleUserMedalType.Pack, CreateTime = DateTime.Now, CreateUser = workUser.WxSaleUser.Id, }); medals.Add(new UserMedalOutDto { content = $"恭喜您解锁知识勋章:{businesModel.MedalName}", url = businesModel.MedalYSrc, name = businesModel.MedalName, }); } } _rep.SaveChanges(); tran.Commit(); } catch (Exception) { tran.Rollback(); } } return(medals); }