268 lines
11 KiB
Java
268 lines
11 KiB
Java
package com.gunshi.project.hsz.controller;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.gunshi.core.result.R;
|
|
import com.gunshi.project.hsz.entity.so.PersonnelPlanLogPage;
|
|
import com.gunshi.project.hsz.entity.vo.PersonnelPlanLogStatisticsVo;
|
|
import com.gunshi.project.hsz.model.PersonnelPlan;
|
|
import com.gunshi.project.hsz.model.PersonnelPlanLog;
|
|
import com.gunshi.project.hsz.service.FileAssociationsService;
|
|
import com.gunshi.project.hsz.service.PersonnelPlanLogService;
|
|
import com.gunshi.project.hsz.service.PersonnelPlanService;
|
|
import com.gunshi.project.hsz.util.DateUtil;
|
|
import com.gunshi.project.hsz.validate.markers.Insert;
|
|
import com.gunshi.project.hsz.validate.markers.Update;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.media.Schema;
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.validation.annotation.Validated;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.io.Serializable;
|
|
import java.time.LocalDate;
|
|
import java.util.*;
|
|
|
|
/**
|
|
* Description:
|
|
* Created by XuSan on 2024/9/23.
|
|
*
|
|
* @author XuSan
|
|
* @version 1.0
|
|
*/
|
|
@Tag(name = "培训记录表")
|
|
@RestController
|
|
@RequestMapping(value = "/personnelPlanLog")
|
|
public class PersonnelPlanLogController extends AbstractCommonFileController{
|
|
|
|
|
|
@Autowired
|
|
private PersonnelPlanLogService service;
|
|
|
|
|
|
@Autowired
|
|
private PersonnelPlanService planService;
|
|
|
|
|
|
@Autowired
|
|
private FileAssociationsService fileService;
|
|
|
|
@Operation(summary = "新增")
|
|
@PostMapping("/insert")
|
|
public R<PersonnelPlanLog> insert(@Validated(Insert.class) @RequestBody PersonnelPlanLog dto) {
|
|
|
|
if (Objects.nonNull(dto.getStm()) && Objects.nonNull(dto.getEtm()) && dto.getStm().compareTo(dto.getEtm()) >= 0){
|
|
throw new IllegalArgumentException("开始时间不能大于结束时间");
|
|
}
|
|
|
|
LambdaQueryChainWrapper<PersonnelPlanLog> query = service.lambdaQuery()
|
|
.eq(PersonnelPlanLog::getPlanDate, dto. getPlanDate())
|
|
.eq(PersonnelPlanLog::getName, dto.getName());
|
|
if (query.count() > 0){
|
|
throw new IllegalArgumentException("当前培训日期标题名称重复");
|
|
}
|
|
|
|
if (Objects.nonNull(dto.getPlanId()) && planService.lambdaQuery().eq(PersonnelPlan::getId, dto.getPlanId()).count() == 0) {
|
|
throw new IllegalArgumentException("培训计划不存在");
|
|
}
|
|
dto.setId(IdWorker.getId());
|
|
dto.setCreateTime(new Date());
|
|
boolean result = service.save(dto);
|
|
|
|
if (result){
|
|
fileService.saveFile(dto.getFiles1(), getGroupId(), String.valueOf( dto.getId()),"1");
|
|
fileService.saveFile(dto.getFiles2(), getGroupId(), String.valueOf( dto.getId()),"2");
|
|
}
|
|
return R.ok(result ? dto : null);
|
|
}
|
|
|
|
@Operation(summary = "修改")
|
|
@PostMapping("/update")
|
|
public R<PersonnelPlanLog> update(@Validated(Update.class) @RequestBody PersonnelPlanLog dto) {
|
|
|
|
if (Objects.nonNull(dto.getStm()) && Objects.nonNull(dto.getEtm()) && dto.getStm().compareTo(dto.getEtm()) >= 0){
|
|
throw new IllegalArgumentException("开始时间不能大于结束时间");
|
|
}
|
|
|
|
LambdaQueryChainWrapper<PersonnelPlanLog> query = service.lambdaQuery()
|
|
.ne(PersonnelPlanLog::getId, dto.getId())
|
|
.eq(PersonnelPlanLog::getPlanDate, dto. getPlanDate())
|
|
.eq(PersonnelPlanLog::getName, dto.getName());
|
|
if (query.count() > 0){
|
|
throw new IllegalArgumentException("当前培训日期标题名称重复");
|
|
}
|
|
if (Objects.nonNull(dto.getPlanId()) && planService.lambdaQuery().eq(PersonnelPlan::getId, dto.getPlanId()).count() == 0) {
|
|
throw new IllegalArgumentException("培训计划不存在");
|
|
}
|
|
|
|
dto.setCreateTime(null);
|
|
dto.setCreateBy(null);
|
|
dto.setCreateName(null);
|
|
dto.setUpdateTime(new Date());
|
|
boolean result = service.updateById(dto);
|
|
if (result){
|
|
fileService.saveFile(dto.getFiles1(), getGroupId(), String.valueOf( dto.getId()),"1");
|
|
fileService.saveFile(dto.getFiles2(), getGroupId(), String.valueOf( dto.getId()),"2");
|
|
}
|
|
return R.ok(result ? dto : null);
|
|
}
|
|
|
|
|
|
@Operation(summary = "删除")
|
|
@GetMapping("/del/{id}")
|
|
public R<Boolean> del(@Schema(name = "id") @PathVariable("id") Serializable id) {
|
|
return R.ok(service.removeById(id));
|
|
}
|
|
@Operation(summary = "获取详情(包括文件信息)")
|
|
@GetMapping("/get/{id}")
|
|
public R<PersonnelPlanLog> get(@Schema(name = "id") @PathVariable("id") Serializable id) {
|
|
PersonnelPlanLog o = service.getById(id);
|
|
if (Objects.isNull(o)) {
|
|
throw new IllegalArgumentException("当前数据不存在");
|
|
}
|
|
|
|
o.setFiles1(fileService.getFiles(getGroupId(),String.valueOf(o.getId()),"1"));
|
|
o.setFiles2(fileService.getFiles(getGroupId(),String.valueOf(o.getId()),"2"));
|
|
return R.ok(o);
|
|
}
|
|
|
|
@Operation(summary = "列表")
|
|
@PostMapping("/list")
|
|
public R<List<PersonnelPlanLog>> list() {
|
|
return R.ok(service.lambdaQuery()
|
|
.list());
|
|
}
|
|
|
|
@Operation(summary = "统计")
|
|
@GetMapping("/statistics/{year}")
|
|
public R<PersonnelPlanLogStatisticsVo> statistics(@PathVariable("year") Integer year) {
|
|
if (year < 1970 || year > LocalDate.now().getYear()) {
|
|
throw new IllegalArgumentException("年份不合法");
|
|
}
|
|
//plan planLog
|
|
Date stm = DateUtil.convertStringToDate(year + "-01-01 00:00:00");
|
|
Date etm = DateUtil.convertStringToDate(year + "-12-31 23:59:59");
|
|
List<PersonnelPlanLog> planLogs = service.lambdaQuery()
|
|
.between(PersonnelPlanLog::getPlanDate, stm, etm)
|
|
.list();
|
|
List<PersonnelPlan> plans = planService.lambdaQuery()
|
|
.between(PersonnelPlan::getStm, stm, etm)
|
|
.list();
|
|
|
|
PersonnelPlanLogStatisticsVo vo = new PersonnelPlanLogStatisticsVo();
|
|
|
|
// Map<Integer, PersonnelPlanLogStatisticsVo.EchartsData> map1 = Maps.newHashMap();
|
|
// Calendar calendar = Calendar.getInstance();
|
|
// int month = calendar.get(Calendar.MONTH) + 1;
|
|
if (CollectionUtils.isNotEmpty(planLogs)) {
|
|
|
|
vo.setNumberOfPeriods1(planLogs.size());
|
|
vo.setNumberOfPeriods2(plans.stream().mapToInt(PersonnelPlan::getNum).sum());
|
|
vo.setPersonNum1(planLogs.stream().mapToInt(PersonnelPlanLog::getNumPeople).sum());
|
|
vo.setPersonNum2(plans.stream()
|
|
.map(
|
|
item -> item.getNumPeople() * item.getNum()
|
|
).mapToInt(Integer::intValue).sum()
|
|
);
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
List<PersonnelPlanLogStatisticsVo.EchartsData> logStatPerMonth = new ArrayList<>();
|
|
for (int i = 0; i < 12; i++) {
|
|
int month = i; //calendar的月份是从0开始
|
|
List<PersonnelPlanLog> logsPerMonth = planLogs.stream().filter(log -> {
|
|
Date planDate = log.getPlanDate();
|
|
calendar.setTime(planDate);
|
|
return month == calendar.get(Calendar.MONTH);
|
|
}).toList();
|
|
|
|
//month num1实际人数 num2实际期数
|
|
PersonnelPlanLogStatisticsVo.EchartsData chartItem = new PersonnelPlanLogStatisticsVo.EchartsData();
|
|
chartItem.setMonth(i + 1); //calendar的月份是从0开始
|
|
chartItem.setNum1(logsPerMonth.stream().mapToInt(PersonnelPlanLog::getNumPeople).sum());
|
|
chartItem.setNum2(logsPerMonth.size());
|
|
|
|
logStatPerMonth.add(chartItem);
|
|
// PersonnelPlanLogStatisticsVo.EchartsData echartsData1 = map1.get(i);
|
|
// if (Objects.isNull(echartsData1)) {
|
|
// echartsData1 = new PersonnelPlanLogStatisticsVo.EchartsData();
|
|
// }
|
|
// int finalI = i;
|
|
// List<PersonnelPlanLog> list = planLogs.stream()
|
|
// .filter(item ->
|
|
// {
|
|
// calendar.setTime(item.getPlanDate());
|
|
// return month == finalI;
|
|
// })
|
|
// .toList();
|
|
//
|
|
// echartsData1.setMonth(finalI)
|
|
// .setNum1(list.stream().mapToInt(PersonnelPlanLog::getNumPeople).sum())
|
|
// .setNum2(list.size());
|
|
// map1.put(i, echartsData1);
|
|
}
|
|
vo.setList1(logStatPerMonth);
|
|
}
|
|
|
|
|
|
return R.ok(vo);
|
|
}
|
|
|
|
|
|
@Operation(summary = "分页")
|
|
@PostMapping("/page")
|
|
public R<Page<PersonnelPlanLog>> page(@RequestBody @Validated PersonnelPlanLogPage page) {
|
|
LambdaQueryWrapper<PersonnelPlanLog> query = Wrappers.lambdaQuery();
|
|
|
|
Date stm = page.getStm();
|
|
if (Objects.nonNull(stm)) {
|
|
query.ge(PersonnelPlanLog::getPlanDate, stm);
|
|
}
|
|
|
|
Date etm = page.getEtm();
|
|
if (Objects.nonNull(etm)) {
|
|
query.le(PersonnelPlanLog::getPlanDate, etm);
|
|
}
|
|
|
|
Integer type = page.getType();
|
|
if (Objects.nonNull(type)) {
|
|
query.eq(PersonnelPlanLog::getType, type);
|
|
}
|
|
|
|
Long planId = page.getPlanId();
|
|
if (Objects.nonNull(planId)) {
|
|
query.eq(PersonnelPlanLog::getPlanId, planId);
|
|
}
|
|
|
|
String trainees = page.getTrainees();
|
|
if (StringUtils.isNotBlank(trainees)) {
|
|
query.like(PersonnelPlanLog::getTrainees, trainees);
|
|
}
|
|
|
|
String unit = page.getUnit();
|
|
if (StringUtils.isNotBlank(unit)) {
|
|
query.like(PersonnelPlanLog::getUnit, unit);
|
|
}
|
|
|
|
query.orderByDesc(PersonnelPlanLog::getPlanDate);
|
|
|
|
Page<PersonnelPlanLog> data = service.page(page.getPageSo().toPage(), query);
|
|
data.getRecords().forEach(o -> {
|
|
o.setFiles1(fileService.getFiles(getGroupId(),String.valueOf(o.getId()),"1"));
|
|
o.setFiles2(fileService.getFiles(getGroupId(),String.valueOf(o.getId()),"2"));
|
|
});
|
|
return R.ok(data);
|
|
}
|
|
|
|
@Override
|
|
public String getGroupId() {
|
|
return "personnelPlanLog";
|
|
}
|
|
|
|
}
|