提交 8d81b301 作者: Administrator

feat:人员统计查询导出功能

上级 c5315cc2
/* (C) 2022 YiRing, Inc. */
package com.yiring.app.domain.perstatistics;
import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;
import javax.persistence.*;
import lombok.*;
import lombok.experimental.FieldDefaults;
import lombok.experimental.FieldNameConstants;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.UpdateTimestamp;
import org.hibernate.snowflake.SnowflakeId;
/**
* @author tzl
* @version 1.0
* @description:
* @date 2022/5/15 9:15
*/
@Getter
@Setter
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
@FieldNameConstants
@FieldDefaults(level = AccessLevel.PRIVATE)
@Entity
@Table(name = "BS_PERSONNEL_STATISTICS")
@Comment("区域人员统计")
public class PersonnelStatistics implements Serializable {
@Serial
private static final long serialVersionUID = 1276138943241366605L;
@Id
@Comment("主键id")
@GeneratedValue(generator = SnowflakeId.GENERATOR)
@GenericGenerator(name = SnowflakeId.GENERATOR, strategy = SnowflakeId.Strategy.LONG)
Long id;
@Comment("区域")
String region;
@Comment("入场人数")
String admissionNumber;
@Comment("出场人数")
String attendance;
@Comment("创建时间")
@CreationTimestamp
LocalDateTime createTime;
@Comment("修改时间")
@UpdateTimestamp
LocalDateTime updateTime;
}
/* (C) 2022 YiRing, Inc. */
package com.yiring.app.domain.perstatistics;
import java.io.Serializable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
/**
* @author tzl
* @version 1.0
* @description:
* @date 2022/5/15 9:20
*/
@Repository
public interface PersonnelStatisticsRepository
extends JpaRepository<PersonnelStatistics, Serializable>, JpaSpecificationExecutor<PersonnelStatistics> {}
/* (C) 2022 YiRing, Inc. */
package com.yiring.app.excel.perstatistics;
import com.github.liaochong.myexcel.core.annotation.ExcelColumn;
import java.io.Serial;
import java.io.Serializable;
import lombok.*;
import lombok.experimental.FieldDefaults;
/**
* @author tzl
* @version 1.0
* @description:
* @date 2022/5/15 9:55
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class PersonnelStatisticsExportExcel implements Serializable {
@Serial
private static final long serialVersionUID = -6466089255235570264L;
@ExcelColumn(title = "区域", width = 10)
String region;
@ExcelColumn(title = "入场人数", width = 5)
String admissionNumber;
@ExcelColumn(title = "出场人数", width = 5)
String attendance;
}
/* (C) 2022 YiRing, Inc. */
package com.yiring.app.param.perstatistics;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serial;
import java.io.Serializable;
import lombok.*;
import lombok.experimental.FieldDefaults;
/**
* @author tzl
* @version 1.0
* @description:
* @date 2022/5/15 9:15
*/
@ApiModel("PersonnelStatisticsQueryParam")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class PersonnelStatisticsQueryParam implements Serializable {
@Serial
private static final long serialVersionUID = -638192778218413746L;
@ApiModelProperty(value = "区域", example = "化工区")
String region;
}
/* (C) 2022 YiRing, Inc. */
package com.yiring.app.service.perstatistics;
import com.yiring.app.param.perstatistics.PersonnelStatisticsQueryParam;
import com.yiring.app.vo.perstatistics.PersonnelStatisticsVo;
import com.yiring.common.core.Result;
import com.yiring.common.param.PageParam;
import com.yiring.common.vo.PageVo;
import javax.servlet.http.HttpServletResponse;
/**
* @author tzl
* @version 1.0
* @description:
* @date 2022/5/15 9:27
*/
public interface PersonnelStatisticsService {
/**
* 分页查询
* @author tzl
* @date 2022/5/15 9:41
* @param personnelStatisticsQueryParam PersonnelStatisticsQueryParam
* @param pageParam PageParam
* @return com.yiring.common.core.Result<com.yiring.common.vo.PageVo<com.yiring.app.vo.perstatistics.PersonnelStatisticsVo>>
*/
Result<PageVo<PersonnelStatisticsVo>> pagePerSta(
PersonnelStatisticsQueryParam personnelStatisticsQueryParam,
PageParam pageParam
);
/**
* 导出人员统计
* @author tzl
* @date 2022/5/15 9:56
* @param personnelStatisticsQueryParam PersonnelStatisticsQueryParam
* @param response HttpServletResponse
*/
void export(PersonnelStatisticsQueryParam personnelStatisticsQueryParam, HttpServletResponse response);
}
/* (C) 2022 YiRing, Inc. */
package com.yiring.app.service.perstatistics.impl;
import cn.hutool.core.util.StrUtil;
import com.github.liaochong.myexcel.core.DefaultStreamExcelBuilder;
import com.yiring.app.domain.perstatistics.PersonnelStatistics;
import com.yiring.app.domain.perstatistics.PersonnelStatisticsRepository;
import com.yiring.app.excel.perstatistics.PersonnelStatisticsExportExcel;
import com.yiring.app.param.perstatistics.PersonnelStatisticsQueryParam;
import com.yiring.app.service.perstatistics.PersonnelStatisticsService;
import com.yiring.app.vo.perstatistics.PersonnelStatisticsVo;
import com.yiring.common.core.Result;
import com.yiring.common.param.PageParam;
import com.yiring.common.vo.PageVo;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @author tzl
* @version 1.0
* @description:
* @date 2022/5/15 9:28
*/
@Slf4j
@Transactional(rollbackFor = RuntimeException.class)
@Service
public class PersonnelStatisticsServiceImpl implements PersonnelStatisticsService {
@Resource
PersonnelStatisticsRepository personnelStatisticsRepository;
@Override
public Result<PageVo<PersonnelStatisticsVo>> pagePerSta(
PersonnelStatisticsQueryParam personnelStatisticsQueryParam,
PageParam pageParam
) {
//分页
Pageable pageable = PageRequest.of(pageParam.getPageNo() - 1, pageParam.getPageSize());
Page<PersonnelStatistics> all = personnelStatisticsRepository.findAll(
condition(personnelStatisticsQueryParam),
pageable
);
List<PersonnelStatisticsVo> data = all
.get()
.map(personnelStatistics -> {
PersonnelStatisticsVo vo = new PersonnelStatisticsVo();
BeanUtils.copyProperties(personnelStatistics, vo);
return vo;
})
.collect(Collectors.toList());
PageVo<PersonnelStatisticsVo> resultVo = PageVo.build(data, all.getTotalElements());
return Result.ok(resultVo);
}
@Override
public void export(PersonnelStatisticsQueryParam personnelStatisticsQueryParam, HttpServletResponse response) {
List<PersonnelStatistics> all = personnelStatisticsRepository.findAll(condition(personnelStatisticsQueryParam));
List<PersonnelStatisticsExportExcel> visitorExportExcels = all
.stream()
.map(personnelStatistics -> {
PersonnelStatisticsExportExcel personnelStatisticsExportExcel = new PersonnelStatisticsExportExcel();
BeanUtils.copyProperties(personnelStatistics, personnelStatisticsExportExcel);
return personnelStatisticsExportExcel;
})
.toList();
try (
DefaultStreamExcelBuilder<PersonnelStatisticsExportExcel> defaultStreamExcelBuilder = DefaultStreamExcelBuilder
.of(PersonnelStatisticsExportExcel.class)
.threadPool(Executors.newFixedThreadPool(2))
.rowHeight(14)
.titleRowHeight(14)
.widths(8)
.style(
"cell->vertical-align:center;text-align:center",
"title->vertical-align:center;text-align:center;font-weight:bold;font-family:等线"
)
.start()
) {
defaultStreamExcelBuilder.append(visitorExportExcels);
String fileName = URLEncoder.encode("人员统计.xlsx", StandardCharsets.UTF_8);
response.setContentType("application/octet-stream");
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
OutputStream out = response.getOutputStream();
Workbook workbook = defaultStreamExcelBuilder.fixedTitles().build();
workbook.write(out);
workbook.close();
out.flush();
out.close();
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RuntimeException("导出访客信息失败: " + e.getMessage());
}
}
public Specification<PersonnelStatistics> condition(PersonnelStatisticsQueryParam personnelStatisticsQueryParam) {
return (root, query, criteriaBuilder) -> {
List<Predicate> list = new ArrayList<>();
//查询条件
if (StrUtil.isNotBlank(personnelStatisticsQueryParam.getRegion())) {
//区域查询
list.add(
criteriaBuilder.like(
root.get(PersonnelStatistics.Fields.region),
"%" + personnelStatisticsQueryParam.getRegion() + "%"
)
);
}
Order order = criteriaBuilder.desc(root.get(PersonnelStatistics.Fields.updateTime));
Predicate[] predicates = new Predicate[list.size()];
query.orderBy(order);
query.where(list.toArray(predicates));
return criteriaBuilder.and(list.toArray(predicates));
};
}
}
/* (C) 2022 YiRing, Inc. */
package com.yiring.app.vo.perstatistics;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serial;
import java.io.Serializable;
import lombok.*;
import lombok.experimental.FieldDefaults;
/**
* @author tzl
* @version 1.0
* @description:
* @date 2022/5/15 9:15
*/
@ApiModel("PersonnelStatisticsVo")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class PersonnelStatisticsVo implements Serializable {
@Serial
private static final long serialVersionUID = -5973651085056867803L;
@ApiModelProperty(value = "主键id", example = "1")
Long id;
@ApiModelProperty(value = "区域", example = "化工区")
String region;
@ApiModelProperty(value = "入场人数", example = "10")
String admissionNumber;
@ApiModelProperty(value = "出场人数", example = "5")
String attendance;
}
/* (C) 2022 YiRing, Inc. */
package com.yiring.app.web.perstatistics;
import com.yiring.app.param.perstatistics.PersonnelStatisticsQueryParam;
import com.yiring.app.service.perstatistics.PersonnelStatisticsService;
import com.yiring.app.vo.perstatistics.PersonnelStatisticsVo;
import com.yiring.common.core.Result;
import com.yiring.common.param.PageParam;
import com.yiring.common.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author tzl
* @version 1.0
* @description:
* @date 2022/5/15 9:49
*/
@Slf4j
@Validated
@Api(tags = "personnelStatistics(人员统计)")
@RestController
@RequestMapping("/personnelStatistics/")
public class PersonnelStatisticsController {
@Resource
PersonnelStatisticsService personnelStatisticsService;
@ApiOperation(value = "分页查询人员统计")
@GetMapping("pagePerSta")
public Result<PageVo<PersonnelStatisticsVo>> pagePerSta(
@Valid PersonnelStatisticsQueryParam personnelStatisticsQueryParam,
@Valid PageParam pageParam
) {
return personnelStatisticsService.pagePerSta(personnelStatisticsQueryParam, pageParam);
}
@ApiOperation(value = "导出", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@PostMapping("export")
public void exportVideo(
HttpServletResponse response,
@Valid PersonnelStatisticsQueryParam personnelStatisticsQueryParam
) {
personnelStatisticsService.export(personnelStatisticsQueryParam, response);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论