|
| 1 | +package com.codingapi.example.app.query.service; |
| 2 | + |
| 3 | +import com.codingapi.example.domain.user.entity.User; |
| 4 | +import com.codingapi.example.domain.user.repository.UserRepository; |
| 5 | +import com.codingapi.example.infra.flow.entity.FlowRecordEntity; |
| 6 | +import com.codingapi.example.infra.flow.jpa.FlowRecordEntityRepository; |
| 7 | +import com.codingapi.springboot.fast.jpa.SQLBuilder; |
| 8 | +import com.codingapi.springboot.framework.dto.request.SearchRequest; |
| 9 | +import com.codingapi.springboot.security.gateway.TokenContext; |
| 10 | +import lombok.AllArgsConstructor; |
| 11 | +import org.springframework.data.domain.Page; |
| 12 | +import org.springframework.data.domain.PageRequest; |
| 13 | +import org.springframework.stereotype.Service; |
| 14 | +import org.springframework.util.StringUtils; |
| 15 | + |
| 16 | +@Service |
| 17 | +@AllArgsConstructor |
| 18 | +public class FlowAppQueryService { |
| 19 | + |
| 20 | + private final FlowRecordEntityRepository flowRecordQuery; |
| 21 | + private final UserRepository userRepository; |
| 22 | + |
| 23 | + public Page<FlowRecordEntity> list(SearchRequest searchRequest) { |
| 24 | + String lastId = searchRequest.getParameter("lastId"); |
| 25 | + SQLBuilder sqlBuilder = new SQLBuilder("from FlowRecordEntity d where d.id in (select max(r.id) from FlowRecordEntity r group by r.processId ) "); |
| 26 | + if(StringUtils.hasText(lastId)){ |
| 27 | + sqlBuilder.append(" and d.id < ?",Long.parseLong(lastId)); |
| 28 | + } |
| 29 | + sqlBuilder.appendSql(" order by d.id desc "); |
| 30 | + PageRequest pageRequest = PageRequest.of(0, searchRequest.getPageSize()); |
| 31 | + return flowRecordQuery.dynamicPageQuery(sqlBuilder,pageRequest); |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + public Page<FlowRecordEntity> findAllByOperatorId(SearchRequest searchRequest) { |
| 36 | + User user = userRepository.getUserByUsername(TokenContext.current().getUsername()); |
| 37 | + String lastId = searchRequest.getParameter("lastId"); |
| 38 | + SQLBuilder sqlBuilder = new SQLBuilder("from FlowRecordEntity d where d.id in (select max(r.id) from FlowRecordEntity r where r.currentOperatorId = ?1 group by r.processId) "); |
| 39 | + sqlBuilder.addParam(user.getId()); |
| 40 | + if(StringUtils.hasText(lastId)){ |
| 41 | + sqlBuilder.append(" and d.id < ?",Long.parseLong(lastId)); |
| 42 | + } |
| 43 | + sqlBuilder.appendSql(" order by d.id desc "); |
| 44 | + PageRequest pageRequest = PageRequest.of(0, searchRequest.getPageSize()); |
| 45 | + return flowRecordQuery.dynamicPageQuery(sqlBuilder,pageRequest); |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + public Page<FlowRecordEntity> findTodoByOperatorId(SearchRequest searchRequest) { |
| 50 | + User user = userRepository.getUserByUsername(TokenContext.current().getUsername()); |
| 51 | + String lastId = searchRequest.getParameter("lastId"); |
| 52 | + SQLBuilder sqlBuilder = new SQLBuilder("from FlowRecordEntity r where r.currentOperatorId = ?1 and r.flowType = 'TODO' and r.flowStatus = 'RUNNING' "); |
| 53 | + sqlBuilder.addParam(user.getId()); |
| 54 | + if(StringUtils.hasText(lastId)){ |
| 55 | + sqlBuilder.append(" and r.id < ?",Long.parseLong(lastId)); |
| 56 | + } |
| 57 | + sqlBuilder.appendSql(" order by r.id desc "); |
| 58 | + PageRequest pageRequest = PageRequest.of(0, searchRequest.getPageSize()); |
| 59 | + return flowRecordQuery.dynamicPageQuery(sqlBuilder,pageRequest); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + public Page<FlowRecordEntity> findDoneByOperatorId(SearchRequest searchRequest) { |
| 64 | + User user = userRepository.getUserByUsername(TokenContext.current().getUsername()); |
| 65 | + String lastId = searchRequest.getParameter("lastId"); |
| 66 | + SQLBuilder sqlBuilder = new SQLBuilder("from FlowRecordEntity d where d.id in (select max(r.id) from FlowRecordEntity r where r.currentOperatorId = ?1 and r.flowType = 'DONE' group by r.processId ) "); |
| 67 | + sqlBuilder.addParam(user.getId()); |
| 68 | + if(StringUtils.hasText(lastId)){ |
| 69 | + sqlBuilder.append(" and d.id < ?",Long.parseLong(lastId)); |
| 70 | + } |
| 71 | + sqlBuilder.appendSql(" order by d.id desc "); |
| 72 | + PageRequest pageRequest = PageRequest.of(0, searchRequest.getPageSize()); |
| 73 | + return flowRecordQuery.dynamicPageQuery(sqlBuilder,pageRequest); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + public Page<FlowRecordEntity> findInitiatedByOperatorId(SearchRequest searchRequest) { |
| 78 | + User user = userRepository.getUserByUsername(TokenContext.current().getUsername()); |
| 79 | + String lastId = searchRequest.getParameter("lastId"); |
| 80 | + SQLBuilder sqlBuilder = new SQLBuilder("from FlowRecordEntity d where d.id in (select max(r.id) from FlowRecordEntity r where r.currentOperatorId = ?1 and r.preId = 0 and r.nodeCode = 'start' group by r.processId) "); |
| 81 | + sqlBuilder.addParam(user.getId()); |
| 82 | + if(StringUtils.hasText(lastId)){ |
| 83 | + sqlBuilder.append(" and d.id < ?",Long.parseLong(lastId)); |
| 84 | + } |
| 85 | + sqlBuilder.appendSql(" order by d.id desc "); |
| 86 | + PageRequest pageRequest = PageRequest.of(0, searchRequest.getPageSize()); |
| 87 | + return flowRecordQuery.dynamicPageQuery(sqlBuilder,pageRequest); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + public Page<FlowRecordEntity> findTimeoutTodoByOperatorId(SearchRequest searchRequest) { |
| 92 | + User user = userRepository.getUserByUsername(TokenContext.current().getUsername()); |
| 93 | + String lastId = searchRequest.getParameter("lastId"); |
| 94 | + SQLBuilder sqlBuilder = new SQLBuilder("from FlowRecordEntity r where r.currentOperatorId = ?1 and r.flowType = 'TODO' and r.flowStatus = 'RUNNING' and r.timeoutTime >0 and r.timeoutTime < ?2 "); |
| 95 | + sqlBuilder.addParam(user.getId()); |
| 96 | + sqlBuilder.addParam(System.currentTimeMillis()); |
| 97 | + if(StringUtils.hasText(lastId)){ |
| 98 | + sqlBuilder.append(" and r.id < ?",Long.parseLong(lastId)); |
| 99 | + } |
| 100 | + sqlBuilder.appendSql(" order by r.id desc "); |
| 101 | + PageRequest pageRequest = PageRequest.of(0, searchRequest.getPageSize()); |
| 102 | + return flowRecordQuery.dynamicPageQuery(sqlBuilder,pageRequest); |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + public Page<FlowRecordEntity> findPostponedTodoByOperatorId(SearchRequest searchRequest) { |
| 107 | + User user = userRepository.getUserByUsername(TokenContext.current().getUsername()); |
| 108 | + String lastId = searchRequest.getParameter("lastId"); |
| 109 | + SQLBuilder sqlBuilder = new SQLBuilder("from FlowRecordEntity r where r.currentOperatorId = ?1 and r.flowType = 'TODO' and r.flowStatus = 'RUNNING' and r.postponedCount > 0 "); |
| 110 | + sqlBuilder.addParam(user.getId()); |
| 111 | + if(StringUtils.hasText(lastId)){ |
| 112 | + sqlBuilder.append(" and r.id < ?",Long.parseLong(lastId)); |
| 113 | + } |
| 114 | + sqlBuilder.appendSql(" order by r.id desc "); |
| 115 | + PageRequest pageRequest = PageRequest.of(0, searchRequest.getPageSize()); |
| 116 | + return flowRecordQuery.dynamicPageQuery(sqlBuilder,pageRequest); |
| 117 | + } |
| 118 | +} |
0 commit comments