package com.budwk.app; import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.ExcelImportUtil; import cn.afterturn.easypoi.excel.annotation.Excel; import cn.afterturn.easypoi.excel.entity.ExportParams; import cn.afterturn.easypoi.excel.entity.ImportParams; import cn.hutool.core.util.StrUtil; import com.budwk.app.sys.models.Sys_user; import com.budwk.app.sys.models.Sys_user_source; import com.budwk.app.zhgh.welfare.model.WelfareList; import com.budwk.app.zhgh.welfare.model.WelfareProjectSubjectOption; import com.budwk.app.zhgh.welfare.model.WelfareUserSelection; import com.budwk.app.zhgh.welfare.service.WelfareProjectService; import lombok.Data; import org.apache.poi.ss.usermodel.Workbook; import org.junit.Test; import org.junit.runner.RunWith; import org.nutz.boot.test.junit4.NbJUnit4Runner; import org.nutz.dao.Cnd; import org.nutz.dao.Dao; import org.nutz.dao.Sqls; import org.nutz.dao.sql.Sql; import org.nutz.ioc.loader.annotation.Inject; import org.nutz.ioc.loader.annotation.IocBean; import org.nutz.lang.util.NutMap; import org.nutz.mvc.annotation.At; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.time.LocalDate; import java.time.ZoneId; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @IocBean @RunWith(NbJUnit4Runner.class) public class WelfareTest { @Inject private Dao dao; @Inject private WelfareProjectService welfareService; @Test public void importSelectUser() { List list = ExcelImportUtil.importExcel(new File("C:\\Users\\jug\\Desktop\\新建文件夹 (2)\\蛋糕汇总表.xlsx"), SelectUser.class, new ImportParams()); // 福利id String welfareId = "6b6b438811044d7180b76facaebbe2f3"; // 福利名单 Sql sql = Sqls.create(""" SELECT u.id, u.username AS userName, u.loginname AS loginName, u.mobile FROM `welfare_list` t1 LEFT JOIN sys_user u ON u.id = t1.userId WHERE t1.projectId = @projectId group by u.loginname """); sql.setParam("projectId", welfareId); List welfareLists = welfareService.listMap(sql); List loginNames = welfareLists.stream().map(v -> v.getString("loginName")).toList(); Map loginNameMap = welfareLists.stream().collect(Collectors.toMap(v -> v.getString("loginName"), v -> v)); // 筛选出本次导入的 List thisList = list.stream().filter(v -> StrUtil.isNotBlank(v.getLoginName()) && loginNames.contains(v.getLoginName())) .peek(v -> v.setLoginName(v.getLoginName().trim())) .toList(); System.out.println(thisList.size()); // 福利选项 List options = dao.query(WelfareProjectSubjectOption.class, Cnd.where(WelfareProjectSubjectOption::getWelfareId, "=", welfareId)); List selections = thisList.stream().map(v -> { WelfareUserSelection welfareUserSelection = new WelfareUserSelection(); welfareUserSelection.setSelectTime(new Date()); welfareUserSelection.setSelectUserId(loginNameMap.get(v.getLoginName()).getString("id")); welfareUserSelection.setSelectNum(1); if (StrUtil.isNotBlank(v.getMobile())) { welfareUserSelection.setMobile(v.getMobile()); } else { welfareUserSelection.setMobile(loginNameMap.get(v.getLoginName()).getString("mobile")); } welfareUserSelection.setWelfareId(welfareId); String optionId = options.stream().filter(option -> option.getOptionName().equals(v.getOptionName())).findFirst().map(option -> option.getId()).orElse(null); welfareUserSelection.setSelectOptionId(optionId); return welfareUserSelection; }).toList(); dao.insert(selections); } @Test public void deleteUser() { List list = ExcelImportUtil.importExcel(new File("C:\\Users\\jug\\Desktop\\新建文件夹 (2)\\蛋糕汇总表.xlsx"), SelectUser.class, new ImportParams()); List loginNames = list.stream().map(v -> v.getLoginName()).toList(); Sql sql = Sqls.create("select id from sys_user where loginname in (@loginname)"); sql.setParam("loginname", loginNames); sql.setCallback(Sqls.callback.strList()); dao.execute(sql); List ids = sql.getList(String.class); List welfareLists = dao.query(WelfareList.class, Cnd.NEW()); List welfareUserIds = welfareLists.stream().map(v -> v.getUserId()).toList(); List list1 = welfareUserIds.stream().filter(v -> !ids.contains(v)).toList(); System.out.println(list1.size()); //dao.clear(WelfareList.class, Cnd.where("userId", "not in", ids.stream().map(v -> v.getString("id")).toList())); } @Data public static class SelectUser { @Excel(name = "工号") private String loginName; @Excel(name = "姓名") private String userName; @Excel(name = "手机号码") private String mobile; @Excel(name = "选择物品") private String optionName; @Excel(name = "出生日期", importFormat = "yyyy-MM-dd", exportFormat = "yyyy-MM-dd") private Date birthday; } @Test public void fixData() throws IOException { List users = dao.query(Sys_user_source.class, Cnd.NEW().groupBy(Sys_user::getLoginname)); Map userMap = users.stream().filter(v -> v.getBirthday() != null).collect(Collectors.toMap(v -> v.getLoginname(), v -> v.getBirthday())); List allList = ExcelImportUtil.importExcel(new File("C:\\Users\\jug\\Desktop\\南京师范蛋糕卡\\蛋糕汇总表2.xlsx"), SelectUser.class, new ImportParams()); for (SelectUser selectUser : allList) { if (userMap.containsKey(selectUser.getLoginName())) { selectUser.setBirthday(userMap.get(selectUser.getLoginName())); } } Workbook workbook = ExcelExportUtil.exportExcel( new ExportParams("修改后数据", "Sheet1"), SelectUser.class, allList ); FileOutputStream fos = new FileOutputStream("C:\\Users\\jug\\Desktop\\南京师范蛋糕卡\\匹配上生日.xlsx"); workbook.write(fos); fos.close(); workbook.close(); } @Test public void importUser() { List allList = ExcelImportUtil.importExcel(new File("C:\\Users\\jug\\Desktop\\南京师范蛋糕卡\\匹配上生日.xlsx"), SelectUser.class, new ImportParams()); List list = allList.stream().filter(v -> StrUtil.isNotBlank(v.getLoginName())).toList(); List users = dao.query(Sys_user.class, Cnd.NEW()); Map userMap = users.stream().collect(Collectors.toMap(v -> v.getLoginname(), v -> v)); // 福利id String welfareId = "6b6b438811044d7180b76facaebbe2f3"; List options = dao.query(WelfareProjectSubjectOption.class, Cnd.where(WelfareProjectSubjectOption::getWelfareId, "=", welfareId)); Map optionMap = options.stream().collect(Collectors.toMap(v -> v.getOptionName(), v -> v.getId())); // 福利名单 List welfareLists = list.stream().filter(v -> v.getBirthday().getMonth() + 1 > 6 && v.getBirthday().getMonth() + 1 <= 9).map(v -> { WelfareList welfareList = new WelfareList(); welfareList.setProjectId(welfareId); if (userMap.get(v.getLoginName()) != null) { welfareList.setUserId(userMap.get(v.getLoginName()).getId()); welfareList.setWelfareUnitId(userMap.get(v.getLoginName()).getUnitId()); welfareList.setPersonType(userMap.get(v.getLoginName()).getPersonType()); welfareList.setUserState(userMap.get(v.getLoginName()).getUserState()); welfareList.setPreparedBy(userMap.get(v.getLoginName()).getPreparedBy()); } else { System.out.println(v.getLoginName()); } return welfareList; }).toList(); // 选择数据 List selections = list.stream().filter(v -> v.getBirthday().getMonth() + 1 > 6 && v.getBirthday().getMonth() + 1 <= 9).map(v -> { WelfareUserSelection selection = new WelfareUserSelection(); selection.setWelfareId(welfareId); selection.setSelectUserId(userMap.get(v.getLoginName()).getId()); selection.setSelectOptionId(optionMap.get(v.getOptionName())); selection.setSelectNum(1); LocalDate localDate = LocalDate.of(2024, 12, 15); Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); selection.setSelectTime(date); selection.setMobile(v.getMobile()); return selection; }).toList(); System.out.println(welfareLists.size()); System.out.println(selections.size()); dao.insert(welfareLists); dao.insert(selections); } @Test public void updateWelfareList() { Sql sql = Sqls.create("select * from vw_user"); List users = welfareService.listMap(sql); Map loginNameMap = users.stream().collect(Collectors.toMap(v -> v.getString("id"), v -> v)); List list = dao.query(WelfareList.class, Cnd.NEW()); for (WelfareList w : list) { w.setWelfareUnitName(loginNameMap.get(w.getUserId()).getString("unitName")); w.setWelfareUnionId(loginNameMap.get(w.getUserId()).getString("unionId")); w.setWelfareUnionName(loginNameMap.get(w.getUserId()).getString("unionName")); } dao.update(list, "welfareUnitName|welfareUnionId|welfareUnionName"); List userSelections = dao.query(WelfareUserSelection.class, Cnd.NEW()); for (WelfareUserSelection w : userSelections) { w.setMobile(loginNameMap.get(w.getSelectUserId()).getString("mobile")); } dao.update(userSelections, "mobile"); } }