Pu Zhibing
18 小时以前 98b09eae533537dc9a5277aa6374bd7d35cfe5c4
修改bug和定时任务
5个文件已修改
286 ■■■■■ 已修改文件
ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/task/TaskUtil.java 62 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-service/ruoyi-chargingPile/src/main/java/com/ruoyi/chargingPile/controller/SiteController.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-service/ruoyi-chargingPile/src/main/java/com/ruoyi/chargingPile/service/impl/TChargingPileServiceImpl.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/util/task/TaskUtil.java 219 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml
@@ -191,6 +191,7 @@
             <if test="roleType != null and roleType != ''">roleType,</if>
             <if test="remark != null and remark != ''">remark,</if>
             <if test="objectId != null and objectId != ''">objectId,</if>
            <if test="passWordUpdate != null and passWordUpdate != ''">passWordUpdate,</if>
             create_time
         )values(
             <if test="userId != null and userId != ''">#{userId},</if>
@@ -207,6 +208,7 @@
             <if test="roleType != null and roleType != ''">#{roleType},</if>
             <if test="remark != null and remark != ''">#{remark},</if>
             <if test="objectId != null and objectId != ''">#{objectId},</if>
            <if test="passWordUpdate != null and passWordUpdate != ''">#{passWordUpdate},</if>
             sysdate()
         )
    </insert>
ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/task/TaskUtil.java
@@ -5,6 +5,8 @@
import com.ruoyi.account.api.model.TAppUserVipDetail;
import com.ruoyi.account.service.TAppUserService;
import com.ruoyi.account.service.TAppUserVipDetailService;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@@ -18,7 +20,7 @@
 * 定时任务工具类
 */
@Component
public class TaskUtil {
public class TaskUtil implements ApplicationListener<WebServerInitializedEvent> {
    
    @Resource
    private TAppUserVipDetailService tAppUserVipDetailService;
@@ -26,26 +28,30 @@
    @Resource
    private TAppUserService appUserService;
    
    private Integer port = null;
    
    /**
     * 每隔一分钟去处理的定时任务
     */
    @Scheduled(fixedRate = 1000 * 60)
    public void sendVipCoupon() {
        try {
            //会员优惠次数使用完后重新续次数
            LocalDateTime currentDate = LocalDateTime.now();
            List<TAppUserVipDetail> recentDetails = tAppUserVipDetailService.lambdaQuery()
                    .le(TAppUserVipDetail::getStartTime, currentDate)
                    .ge(TAppUserVipDetail::getEndTime, currentDate)
                    .ne(TAppUserVipDetail::getVipType, 1)
                    .le(TAppUserVipDetail::getMonthEndTime, currentDate)
                    .orderByDesc(TAppUserVipDetail::getStartTime).list();
            for (TAppUserVipDetail recentDetail : recentDetails) {
                tAppUserVipDetailService.giveVipCoupun(recentDetail);
        if(null != port && port == 5200){
            try {
                //会员优惠次数使用完后重新续次数
                LocalDateTime currentDate = LocalDateTime.now();
                List<TAppUserVipDetail> recentDetails = tAppUserVipDetailService.lambdaQuery()
                        .le(TAppUserVipDetail::getStartTime, currentDate)
                        .ge(TAppUserVipDetail::getEndTime, currentDate)
                        .ne(TAppUserVipDetail::getVipType, 1)
                        .le(TAppUserVipDetail::getMonthEndTime, currentDate)
                        .orderByDesc(TAppUserVipDetail::getStartTime).list();
                for (TAppUserVipDetail recentDetail : recentDetails) {
                    tAppUserVipDetailService.giveVipCoupun(recentDetail);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        
@@ -53,20 +59,28 @@
    
    @Scheduled(fixedRate = 1000 * 60)
    public void changeVipId() {
        LocalDateTime currentDate = LocalDateTime.now();
        List<TAppUserVipDetail> recentDetails = tAppUserVipDetailService.lambdaQuery()
                .le(TAppUserVipDetail::getStartTime, currentDate)
                .ge(TAppUserVipDetail::getEndTime, currentDate)
                .orderByDesc(TAppUserVipDetail::getStartTime).list();
        for (TAppUserVipDetail recentDetail : recentDetails) {
            TAppUser byId = appUserService.getById(recentDetail.getAppUserId());
            if (byId != null) {
                byId.setVipId(recentDetail.getVipId());
                appUserService.updateById(byId);
        if(null != port && port == 5200){
            LocalDateTime currentDate = LocalDateTime.now();
            List<TAppUserVipDetail> recentDetails = tAppUserVipDetailService.lambdaQuery()
                    .le(TAppUserVipDetail::getStartTime, currentDate)
                    .ge(TAppUserVipDetail::getEndTime, currentDate)
                    .orderByDesc(TAppUserVipDetail::getStartTime).list();
            for (TAppUserVipDetail recentDetail : recentDetails) {
                TAppUser byId = appUserService.getById(recentDetail.getAppUserId());
                if (byId != null) {
                    byId.setVipId(recentDetail.getVipId());
                    appUserService.updateById(byId);
                }
            }
        }
        
    }
    
    
    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        port = event.getWebServer().getPort();
        System.out.println("端口号:" + port);
    }
}
ruoyi-service/ruoyi-chargingPile/src/main/java/com/ruoyi/chargingPile/controller/SiteController.java
@@ -128,6 +128,7 @@
     * @param number
     * @return
     */
    @Log(title = "【扫一扫】通过桩编号获取电站信息", businessType = BusinessType.OTHER,operatorType = OperatorType.MOBILE)
    @GetMapping("/getSiteInfoByNumber")
    @ApiOperation(value = "扫一扫后通过桩编号获取电站信息", tags = {"小程序-扫一扫"})
    public R<SiteInfoVO> getSiteInfoByNumber(@RequestParam("number") String number){
ruoyi-service/ruoyi-chargingPile/src/main/java/com/ruoyi/chargingPile/service/impl/TChargingPileServiceImpl.java
@@ -711,7 +711,7 @@
        keys.forEach(key -> {
                        Long time = (Long) redisTemplate.opsForHash().get("charging_gun_online", key);
                        if(null != time && System.currentTimeMillis() - time > 300000){
                            log.info("设备编号:{}已离线", key);
                            log.info("设备编号:{} 已离线", key);
                            log.info("间隔时间", System.currentTimeMillis() - time);
                            fullNumbers.add(key.toString());
                        }
ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/util/task/TaskUtil.java
@@ -11,6 +11,8 @@
import com.ruoyi.order.service.TChargingOrderService;
import org.apache.logging.log4j.core.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@@ -30,7 +32,7 @@
 * @date 2023/7/11 8:39
 */
@Component
public class TaskUtil {
public class TaskUtil implements ApplicationListener<WebServerInitializedEvent> {
    @Resource
    private TChargingBillService chargingBillService;
@@ -38,124 +40,135 @@
    private SiteClient siteClient;
    @Resource
    private TChargingOrderService chargingOrderService;
    private Integer port = null;
    @Scheduled(cron = "0 0 0 2 * ?")
    public void taskMonth() {
        try {
            // 获取上个月的开始和结束日期
            LocalDate firstDayOfLastMonth = LocalDate.now().minusMonths(1).
                    withDayOfMonth(2);
            String string = firstDayOfLastMonth.toString();
            // 将-替换为空字符串
            string = string.replace("-", "");
            // 生成一次全站订单
            TChargingBill tChargingBill = new TChargingBill();
            // 订单生成规则JSD+20231201(账单所属月份)+1131304205(随机10位数)+001(当月账单序号,每月重置)
            Random random = new Random();
            String randomDigits = random.ints(10, 0, 10) // 生成10个随机数字,范围在0-9
                    .mapToObj(String::valueOf)
                    .collect(Collectors.joining()); // 将其连接成一个字符串
            tChargingBill.setCode("JSD"+string+randomDigits );
            tChargingBill.setType(1);
            tChargingBill.setSiteId(0);
            tChargingBill.setBillTime(LocalDateTime.now());
            tChargingBill.setStatus(1);
            tChargingBill.setOrderState(2);
            tChargingBill.setBillType(1);
            System.err.println("定时任务生成充电算帐单:全站");
            chargingBillService.save(tChargingBill);
            // 生成一次全站订单
            TChargingBill tChargingBill1 = new TChargingBill();
            // 订单生成规则JSD+20231201(账单所属月份)+1131304205(随机10位数)+001(当月账单序号,每月重置)
            Random random1 = new Random();
            String randomDigits1 = random1.ints(10, 0, 10) // 生成10个随机数字,范围在0-9
                    .mapToObj(String::valueOf)
                    .collect(Collectors.joining()); // 将其连接成一个字符串
            tChargingBill1.setCode("JSD"+string+randomDigits1 );
            tChargingBill1.setType(1);
            tChargingBill1.setSiteId(0);
            tChargingBill1.setBillTime(LocalDateTime.now());
            tChargingBill1.setStatus(1);
            tChargingBill1.setOrderState(2);
            tChargingBill1.setBillType(2);
            System.err.println("定时任务生成账户结算帐单:全站");
            chargingBillService.save(tChargingBill1);
            System.err.println("定时任务生成各个站点结算帐单");
            List<Integer> collect = siteClient.getSiteAll().getData().stream().map(Site::getId).collect(Collectors.toList());
            // 充电算帐单
            List<TChargingBill> tChargingBills = new ArrayList<>();
            // 账户结算账单
            List<TChargingBill> tChargingBills1 = new ArrayList<>();
            for (int i = 0; i < collect.size(); i++) {
                TChargingBill tChargingBill2 = new TChargingBill();
                Random random2 = new Random();
                String randomDigits2 = random2.ints(10, 0, 10) // 生成10个随机数字,范围在0-9
        if(null != port && port == 5400){
            try {
                // 获取上个月的开始和结束日期
                LocalDate firstDayOfLastMonth = LocalDate.now().minusMonths(1).
                        withDayOfMonth(2);
                String string = firstDayOfLastMonth.toString();
                // 将-替换为空字符串
                string = string.replace("-", "");
                // 生成一次全站订单
                TChargingBill tChargingBill = new TChargingBill();
                // 订单生成规则JSD+20231201(账单所属月份)+1131304205(随机10位数)+001(当月账单序号,每月重置)
                Random random = new Random();
                String randomDigits = random.ints(10, 0, 10) // 生成10个随机数字,范围在0-9
                        .mapToObj(String::valueOf)
                        .collect(Collectors.joining()); // 将其连接成一个字符串
                tChargingBill2.setCode("JSD"+string+randomDigits2+(i+1) );
                tChargingBill2.setType(2);
                List<TChargingOrder> list = chargingOrderService.lambdaQuery()
                        .eq(TChargingOrder::getSiteId, collect.get(i)).list();
                if (list.isEmpty()){
                    continue;
                tChargingBill.setCode("JSD"+string+randomDigits );
                tChargingBill.setType(1);
                tChargingBill.setSiteId(0);
                tChargingBill.setBillTime(LocalDateTime.now());
                tChargingBill.setStatus(1);
                tChargingBill.setOrderState(2);
                tChargingBill.setBillType(1);
                System.err.println("定时任务生成充电算帐单:全站");
                chargingBillService.save(tChargingBill);
                // 生成一次全站订单
                TChargingBill tChargingBill1 = new TChargingBill();
                // 订单生成规则JSD+20231201(账单所属月份)+1131304205(随机10位数)+001(当月账单序号,每月重置)
                Random random1 = new Random();
                String randomDigits1 = random1.ints(10, 0, 10) // 生成10个随机数字,范围在0-9
                        .mapToObj(String::valueOf)
                        .collect(Collectors.joining()); // 将其连接成一个字符串
                tChargingBill1.setCode("JSD"+string+randomDigits1 );
                tChargingBill1.setType(1);
                tChargingBill1.setSiteId(0);
                tChargingBill1.setBillTime(LocalDateTime.now());
                tChargingBill1.setStatus(1);
                tChargingBill1.setOrderState(2);
                tChargingBill1.setBillType(2);
                System.err.println("定时任务生成账户结算帐单:全站");
                chargingBillService.save(tChargingBill1);
                System.err.println("定时任务生成各个站点结算帐单");
                List<Integer> collect = siteClient.getSiteAll().getData().stream().map(Site::getId).collect(Collectors.toList());
                // 充电算帐单
                List<TChargingBill> tChargingBills = new ArrayList<>();
                // 账户结算账单
                List<TChargingBill> tChargingBills1 = new ArrayList<>();
                for (int i = 0; i < collect.size(); i++) {
                    TChargingBill tChargingBill2 = new TChargingBill();
                    Random random2 = new Random();
                    String randomDigits2 = random2.ints(10, 0, 10) // 生成10个随机数字,范围在0-9
                            .mapToObj(String::valueOf)
                            .collect(Collectors.joining()); // 将其连接成一个字符串
                    tChargingBill2.setCode("JSD"+string+randomDigits2+(i+1) );
                    tChargingBill2.setType(2);
                    List<TChargingOrder> list = chargingOrderService.lambdaQuery()
                            .eq(TChargingOrder::getSiteId, collect.get(i)).list();
                    if (list.isEmpty()){
                        continue;
                    }
                    tChargingBill2.setSiteId(collect.get(i));
                    tChargingBill2.setBillTime(LocalDateTime.now());
                    tChargingBill2.setBillType(1);
                    tChargingBill2.setStatus(1);
                    tChargingBill2.setOrderState(2);
                    tChargingBills.add(tChargingBill2);
                    TChargingBill tChargingBill3 = new TChargingBill();
                    Random random3 = new Random();
                    String randomDigits3 = random3.ints(10, 0, 10) // 生成10个随机数字,范围在0-9
                            .mapToObj(String::valueOf)
                            .collect(Collectors.joining()); // 将其连接成一个字符串
                    tChargingBill3.setCode("JSD"+string+randomDigits3+(i+1) );
                    tChargingBill3.setType(2);
                    tChargingBill3.setSiteId(collect.get(i));
                    tChargingBill3.setBillTime(LocalDateTime.now());
                    tChargingBill3.setBillType(2);
                    tChargingBill3.setStatus(1);
                    tChargingBill3.setOrderState(2);
                    tChargingBills1.add(tChargingBill3);
                }
                tChargingBill2.setSiteId(collect.get(i));
                tChargingBill2.setBillTime(LocalDateTime.now());
                tChargingBill2.setBillType(1);
                tChargingBill2.setStatus(1);
                tChargingBill2.setOrderState(2);
                tChargingBills.add(tChargingBill2);
                TChargingBill tChargingBill3 = new TChargingBill();
                Random random3 = new Random();
                String randomDigits3 = random3.ints(10, 0, 10) // 生成10个随机数字,范围在0-9
                        .mapToObj(String::valueOf)
                        .collect(Collectors.joining()); // 将其连接成一个字符串
                tChargingBill3.setCode("JSD"+string+randomDigits3+(i+1) );
                tChargingBill3.setType(2);
                tChargingBill3.setSiteId(collect.get(i));
                tChargingBill3.setBillTime(LocalDateTime.now());
                tChargingBill3.setBillType(2);
                tChargingBill3.setStatus(1);
                tChargingBill3.setOrderState(2);
                tChargingBills1.add(tChargingBill3);
                System.err.println("列表"+tChargingBills);
                if (!tChargingBills.isEmpty())chargingBillService.saveBatch(tChargingBills);
                if (!tChargingBills1.isEmpty())chargingBillService.saveBatch(tChargingBills1);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.err.println("列表"+tChargingBills);
            if (!tChargingBills.isEmpty())chargingBillService.saveBatch(tChargingBills);
            if (!tChargingBills1.isEmpty())chargingBillService.saveBatch(tChargingBills1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // 每个月最后一天23点执行的定时任务
    @Scheduled(cron = "0 0 23 * * ?")
    public void taskLastDay() {
        try {
            // 获取上个月的开始和结束日期
            LocalDate firstDayOfLastMonth = LocalDate.now().minusMonths(1).
                    withDayOfMonth(1);
            LocalDate lastDayOfLastMonth = LocalDate.now().minusMonths(1).
                    withDayOfMonth(firstDayOfLastMonth.lengthOfMonth());
            // 将 LocalDate 转换为 LocalDateTime,并设定时间为一天的开始
            LocalDateTime startDateTime = firstDayOfLastMonth.atStartOfDay();
            LocalDateTime endDateTime = lastDayOfLastMonth.atTime(23, 59, 59); // 设定到最后一秒
            // 构建查询条件
            LambdaQueryWrapper<TChargingBill> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.ge(TChargingBill::getBillTime,
                            Date.from(startDateTime.atZone(ZoneId.systemDefault()).toInstant()))
                    .le(TChargingBill::getBillTime,
                            Date.from(endDateTime.atZone(ZoneId.systemDefault()).toInstant())); // 使用 le 包括最后一天的记录
            List<TChargingBill> list = chargingBillService.list(queryWrapper);
            for (TChargingBill tChargingBill : list) {
                tChargingBill.setStatus(2);
        if(null != port && port == 5400){
            try {
                // 获取上个月的开始和结束日期
                LocalDate firstDayOfLastMonth = LocalDate.now().minusMonths(1).
                        withDayOfMonth(1);
                LocalDate lastDayOfLastMonth = LocalDate.now().minusMonths(1).
                        withDayOfMonth(firstDayOfLastMonth.lengthOfMonth());
                // 将 LocalDate 转换为 LocalDateTime,并设定时间为一天的开始
                LocalDateTime startDateTime = firstDayOfLastMonth.atStartOfDay();
                LocalDateTime endDateTime = lastDayOfLastMonth.atTime(23, 59, 59); // 设定到最后一秒
                // 构建查询条件
                LambdaQueryWrapper<TChargingBill> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.ge(TChargingBill::getBillTime,
                                Date.from(startDateTime.atZone(ZoneId.systemDefault()).toInstant()))
                        .le(TChargingBill::getBillTime,
                                Date.from(endDateTime.atZone(ZoneId.systemDefault()).toInstant())); // 使用 le 包括最后一天的记录
                List<TChargingBill> list = chargingBillService.list(queryWrapper);
                for (TChargingBill tChargingBill : list) {
                    tChargingBill.setStatus(2);
                }
                chargingBillService.updateBatchById(list);
            } catch (Exception e) {
                e.printStackTrace();
            }
            chargingBillService.updateBatchById(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        port = event.getWebServer().getPort();
        System.out.println("端口号:" + port);
    }
}