package cn.hmsoft.ltc.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.hmsoft.ltc.common.util.DbUtil;
import cn.hmsoft.ltc.model.SubjectOption;
import javafx.collections.ObservableList;

/**
 * 题目选项dao
 * 
 * @author zhanqiang,xuwenjin
 * @date 2019-03-29
 *
 */
public class SubjectOptionDao {

	/**
	 * 查询选项列表.
	 * 
	 * @param subjectId
	 * @return
	 * @throws SQLException
	 */
	public List<SubjectOption> findSubjectOptions(Integer subjectId) throws SQLException {
		DbUtil util = new DbUtil();
		List<SubjectOption> soList = new ArrayList<SubjectOption>();
		String sql = "select * from subject_option where 1=1";
		if (subjectId != null) {
			sql += " and subject_id=" + subjectId;
		}
		ResultSet rs = util.query(sql);
		while (rs.next()) {
			SubjectOption so = new SubjectOption();
			so.setSubjectOptionId(rs.getInt(1));
			so.setSubjectId(rs.getInt(2));
			so.setSubjectOptionNo(rs.getString(3));
			so.setSubjectOptionContent(rs.getString(4));
			so.getTextField().setText(rs.getString(4));
			soList.add(so);
		}
		return soList;
	}

	public void insertSubjectOption(Integer subjectId, ObservableList<SubjectOption> subjectOptionList) {
		DbUtil util = new DbUtil();
		for (SubjectOption subjectOption : subjectOptionList) {
			String sql = "insert into subject_option(subject_id,subject_option_no,subject_option_content) values("
					+ subjectId + ",'" + subjectOption.getSubjectOptionNo() + "','"
					+ subjectOption.getTextField().getText() + "')";
			util.insert(sql);
		}
	}

	public void deleteSubjectOption(SubjectOption subjectOption) {
		DbUtil util = new DbUtil();
		String sql = "delete from subject_option where 1=1 ";
		if (subjectOption.getSubjectId().intValue() > 0) {
			sql += " and subject_id = " + subjectOption.getSubjectId();
		}
		if (subjectOption.getSubjectOptionId().intValue() > 0) {
			sql += " and subject_option_id = " + subjectOption.getSubjectOptionId();
		}
		util.delete(sql);
	}
}
