123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using AutoMapper;
- using GxPress.Common.AppOptions;
- using GxPress.Common.Tools;
- using GxPress.Entity.WorkFlow;
- using GxPress.Repository.Interface;
- using GxPress.Repository.Interface.WorkFlow;
- using GxPress.Repository.Interface.WorkProcess;
- using GxPress.Request.App.Flow;
- using Microsoft.Extensions.Options;
- using Datory;
- using GxPress.EnumConst;
- using Dapper;
- using GxPress.Common.Page;
- namespace GxPress.Repository.Implement.WorkFlow
- {
- public class FlowRepository : IFlowRepository
- {
- private readonly Repository<Flow> _repository;
- private readonly IMapper _mapper;
- private readonly IFlowFieldValueRepository _flowFieldValueRepository;
- private readonly IFlowTodoRepository _flowTodoRepository;
- private readonly IProcessRepository _processRepository;
- private readonly IProcessFieldRepository _processFieldRepository;
- private readonly IProcessGroupRepository _processGroupRepository;
- private readonly IProcessNodeRepository _processNodeRepository;
- private readonly IProcessRequestLimitRepository _processRequestLimitRepository;
- private readonly IRuleConditionRepository _conditionRuleRepository;
- private readonly IRuleCarbonCopyRepository _ruleCarbonCopyRepository;
- private readonly IRuleApproverCheckRepository _ruleApproverCheckRepository;
- private readonly IDepartmentRepository _departmentRepository;
- private readonly IRoleRepository _roleRepository;
- private readonly IUserRepository _userRepository;
- private readonly string _connectionString;
- private readonly string _databaseTypeStr;
- public FlowRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper,
- IFlowFieldValueRepository flowFieldValueRepository,
- IFlowTodoRepository flowTodoRepository,
- IProcessRepository processRepository,
- IProcessFieldRepository processFieldRepository,
- IProcessGroupRepository processGroupRepository,
- IProcessRequestLimitRepository processRequestLimitRepository,
- IProcessNodeRepository processNodeRepository,
- IRuleConditionRepository conditionRuleRepository,
- IRuleCarbonCopyRepository ruleCarbonCopyRepository,
- IRuleApproverCheckRepository ruleApproverCheckRepository,
- IDepartmentRepository departmentRepository,
- IRoleRepository roleRepository,
- IUserRepository userRepository)
- {
- _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
- _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
- var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
- var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
- _repository = new Repository<Flow>(database);
- _mapper = mapper;
- _flowFieldValueRepository = flowFieldValueRepository;
- _flowTodoRepository = flowTodoRepository;
- _processRepository = processRepository;
- _processFieldRepository = processFieldRepository;
- _processGroupRepository = processGroupRepository;
- _processRequestLimitRepository = processRequestLimitRepository;
- _processNodeRepository = processNodeRepository;
- _conditionRuleRepository = conditionRuleRepository;
- _ruleCarbonCopyRepository = ruleCarbonCopyRepository;
- _ruleApproverCheckRepository = ruleApproverCheckRepository;
- _departmentRepository = departmentRepository;
- _roleRepository = roleRepository;
- _userRepository = userRepository;
- }
- public IDatabase Database => _repository.Database;
- public string TableName => _repository.TableName;
- public List<TableColumn> TableColumns => _repository.TableColumns;
- public async Task<IEnumerable<Flow>> GetListAsync(int processId)
- {
- return await _repository.GetAllAsync(Q
- .Where(nameof(Flow.ProcessId), processId)
- );
- }
- public async Task<int> InsertAsync(Flow flow)
- {
- var id = await _repository.InsertAsync(flow);
- var no = StringUtils.GetFlowNo(DateTime.Now, id);
- await _repository.UpdateAsync(Q
- .Set(nameof(Flow.No), no)
- .Where(nameof(Flow.Id), id)
- );
- return id;
- }
- public async Task<Flow> GetAsync(int flowId)
- {
- return await _repository.GetAsync(flowId);
- }
- public async Task<int> GetCountByUserIdAsync(int userId, bool checking, FlowListRequest request)
- {
- string sql = $@"SELECT
- count(1)
- FROM
- ccpph.tede_flow a
- WHERE 1=1 ";
- if (!checking)
- sql += $@" and a.State IN ('Denied','Checked')";
- else
- sql += $@" and a.State Not IN ('Denied','Checked')";
- sql += $@" AND a.UserId = {userId}
- AND a.ProcessId IN (SELECT
- id
- FROM
- ccpph.tede_process)";
- if (!string.IsNullOrWhiteSpace(request.Keyword))
- {
- sql += $@" AND (a.ProcessId IN (SELECT
- Id
- FROM
- tede_process
- WHERE
- name LIKE '%{request.Keyword}%')
- OR a.Id IN (SELECT
- FlowId
- FROM
- tede_flow_field_value
- WHERE
- StringValues LIKE '%{request.Keyword}%')
- OR a.UserId IN (SELECT
- Id
- FROM
- ccpph.tede_user
- WHERE
- Name LIKE '%{request.Keyword}%') or a.Id In(SELECT
- FlowId
- FROM
- tede_flow_todo
- WHERE
- UserId IN (SELECT
- Id
- FROM
- tede_user
- WHERE
- Name LIKE '%{request.Keyword}%'))
- or a.Id In(SELECT
- FlowId
- FROM
- tede_flow_message
- WHERE
- Message LIKE '%{request.Keyword}%'))";
- }
- if (request.ProcessIds.Count > 0)
- {
- var processIdValue = "";
- foreach (var item in request.ProcessIds)
- {
- if (item == 0)
- continue;
- processIdValue += $"{item},";
- }
- if (!string.IsNullOrWhiteSpace(processIdValue))
- {
- processIdValue = processIdValue.Remove(processIdValue.Length - 1, 1);
- sql += $" and a.ProcessId in({processIdValue})";
- }
- }
- sql += "ORDER BY a.CreatedDate DESC";
- var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
- var database = new Database(databaseType, _connectionString);
- var connection = database.GetConnection();
- return await connection.ExecuteScalarAsync<int>(sql);
- }
- /// <summary>
- /// 新增 未读数量
- /// </summary>
- /// <param name="userId"></param>
- /// <param name="checking"></param>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<int> GetUReadCountByUserIdAsync(int userId, bool checking, FlowListRequest request)
- {
- var query = Q
- .Where(nameof(Flow.UserId), userId)
- .WhereNot(nameof(Flow.ProcessId), 0);
- if (checking)
- {
- query.WhereNotIn(nameof(Flow.State), new List<string>
- {
- //FlowState.Meeting.GetValue(),
- FlowState.Checked.GetValue(),
- FlowState.Denied.GetValue()
- });
- }
- else
- {
- query.WhereIn(nameof(Flow.State), new List<string>
- {
- //FlowState.Meeting.GetValue(),
- FlowState.Checked.GetValue(),
- FlowState.Denied.GetValue()
- });
- }
- if (request.ProcessIds != null && request.ProcessIds.Any())
- {
- query.WhereIn(nameof(Flow.ProcessId), request.ProcessIds);
- }
- // if (!string.IsNullOrEmpty(request.Keyword))
- // {
- // var like = $"%{request.Keyword}%";
- // query.WhereLike(nameof(Flow.ProcessId), like);
- // }
- #region 新增
- if (!string.IsNullOrEmpty(request.Keyword))
- {
- if (int.TryParse(request.Keyword, out int dd))
- {
- var like = $"%{request.Keyword}%";
- query.WhereLike(nameof(Flow.ProcessId), like);
- }
- }
- if (!string.IsNullOrWhiteSpace(request.Keyword))
- {
- //获取用户
- var users = await _userRepository.GetAllAsync(Q.WhereLike(nameof(Entity.User.Name), $"%{request.Keyword}%"));
- query.WhereIn(nameof(FlowTodo.UserId), users.Select(n => n.Id));
- }
- #endregion
- return await _repository.CountAsync(query);
- }
- public async Task<IEnumerable<Flow>> GetListByUserIdAsync(int userId, bool checking, FlowListRequest request)
- {
- string sql = $@"SELECT
- a.*
- FROM
- ccpph.tede_flow a
- WHERE 1=1 ";
- if (!checking)
- sql += $@" and a.State IN ('Denied','Checked')";
- else
- sql += $@" and a.State Not IN ('Denied','Checked')";
- sql += $@" AND a.UserId = {userId}
- AND a.ProcessId IN (SELECT
- id
- FROM
- ccpph.tede_process)";
- if (!string.IsNullOrWhiteSpace(request.Keyword))
- {
- sql += $@" AND (a.ProcessId IN (SELECT
- Id
- FROM
- tede_process
- WHERE
- name LIKE '%{request.Keyword}%')
- OR a.Id IN (SELECT
- FlowId
- FROM
- tede_flow_field_value
- WHERE
- StringValues LIKE '%{request.Keyword}%')
- OR a.UserId IN (SELECT
- Id
- FROM
- ccpph.tede_user
- WHERE
- Name LIKE '%{request.Keyword}%') or a.Id In(SELECT
- FlowId
- FROM
- tede_flow_todo
- WHERE
- UserId IN (SELECT
- Id
- FROM
- tede_user
- WHERE
- Name LIKE '%{request.Keyword}%'))
- or a.Id In(SELECT
- FlowId
- FROM
- tede_flow_message
- WHERE
- Message LIKE '%{request.Keyword}%'))";
- }
- if (request.ProcessIds.Count > 0)
- {
- var processIdValue = "";
- foreach (var item in request.ProcessIds)
- {
- if (item == 0)
- continue;
- processIdValue += $"{item},";
- }
- if (!string.IsNullOrWhiteSpace(processIdValue))
- {
- processIdValue = processIdValue.Remove(processIdValue.Length - 1, 1);
- sql += $" and a.ProcessId in({processIdValue})";
- }
- }
- sql += $"ORDER BY a.CreatedDate DESC limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
- var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
- var database = new Database(databaseType, _connectionString);
- var connection = database.GetConnection();
- var items = await connection.QueryAsync<Flow>(sql);
- //result.Total = await connection.ExecuteScalarAsync<int>(AssembleSqlCount(request));
- return items;
- }
- public async Task<IEnumerable<Flow>> GetListByIdListAsync(IEnumerable<int> flowIdList, int page, int perPage)
- {
- return await _repository.GetAllAsync(Q
- .WhereIn(nameof(Flow.Id), flowIdList)
- .WhereNot(nameof(Flow.ProcessId), 0)
- .ForPage(page, perPage)
- .OrderByDesc(nameof(Flow.Id))
- );
- }
- public async Task<int> GetCountAsync(int processId, string state, string startDate, string endDate)
- {
- var query = Q
- .WhereNot(nameof(Flow.ProcessId), 0);
- if (processId > 0)
- {
- query.Where(nameof(Flow.ProcessId), processId);
- }
- if (!string.IsNullOrEmpty(state) && !StringUtils.EqualsIgnoreCase(state, "all"))
- {
- query.Where(nameof(Flow.State), state);
- }
- if (!string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
- {
- query.WhereBetween(nameof(Flow.CreatedDate), StringUtils.ToDateTime(startDate), StringUtils.ToDateTime(endDate));
- }
- return await _repository.CountAsync(query);
- }
- public async Task<IEnumerable<Flow>> GetListAllAsync(int page, int perPage, int processId, string state, string startDate, string endDate)
- {
- var query = Q
- .WhereNot(nameof(Flow.ProcessId), 0)
- .ForPage(page, perPage)
- .OrderByDesc(nameof(Flow.Id));
- if (processId > 0)
- {
- query.Where(nameof(Flow.ProcessId), processId);
- }
- if (!string.IsNullOrEmpty(state) && !StringUtils.EqualsIgnoreCase(state, "all"))
- {
- query.Where(nameof(Flow.State), state);
- }
- if (!string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
- {
- query.WhereBetween(nameof(Flow.CreatedDate), StringUtils.ToDateTime(startDate), StringUtils.ToDateTime(endDate));
- }
- return await _repository.GetAllAsync(query);
- }
- public async Task<PagedList<Flow>> GetFlowListAllAsync(int page, int perPage, int processId, string state, string startDate, string endDate)
- {
- var result = new PagedList<Flow>();
- var query = new SqlKata.Query("tede_flow");
- query.WhereNot(nameof(Flow.ProcessId), 0);
- if (processId > 0)
- query.Where(nameof(Flow.ProcessId), processId);
- if (!string.IsNullOrEmpty(state) && !StringUtils.EqualsIgnoreCase(state, "all"))
- {
- if (state.Equals("Meeting"))
- {
- var queryParameter = new SqlKata.Query("tede_flow_todo");
- queryParameter.WhereColumns("tede_flow_todo.FlowId", "=", "tede_flow.Id");
- queryParameter.Where("tede_flow_todo.DoneType", 1);
- queryParameter.Limit(1);
- queryParameter.Select("tede_flow_todo.FlowId");
- query.Where(nameof(Entity.WorkFlow.Flow.Id), "=", queryParameter);
- }
- else if (state.Equals("Checking"))
- query.Where(n => n.Where(nameof(Flow.State), 4).OrWhere(nameof(Entity.WorkFlow.Flow.State), state));
- else
- query.Where(nameof(Flow.State), state);
- }
- if (!string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
- query.WhereBetween(nameof(Flow.CreatedDate), StringUtils.ToDateTime(startDate), StringUtils.ToDateTime(endDate));
- result.Total = await _repository.CountAsync(query);
- query.ForPage(page, perPage).OrderByDesc(nameof(Flow.Id));
- result.Items = await _repository.GetAllAsync(query);
- return result;
- }
- public async Task<bool> DeleteAsync(int flowId)
- {
- return await _repository.DeleteAsync(flowId);
- }
- public async Task UpdateStateAsync(int flowId, string state)
- {
- await _repository.UpdateAsync(Q
- .Set(nameof(Flow.State), state)
- .Where(nameof(Flow.Id), flowId)
- );
- }
- public async Task UpdateProcessNodeIdAsync(int flowId, int processNodeId)
- {
- await _repository.UpdateAsync(Q
- .Set(nameof(Flow.ProcessNodeId), processNodeId)
- .Where(nameof(Flow.Id), flowId)
- );
- }
- public async Task<int> CountAsync(SqlKata.Query query)
- {
- return await _repository.CountAsync(query);
- }
- }
- }
|