package com.zrcx.millitMg.address.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.sun.org.apache.xpath.internal.operations.Gt;
import com.zrcx.millitMg.address.bean.AddressBean;
import com.zrcx.millitMg.base.BaseDao;
public class AddressDao extends BaseDao{
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String sql = null;
/**
*
* @Description: getProvince()
* @return
* List<AddressBean[]>
* @throws
* @author @Meng
* @date 2017年2月10日 上午12:45:28
*/
public List<AddressBean> getProvince() {
List<AddressBean> list = new ArrayList<AddressBean>();
try {
connection = getConnection(false);
sql = "select ProvinceID,ProvinceName from province";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
AddressBean addressBean = new AddressBean();
addressBean.setProvinceId(resultSet.getInt(1));
addressBean.setProvince(resultSet.getString(2));
list.add(addressBean);
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
close(resultSet, preparedStatement);
}
return list;
}
/**
*
* @Description: getCity(int pid)
* @param pid
* @return
* List<AddressBean>
* @throws
* @author @Meng
* @date 2017年2月10日 上午1:46:33
*/
public List<AddressBean> getCity(int pid) {
List<AddressBean> list = new ArrayList<AddressBean>();
try {
connection = getConnection(false);
sql = "select CityID,CityName from city where ProvinceId=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, pid);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
AddressBean addressBean = new AddressBean();
addressBean.setProvinceId(resultSet.getInt(1));
addressBean.setProvince(resultSet.getString(2));
list.add(addressBean);
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
close(resultSet, preparedStatement);
}
return list;
}
/**
*
* @Description: getDistrict(int cid)
* @param cid
* @return
* List<AddressBean>
* @throws
* @author @Meng
* @date 2017年2月10日 上午1:49:51
*/
public List<AddressBean> getDistrict(int cid) {
List<AddressBean> list = new ArrayList<AddressBean>();
try {
connection = getConnection(false);
sql = "select DistrictID,DistrictName from district where CityId=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, cid);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
AddressBean addressBean = new AddressBean();
addressBean.setProvinceId(resultSet.getInt(1));
addressBean.setProvince(resultSet.getString(2));
list.add(addressBean);
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
close(resultSet, preparedStatement);
}
return list;
}
/**
*
* @Description: addAddress(AddressBean addressBean)
* @param addressBean
* @return
* boolean
* @throws
* @author @Meng
* @date 2017年2月10日 上午2:43:37
*/
public boolean addAddress(AddressBean addressBean) {
try {
connection = getConnection(false);
sql = "insert into address "
+ "(`name`,province,city,district,street,postCode,phone,user_id,state) "
+ "values (?,?,?,?,?,?,?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,addressBean.getName());
preparedStatement.setString(2,addressBean.getProvince());
preparedStatement.setString(3,addressBean.getCity());
preparedStatement.setString(4,addressBean.getDistrict());
preparedStatement.setString(5,addressBean.getStreet());
preparedStatement.setString(6,addressBean.getPostCode());
preparedStatement.setString(7,addressBean.getPhone());
preparedStatement.setInt(8,addressBean.getUserId());
preparedStatement.setString(9, "0");
preparedStatement.executeUpdate();
connection.commit();
return true;
} catch (Exception e) {
e.printStackTrace();
try {
connection.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
finally{
close(preparedStatement);
}
return false;
}
/**
*
* @Description: getAddressByUserId(int userId)
* @param userId
* @return
* List<AddressBean>
* @throws
* @author @Meng
* @date 2017年2月13日 下午7:41:43
*/
public List<AddressBean> getAddressByUserId(int userId) {
List<AddressBean> list = new ArrayList<AddressBean>();
try {
connection = getConnection(false);
sql = "select id,`name`,province,city,district,street,postCode,phone,user_id,state from address where user_id=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, userId);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
AddressBean addressBean = new AddressBean();
addressBean.setId(resultSet.getInt(1));
addressBean.setName(resultSet.getString(2));
addressBean.setProvince(resultSet.getString(3));
addressBean.setCity(resultSet.getString(4));
addressBean.setDistrict(resultSet.getString(5));
addressBean.setStreet(resultSet.getString(6));
addressBean.setPostCode(resultSet.getString(7));
addressBean.setPhone(resultSet.getString(8));
addressBean.setUserId(resultSet.getInt(9));
addressBean.setState(resultSet.getString(10));
list.add(addressBean);
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
close(resultSet,preparedStatement);
}
return list;
}
/**
*
* @Description: getDefaultAddressIdByUserId(int userId)
* @param userId
* @return
* int
* @throws
* @author @Meng
* @date 2017年2月13日 下午8:38:20
*/
public int getDefaultAddressIdByUserId(int userId) {
try {
connection = getConnection(false);
sql = "select id from address where user_id=? and state='1'";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, userId);
resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
return resultSet.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
close(resultSet,preparedStatement);
}
return 0;
}
public AddressBean getPCD(int provinceId, int cityId, int districtId) {
try {
connection = getConnection(false);
sql = "select p.ProvinceName,c.CityName,d.DistrictName from province p left join city c on p.ProvinceID=c.ProvinceID left join district d on c.CityID=d.CityID where p.ProvinceID=? and c.CityID=? and d.DistrictID=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, provinceId);
preparedStatement.setInt(2, cityId);
preparedStatement.setInt(3, districtId);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
AddressBean addressBean = new AddressBean();
addressBean.setProvince(resultSet.getString(1));
addressBean.setCity(resultSet.getString(2));
addressBean.setDistrict(resultSet.getString(3));
return addressBean;
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
close(resultSet,preparedStatement);
}
return null;
}
public boolean modifyAddressState(int addressId) {
try {
connection = getConnection(false);
sql = "update address set state='0' where id<>?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, addressId);
preparedStatement.executeUpdate();
connection.commit();
} catch (SQLExceptio