diff --git a/src/main/java/com/mixi/common/tasks/MiTuExportScheduledTask.java b/src/main/java/com/mixi/common/tasks/MiTuExportScheduledTask.java index 6d6db1a..ea58c2e 100644 --- a/src/main/java/com/mixi/common/tasks/MiTuExportScheduledTask.java +++ b/src/main/java/com/mixi/common/tasks/MiTuExportScheduledTask.java @@ -1,1332 +1,1651 @@ -package com.mixi.common.tasks; - -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.mixi.common.tasks.mituExportEntity.*; -import com.mixi.common.utils.CopyUtil; -import com.mixi.common.utils.MinioUtil; -import com.mixi.mapper.*; -import com.mixi.mapper.entity.*; -import org.apache.poi.ss.usermodel.*; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.apache.commons.fileupload.FileItem; -import org.apache.commons.fileupload.FileItemFactory; -import org.apache.commons.fileupload.disk.DiskFileItemFactory; -import org.springframework.context.annotation.Bean; -import org.springframework.retry.annotation.Backoff; -import org.springframework.retry.annotation.Retryable; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Component; -import org.springframework.web.multipart.MultipartFile; -import org.springframework.web.multipart.commons.CommonsMultipartFile; - -import javax.annotation.PostConstruct; -import javax.annotation.Resource; -import java.io.*; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.sql.*; -import java.time.*; -import java.time.format.DateTimeFormatter; -import java.time.temporal.TemporalAdjusters; -import java.util.*; -import java.util.Date; -import java.util.stream.Collectors; - -@Component -public class MiTuExportScheduledTask { - - @Resource - private MinioUtil minioUtil; - - static final String JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; - static final String DB_URL = "jdbc:sqlserver://118.142.0.178:1550;databaseName=Hayman_prod"; - - // 数据库凭据 - static final String USER = "user01"; - static final String PASS = "haySIS-0522"; - - @Resource - private MiTuExportMapper miTuExportMapper; - @Resource - private MiTuProductMapper miTuProductMapper; - @Resource - private TProductMapper productMapper; - - @Resource - private MiTuProductSellNumMapper miTuProductSellNumMapper; - @PostConstruct - public void executeWeeklyHeavyStockReport() { -// customerPurchaseReport(); -// NewJoinVIPReport(); -// weeklySellThrReport(); -// WeeklyHeavyStockReport(); -// QuarterlyProductGroupingReport(); -// customerTypeAnalysis(); -// getBestSell(); - } - -// public static void main(String[] args) { +//package com.mixi.common.tasks; +// +//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +//import com.mixi.common.tasks.mituExportEntity.*; +//import com.mixi.common.utils.CopyUtil; +//import com.mixi.common.utils.MinioUtil; +//import com.mixi.mapper.*; +//import com.mixi.mapper.entity.*; +//import org.apache.poi.ss.usermodel.*; +//import org.apache.poi.xssf.usermodel.XSSFWorkbook; +//import org.apache.commons.fileupload.FileItem; +//import org.apache.commons.fileupload.FileItemFactory; +//import org.apache.commons.fileupload.disk.DiskFileItemFactory; +//import org.springframework.context.annotation.Bean; +//import org.springframework.retry.annotation.Backoff; +//import org.springframework.retry.annotation.Retryable; +//import org.springframework.scheduling.annotation.Scheduled; +//import org.springframework.stereotype.Component; +//import org.springframework.web.multipart.MultipartFile; +//import org.springframework.web.multipart.commons.CommonsMultipartFile; +// +//import javax.annotation.PostConstruct; +//import javax.annotation.Resource; +//import java.io.*; +//import java.nio.file.Files; +//import java.nio.file.Paths; +//import java.sql.*; +//import java.time.*; +//import java.time.format.DateTimeFormatter; +//import java.time.temporal.TemporalAdjusters; +//import java.util.*; +//import java.util.Date; +//import java.util.stream.Collectors; +// +//@Component +//public class MiTuExportScheduledTask { +// +// @Resource +// private MinioUtil minioUtil; +// +// static final String JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; +// static final String DB_URL = "jdbc:sqlserver://118.142.0.178:1550;databaseName=Hayman_prod"; +// +// // 数据库凭据 +// static final String USER = "user01"; +// static final String PASS = "haySIS-0522"; +// +// @Resource +// private MiTuExportMapper miTuExportMapper; +// @Resource +// private MiTuProductMapper miTuProductMapper; +// @Resource +// private TProductMapper productMapper; +// +// @Resource +// private MiTuProductSellNumMapper miTuProductSellNumMapper; +// @PostConstruct +// public void executeWeeklyHeavyStockReport() { +//// customerPurchaseReport(); +//// NewJoinVIPReport(); +//// weeklySellThrReport(); +//// WeeklyHeavyStockReport(); +//// QuarterlyProductGroupingReport(); +//// customerTypeAnalysis(); +//// getBestSell(); +// } +// +//// public static void main(String[] args) { +//// LocalDate today = LocalDate.now(); +//// for (int i = 0; i < 30; i++) { +//// // 将这一天的商品销售数量入库 +//// test(today); +//// System.out.println(today); +////// miTuProductSellNumEntry(today, productListAll); +//// today = today.minusDays(1); +//// } +//// } +//// public static void test(LocalDate today) { +//// today = today.minusDays(1); +//// } +// +// @Scheduled(cron = "0 0 1 * * ?") +// private void getBestSell() { +// QueryWrapper bestSellIsNotNull = new QueryWrapper<>(); +//// bestSellIsNotNull.lambda().isNotNull(TProduct::getBestSell); +// List productListAll = productMapper.selectList(bestSellIsNotNull); // LocalDate today = LocalDate.now(); -// for (int i = 0; i < 30; i++) { +// for (int i = 0; i < 1; i++) { // // 将这一天的商品销售数量入库 -// test(today); -// System.out.println(today); -//// miTuProductSellNumEntry(today, productListAll); +// miTuProductSellNumEntry(today, productListAll); // today = today.minusDays(1); // } +//// for (TProduct tProduct : productList1) { +//// tProduct.setBestSell(null); +//// productMapper.updateById(tProduct); +//// } +//// Map> itemNameMap = getBestSellItemNameList(); +//// for (String s : itemNameMap.keySet()) { +//// if (s.equals("yesterdayItemNameList")) { +//// QueryWrapper qw = new QueryWrapper<>(); +//// qw.lambda().in(MiTuProduct::getItemName, itemNameMap.get(s)); +//// List miTuProductList = miTuProductMapper.selectList(qw); +//// if (!CollectionUtils.isEmpty(miTuProductList)) { +//// Set collect = miTuProductList.stream().map(MiTuProduct::getProductId).collect(Collectors.toSet()); +//// if (!CollectionUtils.isEmpty(collect)) { +//// QueryWrapper qwBestSell = new QueryWrapper<>(); +//// qwBestSell.lambda().in(TProduct::getId, new ArrayList<>(collect)); +//// List productList = productMapper.selectList(qwBestSell); +//// if (!CollectionUtils.isEmpty(productList)) { +//// for (TProduct tProduct : productList) { +//// tProduct.setBestSell("1"); +//// productMapper.updateById(tProduct); +//// } +//// } +//// } +//// } +//// } +//// if (s.equals("lastWeekItemNameList")) { +//// QueryWrapper qw = new QueryWrapper<>(); +//// qw.lambda().in(MiTuProduct::getItemName, itemNameMap.get(s)); +//// List miTuProductList = miTuProductMapper.selectList(qw); +//// if (!CollectionUtils.isEmpty(miTuProductList)) { +//// Set collect = miTuProductList.stream().map(MiTuProduct::getProductId).collect(Collectors.toSet()); +//// if (!CollectionUtils.isEmpty(collect)) { +//// QueryWrapper qwBestSell = new QueryWrapper<>(); +//// qwBestSell.lambda().in(TProduct::getId, new ArrayList<>(collect)); +//// List productList = productMapper.selectList(qwBestSell); +//// if (!CollectionUtils.isEmpty(productList)) { +//// for (TProduct tProduct : productList) { +//// if (StringUtils.isEmpty(tProduct.getBestSell())) { +//// tProduct.setBestSell("2"); +//// }else { +//// tProduct.setBestSell(tProduct.getBestSell() + ",2"); +//// } +//// productMapper.updateById(tProduct); +//// } +//// } +//// } +//// } +//// } +//// if (s.equals("lastMonthItemNameList")) { +//// QueryWrapper qw = new QueryWrapper<>(); +//// qw.lambda().in(MiTuProduct::getItemName, itemNameMap.get(s)); +//// List miTuProductList = miTuProductMapper.selectList(qw); +//// if (!CollectionUtils.isEmpty(miTuProductList)) { +//// Set collect = miTuProductList.stream().map(MiTuProduct::getProductId).collect(Collectors.toSet()); +//// if (!CollectionUtils.isEmpty(collect)) { +//// QueryWrapper qwBestSell = new QueryWrapper<>(); +//// qwBestSell.lambda().in(TProduct::getId, new ArrayList<>(collect)); +//// List productList = productMapper.selectList(qwBestSell); +//// if (!CollectionUtils.isEmpty(productList)) { +//// for (TProduct tProduct : productList) { +//// if (StringUtils.isEmpty(tProduct.getBestSell())) { +//// tProduct.setBestSell("3"); +//// }else { +//// tProduct.setBestSell(tProduct.getBestSell() + ",3"); +//// } +//// productMapper.updateById(tProduct); +//// } +//// } +//// } +//// } +//// } +//// } // } -// public static void test(LocalDate today) { -// today = today.minusDays(1); -// } - - @Scheduled(cron = "0 0 1 * * ?") - private void getBestSell() { - QueryWrapper bestSellIsNotNull = new QueryWrapper<>(); -// bestSellIsNotNull.lambda().isNotNull(TProduct::getBestSell); - List productListAll = productMapper.selectList(bestSellIsNotNull); - LocalDate today = LocalDate.now(); - for (int i = 0; i < 1; i++) { - // 将这一天的商品销售数量入库 - miTuProductSellNumEntry(today, productListAll); - today = today.minusDays(1); - } -// for (TProduct tProduct : productList1) { -// tProduct.setBestSell(null); -// productMapper.updateById(tProduct); +// +// private void miTuProductSellNumEntry(LocalDate today, List productListAll) { +// String endDate = formatDate(today); +// String startDate = formatDate(today.minusDays(1)); +// for (TProduct product : productListAll) { +// String pictureName = product.getPictureName(); +// String[] split = pictureName.split("_"); +// String itemName = split[0]; +// String color = split[1]; +// MiTuProductSellNum miTuProductSellNum = getProductSellNum(itemName, color, startDate, endDate); +// miTuProductSellNum.setProductId(product.getId()); +// miTuProductSellNumMapper.insert(miTuProductSellNum); // } -// Map> itemNameMap = getBestSellItemNameList(); -// for (String s : itemNameMap.keySet()) { -// if (s.equals("yesterdayItemNameList")) { -// QueryWrapper qw = new QueryWrapper<>(); -// qw.lambda().in(MiTuProduct::getItemName, itemNameMap.get(s)); -// List miTuProductList = miTuProductMapper.selectList(qw); -// if (!CollectionUtils.isEmpty(miTuProductList)) { -// Set collect = miTuProductList.stream().map(MiTuProduct::getProductId).collect(Collectors.toSet()); -// if (!CollectionUtils.isEmpty(collect)) { -// QueryWrapper qwBestSell = new QueryWrapper<>(); -// qwBestSell.lambda().in(TProduct::getId, new ArrayList<>(collect)); -// List productList = productMapper.selectList(qwBestSell); -// if (!CollectionUtils.isEmpty(productList)) { -// for (TProduct tProduct : productList) { -// tProduct.setBestSell("1"); -// productMapper.updateById(tProduct); -// } -// } -// } -// } +// } +// +// private MiTuProductSellNum getProductSellNum(String itemName, String color, String startDate, String endDate) { +// Connection conn = null; +// Statement stmt = null; +// MiTuProductSellNum miTuProductSellNum = new MiTuProductSellNum(); +// try { +// // 注册 JDBC 驱动器 +// Class.forName(JDBC_DRIVER); +// +// // 打开一个连接 +// System.out.println("连接数据库..."); +// conn = DriverManager.getConnection(DB_URL, USER, PASS); +// +// // 执行查询 +// System.out.println("创建声明..."); +// stmt = conn.createStatement(); +// String sql; +// sql = "SELECT count(1) as num FROM v_MZG020B\n" + +// "WHERE TRX_DATE >= '" + startDate + "'\n" + +// "AND TRX_DATE < '" + endDate + "'\n" + +// "AND ITEM_NAME = '" + itemName + "'\n" + +// "AND COLOR = '" + color + "'"; +// +// ResultSet rs = stmt.executeQuery(sql); +// // 处理结果集 +// while (rs.next()) { +// miTuProductSellNum.setSellNum(rs.getInt("num")); +// miTuProductSellNum.setSellDate(LocalDate.parse(startDate)); // } -// if (s.equals("lastWeekItemNameList")) { -// QueryWrapper qw = new QueryWrapper<>(); -// qw.lambda().in(MiTuProduct::getItemName, itemNameMap.get(s)); -// List miTuProductList = miTuProductMapper.selectList(qw); -// if (!CollectionUtils.isEmpty(miTuProductList)) { -// Set collect = miTuProductList.stream().map(MiTuProduct::getProductId).collect(Collectors.toSet()); -// if (!CollectionUtils.isEmpty(collect)) { -// QueryWrapper qwBestSell = new QueryWrapper<>(); -// qwBestSell.lambda().in(TProduct::getId, new ArrayList<>(collect)); -// List productList = productMapper.selectList(qwBestSell); -// if (!CollectionUtils.isEmpty(productList)) { -// for (TProduct tProduct : productList) { -// if (StringUtils.isEmpty(tProduct.getBestSell())) { -// tProduct.setBestSell("2"); -// }else { -// tProduct.setBestSell(tProduct.getBestSell() + ",2"); -// } -// productMapper.updateById(tProduct); -// } -// } -// } -// } +// // 清理环境 +// rs.close(); +// stmt.close(); +// conn.close(); +// } catch (SQLException | ClassNotFoundException e) { +// // 处理异常 +// e.printStackTrace(); +// } +// System.out.println("查询执行完成!"); +// return miTuProductSellNum; +// } +// +// // 格式化日期为字符串 +// private static String formatDate(LocalDate date) { +// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); +// return date.format(formatter); +// } +// private Map> getBestSellItemNameList() { +// Map> result = new HashMap<>(); +// // 获取当前日期 +// LocalDate currentDate = LocalDate.now(); +// // 格式化当前日期 +// String currentDateString = formatDate(currentDate); +// +// // 获取前一天日期 +// LocalDate yesterdayDate = currentDate.minusDays(1); +// // 格式化前一天日期 +// String yesterdayDateString = formatDate(yesterdayDate); +// +// // 获取前一周日期 +// LocalDate lastWeekDate = currentDate.minusWeeks(1); +// // 格式化前一周日期 +// String lastWeekDateString = formatDate(lastWeekDate); +// +// // 获取前一个月日期 +// LocalDate lastMonthDate = currentDate.minusMonths(1); +// // 格式化前一个月日期 +// String lastMonthDateString = formatDate(lastMonthDate); +// Connection conn = null; +// Statement stmt = null; +// try { +// // 注册 JDBC 驱动器 +// Class.forName(JDBC_DRIVER); +// +// // 打开一个连接 +// System.out.println("连接数据库..."); +// conn = DriverManager.getConnection(DB_URL, USER, PASS); +// +// // 执行查询 +// System.out.println("创建声明..."); +// stmt = conn.createStatement(); +// String sql; +// sql = "SELECT TOP 20 ITEM_NAME FROM v_MZG020B\n" + +// " WHERE TRX_DATE >= '" + yesterdayDateString + "'\n" + +// " AND TRX_DATE < '" + currentDateString + "'\n" + +// " AND ITEM_NAME != 'Shopping Bag Fee'\n" + +// " GROUP BY ITEM_NAME\n" + +// " ORDER BY COUNT(1) DESC"; +// +// ResultSet rs = stmt.executeQuery(sql); +// // 处理结果集 +// List yesterdayItemNameList = new ArrayList<>(); +// while (rs.next()) { +// yesterdayItemNameList.add(rs.getString("ITEM_NAME")); // } -// if (s.equals("lastMonthItemNameList")) { -// QueryWrapper qw = new QueryWrapper<>(); -// qw.lambda().in(MiTuProduct::getItemName, itemNameMap.get(s)); -// List miTuProductList = miTuProductMapper.selectList(qw); -// if (!CollectionUtils.isEmpty(miTuProductList)) { -// Set collect = miTuProductList.stream().map(MiTuProduct::getProductId).collect(Collectors.toSet()); -// if (!CollectionUtils.isEmpty(collect)) { -// QueryWrapper qwBestSell = new QueryWrapper<>(); -// qwBestSell.lambda().in(TProduct::getId, new ArrayList<>(collect)); -// List productList = productMapper.selectList(qwBestSell); -// if (!CollectionUtils.isEmpty(productList)) { -// for (TProduct tProduct : productList) { -// if (StringUtils.isEmpty(tProduct.getBestSell())) { -// tProduct.setBestSell("3"); -// }else { -// tProduct.setBestSell(tProduct.getBestSell() + ",3"); -// } -// productMapper.updateById(tProduct); -// } -// } -// } +// result.put("yesterdayItemNameList", yesterdayItemNameList); +// sql = "SELECT TOP 20 ITEM_NAME FROM v_MZG020B\n" + +// " WHERE TRX_DATE >= '" + lastWeekDateString + "'\n" + +// " AND TRX_DATE < '" + currentDateString + "'\n" + +// " AND ITEM_NAME != 'Shopping Bag Fee'\n" + +// " GROUP BY ITEM_NAME\n" + +// " ORDER BY COUNT(1) DESC"; +// +// rs = stmt.executeQuery(sql); +// // 处理结果集 +// List lastWeekItemNameList = new ArrayList<>(); +// while (rs.next()) { +// lastWeekItemNameList.add(rs.getString("ITEM_NAME")); +// } +// result.put("lastWeekItemNameList", lastWeekItemNameList); +// sql = "SELECT TOP 20 ITEM_NAME FROM v_MZG020B\n" + +// " WHERE TRX_DATE >= '" + lastMonthDateString + "'\n" + +// " AND TRX_DATE < '" + currentDateString + "'\n" + +// " AND ITEM_NAME != 'Shopping Bag Fee'\n" + +// " GROUP BY ITEM_NAME\n" + +// " ORDER BY COUNT(1) DESC"; +// +// rs = stmt.executeQuery(sql); +// // 处理结果集 +// List lastMonthItemNameList = new ArrayList<>(); +// while (rs.next()) { +// lastMonthItemNameList.add(rs.getString("ITEM_NAME")); +// } +// result.put("lastMonthItemNameList", lastMonthItemNameList); +// +// // 清理环境 +// rs.close(); +// stmt.close(); +// conn.close(); +// } catch (SQLException | ClassNotFoundException e) { +// // 处理异常 +// e.printStackTrace(); +// } +// System.out.println("查询执行完成!"); +// return result; +// } +// +// /** +// * Customer purchase report +// */ +// @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000)) +// @Scheduled(cron = "0 0 23 28-31 * ?") +// public void customerPurchaseReport() { +// final Calendar c = Calendar.getInstance(); +// // 如果是最后一天 则执行定时任务 +// if (c.get(Calendar.DATE) == c.getActualMaximum(Calendar.DATE)) { +// MiTuExport miTuExport = createMiTuExport("Customer purchase report", "month"); +// try { +// List transactionData = retrieveTransactionData(); +// List userMembers = transactionData.stream().map(TransactionData::getUserMember).collect(Collectors.toList()); +// List customerData = retrieveCustomerData(userMembers); +// updateCustomerDataWithTransactionData(customerData, transactionData); +// String filePath = miTuExport.getExportName()+".xlsx"; +// exportToExcelCustomerPurchaseReport(customerData, filePath); +// miTuExport.setUrl("mi-tu/export/" + filePath); +// miTuExport.setStatus(1); +// } catch (Exception e) { +// miTuExport.setStatus(0); +// e.printStackTrace(); +// } finally { +// miTuExportMapper.insert(miTuExport); +// System.out.println("接口执行完成!"); +// } +// } +// } +// +// private MiTuExport createMiTuExport(String fileName, String period) { +// LocalDateTime currentDateTime = LocalDateTime.now(); +// LocalDateTime startTime; +// LocalDateTime endTime; +// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); +// String exportName; +// +// switch (period) { +// case "week": +// startTime = currentDateTime.with(DayOfWeek.MONDAY).with(LocalTime.MIN); +// endTime = currentDateTime.with(DayOfWeek.SUNDAY).with(LocalTime.MAX); +// exportName = fileName + " week " + currentDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd")); +// break; +// case "month": +// startTime = currentDateTime.withDayOfMonth(1).with(LocalTime.MIN); +// endTime = currentDateTime.withDayOfMonth(currentDateTime.toLocalDate().lengthOfMonth()).with(LocalTime.MAX); +// exportName = fileName + " month " + currentDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd")); +// break; +// case "year": +// startTime = currentDateTime.withDayOfYear(1).with(LocalTime.MIN); +// endTime = currentDateTime.withDayOfYear(currentDateTime.toLocalDate().lengthOfYear()).with(LocalTime.MAX); +// exportName = fileName + " year " + currentDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd")); +// break; +// case "quarter": +// // 获取当前季度的第一个月和最后一个月 +// int currentQuarter = (currentDateTime.getMonthValue() - 1) / 3 + 1; +// int firstMonthOfQuarter = (currentQuarter - 1) * 3 + 1; +// int lastMonthOfQuarter = currentQuarter * 3; +// +// // 获取当前季度的起始时间和结束时间 +// startTime = currentDateTime.withMonth(firstMonthOfQuarter).withDayOfMonth(1).with(LocalTime.MIN); +// endTime = currentDateTime.withMonth(lastMonthOfQuarter).with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX); +// +// // 设置导出名称 +// exportName = fileName + " quarter " + currentDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd")); +// break; +// default: +// throw new IllegalArgumentException("Invalid period argument: " + period); +// } +// +// String startOfPeriod = startTime.format(formatter); +// String endOfPeriod = endTime.format(formatter); +// +// MiTuExport miTuExport = new MiTuExport(); +// miTuExport.setExportName(exportName); +// miTuExport.setSpan(period); +// miTuExport.setCreateTime(LocalDateTime.now()); +// miTuExport.setDataTime(startOfPeriod + " - " + endOfPeriod); +// return miTuExport; +// } +// +// +// private void updateCustomerDataWithTransactionData(List customerDataList, List transactionDataList) { +// for (TransactionData transactionDatum : transactionDataList) { +// for (CustomerData customerDatum : customerDataList) { +// if (customerDatum.getMbrCode().equals(transactionDatum.getUserMember())) { +// customerDatum.setThisMonth(transactionDatum.getThisMonth()); +// customerDatum.setMonth1(transactionDatum.getMonth1()); +// customerDatum.setMonth2(transactionDatum.getMonth2()); +// customerDatum.setMonth3(transactionDatum.getMonth3()); +// customerDatum.setMonth4(transactionDatum.getMonth4()); +// customerDatum.setMonth5(transactionDatum.getMonth5()); +// customerDatum.setMonth6(transactionDatum.getMonth6()); +// customerDatum.setMonth7(transactionDatum.getMonth7()); +// customerDatum.setMonth8(transactionDatum.getMonth8()); +// customerDatum.setMonth9(transactionDatum.getMonth9()); +// customerDatum.setMonth10(transactionDatum.getMonth10()); +// customerDatum.setMonth11(transactionDatum.getMonth11()); +// customerDatum.setMonth12(transactionDatum.getMonth12()); +// break; // } // } // } - } - - private void miTuProductSellNumEntry(LocalDate today, List productListAll) { - String endDate = formatDate(today); - String startDate = formatDate(today.minusDays(1)); - for (TProduct product : productListAll) { - String pictureName = product.getPictureName(); - String[] split = pictureName.split("_"); - String itemName = split[0]; - String color = split[1]; - MiTuProductSellNum miTuProductSellNum = getProductSellNum(itemName, color, startDate, endDate); - miTuProductSellNum.setProductId(product.getId()); - miTuProductSellNumMapper.insert(miTuProductSellNum); - } - } - - private MiTuProductSellNum getProductSellNum(String itemName, String color, String startDate, String endDate) { - Connection conn = null; - Statement stmt = null; - MiTuProductSellNum miTuProductSellNum = new MiTuProductSellNum(); - try { - // 注册 JDBC 驱动器 - Class.forName(JDBC_DRIVER); - - // 打开一个连接 - System.out.println("连接数据库..."); - conn = DriverManager.getConnection(DB_URL, USER, PASS); - - // 执行查询 - System.out.println("创建声明..."); - stmt = conn.createStatement(); - String sql; - sql = "SELECT count(1) as num FROM v_MZG020B\n" + - "WHERE TRX_DATE >= '" + startDate + "'\n" + - "AND TRX_DATE < '" + endDate + "'\n" + - "AND ITEM_NAME = '" + itemName + "'\n" + - "AND COLOR = '" + color + "'"; - - ResultSet rs = stmt.executeQuery(sql); - // 处理结果集 - while (rs.next()) { - miTuProductSellNum.setSellNum(rs.getInt("num")); - miTuProductSellNum.setSellDate(LocalDate.parse(startDate)); - } - // 清理环境 - rs.close(); - stmt.close(); - conn.close(); - } catch (SQLException | ClassNotFoundException e) { - // 处理异常 - e.printStackTrace(); - } - System.out.println("查询执行完成!"); - return miTuProductSellNum; - } - - // 格式化日期为字符串 - private static String formatDate(LocalDate date) { - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); - return date.format(formatter); - } - private Map> getBestSellItemNameList() { - Map> result = new HashMap<>(); - // 获取当前日期 - LocalDate currentDate = LocalDate.now(); - // 格式化当前日期 - String currentDateString = formatDate(currentDate); - - // 获取前一天日期 - LocalDate yesterdayDate = currentDate.minusDays(1); - // 格式化前一天日期 - String yesterdayDateString = formatDate(yesterdayDate); - - // 获取前一周日期 - LocalDate lastWeekDate = currentDate.minusWeeks(1); - // 格式化前一周日期 - String lastWeekDateString = formatDate(lastWeekDate); - - // 获取前一个月日期 - LocalDate lastMonthDate = currentDate.minusMonths(1); - // 格式化前一个月日期 - String lastMonthDateString = formatDate(lastMonthDate); - Connection conn = null; - Statement stmt = null; - try { - // 注册 JDBC 驱动器 - Class.forName(JDBC_DRIVER); - - // 打开一个连接 - System.out.println("连接数据库..."); - conn = DriverManager.getConnection(DB_URL, USER, PASS); - - // 执行查询 - System.out.println("创建声明..."); - stmt = conn.createStatement(); - String sql; - sql = "SELECT TOP 20 ITEM_NAME FROM v_MZG020B\n" + - " WHERE TRX_DATE >= '" + yesterdayDateString + "'\n" + - " AND TRX_DATE < '" + currentDateString + "'\n" + - " AND ITEM_NAME != 'Shopping Bag Fee'\n" + - " GROUP BY ITEM_NAME\n" + - " ORDER BY COUNT(1) DESC"; - - ResultSet rs = stmt.executeQuery(sql); - // 处理结果集 - List yesterdayItemNameList = new ArrayList<>(); - while (rs.next()) { - yesterdayItemNameList.add(rs.getString("ITEM_NAME")); - } - result.put("yesterdayItemNameList", yesterdayItemNameList); - sql = "SELECT TOP 20 ITEM_NAME FROM v_MZG020B\n" + - " WHERE TRX_DATE >= '" + lastWeekDateString + "'\n" + - " AND TRX_DATE < '" + currentDateString + "'\n" + - " AND ITEM_NAME != 'Shopping Bag Fee'\n" + - " GROUP BY ITEM_NAME\n" + - " ORDER BY COUNT(1) DESC"; - - rs = stmt.executeQuery(sql); - // 处理结果集 - List lastWeekItemNameList = new ArrayList<>(); - while (rs.next()) { - lastWeekItemNameList.add(rs.getString("ITEM_NAME")); - } - result.put("lastWeekItemNameList", lastWeekItemNameList); - sql = "SELECT TOP 20 ITEM_NAME FROM v_MZG020B\n" + - " WHERE TRX_DATE >= '" + lastMonthDateString + "'\n" + - " AND TRX_DATE < '" + currentDateString + "'\n" + - " AND ITEM_NAME != 'Shopping Bag Fee'\n" + - " GROUP BY ITEM_NAME\n" + - " ORDER BY COUNT(1) DESC"; - - rs = stmt.executeQuery(sql); - // 处理结果集 - List lastMonthItemNameList = new ArrayList<>(); - while (rs.next()) { - lastMonthItemNameList.add(rs.getString("ITEM_NAME")); - } - result.put("lastMonthItemNameList", lastMonthItemNameList); - - // 清理环境 - rs.close(); - stmt.close(); - conn.close(); - } catch (SQLException | ClassNotFoundException e) { - // 处理异常 - e.printStackTrace(); - } - System.out.println("查询执行完成!"); - return result; - } - - /** - * Customer purchase report - */ - @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000)) - @Scheduled(cron = "0 0 23 28-31 * ?") - public void customerPurchaseReport() { - final Calendar c = Calendar.getInstance(); - // 如果是最后一天 则执行定时任务 - if (c.get(Calendar.DATE) == c.getActualMaximum(Calendar.DATE)) { - MiTuExport miTuExport = createMiTuExport("Customer purchase report", "month"); - try { - List transactionData = retrieveTransactionData(); - List userMembers = transactionData.stream().map(TransactionData::getUserMember).collect(Collectors.toList()); - List customerData = retrieveCustomerData(userMembers); - updateCustomerDataWithTransactionData(customerData, transactionData); - String filePath = miTuExport.getExportName()+".xlsx"; - exportToExcelCustomerPurchaseReport(customerData, filePath); - miTuExport.setUrl("mi-tu/export/" + filePath); - miTuExport.setStatus(1); - } catch (Exception e) { - miTuExport.setStatus(0); - e.printStackTrace(); - } finally { - miTuExportMapper.insert(miTuExport); - System.out.println("接口执行完成!"); - } - } - } - - private MiTuExport createMiTuExport(String fileName, String period) { - LocalDateTime currentDateTime = LocalDateTime.now(); - LocalDateTime startTime; - LocalDateTime endTime; - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); - String exportName; - - switch (period) { - case "week": - startTime = currentDateTime.with(DayOfWeek.MONDAY).with(LocalTime.MIN); - endTime = currentDateTime.with(DayOfWeek.SUNDAY).with(LocalTime.MAX); - exportName = fileName + " week " + currentDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd")); - break; - case "month": - startTime = currentDateTime.withDayOfMonth(1).with(LocalTime.MIN); - endTime = currentDateTime.withDayOfMonth(currentDateTime.toLocalDate().lengthOfMonth()).with(LocalTime.MAX); - exportName = fileName + " month " + currentDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd")); - break; - case "year": - startTime = currentDateTime.withDayOfYear(1).with(LocalTime.MIN); - endTime = currentDateTime.withDayOfYear(currentDateTime.toLocalDate().lengthOfYear()).with(LocalTime.MAX); - exportName = fileName + " year " + currentDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd")); - break; - case "quarter": - // 获取当前季度的第一个月和最后一个月 - int currentQuarter = (currentDateTime.getMonthValue() - 1) / 3 + 1; - int firstMonthOfQuarter = (currentQuarter - 1) * 3 + 1; - int lastMonthOfQuarter = currentQuarter * 3; - - // 获取当前季度的起始时间和结束时间 - startTime = currentDateTime.withMonth(firstMonthOfQuarter).withDayOfMonth(1).with(LocalTime.MIN); - endTime = currentDateTime.withMonth(lastMonthOfQuarter).with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX); - - // 设置导出名称 - exportName = fileName + " quarter " + currentDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd")); - break; - default: - throw new IllegalArgumentException("Invalid period argument: " + period); - } - - String startOfPeriod = startTime.format(formatter); - String endOfPeriod = endTime.format(formatter); - - MiTuExport miTuExport = new MiTuExport(); - miTuExport.setExportName(exportName); - miTuExport.setSpan(period); - miTuExport.setCreateTime(LocalDateTime.now()); - miTuExport.setDataTime(startOfPeriod + " - " + endOfPeriod); - return miTuExport; - } - - - private void updateCustomerDataWithTransactionData(List customerDataList, List transactionDataList) { - for (TransactionData transactionDatum : transactionDataList) { - for (CustomerData customerDatum : customerDataList) { - if (customerDatum.getMbrCode().equals(transactionDatum.getUserMember())) { - customerDatum.setThisMonth(transactionDatum.getThisMonth()); - customerDatum.setMonth1(transactionDatum.getMonth1()); - customerDatum.setMonth2(transactionDatum.getMonth2()); - customerDatum.setMonth3(transactionDatum.getMonth3()); - customerDatum.setMonth4(transactionDatum.getMonth4()); - customerDatum.setMonth5(transactionDatum.getMonth5()); - customerDatum.setMonth6(transactionDatum.getMonth6()); - customerDatum.setMonth7(transactionDatum.getMonth7()); - customerDatum.setMonth8(transactionDatum.getMonth8()); - customerDatum.setMonth9(transactionDatum.getMonth9()); - customerDatum.setMonth10(transactionDatum.getMonth10()); - customerDatum.setMonth11(transactionDatum.getMonth11()); - customerDatum.setMonth12(transactionDatum.getMonth12()); - break; - } - } - } - } - - - /** - * New Join VIP report - */ - @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000)) - @Scheduled(cron = "0 0 23 ? * 7") - public void NewJoinVIPReport() { - MiTuExport miTuExport = createMiTuExport("New join vip report", "week"); - try { - List transactionNewCustomerList = getTransactionNewCustomerList(); - String filePath = miTuExport.getExportName()+".xlsx"; - miTuExport.setUrl("mi-tu/export/" + filePath); - exportNewJoinVIPReport(transactionNewCustomerList, filePath); - miTuExport.setStatus(1); - } catch (Exception e) { - miTuExport.setStatus(0); - e.printStackTrace(); - } finally { - miTuExportMapper.insert(miTuExport); - System.out.println("接口执行完成!"); - } - } - - private void exportNewJoinVIPReport(List transactionNewCustomerList, String filePathName) throws IOException { - String currentPath = Paths.get("").toAbsolutePath().toString(); - File file = new File(currentPath + "/" + filePathName); - // 如果文件不存在或者为空,则创建一个新的 Workbook - Workbook workbook = new XSSFWorkbook(); - - // 创建一个新的 Sheet 或获取现有的 Sheet - Sheet sheet = workbook.getSheet("Transaction New Customer"); - if (sheet == null) { - sheet = workbook.createSheet("Transaction New Customer"); - } - - // 创建标题行 - Row headerRow = sheet.getRow(0); - if (headerRow == null) { - headerRow = sheet.createRow(0); - } - - // 写入标题行 - headerRow.createCell(0).setCellValue("shCode"); - headerRow.createCell(1).setCellValue("userMember"); - headerRow.createCell(2).setCellValue("mbrName"); - headerRow.createCell(3).setCellValue("mbrMobile"); - headerRow.createCell(4).setCellValue("joinDate"); - headerRow.createCell(5).setCellValue("salesAmt"); - headerRow.createCell(6).setCellValue("vipGrade"); - headerRow.createCell(7).setCellValue("salesmanName"); - - // 写入数据 - int rowNum = sheet.getLastRowNum() + 1; - for (TransactionNewCustomer transactionNewCustomer : transactionNewCustomerList) { - Row row = sheet.createRow(rowNum++); - - row.createCell(0).setCellValue(transactionNewCustomer.getShCode()); - row.createCell(1).setCellValue(transactionNewCustomer.getUserMember()); - row.createCell(2).setCellValue(transactionNewCustomer.getMbrName()); - row.createCell(3).setCellValue(transactionNewCustomer.getMbrMobile()); - row.createCell(4).setCellValue(transactionNewCustomer.getJoinDate()); - row.createCell(5).setCellValue(transactionNewCustomer.getSalesAmt()); - row.createCell(6).setCellValue(transactionNewCustomer.getVipGrade()); - row.createCell(7).setCellValue(transactionNewCustomer.getSalesmanName()); - } - - // 将Workbook写入文件 - try (FileOutputStream outputStream = new FileOutputStream(file)) { - workbook.write(outputStream); - } - FileItem a = getMultipartFile(file, file.getName()); - MultipartFile multipartFile = new CommonsMultipartFile(a); - minioUtil.upload("mi-tu", "export", multipartFile); - System.out.println("Excel file has been created successfully!"); - } - - - /** - * Weekly Sell Through Report - */ - @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000)) - @Scheduled(cron = "0 0 23 ? * 7") - public void weeklySellThrReport() { - MiTuExport miTuExport = createMiTuExport("Weekly sell thr report", "week"); - try { - List transactionSummaryList = getTransactionSummaryList(); - String filePath = miTuExport.getExportName()+".xlsx"; - miTuExport.setUrl("mi-tu/export/" + filePath); - exportWeeklySellThrReport(transactionSummaryList, filePath); - miTuExport.setStatus(1); - } catch (Exception e) { - miTuExport.setStatus(0); - e.printStackTrace(); - } finally { - miTuExportMapper.insert(miTuExport); - System.out.println("接口执行完成!"); - } - } - - private void exportWeeklySellThrReport(List transactionSummaryList, String filePathName) throws IOException { - String currentPath = Paths.get("").toAbsolutePath().toString(); - File file = new File(currentPath + "/" + filePathName); - // 如果文件不存在或者为空,则创建一个新的 Workbook - Workbook workbook = new XSSFWorkbook(); - - // 创建一个新的 Sheet 或获取现有的 Sheet - Sheet sheet = workbook.getSheet("Weekly Sell Through Report"); - if (sheet == null) { - sheet = workbook.createSheet("Weekly Sell Through Report"); - } - - // 写入标题行 - Row headerRow = sheet.getRow(0); - if (headerRow == null) { - headerRow = sheet.createRow(0); - } - headerRow.createCell(0).setCellValue("PLU_CODE"); - headerRow.createCell(1).setCellValue("ToShopDate"); - headerRow.createCell(2).setCellValue("SalesDay"); - headerRow.createCell(3).setCellValue("CATEGORY"); - headerRow.createCell(4).setCellValue("SUB_CAT"); - headerRow.createCell(5).setCellValue("ItemName"); - headerRow.createCell(6).setCellValue("PriceOriginal"); - headerRow.createCell(7).setCellValue("ToRetailQty"); - headerRow.createCell(8).setCellValue("TotalSaleNum"); - headerRow.createCell(9).setCellValue("MAS_count"); - headerRow.createCell(10).setCellValue("MEL_count"); - headerRow.createCell(11).setCellValue("MPC_count"); - headerRow.createCell(12).setCellValue("MPS_count"); - headerRow.createCell(13).setCellValue("MTF_count"); - headerRow.createCell(14).setCellValue("MWP_count"); - headerRow.createCell(15).setCellValue("MYO_count"); - headerRow.createCell(16).setCellValue("LwSalesQty"); - headerRow.createCell(17).setCellValue("RetailOnHand"); - headerRow.createCell(18).setCellValue("SalesRate"); - - // 写入数据 - int rowNum = sheet.getLastRowNum() + 1; - for (TransactionSummary summary : transactionSummaryList) { - Row row = sheet.createRow(rowNum++); - - row.createCell(0).setCellValue(summary.getPLU_CODE()); - if (summary.getToShopDate() != null) { - row.createCell(1).setCellValue(summary.getToShopDate().toString()); - }else { - row.createCell(1).setCellValue(""); - } - row.createCell(2).setCellValue(summary.getSalesDay()); - row.createCell(3).setCellValue(summary.getCATEGORY()); - row.createCell(4).setCellValue(summary.getSUB_CAT()); - row.createCell(5).setCellValue(summary.getItemName()); - row.createCell(6).setCellValue(summary.getPriceOriginal()); - row.createCell(7).setCellValue(summary.getToRetailQty()); - row.createCell(8).setCellValue(summary.getTotalSaleNum()); - row.createCell(9).setCellValue(summary.getMAS_count()); - row.createCell(10).setCellValue(summary.getMEL_count()); - row.createCell(11).setCellValue(summary.getMPC_count()); - row.createCell(12).setCellValue(summary.getMPS_count()); - row.createCell(13).setCellValue(summary.getMTF_count()); - row.createCell(14).setCellValue(summary.getMWP_count()); - row.createCell(15).setCellValue(summary.getMYO_count()); - row.createCell(16).setCellValue(summary.getLwSalesQty()); - row.createCell(17).setCellValue(summary.getRetailOnHand()); - row.createCell(18).setCellValue(summary.getSalesRate()); - } - - // 将Workbook写入文件 - try (FileOutputStream outputStream = new FileOutputStream(file)) { - workbook.write(outputStream); - } - FileItem a = getMultipartFile(file, file.getName()); - MultipartFile multipartFile = new CommonsMultipartFile(a); - minioUtil.upload("mi-tu", "export", multipartFile); - - System.out.println("Excel file has been created successfully!"); - } - - - /** - * WeeklyHeavyStockReport - */ - @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000)) - @Scheduled(cron = "0 0 23 ? * 7") - public void WeeklyHeavyStockReport() { - MiTuExport miTuExport = createMiTuExport("Weekly heavy stock report", "week"); - try { - List weeklyHeavyStockList = getWeeklyHeavyStockList(); - String filePath = miTuExport.getExportName()+".xlsx"; - miTuExport.setUrl("mi-tu/export/" + filePath); - exportWeeklyHeavyStock(weeklyHeavyStockList, filePath); - miTuExport.setStatus(1); - } catch (Exception e) { - miTuExport.setStatus(0); - e.printStackTrace(); - } finally { - miTuExportMapper.insert(miTuExport); - System.out.println("接口执行完成!"); - } - } - - private void exportWeeklyHeavyStock(List weeklyHeavyStockList, String weeklyHeavyStockReportName) { - String currentPath = Paths.get("").toAbsolutePath().toString(); - File file = new File(currentPath + "/" + weeklyHeavyStockReportName); - // 创建工作簿 - Workbook workbook = new XSSFWorkbook(); - // 创建工作表 - Sheet sheet = workbook.createSheet("Weekly Heavy Stock Report"); - // 创建标题行 - Row headerRow = sheet.createRow(0); - headerRow.createCell(0).setCellValue("PLU_CODE"); - headerRow.createCell(1).setCellValue("item_name"); - headerRow.createCell(2).setCellValue("item_barcode"); - headerRow.createCell(3).setCellValue("cat"); - headerRow.createCell(4).setCellValue("sub_cat"); - headerRow.createCell(5).setCellValue("Col"); - headerRow.createCell(6).setCellValue("price_sales"); - headerRow.createCell(7).setCellValue("MAS"); - headerRow.createCell(8).setCellValue("MEL"); - headerRow.createCell(9).setCellValue("MPC"); - headerRow.createCell(10).setCellValue("MPS"); - headerRow.createCell(11).setCellValue("MTF"); - headerRow.createCell(12).setCellValue("MWP"); - headerRow.createCell(13).setCellValue("MYO"); - headerRow.createCell(14).setCellValue("MHZ"); - headerRow.createCell(15).setCellValue("MRT"); - headerRow.createCell(16).setCellValue("MBZ"); - headerRow.createCell(17).setCellValue("TLT"); - headerRow.createCell(18).setCellValue("subtotal"); - headerRow.createCell(19).setCellValue("G.TLT"); - - // 写入数据 - int rowNum = 1; - for (WeeklyHeavyStock weeklyHeavyStock : weeklyHeavyStockList) { - Row row = sheet.createRow(rowNum++); - row.createCell(0).setCellValue(weeklyHeavyStock.getPLU_CODE()); - row.createCell(1).setCellValue(weeklyHeavyStock.getItem_name()); - row.createCell(2).setCellValue(weeklyHeavyStock.getItem_barcode()); - row.createCell(3).setCellValue(weeklyHeavyStock.getCat()); - row.createCell(4).setCellValue(weeklyHeavyStock.getSub_cat()); - row.createCell(5).setCellValue(weeklyHeavyStock.getCol()); - row.createCell(6).setCellValue(weeklyHeavyStock.getPrice_sales()); - row.createCell(7).setCellValue(weeklyHeavyStock.getMAS()); - row.createCell(8).setCellValue(weeklyHeavyStock.getMEL()); - row.createCell(9).setCellValue(weeklyHeavyStock.getMPC()); - row.createCell(10).setCellValue(weeklyHeavyStock.getMPS()); - row.createCell(11).setCellValue(weeklyHeavyStock.getMTF()); - row.createCell(12).setCellValue(weeklyHeavyStock.getMWP()); - row.createCell(13).setCellValue(weeklyHeavyStock.getMYO()); - row.createCell(14).setCellValue(weeklyHeavyStock.getMHZ()); - row.createCell(15).setCellValue(weeklyHeavyStock.getMRT()); - row.createCell(16).setCellValue(weeklyHeavyStock.getMBZ()); - row.createCell(17).setCellValue(weeklyHeavyStock.getTLT()); - row.createCell(18).setCellValue(weeklyHeavyStock.getSubtotal()); - row.createCell(19).setCellValue(weeklyHeavyStock.getG_TLT()); - } - - // 将工作簿写入文件 - try (FileOutputStream outputStream = new FileOutputStream(file)) { - workbook.write(outputStream); - } catch (IOException e) { - e.printStackTrace(); - } - FileItem a = getMultipartFile(file, file.getName()); - MultipartFile multipartFile = new CommonsMultipartFile(a); - minioUtil.upload("mi-tu", "export", multipartFile); - - System.out.println("Excel file has been created successfully!"); - } - - - /** - * Quarterly Product Grouping Report - */ - @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000)) - @Scheduled(cron = "#{@calculateLastDayOfQuarterCron}") - public void QuarterlyProductGroupingReport() { - MiTuExport miTuExport = createMiTuExport("Quarterly product grouping report", "quarter"); - try { - List QuarterlyProductGroupingList = getQuarterlyProductGroupingList(); - String filePath = miTuExport.getExportName()+".xlsx"; - miTuExport.setUrl("mi-tu/export/" + filePath); - exportQuarterlyProductGrouping(QuarterlyProductGroupingList, filePath); - miTuExport.setStatus(1); - } catch (Exception e) { - miTuExport.setStatus(0); - e.printStackTrace(); - } finally { - miTuExportMapper.insert(miTuExport); - System.out.println("接口执行完成!"); - } - } - @Bean - public String calculateLastDayOfQuarterCron() { - // 获取当前日期 - LocalDate currentDate = LocalDate.now(); - - // 计算下一个季度的最后一天 - Month currentMonth = currentDate.getMonth(); - Month nextQuarterStartMonth = currentMonth.plus(3 - currentMonth.getValue() % 3); - LocalDate nextQuarterStartDate = LocalDate.of(currentDate.getYear(), nextQuarterStartMonth, 1); - LocalDate lastDayOfNextQuarter = nextQuarterStartDate.with(TemporalAdjusters.lastDayOfMonth()); - - // 将日期转换成 Cron 表达式的格式 - int year = lastDayOfNextQuarter.getYear(); - int month = lastDayOfNextQuarter.getMonthValue(); - int dayOfMonth = lastDayOfNextQuarter.getDayOfMonth(); - return String.format("0 0 22 %d %d ?", dayOfMonth, month); - } - - private void exportQuarterlyProductGrouping(List weeklyHeavyStockList, String weeklyHeavyStockReportName) { - String currentPath = Paths.get("").toAbsolutePath().toString(); - File file = new File(currentPath + "/" + weeklyHeavyStockReportName); - // 创建工作簿 - Workbook workbook = new XSSFWorkbook(); - // 创建工作表 - Sheet sheet = workbook.createSheet("Quarterly Product Grouping Report"); - // 创建标题行 - Row headerRow = sheet.createRow(0); - headerRow.createCell(0).setCellValue("PLU_CODE"); - headerRow.createCell(1).setCellValue("Col"); - headerRow.createCell(2).setCellValue("Size"); - headerRow.createCell(3).setCellValue("item_barcode"); - headerRow.createCell(4).setCellValue("item_name"); - headerRow.createCell(5).setCellValue("cat"); - headerRow.createCell(6).setCellValue("sub_cat"); - headerRow.createCell(7).setCellValue("price_sales"); - headerRow.createCell(8).setCellValue("MASSOLD"); - headerRow.createCell(9).setCellValue("MELSOLD"); - headerRow.createCell(10).setCellValue("MPCSOLD"); - headerRow.createCell(11).setCellValue("MPSSOLD"); - headerRow.createCell(12).setCellValue("MTFSOLD"); - headerRow.createCell(13).setCellValue("MWPSOLD"); - headerRow.createCell(14).setCellValue("MYOSOLD"); - headerRow.createCell(15).setCellValue("MAS"); - headerRow.createCell(16).setCellValue("MEL"); - headerRow.createCell(17).setCellValue("MPC"); - headerRow.createCell(18).setCellValue("MPS"); - headerRow.createCell(19).setCellValue("MTF"); - headerRow.createCell(20).setCellValue("MWP"); - headerRow.createCell(21).setCellValue("MYO"); - headerRow.createCell(22).setCellValue("MHZ"); - headerRow.createCell(23).setCellValue("MRT"); - headerRow.createCell(24).setCellValue("MBZ"); - - // 写入数据 - int rowNum = 1; - for (WeeklyHeavyStock weeklyHeavyStock : weeklyHeavyStockList) { - Row row = sheet.createRow(rowNum++); - row.createCell(0).setCellValue(weeklyHeavyStock.getPLU_CODE()); - row.createCell(1).setCellValue(weeklyHeavyStock.getCol()); - row.createCell(2).setCellValue(weeklyHeavyStock.getSize()); - row.createCell(3).setCellValue(weeklyHeavyStock.getItem_barcode()); - row.createCell(4).setCellValue(weeklyHeavyStock.getItem_name()); - row.createCell(5).setCellValue(weeklyHeavyStock.getCat()); - row.createCell(6).setCellValue(weeklyHeavyStock.getSub_cat()); - row.createCell(7).setCellValue(weeklyHeavyStock.getPrice_sales()); - row.createCell(8).setCellValue(weeklyHeavyStock.getMASSOLD()); - row.createCell(9).setCellValue(weeklyHeavyStock.getMELSOLD()); - row.createCell(10).setCellValue(weeklyHeavyStock.getMPCSOLD()); - row.createCell(11).setCellValue(weeklyHeavyStock.getMPSSOLD()); - row.createCell(12).setCellValue(weeklyHeavyStock.getMTFSOLD()); - row.createCell(13).setCellValue(weeklyHeavyStock.getMWPSOLD()); - row.createCell(14).setCellValue(weeklyHeavyStock.getMYOSOLD()); - row.createCell(15).setCellValue(weeklyHeavyStock.getMAS()); - row.createCell(16).setCellValue(weeklyHeavyStock.getMEL()); - row.createCell(17).setCellValue(weeklyHeavyStock.getMPC()); - row.createCell(18).setCellValue(weeklyHeavyStock.getMPS()); - row.createCell(19).setCellValue(weeklyHeavyStock.getMTF()); - row.createCell(20).setCellValue(weeklyHeavyStock.getMWP()); - row.createCell(21).setCellValue(weeklyHeavyStock.getMYO()); - row.createCell(22).setCellValue(weeklyHeavyStock.getMHZ()); - row.createCell(23).setCellValue(weeklyHeavyStock.getMRT()); - row.createCell(24).setCellValue(weeklyHeavyStock.getMBZ()); - } - - // 将工作簿写入文件 - try (FileOutputStream outputStream = new FileOutputStream(file)) { - workbook.write(outputStream); - } catch (IOException e) { - e.printStackTrace(); - } - FileItem a = getMultipartFile(file, file.getName()); - MultipartFile multipartFile = new CommonsMultipartFile(a); - minioUtil.upload("mi-tu", "export", multipartFile); - - System.out.println("Excel file has been created successfully!"); - } - - - - private static List getQuarterlyProductGroupingList() { - List QuarterlyProductGroupingList = new ArrayList<>(); - Connection conn = null; - Statement stmt = null; - try { - Class.forName(JDBC_DRIVER); - conn = DriverManager.getConnection(DB_URL, USER, PASS); - stmt = conn.createStatement(); - - String sql = "WITH CountedData AS (\n" + - " SELECT\n" + - " WHCODE,\n" + - " PLU_CODE,\n" + - " item_name,\n" + - " item_barcode,\n" + - " cat,\n" + - " sub_cat,\n" + - " Col,\n" + - " Size,\n" + - " price_sales,\n" + - " CASE WHEN WHCODE = 'MAS' THEN 1 ELSE 0 END AS MAS,\n" + - " CASE WHEN WHCODE = 'MEL' THEN 1 ELSE 0 END AS MEL,\n" + - " CASE WHEN WHCODE = 'MPC' THEN 1 ELSE 0 END AS MPC,\n" + - " CASE WHEN WHCODE = 'MPS' THEN 1 ELSE 0 END AS MPS,\n" + - " CASE WHEN WHCODE = 'MTF' THEN 1 ELSE 0 END AS MTF,\n" + - " CASE WHEN WHCODE = 'MWP' THEN 1 ELSE 0 END AS MWP,\n" + - " CASE WHEN WHCODE = 'MYO' THEN 1 ELSE 0 END AS MYO,\n" + - " CASE WHEN WHCODE = 'MHZ' THEN 1 ELSE 0 END AS MHZ,\n" + - " CASE WHEN WHCODE = 'MRT' THEN 1 ELSE 0 END AS MRT,\n" + - " CASE WHEN WHCODE = 'MBZ' THEN 1 ELSE 0 END AS MBZ\n" + - " FROM\n" + - " (\n" + - " SELECT\n" + - " WHCODE,\n" + - " PLU_CODE,\n" + - " item_name,\n" + - " item_code AS item_barcode,\n" + - " CATEGORY AS cat,\n" + - " SUB_CAT AS sub_cat,\n" + - " COLOR AS Col,\n" + - " SIZE AS SIZE,\n" + - " price_original as price_sales\n" + - " FROM\n" + - " v_MZG003A \n" + - " WHERE\n" + - " PLU_CODE IN (SELECT TOP 3\n" + - "PLU_CODE\n" + - "FROM v_MZG020B\n" + - "WHERE TRX_DATE >= '2024-03-04'\n" + - "AND TRX_DATE <= '2024-03-10'\n" + - "AND SH_CODE IN ('MAS', 'MEL', 'MPC', 'MPS', 'MTF', 'MWP', 'MYO')\n" + - "GROUP BY PLU_CODE\n" + - "ORDER BY count(1) desc)\n" + - " AND item_active = 'Y' \n" + - " AND WHCODE IN ('MAS', 'MEL', 'MPC', 'MPS', 'MTF', 'MWP', 'MYO', 'MHZ', 'MRT', 'MBZ')\n" + - " ) AS v \n" + - ") \n" + - "SELECT\n" + - " PLU_CODE,\n" + - " item_name,\n" + - " item_barcode,\n" + - " cat,\n" + - " sub_cat,\n" + - " Col,\n" + - " Size,\n" + - " price_sales,\n" + - " SUM(MAS) AS MAS,\n" + - " SUM(MEL) AS MEL,\n" + - " SUM(MPC) AS MPC,\n" + - " SUM(MPS) AS MPS,\n" + - " SUM(MTF) AS MTF,\n" + - " SUM(MWP) AS MWP,\n" + - " SUM(MYO) AS MYO,\n" + - " SUM(MHZ) AS MHZ,\n" + - " SUM(MRT) AS MRT,\n" + - " SUM(MBZ) AS MBZ\n" + - "FROM\n" + - " CountedData \n" + - "GROUP BY\n" + - " PLU_CODE,\n" + - " item_name,\n" + - " item_barcode,\n" + - " cat,\n" + - " sub_cat,\n" + - " Col,\n" + - " Size,\n" + - " price_sales"; - - ResultSet rs = stmt.executeQuery(sql); - - while (rs.next()) { - WeeklyHeavyStock weeklyHeavyStock = new WeeklyHeavyStock(); - weeklyHeavyStock.setPLU_CODE(rs.getString("PLU_CODE")); - weeklyHeavyStock.setItem_name(rs.getString("item_name")); - weeklyHeavyStock.setItem_barcode(rs.getString("item_barcode")); - weeklyHeavyStock.setCat(rs.getString("cat")); - weeklyHeavyStock.setSub_cat(rs.getString("sub_cat")); - weeklyHeavyStock.setCol(rs.getString("Col")); - weeklyHeavyStock.setSize(rs.getString("Size")); - weeklyHeavyStock.setPrice_sales(rs.getDouble("price_sales")); - weeklyHeavyStock.setMAS(rs.getInt("MAS")); - weeklyHeavyStock.setMEL(rs.getInt("MEL")); - weeklyHeavyStock.setMPC(rs.getInt("MPC")); - weeklyHeavyStock.setMPS(rs.getInt("MPS")); - weeklyHeavyStock.setMTF(rs.getInt("MTF")); - weeklyHeavyStock.setMWP(rs.getInt("MWP")); - weeklyHeavyStock.setMYO(rs.getInt("MYO")); - weeklyHeavyStock.setMHZ(rs.getInt("MHZ")); - weeklyHeavyStock.setMRT(rs.getInt("MRT")); - weeklyHeavyStock.setMBZ(rs.getInt("MBZ")); - QuarterlyProductGroupingList.add(weeklyHeavyStock); - } - sql = "SELECT\n" + - "PLU_CODE,\n" + - "ITEM_CODE,\n" + - "SH_CODE\n" + - "FROM\n" + - "v_MZG020B\n" + - "WHERE TRX_DATE >= '2024-03-04'\n" + - "AND TRX_DATE <= '2024-03-10'\n" + - "AND PLU_CODE IN (SELECT TOP 3\n" + - "PLU_CODE\n" + - "FROM v_MZG020B\n" + - "WHERE TRX_DATE >= '2024-03-04'\n" + - "AND TRX_DATE <= '2024-03-10'\n" + - "AND SH_CODE IN ('MAS', 'MEL', 'MPC', 'MPS', 'MTF', 'MWP', 'MYO')\n" + - "GROUP BY PLU_CODE\n" + - "ORDER BY count(1) desc)"; - - rs = stmt.executeQuery(sql); - - List relationShCodeList = new ArrayList<>(); - while (rs.next()) { - WeeklyHeavyStock weeklyHeavyStock = new WeeklyHeavyStock(); - weeklyHeavyStock.setPLU_CODE(rs.getString("PLU_CODE")); - weeklyHeavyStock.setItem_barcode(rs.getString("ITEM_CODE")); - weeklyHeavyStock.setSH_CODE(rs.getString("SH_CODE")); - relationShCodeList.add(weeklyHeavyStock); - } - for (WeeklyHeavyStock relationShCode : relationShCodeList) { - for (WeeklyHeavyStock quarterlyProductGrouping : QuarterlyProductGroupingList) { - if (quarterlyProductGrouping.getItem_barcode().equals(relationShCode.getItem_barcode())) { - if (relationShCode.getSH_CODE().equals("MPS ")) { - quarterlyProductGrouping.setMASSOLD(quarterlyProductGrouping.getMASSOLD() + 1); - } - else if (relationShCode.getSH_CODE().equals("MEL ")) { - quarterlyProductGrouping.setMELSOLD(quarterlyProductGrouping.getMELSOLD() + 1); - } - else if (relationShCode.getSH_CODE().equals("MPC ")) { - quarterlyProductGrouping.setMPCSOLD(quarterlyProductGrouping.getMPCSOLD() + 1); - } - else if (relationShCode.getSH_CODE().equals("MPS ")) { - quarterlyProductGrouping.setMPSSOLD(quarterlyProductGrouping.getMPSSOLD() + 1); - } - else if (relationShCode.getSH_CODE().equals("MTF ")) { - quarterlyProductGrouping.setMTFSOLD(quarterlyProductGrouping.getMTFSOLD() + 1); - } - else if (relationShCode.getSH_CODE().equals("MWP ")) { - quarterlyProductGrouping.setMWPSOLD(quarterlyProductGrouping.getMWPSOLD() + 1); - } - else if (relationShCode.getSH_CODE().equals("MYO ")) { - quarterlyProductGrouping.setMYOSOLD(quarterlyProductGrouping.getMYOSOLD() + 1); - } - } - } - } - rs.close(); - stmt.close(); - conn.close(); - } catch (SQLException | ClassNotFoundException se) { - se.printStackTrace(); - } finally { - try { - if (stmt != null) stmt.close(); - } catch (SQLException ignored) {} - try { - if (conn != null) conn.close(); - } catch (SQLException se) { - se.printStackTrace(); - } - } - System.out.println("Query execution completed!"); - return QuarterlyProductGroupingList; - } - - private static List getWeeklyHeavyStockList() { - List weeklyHeavyStockList = new ArrayList<>(); - Connection conn = null; - Statement stmt = null; - try { - Class.forName(JDBC_DRIVER); - conn = DriverManager.getConnection(DB_URL, USER, PASS); - stmt = conn.createStatement(); - - String sql = "WITH CountedData AS (\n" + - " SELECT\n" + - " WHCODE,\n" + - " PLU_CODE,\n" + - " item_name,\n" + - " item_barcode,\n" + - " cat,\n" + - " sub_cat,\n" + - " Col,\n" + - " price_sales,\n" + - " CASE WHEN WHCODE = 'MAS' THEN 1 ELSE 0 END AS MAS,\n" + - " CASE WHEN WHCODE = 'MEL' THEN 1 ELSE 0 END AS MEL,\n" + - " CASE WHEN WHCODE = 'MPC' THEN 1 ELSE 0 END AS MPC,\n" + - " CASE WHEN WHCODE = 'MPS' THEN 1 ELSE 0 END AS MPS,\n" + - " CASE WHEN WHCODE = 'MTF' THEN 1 ELSE 0 END AS MTF,\n" + - " CASE WHEN WHCODE = 'MWP' THEN 1 ELSE 0 END AS MWP,\n" + - " CASE WHEN WHCODE = 'MYO' THEN 1 ELSE 0 END AS MYO,\n" + - " CASE WHEN WHCODE = 'MHZ' THEN 1 ELSE 0 END AS MHZ,\n" + - " CASE WHEN WHCODE = 'MRT' THEN 1 ELSE 0 END AS MRT,\n" + - " CASE WHEN WHCODE = 'MBZ' THEN 1 ELSE 0 END AS MBZ,\n" + - " SUM(1) OVER (PARTITION BY PLU_CODE, Col) AS subtotal,\n" + - " SUM(1) OVER (PARTITION BY PLU_CODE) AS \"G.TLT\"\n" + - " FROM\n" + - " (\n" + - " SELECT\n" + - " WHCODE,\n" + - " PLU_CODE,\n" + - " item_name,\n" + - " item_code AS item_barcode,\n" + - " CATEGORY AS cat,\n" + - " SUB_CAT AS sub_cat,\n" + - " COLOR AS Col,\n" + - " SIZE AS SIZE,\n" + - " price_original as price_sales\n" + - " FROM\n" + - " v_MZG003A \n" + - " WHERE\n" + - " PLU_CODE IN (\n" + - " SELECT TOP 30 PLU_CODE \n" + - " FROM v_MZG003A \n" + - " WHERE item_active = 'Y' \n" + - " GROUP BY PLU_CODE \n" + - " ORDER BY COUNT ( 1 ) DESC\n" + - " ) \n" + - " AND item_active = 'Y' \n" + - " AND WHCODE IN ('MAS', 'MEL', 'MPC', 'MPS', 'MTF', 'MWP', 'MYO', 'MHZ', 'MRT', 'MBZ')\n" + - " ) AS v \n" + - ") \n" + - "SELECT\n" + - " PLU_CODE,\n" + - " item_name,\n" + - " item_barcode,\n" + - " cat,\n" + - " sub_cat,\n" + - " Col,\n" + - " price_sales,\n" + - " SUM(MAS) AS MAS,\n" + - " SUM(MEL) AS MEL,\n" + - " SUM(MPC) AS MPC,\n" + - " SUM(MPS) AS MPS,\n" + - " SUM(MTF) AS MTF,\n" + - " SUM(MWP) AS MWP,\n" + - " SUM(MYO) AS MYO,\n" + - " SUM(MHZ) AS MHZ,\n" + - " SUM(MRT) AS MRT,\n" + - " SUM(MBZ) AS MBZ,\n" + - " SUM(MAS + MEL + MPC + MPS + MTF + MWP + MYO + MHZ + MRT + MBZ) AS TLT,\n" + - " subtotal,\n" + - " \"G.TLT\"\n" + - "FROM\n" + - " CountedData \n" + - "GROUP BY\n" + - " PLU_CODE,\n" + - " item_name,\n" + - " item_barcode,\n" + - " cat,\n" + - " sub_cat,\n" + - " Col,\n" + - " price_sales,\n" + - " subtotal,\n" + - " \"G.TLT\"\n" + - "ORDER BY \"G.TLT\" DESC, PLU_CODE ASC;\n"; - - ResultSet rs = stmt.executeQuery(sql); - - while (rs.next()) { - WeeklyHeavyStock weeklyHeavyStock = new WeeklyHeavyStock(); - weeklyHeavyStock.setPLU_CODE(rs.getString("PLU_CODE")); - weeklyHeavyStock.setItem_name(rs.getString("item_name")); - weeklyHeavyStock.setItem_barcode(rs.getString("item_barcode")); - weeklyHeavyStock.setCat(rs.getString("cat")); - weeklyHeavyStock.setSub_cat(rs.getString("sub_cat")); - weeklyHeavyStock.setCol(rs.getString("Col")); - weeklyHeavyStock.setPrice_sales(rs.getDouble("price_sales")); - weeklyHeavyStock.setMAS(rs.getInt("MAS")); - weeklyHeavyStock.setMEL(rs.getInt("MEL")); - weeklyHeavyStock.setMPC(rs.getInt("MPC")); - weeklyHeavyStock.setMPS(rs.getInt("MPS")); - weeklyHeavyStock.setMTF(rs.getInt("MTF")); - weeklyHeavyStock.setMWP(rs.getInt("MWP")); - weeklyHeavyStock.setMYO(rs.getInt("MYO")); - weeklyHeavyStock.setMHZ(rs.getInt("MHZ")); - weeklyHeavyStock.setMRT(rs.getInt("MRT")); - weeklyHeavyStock.setMBZ(rs.getInt("MBZ")); - weeklyHeavyStock.setTLT(rs.getInt("TLT")); - weeklyHeavyStock.setSubtotal(rs.getInt("subtotal")); - weeklyHeavyStock.setG_TLT(rs.getInt("G.TLT")); - weeklyHeavyStockList.add(weeklyHeavyStock); - } - - rs.close(); - stmt.close(); - conn.close(); - } catch (SQLException | ClassNotFoundException se) { - se.printStackTrace(); - } finally { - try { - if (stmt != null) stmt.close(); - } catch (SQLException ignored) {} - try { - if (conn != null) conn.close(); - } catch (SQLException se) { - se.printStackTrace(); - } - } - System.out.println("Query execution completed!"); - return weeklyHeavyStockList; - } - - private static List getTransactionSummaryList() { - List transactionSummaryList = new ArrayList<>(); - Connection conn = null; - Statement stmt = null; - try { - // 注册 JDBC 驱动器 - Class.forName(JDBC_DRIVER); - - // 打开一个连接 - System.out.println("连接数据库..."); - conn = DriverManager.getConnection(DB_URL, USER, PASS); - - // 执行查询 - System.out.println("创建声明..."); - stmt = conn.createStatement(); - String sql; - - // 获取当前日期 - LocalDate currentDate = LocalDate.now(); - - // 获取当前星期的第一天(星期一)和最后一天(星期日) - LocalDate firstDayOfWeek = currentDate.with(DayOfWeek.MONDAY); - LocalDate lastDayOfWeek = currentDate.with(DayOfWeek.SUNDAY); - - // 格式化日期字符串 - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); - String firstDayOfWeekStr = formatter.format(firstDayOfWeek); - String lastDayOfWeekStr = formatter.format(lastDayOfWeek); - // 构建完整的 SQL 查询语句 - sql = "SELECT TOP 30\n" + - " PLU_CODE,\n" + - " ITEM_NAME,\n" + - " CATEGORY,\n" + - " SUB_CAT,\n" + - " PRICE_ORIGINAL,\n" + - " COUNT(1) AS totalSaleNum,\n" + - " SUM(CASE WHEN SH_CODE = 'MAS' THEN 1 ELSE 0 END) AS MAS,\n" + - " SUM(CASE WHEN SH_CODE = 'MEL' THEN 1 ELSE 0 END) AS MEL,\n" + - " SUM(CASE WHEN SH_CODE = 'MPC' THEN 1 ELSE 0 END) AS MPC,\n" + - " SUM(CASE WHEN SH_CODE = 'MPS' THEN 1 ELSE 0 END) AS MPS,\n" + - " SUM(CASE WHEN SH_CODE = 'MTF' THEN 1 ELSE 0 END) AS MTF,\n" + - " SUM(CASE WHEN SH_CODE = 'MWP' THEN 1 ELSE 0 END) AS MWP,\n" + - " SUM(CASE WHEN SH_CODE = 'MYO' THEN 1 ELSE 0 END) AS MYO\n" + - "FROM\n" + - " v_MZG020B\n" + - "WHERE\n" + - " TRX_DATE >= '" + firstDayOfWeekStr + "' \n" + - " AND TRX_DATE <= '" + lastDayOfWeekStr + "' \n" + - "GROUP BY\n" + - " PLU_CODE, ITEM_NAME, CATEGORY, SUB_CAT, PRICE_ORIGINAL\n" + - "ORDER BY \n" + - " totalSaleNum DESC, PLU_CODE;"; - ResultSet rs = stmt.executeQuery(sql); - - // 处理结果集 - while (rs.next()) { - TransactionSummary summary = new TransactionSummary(); - summary.setPLU_CODE(rs.getString("PLU_CODE")); - summary.setItemName(rs.getString("ITEM_NAME")); - summary.setCATEGORY(rs.getString("CATEGORY")); - summary.setSUB_CAT(rs.getString("SUB_CAT")); - summary.setPriceOriginal(rs.getDouble("PRICE_ORIGINAL")); - summary.setTotalSaleNum(rs.getInt("totalSaleNum")); - summary.setMAS_count(rs.getInt("MAS")); - summary.setMEL_count(rs.getInt("MEL")); - summary.setMPC_count(rs.getInt("MPC")); - summary.setMPS_count(rs.getInt("MPS")); - summary.setMTF_count(rs.getInt("MTF")); - summary.setMWP_count(rs.getInt("MWP")); - summary.setMYO_count(rs.getInt("MYO")); - transactionSummaryList.add(summary); - } - - List pluCodeList = transactionSummaryList.stream().map(TransactionSummary::getPLU_CODE).collect(Collectors.toList()); - StringBuilder inClause = new StringBuilder("("); - for (String pluCode : pluCodeList) { - inClause.append("'").append(pluCode).append("',"); - } - inClause.deleteCharAt(inClause.length() - 1); // 删除最后一个逗号 - inClause.append(")"); - - // 构建完整的 SQL 查询语句 - sql = "SELECT\n" + - "PLU_CODE,\n" + - "COUNT(1) AS retailOnHand\n" + - "FROM v_MZG003A\n" + - "WHERE PLU_CODE IN " + inClause.toString() + "\n" + - "GROUP BY PLU_CODE;"; - rs = stmt.executeQuery(sql); - List retailOnHandList = new ArrayList<>(); - while (rs.next()) { - TransactionSummary summary = new TransactionSummary(); - summary.setPLU_CODE(rs.getString("PLU_CODE")); - summary.setRetailOnHand(rs.getInt("retailOnHand")); - retailOnHandList.add(summary); - } - for (TransactionSummary transactionSummary : transactionSummaryList) { - for (TransactionSummary summary : retailOnHandList) { - if (transactionSummary.getPLU_CODE().equals(summary.getPLU_CODE())) { - transactionSummary.setRetailOnHand(summary.getRetailOnHand()); - break; - } - } - } - - sql = "SELECT plu_code,toshopdate,lastwk_salesqty FROM v_MZG019C WHERE plu_code in" + inClause; - rs = stmt.executeQuery(sql); - List toShopDateList = new ArrayList<>(); - while (rs.next()) { - TransactionSummary summary = new TransactionSummary(); - summary.setPLU_CODE(rs.getString("plu_code")); - summary.setToShopDate(rs.getDate("toshopdate")); - summary.setLwSalesQty(rs.getInt("lastwk_salesqty")); - toShopDateList.add(summary); - } - - for (TransactionSummary transactionSummary : transactionSummaryList) { - for (TransactionSummary toShopDate : toShopDateList) { - if (transactionSummary.getPLU_CODE().equals(toShopDate.getPLU_CODE())) { - transactionSummary.setToShopDate(toShopDate.getToShopDate()); - transactionSummary.setSalesDay(calculateDaysDifference(toShopDate.getToShopDate())); - transactionSummary.setLwSalesQty(toShopDate.getLwSalesQty()); - break; - } - } - } - - for (TransactionSummary transactionSummary : transactionSummaryList) { - int ttlSalesQty = transactionSummary.getTotalSaleNum(); - int retailOnHand = transactionSummary.getRetailOnHand(); - int toRetailQty = ttlSalesQty + retailOnHand; - - double result = (double) ttlSalesQty / toRetailQty; - // 将结果四舍五入到两位小数 - double roundedResult = Math.round(result * 100.0) / 100.0; - // 将销售率转换为百分比形式 - String salesRate = String.format("%.0f%%", roundedResult * 100); - - transactionSummary.setToRetailQty(toRetailQty); - transactionSummary.setSalesRate(salesRate); - } - - // 清理环境 - rs.close(); - stmt.close(); - conn.close(); - } catch (SQLException se) { - // 处理 JDBC 错误 - se.printStackTrace(); - } catch (Exception e) { - // 处理 Class.forName 错误 - e.printStackTrace(); - } finally { - // 关闭资源 - try { - if (stmt != null) stmt.close(); - } catch (SQLException se2) { - } // 什么都不做 - try { - if (conn != null) conn.close(); - } catch (SQLException se) { - se.printStackTrace(); - } - } - System.out.println("查询执行完成!"); - return transactionSummaryList; - } - - public static int calculateDaysDifference(Date toShopDate) { - // 获取当前时间的毫秒数 - long currentTimeMillis = System.currentTimeMillis(); - - // 获取toShopDate的毫秒数 - long toShopTimeMillis = toShopDate.getTime(); - - // 计算毫秒差,并转换为天数,并向上取整 - int daysDifference = (int) Math.ceil((currentTimeMillis - toShopTimeMillis) / (1000.0 * 60 * 60 * 24)); - - return daysDifference; - } - - private static List getCustomerData() { - List customerDataList = new ArrayList<>(); - Connection conn = null; - Statement stmt = null; - try { - // 注册 JDBC 驱动器 - Class.forName(JDBC_DRIVER); - - // 打开一个连接 - System.out.println("连接数据库..."); - conn = DriverManager.getConnection(DB_URL, USER, PASS); - - // 执行查询 - System.out.println("创建声明..."); - stmt = conn.createStatement(); - String sql; +// } +// +// +// /** +// * New Join VIP report +// */ +// @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000)) +// @Scheduled(cron = "0 0 23 ? * 7") +// public void NewJoinVIPReport() { +// MiTuExport miTuExport = createMiTuExport("New join vip report", "week"); +// try { +// List transactionNewCustomerList = getTransactionNewCustomerList(); +// String filePath = miTuExport.getExportName()+".xlsx"; +// miTuExport.setUrl("mi-tu/export/" + filePath); +// exportNewJoinVIPReport(transactionNewCustomerList, filePath); +// miTuExport.setStatus(1); +// } catch (Exception e) { +// miTuExport.setStatus(0); +// e.printStackTrace(); +// } finally { +// miTuExportMapper.insert(miTuExport); +// System.out.println("接口执行完成!"); +// } +// } +// +// private void exportNewJoinVIPReport(List transactionNewCustomerList, String filePathName) throws IOException { +// String currentPath = Paths.get("").toAbsolutePath().toString(); +// File file = new File(currentPath + "/" + filePathName); +// // 如果文件不存在或者为空,则创建一个新的 Workbook +// Workbook workbook = new XSSFWorkbook(); +// +// // 创建一个新的 Sheet 或获取现有的 Sheet +// Sheet sheet = workbook.getSheet("Transaction New Customer"); +// if (sheet == null) { +// sheet = workbook.createSheet("Transaction New Customer"); +// } +// +// // 创建标题行 +// Row headerRow = sheet.getRow(0); +// if (headerRow == null) { +// headerRow = sheet.createRow(0); +// } +// +// // 写入标题行 +// headerRow.createCell(0).setCellValue("shCode"); +// headerRow.createCell(1).setCellValue("userMember"); +// headerRow.createCell(2).setCellValue("mbrName"); +// headerRow.createCell(3).setCellValue("mbrMobile"); +// headerRow.createCell(4).setCellValue("joinDate"); +// headerRow.createCell(5).setCellValue("salesAmt"); +// headerRow.createCell(6).setCellValue("vipGrade"); +// headerRow.createCell(7).setCellValue("salesmanName"); +// +// // 写入数据 +// int rowNum = sheet.getLastRowNum() + 1; +// for (TransactionNewCustomer transactionNewCustomer : transactionNewCustomerList) { +// Row row = sheet.createRow(rowNum++); +// +// row.createCell(0).setCellValue(transactionNewCustomer.getShCode()); +// row.createCell(1).setCellValue(transactionNewCustomer.getUserMember()); +// row.createCell(2).setCellValue(transactionNewCustomer.getMbrName()); +// row.createCell(3).setCellValue(transactionNewCustomer.getMbrMobile()); +// row.createCell(4).setCellValue(transactionNewCustomer.getJoinDate()); +// row.createCell(5).setCellValue(transactionNewCustomer.getSalesAmt()); +// row.createCell(6).setCellValue(transactionNewCustomer.getVipGrade()); +// row.createCell(7).setCellValue(transactionNewCustomer.getSalesmanName()); +// } +// +// // 将Workbook写入文件 +// try (FileOutputStream outputStream = new FileOutputStream(file)) { +// workbook.write(outputStream); +// } +// FileItem a = getMultipartFile(file, file.getName()); +// MultipartFile multipartFile = new CommonsMultipartFile(a); +// minioUtil.upload("mi-tu", "export", multipartFile); +// System.out.println("Excel file has been created successfully!"); +// } +// +// +// /** +// * Weekly Sell Through Report +// */ +// @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000)) +// @Scheduled(cron = "0 0 23 ? * 7") +// public void weeklySellThrReport() { +// MiTuExport miTuExport = createMiTuExport("Weekly sell thr report", "week"); +// try { +// List transactionSummaryList = getTransactionSummaryList(); +// String filePath = miTuExport.getExportName()+".xlsx"; +// miTuExport.setUrl("mi-tu/export/" + filePath); +// exportWeeklySellThrReport(transactionSummaryList, filePath); +// miTuExport.setStatus(1); +// } catch (Exception e) { +// miTuExport.setStatus(0); +// e.printStackTrace(); +// } finally { +// miTuExportMapper.insert(miTuExport); +// System.out.println("接口执行完成!"); +// } +// } +// +// private void exportWeeklySellThrReport(List transactionSummaryList, String filePathName) throws IOException { +// String currentPath = Paths.get("").toAbsolutePath().toString(); +// File file = new File(currentPath + "/" + filePathName); +// // 如果文件不存在或者为空,则创建一个新的 Workbook +// Workbook workbook = new XSSFWorkbook(); +// +// // 创建一个新的 Sheet 或获取现有的 Sheet +// Sheet sheet = workbook.getSheet("Weekly Sell Through Report"); +// if (sheet == null) { +// sheet = workbook.createSheet("Weekly Sell Through Report"); +// } +// +// // 写入标题行 +// Row headerRow = sheet.getRow(0); +// if (headerRow == null) { +// headerRow = sheet.createRow(0); +// } +// headerRow.createCell(0).setCellValue("PLU_CODE"); +// headerRow.createCell(1).setCellValue("ToShopDate"); +// headerRow.createCell(2).setCellValue("SalesDay"); +// headerRow.createCell(3).setCellValue("CATEGORY"); +// headerRow.createCell(4).setCellValue("SUB_CAT"); +// headerRow.createCell(5).setCellValue("ItemName"); +// headerRow.createCell(6).setCellValue("PriceOriginal"); +// headerRow.createCell(7).setCellValue("ToRetailQty"); +// headerRow.createCell(8).setCellValue("TotalSaleNum"); +// headerRow.createCell(9).setCellValue("MAS_count"); +// headerRow.createCell(10).setCellValue("MEL_count"); +// headerRow.createCell(11).setCellValue("MPC_count"); +// headerRow.createCell(12).setCellValue("MPS_count"); +// headerRow.createCell(13).setCellValue("MTF_count"); +// headerRow.createCell(14).setCellValue("MWP_count"); +// headerRow.createCell(15).setCellValue("MYO_count"); +// headerRow.createCell(16).setCellValue("LwSalesQty"); +// headerRow.createCell(17).setCellValue("RetailOnHand"); +// headerRow.createCell(18).setCellValue("SalesRate"); +// +// // 写入数据 +// int rowNum = sheet.getLastRowNum() + 1; +// for (TransactionSummary summary : transactionSummaryList) { +// Row row = sheet.createRow(rowNum++); +// +// row.createCell(0).setCellValue(summary.getPLU_CODE()); +// if (summary.getToShopDate() != null) { +// row.createCell(1).setCellValue(summary.getToShopDate().toString()); +// }else { +// row.createCell(1).setCellValue(""); +// } +// row.createCell(2).setCellValue(summary.getSalesDay()); +// row.createCell(3).setCellValue(summary.getCATEGORY()); +// row.createCell(4).setCellValue(summary.getSUB_CAT()); +// row.createCell(5).setCellValue(summary.getItemName()); +// row.createCell(6).setCellValue(summary.getPriceOriginal()); +// row.createCell(7).setCellValue(summary.getToRetailQty()); +// row.createCell(8).setCellValue(summary.getTotalSaleNum()); +// row.createCell(9).setCellValue(summary.getMAS_count()); +// row.createCell(10).setCellValue(summary.getMEL_count()); +// row.createCell(11).setCellValue(summary.getMPC_count()); +// row.createCell(12).setCellValue(summary.getMPS_count()); +// row.createCell(13).setCellValue(summary.getMTF_count()); +// row.createCell(14).setCellValue(summary.getMWP_count()); +// row.createCell(15).setCellValue(summary.getMYO_count()); +// row.createCell(16).setCellValue(summary.getLwSalesQty()); +// row.createCell(17).setCellValue(summary.getRetailOnHand()); +// row.createCell(18).setCellValue(summary.getSalesRate()); +// } +// +// // 将Workbook写入文件 +// try (FileOutputStream outputStream = new FileOutputStream(file)) { +// workbook.write(outputStream); +// } +// FileItem a = getMultipartFile(file, file.getName()); +// MultipartFile multipartFile = new CommonsMultipartFile(a); +// minioUtil.upload("mi-tu", "export", multipartFile); +// +// System.out.println("Excel file has been created successfully!"); +// } +// +// +// /** +// * WeeklyHeavyStockReport +// */ +// @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000)) +// @Scheduled(cron = "0 0 23 ? * 7") +// public void WeeklyHeavyStockReport() { +// MiTuExport miTuExport = createMiTuExport("Weekly heavy stock report", "week"); +// try { +// List weeklyHeavyStockList = getWeeklyHeavyStockList(); +// String filePath = miTuExport.getExportName()+".xlsx"; +// miTuExport.setUrl("mi-tu/export/" + filePath); +// exportWeeklyHeavyStock(weeklyHeavyStockList, filePath); +// miTuExport.setStatus(1); +// } catch (Exception e) { +// miTuExport.setStatus(0); +// e.printStackTrace(); +// } finally { +// miTuExportMapper.insert(miTuExport); +// System.out.println("接口执行完成!"); +// } +// } +// +// private void exportWeeklyHeavyStock(List weeklyHeavyStockList, String weeklyHeavyStockReportName) { +// String currentPath = Paths.get("").toAbsolutePath().toString(); +// File file = new File(currentPath + "/" + weeklyHeavyStockReportName); +// // 创建工作簿 +// Workbook workbook = new XSSFWorkbook(); +// // 创建工作表 +// Sheet sheet = workbook.createSheet("Weekly Heavy Stock Report"); +// // 创建标题行 +// Row headerRow = sheet.createRow(0); +// headerRow.createCell(0).setCellValue("PLU_CODE"); +// headerRow.createCell(1).setCellValue("item_name"); +// headerRow.createCell(2).setCellValue("item_barcode"); +// headerRow.createCell(3).setCellValue("cat"); +// headerRow.createCell(4).setCellValue("sub_cat"); +// headerRow.createCell(5).setCellValue("Col"); +// headerRow.createCell(6).setCellValue("price_sales"); +// headerRow.createCell(7).setCellValue("MAS"); +// headerRow.createCell(8).setCellValue("MEL"); +// headerRow.createCell(9).setCellValue("MPC"); +// headerRow.createCell(10).setCellValue("MPS"); +// headerRow.createCell(11).setCellValue("MTF"); +// headerRow.createCell(12).setCellValue("MWP"); +// headerRow.createCell(13).setCellValue("MYO"); +// headerRow.createCell(14).setCellValue("MHZ"); +// headerRow.createCell(15).setCellValue("MRT"); +// headerRow.createCell(16).setCellValue("MBZ"); +// headerRow.createCell(17).setCellValue("TLT"); +// headerRow.createCell(18).setCellValue("subtotal"); +// headerRow.createCell(19).setCellValue("G.TLT"); +// +// // 写入数据 +// int rowNum = 1; +// for (WeeklyHeavyStock weeklyHeavyStock : weeklyHeavyStockList) { +// Row row = sheet.createRow(rowNum++); +// row.createCell(0).setCellValue(weeklyHeavyStock.getPLU_CODE()); +// row.createCell(1).setCellValue(weeklyHeavyStock.getItem_name()); +// row.createCell(2).setCellValue(weeklyHeavyStock.getItem_barcode()); +// row.createCell(3).setCellValue(weeklyHeavyStock.getCat()); +// row.createCell(4).setCellValue(weeklyHeavyStock.getSub_cat()); +// row.createCell(5).setCellValue(weeklyHeavyStock.getCol()); +// row.createCell(6).setCellValue(weeklyHeavyStock.getPrice_sales()); +// row.createCell(7).setCellValue(weeklyHeavyStock.getMAS()); +// row.createCell(8).setCellValue(weeklyHeavyStock.getMEL()); +// row.createCell(9).setCellValue(weeklyHeavyStock.getMPC()); +// row.createCell(10).setCellValue(weeklyHeavyStock.getMPS()); +// row.createCell(11).setCellValue(weeklyHeavyStock.getMTF()); +// row.createCell(12).setCellValue(weeklyHeavyStock.getMWP()); +// row.createCell(13).setCellValue(weeklyHeavyStock.getMYO()); +// row.createCell(14).setCellValue(weeklyHeavyStock.getMHZ()); +// row.createCell(15).setCellValue(weeklyHeavyStock.getMRT()); +// row.createCell(16).setCellValue(weeklyHeavyStock.getMBZ()); +// row.createCell(17).setCellValue(weeklyHeavyStock.getTLT()); +// row.createCell(18).setCellValue(weeklyHeavyStock.getSubtotal()); +// row.createCell(19).setCellValue(weeklyHeavyStock.getG_TLT()); +// } +// +// // 将工作簿写入文件 +// try (FileOutputStream outputStream = new FileOutputStream(file)) { +// workbook.write(outputStream); +// } catch (IOException e) { +// e.printStackTrace(); +// } +// FileItem a = getMultipartFile(file, file.getName()); +// MultipartFile multipartFile = new CommonsMultipartFile(a); +// minioUtil.upload("mi-tu", "export", multipartFile); +// +// System.out.println("Excel file has been created successfully!"); +// } +// +// +// /** +// * Quarterly Product Grouping Report +// */ +// @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000)) +// @Scheduled(cron = "#{@calculateLastDayOfQuarterCron}") +// public void QuarterlyProductGroupingReport() { +// MiTuExport miTuExport = createMiTuExport("Quarterly product grouping report", "quarter"); +// try { +// List QuarterlyProductGroupingList = getQuarterlyProductGroupingList(); +// String filePath = miTuExport.getExportName()+".xlsx"; +// miTuExport.setUrl("mi-tu/export/" + filePath); +// exportQuarterlyProductGrouping(QuarterlyProductGroupingList, filePath); +// miTuExport.setStatus(1); +// } catch (Exception e) { +// miTuExport.setStatus(0); +// e.printStackTrace(); +// } finally { +// miTuExportMapper.insert(miTuExport); +// System.out.println("接口执行完成!"); +// } +// } +// @Bean +// public String calculateLastDayOfQuarterCron() { +// // 获取当前日期 +// LocalDate currentDate = LocalDate.now(); +// +// // 计算下一个季度的最后一天 +// Month currentMonth = currentDate.getMonth(); +// Month nextQuarterStartMonth = currentMonth.plus(3 - currentMonth.getValue() % 3); +// LocalDate nextQuarterStartDate = LocalDate.of(currentDate.getYear(), nextQuarterStartMonth, 1); +// LocalDate lastDayOfNextQuarter = nextQuarterStartDate.with(TemporalAdjusters.lastDayOfMonth()); +// +// // 将日期转换成 Cron 表达式的格式 +// int year = lastDayOfNextQuarter.getYear(); +// int month = lastDayOfNextQuarter.getMonthValue(); +// int dayOfMonth = lastDayOfNextQuarter.getDayOfMonth(); +// return String.format("0 0 22 %d %d ?", dayOfMonth, month); +// } +// +// private void exportQuarterlyProductGrouping(List weeklyHeavyStockList, String weeklyHeavyStockReportName) { +// String currentPath = Paths.get("").toAbsolutePath().toString(); +// File file = new File(currentPath + "/" + weeklyHeavyStockReportName); +// // 创建工作簿 +// Workbook workbook = new XSSFWorkbook(); +// // 创建工作表 +// Sheet sheet = workbook.createSheet("Quarterly Product Grouping Report"); +// // 创建标题行 +// Row headerRow = sheet.createRow(0); +// headerRow.createCell(0).setCellValue("PLU_CODE"); +// headerRow.createCell(1).setCellValue("Col"); +// headerRow.createCell(2).setCellValue("Size"); +// headerRow.createCell(3).setCellValue("item_barcode"); +// headerRow.createCell(4).setCellValue("item_name"); +// headerRow.createCell(5).setCellValue("cat"); +// headerRow.createCell(6).setCellValue("sub_cat"); +// headerRow.createCell(7).setCellValue("price_sales"); +// headerRow.createCell(8).setCellValue("MASSOLD"); +// headerRow.createCell(9).setCellValue("MELSOLD"); +// headerRow.createCell(10).setCellValue("MPCSOLD"); +// headerRow.createCell(11).setCellValue("MPSSOLD"); +// headerRow.createCell(12).setCellValue("MTFSOLD"); +// headerRow.createCell(13).setCellValue("MWPSOLD"); +// headerRow.createCell(14).setCellValue("MYOSOLD"); +// headerRow.createCell(15).setCellValue("MAS"); +// headerRow.createCell(16).setCellValue("MEL"); +// headerRow.createCell(17).setCellValue("MPC"); +// headerRow.createCell(18).setCellValue("MPS"); +// headerRow.createCell(19).setCellValue("MTF"); +// headerRow.createCell(20).setCellValue("MWP"); +// headerRow.createCell(21).setCellValue("MYO"); +// headerRow.createCell(22).setCellValue("MHZ"); +// headerRow.createCell(23).setCellValue("MRT"); +// headerRow.createCell(24).setCellValue("MBZ"); +// +// // 写入数据 +// int rowNum = 1; +// for (WeeklyHeavyStock weeklyHeavyStock : weeklyHeavyStockList) { +// Row row = sheet.createRow(rowNum++); +// row.createCell(0).setCellValue(weeklyHeavyStock.getPLU_CODE()); +// row.createCell(1).setCellValue(weeklyHeavyStock.getCol()); +// row.createCell(2).setCellValue(weeklyHeavyStock.getSize()); +// row.createCell(3).setCellValue(weeklyHeavyStock.getItem_barcode()); +// row.createCell(4).setCellValue(weeklyHeavyStock.getItem_name()); +// row.createCell(5).setCellValue(weeklyHeavyStock.getCat()); +// row.createCell(6).setCellValue(weeklyHeavyStock.getSub_cat()); +// row.createCell(7).setCellValue(weeklyHeavyStock.getPrice_sales()); +// row.createCell(8).setCellValue(weeklyHeavyStock.getMASSOLD()); +// row.createCell(9).setCellValue(weeklyHeavyStock.getMELSOLD()); +// row.createCell(10).setCellValue(weeklyHeavyStock.getMPCSOLD()); +// row.createCell(11).setCellValue(weeklyHeavyStock.getMPSSOLD()); +// row.createCell(12).setCellValue(weeklyHeavyStock.getMTFSOLD()); +// row.createCell(13).setCellValue(weeklyHeavyStock.getMWPSOLD()); +// row.createCell(14).setCellValue(weeklyHeavyStock.getMYOSOLD()); +// row.createCell(15).setCellValue(weeklyHeavyStock.getMAS()); +// row.createCell(16).setCellValue(weeklyHeavyStock.getMEL()); +// row.createCell(17).setCellValue(weeklyHeavyStock.getMPC()); +// row.createCell(18).setCellValue(weeklyHeavyStock.getMPS()); +// row.createCell(19).setCellValue(weeklyHeavyStock.getMTF()); +// row.createCell(20).setCellValue(weeklyHeavyStock.getMWP()); +// row.createCell(21).setCellValue(weeklyHeavyStock.getMYO()); +// row.createCell(22).setCellValue(weeklyHeavyStock.getMHZ()); +// row.createCell(23).setCellValue(weeklyHeavyStock.getMRT()); +// row.createCell(24).setCellValue(weeklyHeavyStock.getMBZ()); +// } +// +// // 将工作簿写入文件 +// try (FileOutputStream outputStream = new FileOutputStream(file)) { +// workbook.write(outputStream); +// } catch (IOException e) { +// e.printStackTrace(); +// } +// FileItem a = getMultipartFile(file, file.getName()); +// MultipartFile multipartFile = new CommonsMultipartFile(a); +// minioUtil.upload("mi-tu", "export", multipartFile); +// +// System.out.println("Excel file has been created successfully!"); +// } +// +// +// +// private static List getQuarterlyProductGroupingList() { +// List QuarterlyProductGroupingList = new ArrayList<>(); +// Connection conn = null; +// Statement stmt = null; +// try { +// Class.forName(JDBC_DRIVER); +// conn = DriverManager.getConnection(DB_URL, USER, PASS); +// stmt = conn.createStatement(); +// +// String sql = "WITH CountedData AS (\n" + +// " SELECT\n" + +// " WHCODE,\n" + +// " PLU_CODE,\n" + +// " item_name,\n" + +// " item_barcode,\n" + +// " cat,\n" + +// " sub_cat,\n" + +// " Col,\n" + +// " Size,\n" + +// " price_sales,\n" + +// " CASE WHEN WHCODE = 'MAS' THEN 1 ELSE 0 END AS MAS,\n" + +// " CASE WHEN WHCODE = 'MEL' THEN 1 ELSE 0 END AS MEL,\n" + +// " CASE WHEN WHCODE = 'MPC' THEN 1 ELSE 0 END AS MPC,\n" + +// " CASE WHEN WHCODE = 'MPS' THEN 1 ELSE 0 END AS MPS,\n" + +// " CASE WHEN WHCODE = 'MTF' THEN 1 ELSE 0 END AS MTF,\n" + +// " CASE WHEN WHCODE = 'MWP' THEN 1 ELSE 0 END AS MWP,\n" + +// " CASE WHEN WHCODE = 'MYO' THEN 1 ELSE 0 END AS MYO,\n" + +// " CASE WHEN WHCODE = 'MHZ' THEN 1 ELSE 0 END AS MHZ,\n" + +// " CASE WHEN WHCODE = 'MRT' THEN 1 ELSE 0 END AS MRT,\n" + +// " CASE WHEN WHCODE = 'MBZ' THEN 1 ELSE 0 END AS MBZ\n" + +// " FROM\n" + +// " (\n" + +// " SELECT\n" + +// " WHCODE,\n" + +// " PLU_CODE,\n" + +// " item_name,\n" + +// " item_code AS item_barcode,\n" + +// " CATEGORY AS cat,\n" + +// " SUB_CAT AS sub_cat,\n" + +// " COLOR AS Col,\n" + +// " SIZE AS SIZE,\n" + +// " price_original as price_sales\n" + +// " FROM\n" + +// " v_MZG003A \n" + +// " WHERE\n" + +// " PLU_CODE IN (SELECT TOP 3\n" + +// "PLU_CODE\n" + +// "FROM v_MZG020B\n" + +// "WHERE TRX_DATE >= '2024-03-04'\n" + +// "AND TRX_DATE <= '2024-03-10'\n" + +// "AND SH_CODE IN ('MAS', 'MEL', 'MPC', 'MPS', 'MTF', 'MWP', 'MYO')\n" + +// "GROUP BY PLU_CODE\n" + +// "ORDER BY count(1) desc)\n" + +// " AND item_active = 'Y' \n" + +// " AND WHCODE IN ('MAS', 'MEL', 'MPC', 'MPS', 'MTF', 'MWP', 'MYO', 'MHZ', 'MRT', 'MBZ')\n" + +// " ) AS v \n" + +// ") \n" + +// "SELECT\n" + +// " PLU_CODE,\n" + +// " item_name,\n" + +// " item_barcode,\n" + +// " cat,\n" + +// " sub_cat,\n" + +// " Col,\n" + +// " Size,\n" + +// " price_sales,\n" + +// " SUM(MAS) AS MAS,\n" + +// " SUM(MEL) AS MEL,\n" + +// " SUM(MPC) AS MPC,\n" + +// " SUM(MPS) AS MPS,\n" + +// " SUM(MTF) AS MTF,\n" + +// " SUM(MWP) AS MWP,\n" + +// " SUM(MYO) AS MYO,\n" + +// " SUM(MHZ) AS MHZ,\n" + +// " SUM(MRT) AS MRT,\n" + +// " SUM(MBZ) AS MBZ\n" + +// "FROM\n" + +// " CountedData \n" + +// "GROUP BY\n" + +// " PLU_CODE,\n" + +// " item_name,\n" + +// " item_barcode,\n" + +// " cat,\n" + +// " sub_cat,\n" + +// " Col,\n" + +// " Size,\n" + +// " price_sales"; +// +// ResultSet rs = stmt.executeQuery(sql); +// +// while (rs.next()) { +// WeeklyHeavyStock weeklyHeavyStock = new WeeklyHeavyStock(); +// weeklyHeavyStock.setPLU_CODE(rs.getString("PLU_CODE")); +// weeklyHeavyStock.setItem_name(rs.getString("item_name")); +// weeklyHeavyStock.setItem_barcode(rs.getString("item_barcode")); +// weeklyHeavyStock.setCat(rs.getString("cat")); +// weeklyHeavyStock.setSub_cat(rs.getString("sub_cat")); +// weeklyHeavyStock.setCol(rs.getString("Col")); +// weeklyHeavyStock.setSize(rs.getString("Size")); +// weeklyHeavyStock.setPrice_sales(rs.getDouble("price_sales")); +// weeklyHeavyStock.setMAS(rs.getInt("MAS")); +// weeklyHeavyStock.setMEL(rs.getInt("MEL")); +// weeklyHeavyStock.setMPC(rs.getInt("MPC")); +// weeklyHeavyStock.setMPS(rs.getInt("MPS")); +// weeklyHeavyStock.setMTF(rs.getInt("MTF")); +// weeklyHeavyStock.setMWP(rs.getInt("MWP")); +// weeklyHeavyStock.setMYO(rs.getInt("MYO")); +// weeklyHeavyStock.setMHZ(rs.getInt("MHZ")); +// weeklyHeavyStock.setMRT(rs.getInt("MRT")); +// weeklyHeavyStock.setMBZ(rs.getInt("MBZ")); +// QuarterlyProductGroupingList.add(weeklyHeavyStock); +// } +// sql = "SELECT\n" + +// "PLU_CODE,\n" + +// "ITEM_CODE,\n" + +// "SH_CODE\n" + +// "FROM\n" + +// "v_MZG020B\n" + +// "WHERE TRX_DATE >= '2024-03-04'\n" + +// "AND TRX_DATE <= '2024-03-10'\n" + +// "AND PLU_CODE IN (SELECT TOP 3\n" + +// "PLU_CODE\n" + +// "FROM v_MZG020B\n" + +// "WHERE TRX_DATE >= '2024-03-04'\n" + +// "AND TRX_DATE <= '2024-03-10'\n" + +// "AND SH_CODE IN ('MAS', 'MEL', 'MPC', 'MPS', 'MTF', 'MWP', 'MYO')\n" + +// "GROUP BY PLU_CODE\n" + +// "ORDER BY count(1) desc)"; +// +// rs = stmt.executeQuery(sql); +// +// List relationShCodeList = new ArrayList<>(); +// while (rs.next()) { +// WeeklyHeavyStock weeklyHeavyStock = new WeeklyHeavyStock(); +// weeklyHeavyStock.setPLU_CODE(rs.getString("PLU_CODE")); +// weeklyHeavyStock.setItem_barcode(rs.getString("ITEM_CODE")); +// weeklyHeavyStock.setSH_CODE(rs.getString("SH_CODE")); +// relationShCodeList.add(weeklyHeavyStock); +// } +// for (WeeklyHeavyStock relationShCode : relationShCodeList) { +// for (WeeklyHeavyStock quarterlyProductGrouping : QuarterlyProductGroupingList) { +// if (quarterlyProductGrouping.getItem_barcode().equals(relationShCode.getItem_barcode())) { +// if (relationShCode.getSH_CODE().equals("MPS ")) { +// quarterlyProductGrouping.setMASSOLD(quarterlyProductGrouping.getMASSOLD() + 1); +// } +// else if (relationShCode.getSH_CODE().equals("MEL ")) { +// quarterlyProductGrouping.setMELSOLD(quarterlyProductGrouping.getMELSOLD() + 1); +// } +// else if (relationShCode.getSH_CODE().equals("MPC ")) { +// quarterlyProductGrouping.setMPCSOLD(quarterlyProductGrouping.getMPCSOLD() + 1); +// } +// else if (relationShCode.getSH_CODE().equals("MPS ")) { +// quarterlyProductGrouping.setMPSSOLD(quarterlyProductGrouping.getMPSSOLD() + 1); +// } +// else if (relationShCode.getSH_CODE().equals("MTF ")) { +// quarterlyProductGrouping.setMTFSOLD(quarterlyProductGrouping.getMTFSOLD() + 1); +// } +// else if (relationShCode.getSH_CODE().equals("MWP ")) { +// quarterlyProductGrouping.setMWPSOLD(quarterlyProductGrouping.getMWPSOLD() + 1); +// } +// else if (relationShCode.getSH_CODE().equals("MYO ")) { +// quarterlyProductGrouping.setMYOSOLD(quarterlyProductGrouping.getMYOSOLD() + 1); +// } +// } +// } +// } +// rs.close(); +// stmt.close(); +// conn.close(); +// } catch (SQLException | ClassNotFoundException se) { +// se.printStackTrace(); +// } finally { +// try { +// if (stmt != null) stmt.close(); +// } catch (SQLException ignored) {} +// try { +// if (conn != null) conn.close(); +// } catch (SQLException se) { +// se.printStackTrace(); +// } +// } +// System.out.println("Query execution completed!"); +// return QuarterlyProductGroupingList; +// } +// +// private static List getWeeklyHeavyStockList() { +// List weeklyHeavyStockList = new ArrayList<>(); +// Connection conn = null; +// Statement stmt = null; +// try { +// Class.forName(JDBC_DRIVER); +// conn = DriverManager.getConnection(DB_URL, USER, PASS); +// stmt = conn.createStatement(); +// +// String sql = "WITH CountedData AS (\n" + +// " SELECT\n" + +// " WHCODE,\n" + +// " PLU_CODE,\n" + +// " item_name,\n" + +// " item_barcode,\n" + +// " cat,\n" + +// " sub_cat,\n" + +// " Col,\n" + +// " price_sales,\n" + +// " CASE WHEN WHCODE = 'MAS' THEN 1 ELSE 0 END AS MAS,\n" + +// " CASE WHEN WHCODE = 'MEL' THEN 1 ELSE 0 END AS MEL,\n" + +// " CASE WHEN WHCODE = 'MPC' THEN 1 ELSE 0 END AS MPC,\n" + +// " CASE WHEN WHCODE = 'MPS' THEN 1 ELSE 0 END AS MPS,\n" + +// " CASE WHEN WHCODE = 'MTF' THEN 1 ELSE 0 END AS MTF,\n" + +// " CASE WHEN WHCODE = 'MWP' THEN 1 ELSE 0 END AS MWP,\n" + +// " CASE WHEN WHCODE = 'MYO' THEN 1 ELSE 0 END AS MYO,\n" + +// " CASE WHEN WHCODE = 'MHZ' THEN 1 ELSE 0 END AS MHZ,\n" + +// " CASE WHEN WHCODE = 'MRT' THEN 1 ELSE 0 END AS MRT,\n" + +// " CASE WHEN WHCODE = 'MBZ' THEN 1 ELSE 0 END AS MBZ,\n" + +// " SUM(1) OVER (PARTITION BY PLU_CODE, Col) AS subtotal,\n" + +// " SUM(1) OVER (PARTITION BY PLU_CODE) AS \"G.TLT\"\n" + +// " FROM\n" + +// " (\n" + +// " SELECT\n" + +// " WHCODE,\n" + +// " PLU_CODE,\n" + +// " item_name,\n" + +// " item_code AS item_barcode,\n" + +// " CATEGORY AS cat,\n" + +// " SUB_CAT AS sub_cat,\n" + +// " COLOR AS Col,\n" + +// " SIZE AS SIZE,\n" + +// " price_original as price_sales\n" + +// " FROM\n" + +// " v_MZG003A \n" + +// " WHERE\n" + +// " PLU_CODE IN (\n" + +// " SELECT TOP 30 PLU_CODE \n" + +// " FROM v_MZG003A \n" + +// " WHERE item_active = 'Y' \n" + +// " GROUP BY PLU_CODE \n" + +// " ORDER BY COUNT ( 1 ) DESC\n" + +// " ) \n" + +// " AND item_active = 'Y' \n" + +// " AND WHCODE IN ('MAS', 'MEL', 'MPC', 'MPS', 'MTF', 'MWP', 'MYO', 'MHZ', 'MRT', 'MBZ')\n" + +// " ) AS v \n" + +// ") \n" + +// "SELECT\n" + +// " PLU_CODE,\n" + +// " item_name,\n" + +// " item_barcode,\n" + +// " cat,\n" + +// " sub_cat,\n" + +// " Col,\n" + +// " price_sales,\n" + +// " SUM(MAS) AS MAS,\n" + +// " SUM(MEL) AS MEL,\n" + +// " SUM(MPC) AS MPC,\n" + +// " SUM(MPS) AS MPS,\n" + +// " SUM(MTF) AS MTF,\n" + +// " SUM(MWP) AS MWP,\n" + +// " SUM(MYO) AS MYO,\n" + +// " SUM(MHZ) AS MHZ,\n" + +// " SUM(MRT) AS MRT,\n" + +// " SUM(MBZ) AS MBZ,\n" + +// " SUM(MAS + MEL + MPC + MPS + MTF + MWP + MYO + MHZ + MRT + MBZ) AS TLT,\n" + +// " subtotal,\n" + +// " \"G.TLT\"\n" + +// "FROM\n" + +// " CountedData \n" + +// "GROUP BY\n" + +// " PLU_CODE,\n" + +// " item_name,\n" + +// " item_barcode,\n" + +// " cat,\n" + +// " sub_cat,\n" + +// " Col,\n" + +// " price_sales,\n" + +// " subtotal,\n" + +// " \"G.TLT\"\n" + +// "ORDER BY \"G.TLT\" DESC, PLU_CODE ASC;\n"; +// +// ResultSet rs = stmt.executeQuery(sql); +// +// while (rs.next()) { +// WeeklyHeavyStock weeklyHeavyStock = new WeeklyHeavyStock(); +// weeklyHeavyStock.setPLU_CODE(rs.getString("PLU_CODE")); +// weeklyHeavyStock.setItem_name(rs.getString("item_name")); +// weeklyHeavyStock.setItem_barcode(rs.getString("item_barcode")); +// weeklyHeavyStock.setCat(rs.getString("cat")); +// weeklyHeavyStock.setSub_cat(rs.getString("sub_cat")); +// weeklyHeavyStock.setCol(rs.getString("Col")); +// weeklyHeavyStock.setPrice_sales(rs.getDouble("price_sales")); +// weeklyHeavyStock.setMAS(rs.getInt("MAS")); +// weeklyHeavyStock.setMEL(rs.getInt("MEL")); +// weeklyHeavyStock.setMPC(rs.getInt("MPC")); +// weeklyHeavyStock.setMPS(rs.getInt("MPS")); +// weeklyHeavyStock.setMTF(rs.getInt("MTF")); +// weeklyHeavyStock.setMWP(rs.getInt("MWP")); +// weeklyHeavyStock.setMYO(rs.getInt("MYO")); +// weeklyHeavyStock.setMHZ(rs.getInt("MHZ")); +// weeklyHeavyStock.setMRT(rs.getInt("MRT")); +// weeklyHeavyStock.setMBZ(rs.getInt("MBZ")); +// weeklyHeavyStock.setTLT(rs.getInt("TLT")); +// weeklyHeavyStock.setSubtotal(rs.getInt("subtotal")); +// weeklyHeavyStock.setG_TLT(rs.getInt("G.TLT")); +// weeklyHeavyStockList.add(weeklyHeavyStock); +// } +// +// rs.close(); +// stmt.close(); +// conn.close(); +// } catch (SQLException | ClassNotFoundException se) { +// se.printStackTrace(); +// } finally { +// try { +// if (stmt != null) stmt.close(); +// } catch (SQLException ignored) {} +// try { +// if (conn != null) conn.close(); +// } catch (SQLException se) { +// se.printStackTrace(); +// } +// } +// System.out.println("Query execution completed!"); +// return weeklyHeavyStockList; +// } +// +// private static List getTransactionSummaryList() { +// List transactionSummaryList = new ArrayList<>(); +// Connection conn = null; +// Statement stmt = null; +// try { +// // 注册 JDBC 驱动器 +// Class.forName(JDBC_DRIVER); +// +// // 打开一个连接 +// System.out.println("连接数据库..."); +// conn = DriverManager.getConnection(DB_URL, USER, PASS); +// +// // 执行查询 +// System.out.println("创建声明..."); +// stmt = conn.createStatement(); +// String sql; +// +// // 获取当前日期 +// LocalDate currentDate = LocalDate.now(); +// +// // 获取当前星期的第一天(星期一)和最后一天(星期日) +// LocalDate firstDayOfWeek = currentDate.with(DayOfWeek.MONDAY); +// LocalDate lastDayOfWeek = currentDate.with(DayOfWeek.SUNDAY); +// +// // 格式化日期字符串 +// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); +// String firstDayOfWeekStr = formatter.format(firstDayOfWeek); +// String lastDayOfWeekStr = formatter.format(lastDayOfWeek); +// // 构建完整的 SQL 查询语句 +// sql = "SELECT TOP 30\n" + +// " PLU_CODE,\n" + +// " ITEM_NAME,\n" + +// " CATEGORY,\n" + +// " SUB_CAT,\n" + +// " PRICE_ORIGINAL,\n" + +// " COUNT(1) AS totalSaleNum,\n" + +// " SUM(CASE WHEN SH_CODE = 'MAS' THEN 1 ELSE 0 END) AS MAS,\n" + +// " SUM(CASE WHEN SH_CODE = 'MEL' THEN 1 ELSE 0 END) AS MEL,\n" + +// " SUM(CASE WHEN SH_CODE = 'MPC' THEN 1 ELSE 0 END) AS MPC,\n" + +// " SUM(CASE WHEN SH_CODE = 'MPS' THEN 1 ELSE 0 END) AS MPS,\n" + +// " SUM(CASE WHEN SH_CODE = 'MTF' THEN 1 ELSE 0 END) AS MTF,\n" + +// " SUM(CASE WHEN SH_CODE = 'MWP' THEN 1 ELSE 0 END) AS MWP,\n" + +// " SUM(CASE WHEN SH_CODE = 'MYO' THEN 1 ELSE 0 END) AS MYO\n" + +// "FROM\n" + +// " v_MZG020B\n" + +// "WHERE\n" + +// " TRX_DATE >= '" + firstDayOfWeekStr + "' \n" + +// " AND TRX_DATE <= '" + lastDayOfWeekStr + "' \n" + +// "GROUP BY\n" + +// " PLU_CODE, ITEM_NAME, CATEGORY, SUB_CAT, PRICE_ORIGINAL\n" + +// "ORDER BY \n" + +// " totalSaleNum DESC, PLU_CODE;"; +// ResultSet rs = stmt.executeQuery(sql); +// +// // 处理结果集 +// while (rs.next()) { +// TransactionSummary summary = new TransactionSummary(); +// summary.setPLU_CODE(rs.getString("PLU_CODE")); +// summary.setItemName(rs.getString("ITEM_NAME")); +// summary.setCATEGORY(rs.getString("CATEGORY")); +// summary.setSUB_CAT(rs.getString("SUB_CAT")); +// summary.setPriceOriginal(rs.getDouble("PRICE_ORIGINAL")); +// summary.setTotalSaleNum(rs.getInt("totalSaleNum")); +// summary.setMAS_count(rs.getInt("MAS")); +// summary.setMEL_count(rs.getInt("MEL")); +// summary.setMPC_count(rs.getInt("MPC")); +// summary.setMPS_count(rs.getInt("MPS")); +// summary.setMTF_count(rs.getInt("MTF")); +// summary.setMWP_count(rs.getInt("MWP")); +// summary.setMYO_count(rs.getInt("MYO")); +// transactionSummaryList.add(summary); +// } +// +// List pluCodeList = transactionSummaryList.stream().map(TransactionSummary::getPLU_CODE).collect(Collectors.toList()); +// StringBuilder inClause = new StringBuilder("("); +// for (String pluCode : pluCodeList) { +// inClause.append("'").append(pluCode).append("',"); +// } +// inClause.deleteCharAt(inClause.length() - 1); // 删除最后一个逗号 +// inClause.append(")"); +// +// // 构建完整的 SQL 查询语句 +// sql = "SELECT\n" + +// "PLU_CODE,\n" + +// "COUNT(1) AS retailOnHand\n" + +// "FROM v_MZG003A\n" + +// "WHERE PLU_CODE IN " + inClause.toString() + "\n" + +// "GROUP BY PLU_CODE;"; +// rs = stmt.executeQuery(sql); +// List retailOnHandList = new ArrayList<>(); +// while (rs.next()) { +// TransactionSummary summary = new TransactionSummary(); +// summary.setPLU_CODE(rs.getString("PLU_CODE")); +// summary.setRetailOnHand(rs.getInt("retailOnHand")); +// retailOnHandList.add(summary); +// } +// for (TransactionSummary transactionSummary : transactionSummaryList) { +// for (TransactionSummary summary : retailOnHandList) { +// if (transactionSummary.getPLU_CODE().equals(summary.getPLU_CODE())) { +// transactionSummary.setRetailOnHand(summary.getRetailOnHand()); +// break; +// } +// } +// } +// +// sql = "SELECT plu_code,toshopdate,lastwk_salesqty FROM v_MZG019C WHERE plu_code in" + inClause; +// rs = stmt.executeQuery(sql); +// List toShopDateList = new ArrayList<>(); +// while (rs.next()) { +// TransactionSummary summary = new TransactionSummary(); +// summary.setPLU_CODE(rs.getString("plu_code")); +// summary.setToShopDate(rs.getDate("toshopdate")); +// summary.setLwSalesQty(rs.getInt("lastwk_salesqty")); +// toShopDateList.add(summary); +// } +// +// for (TransactionSummary transactionSummary : transactionSummaryList) { +// for (TransactionSummary toShopDate : toShopDateList) { +// if (transactionSummary.getPLU_CODE().equals(toShopDate.getPLU_CODE())) { +// transactionSummary.setToShopDate(toShopDate.getToShopDate()); +// transactionSummary.setSalesDay(calculateDaysDifference(toShopDate.getToShopDate())); +// transactionSummary.setLwSalesQty(toShopDate.getLwSalesQty()); +// break; +// } +// } +// } +// +// for (TransactionSummary transactionSummary : transactionSummaryList) { +// int ttlSalesQty = transactionSummary.getTotalSaleNum(); +// int retailOnHand = transactionSummary.getRetailOnHand(); +// int toRetailQty = ttlSalesQty + retailOnHand; +// +// double result = (double) ttlSalesQty / toRetailQty; +// // 将结果四舍五入到两位小数 +// double roundedResult = Math.round(result * 100.0) / 100.0; +// // 将销售率转换为百分比形式 +// String salesRate = String.format("%.0f%%", roundedResult * 100); +// +// transactionSummary.setToRetailQty(toRetailQty); +// transactionSummary.setSalesRate(salesRate); +// } +// +// // 清理环境 +// rs.close(); +// stmt.close(); +// conn.close(); +// } catch (SQLException se) { +// // 处理 JDBC 错误 +// se.printStackTrace(); +// } catch (Exception e) { +// // 处理 Class.forName 错误 +// e.printStackTrace(); +// } finally { +// // 关闭资源 +// try { +// if (stmt != null) stmt.close(); +// } catch (SQLException se2) { +// } // 什么都不做 +// try { +// if (conn != null) conn.close(); +// } catch (SQLException se) { +// se.printStackTrace(); +// } +// } +// System.out.println("查询执行完成!"); +// return transactionSummaryList; +// } +// +// public static int calculateDaysDifference(Date toShopDate) { +// // 获取当前时间的毫秒数 +// long currentTimeMillis = System.currentTimeMillis(); +// +// // 获取toShopDate的毫秒数 +// long toShopTimeMillis = toShopDate.getTime(); +// +// // 计算毫秒差,并转换为天数,并向上取整 +// int daysDifference = (int) Math.ceil((currentTimeMillis - toShopTimeMillis) / (1000.0 * 60 * 60 * 24)); +// +// return daysDifference; +// } +// +// private static List getCustomerData() { +// List customerDataList = new ArrayList<>(); +// Connection conn = null; +// Statement stmt = null; +// try { +// // 注册 JDBC 驱动器 +// Class.forName(JDBC_DRIVER); +// +// // 打开一个连接 +// System.out.println("连接数据库..."); +// conn = DriverManager.getConnection(DB_URL, USER, PASS); +// +// // 执行查询 +// System.out.println("创建声明..."); +// stmt = conn.createStatement(); +// String sql; +//// // 构建 IN 子句的字符串,用于添加到 SQL 查询中 +//// StringBuilder inClause = new StringBuilder("("); +//// for (String code : collect) { +//// inClause.append("'").append(code).append("',"); +//// } +//// inClause.deleteCharAt(inClause.length() - 1); // 删除最后一个逗号 +//// inClause.append(")"); +// +// // 构建完整的 SQL 查询语句 +// sql = "SELECT * \n" + +// "FROM V_RW0095B\n" + +// "WHERE join_date >= DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()) - 1, 0)\n" + +// "AND mbr_group != 'NONMBR'\n" + +// "ORDER BY join_date DESC"; +// ResultSet rs = stmt.executeQuery(sql); +// +// +// // 处理结果集 +// while (rs.next()) { +// // 将每行数据转换为 CustomerData 对象并添加到列表中 +// CustomerData customer = new CustomerData(); +// customer.setMbrCode(rs.getString("mbr_code")); +// customer.setMbrName(rs.getString("mbr_name")); +// customer.setMbrMobile(rs.getString("mbr_mobile")); +// customer.setMbrGroup(rs.getString("mbr_group")); +// customer.setMbrStatus(rs.getString("mbr_status")); +// customer.setJoinDate(rs.getDate("join_date")); +// customer.setMbrIssue(rs.getString("mbr_issue")); +// customer.setBirthMonth(rs.getInt("birth_m")); +// customer.setMbrSex(rs.getString("mbr_sex")); +// customer.setOffBonus(rs.getDouble("off_bonus")); +// customer.setEffBonus(rs.getDouble("eff_bonus")); +// customer.setSumBonus(rs.getDouble("sum_bonus")); +// customerDataList.add(customer); +// +// } +// +// // 清理环境 +// rs.close(); +// stmt.close(); +// conn.close(); +// } catch (SQLException se) { +// // 处理 JDBC 错误 +// se.printStackTrace(); +// } catch (Exception e) { +// // 处理 Class.forName 错误 +// e.printStackTrace(); +// } finally { +// // 关闭资源 +// try { +// if (stmt != null) stmt.close(); +// } catch (SQLException se2) { +// } // 什么都不做 +// try { +// if (conn != null) conn.close(); +// } catch (SQLException se) { +// se.printStackTrace(); +// } +// } +// System.out.println("查询执行完成!"); +// return customerDataList; +// } +// +// private static List getTransactionNewCustomerList() { +// List transactionNewCustomerList = new ArrayList<>(); +// Connection conn = null; +// Statement stmt = null; +// try { +// // 注册 JDBC 驱动器 +// Class.forName(JDBC_DRIVER); +// +// // 打开一个连接 +// System.out.println("连接数据库..."); +// conn = DriverManager.getConnection(DB_URL, USER, PASS); +// +// // 执行查询 +// System.out.println("创建声明..."); +// stmt = conn.createStatement(); +// String sql; +// +// // 构建完整的 SQL 查询语句 +// sql = "SELECT * FROM v_MZG002b\n" + +// "WHERE join_date >= DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()) - 1, 0)\n" + +// "AND join_date <= DATEADD(DAY, -1, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 0))\n" + +// "AND VIP_GRADE != 'WALK IN'\n" + +// "AND VIP_GRADE != 'OLD VIP-VIPP'\n" + +// "AND VIP_GRADE != 'OLD VIP-VIPC'\n" + +// "ORDER BY join_date DESC"; +// ResultSet rs = stmt.executeQuery(sql); +// +// +// // 处理结果集 +// while (rs.next()) { +// // 将每行数据转换为 TransactionNewCustomer 对象并添加到列表中 +// TransactionNewCustomer transactionNewCustomer = new TransactionNewCustomer(); +// transactionNewCustomer.setTrxNo(rs.getString("TRX_NO")); +// transactionNewCustomer.setShCode(rs.getString("SH_CODE")); +// transactionNewCustomer.setSalesmanName(rs.getString("SALEMAN_NAME")); +// transactionNewCustomer.setVipGrade(rs.getString("VIP_GRADE")); +// transactionNewCustomer.setSalesAmt(rs.getDouble("SALES_AMT")); +// transactionNewCustomer.setUserMember(rs.getString("USER_MEMBER")); +// transactionNewCustomer.setMbrName(rs.getString("MBR_NAME")); +// transactionNewCustomer.setMbrMobile(rs.getString("mbr_mobile")); +// transactionNewCustomer.setJoinDate(rs.getDate("join_date")); +// transactionNewCustomerList.add(transactionNewCustomer); +// } +// sql = "SELECT\n" + +// "aaa.TRX_NO,\n" + +// "sum(bbb.pay_bas_amt) as sumPayBasAmt\n" + +// "FROM\n" + +// "v_MZG002b aaa, V_MZG013 bbb\n" + +// "WHERE aaa.TRX_NO = bbb.trx_no\n" + +// "AND aaa.join_date >= DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()) - 1, 0)\n" + +// "AND aaa.join_date <= DATEADD(DAY, -1, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 0))\n" + +// "AND aaa.VIP_GRADE != 'WALK IN'\n" + +// "AND aaa.VIP_GRADE != 'OLD VIP-VIPP'\n" + +// "AND aaa.VIP_GRADE != 'OLD VIP-VIPC'\n" + +// "AND bbb.pay_desc != 'Bonus'\n" + +// "AND bbb.pay_desc != 'mi-tu Cash Coupon'\n" + +// "GROUP BY aaa.TRX_NO"; +// ResultSet rs1 = stmt.executeQuery(sql); +// List transactionNewCustomerListSumPayBasAmt = new ArrayList<>(); +// while (rs1.next()) { +// TransactionNewCustomer transactionNewCustomer = new TransactionNewCustomer(); +// transactionNewCustomer.setTrxNo(rs1.getString("TRX_NO")); +// transactionNewCustomer.setSumPayBasAmt(rs1.getDouble("sumPayBasAmt")); +// transactionNewCustomerListSumPayBasAmt.add(transactionNewCustomer); +// } +// Iterator iterator = transactionNewCustomerList.iterator(); +// while (iterator.hasNext()) { +// TransactionNewCustomer transactionNewCustomer = iterator.next(); +// boolean exist = false; +// for (TransactionNewCustomer newCustomer : transactionNewCustomerListSumPayBasAmt) { +// if (newCustomer.getTrxNo().equals(transactionNewCustomer.getTrxNo())) { +// exist = true; +// break; +// } +// } +// if (!exist) { +// iterator.remove(); +// } +// } +// +// for (TransactionNewCustomer transactionNewCustomer : transactionNewCustomerListSumPayBasAmt) { +// for (TransactionNewCustomer newCustomer : transactionNewCustomerList) { +// if (newCustomer.getTrxNo().equals(transactionNewCustomer.getTrxNo())) { +// newCustomer.setSalesAmt(transactionNewCustomer.getSumPayBasAmt()); +// break; +// } +// } +// } +// +// // 清理环境 +// rs.close(); +// stmt.close(); +// conn.close(); +// } catch (SQLException se) { +// // 处理 JDBC 错误 +// se.printStackTrace(); +// } catch (Exception e) { +// // 处理 Class.forName 错误 +// e.printStackTrace(); +// } finally { +// // 关闭资源 +// try { +// if (stmt != null) stmt.close(); +// } catch (SQLException se2) { +// } // 什么都不做 +// try { +// if (conn != null) conn.close(); +// } catch (SQLException se) { +// se.printStackTrace(); +// } +// } +// System.out.println("查询执行完成!"); +// return transactionNewCustomerList; +// } +// +// public final static String MITU = "mi-tu"; +// +// public void exportToExcelCustomerPurchaseReport(List customerDataList, String fileName) throws IOException { +// String currentPath = Paths.get("").toAbsolutePath().toString(); +// File file = new File(currentPath + "/" + fileName); +// // 如果文件不存在或者为空,则创建一个新的 Workbook +// Workbook workbook = new XSSFWorkbook(); +// +// // 创建一个新的 Sheet 或获取现有的 Sheet +// Sheet sheet = workbook.getSheet("Customer Data"); +// if (sheet == null) { +// sheet = workbook.createSheet("Customer Data"); +// } +// +// // 创建标题行 +// Row headerRow = sheet.getRow(0); +// if (headerRow == null) { +// headerRow = sheet.createRow(0); +// } +// +// // 获取当前年月 +// YearMonth currentYearMonth = YearMonth.now(); +// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM"); +// String[] headers = new String[25]; +// headers[0] = "mbrCode"; +// headers[1] = "mbrName"; +// headers[2] = "mbrMobile"; +// headers[3] = "mbrGroup"; +// headers[4] = "mbrStatus"; +// headers[5] = "joinDate"; +// headers[6] = "mbrIssue"; +// headers[7] = "birthMonth"; +// headers[8] = "mbrSex"; +// headers[9] = "offBonus"; +// headers[10] = "effBonus"; +// headers[11] = "sumBonus"; +// headers[12] = "thisMonth"; +// +// // 添加过去十一个月的年月 +// for (int i = 0; i < 11; i++) { +// headers[13 + i] = currentYearMonth.minusMonths(i + 1).format(formatter); +// } +// for (int i = 0; i < headers.length; i++) { +// Cell cell = headerRow.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); +// cell.setCellValue(headers[i]); +// } +// +// // 写入数据 +// int rowNum = sheet.getLastRowNum() + 1; +// for (CustomerData customer : customerDataList) { +// Row row = sheet.createRow(rowNum++); +// +// row.createCell(0).setCellValue(customer.getMbrCode()); +// row.createCell(1).setCellValue(customer.getMbrName()); +// row.createCell(2).setCellValue(customer.getMbrMobile()); +// row.createCell(3).setCellValue(customer.getMbrGroup()); +// row.createCell(4).setCellValue(customer.getMbrStatus()); +// if (customer.getJoinDate() != null) { +// row.createCell(5).setCellValue(customer.getJoinDate().toString()); +// } +// row.createCell(6).setCellValue(customer.getMbrIssue()); +// row.createCell(7).setCellValue(customer.getBirthMonth()); +// row.createCell(8).setCellValue(customer.getMbrSex()); +// row.createCell(9).setCellValue(customer.getOffBonus()); +// row.createCell(10).setCellValue(customer.getEffBonus()); +// row.createCell(11).setCellValue(customer.getSumBonus()); +// row.createCell(12).setCellValue(customer.getThisMonth()); +// row.createCell(13).setCellValue(customer.getMonth1()); +// row.createCell(14).setCellValue(customer.getMonth2()); +// row.createCell(15).setCellValue(customer.getMonth3()); +// row.createCell(16).setCellValue(customer.getMonth4()); +// row.createCell(17).setCellValue(customer.getMonth5()); +// row.createCell(18).setCellValue(customer.getMonth6()); +// row.createCell(19).setCellValue(customer.getMonth7()); +// row.createCell(20).setCellValue(customer.getMonth8()); +// row.createCell(21).setCellValue(customer.getMonth9()); +// row.createCell(22).setCellValue(customer.getMonth10()); +// row.createCell(23).setCellValue(customer.getMonth11()); +// row.createCell(24).setCellValue(customer.getMonth12()); +// } +// +// // 将Workbook写入文件 +// try (FileOutputStream outputStream = new FileOutputStream(file)) { +// workbook.write(outputStream); +// } +// FileItem a = getMultipartFile(file, file.getName()); +// MultipartFile multipartFile = new CommonsMultipartFile(a); +// minioUtil.upload("mi-tu", "export", multipartFile); +// +// System.out.println("Excel file has been created successfully!"); +// } +// +// private static FileItem getMultipartFile(File file, String fieldName) { +// // 使用 DiskFileItemFactory 创建一个 FileItemFactory +// FileItemFactory factory = new DiskFileItemFactory(16, null); +// +// // 使用 FileItemFactory 创建一个 FileItem 对象 +// String mimeType = getMimeType(file); +// FileItem item = factory.createItem(fieldName, mimeType, true, file.getName()); +// +// byte[] buffer = new byte[8192]; +// int bytesRead; +// +// try (FileInputStream fis = new FileInputStream(file); +// OutputStream os = item.getOutputStream()) { +// // 从文件中读取数据并写入 FileItem +// while ((bytesRead = fis.read(buffer)) != -1) { +// os.write(buffer, 0, bytesRead); +// } +// } catch (IOException e) { +// e.printStackTrace(); +// } +// +// return item; +// } +// +// private static String getMimeType(File file) { +// try { +// return Files.probeContentType(file.toPath()); +// } catch (IOException e) { +// e.printStackTrace(); +// return null; +// } +// } +// +// public static List retrieveCustomerData(List collect) { +// List customerDataList = new ArrayList<>(); +// Connection conn = null; +// Statement stmt = null; +// try { +// // 注册 JDBC 驱动器 +// Class.forName(JDBC_DRIVER); +// +// // 打开一个连接 +// System.out.println("连接数据库..."); +// conn = DriverManager.getConnection(DB_URL, USER, PASS); +// +// // 执行查询 +// System.out.println("创建声明..."); +// stmt = conn.createStatement(); +// String sql; // // 构建 IN 子句的字符串,用于添加到 SQL 查询中 // StringBuilder inClause = new StringBuilder("("); // for (String code : collect) { @@ -1334,762 +1653,443 @@ public class MiTuExportScheduledTask { // } // inClause.deleteCharAt(inClause.length() - 1); // 删除最后一个逗号 // inClause.append(")"); - - // 构建完整的 SQL 查询语句 - sql = "SELECT * \n" + - "FROM V_RW0095B\n" + - "WHERE join_date >= DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()) - 1, 0)\n" + - "AND mbr_group != 'NONMBR'\n" + - "ORDER BY join_date DESC"; - ResultSet rs = stmt.executeQuery(sql); - - - // 处理结果集 - while (rs.next()) { - // 将每行数据转换为 CustomerData 对象并添加到列表中 - CustomerData customer = new CustomerData(); - customer.setMbrCode(rs.getString("mbr_code")); - customer.setMbrName(rs.getString("mbr_name")); - customer.setMbrMobile(rs.getString("mbr_mobile")); - customer.setMbrGroup(rs.getString("mbr_group")); - customer.setMbrStatus(rs.getString("mbr_status")); - customer.setJoinDate(rs.getDate("join_date")); - customer.setMbrIssue(rs.getString("mbr_issue")); - customer.setBirthMonth(rs.getInt("birth_m")); - customer.setMbrSex(rs.getString("mbr_sex")); - customer.setOffBonus(rs.getDouble("off_bonus")); - customer.setEffBonus(rs.getDouble("eff_bonus")); - customer.setSumBonus(rs.getDouble("sum_bonus")); - customerDataList.add(customer); - - } - - // 清理环境 - rs.close(); - stmt.close(); - conn.close(); - } catch (SQLException se) { - // 处理 JDBC 错误 - se.printStackTrace(); - } catch (Exception e) { - // 处理 Class.forName 错误 - e.printStackTrace(); - } finally { - // 关闭资源 - try { - if (stmt != null) stmt.close(); - } catch (SQLException se2) { - } // 什么都不做 - try { - if (conn != null) conn.close(); - } catch (SQLException se) { - se.printStackTrace(); - } - } - System.out.println("查询执行完成!"); - return customerDataList; - } - - private static List getTransactionNewCustomerList() { - List transactionNewCustomerList = new ArrayList<>(); - Connection conn = null; - Statement stmt = null; - try { - // 注册 JDBC 驱动器 - Class.forName(JDBC_DRIVER); - - // 打开一个连接 - System.out.println("连接数据库..."); - conn = DriverManager.getConnection(DB_URL, USER, PASS); - - // 执行查询 - System.out.println("创建声明..."); - stmt = conn.createStatement(); - String sql; - - // 构建完整的 SQL 查询语句 - sql = "SELECT * FROM v_MZG002b\n" + - "WHERE join_date >= DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()) - 1, 0)\n" + - "AND join_date <= DATEADD(DAY, -1, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 0))\n" + - "AND VIP_GRADE != 'WALK IN'\n" + - "AND VIP_GRADE != 'OLD VIP-VIPP'\n" + - "AND VIP_GRADE != 'OLD VIP-VIPC'\n" + - "ORDER BY join_date DESC"; - ResultSet rs = stmt.executeQuery(sql); - - - // 处理结果集 - while (rs.next()) { - // 将每行数据转换为 TransactionNewCustomer 对象并添加到列表中 - TransactionNewCustomer transactionNewCustomer = new TransactionNewCustomer(); - transactionNewCustomer.setTrxNo(rs.getString("TRX_NO")); - transactionNewCustomer.setShCode(rs.getString("SH_CODE")); - transactionNewCustomer.setSalesmanName(rs.getString("SALEMAN_NAME")); - transactionNewCustomer.setVipGrade(rs.getString("VIP_GRADE")); - transactionNewCustomer.setSalesAmt(rs.getDouble("SALES_AMT")); - transactionNewCustomer.setUserMember(rs.getString("USER_MEMBER")); - transactionNewCustomer.setMbrName(rs.getString("MBR_NAME")); - transactionNewCustomer.setMbrMobile(rs.getString("mbr_mobile")); - transactionNewCustomer.setJoinDate(rs.getDate("join_date")); - transactionNewCustomerList.add(transactionNewCustomer); - } - sql = "SELECT\n" + - "aaa.TRX_NO,\n" + - "sum(bbb.pay_bas_amt) as sumPayBasAmt\n" + - "FROM\n" + - "v_MZG002b aaa, V_MZG013 bbb\n" + - "WHERE aaa.TRX_NO = bbb.trx_no\n" + - "AND aaa.join_date >= DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()) - 1, 0)\n" + - "AND aaa.join_date <= DATEADD(DAY, -1, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 0))\n" + - "AND aaa.VIP_GRADE != 'WALK IN'\n" + - "AND aaa.VIP_GRADE != 'OLD VIP-VIPP'\n" + - "AND aaa.VIP_GRADE != 'OLD VIP-VIPC'\n" + - "AND bbb.pay_desc != 'Bonus'\n" + - "AND bbb.pay_desc != 'mi-tu Cash Coupon'\n" + - "GROUP BY aaa.TRX_NO"; - ResultSet rs1 = stmt.executeQuery(sql); - List transactionNewCustomerListSumPayBasAmt = new ArrayList<>(); - while (rs1.next()) { - TransactionNewCustomer transactionNewCustomer = new TransactionNewCustomer(); - transactionNewCustomer.setTrxNo(rs1.getString("TRX_NO")); - transactionNewCustomer.setSumPayBasAmt(rs1.getDouble("sumPayBasAmt")); - transactionNewCustomerListSumPayBasAmt.add(transactionNewCustomer); - } - Iterator iterator = transactionNewCustomerList.iterator(); - while (iterator.hasNext()) { - TransactionNewCustomer transactionNewCustomer = iterator.next(); - boolean exist = false; - for (TransactionNewCustomer newCustomer : transactionNewCustomerListSumPayBasAmt) { - if (newCustomer.getTrxNo().equals(transactionNewCustomer.getTrxNo())) { - exist = true; - break; - } - } - if (!exist) { - iterator.remove(); - } - } - - for (TransactionNewCustomer transactionNewCustomer : transactionNewCustomerListSumPayBasAmt) { - for (TransactionNewCustomer newCustomer : transactionNewCustomerList) { - if (newCustomer.getTrxNo().equals(transactionNewCustomer.getTrxNo())) { - newCustomer.setSalesAmt(transactionNewCustomer.getSumPayBasAmt()); - break; - } - } - } - - // 清理环境 - rs.close(); - stmt.close(); - conn.close(); - } catch (SQLException se) { - // 处理 JDBC 错误 - se.printStackTrace(); - } catch (Exception e) { - // 处理 Class.forName 错误 - e.printStackTrace(); - } finally { - // 关闭资源 - try { - if (stmt != null) stmt.close(); - } catch (SQLException se2) { - } // 什么都不做 - try { - if (conn != null) conn.close(); - } catch (SQLException se) { - se.printStackTrace(); - } - } - System.out.println("查询执行完成!"); - return transactionNewCustomerList; - } - - public final static String MITU = "mi-tu"; - - public void exportToExcelCustomerPurchaseReport(List customerDataList, String fileName) throws IOException { - String currentPath = Paths.get("").toAbsolutePath().toString(); - File file = new File(currentPath + "/" + fileName); - // 如果文件不存在或者为空,则创建一个新的 Workbook - Workbook workbook = new XSSFWorkbook(); - - // 创建一个新的 Sheet 或获取现有的 Sheet - Sheet sheet = workbook.getSheet("Customer Data"); - if (sheet == null) { - sheet = workbook.createSheet("Customer Data"); - } - - // 创建标题行 - Row headerRow = sheet.getRow(0); - if (headerRow == null) { - headerRow = sheet.createRow(0); - } - - // 获取当前年月 - YearMonth currentYearMonth = YearMonth.now(); - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM"); - String[] headers = new String[25]; - headers[0] = "mbrCode"; - headers[1] = "mbrName"; - headers[2] = "mbrMobile"; - headers[3] = "mbrGroup"; - headers[4] = "mbrStatus"; - headers[5] = "joinDate"; - headers[6] = "mbrIssue"; - headers[7] = "birthMonth"; - headers[8] = "mbrSex"; - headers[9] = "offBonus"; - headers[10] = "effBonus"; - headers[11] = "sumBonus"; - headers[12] = "thisMonth"; - - // 添加过去十一个月的年月 - for (int i = 0; i < 11; i++) { - headers[13 + i] = currentYearMonth.minusMonths(i + 1).format(formatter); - } - for (int i = 0; i < headers.length; i++) { - Cell cell = headerRow.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); - cell.setCellValue(headers[i]); - } - - // 写入数据 - int rowNum = sheet.getLastRowNum() + 1; - for (CustomerData customer : customerDataList) { - Row row = sheet.createRow(rowNum++); - - row.createCell(0).setCellValue(customer.getMbrCode()); - row.createCell(1).setCellValue(customer.getMbrName()); - row.createCell(2).setCellValue(customer.getMbrMobile()); - row.createCell(3).setCellValue(customer.getMbrGroup()); - row.createCell(4).setCellValue(customer.getMbrStatus()); - if (customer.getJoinDate() != null) { - row.createCell(5).setCellValue(customer.getJoinDate().toString()); - } - row.createCell(6).setCellValue(customer.getMbrIssue()); - row.createCell(7).setCellValue(customer.getBirthMonth()); - row.createCell(8).setCellValue(customer.getMbrSex()); - row.createCell(9).setCellValue(customer.getOffBonus()); - row.createCell(10).setCellValue(customer.getEffBonus()); - row.createCell(11).setCellValue(customer.getSumBonus()); - row.createCell(12).setCellValue(customer.getThisMonth()); - row.createCell(13).setCellValue(customer.getMonth1()); - row.createCell(14).setCellValue(customer.getMonth2()); - row.createCell(15).setCellValue(customer.getMonth3()); - row.createCell(16).setCellValue(customer.getMonth4()); - row.createCell(17).setCellValue(customer.getMonth5()); - row.createCell(18).setCellValue(customer.getMonth6()); - row.createCell(19).setCellValue(customer.getMonth7()); - row.createCell(20).setCellValue(customer.getMonth8()); - row.createCell(21).setCellValue(customer.getMonth9()); - row.createCell(22).setCellValue(customer.getMonth10()); - row.createCell(23).setCellValue(customer.getMonth11()); - row.createCell(24).setCellValue(customer.getMonth12()); - } - - // 将Workbook写入文件 - try (FileOutputStream outputStream = new FileOutputStream(file)) { - workbook.write(outputStream); - } - FileItem a = getMultipartFile(file, file.getName()); - MultipartFile multipartFile = new CommonsMultipartFile(a); - minioUtil.upload("mi-tu", "export", multipartFile); - - System.out.println("Excel file has been created successfully!"); - } - - private static FileItem getMultipartFile(File file, String fieldName) { - // 使用 DiskFileItemFactory 创建一个 FileItemFactory - FileItemFactory factory = new DiskFileItemFactory(16, null); - - // 使用 FileItemFactory 创建一个 FileItem 对象 - String mimeType = getMimeType(file); - FileItem item = factory.createItem(fieldName, mimeType, true, file.getName()); - - byte[] buffer = new byte[8192]; - int bytesRead; - - try (FileInputStream fis = new FileInputStream(file); - OutputStream os = item.getOutputStream()) { - // 从文件中读取数据并写入 FileItem - while ((bytesRead = fis.read(buffer)) != -1) { - os.write(buffer, 0, bytesRead); - } - } catch (IOException e) { - e.printStackTrace(); - } - - return item; - } - - private static String getMimeType(File file) { - try { - return Files.probeContentType(file.toPath()); - } catch (IOException e) { - e.printStackTrace(); - return null; - } - } - - public static List retrieveCustomerData(List collect) { - List customerDataList = new ArrayList<>(); - Connection conn = null; - Statement stmt = null; - try { - // 注册 JDBC 驱动器 - Class.forName(JDBC_DRIVER); - - // 打开一个连接 - System.out.println("连接数据库..."); - conn = DriverManager.getConnection(DB_URL, USER, PASS); - - // 执行查询 - System.out.println("创建声明..."); - stmt = conn.createStatement(); - String sql; - // 构建 IN 子句的字符串,用于添加到 SQL 查询中 - StringBuilder inClause = new StringBuilder("("); - for (String code : collect) { - inClause.append("'").append(code).append("',"); - } - inClause.deleteCharAt(inClause.length() - 1); // 删除最后一个逗号 - inClause.append(")"); - - // 构建完整的 SQL 查询语句 - sql = "SELECT * FROM V_RW0095B WHERE mbr_code IN " + inClause.toString(); - ResultSet rs = stmt.executeQuery(sql); - - - // 处理结果集 - while (rs.next()) { - // 将每行数据转换为 CustomerData 对象并添加到列表中 - CustomerData customer = new CustomerData(); - customer.setMbrCode(rs.getString("mbr_code")); - customer.setMbrName(rs.getString("mbr_name")); - customer.setMbrMobile(rs.getString("mbr_mobile")); - customer.setMbrGroup(rs.getString("mbr_group")); - customer.setMbrStatus(rs.getString("mbr_status")); - customer.setJoinDate(rs.getDate("join_date")); - customer.setMbrIssue(rs.getString("mbr_issue")); - customer.setBirthMonth(rs.getInt("birth_m")); - customer.setMbrSex(rs.getString("mbr_sex")); - customer.setOffBonus(rs.getDouble("off_bonus")); - customer.setEffBonus(rs.getDouble("eff_bonus")); - customer.setSumBonus(rs.getDouble("sum_bonus")); - customerDataList.add(customer); - - } - - // 清理环境 - rs.close(); - stmt.close(); - conn.close(); - } catch (SQLException se) { - // 处理 JDBC 错误 - se.printStackTrace(); - } catch (Exception e) { - // 处理 Class.forName 错误 - e.printStackTrace(); - } finally { - // 关闭资源 - try { - if (stmt != null) stmt.close(); - } catch (SQLException se2) { - } // 什么都不做 - try { - if (conn != null) conn.close(); - } catch (SQLException se) { - se.printStackTrace(); - } - } - System.out.println("查询执行完成!"); - return customerDataList; - } - - public static List retrieveTransactionData() { - List transactionDataList = new ArrayList<>(); - Connection conn = null; - Statement stmt = null; - try { - // 注册 JDBC 驱动器 - Class.forName(JDBC_DRIVER); - - // 打开一个连接 - System.out.println("连接数据库..."); - conn = DriverManager.getConnection(DB_URL, USER, PASS); - - // 执行查询 - System.out.println("创建声明..."); - stmt = conn.createStatement(); - String sql; - sql = "SELECT \n" + - " user_member,\n" + - " SUM(CASE WHEN trx_date >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) AND trx_date <= GETDATE() THEN pay_bas_amt ELSE 0 END) AS thisMonth,\n" + - " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) THEN pay_bas_amt ELSE 0 END) AS month1,\n" + - " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -2, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month2,\n" + - " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -3, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -2, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month3,\n" + - " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -4, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -3, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month4,\n" + - " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -5, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -4, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month5,\n" + - " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -6, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -5, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month6,\n" + - " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -7, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -6, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month7,\n" + - " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -8, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -7, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month8,\n" + - " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -9, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -8, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month9,\n" + - " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -10, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -9, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month10,\n" + - " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -11, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -10, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month11,\n" + - " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -12, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -11, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month12\n" + - "FROM \n" + - " V_MZG013\n" + - "WHERE \n" + - " trx_date >= DATEADD(MONTH, -12, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))\n" + - " AND user_member IS NOT NULL\n" + - " AND pay_desc != 'Bonus'\n" + - " AND pay_desc != 'mi-tu Cash Coupon'\n" + - "GROUP BY \n" + - " user_member\n"; - - ResultSet rs = stmt.executeQuery(sql); -// rs.setFetchSize(500); - int a = 0; - // 处理结果集 - while (rs.next()) { - a ++; -// System.out.println(a); - // 将每行数据转换为 TransactionData 对象并添加到列表中 - TransactionData transaction = new TransactionData(); - transaction.setUserMember(rs.getString("user_member")); - -// transaction.setMbrName(rs.getString("mbr_name")); - transaction.setThisMonth(rs.getDouble("thisMonth")); - transaction.setMonth1(rs.getDouble("month1")); - transaction.setMonth2(rs.getDouble("month2")); - transaction.setMonth3(rs.getDouble("month3")); - transaction.setMonth4(rs.getDouble("month4")); - transaction.setMonth5(rs.getDouble("month5")); - transaction.setMonth6(rs.getDouble("month6")); - transaction.setMonth7(rs.getDouble("month7")); - transaction.setMonth8(rs.getDouble("month8")); - transaction.setMonth9(rs.getDouble("month9")); - transaction.setMonth10(rs.getDouble("month10")); - transaction.setMonth11(rs.getDouble("month11")); - transaction.setMonth12(rs.getDouble("month12")); - - transactionDataList.add(transaction); - } - - // 清理环境 - rs.close(); - stmt.close(); - conn.close(); - } catch (SQLException | ClassNotFoundException e) { - // 处理异常 - e.printStackTrace(); - } - System.out.println("查询执行完成!"); - return transactionDataList; - } - - // 示例:导出数据到CSV文件 - private static void exportToCSV(ResultSet resultSet, String filePath) throws SQLException, IOException { - FileWriter writer = new FileWriter(filePath); - - while (resultSet.next()) { - // 将结果写入CSV文件 - // 这里需要根据实际情况将数据写入CSV文件 - } - - writer.close(); - } - - @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000)) - @Scheduled(cron = "#{@calculateLastDayOfQuarterCron}") - public void customerTypeAnalysis() { - try { - // 根据近三个月购买记录获取顾客member_code - List memberCodeList = getMemberCode(); - // 遍历每个顾客的类型 - List miTuMemberList = getMemberType(memberCodeList); - } catch (Exception e) { - e.printStackTrace(); - } finally { - System.out.println("接口执行完成!"); - } - } - - @Resource - private MiTuMemberMapper miTuMemberMapper; - - public Map varianceMap = new HashMap<>(); - public Map mostFrequentColorMap = new HashMap<>(); - private List getMemberType(List memberCodeList) { - List miTuMemberList = new ArrayList<>(); - for (String memberCode : memberCodeList) { - if (memberCode == null) { - continue; - } - String memberType = getMemberTypeByMemberCode(memberCode); - MiTuMember member = getMember(memberCode); - MiTuMember miTuMember = CopyUtil.copyObject(member, MiTuMember.class); - miTuMember.setMbrCode(memberCode); - miTuMember.setMemberType(memberType); - miTuMemberList.add(miTuMember); - } - // 计算顾客颜色方差的方差 - // 计算所有顾客方差值的方差 - double overallVariance = calculateOverallVariance(varianceMap); - for (MiTuMember miTuMember : miTuMemberList) { - if (miTuMember.getMemberType() == null) { - if (varianceMap.get(miTuMember.getMbrCode()) < overallVariance) { - miTuMember.setMemberType("Type D"); - miTuMember.setMostFrequentColor(mostFrequentColorMap.get(miTuMember.getMbrCode())); - }else { - miTuMember.setMemberType("Type S"); - } - } - miTuMemberMapper.insert(miTuMember); - } - return miTuMemberList; - } - - private MiTuMember getMember(String memberCode) { - MiTuMember member = new MiTuMember(); - Connection conn = null; - Statement stmt = null; - try { - // 注册 JDBC 驱动器 - Class.forName(JDBC_DRIVER); - - // 打开一个连接 - System.out.println("连接数据库..."); - conn = DriverManager.getConnection(DB_URL, USER, PASS); - - // 执行查询 - System.out.println("创建声明..."); - stmt = conn.createStatement(); - String sql; - sql = "SELECT * FROM V_RW0095B\n" + - "WHERE mbr_code = '" + memberCode + "'"; - - ResultSet rs = stmt.executeQuery(sql); - // 处理结果集 - while (rs.next()) { - member.setMbrCode(rs.getString("mbr_code")); - member.setMbrName(rs.getString("mbr_name")); - member.setMbrMobile(rs.getString("mbr_mobile")); - member.setMbrGroup(rs.getString("mbr_group")); - - // Check if "mbr_status" is not NULL - String mbrStatus = rs.getString("mbr_status"); - if (mbrStatus != null) { - member.setMbrStatus(mbrStatus.charAt(0)); - } else { - // Handle NULL value for "mbr_status" - } - - // Check if "join_date" is not NULL - member.setJoinDate(rs.getDate("join_date").toLocalDate()); - member.setMbrIssue(rs.getString("mbr_issue")); - member.setBirthMonth(rs.getInt("birth_m")); - - // Check if "mbr_sex" is not NULL - String mbrSex = rs.getString("mbr_sex"); - if (mbrSex != null) { - member.setMbrSex(mbrSex.charAt(0)); - } else { - // Handle NULL value for "mbr_sex" - } - - // Check if "off_bonus" is not NULL - double offBonus = rs.getDouble("off_bonus"); - if (!rs.wasNull()) { - member.setOffBonus(offBonus); - } else { - // Handle NULL value for "off_bonus" - } - - // Repeat similar checks for other fields... - } - - // 清理环境 - rs.close(); - stmt.close(); - conn.close(); - } catch (SQLException | ClassNotFoundException e) { - // 处理异常 - e.printStackTrace(); - } - return member; - } - - private double calculateOverallVariance(Map varianceMap) { - // 计算平均值 - double mean = varianceMap.values().stream().mapToDouble(Double::doubleValue).average().orElse(0.0); - - // 计算方差 - double sumOfSquaredDifferences = varianceMap.values().stream() - .mapToDouble(value -> Math.pow(value - mean, 2)) - .sum(); - - return sumOfSquaredDifferences / varianceMap.size(); - } - - private String getMemberTypeByMemberCode(String memberCode) { - List transactionList = new ArrayList<>(); - Connection conn = null; - Statement stmt = null; - try { - // 注册 JDBC 驱动器 - Class.forName(JDBC_DRIVER); - - // 打开一个连接 - System.out.println("连接数据库..."); - conn = DriverManager.getConnection(DB_URL, USER, PASS); - - // 执行查询 - System.out.println("创建声明..."); - stmt = conn.createStatement(); - String sql; - String threeMonthsAgoDateAsString = getThreeMonthsAgoDateAsString(); - sql = "SELECT\n" + - "*\n" + - "FROM v_MZG016A\n" + - "WHERE TRX_DATE >= '" + threeMonthsAgoDateAsString +"'\n" + - "AND member_code = '" + memberCode + "'"; - - ResultSet rs = stmt.executeQuery(sql); - // 处理结果集 - while (rs.next()) { - Transaction transaction = new Transaction(); - double itemDiscount = rs.getDouble("item_discount"); - if (!rs.wasNull()) { - transaction.setItemDiscount(itemDiscount); - } - double discountPer = rs.getDouble("discount_per"); - if (!rs.wasNull()) { - transaction.setDiscountPer(discountPer); - } - transaction.setYear(rs.getString("YEAR")); - transaction.setSeason(rs.getString("SEASON")); - transaction.setColor(rs.getString("COLOR")); - transaction.setSubCat(rs.getString("SUB_CAT")); - transactionList.add(transaction); - } - - // 清理环境 - rs.close(); - stmt.close(); - conn.close(); - } catch (SQLException | ClassNotFoundException e) { - // 处理异常 - e.printStackTrace(); - } - - return analysis(transactionList, memberCode); - } - - private String analysis(List transactionList, String memberCode) { - int discountNum = 0; - int currentSeasonNum = 0; - int leadingNum = 0; - - Map colorCountMap = new HashMap<>(); - int totalCount = transactionList.size(); - - // 统计每种颜色的出现次数 - for (Transaction transaction : transactionList) { - String color = transaction.getColor(); - colorCountMap.put(color, colorCountMap.getOrDefault(color, 0) + 1); - } - - // 计算每种颜色的出现频率 - double mean = (double) totalCount / colorCountMap.size(); - double varianceSum = 0; - String mostFrequentColor = null; - int maxCount = 0; - for (Map.Entry entry : colorCountMap.entrySet()) { - int count = entry.getValue(); - varianceSum += Math.pow(count - mean, 2); - if (count > maxCount) { - maxCount = count; - mostFrequentColor = entry.getKey(); - } - } - - // 计算方差 - double variance = varianceSum / colorCountMap.size(); - for (Transaction transaction : transactionList) { - if ((transaction.getDiscountPer() != null && transaction.getDiscountPer() >= 20.0) || transaction.getSubCat().equals("TWIN SET")) { - discountNum ++; - } - if (transaction.getYear().equals("TE") && transaction.getSeason().equals("A")) { - currentSeasonNum ++; - } - } - if (discountNum >= transactionList.size() * 0.5) { - return "Type C"; - } - if (currentSeasonNum >= transactionList.size() * 0.7) { - return "Type i"; - } - varianceMap.put(memberCode, variance); - mostFrequentColorMap.put(memberCode, mostFrequentColor); - return null; -// if (variance <= 0.2) { -// return "Type D"; +// +// // 构建完整的 SQL 查询语句 +// sql = "SELECT * FROM V_RW0095B WHERE mbr_code IN " + inClause.toString(); +// ResultSet rs = stmt.executeQuery(sql); +// +// +// // 处理结果集 +// while (rs.next()) { +// // 将每行数据转换为 CustomerData 对象并添加到列表中 +// CustomerData customer = new CustomerData(); +// customer.setMbrCode(rs.getString("mbr_code")); +// customer.setMbrName(rs.getString("mbr_name")); +// customer.setMbrMobile(rs.getString("mbr_mobile")); +// customer.setMbrGroup(rs.getString("mbr_group")); +// customer.setMbrStatus(rs.getString("mbr_status")); +// customer.setJoinDate(rs.getDate("join_date")); +// customer.setMbrIssue(rs.getString("mbr_issue")); +// customer.setBirthMonth(rs.getInt("birth_m")); +// customer.setMbrSex(rs.getString("mbr_sex")); +// customer.setOffBonus(rs.getDouble("off_bonus")); +// customer.setEffBonus(rs.getDouble("eff_bonus")); +// customer.setSumBonus(rs.getDouble("sum_bonus")); +// customerDataList.add(customer); +// +// } +// +// // 清理环境 +// rs.close(); +// stmt.close(); +// conn.close(); +// } catch (SQLException se) { +// // 处理 JDBC 错误 +// se.printStackTrace(); +// } catch (Exception e) { +// // 处理 Class.forName 错误 +// e.printStackTrace(); +// } finally { +// // 关闭资源 +// try { +// if (stmt != null) stmt.close(); +// } catch (SQLException se2) { +// } // 什么都不做 +// try { +// if (conn != null) conn.close(); +// } catch (SQLException se) { +// se.printStackTrace(); +// } // } -// return "Type S"; - } - - private List getMemberCode() { - List result = new ArrayList<>(); - Connection conn = null; - Statement stmt = null; - try { - // 注册 JDBC 驱动器 - Class.forName(JDBC_DRIVER); - - // 打开一个连接 - System.out.println("连接数据库..."); - conn = DriverManager.getConnection(DB_URL, USER, PASS); - - // 执行查询 - System.out.println("创建声明..."); - stmt = conn.createStatement(); - String sql; - String threeMonthsAgoDateAsString = getThreeMonthsAgoDateAsString(); - sql = "SELECT\n" + - "member_code\n" + - "FROM v_MZG016A\n" + - "WHERE TRX_DATE > '" + threeMonthsAgoDateAsString + "'\n" + - "GROUP BY member_code"; - - ResultSet rs = stmt.executeQuery(sql); - // 处理结果集 - while (rs.next()) { - result.add(rs.getString("member_code")); - } - - // 清理环境 - rs.close(); - stmt.close(); - conn.close(); - } catch (SQLException | ClassNotFoundException e) { - // 处理异常 - e.printStackTrace(); - } - System.out.println("查询执行完成!"); - return result; - } - - public static String getThreeMonthsAgoDateAsString() { - // 获取当前日期和时间 - LocalDateTime currentDateTime = LocalDateTime.now(); - - // 计算三个月前的日期和时间 - LocalDateTime threeMonthsAgoDateTime = currentDateTime.minusMonths(3); - - // 格式化日期和时间为字符串 - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); - String threeMonthsAgoDateTimeAsString = threeMonthsAgoDateTime.format(formatter); - - return threeMonthsAgoDateTimeAsString; - } -} +// System.out.println("查询执行完成!"); +// return customerDataList; +// } +// +// public static List retrieveTransactionData() { +// List transactionDataList = new ArrayList<>(); +// Connection conn = null; +// Statement stmt = null; +// try { +// // 注册 JDBC 驱动器 +// Class.forName(JDBC_DRIVER); +// +// // 打开一个连接 +// System.out.println("连接数据库..."); +// conn = DriverManager.getConnection(DB_URL, USER, PASS); +// +// // 执行查询 +// System.out.println("创建声明..."); +// stmt = conn.createStatement(); +// String sql; +// sql = "SELECT \n" + +// " user_member,\n" + +// " SUM(CASE WHEN trx_date >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) AND trx_date <= GETDATE() THEN pay_bas_amt ELSE 0 END) AS thisMonth,\n" + +// " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) THEN pay_bas_amt ELSE 0 END) AS month1,\n" + +// " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -2, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month2,\n" + +// " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -3, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -2, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month3,\n" + +// " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -4, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -3, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month4,\n" + +// " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -5, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -4, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month5,\n" + +// " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -6, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -5, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month6,\n" + +// " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -7, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -6, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month7,\n" + +// " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -8, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -7, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month8,\n" + +// " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -9, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -8, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month9,\n" + +// " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -10, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -9, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month10,\n" + +// " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -11, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -10, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month11,\n" + +// " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -12, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -11, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month12\n" + +// "FROM \n" + +// " V_MZG013\n" + +// "WHERE \n" + +// " trx_date >= DATEADD(MONTH, -12, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))\n" + +// " AND user_member IS NOT NULL\n" + +// " AND pay_desc != 'Bonus'\n" + +// " AND pay_desc != 'mi-tu Cash Coupon'\n" + +// "GROUP BY \n" + +// " user_member\n"; +// +// ResultSet rs = stmt.executeQuery(sql); +//// rs.setFetchSize(500); +// int a = 0; +// // 处理结果集 +// while (rs.next()) { +// a ++; +//// System.out.println(a); +// // 将每行数据转换为 TransactionData 对象并添加到列表中 +// TransactionData transaction = new TransactionData(); +// transaction.setUserMember(rs.getString("user_member")); +// +//// transaction.setMbrName(rs.getString("mbr_name")); +// transaction.setThisMonth(rs.getDouble("thisMonth")); +// transaction.setMonth1(rs.getDouble("month1")); +// transaction.setMonth2(rs.getDouble("month2")); +// transaction.setMonth3(rs.getDouble("month3")); +// transaction.setMonth4(rs.getDouble("month4")); +// transaction.setMonth5(rs.getDouble("month5")); +// transaction.setMonth6(rs.getDouble("month6")); +// transaction.setMonth7(rs.getDouble("month7")); +// transaction.setMonth8(rs.getDouble("month8")); +// transaction.setMonth9(rs.getDouble("month9")); +// transaction.setMonth10(rs.getDouble("month10")); +// transaction.setMonth11(rs.getDouble("month11")); +// transaction.setMonth12(rs.getDouble("month12")); +// +// transactionDataList.add(transaction); +// } +// +// // 清理环境 +// rs.close(); +// stmt.close(); +// conn.close(); +// } catch (SQLException | ClassNotFoundException e) { +// // 处理异常 +// e.printStackTrace(); +// } +// System.out.println("查询执行完成!"); +// return transactionDataList; +// } +// +// // 示例:导出数据到CSV文件 +// private static void exportToCSV(ResultSet resultSet, String filePath) throws SQLException, IOException { +// FileWriter writer = new FileWriter(filePath); +// +// while (resultSet.next()) { +// // 将结果写入CSV文件 +// // 这里需要根据实际情况将数据写入CSV文件 +// } +// +// writer.close(); +// } +// +// @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000)) +// @Scheduled(cron = "#{@calculateLastDayOfQuarterCron}") +// public void customerTypeAnalysis() { +// try { +// // 根据近三个月购买记录获取顾客member_code +// List memberCodeList = getMemberCode(); +// // 遍历每个顾客的类型 +// List miTuMemberList = getMemberType(memberCodeList); +// } catch (Exception e) { +// e.printStackTrace(); +// } finally { +// System.out.println("接口执行完成!"); +// } +// } +// +// @Resource +// private MiTuMemberMapper miTuMemberMapper; +// +// public Map varianceMap = new HashMap<>(); +// public Map mostFrequentColorMap = new HashMap<>(); +// private List getMemberType(List memberCodeList) { +// List miTuMemberList = new ArrayList<>(); +// for (String memberCode : memberCodeList) { +// if (memberCode == null) { +// continue; +// } +// String memberType = getMemberTypeByMemberCode(memberCode); +// MiTuMember member = getMember(memberCode); +// MiTuMember miTuMember = CopyUtil.copyObject(member, MiTuMember.class); +// miTuMember.setMbrCode(memberCode); +// miTuMember.setMemberType(memberType); +// miTuMemberList.add(miTuMember); +// } +// // 计算顾客颜色方差的方差 +// // 计算所有顾客方差值的方差 +// double overallVariance = calculateOverallVariance(varianceMap); +// for (MiTuMember miTuMember : miTuMemberList) { +// if (miTuMember.getMemberType() == null) { +// if (varianceMap.get(miTuMember.getMbrCode()) < overallVariance) { +// miTuMember.setMemberType("Type D"); +// miTuMember.setMostFrequentColor(mostFrequentColorMap.get(miTuMember.getMbrCode())); +// }else { +// miTuMember.setMemberType("Type S"); +// } +// } +// miTuMemberMapper.insert(miTuMember); +// } +// return miTuMemberList; +// } +// +// private MiTuMember getMember(String memberCode) { +// MiTuMember member = new MiTuMember(); +// Connection conn = null; +// Statement stmt = null; +// try { +// // 注册 JDBC 驱动器 +// Class.forName(JDBC_DRIVER); +// +// // 打开一个连接 +// System.out.println("连接数据库..."); +// conn = DriverManager.getConnection(DB_URL, USER, PASS); +// +// // 执行查询 +// System.out.println("创建声明..."); +// stmt = conn.createStatement(); +// String sql; +// sql = "SELECT * FROM V_RW0095B\n" + +// "WHERE mbr_code = '" + memberCode + "'"; +// +// ResultSet rs = stmt.executeQuery(sql); +// // 处理结果集 +// while (rs.next()) { +// member.setMbrCode(rs.getString("mbr_code")); +// member.setMbrName(rs.getString("mbr_name")); +// member.setMbrMobile(rs.getString("mbr_mobile")); +// member.setMbrGroup(rs.getString("mbr_group")); +// +// // Check if "mbr_status" is not NULL +// String mbrStatus = rs.getString("mbr_status"); +// if (mbrStatus != null) { +// member.setMbrStatus(mbrStatus.charAt(0)); +// } else { +// // Handle NULL value for "mbr_status" +// } +// +// // Check if "join_date" is not NULL +// member.setJoinDate(rs.getDate("join_date").toLocalDate()); +// member.setMbrIssue(rs.getString("mbr_issue")); +// member.setBirthMonth(rs.getInt("birth_m")); +// +// // Check if "mbr_sex" is not NULL +// String mbrSex = rs.getString("mbr_sex"); +// if (mbrSex != null) { +// member.setMbrSex(mbrSex.charAt(0)); +// } else { +// // Handle NULL value for "mbr_sex" +// } +// +// // Check if "off_bonus" is not NULL +// double offBonus = rs.getDouble("off_bonus"); +// if (!rs.wasNull()) { +// member.setOffBonus(offBonus); +// } else { +// // Handle NULL value for "off_bonus" +// } +// +// // Repeat similar checks for other fields... +// } +// +// // 清理环境 +// rs.close(); +// stmt.close(); +// conn.close(); +// } catch (SQLException | ClassNotFoundException e) { +// // 处理异常 +// e.printStackTrace(); +// } +// return member; +// } +// +// private double calculateOverallVariance(Map varianceMap) { +// // 计算平均值 +// double mean = varianceMap.values().stream().mapToDouble(Double::doubleValue).average().orElse(0.0); +// +// // 计算方差 +// double sumOfSquaredDifferences = varianceMap.values().stream() +// .mapToDouble(value -> Math.pow(value - mean, 2)) +// .sum(); +// +// return sumOfSquaredDifferences / varianceMap.size(); +// } +// +// private String getMemberTypeByMemberCode(String memberCode) { +// List transactionList = new ArrayList<>(); +// Connection conn = null; +// Statement stmt = null; +// try { +// // 注册 JDBC 驱动器 +// Class.forName(JDBC_DRIVER); +// +// // 打开一个连接 +// System.out.println("连接数据库..."); +// conn = DriverManager.getConnection(DB_URL, USER, PASS); +// +// // 执行查询 +// System.out.println("创建声明..."); +// stmt = conn.createStatement(); +// String sql; +// String threeMonthsAgoDateAsString = getThreeMonthsAgoDateAsString(); +// sql = "SELECT\n" + +// "*\n" + +// "FROM v_MZG016A\n" + +// "WHERE TRX_DATE >= '" + threeMonthsAgoDateAsString +"'\n" + +// "AND member_code = '" + memberCode + "'"; +// +// ResultSet rs = stmt.executeQuery(sql); +// // 处理结果集 +// while (rs.next()) { +// Transaction transaction = new Transaction(); +// double itemDiscount = rs.getDouble("item_discount"); +// if (!rs.wasNull()) { +// transaction.setItemDiscount(itemDiscount); +// } +// double discountPer = rs.getDouble("discount_per"); +// if (!rs.wasNull()) { +// transaction.setDiscountPer(discountPer); +// } +// transaction.setYear(rs.getString("YEAR")); +// transaction.setSeason(rs.getString("SEASON")); +// transaction.setColor(rs.getString("COLOR")); +// transaction.setSubCat(rs.getString("SUB_CAT")); +// transactionList.add(transaction); +// } +// +// // 清理环境 +// rs.close(); +// stmt.close(); +// conn.close(); +// } catch (SQLException | ClassNotFoundException e) { +// // 处理异常 +// e.printStackTrace(); +// } +// +// return analysis(transactionList, memberCode); +// } +// +// private String analysis(List transactionList, String memberCode) { +// int discountNum = 0; +// int currentSeasonNum = 0; +// int leadingNum = 0; +// +// Map colorCountMap = new HashMap<>(); +// int totalCount = transactionList.size(); +// +// // 统计每种颜色的出现次数 +// for (Transaction transaction : transactionList) { +// String color = transaction.getColor(); +// colorCountMap.put(color, colorCountMap.getOrDefault(color, 0) + 1); +// } +// +// // 计算每种颜色的出现频率 +// double mean = (double) totalCount / colorCountMap.size(); +// double varianceSum = 0; +// String mostFrequentColor = null; +// int maxCount = 0; +// for (Map.Entry entry : colorCountMap.entrySet()) { +// int count = entry.getValue(); +// varianceSum += Math.pow(count - mean, 2); +// if (count > maxCount) { +// maxCount = count; +// mostFrequentColor = entry.getKey(); +// } +// } +// +// // 计算方差 +// double variance = varianceSum / colorCountMap.size(); +// for (Transaction transaction : transactionList) { +// if ((transaction.getDiscountPer() != null && transaction.getDiscountPer() >= 20.0) || transaction.getSubCat().equals("TWIN SET")) { +// discountNum ++; +// } +// if (transaction.getYear().equals("TE") && transaction.getSeason().equals("A")) { +// currentSeasonNum ++; +// } +// } +// if (discountNum >= transactionList.size() * 0.5) { +// return "Type C"; +// } +// if (currentSeasonNum >= transactionList.size() * 0.7) { +// return "Type i"; +// } +// varianceMap.put(memberCode, variance); +// mostFrequentColorMap.put(memberCode, mostFrequentColor); +// return null; +//// if (variance <= 0.2) { +//// return "Type D"; +//// } +//// return "Type S"; +// } +// +// private List getMemberCode() { +// List result = new ArrayList<>(); +// Connection conn = null; +// Statement stmt = null; +// try { +// // 注册 JDBC 驱动器 +// Class.forName(JDBC_DRIVER); +// +// // 打开一个连接 +// System.out.println("连接数据库..."); +// conn = DriverManager.getConnection(DB_URL, USER, PASS); +// +// // 执行查询 +// System.out.println("创建声明..."); +// stmt = conn.createStatement(); +// String sql; +// String threeMonthsAgoDateAsString = getThreeMonthsAgoDateAsString(); +// sql = "SELECT\n" + +// "member_code\n" + +// "FROM v_MZG016A\n" + +// "WHERE TRX_DATE > '" + threeMonthsAgoDateAsString + "'\n" + +// "GROUP BY member_code"; +// +// ResultSet rs = stmt.executeQuery(sql); +// // 处理结果集 +// while (rs.next()) { +// result.add(rs.getString("member_code")); +// } +// +// // 清理环境 +// rs.close(); +// stmt.close(); +// conn.close(); +// } catch (SQLException | ClassNotFoundException e) { +// // 处理异常 +// e.printStackTrace(); +// } +// System.out.println("查询执行完成!"); +// return result; +// } +// +// public static String getThreeMonthsAgoDateAsString() { +// // 获取当前日期和时间 +// LocalDateTime currentDateTime = LocalDateTime.now(); +// +// // 计算三个月前的日期和时间 +// LocalDateTime threeMonthsAgoDateTime = currentDateTime.minusMonths(3); +// +// // 格式化日期和时间为字符串 +// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); +// String threeMonthsAgoDateTimeAsString = threeMonthsAgoDateTime.format(formatter); +// +// return threeMonthsAgoDateTimeAsString; +// } +//} diff --git a/src/main/java/com/mixi/service/TAppProductService.java b/src/main/java/com/mixi/service/TAppProductService.java index 751cd99..76fef62 100644 --- a/src/main/java/com/mixi/service/TAppProductService.java +++ b/src/main/java/com/mixi/service/TAppProductService.java @@ -464,6 +464,9 @@ public class TAppProductService extends ServiceImpl { } else { throw new BusinessException("substring exception."); } + if (result.contains("%20")) { + result = result.replace("%20", " "); + } //商品排行 List tProducts = pythonService.similarityMatch(result); if(CollectionUtils.isEmpty(tProducts)){