<?php
class Table_BilServeBillDetail
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_BilServeBillDetail();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_BilServeBillDetail();
    }

    /**
     * @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 = "bsbd_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 = "bsbd_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
        return $this->_table->delete($where);
    }

    /**
     * @param $value
     * @param string $field
     * @param string $colums
     * @return mixed
     */
    public function getByField($value, $field = 'bsbd_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);
    }

    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["customer_code"]) && $condition["customer_code"] != ""){
            $select->where("customer_code = ?",$condition["customer_code"]);
        }
        if(isset($condition["bsb_id"]) && $condition["bsb_id"] != ""){
            $select->where("bsb_id = ?",$condition["bsb_id"]);
        }
        if(isset($condition["bsbd_hawb_code"]) && $condition["bsbd_hawb_code"] != ""){
            $select->where("bsbd_hawb_code = ?",$condition["bsbd_hawb_code"]);
        }
        if(isset($condition["bsbd_refer_hawb_code"]) && $condition["bsbd_refer_hawb_code"] != ""){
            $select->where("bsbd_refer_hawb_code = ?",$condition["bsbd_refer_hawb_code"]);
        }
        if(isset($condition["bsbd_pieces"]) && $condition["bsbd_pieces"] != ""){
            $select->where("bsbd_pieces = ?",$condition["bsbd_pieces"]);
        }
        if(isset($condition["bsbd_gross_weight"]) && $condition["bsbd_gross_weight"] != ""){
            $select->where("bsbd_gross_weight = ?",$condition["bsbd_gross_weight"]);
        }
        if(isset($condition["bsbd_volume_weight"]) && $condition["bsbd_volume_weight"] != ""){
            $select->where("bsbd_volume_weight = ?",$condition["bsbd_volume_weight"]);
        }
        if(isset($condition["bsbd_charge_weight"]) && $condition["bsbd_charge_weight"] != ""){
            $select->where("bsbd_charge_weight = ?",$condition["bsbd_charge_weight"]);
        }
        if(isset($condition["bsbd_delivered_weight"]) && $condition["bsbd_delivered_weight"] != ""){
            $select->where("bsbd_delivered_weight = ?",$condition["bsbd_delivered_weight"]);
        }
        if(isset($condition["country_code"]) && $condition["country_code"] != ""){
            $select->where("country_code = ?",$condition["country_code"]);
        }
        if(isset($condition["bsbd_sales_id"]) && $condition["bsbd_sales_id"] != ""){
            $select->where("bsbd_sales_id = ?",$condition["bsbd_sales_id"]);
        }
        if(isset($condition["bsbd_compare_sign"]) && $condition["bsbd_compare_sign"] != ""){
            $select->where("bsbd_compare_sign = ?",$condition["bsbd_compare_sign"]);
        }
        if(isset($condition["bsbd_verifier_id"]) && $condition["bsbd_verifier_id"] != ""){
            $select->where("bsbd_verifier_id = ?",$condition["bsbd_verifier_id"]);
        }

        if(isset($condition["bsbd_compare_sign_arr"]) && !empty($condition["bsbd_compare_sign_arr"]) && is_array($condition["bsbd_compare_sign_arr"])){
            $select->where("bsbd_compare_sign in(?)",$condition["bsbd_compare_sign_arr"]);
        }
        /*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 getAllByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft("bil_serve_bill_detail_fee", "bil_serve_bill_detail_fee.bsbd_id=bil_serve_bill_detail.bsbd_id");
        $select->joinLeft("bil_serve_bill_difference", "bil_serve_bill_difference.bsbd_id=bil_serve_bill_detail.bsbd_id");

        $select->joinLeft("user as u1", "u1.user_id=bil_serve_bill_detail.bsbd_verifier_id", array("u1.user_name as verifier"));

        $select->where("1 =?", 1);
        /*CONDITION_START*/

        if(isset($condition["bsb_id"]) && $condition["bsb_id"] != ""){
            $select->where("bil_serve_bill_detail.bsb_id = ?",$condition["bsb_id"]);
        }
        if(isset($condition["bsbd_hawb_code"]) && $condition["bsbd_hawb_code"] != ""){
            $select->where("bsbd_hawb_code = ?",$condition["bsbd_hawb_code"]);
        }
        if(isset($condition["bsbd_refer_hawb_code"]) && $condition["bsbd_refer_hawb_code"] != ""){
            $select->where("bsbd_refer_hawb_code = ?",$condition["bsbd_refer_hawb_code"]);
        }
        if(isset($condition["bsbd_compare_sign"]) && $condition["bsbd_compare_sign"] !== ""){
            $select->where("bsbd_compare_sign = ?",$condition["bsbd_compare_sign"]);
        }
        if(isset($condition["bsd_diff_type"]) && $condition["bsd_diff_type"] !== ""){
            $select->where("bil_serve_bill_difference.bsd_diff_type = ?",$condition["bsd_diff_type"]);
        }
        if(isset($condition["bsd_status"]) && $condition["bsd_status"] !== ""){
            $select->where("bil_serve_bill_difference.bsd_status = ?",$condition["bsd_status"]);
        }
        if(isset($condition["bsd_withdraw_sign"]) && $condition["bsd_withdraw_sign"] !== ""){
            $select->where("bil_serve_bill_difference.bsd_withdraw_sign = ?",$condition["bsd_withdraw_sign"]);
        }
        if($type!='count(*)'){
            if(isset($condition["weight_start"]) && $condition["weight_start"] !== ""){
				$condition["weight_start"] = $condition["weight_start"]+0;
                $select->where("(bil_serve_bill_detail.bsbd_fee_weight-bil_serve_bill_detail.bsbd_charge_weight) >= ?",$condition["weight_start"]);
            }
            if(isset($condition["weight_end"]) && $condition["weight_end"] !== ""){
				$condition["weight_end"]=$condition["weight_end"]+0;
                $select->where("(bil_serve_bill_detail.bsbd_fee_weight-bil_serve_bill_detail.bsbd_charge_weight) <= ?",$condition["weight_end"]);
            }
            if(isset($condition["fee_start"]) && $condition["fee_start"] !== ""){
				$condition["fee_start"]=$condition["fee_start"]+0;
                $select->having("(IFNULL(amount,0)-bil_serve_bill_detail_fee.bsbdf_amount) >= ?",$condition["fee_start"]);
            }
            if(isset($condition["fee_end"]) && $condition["fee_end"] !== ""){
				$condition["fee_end"]=$condition["fee_end"]+0;
                $select->having("(IFNULL(amount,0)-bil_serve_bill_detail_fee.bsbdf_amount) <= ?",$condition["fee_end"]);
            }
        }
        //echo $select;//exit;
        /*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);
        }
    }


    /**
     * @desc random模板 由于明细是对应多条费用
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getRandomByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft("bil_serve_bill_difference", "bil_serve_bill_difference.bsbd_id=bil_serve_bill_detail.bsbd_id");

        $select->joinLeft("user as u1", "u1.user_id=bil_serve_bill_detail.bsbd_verifier_id", array("u1.user_name as verifier"));

        $select->where("1 =?", 1);
        /*CONDITION_START*/

        if(isset($condition["bsb_id"]) && $condition["bsb_id"] != ""){
            $select->where("bil_serve_bill_detail.bsb_id = ?",$condition["bsb_id"]);
        }
        if(isset($condition["bsbd_hawb_code"]) && $condition["bsbd_hawb_code"] != ""){
            $select->where("bsbd_hawb_code = ?",$condition["bsbd_hawb_code"]);
        }
        if(isset($condition["bsbd_refer_hawb_code"]) && $condition["bsbd_refer_hawb_code"] != ""){
            $select->where("bsbd_refer_hawb_code = ?",$condition["bsbd_refer_hawb_code"]);
        }
        if(isset($condition["bsbd_compare_sign"]) && $condition["bsbd_compare_sign"] !== ""){
            $select->where("bsbd_compare_sign = ?",$condition["bsbd_compare_sign"]);
        }
        if(isset($condition["bsd_diff_type"]) && $condition["bsd_diff_type"] !== ""){
            $select->where("bil_serve_bill_difference.bsd_diff_type = ?",$condition["bsd_diff_type"]);
        }
        if(isset($condition["bsd_status"]) && $condition["bsd_status"] !== ""){
            $select->where("bil_serve_bill_difference.bsd_status = ?",$condition["bsd_status"]);
        }
        if(isset($condition["bsd_withdraw_sign"]) && $condition["bsd_withdraw_sign"] !== ""){
            $select->where("bil_serve_bill_difference.bsd_withdraw_sign = ?",$condition["bsd_withdraw_sign"]);
        }


        /*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 分页size
     * @param int $page 当前页面
     * @param string $orderBy 排序字段
     * @return string|multitype:
     */
    public function getOwnTotalByCondition($condition, $type, $pageSize, $page, $orderBy){
    	$select = $this->_table->getAdapter()->select();
    	$table = $this->_table->info('name');
    	$select->from($table, $type);
    	$select->joinLeft("bil_serve_bill_detail_fee", "bil_serve_bill_detail_fee.bsbd_id=bil_serve_bill_detail.bsbd_id");
    //	$select->joinLeft("bil_serve_bill_difference", "bil_serve_bill_difference.bsbd_id=bil_serve_bill_detail.bsbd_id");
    	$select->where("1 =?", 1);
    	if(isset($condition["bsb_id"]) && $condition["bsb_id"] != ""){
    		$select->where("bil_serve_bill_detail.bsb_id = ?", $condition["bsb_id"]);
    	}
    	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();
    //	echo $sql;
    		return $this->_table->getAdapter()->fetchAll($sql);
    	}
    }
}