Pu Zhibing
2025-05-16 4c99ee7028c3fe58a2cd4b8273b22c75c45574fc
ManagementIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/controller/general/SysCouponActivityController.java
@@ -12,11 +12,17 @@
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.SinataUtil;
import com.stylefeng.guns.modular.system.model.SysCouponRecord;
import com.stylefeng.guns.modular.system.model.TUser;
import com.stylefeng.guns.modular.system.model.UserCouponRecord;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.ISysCouponRecordService;
import com.stylefeng.guns.modular.system.service.ITUserService;
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.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@@ -25,9 +31,13 @@
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.SysCouponActivity;
import com.stylefeng.guns.modular.system.service.ISysCouponActivityService;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.*;
/**
@@ -50,6 +60,12 @@
    @Autowired
    private ISysCouponRecordService couponRecordService;
    @Value("${spring.mail.template-path}")
    private String templatePath;
    @Autowired
    private TEmailService emailService;
    /**
     * 跳转到首页
@@ -203,6 +219,7 @@
            userCouponRecord.setCouponActivityId(sysCouponActivity.getId());
            userCouponRecord.setActivityType(1);
            //判断发送类型1=全部用户,2=选择用户
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(sysCouponActivity.getSendType()==1){
                Wrapper wrapper = new EntityWrapper<TUser>();
                if(sysCouponActivity.getCompanyType()!=1){
@@ -214,12 +231,31 @@
                    for (int i=0;i<sysCouponActivity.getNumber();i++){
                        userCouponRecord.insert();
                    }
                    String email = user.getEmail();
                    if(ToolUtil.isNotEmpty(email)){
                        Date expirationTime = userCouponRecord.getExpirationTime();
                        String e = sdf.format(expirationTime);
                        String s = sdf.format(new Date());
                        sendEmail(user, sysCouponActivity.getNumber(), s, e, userCouponRecord.getFullMoney().doubleValue());
                    }
                }
                sysCouponActivity.setSendUserNum(list.size());
                sysCouponActivity.updateById();
            }else{
               JSONArray  userId = JSON.parseArray(sysCouponActivity.getSendUserId());
                for(int j=0;j<userId.size();j++){
                    // 获取邮箱
                    TUser tUser = userService.selectById(userId.getJSONObject(j).getInteger("userId"));
                    String email = tUser.getEmail();
                    if(ToolUtil.isNotEmpty(email)){
                        Date expirationTime = userCouponRecord.getExpirationTime();
                        String e = sdf.format(expirationTime);
                        String s = sdf.format(new Date());
                        sendEmail(tUser, sysCouponActivity.getNumber(), s, e, userCouponRecord.getFullMoney().doubleValue());
                    }
                    userCouponRecord.setUserId(userId.getJSONObject(j).getInteger("userId"));
                    for (int i=0;i<sysCouponActivity.getNumber();i++){
                        userCouponRecord.insert();
@@ -232,6 +268,97 @@
        }
    }
    public void sendEmail(TUser userInfo1, Integer num, String startTime, String endTime, Double fullMoney){
        try {
            if(ToolUtil.isNotEmpty(userInfo1.getEmail())){
                Integer language = userInfo1.getLanguage();
                String path1 = templatePath +  "user/coupon.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("您好 " + userInfo1.getNickName() + ",");
                    Element chinese_number = document1.getElementById("chinese_number");
                    chinese_number.text("您有" + num + "张优惠券到账");
                    Element chinese_date = document1.getElementById("chinese_date");
                    chinese_date.text("此活动有效期在" + startTime + "至" + endTime + ",详情请查看I-GO平台");
                    Element chinese_remark = document1.getElementById("chinese_remark");
                    chinese_remark.text("注意:满减活动,每单消费至少GHS " + fullMoney + ",才可享用。");
                    EmailUtil.send(userInfo1.getEmail(), "优惠券到账",  document1.html());
                }
                if(2 == language){
                    document1.getElementById("chinese").remove();
                    document1.getElementById("french").remove();
                    document1.getElementsByTag("title").get(0).text("Coupon is in");
                    Element english_user = document1.getElementById("english_user");
                    english_user.text("Hello " + userInfo1.getNickName() + ",");
                    Element english_number = document1.getElementById("english_number");
                    english_number.text("You have received " + num + " coupons");
                    Element english_date = document1.getElementById("english_date");
                    english_date.text("You could use them from " + com.stylefeng.guns.modular.system.util.DateUtil.conversionFormat(2, startTime) + " to " + com.stylefeng.guns.modular.system.util.DateUtil.conversionFormat(2, endTime));
                    Element english_remark = document1.getElementById("english_remark");
                    english_remark.text("NB! Coupon is valid only for the trip fare not less than GHS " + fullMoney + " per ride.");
                    EmailUtil.send(userInfo1.getEmail(), "Coupon is in",  document1.html());
                }
                if(3 == language){
                    document1.getElementById("chinese").remove();
                    document1.getElementById("english").remove();
                    document1.getElementsByTag("title").get(0).text("Le coupon est arrivé");
                    Element french_user = document1.getElementById("french_user");
                    french_user.text("Cher(ère) " + userInfo1.getNickName() + ",");
                    Element french_number = document1.getElementById("french_number");
                    french_number.text("Vous avez reçu " + num + " coupons.");
                    Element french_date = document1.getElementById("french_date");
                    french_date.text("Vous devriez les utiliser du " + com.stylefeng.guns.modular.system.util.DateUtil.conversionFormat(3, startTime) + " au " + com.stylefeng.guns.modular.system.util.DateUtil.conversionFormat(3, endTime) + ", voir le détail sur la plate-forme I-GO.");
                    Element french_remark = document1.getElementById("french_remark");
                    french_remark.text("Remarque! Le coupon n'est valable que pour le tarif d'au moins GHS " + fullMoney + " par trajet.");
                    EmailUtil.send(userInfo1.getEmail(), "Le coupon est arrivé",  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/coupon_" + 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/coupon_" + randomString + ".html";
                TEmail tEmail = new TEmail();
                tEmail.setLink(link);
                tEmail.setUserId(userInfo1.getId());
                tEmail.setType(1);
                tEmail.setName(language == 1 ? "优惠券到账" : language == 2 ? "Coupon is in" : "Le coupon est arrivé");
                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);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 删除
     */