Commit 5e1e1eb1 authored by wangyangyang's avatar wangyangyang

借阅单:借阅单统计

parent fdcfb444
package com.inet.dcs.document.common.otherVo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
/**
* @author wyy
* @date 2024/11/4 13:35
* @describe 统计结果Vo
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ExtBorrowingFormCountVo {
/**
* 借阅次数=借阅单个数
* 借阅中文件数=借阅单状态为借阅中的数量
* 已归还文件数=借阅单状态为已归还的数量
* 审批中=借阅单状态为借阅审批中的数量
*/
/**
* 文件名称
*/
private String fileName;
/**
* 文件类型
*/
private String fileSubtypeName;
/**
* 项目
*/
private String contextName;
/**
* 借阅部门
*/
private String dxBorrowingOrgName;
/**
* 借阅状态
*/
private String state;
/**
* 借阅人
*/
private String dxBorrowingUserInfoName;
/**
* 借阅时间 沟通后数据只保留到年份即可
*/
private String borrowingDate;
/**
* 借阅次数(后面三个属性加起来)
*/
private String borrowCount;
/**
* 借阅中 文件数
*/
private String borrowFileCount;
/**
* 归还 文件数
*/
private String returnFileCount;
/**
* 审批中 文件数
*/
private String processFileCount;
}
package com.inet.dcs.document.common.otherVo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.swing.text.StyledEditorKit;
import java.time.LocalDateTime;
/**
* @author wyy
* @date 2024/11/4 11:30
* @describe 借阅单统计 沟通后确认先分组,后查询 查询入参
*/
@AllArgsConstructor
@NoArgsConstructor
@Data
public class ExtBorrowingFormSearchVo {
//分组条件
/**
* 是否项目查询 true为是 后面条件一样
*/
private Boolean dxContextIdIs;
/**
* 借阅部门
*/
private Boolean dxBorrowingOrgIs;
/**
* 借阅状态 --- 废弃无用 本身就需要状态分组
*/
private Boolean stateIs;
/**
* 借阅人
*/
private Boolean dxBorrowingUserInfoIdIs;
/**
* 年度
*/
private Boolean borrowingDateIs;
//查询条件
/**
* 项目 名称
*/
private String dxContextId;
/**
* 借阅部门 名称
*/
private String dxBorrowingOrgId;
/**
* 借阅状态
*/
private String state;
/**
* 借阅人 名称
*/
private String dxBorrowingUserInfoId;
/**
* 文件名称
*/
private String fileName;
/**
* 文件类型 直接传 中文名称即可
*/
private String fileSubtypeName;
/**
* 借阅 开始时间 例如2023
*/
private String borrowingDateStart;
/**
* 借阅 结束时间 例如2024
*/
private String borrowingDateEnd;
}
package com.inet.dcs.document.core.constants;
/**
* @author wyy
* @date 2024/11/5 10:09
* @describe
*/
public class SqlConstants {
//替换 条件过滤参数
public static String REPLACE_WHERE_ATTR = "REPLACE_WHERE_ATTR";
//替换 要分组的参数
public static String REPLACE_GROUP_ATTR = "REPLACE_GROUP_ATTR";
/**
* 构建新借阅单 数据表
*/
public static String SQL_EXT_NEW_BORROW_DATA = "SELECT\n" +
"\t\tcp.NAME AS contextName ,\n" +
"\t\torg.NAME AS dxBorrowingOrgName ,\n" +
"\t\tbf.STATE AS STATE ,\n" +
"\t\tui.NAME AS dxBorrowingUserInfoName,\n" +
//"\t\tCONCAT(YEAR(bf.BORROWING_DATE),'-01-01 00:00:00') AS borrowingDate ,\n" +
"\t\tYEAR(bf.BORROWING_DATE) AS borrowingDate ,\n" +
"\t\td.NAME AS fileName ,\n" +
"\t\tmd.DISPLAY_NAME AS fileSubtypeName\n" +
"FROM\n" +
"\t\tEXT_BORROWING_FORM bf\n" +
"JOIN EXT_BORROWING_DOC_LINK bdl ON\n" +
"\t\tbdl.TARGET_ID = bf.ID\n" +
"JOIN DX_DOCUMENT d ON\n" +
"\t\td.ID = bdl.SOURCE_ID\n" +
"JOIN DEX_BASIC.DX_MODEL_DEFINITION md ON\n" +
"\t\tmd.NAME = d.SUB_TYPE_NAME\n" +
"JOIN DEX_BASIC.DX_CONTEXT_PROJECT cp ON\n" +
"\t\tcp.ID = bf.DX_CONTEXT_ID\n" +
"JOIN DEX_BASIC.DX_ORGANIZATION org ON\n" +
"\t\torg.ID = bf.DX_BORROWING_ORG_ID\n" +
"JOIN DEX_BASIC.DX_USER_INFO ui ON\n" +
"\t\tui.ID = bf.DX_BORROWING_USER_INFO_ID";
//借阅次数
public static String SQL_STATE = " COUNT(state) AS borrowCount,";
//借阅中
public static String SQL_BORROW_STATE = " SUM(CASE WHEN state = 'borrowing' THEN 1 ELSE 0 END) AS borrowFileCount,";
//已归还
public static String SQL_RETURN_STATE = " SUM(CASE WHEN state = 'returned' THEN 1 ELSE 0 END) AS returnFileCount,";
//借阅审批中
public static String SQL_PROCESS_STATE = " SUM(CASE WHEN state = 'borrowingApproval' THEN 1 ELSE 0 END) AS processFileCount";
//分组
//状态:默认分组
public static String SQL_GROUP_STATE = " state ";
//项目
public static String SQL_GROUP_CONTEXT = ", contextName ";
//借阅部门
public static String SQL_GROUP_ORG = ", dxBorrowingOrgName ";
//借阅人
public static String SQL_GROUP_USER = ", dxBorrowingUserInfoName ";
//年度
public static String SQL_GROUP_DATE = ", borrowingDate ";
//最终sql 其中两个属性需要替换
// REPLACE_GROUP_ATTR 分组属性
// REPLACE_WHERE_ATTR 过滤条件
public static String SQL_SEARCH = "SELECT " + SQL_GROUP_STATE
+ REPLACE_GROUP_ATTR
//四种状态数量
+ SQL_STATE + SQL_BORROW_STATE + SQL_RETURN_STATE + SQL_PROCESS_STATE
+ " FROM ("
+ SQL_EXT_NEW_BORROW_DATA + " ) AS EXT_NEW_BORROW GROUP BY "
//分组
+ SQL_GROUP_STATE
+ REPLACE_GROUP_ATTR;
//条件
//+ " HAVING "
//+ REPLACE_WHERE_ATTR;
}
package com.inet.dcs.document.core.controller;
import com.inet.dcs.document.common.otherVo.ExtBorrowingFormSearchVo;
import org.springframework.stereotype.Controller;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
......@@ -36,6 +37,13 @@ public class ExtBorrowingFormController<V extends ExtBorrowingFormVO, S extends
return ApiResult.ok(extBorrowingFormService.startWorkflow(id), "流程启动成功");
}
@ApiOperation("借阅统计")
@PostMapping({"borrow/count"})
public ApiResult borrowCount(@RequestBody ExtBorrowingFormSearchVo searchVo) {
return ApiResult.ok(extBorrowingFormService.borrowCount(searchVo));
}
}
package com.inet.dcs.document.core.service;
import com.inet.dcs.document.common.otherVo.ExtBorrowingFormCountVo;
import com.inet.dcs.document.common.otherVo.ExtBorrowingFormSearchVo;
import com.inet.dcs.document.common.vo.ExtBorrowingFormVO;
import com.inet.dcs.document.core.service.shadow.ExtBorrowingFormServiceShadow;
import java.util.List;
/**
* @description: ExtBorrowingForm-service
* @author: dexadmin
* @version: V
* @date: 2024-10-29 10:30:50
**/
* @description: ExtBorrowingForm-service
* @author: dexadmin
* @version: V
* @date: 2024-10-29 10:30:50
**/
public interface ExtBorrowingFormService<V extends ExtBorrowingFormVO> extends ExtBorrowingFormServiceShadow<V> {
ExtBorrowingFormVO startWorkflow(Long id);
/**
* 借阅统计
*
* @param searchVo
* @return
*/
List<ExtBorrowingFormCountVo> borrowCount(ExtBorrowingFormSearchVo searchVo);
}
package com.inet.dcs.document.core.service.impl;
import cn.hutool.core.util.StrUtil;
import com.inet.dcs.document.common.otherVo.ExtBorrowingFormCountVo;
import com.inet.dcs.document.common.otherVo.ExtBorrowingFormSearchVo;
import com.inet.dcs.document.common.vo.ExtTransferDocumentVO;
import com.inet.dcs.document.core.constants.Constants;
import com.inet.dcs.document.core.constants.SqlConstants;
import com.yonde.dex.basedata.exception.DxBusinessException;
import com.yonde.dex.wfc.feign.api.WfcProcessFeign;
import org.hibernate.query.internal.NativeQueryImpl;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Service;
import io.swagger.annotations.ApiOperation;
import java.util.Collections;
import java.util.List;
import com.inet.dcs.document.common.vo.ExtBorrowingFormVO;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -15,17 +24,19 @@ import org.springframework.web.bind.annotation.*;
import com.inet.dcs.document.core.service.ExtBorrowingFormService;
import com.inet.dcs.document.core.repository.ExtBorrowingFormRepository;
import com.inet.dcs.document.entity.po.ExtBorrowingForm;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
/**
* @description: ExtBorrowingForm-ServiceImpl
* @author: dexadmin
* @version: V
* @date: 2024-10-29 10:30:50
**/
* @description: ExtBorrowingForm-ServiceImpl
* @author: dexadmin
* @version: V
* @date: 2024-10-29 10:30:50
**/
@Slf4j
@Service(ExtBorrowingFormServiceImpl.BEAN_NAME)
public class ExtBorrowingFormServiceImpl<V extends ExtBorrowingFormVO> implements ExtBorrowingFormService<V>{
public class ExtBorrowingFormServiceImpl<V extends ExtBorrowingFormVO> implements ExtBorrowingFormService<V> {
public static final String BEAN_NAME = "extBorrowingFormServiceImpl";
......@@ -38,6 +49,9 @@ public class ExtBorrowingFormServiceImpl<V extends ExtBorrowingFormVO> implement
private static final String START_PROCESS_BY_KEY = "ExtBorrowingFormWF";
@Resource
private EntityManager entityManager;
@Override
public ExtBorrowingFormVO startWorkflow(Long id) {
ExtBorrowingFormVO extBorrowingFormVO = this.get(id);
......@@ -50,6 +64,94 @@ public class ExtBorrowingFormServiceImpl<V extends ExtBorrowingFormVO> implement
wfcProcessFeign.startProcessByKey(START_PROCESS_BY_KEY, extBorrowingFormVO);
return this.changeStatus(extBorrowingFormVO.getId(), Constants.SUBMIT_REVIEWING, true);
}
@Override
public List<ExtBorrowingFormCountVo> borrowCount(ExtBorrowingFormSearchVo searchVo) {
//TODO 沟通后 先分组,在统计
//固定分组有 5个属性 年度为年份
//状态分组自带
String replace_group_attr = " ";
//借阅部门
if (searchVo.getDxBorrowingOrgIs()) {
replace_group_attr = replace_group_attr + SqlConstants.SQL_GROUP_ORG;
}
//借阅人
if (searchVo.getDxBorrowingUserInfoIdIs()) {
replace_group_attr = replace_group_attr + SqlConstants.SQL_GROUP_USER;
}
//项目
if (searchVo.getDxContextIdIs()) {
replace_group_attr = replace_group_attr + SqlConstants.SQL_GROUP_CONTEXT;
}
//年度
if (searchVo.getBorrowingDateIs()) {
replace_group_attr = replace_group_attr + SqlConstants.SQL_GROUP_DATE;
}
//过滤条件
String replace_where_attr = "";
Boolean isAddAnd = false;
//部门 名称
if (!StrUtil.isEmpty(searchVo.getDxBorrowingOrgId())) {
replace_where_attr = replace_where_attr + " dxBorrowingOrgName = '" + searchVo.getDxBorrowingOrgId() + "'";
isAddAnd = true;
}
//借阅人
if (!StrUtil.isEmpty(searchVo.getDxBorrowingUserInfoId())) {
if (isAddAnd) {
replace_where_attr = replace_where_attr + " and ";
}
isAddAnd = true;
replace_where_attr = replace_where_attr + " dxBorrowingUserInfoName = '" + searchVo.getDxBorrowingUserInfoId() + "'";
}
//项目
if (!StrUtil.isEmpty(searchVo.getDxContextId())) {
if (isAddAnd) {
replace_where_attr = replace_where_attr + " and ";
}
isAddAnd = true;
replace_where_attr = replace_where_attr + " contextName = '" + searchVo.getDxContextId() + "'";
}
//借阅时间 只保留年份
if (!(StrUtil.isEmpty(searchVo.getBorrowingDateStart()) && StrUtil.isEmpty(searchVo.getBorrowingDateEnd()))) {
if (isAddAnd) {
replace_where_attr = replace_where_attr + " and ";
}
isAddAnd = true;
replace_where_attr = replace_where_attr + " borrowingDate <= '" + searchVo.getBorrowingDateEnd() + "'";
replace_where_attr = replace_where_attr + " borrowingDate >= '" + searchVo.getBorrowingDateStart() + "'";
}
//文件名称
if (!StrUtil.isEmpty(searchVo.getFileName())) {
if (isAddAnd) {
replace_where_attr = replace_where_attr + " and ";
}
isAddAnd = true;
replace_where_attr = replace_where_attr + " fileName = '" + searchVo.getFileName() + "'";
}
//文件类型
if (!StrUtil.isEmpty(searchVo.getDxContextId())) {
if (isAddAnd) {
replace_where_attr = replace_where_attr + " and ";
}
isAddAnd = true;
replace_where_attr = replace_where_attr + " fileSubtypeName = '" + searchVo.getFileSubtypeName() + "'";
}
String finalSql = SqlConstants.SQL_SEARCH.replaceAll(SqlConstants.REPLACE_GROUP_ATTR, replace_group_attr);
//.replace(SqlConstants.REPLACE_WHERE_ATTR, replace_where_attr);
if (!StrUtil.isEmpty(replace_where_attr)){
finalSql = finalSql + " HAVING " + replace_where_attr;
}
NativeQueryImpl<?> query = entityManager.createNativeQuery(finalSql).unwrap(NativeQueryImpl.class);
query.setResultTransformer(Transformers.aliasToBean(ExtBorrowingFormCountVo.class));
List<ExtBorrowingFormCountVo> result = (List<ExtBorrowingFormCountVo>) query.getResultList();
return result;
}
}
---- 最终sql
SELECT
contextName,
borrowingDate,
dxBorrowingOrgName,
---借阅次数
COUNT(state) AS borrowCount,
---借阅中
SUM(CASE WHEN state = 'borrowing' THEN 1 ELSE 0 END) AS borrowFileCount,
---已归还
SUM(CASE WHEN state = 'returned' THEN 1 ELSE 0 END) AS returnFileCount,
---借阅审批中
SUM(CASE WHEN state = 'borrowingApproval' THEN 1 ELSE 0 END) AS processFileCount
FROM
(
SELECT
cp.NAME AS contextName ,
org.NAME AS dxBorrowingOrgName ,
bf.STATE AS STATE ,
ui.NAME AS dxBorrowingUserInfoName,
YEAR(bf.BORROWING_DATE) AS borrowingDate ,
d.NAME AS fileName ,
md.DISPLAY_NAME AS fileSubtypeName
FROM
EXT_BORROWING_FORM bf
JOIN EXT_BORROWING_DOC_LINK bdl ON
bdl.TARGET_ID = bf.ID
JOIN DX_DOCUMENT d ON
d.ID = bdl.SOURCE_ID
JOIN DEX_BASIC.DX_MODEL_DEFINITION md ON
md.NAME = d.SUB_TYPE_NAME
JOIN DEX_BASIC.DX_CONTEXT_PROJECT cp ON
cp.ID = bf.DX_CONTEXT_ID
JOIN DEX_BASIC.DX_ORGANIZATION org ON
org.ID = bf.DX_BORROWING_ORG_ID
JOIN DEX_BASIC.DX_USER_INFO ui ON
ui.ID = bf.DX_BORROWING_USER_INFO_ID) AS EXT_NEW_BORROW
GROUP BY
contextName,
borrowingDate,
dxBorrowingOrgName
HAVING
contextName = 'aa'
AND dxBorrowingOrgName = 'bb'
--- 中间表sql
SELECT
cp.NAME AS contextName ,
org.NAME AS dxBorrowingOrgName ,
bf.STATE AS STATE ,
ui.NAME AS dxBorrowingUserInfoName,
bf.BORROWING_DATE AS borrowingDate ,
d.NAME AS fileName ,
md.DISPLAY_NAME AS fileSubtypeName
FROM
EXT_BORROWING_FORM bf
JOIN EXT_BORROWING_DOC_LINK bdl ON
bdl.TARGET_ID = bf.ID
JOIN DX_DOCUMENT d ON
d.ID = bdl.SOURCE_ID
JOIN DEX_BASIC.DX_MODEL_DEFINITION md ON
md.NAME = d.SUB_TYPE_NAME
JOIN DEX_BASIC.DX_CONTEXT_PROJECT cp ON
cp.ID = bf.DX_CONTEXT_ID
JOIN DEX_BASIC.DX_ORGANIZATION org ON
org.ID = bf.DX_BORROWING_ORG_ID
JOIN DEX_BASIC.DX_USER_INFO ui ON
ui.ID = bf.DX_BORROWING_USER_INFO_ID
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment