<?php

class Service_BillBookProduct extends Common_Service
{
    /**
     * @var null
     */
    public static $_modelClass = null;

    /**
     * @return Table_Bank|null
     */
    public static function getModelInstance()
    {
        if (is_null(self::$_modelClass)) {
            self::$_modelClass = new Table_BillBookProduct();
        }
        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 = "bbp_id")
    {
        $model = self::getModelInstance();
        return $model->update($row, $value, $field);
    }

    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public static function delete($value, $field = "bbp_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 = 'bbp_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);
    }
    /**
     * @todo inner join
     */
    public static function getInnerByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $order = ""){
        $model = self::getModelInstance();
        return $model->getInnerByCondition($condition, $type, $pageSize, $page, $order);
    }
    /**
     * @param $val
     * @return array
     */
    public static function validator($val)
    {
        $validateArr = $error = array();

        return Common_Validator::formValidator($validateArr);
    }

    /**
     * Note:获取账册信息
     * User: Yao
     * Date: 2019/6/25
     * Time: 16:09
     * @param $productId
     * @return array|mixed
     */
    public static  function getBillRecord($productId){
        $billBook = self::getByCondition(array(
            'product_id'=>$productId,
        ));
        if(!empty($billBook)){
            return $billBook;
        }
        $swapProductCondition = array(
            'product_id'=>$productId,
            'sellable_quantity_gt'=>0
        );
        $swapProduct = Service_SwapProduct::getByCondition($swapProductCondition);
        if(!empty($swapProduct)){
            foreach ($swapProduct as $sproduct){
                $billBook = self::getByCondition(array(
                    'product_id'=>$sproduct['from_product_id'],
                ));
                if(!empty($billBook)){
                    return $billBook;
                }
            }
        }
        return array();
    }

    /**
     * Note:根据产品id获取账册号
     * User: Yao
     * Date: 2019/6/29
     * Time: 10:07
     * @param $gt 账册最少可以用数量
     * @param $productId 产品id
     * @return string
     */
   public  static  function getBill($gt,$productId){
        $billBook = Service_BillBookProduct::getByCondition(array(
            'product_id'=>$productId,
        ));
        if(!empty($billBook)){
            $bilId = array();
            foreach($billBook as $bill){
                $bilId[] = $bill['bill_book_id'];
            }
            $bilId = array_unique($bilId);
            $billBook = Service_BillBook::getByCondition(array('id_array' => $bilId,'warehous_account_no'=>'L5220B19A001','stock_quantity_gt'=>$gt), '*', '1', '1', array('stock_quantity asc '));
            return $billBook[0]['recorde_no'];
        }
        return '';
    }



    /**
     * @param array $params
     * @return array
     */
    public function getFields()
    {
        $row = array(
            'E0' => 'bbp_id',
            'E1' => 'bill_book_id',
            'E2' => 'product_sku',
            'E3' => 'product_id',
            'E4' => 'customer_code',
            'E5' => 'add_time',
        );
        return $row;
    }

}