package com.changhong.epc.tenant.service.report.impl; import com.changhong.epc.bean.admin.InitCompany; import com.changhong.epc.bean.tenant.cost.CostFromMapping; import com.changhong.epc.constter.base.Context; import com.changhong.epc.rely.api.bean.report.WidgetEntity; import com.changhong.epc.rely.api.service.ReportApiService; import com.changhong.epc.tenant.mapper.tenant.cost.CostFromMappingMapper; import com.changhong.epc.tenant.mapper.tenant.system.TenantInfoMapper; import com.changhong.epc.tenant.service.report.ReportService; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.stream.Stream; @Service public class ReportServiceImpl implements ReportService { public static final String APPLY_SQL = "" + "SELECT\n" + "\t`a`.`DATAROWNUM` AS `主键id`,\n" + "\t`a`.`TENANTID` AS `租户id`,\n" + "\t`a`.`number_apply` AS `申请单号`,\n" + "\t`a`.`C_Type` AS `业务类型`,\n" + "\t`a`.`voucherCode` AS `凭证号`,\n" + "\t`a`.`Remarks` AS `申请说明`,\n" + "\t`cost`.`node_name` AS `业务类型名称`,\n" + "\t`a`.`currency` AS `币种`,\n" + "\t`a`.`budgetStartDate` AS `申请日期`,\n" + "\t`a`.`reimburseMan` AS `申请人`,\n" + "\t`p`.`员工姓名` AS `申请人名称`,\n" + "\t`a`.`apply_department` AS `申请部门`,\n" + "\t`org`.`name` AS `申请部门名称`,\n" + "\t`a`.`accountFlag` AS `是否借款`,\n" + "\t`a`.`paymentMethod` AS `支付方式`,\n" + "\t`a`.`loanAmount` AS `借款金额`,\n" + "\t 0 AS `已还金额`,\n" + "\t`a`.`toWriteBudget` AS `预算id`,\n" + "\t`a`.`budget_itemBalance` AS `预算金额`,\n" + "\t`a`.`applyUseMoney` AS `申请金额`,\n" + "\t`a`.`processState` AS `流程状态`,\n" + "\t`a`.`processLog` AS `流程执行记录`,\n" + "\t`a`.`processTask` AS `流程待办`,\n" + "\t`a`.`CREATEDATETIME` AS `创建时间`,\n" + "\t`a`.`CREATEUSER` AS `创建人`,\n" + "\t'{formId}' AS `表单ID` \n" + "FROM\n" + "\t`formdata_{tenantId}_{formId}` `a`\n" + "\tLEFT JOIN `epc_cost_type_{tenantId}_{companyId}` `cost` ON ( ( `a`.`C_Type` = `cost`.`id` ) )\n" + "\tLEFT JOIN `epc_tenant_org_{tenantId}_{companyId}` `org` ON ( ( `a`.`apply_department` = `org`.`code` ) )\n" + "\tLEFT JOIN `persion_{tenantId}_{companyId}` `p` ON ( ( `a`.`reimburseMan` = `p`.`员工编号` ) ) \n" + "WHERE\n" + "\t( `a`.`DELETEFLG` = 0 )"; public static final String EXPEND_SQL = "" + "SELECT\n" + "\t`e`.`number_expend` AS `报销单号`,\n" + "\t`e`.`reimburseMan` AS `报销人`,\n" + "\t`p`.`员工姓名` AS `报销人名称`,\n" + "\t`e`.`budgetStartDate` AS `报销时间`,\n" + "\t`e`.`budget_project` AS `项目`,\n" + "\t`j`.`项目名称` AS `项目名称`,\n" + "\t'' AS `复合状态`,\n" + "\t'' AS `凭证号`,\n" + "\t'' AS `交易号`,\n" + "\t`e`.`selectContracts` AS `申请单id`,\n" + "\t0 AS `申请金额`,\n" + "\t`e`.`expend_department` AS `报销部门`,\n" + "\t`org`.`name` AS `报销部门名称`,\n" + "\t`e`.`currency` AS `币种`,\n" + "\t`e`.`C_Type` AS `业务类型`,\n" + "\t`cost`.`node_name` AS `业务类型名称`,\n" + "\t`e`.`expendUseMoney` AS `报销金额`,\n" + "\t`e`.`processState` AS `流程状态`,\n" + "\t`e`.`processLog` AS `流程执行记录`,\n" + "\t`e`.`processTask` AS `流程待办`,\n" + "\t`e`.`CREATEDATETIME` AS `创建时间`,\n" + "\t`e`.`CREATEUSER` AS `创建人`,\n" + "\t`e`.`orderStatus` AS `订单状态` \n" + "FROM\n" + "\t`formdata_{tenantId}_{formId}` `e`\n" + "\tLEFT JOIN `epc_cost_type_{tenantId}_{companyId}` `cost` ON ( ( `e`.`C_Type` = `cost`.`id` ) )\n" + "\tLEFT JOIN `epc_tenant_org_{tenantId}_{companyId}` `org` ON ( ( `e`.`expend_department` = `org`.`code` ) )\n" + "\tLEFT JOIN `persion_{tenantId}_{companyId}` `p` ON ( ( `e`.`reimburseMan` = `p`.`员工编号` ) )\n" + "\tLEFT JOIN `project_{tenantId}_{companyId}` `j` ON ( ( `e`.`budget_project` = `j`.`项目编号` ) ) \n" + "WHERE\n" + "\t( ( `e`.`DELETEFLG` = 0 ) AND ( `cost`.`node_name` IS NOT NULL ) )"; @Resource private TenantInfoMapper tenantInfoMapper; @Resource private CostFromMappingMapper costFromMappingMapper; @Resource private ReportApiService reportApiService; @Override public Boolean initReport(InitCompany initCompany) { // 查询主数据创建基础视图 - 人员、项目 tenantInfoMapper.createView(); // 查询表单创建业务视图 - 申请单、报销单 initSQAndBX(); // 引用报表 initJoinReport(initCompany); // 添加系统配置表数据 return true; } protected void initSQAndBX(){ CostFromMapping costFromMapping = new CostFromMapping(); costFromMapping.setDataStart(0); List costFromMappings = costFromMappingMapper.select(costFromMapping); initSQ(costFromMappings.stream().filter(cf->Objects.equals(cf.getType(), "SQ")).map(CostFromMapping::getFId)); initBX(costFromMappings.stream().filter(cf->Objects.equals(cf.getType(), "BX")).map(CostFromMapping::getFId)); } protected void initSQ(Stream fromIds){ String viewSql = " CREATE VIEW `apply_"+Context.getTenantIdStr()+"_"+Context.getCompanyIdStr()+"` AS \n "; viewSql += String.join("\n UNION ALL \n" , fromIds.map(fromId-> APPLY_SQL.replace("{tenantId}", Context.getTenantIdStr()) .replace("{companyId}", Context.getCompanyIdStr()) .replace("{formId}", fromId)) .toArray(String[]::new)); tenantInfoMapper.runSql(viewSql); } protected void initBX(Stream fromIds){ String viewSql = " CREATE VIEW `expend_"+Context.getTenantIdStr()+"_"+Context.getCompanyIdStr()+"` AS \n "; viewSql += String.join("\n UNION ALL \n" , fromIds.map(fromId-> EXPEND_SQL.replace("{tenantId}", Context.getTenantIdStr()) .replace("{companyId}", Context.getCompanyIdStr()) .replace("{formId}", fromId)) .toArray(String[]::new)); tenantInfoMapper.runSql(viewSql); } protected void initJoinReport(InitCompany initCompany){ Optional.ofNullable(initCompany.getReportRefModelIds()) .orElse(Collections.emptyList()) .stream() .map(reportApiService::refReport) .map(this::toUrl) .forEach(System.out::println); } protected String toUrl(WidgetEntity widgetEntity) { // TODO return ""; } }