package integralmanege;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
import main.MSSql;
//积分类别管理界面的处理类
public class InterlManageDW implements ActionListener {
JOptionPane jop = new JOptionPane(); // 提示窗口
InterManage inter;
IntegralType integraltype;
MSSql mssql;
List listinter = null;
boolean juge = false, res = true;
public InterlManageDW(InterManage inter) {
this.inter = inter;
jop.setSize(200, 200); // 提示窗口的大小
}
@Override
public void actionPerformed(ActionEvent e) {
mssql = new MSSql();
String btn = e.getActionCommand();
if (btn.equals("增加类别")) {
IntegralType integraltype = new IntegralType();
inter.model.addRow(inter.tabData.getSelectedRow(), integraltype);
} else if (btn.equals("删除类别")) {
int[] rows = inter.tabData.getSelectedRows();
if (rows.length > 0) {
int flag = jop.showConfirmDialog(null, "确定删除?");
if (flag == jop.YES_OPTION) {
ArrayList list = new ArrayList();
for (int i = rows.length - 1; i >= 0; i--) {
list.add(inter.model.getRow(rows[i]));
inter.model.deleteRow(rows[i]);
DeleteIntegralType(list);
}
} else {
jop.showMessageDialog(null, "请选择要删除的行!");
}
}
} else if (btn.equals("提交")) {
List changeList = inter.model.getChangeList();
if (changeList.size() > 0) {
if (UpdateIntegralType(changeList)) {
changeList.clear();
if (res == true)
jop.showMessageDialog(null, "修改成功!");
}
}
List newrow = inter.model.getNewRow();
if (newrow.size() > 0) {
if (AddIntegralType(newrow)) {
inter.tabData.updateUI();
newrow.clear();
if(res==true)
jop.showMessageDialog(null, "添加积分类别成功!");
}
}
} else if (btn.equals("刷新")) {
int row = inter.integrallist.size();
for (int i = row; i >= 0; i--) {
inter.model.deleteRow(i);
}
listinter = getList();
for (int j = 0; j < listinter.size(); j++) {
integraltype = (IntegralType) listinter.get(j);
inter.model.addRow(j, integraltype);
}
}
}
private boolean AddIntegralType(List list) {
String s1 = "";
PreparedStatement pmt = null;
Iterator it = list.iterator();
Iterator it2 = inter.integrallist.iterator();
try {
while (it.hasNext()) {
IntegralType integraltype = (IntegralType) it.next();
String name = integraltype.getIntegralName();
int up = integraltype.getIntegralUp();
int low = integraltype.getIntegralLow();
if (up <= low) {
jop.showMessageDialog(null, "积分上下限取值不合法!");
} else {
while (it2.hasNext()) {
IntegralType integraltypes = new IntegralType();
integraltypes = (IntegralType) it2.next();
/*if (up <= integraltype.getIntegralUp()
&& up >= integraltype.getIntegralLow()
|| low <= integraltype.getIntegralUp()
&& low >= integraltype.getIntegralLow()) {
jop.showMessageDialog(null, "积分上限或下限取值不合法!");
res = false;
break;
}*/
}
if (res == true) {
s1 = "insert into intergalmanage(类别名称,"
+ "积分上限,积分下限,人数) values(?,?,?,?)";
pmt = mssql.conn.prepareStatement(s1);
pmt.setString(1, name);
pmt.setInt(2, up);
pmt.setInt(3, low);
pmt.setInt(4, 0);
pmt.executeUpdate();
juge = true;
}
}
}
} catch (SQLException e) {
jop.showMessageDialog(null, "该积分类别已经存在!");
juge = false;
} finally {
try {
if (pmt != null)
pmt.close();
if (mssql.conn != null)
mssql.conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return juge;
}
private boolean DeleteIntegralType(List list) {
int id = 0;
Iterator it = list.iterator();
PreparedStatement pmt = null;
String sql = "delete from intergalmanage where id=?";
try {
while (it.hasNext()) {
id = ((IntegralType) it.next()).getIntegralId();
pmt = mssql.conn.prepareStatement(sql);
pmt.setInt(1, id);
pmt.executeUpdate();
juge = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (mssql.conn != null)
mssql.conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return juge;
}
public boolean UpdateIntegralType(List list) {
PreparedStatement pmt = null;
ResultSet rs = null;
Statement stmt = null;
Iterator it = list.iterator();
int id = 0;
int column = 0;
String value = "";
String str = "";
Iterator it2 = inter.integrallist.iterator();
boolean result = true;
try {
while (it.hasNext()) {
ChangeIntegral changeinter = (ChangeIntegral) it.next();
id = changeinter.getId();
column = changeinter.getColumn();
value = changeinter.getValue();
int scode = Integer.valueOf(value);
switch (column) {
case 1:
while (it2.hasNext()) {
IntegralType integraltype = new IntegralType();
integraltype = (IntegralType) it2.next();
if (integraltype.getIntegralId() != id) {
if (integraltype.getIntegralName().equals(value)) {
jop.showMessageDialog(null, "该积分类别已经存在!");
result = false;
break;
}
}
}
if (result == true) {
str = "update intergalmanage set 类别名称=? where id=?";
juge = true;
}
break;
case 2:
while (it2.hasNext()) {
IntegralType integraltype = new IntegralType();
integraltype = (IntegralType) it2.next();
if (integraltype.getIntegralId() != id) {
if (scode <= integraltype.getIntegralUp()
&& scode >= integraltype.getIntegralLow()) {
jop.showMessageDialog(null, "积分上限取值不正确!");
result = false;
res = false;
break;
}
}
}
if (result == true) {
str = "update intergalmanage set 积分上限=? where id=?";
juge = true;
}
break;
case 3:
while (it2.hasNext()) {
IntegralType integraltype = new IntegralType();
integraltype = (IntegralType) it2.next();
if (integraltype.getIntegralId() != id) {
if (scode <= integraltype.getIntegralUp()
&& scode >= integraltype.getIntegralLow()) {
jop.showMessageDialog(null, "积分下限取值不正确!");
result = false;
res = false;
break;
}
}
}
if (result == true) {
str = "update intergalmanage set 积分下限=? where id=?";
juge = true;
}
}
if (result == true) {
pmt = mssql.conn.prepareStatement(str);
pmt.setString(1, value);
pmt.setInt(2, id);
pmt.executeUpdate();
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pmt != null)
pmt.close();
if (mssql.conn != null)
mssql.conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return juge;
}
// 得到存放有积分类别信息的List
public List getList() {
SrueNumber();
mssql = new MSSql();
String s1 = "select * from intergalmanage order by id asc";// 选择积分信息按正序排序
Statement stmt = null;
ResultSet rs = null;
List list = new ArrayList();
try {
stmt = mssq