UserScoreSignDaoImpl 코드 기록

package com.iflytek.edu.zx.archive.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import com.iflytek.edu.elp.common.dao.base.AbstractBaseDao;
import com.iflytek.edu.elp.common.dto.page.PageParam;
import com.iflytek.edu.zx.archive.dao.UserScoreSignDao;
import com.iflytek.edu.zx.archive.model.UserScoreSign;

/**
 * @author yywang5
 */
@Repository("userScoreSignDao")
public class UserScoreSignDaoImpl extends AbstractBaseDao implements UserScoreSignDao {

	@Override
	public void insert(UserScoreSign userScoreSign) {
		Assert.notNull(userScoreSign, "userScoreSign is required!");
		String sql = "insert into archive_userscoresign (id,topicSetId,userId,userName,classId,signUserName,phoneNum,createTime,parentTitle)";
		sql += "values(:id,:topicSetId,:userId,:userName,:classId,:signUserName,:phoneNum,:createTime,:parentTitle)";
		Map params = new HashMap();
		params.put("id", userScoreSign.getId());
		params.put("topicSetId", userScoreSign.getTopicSetId());
		params.put("userId", userScoreSign.getUserId());
		params.put("userName", userScoreSign.getUserName());
		params.put("classId", userScoreSign.getClassId());
		params.put("signUserName", userScoreSign.getSignUserName());
		params.put("phoneNum", userScoreSign.getPhoneNum());
		params.put("createTime", userScoreSign.getCreateTime());
		params.put("parentTitle", userScoreSign.getParentTitle());
		namedParameterJdbcTemplate.update(sql, params);
	}

	@Override
	public UserScoreSign find(String topicSetId, String userId) {
		Assert.notNull(topicSetId, "topicSetId is required!");
		Assert.notNull(userId, "userId is required!");
		String sql = "select * from archive_userscoresign where topicSetId=:topicSetId and userId=:userId limit 1";
		Map params = new HashMap();
		params.put("topicSetId", topicSetId);
		params.put("userId", userId);
		List ussList = namedParameterJdbcTemplate.query(sql, params, new UserScoreSignMapper());
		if (CollectionUtils.isNotEmpty(ussList)) {
			return ussList.get(0);
		}
		return null;
	}

	@Override
	public List findByClassId(String topicSetId, String classId) {
		Assert.notNull(topicSetId, "topicSetId is required!");
		Assert.notNull(classId, "classId is required!");
		String sql = "select * from archive_userscoresign where topicSetId=:topicSetId and classId=:classId ";
		Map params = new HashMap();
		params.put("topicSetId", topicSetId);
		params.put("classId", classId);
		return namedParameterJdbcTemplate.query(sql, params, new UserScoreSignMapper());
	}
	
	@Override
	public List findByClassId(Set classIds, PageParam pageParams) {
		Assert.notNull(classIds, "classIds is required!");
		Map params = new HashMap();
		params.put("classIds", classIds);
		String orderBy = StringUtils.isEmpty(pageParams.getOrderBy())? "createTime":pageParams.getOrderBy();
		String orderDirection = pageParams.getOrderDirection() == null? "desc":pageParams.getOrderDirection().name();
		String sql = "select * from archive_userscoresign where classId in (:classIds) order by " +orderBy+ " "+orderDirection+" limit :startIndex, :pageSize";
		return namedParameterJdbcTemplate.query(sql, params, new UserScoreSignMapper());
	}

	@Override
	public List findByUserIds(Set userIds, PageParam pageParams) {
		Assert.notEmpty(userIds,"userIds is required!");
		Map params = new HashMap();
		params.put("userIds", userIds);
		String orderBy = StringUtils.isEmpty(pageParams.getOrderBy())? "createTime":pageParams.getOrderBy();
		String orderDirection = pageParams.getOrderDirection() == null? "desc":pageParams.getOrderDirection().name();
		params.put("startIndex", pageParams.getStartIndex());
		params.put("pageSize", pageParams.getPageSize());
		String sql = "select * from archive_userscoresign where userId in (:userIds) order by " +orderBy+ " "+orderDirection+" limit :startIndex, :pageSize";
		return namedParameterJdbcTemplate.query(sql, params, new UserScoreSignMapper());
	}

	private class UserScoreSignMapper implements RowMapper {
		@Override
		public UserScoreSign mapRow(ResultSet rs, int rowNum) throws SQLException {
			UserScoreSign userScoreSign = new UserScoreSign();
			userScoreSign.setId(rs.getString("id"));
			userScoreSign.setClassId(rs.getString("classId"));
			userScoreSign.setTopicSetId(rs.getString("topicSetId"));
			userScoreSign.setPhoneNum(rs.getString("phoneNum"));
			userScoreSign.setSignUserName(rs.getString("signUserName"));
			userScoreSign.setUserId(rs.getString("userId"));
			userScoreSign.setUserName(rs.getString("userName"));
			userScoreSign.setCreateTime(rs.getTimestamp("createTime"));
			userScoreSign.setParentTitle(rs.getString("parentTitle"));
			return userScoreSign;
		}
	}

}

좋은 웹페이지 즐겨찾기