首页 > 其他分享 >判断当月

判断当月

时间:2022-12-19 10:55:33浏览次数:31  
标签:判断 set String subStatus 当月 Calendar endTime calendar

// 实现当月查询数据----java实现

@Service("subscribeSendMsgService")
public class SubscribeSendMsgServiceImpl implements SubscribeSendMsgService{

    protected static final Logger logger = LoggerFactory.getLogger(SubscribeSendMsgServiceImpl.class);

    @Resource
    private TrSubscribeDas subscribeDas;
    @Resource
    private IMqSendMessageService mqSendMessageService;

    /**
     * 自动扣款提醒短信模板编号
     */
    private static final String SUBSCRIBE_AUTO_DEDUCTION_REMIND = "SUBSCRIBE_AUTO_DEDUCTION_REMIND";

    @Override
    public void subscribeSendMsg() {
        logger.info("subscribeSendMsg========begin");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        // 获得本月第一天
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        String startTime = sdf.format(calendar.getTime());
        //  获得本月最后一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        String endTime = sdf.format(calendar.getTime());

        // 2: 状态已订阅
        List<TrSubscribeEo> subscribedMembers = subscribeDas.getSubscribedMembers(SubscribeShopConstants.SUB_STATUS_2, startTime, endTime);
        List<TrSubscribeEo> subscribedMembers2 = subscribeDas.getSubscribedMembers2(SubscribeShopConstants.SUB_STATUS_2);
        logger.info("subscribeSendMsg开始发送短信size");
        if (subscribedMembers != null && subscribedMembers.size() > 0) {
            subscribedMembers.forEach(subscribedMember->{
                sendMsg(subscribedMember.getMemberId(), subscribedMember.getDeliveryMobile(), SUBSCRIBE_AUTO_DEDUCTION_REMIND);
            });
            logger.info("发送短信条数: {}", subscribedMembers.size());
        }
        logger.info("subscribeSendMsg========end");
    }

    /**
     * 发送短信
     * @param memberId 用户Id
     * @param mobile 手机号
     */
    private void sendMsg(Long memberId, String mobile, String msgTemplateCode) {
        if (StringUtils.isEmpty(mobile)) {
            return;
        }
        mqSendMessageService.sendSMS(mobile, msgTemplateCode, null);
    }
}

// ===================================================================

@Service
public class TrSubscribeDas extends AbstractTrBaseDas<TrSubscribeEo, TrSubscribeMapper,String> {

    @Resource
    private TrSubscribeMapper subscribeMapper;

    /**
     * 查询出参与订阅需要发送提醒短信的用户信息
     * @param subStatus 订阅状态
     * @param startTime 当月起始时间
     * @param endTime 当月月末时间
     * @return 参与订阅的相关信息
     */
    public List<TrSubscribeEo> getSubscribedMembers(Integer subStatus, String startTime, String endTime){
        return subscribeMapper.getSubscribedMembers(subStatus, startTime, endTime);
    }
}

// ==========================================================================================

public interface TrSubscribeMapper extends BaseMapper<TrSubscribeEo> {

    /**
     * 查询当月订阅的用户信息
     * @param subStatus 订阅状态
     * @return 订阅信息
     *  select * from `tr_subscribe` where DATE_FORMAT( `create_time`, '%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m');
     *
     *      Select("<script> " +
     *             "select * from `tr_subscribe` " +
     *             "where sub_status=#{subStatus} " +
     *             "and DATE_FORMAT(`create_time`, '%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m') " +
     *             "</script>")
     */
    @Select("<script> " +
            "select * from tr_subscribe " +
            "where sub_status=#{subStatus} " +
            "and create_time &lt;= #{endTime} " +
            "and create_time &gt;= #{startTime} " +
            "and dr='0' " +
            "</script>")
    List<TrSubscribeEo> getSubscribedMembers(@Param("subStatus") Integer subStatus, @Param("startTime") String startTime, @Param("endTime") String endTime);
}
// sql中实现
public interface TrSubscribeMapper extends BaseMapper<TrSubscribeEo> {
    @Select("<script> " +
            "select * from tr_subscribe " +
            "where sub_status=#{subStatus} " +
            "and DATE_FORMAT(create_time, '%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m') " +
            "and dr='0' " +
            "</script>")
    List<TrSubscribeEo> getSubscribedMembers2(@Param("subStatus") Integer subStatus);
}

=======================================================================================

@Service("subscribeSendMsgService")
public class SubscribeSendMsgServiceImpl implements SubscribeSendMsgService{

    protected static final Logger logger = LoggerFactory.getLogger(SubscribeSendMsgServiceImpl.class);

    @Resource
    private TrSubscribeDas subscribeDas;
    @Resource
    private IMqSendMessageService mqSendMessageService;

    /**
     * 自动扣款提醒短信模板编号
     */
    private static final String SUBSCRIBE_AUTO_DEDUCTION_REMIND = "SUBSCRIBE_AUTO_DEDUCTION_REMIND";

    @Override
    public void subscribeSendMsg() {
        logger.info("subscribeSendMsg========begin");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        // 获得本月第一天
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        String startTime = sdf.format(calendar.getTime());
        //  获得本月最后一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        String endTime = sdf.format(calendar.getTime());

        // 2: 状态已订阅
        List<TrSubscribeEo> subscribedMembers = subscribeDas.getSubscribedMembers(SubscribeShopConstants.SUB_STATUS_2, startTime, endTime);
        List<TrSubscribeEo> subscribedMembers2 = subscribeDas.getSubscribedMembers2(SubscribeShopConstants.SUB_STATUS_2);
        if (subscribedMembers2 != null){
            logger.info("subscribedMembers2 size: {}", subscribedMembers2.size());
        }
        logger.info("subscribeSendMsg开始发送短信size");
        if (subscribedMembers != null && subscribedMembers.size() > 0) {
            subscribedMembers.forEach(subscribedMember->{
                sendMsg(subscribedMember.getMemberId(), subscribedMember.getDeliveryMobile(), SUBSCRIBE_AUTO_DEDUCTION_REMIND);
            });
            logger.info("发送短信条数: {}", subscribedMembers.size());
        }
        logger.info("subscribeSendMsg========end");
    }

    /**
     * 发送短信
     * @param memberId 用户Id
     * @param mobile 手机号
     */
    private void sendMsg(Long memberId, String mobile, String msgTemplateCode) {
        if (StringUtils.isEmpty(mobile)) {
            return;
        }
        mqSendMessageService.sendSMS(mobile, msgTemplateCode, null);
    }
}

// ============================================================================================

@Service
public class TrSubscribeDas extends AbstractTrBaseDas<TrSubscribeEo, TrSubscribeMapper,String> {

    @Resource
    private TrSubscribeMapper subscribeMapper;

    /**
     * 查询出参与订阅需要发送提醒短信的用户信息
     * @param subStatus 订阅状态
     * @param startTime 当月起始时间
     * @param endTime 当月月末时间
     * @return 参与订阅的相关信息
     */
    public List<TrSubscribeEo> getSubscribedMembers(Integer subStatus, String startTime, String endTime){
        return subscribeMapper.getSubscribedMembers(subStatus, startTime, endTime);
    }

    public List<TrSubscribeEo> getSubscribedMembers2(Integer subStatus){
        return subscribeMapper.getSubscribedMembers2(subStatus);
    }
}

// =============================================================================================

public interface TrSubscribeMapper extends BaseMapper<TrSubscribeEo> {

    /**
     * 查询当月订阅的用户信息
     * @param subStatus 订阅状态
     * @return 订阅信息
     *  select * from `tr_subscribe` where DATE_FORMAT( `create_time`, '%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m');
     *
     *      Select("<script> " +
     *             "select * from `tr_subscribe` " +
     *             "where sub_status=#{subStatus} " +
     *             "and DATE_FORMAT(`create_time`, '%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m') " +
     *             "</script>")
     */
    @Select("<script> " +
            "select * from tr_subscribe " +
            "where sub_status=#{subStatus} " +
            "and create_time &lt;= #{endTime} " +
            "and create_time &gt;= #{startTime} " +
            "and dr='0' " +
            "</script>")
    List<TrSubscribeEo> getSubscribedMembers(@Param("subStatus") Integer subStatus, @Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select("<script> " +
            "select * from tr_subscribe " +
            "where sub_status=#{subStatus} " +
            "and DATE_FORMAT(create_time, '%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m') " +
            "and dr='0' " +
            "</script>")
    List<TrSubscribeEo> getSubscribedMembers2(@Param("subStatus") Integer subStatus);

}

标签:判断,set,String,subStatus,当月,Calendar,endTime,calendar
From: https://www.cnblogs.com/gzyup/p/16991636.html

相关文章