<?php
/**
 * 更新海关编码
 */
require_once('config.php');
$flagFile = APPLICATION_PATH . '/../data/log/fixhs.lock';
if (@file_exists($flagFile)) {
    echo 'The running.'."\r\n";
    echo $flagFile."\r\n";
    //exit();
}
$link = fopen($flagFile, 'wb');
fclose($link);
define('RUNTIME', '[' . date('Y-m-d H:i:s') . '] ');
echo RUNTIME . "Startingsss!\n";

function updetail($hscode,$hsArr){
    //编码新的信息
    foreach ($hsArr as $kh=>$vhs){
        $condition = array(
            'hs_code'=>$hscode,
            //'he_name'=>$vhs['name'],
            'he_sort'=>$vhs['index']
        );
        $hsRows = Service_HsElement::getByCondition($condition,"*");
        if(count($hsRows)==1){
            //存在了，更新吧
            $upRow = array(
                'he_code'=>$vhs['name'],
                'he_name'=>$vhs['name'],
                'he_update_time'=>date("Y-m-d H:i:s",time())
            );
            $hsRow = $hsRows[0];
            if($hsRow['he_name']!=$vhs['name']){
                $upRow['he_description'] = "2021编码要素更新:{$hsRow['he_name']}-->{$vhs['name']}";
            }
            $he_id = $hsRows[0]['he_id'];
            Service_HsElement::update($upRow,$he_id,'he_id');
        }elseif (count($hsRows)==0){
            //不存在，加入吧
            $addRow = array(
                'hs_code'=>$hscode,
                'he_code'=>$vhs['name'],
                'he_name'=>$vhs['name'],
                'he_sort'=>$vhs['index'],
                'he_add_time'=>date("Y-m-d H:i:s",time()),
                'he_description'=>"2021新增要素:{$vhs['name']}"
            );
            Service_HsElement::add($addRow);

        }else{
            //存在2个？异常了
            throw new Exception($hscode."申报序号:{$vhs['index']}->{$vhs['name']},存在多个");
            return false;
        }
    }
    return true;
}

//添加海关编码和申报要素
function addhs($row,$hsArr){
    $hscode = $row['hs10']; //10位海关编码
    $pinming = $row['pinming'];
    $hsRow = Service_Hscodes::getByField($hscode,'hs_code');
    $hs_category_code = substr($hscode,0,6);
    $zssl = $row['zzsl'];
    $jktj = $row['jktj'];
    $jylb = $row['jylb'];
    $is_license = 0;
    if(strstr($zssl,'AB')){
        $is_license = 1;
    }
    if(empty($hsRow)){
        //添加hs编码数据
        $hsAddRow = array(
            'p_name'=>$pinming,
            'hs_category_code'=>$hs_category_code,
            'hs_code'=>$hscode,
            'hs_code_description'=>'',
            'hs_code_status'=>1,
            'has_tax'=>1,
            'consumption_tax'=>0,
            'added_value_tax'=>$zssl,
            'is_positive'=>1,
            'is_license'=>$is_license,
            //'exempt_value',
            //'ref_level',
            'customs_supervision_type'=>$jktj,
            'ciq_type'=>$jylb
        );
        //print_r($hsAddRow);
        Service_Hscodes::add($hsAddRow);
    }

    //添加要素

    updetail($hscode,$hsArr);

    //处理hs_uom
    $hu = Service_HsUom::getByField($hscode,'hs_code');
    if(empty($hu)){
        $pu_code_law = $row['uom1'];
        $pu_code_second = '';
        if($row['uom2']!=''){
            $pu_code_second = $row['uom2'];
        }
        $huRow = array(
            'hs_code'=>$hscode,
            'pu_code_law'=>$pu_code_law,
            'pu_code_second'=>$pu_code_second,
            'hu_add_time'=>date("Y-m-d H:i:s",time()),
        );
        Service_HsUom::add($huRow);
    }
}

function uphs($row){
    try {
        $db = Common_Common::getAdapter();
        $db->beginTransaction();
        //10位海关编码
        $hscode = $row['hs10']; //10位海关编码
        //规格型号
        $guige = $row['guige'];
        $id = $row['id'];

        //按|切割
        $guigeArr = explode("|",$guige);
        $error = [];
        if(!empty($guigeArr)){
            $hsArr = [];
            foreach ($guigeArr as $k=>$ge){
                $geArr = explode(":",$ge);
                $index = $geArr[0];
                $hsname = $geArr[1];
                if(count($geArr)!=2){
                    $error[] = "申报要素{$ge}不对";
                }
                $hsArr[] = array(
                    'index'=>$index,
                    'name'=>$hsname
                );
            }
        }else{
            $error[] = "数据无效,不含|";
        }

        //查看是否商品有备案了
        $products = Service_Product::getByField($hscode,"hs_code");
        if(empty($products)){
            //没有商品编码备案直接更新了
            $hsCondition = array(
                'hs_code'=>$hscode
            );
            $hsElement = Service_HsElement::getByCondition($hsCondition,"*");
            if(empty($hsElement)){
                //不存在
                //$error[] = "申报要素不存在";
                addhs($row,$hsArr);

            }else{
                updetail($hscode,$hsArr);
            }
        }else{
            $hsCondition = array(
                'hs_code'=>$hscode
            );
            $hsElement = Service_HsElement::getByCondition($hsCondition,"*");
            if(empty($hsElement)){
                //不存在
                //$error[] = "申报要素不存在";
                addhs($row,$hsArr);
            }else{
                updetail($hscode,$hsArr);
            }
        }
        if(!empty($error)){
            throw new Exception(implode("|",$error));
        }
        $sql = "update hs_2021up set dealstatus='1',message='success' where id='$id';";
        $db->query($sql);
        echo $sql;
        //$db->rollBack();
        $db->commit();
    }catch (Exception $e){
        $r = $e->getMessage();
        $db->rollBack();
        echo $r;
        $sql = "update hs_2021up set message='{$r}' where id='$id';";
        echo $sql;
        $db->query($sql);
    }
}

function execfix(){
    $sql = "select * from hs_2021up where 1 and zmqd!='' and dealstatus='-1';";
    echo $sql;
    //exit;
    $db = Common_Common::getAdapter();
    $rows = $db->fetchAll($sql);
    echo "<pre>";
    echo "rows";
    //print_r($rows);
    //exit;
    if(!empty($rows)){
        foreach ($rows as $k=>$row){
            uphs($row);
            //exit;
        }
    }
}

execfix();