<?php
class Table_FeePaymentRecords
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_FeePaymentRecords();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_FeePaymentRecords();
    }

    /**
     * @param $row
     * @return mixed
     */
    public function add($row)
    {
        return $this->_table->insert($row);
    }


    /**
     * @param $row
     * @param $value
     * @param string $field
     * @return mixed
     */
    public function update($row, $value, $field = "fpr_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
        return $this->_table->update($row, $where);
    }

    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public function delete($value, $field = "fpr_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
        return $this->_table->delete($where);
    }


    public function getTotalByBill($bill_id, $statusArr = array(1, 2))
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $colums=array("sum(fpr_amount) as amount");
        $select->from($table, $colums);
        $select->where("bill_id = ?", $bill_id);
        $select->where("fpr_status in(?)", $statusArr);
        return $this->_table->getAdapter()->fetchOne($select);
    }

	/**
	 * 查询类似相关的bill_id字段
	 * @param string $like 相似字符串
	 * @param array $statusArr 服务单状态数组
	 * @param string $colum	查询字段
	 * @return string 数据数组
	 */
    public function getBillLikeId($like, $statusArr = array(1, 2), $colum = 'fpr_id'){
    	$select = $this->_table->getAdapter()->select();
    	$table = $this->_table->info('name');
    	$select->from($table, $colum);
		$select->where("bill_id like ?", $like);
        $select->where("fpr_status in(?)", $statusArr);
        return $this->_table->getAdapter()->fetchAll($select);
    }
    
    /**
     * @param $value
     * @param string $field
     * @param string $colums
     * @return mixed
     */
    public function getByField($value, $field = 'fpr_id', $colums = "*")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $colums);
        $select->where("{$field} = ?", $value);
        return $this->_table->getAdapter()->fetchRow($select);
    }


    /**
     * @param $value
     * @param string $field
     * @param string $colums
     * @return mixed
     */
    public function getJoinLeftByField($value, $field = 'ftr_id', $colums = "*")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $colums);
        $colum=array("fpra_payee_bank_name","fpra_payee_account_name","fpra_payee_account",'fpra_id');
        $select->joinLeft('fee_payment_record_attach', "fee_payment_records.fpr_id=fee_payment_record_attach.fpr_id",$colum);
        $select->where("fee_payment_records.{$field} = ?", $value);
        return $this->_table->getAdapter()->fetchRow($select);
    }


    public function getAll()
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, "*");
        return $this->_table->getAdapter()->fetchAll($select);
    }

    /**
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        
        if(isset($condition["payer_code"]) && $condition["payer_code"] != ""){
            $select->where("payer_code = ?",$condition["payer_code"]);
        }
        if(isset($condition["so_code"]) && $condition["so_code"] != ""){
            $select->where("so_code = ?",$condition["so_code"]);
        }
        if(isset($condition["pm_code"]) && $condition["pm_code"] != ""){
            $select->where("pm_code = ?",$condition["pm_code"]);
        }
        if(isset($condition["fpr_transaction_no"]) && $condition["fpr_transaction_no"] != ""){
            $select->where("fpr_transaction_no = ?",$condition["fpr_transaction_no"]);
        }
        if(isset($condition["fpr_payment_account"]) && $condition["fpr_payment_account"] != ""){
            $select->where("fpr_payment_account = ?",$condition["fpr_payment_account"]);
        }
        if(isset($condition["fpr_transaction_date"]) && $condition["fpr_transaction_date"] != ""){
            $select->where("fpr_transaction_date = ?",$condition["fpr_transaction_date"]);
        }
        if(isset($condition["fpr_status"]) && $condition["fpr_status"] != ""){
            $select->where("fpr_status = ?",$condition["fpr_status"]);
        }
        if(isset($condition["ft_code"]) && $condition["ft_code"] != ""){
            $select->where("ft_code = ?",$condition["ft_code"]);
        }
        if(isset($condition["ba_id"]) && $condition["ba_id"] != ""){
            $select->where("ba_id = ?",$condition["ba_id"]);
        }
        if(isset($condition["fpr_amount"]) && $condition["fpr_amount"] != ""){
            $select->where("fpr_amount = ?",$condition["fpr_amount"]);
        }
        if(isset($condition["fpr_currency_code"]) && $condition["fpr_currency_code"] != ""){
            $select->where("fpr_currency_code = ?",$condition["fpr_currency_code"]);
        }
        if(isset($condition["fpr_note"]) && $condition["fpr_note"] != ""){
            $select->where("fpr_note = ?",$condition["fpr_note"]);
        }
        if(isset($condition["bill_id"]) && $condition["bill_id"] != ""){
            $select->where("bill_id = ?",$condition["bill_id"]);
        }
        if(isset($condition["fpr_creator_id"]) && $condition["fpr_creator_id"] != ""){
            $select->where("fpr_creator_id = ?",$condition["fpr_creator_id"]);
        }
        if(isset($condition["fpr_confirm_id"]) && $condition["fpr_confirm_id"] != ""){
            $select->where("fpr_confirm_id = ?",$condition["fpr_confirm_id"]);
        }
        if(isset($condition["fpr_payer_id"]) && $condition["fpr_payer_id"] != ""){
            $select->where("fpr_payer_id = ?",$condition["fpr_payer_id"]);
        }
        if(isset($condition["fpr_dishonour_id"]) && $condition["fpr_dishonour_id"] != ""){
            $select->where("fpr_dishonour_id = ?",$condition["fpr_dishonour_id"]);
        }
        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            if (!empty($orderBy)) {
                $select->order($orderBy);
            }
            if ($pageSize > 0 and $page > 0) {
                $start = ($page - 1) * $pageSize;
                $select->limit($pageSize, $start);
            }
            $sql = $select->__toString();
            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }


    /**
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getSearchByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft("payment_method", "payment_method.pm_code=fee_payment_records.pm_code",array("title"));
        $attach=array(
            'fpra_payee_bank_name','fpra_payee_account_name','fpra_payee_account'
        );
        $select->joinLeft("fee_payment_record_attach", "fee_payment_record_attach.fpr_id=fee_payment_records.fpr_id",$attach);
	//	$select->joinLeft("bil_serve_bill", "bil_serve_bill.bsb_id=fee_payment_records.bill_id",array('bsb_code','bsb_no','bsb_id'));
        $select->joinLeft("fee_type", "fee_type.ft_code=fee_payment_records.ft_code",array("ft_name_cn"));
        $select->joinLeft("user as u1", "u1.user_id=fee_payment_records.fpr_creator_id",array("u1.user_name as creator"));
        $select->joinLeft("user as u2", "u2.user_id=fee_payment_records.fpr_confirm_id",array("u2.user_name as confirm"));
        $select->joinLeft("user as u3", "u3.user_id=fee_payment_records.fpr_payer_id",array("u3.user_name as payer"));
        $select->joinLeft("user as u4", "u4.user_id=fee_payment_records.fpr_dishonour_id",array("u4.user_name as dishonour"));
        $select->where("1 =?", 1);
        /*CONDITION_START*/

        if(isset($condition["payer_code"]) && $condition["payer_code"] != ""){
            $select->where("payer_code = ?",$condition["payer_code"]);
        }
        if(isset($condition["so_code"]) && $condition["so_code"] != ""){
            $select->where("so_code = ?",$condition["so_code"]);
        }
        if(isset($condition["pm_code"]) && $condition["pm_code"] != ""){
            $select->where("pm_code = ?",$condition["pm_code"]);
        }
        if(isset($condition["fpr_transaction_no"]) && $condition["fpr_transaction_no"] != ""){
            $select->where("fpr_transaction_no = ?",$condition["fpr_transaction_no"]);
        }
        if(isset($condition["fpr_payment_account"]) && $condition["fpr_payment_account"] != ""){
            $select->where("fpr_payment_account = ?",$condition["fpr_payment_account"]);
        }
        if(isset($condition["fpr_transaction_date"]) && $condition["fpr_transaction_date"] != ""){
            $select->where("fpr_transaction_date = ?",$condition["fpr_transaction_date"]);
        }
        if(isset($condition["fpr_status"]) && $condition["fpr_status"] != ""){
            $select->where("fpr_status = ?",$condition["fpr_status"]);
        }
        if(isset($condition["ft_code"]) && $condition["ft_code"] != ""){
            $select->where("ft_code = ?",$condition["ft_code"]);
        }
        if(isset($condition["ba_id"]) && $condition["ba_id"] != ""){
            $select->where("ba_id = ?",$condition["ba_id"]);
        }
        if(isset($condition["fpr_amount"]) && $condition["fpr_amount"] != ""){
            $select->where("fpr_amount = ?",$condition["fpr_amount"]);
        }
        if(isset($condition["fpr_currency_code"]) && $condition["fpr_currency_code"] != ""){
            $select->where("fpr_currency_code = ?",$condition["fpr_currency_code"]);
        }
        if(isset($condition["fpr_note"]) && $condition["fpr_note"] != ""){
            $select->where("fpr_note = ?",$condition["fpr_note"]);
        }
        if(isset($condition["bill_id"]) && $condition["bill_id"] != ""){
            $select->where("bill_id = ?",$condition["bill_id"]);
        }
        if(isset($condition["fpr_creator_id"]) && $condition["fpr_creator_id"] != ""){
            $select->where("fpr_creator_id = ?",$condition["fpr_creator_id"]);
        }
        if(isset($condition["fpr_confirm_id"]) && $condition["fpr_confirm_id"] != ""){
            $select->where("fpr_confirm_id = ?",$condition["fpr_confirm_id"]);
        }
        if(isset($condition["fpr_payer_id"]) && $condition["fpr_payer_id"] != ""){
            $select->where("fpr_payer_id = ?",$condition["fpr_payer_id"]);
        }
        if(isset($condition["fpr_dishonour_id"]) && $condition["fpr_dishonour_id"] != ""){
            $select->where("fpr_dishonour_id = ?",$condition["fpr_dishonour_id"]);
        }
        if(isset($condition["dateFor"]) && $condition["dateFor"] != ""){
            $select->where("fpr_add_time >= ?",$condition["dateFor"]);
        }
        if(isset($condition["dateTo"]) && $condition["dateTo"] != ""){
            $select->where("fpr_add_time <= ?",$condition["dateTo"]);
        }

        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            if (!empty($orderBy)) {
                $select->order($orderBy);
            }
            if ($pageSize > 0 and $page > 0) {
                $start = ($page - 1) * $pageSize;
                $select->limit($pageSize, $start);
            }
            $sql = $select->__toString();
            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }


}