添加原煤报表

This commit is contained in:
2024-09-03 21:30:33 +08:00
parent c13f89abbf
commit c5d8194a75
12 changed files with 427 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ import cn.lihongjie.coal.annotation.SysLog;
import cn.lihongjie.coal.base.controller.BaseController;
import cn.lihongjie.coal.base.dto.CommonQuery;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.coalWashingDailyAnalysis.dto.CoalWashingDailyAnalysisCoalReportRequest;
import cn.lihongjie.coal.coalWashingDailyAnalysis.dto.CoalWashingDailyAnalysisDto;
import cn.lihongjie.coal.coalWashingDailyAnalysis.dto.CreateCoalWashingDailyAnalysisDto;
import cn.lihongjie.coal.coalWashingDailyAnalysis.dto.UpdateCoalWashingDailyAnalysisDto;
@@ -80,4 +81,11 @@ public class CoalWashingDailyAnalysisController extends BaseController {
public CoalWashingDailyAnalysisDto getById(@RequestBody IdRequest dto) {
return this.service.getById(dto.getId());
}
@PostMapping("/report")
public Object report(@RequestBody CoalWashingDailyAnalysisCoalReportRequest request) {
return this.service.report(request);
}
}

View File

@@ -0,0 +1,50 @@
package cn.lihongjie.coal.coalWashingDailyAnalysis.dto;
import cn.lihongjie.coal.base.dto.CommonQuery;
import lombok.Data;
import org.hibernate.annotations.Comment;
import java.time.LocalDate;
import java.util.*;
@Data
public class CoalWashingDailyAnalysisCoalReportRequest extends CommonQuery {
@Comment(
"""
时间维度
year
month
""")
private String timeDimension;
@Comment(
"""
统计字段
""")
private List<String> reportFields;
private List<FieldInfo> fieldInfos;
private LocalDate startTime;
private LocalDate endTime;
/** 过滤条件 */
private List<String> productIds;
private List<String> coalInfoIds;
@Data
public static class FieldInfo {
private String fieldName;
@Comment("sum avg count max min")
private String function;
}
}

View File

@@ -3,6 +3,7 @@ package cn.lihongjie.coal.coalWashingDailyAnalysis.service;
import cn.lihongjie.coal.base.dto.CommonQuery;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.base.service.BaseService;
import cn.lihongjie.coal.coalWashingDailyAnalysis.dto.CoalWashingDailyAnalysisCoalReportRequest;
import cn.lihongjie.coal.coalWashingDailyAnalysis.dto.CoalWashingDailyAnalysisDto;
import cn.lihongjie.coal.coalWashingDailyAnalysis.dto.CreateCoalWashingDailyAnalysisDto;
import cn.lihongjie.coal.coalWashingDailyAnalysis.dto.UpdateCoalWashingDailyAnalysisDto;
@@ -11,21 +12,34 @@ import cn.lihongjie.coal.coalWashingDailyAnalysis.mapper.CoalWashingDailyAnalysi
import cn.lihongjie.coal.coalWashingDailyAnalysis.mapper.RoundMapper;
import cn.lihongjie.coal.coalWashingDailyAnalysis.repository.CoalWashingDailyAnalysisRepository;
import cn.lihongjie.coal.common.Ctx;
import cn.lihongjie.coal.common.FreeMakerUtils;
import cn.lihongjie.coal.common.JpaUtils;
import cn.lihongjie.coal.exception.BizException;
import com.google.common.base.CaseFormat;
import jakarta.annotation.PostConstruct;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.Tuple;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
@Slf4j
@Transactional
@@ -131,4 +145,112 @@ public class CoalWashingDailyAnalysisService
public void unarchive(IdRequest dto) {
this.repository.unArchive(dto);
}
@PersistenceContext EntityManager em;
public Object report(CoalWashingDailyAnalysisCoalReportRequest request) {
String sql =
FreeMakerUtils.render(
"""
select to_char(date_trunc('${timeDimension}', a.end_time), 'YYYY-MM-DD') as time
<#list reportFields as field>
<#if field?is_first> ,</#if>
<#if field == 'coalInfoId'>, d.id as coal_info_id, d.code as coal_info_code, d.name as coal_info_name </#if>
<#if field == 'productId'>, p.id as product_id, p.code as product_code, p.name as product_name </#if>
<#if field?has_next>,</#if>
</#list>
, sum(d.amount) as amount
from t_coal_washing_daily_analysis_ym_details d
inner join t_coal_washing_daily_analysis a on a.id = d.coal_washing_daily_analysis_id
inner join t_product p on p.id = a.product_id
where 1=1
<#if startTime??> and a.end_time >= :startTime </#if>
<#if endTime??> and a.end_time <= :endTime </#if>
<#if productIds??> and a.product_id in (:productIds) </#if>
<#if coalInfoIds??> and d.id in (:coalInfoIds) </#if>
group by 1, date_trunc('${timeDimension}', a.end_time)
<#list reportFields as field>
<#if field?is_first> ,</#if>
<#if field == 'coalInfoId'>, d.id </#if>
<#if field == 'productId'>, p.id </#if>
<#if field?has_next>,</#if>
</#list>
""",
request);
var countSql = "select count(1) from (" + sql + ") as t";
var selectSql =
"select * from ("
+ sql
+ ") as t limit "
+ request.getPageSize()
+ " offset "
+ request.getPageNo() * request.getPageSize();
Integer count = JpaUtils.execNativeQuery(em, countSql, request, Integer.class).get(0);
List<Tuple> data = JpaUtils.execNativeQuery(em, selectSql, request, Tuple.class);
var resultList = JpaUtils.convertTuplesToMap(data);
var ans =
resultList.stream()
.map(
x ->
((Map) x)
.entrySet().stream()
.collect(
Collectors.toMap(
(Map.Entry e) ->
CaseFormat
.LOWER_UNDERSCORE
.to(
CaseFormat
.LOWER_CAMEL,
e.getKey()
.toString()),
e ->
ObjectUtils
.defaultIfNull(
e
.getValue(),
""))))
.toList();
return new PageImpl<>(ans, PageRequest.of(0, request.getPageSize()), count);
}
}

View File

@@ -0,0 +1,56 @@
package cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.controller;
import cn.lihongjie.coal.annotation.OrgScope;
import cn.lihongjie.coal.annotation.SysLog;
import cn.lihongjie.coal.base.dto.CommonQuery;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto.CoalWashingDailyAnalysisCoalReportDto;
import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto.CreateCoalWashingDailyAnalysisCoalReportDto;
import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto.UpdateCoalWashingDailyAnalysisCoalReportDto;
import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.service.CoalWashingDailyAnalysisCoalReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/coalWashingDailyAnalysisCoalReport")
@SysLog(module = "")
@Slf4j
@OrgScope
public class CoalWashingDailyAnalysisCoalReportController {
@Autowired private CoalWashingDailyAnalysisCoalReportService service;
@PostMapping("/create")
public CoalWashingDailyAnalysisCoalReportDto create(
@RequestBody CreateCoalWashingDailyAnalysisCoalReportDto request) {
return this.service.create(request);
}
@PostMapping("/update")
public CoalWashingDailyAnalysisCoalReportDto update(
@RequestBody UpdateCoalWashingDailyAnalysisCoalReportDto request) {
return this.service.update(request);
}
@PostMapping("/delete")
public Object delete(@RequestBody IdRequest request) {
this.service.delete(request);
return true;
}
@PostMapping("/getById")
public CoalWashingDailyAnalysisCoalReportDto getById(@RequestBody IdRequest request) {
return this.service.getById(request.getId());
}
@PostMapping("/list")
public Page<CoalWashingDailyAnalysisCoalReportDto> list(@RequestBody CommonQuery request) {
return this.service.list(request);
}
}

View File

@@ -0,0 +1,11 @@
package cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
@Data
public class CoalWashingDailyAnalysisCoalReportDto extends OrgCommonDto {
private String jsonConfig;
}

View File

@@ -0,0 +1,11 @@
package cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
@Data
public class CreateCoalWashingDailyAnalysisCoalReportDto extends OrgCommonDto {
private String jsonConfig;
}

View File

@@ -0,0 +1,12 @@
package cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
@Data
public class UpdateCoalWashingDailyAnalysisCoalReportDto extends OrgCommonDto {
private String jsonConfig;
}

View File

@@ -0,0 +1,16 @@
package cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.entity;
import cn.lihongjie.coal.base.entity.OrgCommonEntity;
import jakarta.persistence.Entity;
import lombok.Data;
@Data
@Entity
public class CoalWashingDailyAnalysisCoalReportEntity extends OrgCommonEntity {
private String jsonConfig;
}

View File

@@ -0,0 +1,23 @@
package cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.mapper;
import cn.lihongjie.coal.base.mapper.BaseMapper;
import cn.lihongjie.coal.base.mapper.CommonEntityMapper;
import cn.lihongjie.coal.base.mapper.CommonMapper;
import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto.CoalWashingDailyAnalysisCoalReportDto;
import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto.CreateCoalWashingDailyAnalysisCoalReportDto;
import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto.UpdateCoalWashingDailyAnalysisCoalReportDto;
import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.entity.CoalWashingDailyAnalysisCoalReportEntity;
import org.mapstruct.Mapper;
import org.mapstruct.control.DeepClone;
@Mapper(
componentModel = org.mapstruct.MappingConstants.ComponentModel.SPRING,
uses = {CommonMapper.class, CommonEntityMapper.class},
mappingControl = DeepClone.class)
public interface CoalWashingDailyAnalysisCoalReportMapper
extends BaseMapper<
CoalWashingDailyAnalysisCoalReportEntity,
CoalWashingDailyAnalysisCoalReportDto,
CreateCoalWashingDailyAnalysisCoalReportDto,
UpdateCoalWashingDailyAnalysisCoalReportDto> {}

View File

@@ -0,0 +1,16 @@
package cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.repository;
import cn.lihongjie.coal.base.dao.BaseRepository;
import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.entity.CoalWashingDailyAnalysisCoalReportEntity;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface CoalWashingDailyAnalysisCoalReportRepository
extends BaseRepository<CoalWashingDailyAnalysisCoalReportEntity> {
@Query("select false")
boolean isLinked(List<String> ids);
}

View File

@@ -0,0 +1,85 @@
package cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.service;
import cn.lihongjie.coal.base.dto.CommonQuery;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.base.service.BaseService;
import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto.CoalWashingDailyAnalysisCoalReportDto;
import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto.CreateCoalWashingDailyAnalysisCoalReportDto;
import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.dto.UpdateCoalWashingDailyAnalysisCoalReportDto;
import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.entity.CoalWashingDailyAnalysisCoalReportEntity;
import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.mapper.CoalWashingDailyAnalysisCoalReportMapper;
import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.repository.CoalWashingDailyAnalysisCoalReportRepository;
import cn.lihongjie.coal.dbFunctions.DbFunctionService;
import cn.lihongjie.coal.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Slf4j
@Transactional
public class CoalWashingDailyAnalysisCoalReportService
extends BaseService<
CoalWashingDailyAnalysisCoalReportEntity,
CoalWashingDailyAnalysisCoalReportRepository> {
@Autowired private CoalWashingDailyAnalysisCoalReportRepository repository;
@Autowired private CoalWashingDailyAnalysisCoalReportMapper mapper;
@Autowired private ConversionService conversionService;
@Autowired private DbFunctionService dbFunctionService;
public CoalWashingDailyAnalysisCoalReportDto create(
CreateCoalWashingDailyAnalysisCoalReportDto request) {
CoalWashingDailyAnalysisCoalReportEntity entity = mapper.toEntity(request);
this.repository.save(entity);
return getById(entity.getId());
}
public CoalWashingDailyAnalysisCoalReportDto update(
UpdateCoalWashingDailyAnalysisCoalReportDto request) {
CoalWashingDailyAnalysisCoalReportEntity entity = this.repository.get(request.getId());
this.mapper.updateEntity(entity, request);
this.repository.save(entity);
return getById(entity.getId());
}
public void delete(IdRequest request) {
boolean linked = this.repository.isLinked(request.getIds());
if (linked) {
throw new BizException("数据已被关联,无法删除");
}
this.repository.deleteAllById(request.getIds());
}
public CoalWashingDailyAnalysisCoalReportDto getById(String id) {
CoalWashingDailyAnalysisCoalReportEntity entity = repository.get(id);
return mapper.toDto(entity);
}
public Page<CoalWashingDailyAnalysisCoalReportDto> list(CommonQuery query) {
Page<CoalWashingDailyAnalysisCoalReportEntity> page =
repository.findAll(
query.specification(conversionService),
PageRequest.of(
query.getPageNo(),
query.getPageSize(),
Sort.by(query.getOrders())));
return page.map(this.mapper::toDto);
}
}

View File

@@ -0,0 +1,17 @@
package scripts.dict
import cn.lihongjie.coal.base.dto.CommonQuery
import cn.lihongjie.coal.coalWashingDailyAnalysisCoalReport.controller.CoalWashingDailyAnalysisCoalReportController
import org.springframework.context.ApplicationContext
ApplicationContext ioc = ioc
def controller = ioc.getBean(CoalWashingDailyAnalysisCoalReportController.class)
return controller.list(new CommonQuery())