<?php

class Service_BillBook 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_BillBook();
        }
        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 = "id")
    {
        $model = self::getModelInstance();
        return $model->update($row, $value, $field);
    }

    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public static function delete($value, $field = "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 = 'id', $colums = "*")
    {
        $model = self::getModelInstance();
        return $model->getByField($value, $field, $colums);
    }
    public static function getByIdForUpdate($value, $field = 'id', $colums = "*")
    {
        $model = self::getModelInstance();
        return $model->getByFieldForUpdate($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 getJoinLeftDetailBycondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $order = "")
    {
        $model = self::getModelInstance();
        return $model->getJoinLeftDetailBycondition($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/2/25
     * Time: 16:15
     * @param $productRow product 表信息
     * @param $row bill_book 表信息
     * @return array
     */
    public static function validatorBookAndProduct($productRow,$row){
        $error = array();
        if(substr($productRow['hs_code'],0,8) !=  substr($row['hs_code'],0,8)){
            $error[]='账册海关编码与系统海关编码前八位不一致';

        }
        if (!empty($productRow['country_code_of_origin'])) {
            $countryInfo = Service_Country::getByField($productRow['country_code_of_origin'], 'country_code');
            if($countryInfo['trade_country'] != $row['country_code']){
                $error[]='账册原产国与系统产品原产国不一致';

            }
        }
        $productUom = Service_HsUomMap::getByField($productRow['product_id'],'product_id');
        if(!empty($productUom)){
            $hsUom = Service_HsUom::getByField($productUom['hu_id']);
            if($hsUom['pu_code_law'] != $row['legal_unit']){
                $error[]='账册计量单位与系统产品计量单位不一致';
            }
            if($hsUom['pu_code_second'] != $row['second_legal_unit']){
                $error[]='账册第二计量单位与系统产品第二计量单位不一致';

            }
        }
        return $error;
    }

    /**
     * Note:
     * User: Yao
     * Date: 2020/8/4
     * Time: 15:48
     * @param $uploadRow
     * @return array
     */
    public static function validateBillStock($uploadRow){
        $error = [];
        if(empty($uploadRow['warehouse_account'])){
            $error[]='仓库账册号为空';
        }
        if(empty($uploadRow['record_no'])){
            $error[]='账册备案号为空';
        }
        if(empty($uploadRow['incre_quantity']) && empty($uploadRow['reduce_quantity']) &&empty($uploadRow['lock_quantity'])&&empty($uploadRow['unlock_quantity'])){
            $error[]='核增、核减、锁定、解锁、数量必须填写一个';
        }else{
            $num = [
                $uploadRow['incre_quantity'],
                $uploadRow['reduce_quantity'],
                $uploadRow['lock_quantity'],
                $uploadRow['unlock_quantity']
            ];
            if(count(array_filter($num))>1){
                $error[]='核增、核减、锁定、解锁、数量只能填写一个';
            }
        }

        if(!empty($uploadRow['incre_quantity'])){
            if(!preg_match('/^[1-9]\d*$/',$uploadRow['incre_quantity']) ){
                $error[]="核增库存必须是整数";
            }
        }
        if(!empty($uploadRow['reduce_quantity'])){
            if(!preg_match('/^[1-9]\d*$/',$uploadRow['reduce_quantity']) ){
                $error[]="核减库存必须是整数";
            }
        }
        if(!empty($uploadRow['lock_quantity'])){
            if(!preg_match('/^[1-9]\d*$/',$uploadRow['lock_quantity'])&& $uploadRow['lock_quantity']!='ALL' ){
                $error[]="锁定库存必须是整数";
            }
        }
        if(!empty($uploadRow['unlock_quantity'])){
            if(!preg_match('/^[1-9]\d*$/',$uploadRow['unlock_quantity']) && $uploadRow['unlock_quantity']!='ALL'){
                $error[]="解锁库存必须是整数";
            }
        }
        if(empty($uploadRow['note'])){
            $error[] ='备注内容请填写';
        }
        return $error;
    }


    /**
     * @param array $params
     * @return array
     */
    public function getFields()
    {
        $row = array(
            'E0' => 'id',
            'E1' => 'warehous_account_no',
            'E2' => 'recorde_no',
            'E3' => 'product_title',
            'E4' => 'hs_code',
            'E5' => 'specification',
            'E6' => 'country_code',
            'E7' => 'decalare_unit',
            'E8' => 'legal_unit',
            'E9' => 'second_legal_unit',
            'E10' => 'decalar_price',
            'E11' => 'decalar_currency',
            'E12' => 'avg_dollar_price',
            'E13' => 'total_dollar_price',
            'E14' => 'in_quantity',
            'E15' => 'in_legal_quantity',
            'E16' => 'in_seconde_legal_quantity',
            'E17' => 'real_increa_quantity',
            'E18' => 'real_reduce_quantity',
            'E19' => 'pre_increa_quantity',
            'E20' => 'pre_reduce_quantity',
            'E21' => 'storage_date',
            'E22' => 'bill_no',
            'E23' => 'bill_serial',
            'E24' => 'stock_quantity',
            'E25' => 'ie_port',
            'E26' => 'bill_time',
            'E27' => 'goods_code',
            'E28' => 'custom_code',
            'E29' => 'total_price',
            'E30' => 'product_sku',
            'E31' => 'product_id',
            'E32' =>'add_time',
            'E33'=>'can_use_quantity',
            'E34'=>'frozen_quantity',
            'E35'=>'lock_quantity',
            'E36'=>'state',
        );
        return $row;
    }
    /**
     * Note:锁定状态
     * User: Yao
     * Date: 2021/7/30
     * Time: 15:18
     */
    public static function getState(){
        return [
            '0'=>'正常',
            '1'=>'锁定',
        ];
    }

}