using System; using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using Dapper; using Datory; using GxPress.Common.AppOptions; using GxPress.Common.Tools; using GxPress.Entity.WaitHandle; using GxPress.Repository.Interface.WaitHandle; using GxPress.Request.App.WaitHandle; using GxPress.Result.App.WaitHandle; using Microsoft.Extensions.Options; using SqlKata; namespace GxPress.Repository.Implement.WaitHandle { public class WaitHandleRepository : IWaitHandleRepository { private readonly Repository _repository; private readonly IMapper _mapper; private readonly string _connectionString; private readonly string _databaseTypeStr; public WaitHandleRepository(IOptionsMonitor dbOptionsAccessor, IMapper mapper) { _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType; _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString; var databaseType = StringUtils.ToEnum(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql); var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString); _repository = new Repository(database); _mapper = mapper; } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; public async Task InsertAsync(Entity.WaitHandle.WaitHandle waitHandle) { return await _repository.InsertAsync(waitHandle); } public async Task UpdateAsync(Entity.WaitHandle.WaitHandle waitHandle) { return await _repository.UpdateAsync(waitHandle); } public async Task UpdateAsync(Query query) { return await _repository.UpdateAsync(query) > 0; } public async Task DeleteAsync(int id) { return await _repository.DeleteAsync(id); } public async Task DeleteAsync(Query query) { return await _repository.DeleteAsync(query) > 0; } public async Task GetAsync(Query query) { return await _repository.GetAsync(query); } public async Task GetAsync(int id) { return await _repository.GetAsync(id); } public async Task> GetAllAsync(Query query) { return await _repository.GetAllAsync(query); } public async Task CountAsync(Query query) { return await _repository.CountAsync(query); } /// /// 查询 /// /// /// public async Task GetAllAsync(ListWaitHandleRequest request) { var @dateTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); var waitHandlesSql = $@" SELECT a.*,(SELECT COUNT(1) FROM tede_wait_handle WHERE DATE_ADD(AlarmTime, INTERVAL -Minute MINUTE)< NOW() and Id=a.Id and IsFinish=0) IsFinish, b.Title FROM tede_wait_handle a LEFT JOIN tede_wait_handle_label b ON a.LabelId = b.Id WHERE a.UserId = {request.UserId} and a.IsFinish=0"; var finishWaitHandlesSql = $@"SELECT a.*, b.Title FROM tede_wait_handle a LEFT JOIN tede_wait_handle_label b ON a.LabelId = b.Id WHERE a.UserId = {request.UserId} and a.IsFinish=1"; var result = new WaitHandleListResult(); if (request.SearchType == 1) { waitHandlesSql += " order by a.IsFinish desc"; finishWaitHandlesSql += " order by a.IsFinish desc"; } else if (request.SearchType == 2) { waitHandlesSql += " order by a.AlarmTime asc"; finishWaitHandlesSql += " order by a.AlarmTime desc"; } else if (request.SearchType == 3) { waitHandlesSql += " order by a.LabelId desc"; finishWaitHandlesSql += " order by a.LabelId desc"; } else if (request.SearchType == 4) { waitHandlesSql += " order by a.Level desc"; finishWaitHandlesSql += " order by a.Level desc"; } else { waitHandlesSql += " order by a.AlarmTime asc"; finishWaitHandlesSql += " order by a.AlarmTime desc"; } var databaseType = _databaseTypeStr.ToEnum(DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var waitHandles = await connection .QueryAsync(waitHandlesSql, (waitHandleResult, waitHandleLabel) => { waitHandleResult.LabelName = waitHandleLabel != null ? waitHandleLabel.Title : ""; return waitHandleResult; }, splitOn: "Title"); result.WaitHandles = waitHandles; var finishWaitHandles = await connection .QueryAsync(finishWaitHandlesSql, (waitHandleResult, waitHandleLabel) => { waitHandleResult.LabelName = waitHandleLabel != null ? waitHandleLabel.Title : "未设置"; return waitHandleResult; }, splitOn: "Title"); result.FinishWaitHandles = finishWaitHandles; return result; } public async Task GetUFinishAsync(int userId) { string sql = $"SELECT count(1) FROM tede_wait_handle WHERE DATE_ADD(AlarmTime,INTERVAL -Minute MINUTE) < NOW() and UserId={userId} and IsFinish=0"; var databaseType = _databaseTypeStr.ToEnum(DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var count = await connection.ExecuteScalarAsync(sql); return count; } /// /// 获取详情 /// /// /// public async Task GetWaitAsync(int id) { var waitHandle = await _repository.GetAsync(id); if (!string.IsNullOrEmpty(waitHandle.AlarmTime)) { var AlarmTime = DateTime.Parse(waitHandle.AlarmTime); var alarmTime = AlarmTime.AddMinutes(-waitHandle.Minute); //设置 if (alarmTime <= System.DateTime.Now) { waitHandle.IsFinish = true; await _repository.UpdateAsync(waitHandle); } } return waitHandle; } } }