优惠券状态改变的定时器

    技术2022-07-10  158

    原来的思路:先查询出所有的优惠券,然后遍历优惠券,比较结束时间跟当前时间,改变其状态。这是我想第一反应的方法。

                          后来别告知,这样的话服务器扛不住,分分种就崩那种,后来改变了写法,记录一下。

                        1、SQL实现

    <!--定时器修改优惠券状态--> <update id="updateByJob"> UPDATE t_discount SET state = #{setState} WHERE state = #{whereState} <if test="endTime != null"> and date_format(end_time,'%Y-%m-%d %H:%i') <![CDATA[<]]> date_format(#{endTime}, '%Y-%m-%d %H:%i') </if> <if test="startTime != null"> and date_format(start_time,'%Y-%m-%d %H:%i') <![CDATA[<=]]> date_format(#{startTime}, '%Y-%m-%d %H:%i') </if> </update>

                     2、控制层传值

    //优惠券 定时更新下载券状态 (每分钟执行一次) @Scheduled(cron = "0 0/1 * * * ? ") public void couponStatus() { Map<String, Object> map = new HashMap<>(); map.put("whereState", 1); map.put("setState", 2); map.put("startTime", new Date()); this.discountService.updateByJob(map); map = new HashMap<>(); map.put("whereState", 2); map.put("setState", 3); map.put("endTime", new Date()); this.discountService.updateByJob(map); }

                       

    Processed: 0.011, SQL: 9