me.redstarstar.rdfx.duty.dao.jdbc.ScheduleJdbcDao.java Source code

Java tutorial

Introduction

Here is the source code for me.redstarstar.rdfx.duty.dao.jdbc.ScheduleJdbcDao.java

Source

/*
 * Copyright 2014 Aliyun.com All right reserved. This software is the
 * confidential and proprietary information of Aliyun.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Aliyun.com.
 */
package me.redstarstar.rdfx.duty.dao.jdbc;

import me.redstarstar.rdfx.duty.Schedule;
import me.redstarstar.rdfx.duty.dao.ScheduleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ?(), lingkong.hx@alibaba-inc.com
 * @version 1.0
 */
@Repository
public class ScheduleJdbcDao implements ScheduleDao {

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public ScheduleJdbcDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public int countTotalByParentId(long parentId) {
        return jdbcTemplate.execute((Statement statement) -> {
            ResultSet rs = statement.executeQuery("SELECT count(*) FROM schedule WHERE parent_id = " + parentId);
            int count = 0;
            if (rs.next()) {
                count = rs.getInt(1);
            }
            return count;
        });
    }

    @Override
    public Schedule getScheduleByWeek(long parentId, int week) {
        return jdbcTemplate.execute((Statement statement) -> {
            ResultSet rs = statement.executeQuery("SELECT * FROM schedule WHERE parent_id = " + parentId
                    + " AND week = " + week + " ORDER BY activity_date");
            Schedule schedule = null;
            if (rs.next()) {
                schedule = new Schedule();
                schedule.setActivityDate(rs.getDate("activity_date").toLocalDate());
                schedule.setParentId(rs.getLong("parent_id"));
                schedule.setWeek(rs.getInt("week"));
            }
            return schedule;
        });
    }

    @Override
    public List<Schedule> listSchedulesByWeek(int week) {
        return jdbcTemplate.execute((Statement statement) -> {
            ResultSet rs = statement
                    .executeQuery("SELECT * FROM schedule WHERE week = " + week + " ORDER BY activity_date");
            List<Schedule> schedules = new ArrayList<>();
            while (rs.next()) {
                Schedule schedule = new Schedule();
                schedule.setActivityDate(rs.getDate("activity_date").toLocalDate());
                schedule.setParentId(rs.getLong("parent_id"));
                schedule.setWeek(rs.getInt("week"));
                schedules.add(schedule);
            }
            return schedules;
        });
    }

    @Override
    public void deleteByWeek(int week) {
        jdbcTemplate.execute((Statement statement) -> {
            statement.execute("DELETE FROM schedule WHERE week = " + week);
            return null;
        });
    }

    @Override
    public void save(Schedule schedule) {
        jdbcTemplate.execute("INSERT INTO schedule (parent_id, week, activity_date, create_date, update_date) "
                + "VALUES (?, ?, ?, NOW(), NOW());", (PreparedStatement preparedStatement) -> {
                    preparedStatement.setLong(1, schedule.getParentId());
                    preparedStatement.setInt(2, schedule.getWeek());
                    preparedStatement.setDate(3, Date.valueOf(schedule.getActivityDate()));
                    preparedStatement.execute();
                    return null;
                });
    }

    @Override
    public void delete(long parentId, int week) {
        jdbcTemplate.execute("DELETE FROM schedule WHERE parent_id = ? AND week = ?",
                (PreparedStatement preparedStatement) -> {
                    preparedStatement.setLong(1, parentId);
                    preparedStatement.setInt(2, week);
                    preparedStatement.execute();
                    return null;
                });
    }

    @Override
    public int getLastWeek() {
        return jdbcTemplate.execute((Statement statement) -> {
            ResultSet rs = statement.executeQuery("SELECT week FROM schedule ORDER BY week DESC LIMIT 0,1");
            int count = 0;
            if (rs.next()) {
                count = rs.getInt(1);
            }
            return count;
        });
    }
}