package com.ekexiu.portal.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 org.jfw.apt.orm.annotation.dao.DAO;
import org.jfw.apt.orm.annotation.dao.method.From;
import org.jfw.apt.orm.annotation.dao.method.operator.Insert;
import org.jfw.apt.orm.annotation.dao.method.operator.UpdateWith;
import org.jfw.apt.orm.annotation.dao.param.Set;
import org.jfw.util.exception.JfwBaseException;

import com.ekexiu.portal.po.Operation;
import com.ekexiu.portal.po.PayHistory;
import com.ekexiu.portal.po.Professor;

@DAO
public abstract class PayHistoryDao {
	@Insert
	public abstract int insert(Connection con, PayHistory payHistory) throws SQLException;
	
	@UpdateWith
	@From(PayHistory.class)
	public abstract int update(Connection con, String payHistoryId, @Set int payStatus) throws SQLException;
	
	public PayHistory query(Connection con,String payHistoryId) throws SQLException, JfwBaseException{
        int _m_1 = 1;
        String sql = "SELECT PAY_HISTORY_ID,OPERATION_ID,PAID_MONEY,RETURN_MONEY,REMARK,ISONLINE,PAY_TYPE,"
        		+ " PAY_STATUS,PAY_HISTORY.PROFESSOR_ID,PAY_HISTORY.CREATE_TIME,PROFESSOR.NAME "
        		+ " FROM PAY_HISTORY LEFT JOIN PROFESSOR ON PAY_HISTORY.PROFESSOR_ID = PROFESSOR.ID "
        		+ " WHERE PAY_HISTORY_ID = ?";
        PreparedStatement ps = con.prepareStatement(sql);
        try{
            ps.setString(_m_1++,payHistoryId);
            ResultSet rs = ps.executeQuery();
            try{
                if(rs.next()){
                   PayHistory _result = new PayHistory();
                    _result.setPayHistoryId(rs.getString(1));
                    _result.setOperationId(rs.getString(2));
                    _result.setPaidMoney(rs.getBigDecimal(3));
                    _result.setReturnMoney(rs.getBigDecimal(4));
                    String _m_2 = rs.getString(5);
                    if(rs.wasNull()){
                        _m_2 = null;
                    }
                    _result.setRemark(_m_2);
                    _result.setIsonline(rs.getInt(6));
                    _result.setPayType(rs.getInt(7));
                    _result.setPayStatus(rs.getInt(8));
                    Professor professor = new Professor();
                    professor.setId(rs.getString(9));
                    _result.setCreateTime(rs.getString(10));
                    professor.setName(rs.getString(11));
                    _result.setProfessor(professor);
                    return _result;
                }else{
                    throw new JfwBaseException(201);
                }
            }finally{
                try{rs.close();}catch(Exception _m_3){}
            }
        }finally{
            try{ps.close();}catch(Exception _m_4){}
        }
    }
	
	public List<PayHistory> queryDetail(Connection con,String operationId) throws SQLException{
        int _m_1 = 1;
        String sql = "SELECT PAY_HISTORY_ID,PAY_HISTORY.OPERATION_ID,PAID_MONEY,RETURN_MONEY,REMARK,ISONLINE,"
        		+ " PAY_TYPE,PAY_STATUS,PAY_HISTORY.CREATE_TIME,OPERATION.DEAL_PRICE,OPERATION.CREATE_TIME,"
        		+ " PAY_HISTORY.PROFESSOR_ID,PROFESSOR.NAME "
        		+ " FROM PAY_HISTORY LEFT JOIN OPERATION ON PAY_HISTORY.OPERATION_ID = OPERATION.OPERATION_ID "
        		+ " LEFT JOIN PROFESSOR ON PAY_HISTORY.PROFESSOR_ID = PROFESSOR.ID "
        		+ " WHERE PAY_HISTORY.OPERATION_ID = ?";
        PreparedStatement ps = con.prepareStatement(sql);
        try{
            ps.setString(_m_1++,operationId);
            ResultSet rs = ps.executeQuery();
            try{
                List<PayHistory> _result = new ArrayList<PayHistory>();
                while(rs.next()){
                    PayHistory _obj =  new PayHistory();
                    _obj.setPayHistoryId(rs.getString(1));
                    Operation operation = new Operation();
                    operation.setOperationId(rs.getString(2));
                    _obj.setPaidMoney(rs.getBigDecimal(3));
                    _obj.setReturnMoney(rs.getBigDecimal(4));
                    java.lang.String _m_2 = rs.getString(5);
                    if(rs.wasNull()){
                        _m_2 = null;
                    }
                    _obj.setRemark(_m_2);
                    _obj.setIsonline(rs.getInt(6));
                    _obj.setPayType(rs.getInt(7));
                    _obj.setPayStatus(rs.getInt(8));
                    _obj.setCreateTime(rs.getString(9));
                    operation.setDealPrice(rs.getBigDecimal(10));
                    operation.setCreateTime(rs.getString(11));
                    _obj.setOperation(operation);
                    Professor professor = new Professor();
                    professor.setId(rs.getString(12));
                    professor.setName(rs.getString(13));
                    _obj.setProfessor(professor);
                    _result.add(_obj);
                }
                return _result;
            }finally{
                try{rs.close();}catch(Exception _m_3){}
            }
        }finally{
            try{ps.close();}catch(Exception _m_4){}
        }
    }
	
}
