package com.stylefeng.guns.modular.system.service.impl;
|
|
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
import com.stylefeng.guns.core.util.ToolUtil;
|
import com.stylefeng.guns.modular.system.model.TEmail;
|
import com.stylefeng.guns.modular.system.model.TUser;
|
import com.stylefeng.guns.modular.system.model.User;
|
import com.stylefeng.guns.modular.system.model.UserActivityDiscount1;
|
import com.stylefeng.guns.modular.system.dao.UserActivityDiscount1Mapper;
|
import com.stylefeng.guns.modular.system.service.ITCompanyService;
|
import com.stylefeng.guns.modular.system.service.ITUserService;
|
import com.stylefeng.guns.modular.system.service.IUserActivityDiscount1Service;
|
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
|
import com.stylefeng.guns.modular.system.service.TEmailService;
|
import com.stylefeng.guns.modular.system.util.EmailUtil;
|
import com.stylefeng.guns.modular.system.util.itextpdf.HtmlToPdfUtils;
|
import org.apache.poi.hssf.usermodel.*;
|
import org.apache.poi.ss.usermodel.CellType;
|
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
import org.apache.poi.ss.util.CellRangeAddress;
|
import org.jsoup.Jsoup;
|
import org.jsoup.nodes.Document;
|
import org.jsoup.nodes.Element;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.FileOutputStream;
|
import java.io.FileWriter;
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
|
/**
|
* <p>
|
* 用户活动-折扣1 服务实现类
|
* </p>
|
*
|
* @author stylefeng
|
* @since 2020-06-19
|
*/
|
@Service
|
public class UserActivityDiscount1ServiceImpl extends ServiceImpl<UserActivityDiscount1Mapper, UserActivityDiscount1> implements IUserActivityDiscount1Service {
|
|
@Resource
|
private UserActivityDiscount1Mapper userActivityDiscount1Mapper;
|
|
@Autowired
|
private ITCompanyService companyService;
|
|
@Autowired
|
private ITUserService userService;
|
|
@Value("${spring.mail.template-path}")
|
private String templatePath;
|
|
@Autowired
|
private TEmailService emailService;
|
|
|
/**
|
* 获取折扣统计
|
* @param name
|
* @param time
|
* @param companyId
|
* @param offset
|
* @param limit
|
* @return
|
* @throws Exception
|
*/
|
@Override
|
public Map<String, Object> queryDiscount1(String name, String time, Integer companyId, Integer offset, Integer limit) throws Exception {
|
String start = null;
|
String end = null;
|
if(ToolUtil.isNotEmpty(time)){
|
start = time.split(" - ")[0];
|
end = time.split(" - ")[1];
|
}
|
List<Map<String, Object>> list = userActivityDiscount1Mapper.queryDiscount1(name, start, end, companyId, offset, limit);
|
int i = userActivityDiscount1Mapper.queryDiscount1Count(name, start, end, companyId);
|
Map<String, Object> map = new HashMap<>();
|
map.put("rows", list);
|
map.put("total", i);
|
return map;
|
}
|
|
|
/**
|
* 下载折扣优惠
|
* @param name
|
* @param time
|
* @param companyId
|
* @return
|
* @throws Exception
|
*/
|
@Override
|
public HSSFWorkbook downloadDiscount1(String name, String time, Integer companyId) throws Exception {
|
String start = null;
|
String end = null;
|
if(ToolUtil.isNotEmpty(time)){
|
start = time.split(" - ")[0];
|
end = time.split(" - ")[1];
|
}
|
List<Map<String, Object>> maps = userActivityDiscount1Mapper.queryDiscount1(name, start, end, companyId, null, null);
|
|
List<List<String>> lists = new ArrayList<>();
|
List<String> list = new ArrayList<>();
|
list.add("活动名称:" + name);
|
list.add("起止时间:" + time);
|
list.add("运营商:" + (null != companyId ? companyService.selectById(companyId).getName() : ""));
|
lists.add(list);
|
list = new ArrayList<>();
|
list.add("时间");
|
list.add("使用情况");
|
list.add("");
|
lists.add(list);
|
list = new ArrayList<>();
|
list.add("");
|
list.add("折扣参与人数");
|
list.add("折扣金额");
|
lists.add(list);
|
|
List<List<List<String>>> lists1 = new ArrayList<>();
|
List<List<String>> lists2 = new ArrayList<>();
|
for(Map<String, Object> map : maps){
|
List<String> list2 = new ArrayList<>();
|
list2.add(null != map.get("time") ? map.get("time").toString() : "");
|
list2.add(null != map.get("userNum") ? map.get("userNum").toString() : "");
|
list2.add(null != map.get("discountMoney") ? map.get("discountMoney").toString() : "");
|
lists2.add(list2);
|
}
|
lists1.add(lists2);
|
|
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
|
HSSFSheet hssfSheet = hssfWorkbook.createSheet();
|
hssfSheet.setColumnWidth(0, 6 * 256);
|
hssfSheet.setDefaultRowHeightInPoints(20f);
|
for(int i = 0; i < lists.size(); i++){
|
HSSFRow hssfRow = hssfSheet.createRow(i);//设置第一行数据(标题)
|
HSSFCellStyle style = hssfWorkbook.createCellStyle();
|
HSSFFont font = hssfWorkbook.createFont();
|
font.setBold(true);
|
style.setFont(font);
|
style.setAlignment(HorizontalAlignment.CENTER);
|
for (int l = 0; l < lists.get(i).size(); l++) {
|
HSSFCell hssfCell = hssfRow.createCell(l);
|
hssfCell.setCellType(CellType.STRING);//设置表格类型
|
hssfCell.setCellValue(lists.get(i).get(l));
|
hssfCell.setCellStyle(style);
|
if(l > 0) {
|
hssfSheet.setColumnWidth(l , 20 * 256);
|
}
|
|
}
|
}
|
//这个就是合并单元格
|
//参数说明:1:开始行 2:结束行 3:开始列 4:结束列
|
//比如我要合并 第二行到第四行的 第六列到第八列 sheet.addMergedRegion(new CellRangeAddress(1,3,5,7));
|
hssfSheet.addMergedRegion(new CellRangeAddress(1,2,0,0));
|
hssfSheet.addMergedRegion(new CellRangeAddress(1,1,1,2));
|
|
|
for(int i = 0; i < lists1.size(); i++){
|
//将数据添加到表格中
|
List<String> data = null;
|
for (int l = 0; l < lists1.get(i).size(); l++) {
|
HSSFRow row = hssfSheet.createRow(l + 3);
|
data = lists1.get(i).get(l);
|
for (int j = 0; j < data.size(); j++) {
|
HSSFCell hssfCell = row.createCell(j);
|
hssfCell.setCellType(CellType.STRING);//设置表格类型
|
hssfCell.setCellValue(data.get(j));
|
}
|
}
|
}
|
return hssfWorkbook;
|
}
|
|
|
/**
|
* 获取折扣优惠详情
|
* @param name
|
* @param time
|
* @param companyId
|
* @param offset
|
* @param limit
|
* @return
|
* @throws Exception
|
*/
|
@Override
|
public Map<String, Object> queryDiscountInfo(String name, String time, Integer companyId, Integer offset, Integer limit) throws Exception {
|
String start = null;
|
String end = null;
|
if(ToolUtil.isNotEmpty(time)){
|
start = time.split(" - ")[0];
|
end = time.split(" - ")[1];
|
}
|
List<Map<String, Object>> list = userActivityDiscount1Mapper.queryDiscountInfo(name, start, end, companyId, offset, limit);
|
int i = userActivityDiscount1Mapper.queryDiscountInfoCount(name, start, end, companyId);
|
Map<String, Object> map = new HashMap<>();
|
map.put("rows", list);
|
map.put("total", i);
|
return map;
|
}
|
|
|
/**
|
* 下载折扣优惠详情
|
* @param name
|
* @param time
|
* @param companyId
|
* @return
|
* @throws Exception
|
*/
|
@Override
|
public HSSFWorkbook downloadDiscountInfo(String name, String time, Integer companyId) throws Exception {
|
String start = null;
|
String end = null;
|
if(ToolUtil.isNotEmpty(time)){
|
start = time.split(" - ")[0];
|
end = time.split(" - ")[1];
|
}
|
List<Map<String, Object>> maps = userActivityDiscount1Mapper.queryDiscountInfo(name, start, end, companyId, null, null);
|
|
List<List<String>> lists = new ArrayList<>();
|
List<String> list = new ArrayList<>();
|
list.add("活动名称:" + name);
|
list.add("起止时间:" + time);
|
list.add("运营商:");
|
list.add(null != companyId ? companyService.selectById(companyId).getName() : "");
|
lists.add(list);
|
list = new ArrayList<>();
|
list.add("打折时间");
|
list.add("打折使用人");
|
list.add("打折使用人电话");
|
list.add("打折金额");
|
lists.add(list);
|
|
List<List<List<String>>> lists1 = new ArrayList<>();
|
List<List<String>> lists2 = new ArrayList<>();
|
for(Map<String, Object> map : maps){
|
List<String> list2 = new ArrayList<>();
|
list2.add(null != map.get("time") ? map.get("time").toString() : "");
|
list2.add(null != map.get("name") ? map.get("name").toString() : "");
|
list2.add(null != map.get("phone") ? map.get("phone").toString() : "");
|
list2.add(null != map.get("discountMoney") ? map.get("discountMoney").toString() : "");
|
lists2.add(list2);
|
}
|
lists1.add(lists2);
|
|
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
|
HSSFSheet hssfSheet = hssfWorkbook.createSheet();
|
hssfSheet.setColumnWidth(0, 6 * 256);
|
hssfSheet.setDefaultRowHeightInPoints(20f);
|
for(int i = 0; i < lists.size(); i++){
|
HSSFRow hssfRow = hssfSheet.createRow(i);//设置第一行数据(标题)
|
HSSFCellStyle style = hssfWorkbook.createCellStyle();
|
HSSFFont font = hssfWorkbook.createFont();
|
font.setBold(true);
|
style.setFont(font);
|
style.setAlignment(HorizontalAlignment.CENTER);
|
for (int l = 0; l < lists.get(i).size(); l++) {
|
HSSFCell hssfCell = hssfRow.createCell(l);
|
hssfCell.setCellType(CellType.STRING);//设置表格类型
|
hssfCell.setCellValue(lists.get(i).get(l));
|
hssfCell.setCellStyle(style);
|
if(l > 0) {
|
hssfSheet.setColumnWidth(l , 20 * 256);
|
}
|
|
}
|
}
|
|
for(int i = 0; i < lists1.size(); i++){
|
//将数据添加到表格中
|
List<String> data = null;
|
for (int l = 0; l < lists1.get(i).size(); l++) {
|
HSSFRow row = hssfSheet.createRow(l + 2);
|
data = lists1.get(i).get(l);
|
for (int j = 0; j < data.size(); j++) {
|
HSSFCell hssfCell = row.createCell(j);
|
hssfCell.setCellType(CellType.STRING);//设置表格类型
|
hssfCell.setCellValue(data.get(j));
|
}
|
}
|
}
|
return hssfWorkbook;
|
}
|
|
|
/**
|
* 发送活动提醒邮件
|
*/
|
@Override
|
public void sendActivityEmail() {
|
try {
|
List<UserActivityDiscount1> list = this.selectList(new EntityWrapper<UserActivityDiscount1>().eq("enable", 2)
|
.last(" and now() >= startTime and now() < ADDDATE(startTime,INTERVAL 1 MINUTE)"));
|
if(list.size() > 0){
|
sendEmail(list);
|
}
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
|
}
|
|
|
|
public void sendEmail(List<UserActivityDiscount1> list) throws Exception{
|
List<TUser> tUsers = userService.selectList(new EntityWrapper<TUser>().ne("flag", 3).eq("state", 1));
|
for (UserActivityDiscount1 uad : list) {
|
Map<String, Double> map = new HashMap<>();
|
Double special = uad.getSpecial();
|
if(null != special){
|
map.put("ride", special * 10);
|
}
|
Double logistics = uad.getLogistics();
|
if(null != special){
|
map.put("delivery", logistics * 10);
|
}
|
Set<String> strings = map.keySet();
|
|
for (TUser user : tUsers) {
|
if(ToolUtil.isNotEmpty(user.getEmail())){
|
Integer language = user.getLanguage();
|
for (String key : strings) {
|
Double aDouble = map.get(key);
|
String path1 = templatePath + "user/discount.html";
|
Document document1 = Jsoup.parse(new File(path1), "UTF-8");
|
if(1 == language){
|
document1.getElementById("english").remove();
|
document1.getElementById("french").remove();
|
document1.getElementsByTag("title").get(0).text("折扣活动");
|
Element chinese_user = document1.getElementById("chinese_user");
|
chinese_user.text("您好 " + user.getNickName() + ",");
|
Element chinese_ride = document1.getElementById("chinese_ride");
|
chinese_ride.text("I-GO" + (key.equals("ride") ? "打车" : "包裹") + "折扣活动奖励");
|
Element chinese_go = document1.getElementById("chinese_go");
|
if("ride".equals(key)){
|
chinese_go.text("去打车");
|
}else{
|
chinese_go.text("寄包裹");
|
}
|
Element chinese_discount = document1.getElementById("chinese_discount");
|
chinese_discount.text("每单费用折扣" + aDouble + "%");
|
EmailUtil.send(user.getEmail(), "折扣活动", document1.html());
|
}
|
if(2 == language){
|
document1.getElementById("chinese").remove();
|
document1.getElementById("french").remove();
|
document1.getElementsByTag("title").get(0).text("Discount activities");
|
Element english_user = document1.getElementById("english_user");
|
english_user.text("Hello " + user.getNickName() + ",");
|
Element english_ride = document1.getElementById("english_ride");
|
english_ride.text("Discount for I-GO " + key);
|
Element english_go = document1.getElementById("english_go");
|
if("ride".equals(key)){
|
english_go.text("REQUEST A RIDE");
|
}else{
|
english_go.text("REQUEST A DELIVERY");
|
}
|
Element english_discount = document1.getElementById("english_discount");
|
english_discount.text(" Enjoy " + new BigDecimal(100).subtract(new BigDecimal(aDouble)).setScale(2, RoundingMode.HALF_EVEN).doubleValue() + "% off your trip fare.");
|
EmailUtil.send(user.getEmail(), "Discount activities", document1.html());
|
}
|
if(3 == language){
|
document1.getElementById("chinese").remove();
|
document1.getElementById("english").remove();
|
document1.getElementsByTag("title").get(0).text("Activités à rabais");
|
Element french_user = document1.getElementById("french_user");
|
french_user.text("Cher(ère) " + user.getNickName() + ",");
|
Element french_ride = document1.getElementById("french_ride");
|
french_ride.text("Remise pour I-GO (commande de " + (key.equals("ride") ? "course" : "livraison") + ")!");
|
Element french_go = document1.getElementById("french_go");
|
if("ride".equals(key)){
|
french_go.text("Déplacez-vous avec I-GO");
|
}else{
|
french_go.text("Demander une livraison avec I-GO");
|
}
|
Element french_discount = document1.getElementById("french_discount");
|
french_discount.text("Profitez aujourd’hui de " + new BigDecimal(100).subtract(new BigDecimal(aDouble)).setScale(2, RoundingMode.HALF_EVEN).doubleValue() + "% de réduction sur les commandes de " + (key.equals("ride") ? "course" : "livraison") + ", allez sur la plate-forme I-GO pour voir le détail.");
|
EmailUtil.send(user.getEmail(), "Activités à rabais", document1.html());
|
}
|
//开始生成pdf收据和html收据
|
File file = new File("/home/igotechgh/nginx/html/files/html/");
|
if(!file.exists()){
|
file.mkdirs();
|
}
|
String randomString = ToolUtil.getRandomString(10);
|
file = new File("/home/igotechgh/nginx/html/files/html/discount_" + randomString + ".html");
|
if(!file.exists()){
|
file.createNewFile();
|
}
|
FileWriter fileWriter = new FileWriter(file);
|
fileWriter.write(document1.html());
|
fileWriter.flush();
|
fileWriter.close();
|
|
String link ="https://igo.i-go.group/files/html/discount_" + randomString + ".html";
|
TEmail tEmail = new TEmail();
|
tEmail.setLink(link);
|
tEmail.setUserId(user.getId());
|
tEmail.setType(1);
|
tEmail.setName(1 == language ? "折扣活动" : 2 == language ? "Discount activities" : "Activités à rabais");
|
tEmail.setCreateTime(new Date());
|
int i = cn.hutool.core.date.DateUtil.dayOfWeek(new Date())-1;
|
tEmail.setWeek(EmailUtil.getWeek(2,i));
|
boolean am = cn.hutool.core.date.DateUtil.isAM(new Date());
|
if(am){
|
tEmail.setAmOrPm("AM");
|
}else {
|
tEmail.setAmOrPm("PM");
|
}
|
emailService.insert(tEmail);
|
}
|
|
}
|
}
|
}
|
}
|
}
|