<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 17-6-1
 * Time: 下午4:47
 * To change this template use File | Settings | File Templates.
 */
class Service_OrderGoods extends Common_Service
{
    /**
     * @var null
     */
    public static $_modelClass = null;

    /**
     * @return Table_Orders|null
     */
    public static function getModelInstance(){
        if(is_null(self::$_modelClass)){
            self::$_modelClass = new Table_OrderGoods();
        }
        return self::$_modelClass;
    }

    public static function add($row){
        $model = self::getModelInstance();
        return $model->add($row);
    }

    public static function update($row, $value, $field = "og_id"){
        $model = self::getModelInstance();
        return $model->update($row, $value, $field);
    }

    public static function delete($value, $field = "og_id")
    {
        $model = self::getModelInstance();
        return $model->delete($value, $field);
    }

    public static function getByField($value, $field = 'og_id', $colums = "*")
    {
        $model = self::getModelInstance();
        return $model->getByField($value, $field, $colums);
    }

    public static function getAll()
    {
        $model = self::getModelInstance();
        return $model->getAll();
    }

    public static function getByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $order = "")
    {
        $model = self::getModelInstance();
        return $model->getByCondition($condition, $type, $pageSize, $page, $order);
    }

    public static function getGoodsByCondition($condition){
        $model = self::getModelInstance();
        $ogList = $model->getByCondition($condition);
        $result = array('total'=>0);
        if(!empty($ogList)){
            foreach($ogList as $row){
                $goods_code = $row['goods_id'];
                $product = Service_Product::getByField($row['product_id'],'product_id',"*");
                $goodsRow = Service_Goods::getByField($goods_code,'goods_id',"*");
                $hsUomMap = Service_HsUomMap::getByField($row['product_id'],'product_id',"*");
                if(empty($goodsRow)){
                    $goodsRow = Service_ProductGoods::getByField($goods_code,'goods_code',"*");
                    $hsUom = Service_HsUom::getByField($product['hs_code'],'hs_code',"*");
                    $goodsRow['hs_code'] = $product['hs_code'];
                    $goodsRow['hs_goods_name'] = $product['hs_goods_name'];
                    $hemList = Service_HsElementMap::getByCondition(array('product_id'=>$product['product_id']), 'hem_detail');
                    $elements = array();
                    foreach($hemList as $hemRow) {
                        $hem_detail = trim($hemRow['hem_detail']);
                        $elements[] = $hem_detail ? $hem_detail : '';
                    }
                    $goodsRow['g_model'] = implode('|',$elements);
                }else{
                    $hsUom = Service_HsUom::getByField($goodsRow['hs_code'],'hs_code',"*");
                }
                $orderProduct = Service_OrderProduct::getByCondition(array('product_id'=>$row['product_id'],'order_code'=>$row['order_code']),"*");
                if(!isset($result[$goods_code])){
                    $result[$goods_code] = array();
                    $result[$goods_code]['goods_id'] = $goods_code;
                    $result[$goods_code]['g_model'] = $goodsRow['g_model'];
                    $result[$goods_code]['customer_id'] = $product['customer_id'];
                    $result[$goods_code]['hs_code'] = $goodsRow['hs_code'];
                    $result[$goods_code]['hs_goods_name'] = $goodsRow['hs_goods_name'];
                    $result[$goods_code]['product_id'] = $row['product_id'];
                    //$result[$goods_code]['hum_quantity_law'] = $row['hum_quantity_law'];
                    $result[$goods_code]['hum_quantity_law'] = 0;
                    $result[$goods_code]['quantity'] = 0;
                    $result[$goods_code]['pu_code'] = $product['pu_code'];
                    $result[$goods_code]['pu_code_law'] = $hsUom['pu_code_law'];
                    $result[$goods_code]['currency_code'] = $product['currency_code'];
                    $result[$goods_code]['pu_code_second'] = $hsUom['pu_code_second'];
                    //$result[$goods_code]['hum_quantity_second'] = $row['hum_quantity_second'];
                    $result[$goods_code]['hum_quantity_second'] = 0;
                    $result[$goods_code]['total_price'] = 0;
                    $result[$goods_code]['net_weight'] = 0;
                }
                $result[$goods_code]['net_weight'] += $row['good_quantity']*$product['product_weight'];
                $result[$goods_code]['hum_quantity_law'] += $hsUomMap['hum_quantity_law']*$row['good_quantity'];
                //}
                $result[$goods_code]['hum_quantity_second'] += $hsUomMap['hum_quantity_second']*$row['good_quantity'];
                $result[$goods_code]['quantity'] += $row['good_quantity'];
                //$total_price = $row['quantity']*$row['product_declared_value'];
                $total_price = $orderProduct[0]['op_declared_value']*$row['good_quantity'];
                $result[$goods_code]['total_price'] += $total_price;
                $result['total'] += $total_price;
            }
        }
        return $result;
    }
}
