<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 17-10-27
 * Time: 下午4:31
 * To change this template use File | Settings | File Templates.
 */
class Service_ShipBatchGoods extends Common_Service
{
    /**
     * @var null
     */
    public static $_modelClass = null;

    /**
     * @return Table_ShipBatchLog|null
     */
    public static function getModelInstance()
    {
        if (is_null(self::$_modelClass)) {
            self::$_modelClass = new Table_ShipBatchGoods();
        }
        return self::$_modelClass;
    }

    /**
     * @param $row
     * @return mixed
     */
    public static function add($row)
    {
        $model = self::getModelInstance();
        return $model->add($row);
    }


    /**
     * @param $row
     * @param $value
     * @param string $field
     * @return mixed
     */
    public static function update($row, $value, $field = "sbg_id")
    {
        $model = self::getModelInstance();
        return $model->update($row, $value, $field);
    }

    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public static function delete($value, $field = "sbg_id")
    {
        $model = self::getModelInstance();
        return $model->delete($value, $field);
    }

    /**
     * @param $value
     * @param string $field
     * @param string $colums
     * @return mixed
     */
    public static function getByField($value, $field = 'sbg_id', $colums = "*")
    {
        $model = self::getModelInstance();
        return $model->getByField($value, $field, $colums);
    }

    /**
     * @return mixed
     */
    public static function getAll()
    {
        $model = self::getModelInstance();
        return $model->getAll();
    }

    /**
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $order
     * @return mixed
     */
    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 getHsinfoExportBySbcode($condition ,$type ='' , $pageSize = 0, $page = 1, $orderBy = ""){
        $model = self::getModelInstance();
        $result = $model->getHsinfoExportBySbcode($condition,$type,$pageSize,$page,$orderBy);
        if(empty($result)){
            $result = Service_ShipBatch::getHsinfoExportBySbcode($condition,$type,$pageSize,$page,$orderBy);
        }else{
            foreach($result as $key=>$value){
                $op = Service_OrderProduct::getByCondition(array('order_code'=>$value['order_code'],'product_id'=>$value['product_id']),"*");
                $result[$key]['op_declared_value'] = $op[0]['op_declared_value'];
                if($value['product_id']!=$value['from_product_id']){
                    $p = Service_Product::getByField($value['product_id'],'product_id',"*");
                    $result[$key]['product_sku'] = $p['product_sku'];
                    $result[$key]['pu_code'] = $p['pu_code'];
                    $result[$key]['product_weight'] = $p['product_weight'];
                    $result[$key]['real_goods_id'] = $p['goods_id'];
                }
            }
        }
        return $result;
    }

    public function getProductsByCondition($condition,$type = '*', $pageSize = 0, $page = 1, $orderBy = ""){
        $model = self::getModelInstance();
        $list=  $model->getProductsByCondition($condition,$type, $pageSize, $page, $orderBy);
        if(empty($list[0]['c'])){
            $pu= new Service_ProductUom();
            $result = array();
            $existGoodsId = array();
            foreach ($list as $k => $v) {
                $op = Service_OrderProduct::getByCondition(array('order_code'=>$v['order_code'],"product_id"=>$v['op_product_id']),"*");
                $ghkey = $v['goods_id']."-".$v['hs_serial_no'];
                if(!in_array($ghkey,$existGoodsId)){
                    $existGoodsId[] = $ghkey;
                    $list[$k]['declared_value']=$op[0]['op_declared_value']*$v['quantity'];
                    $list[$k]['cw']=$v['cw'];
                    if(empty($v['product_id'])){
                        unset($list[$k]);
                        continue;
                    }
                    if(!empty($v['pu_code'])){
                        $pu_code=$pu->getByField($v['pu_code'],'pu_code','pu_name');
                        $list[$k]['pu_code']=$pu_code['pu_name'];
                    }
                    if(!empty($v['pu_code_law'])){
                        $pu_code_law=$pu->getByField($v['pu_code_law'],'pu_code','pu_name');
                        $list[$k]['pu_code_law']=$pu_code_law['pu_name'];
                    }
                    if(!empty($v['pu_code_second'])){
                        $pu_code_second=$pu->getByField($v['pu_code_second'],'pu_code','pu_name');
                        $list[$k]['pu_code_second']=$pu_code_second['pu_name'];
                    }
                    $result[$ghkey] = $list[$k];
                }else{
                    $result[$ghkey]['quantity'] += $v['quantity'];
                    $result[$ghkey]['declared_value'] += $op[0]['op_declared_value']*$v['quantity'];
                    $result[$ghkey]['cw'] += $v['cw'];
                }
            }
            $returnRsult = array();
            foreach($result as $val){
                $returnRsult[] = $val;
            }
            return $returnRsult;
        }else{
            $list=$list[0]['c'];
        }
        return $list;
    }

    /**
     * @param $val
     * @return array
     */
    public static function validator($val)
    {
        $validateArr = $error = array();

        return  Common_Validator::formValidator($validateArr);
    }


    /**
     * @param array $params
     * @return array
     */
    public  function getFields()
    {
        $row = array(

            'E0'=>'sbg_id',
            'E1'=>'sb_code',
            'E2'=>'product_id',
            'E3'=>'goods_id',
            'E4'=>'good_quantity',
        );
        return $row;
    }
}
