import com.alibaba.excel.EasyExcel;
|
import com.alibaba.excel.ExcelWriter;
|
import com.alibaba.excel.util.MapUtils;
|
import com.alibaba.excel.write.metadata.WriteSheet;
|
import com.alibaba.excel.write.metadata.fill.FillConfig;
|
import com.alibaba.fastjson.JSON;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import lombok.EqualsAndHashCode;
|
import lombok.Getter;
|
import lombok.Setter;
|
import org.apache.commons.lang3.StringUtils;
|
import org.jeecg.LbProjectCloudApplication;
|
import org.jeecg.common.api.vo.Result;
|
import org.jeecg.common.util.DateUtils;
|
import org.jeecg.common.util.oConvertUtils;
|
import org.jeecg.modules.project.entity.ProProject;
|
import org.jeecg.modules.project.entity.ProProjectLink;
|
import org.jeecg.modules.project.entity.ProProjectLinkTree;
|
import org.jeecg.modules.project.service.IProProjectLinkService;
|
import org.jeecg.modules.project.service.IProProjectService;
|
import org.junit.Test;
|
import org.junit.runner.RunWith;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
import java.io.File;
|
import java.io.IOException;
|
import java.time.LocalDate;
|
import java.time.Month;
|
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.TemporalAdjusters;
|
import java.util.*;
|
|
@RunWith(SpringRunner.class)
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = LbProjectCloudApplication.class)
|
|
public class ProjectTest {
|
@Autowired
|
private IProProjectService proProjectService;
|
|
@Autowired
|
private IProProjectLinkService proLinkService;
|
|
@Test
|
public void queryPageList() {
|
QueryWrapper<ProProject> queryWrapper = new QueryWrapper<>();
|
List<ProProject> list = proProjectService.list(queryWrapper);
|
//生成动态表头
|
List<Map<String, Object>> dynamcTableHead = createDynamcTableHeader(list);
|
//生成数据
|
List<Map<String, Object>> dynamcTableData = createDynamcTableData(list);
|
// System.err.println(dynamcTableHead);
|
// System.err.println(dynamcTableData);
|
|
|
String curWeekStart = DateUtils.weekToDayStartStr(1);
|
String curWeekEnd = DateUtils.weekToDayEndStr(1);
|
System.err.println(curWeekStart);
|
System.err.println(curWeekEnd);
|
for (int i = 0; i < 12; i++) {
|
Calendar c = Calendar.getInstance();
|
c.set(Calendar.YEAR, 2022); //
|
c.set(Calendar.MONTH, i + 1); //
|
System.err.println("周数" + (i + 1) + ":" + c.getActualMaximum(Calendar.WEEK_OF_MONTH));
|
}
|
|
// 随意取一个时间,取了当前时间
|
LocalDate localDate = LocalDate.of(2022, 1, 2);
|
System.out.println("当前时间为:" + localDate);
|
|
// 根据封装好的月份获取一个一月到十二月的Month流,此时流里的对象为Month
|
Arrays.stream(Month.values())
|
// 把每个month都调整到当前这个时间里,此时流的对象为LocalDate
|
.map(month -> month.adjustInto(localDate))
|
// 这里方便里观察此时流的数据,把转换后的时间打印了出来
|
.peek(System.out::println)
|
// 根据时间的所在月的第一天和最后一天作为间隔计算周数
|
.map(temporal -> ChronoUnit.WEEKS.between(temporal.with(TemporalAdjusters.firstDayOfMonth()), temporal.with(TemporalAdjusters.lastDayOfMonth())))
|
// 打印最后的周数
|
.forEach(System.out::println);
|
|
}
|
|
/**
|
* 构建动态表格数据
|
*/
|
private List<Map<String, Object>> createDynamcTableData(List<ProProject> list) {
|
List<Map<String, Object>> res = new ArrayList<>();
|
Map<String, Object> map = new HashMap<>();
|
list.forEach(item -> {
|
map.put("id", item.getId());
|
map.put("xmmc", item.getXmmc());
|
map.put("wcjd", item.getWcjd());
|
String xmcy = item.getXmcy();
|
if (!StringUtils.isEmpty(xmcy)) {
|
String[] split = xmcy.split(",");
|
for (int i = 0; i < split.length; i++) {
|
map.put(split[i], 1);
|
}
|
}
|
String xmfzr = item.getXmfzr();
|
if (!StringUtils.isEmpty(xmfzr)) {
|
map.put(xmfzr, 1);
|
}
|
|
res.add(map);
|
|
});
|
return res;
|
}
|
|
/**
|
* 构建动态表格头部
|
*/
|
private List<Map<String, Object>> createDynamcTableHeader(List<ProProject> records) {
|
List<Map<String, Object>> res = new ArrayList<>();
|
TreeSet<String> userTree = new TreeSet<>();
|
records.forEach(item -> {
|
if (!StringUtils.isEmpty(item.getXmfzr())) {
|
userTree.add(item.getXmfzr());
|
}
|
if (!StringUtils.isEmpty(item.getXmcy())) {
|
String[] split = item.getXmcy().split(",");
|
for (int i = 0; i < split.length; i++) {
|
String user = split[i];
|
userTree.add(user);
|
}
|
|
}
|
});
|
for (String item : userTree) {
|
Map<String, Object> map = new HashMap<>();
|
map.put("name", item);
|
res.add(map);
|
}
|
return res;
|
|
}
|
|
@Test
|
public void getW() {
|
int curYear = DateUtils.getCurYear();
|
int sum = 0;
|
for (int i = 1; i <= 12; i++) {
|
LocalDate date = LocalDate.of(curYear, i, 1);
|
// 获取星期几
|
int week = date.getDayOfWeek().getValue();
|
date = date.with(TemporalAdjusters.lastDayOfMonth());
|
//这个月最后一天
|
int dayOfMonth = date.getDayOfMonth();
|
// 打印
|
int k = 0;
|
for (int j = 1; j <= dayOfMonth; j++) {
|
LocalDate everyDay = LocalDate.of(curYear, i, j);
|
int w = everyDay.getDayOfWeek().getValue();
|
System.err.println("日期:" + everyDay + "-星期:" + w);
|
if (w % 7 == 0) {
|
k++;
|
sum++;
|
String weekToDayStartStr = DateUtils.weekToDayStartStr(sum);
|
String weekToDayEndStr = DateUtils.weekToDayEndStr(sum);
|
System.out.println(i + "月:" + k + "周-No:" + sum + "------" + weekToDayStartStr + "==" + weekToDayEndStr);
|
}
|
}
|
|
}
|
|
}
|
|
//计算菜单等级
|
@Test
|
public void docTest(){
|
String sxh = "10.2.311.4";
|
char[] chars = sxh.toCharArray();
|
char dot = '.';
|
int dotCount = 0 ;
|
for (int i = 0; i < sxh.length(); i++) {
|
if(dot==chars[i]) dotCount ++;
|
}
|
if(dotCount==3){
|
String substring1 = sxh.substring(0, sxh.indexOf("."));
|
sxh = sxh.substring(sxh.indexOf(".")+1);
|
String substring2 = sxh.substring(0, sxh.indexOf("."));
|
sxh = sxh.substring(sxh.indexOf(".")+1);
|
String substring3 = sxh.substring(0, sxh.indexOf("."));
|
sxh = sxh.substring(sxh.indexOf(".")+1);
|
String substring4 = sxh;
|
System.err.println(substring1);
|
System.err.println(substring2);
|
System.err.println(substring3);
|
System.err.println(substring4);
|
System.err.println(sxh);
|
|
}
|
}
|
//
|
@Test
|
public void proLinkTest(){
|
Result<List<ProProjectLinkTree>> result = new Result<List<ProProjectLinkTree>>();
|
Map<String, Object> map = new HashMap<>();
|
map.put("pro", "1565533629802356737");
|
List<ProProjectLink> list = proLinkService.listByMap(map);
|
Collections.sort(list, (o1, o2) -> o1.getSortNo().compareTo(o2.getSortNo()));
|
List<ProProjectLinkTree> res = new ArrayList<>();
|
getTreeList(res, list, null);
|
getSxh2(res,null,false );
|
System.err.println(JSON.toJSON(res));
|
}
|
|
private void getTreeList(List<ProProjectLinkTree> treeList, List<ProProjectLink> metaList, ProProjectLinkTree temp) {
|
for (int i = 0 ; i< metaList.size() ; i ++) {
|
ProProjectLink permission = metaList.get(i);
|
String tempPid = permission.getPid();
|
ProProjectLinkTree tree = new ProProjectLinkTree(permission);
|
if (temp == null && oConvertUtils.isEmpty(tempPid)) {
|
treeList.add(tree);
|
if (!tree.isLeaf()) {
|
getTreeList(treeList, metaList, tree);
|
}
|
} else if (temp != null && tempPid != null && tempPid.equals(temp.getId())) {
|
temp.getChildren().add(tree);
|
if (!tree.isLeaf()) {
|
getTreeList(treeList, metaList, tree);
|
}
|
}
|
|
}
|
}
|
|
//递归计算
|
|
/**
|
* 递归计算序号
|
* @param treeList
|
* @param oxh p序号
|
* @param isChildren 是否子节点
|
*/
|
private void getSxh2(List<ProProjectLinkTree> treeList ,String oxh,boolean isChildren ){
|
for (int i = 0; i < treeList.size(); i++) {
|
ProProjectLinkTree tree = treeList.get(i);
|
if(tree.getPid()==null) oxh = (i+1) +"";
|
if(isChildren){
|
tree.setSxh(oxh + "." + (i+1));
|
}else {
|
tree.setSxh(oxh);
|
}
|
if(tree.getChildren()!=null && tree.getChildren().size()>0){
|
if(tree.getLevel()>1) oxh += "." + (i+1);
|
getSxh2(tree.getChildren(),oxh,true);
|
}
|
|
}
|
}
|
|
/**
|
* 生成序号 和 a-select-tree组件需要的title 目前仅支持三级数据 TODO:递归支持无限极
|
*/
|
private void getSxh(List<ProProjectLinkTree> treeList) {
|
|
for (int i = 0; i < treeList.size(); i++) {
|
ProProjectLinkTree tree1 = treeList.get(i);
|
tree1.setSxh((i + 1) + "");
|
tree1.setTitle(tree1.getSxh() + org.jeecg.common.util.StringUtils.getNullStr(tree1.getXmhj()));
|
if (tree1.getChildren() != null && tree1.getChildren().size() > 0) {
|
List<ProProjectLinkTree> children1 = tree1.getChildren();
|
for (int j = 0; j < children1.size(); j++) {
|
ProProjectLinkTree tree2 = children1.get(j);
|
tree2.setSxh((i +1)+ "." +( j + 1));
|
tree2.setTitle(tree2.getSxh() + org.jeecg.common.util.StringUtils.getNullStr(tree2.getXmhj()));
|
if (tree2.getChildren() != null && tree2.getChildren().size() > 0) {
|
List<ProProjectLinkTree> children2 = tree2.getChildren();
|
for (int k = 0; k < children2.size(); k++) {
|
ProProjectLinkTree tree3 = children2.get(k);
|
tree3.setSxh((i +1)+ "." + ( j + 1) + '.' + (k + 1));
|
tree3.setTitle(tree3.getSxh() + org.jeecg.common.util.StringUtils.getNullStr(tree3.getXmhj()));
|
if (tree3.getChildren() != null && tree3.getChildren().size() > 0) {
|
List<ProProjectLinkTree> children3 = tree3.getChildren();
|
for (int l = 0; l < children3.size(); l++) {
|
ProProjectLinkTree tree4 = children3.get(l);
|
tree4.setSxh((i +1)+ "." + ( j + 1) + '.' + (k + 1)+ '.' + (l + 1));
|
tree4.setTitle(tree4.getSxh() + org.jeecg.common.util.StringUtils.getNullStr(tree4.getXmhj()));
|
}
|
}
|
}
|
}
|
}
|
}
|
|
}
|
|
}
|
|
|
@Test
|
public void exportExcel(){
|
String roopath = System.getProperty("user.dir");
|
roopath = roopath.substring(0,roopath.indexOf("/lb-boot/")+8) ;
|
|
String templateFileName = roopath+ File.separator + "export-template"+ File.separator + "weekly"+ File.separator + "complexFillWithTable.xlsx";
|
|
System.err.println(templateFileName);
|
String fileName = roopath + File.separator + "export"+ File.separator + System.currentTimeMillis() + ".xlsx";
|
System.err.println(fileName);
|
// 方案1
|
|
|
|
ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
|
WriteSheet writeSheet = EasyExcel.writerSheet().build();
|
// 直接写入数据
|
excelWriter.fill(data(), writeSheet);
|
excelWriter.fill(data(), writeSheet);
|
|
// 写入list之前的数据
|
Map<String, Object> map = new HashMap<String, Object>();
|
map.put("date", "2019年10月9日13:28:28");
|
excelWriter.fill(map, writeSheet);
|
|
// list 后面还有个统计 想办法手动写入
|
// 这里偷懒直接用list 也可以用对象
|
List<List<String>> totalListList = new ArrayList<List<String>>();
|
List<String> totalList = new ArrayList<String>();
|
totalListList.add(totalList);
|
totalList.add(null);
|
totalList.add(null);
|
totalList.add(null);
|
// 第四列
|
totalList.add("统计:1000");
|
// 这里是write 别和fill 搞错了
|
excelWriter.write(totalListList, writeSheet);
|
excelWriter.finish();
|
// 总体上写法比较复杂 但是也没有想到好的版本 异步的去写入excel 不支持行的删除和移动,也不支持备注这种的写入,所以也排除了可以
|
// 新建一个 然后一点点复制过来的方案,最后导致list需要新增行的时候,后面的列的数据没法后移,后续会继续想想解决方案
|
|
|
}
|
|
private List<FillData> data() {
|
List<FillData> list = new ArrayList<FillData>();
|
for (int i = 0; i < 10; i++) {
|
FillData fillData = new FillData();
|
list.add(fillData);
|
fillData.setName("张三");
|
fillData.setNumber(5.2);
|
}
|
return list;
|
}
|
|
|
@Getter
|
@Setter
|
@EqualsAndHashCode
|
public class FillData {
|
private String name;
|
private double number;
|
private Date date;
|
|
}
|
|
|
|
@Test
|
public void simple(){
|
// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
|
String roopath = System.getProperty("user.dir");
|
roopath = roopath.substring(0,roopath.indexOf("/lb-boot/")+8) ;
|
|
String templateFileName = roopath+ File.separator + "export-template"+ File.separator + "weekly"+ File.separator + "weekly.xlsx";
|
|
System.err.println(templateFileName);
|
String fileName = roopath + File.separator + "export"+ File.separator + System.currentTimeMillis() + ".xlsx";
|
System.err.println(fileName); // 这里 会填充到第一个sheet, 然后文件流会自动关闭
|
FillData fillData = new FillData();
|
fillData.setName("张三");
|
fillData.setNumber(5.2);
|
EasyExcel.write(fileName).withTemplate(templateFileName).sheet(1).doFill(fillData);
|
|
}
|
|
|
@Test
|
public void exportWeekly(){
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(new Date());
|
int week = calendar.get(Calendar.WEEK_OF_MONTH);
|
int month = calendar.get(Calendar.MONTH);
|
int year = calendar.get(Calendar.YEAR);
|
|
System.err.println(year);
|
System.err.println(month);
|
System.err.println(week);
|
|
}
|
|
|
|
}
|