<?php
//run 300
require_once ('config.php');
set_time_limit(0);
error_reporting(E_ALL);
// $pageSize=100;
// $page=1;
// $hsElementMap = Service_HsElmmentEdit::getByCondition(array(),'distinct(hs_code) as hs_code',$pageSize,$page);
// $b=fopen('b.csv','a+');
// $db= Common_Common::getAdapter();
// $i = 1;
// while($hsElementMap) {
//     foreach ($hsElementMap as $key => $value) {
//         $syshe = $db->fetchAll("select * from hs_element where hs_code ='{$value['hs_code']}' order by he_sort asc");
//         $edHe = $db->fetchAll("select * from hs_elmment_edit where hs_code ='{$value['hs_code']}' order by he_sort asc");
//         $export = array();
//         foreach ($edHe as $key => $v) {
//             $export = array(
//                 "'" . $v['hs_code'],
//                 $v['he_name'],
//                 $v['he_sort'],
//                 $syshe[$key]['he_name'],
//                 $syshe[$key]['he_sort']
//             );
//             fwrite($b, Service_Csv::convert($export, true));
//         }
//         echo $i;
//         echo PHP_EOL;
//         $i++;
//     }
//     $page++;
//     $hsElementMap = Service_HsElmmentEdit::getByCondition(array(),'distinct(hs_code) as hs_code',$pageSize,$page);
//
// }
// fclose($b);
// die();



// $db= mysqli_connect('127.0.0.1','root','root','test');
// $row = mysqli_query($db,"select * from hs_map_two");
// $saveData = PHPExcel_IOFactory::load($file);
// $sheet1 = $saveData->getSheet(0);
// $sheet4 = $saveData->getSheet(3);
$update = array();
$productLog = array();
$index =1;

/*while($hs=mysqli_fetch_assoc($row)){
    $hsCode = Service_Hscodes::getByField($hs['hs_code'],'hs_code');
    $update = array();
    if(empty($hsCode)){
        $update['is__exit']='不存在';
    }else{
        $element = explode(";",$hs['hs_emlent']);

        $hsOther = false;
        foreach($element as $key =>$val){
            $elementValue = preg_replace("/^\d+:/",'',$val);
            if($elementValue=='其它'||$elementValue=='其他'){
                $hsOther = true;
            }
            $add = array(
                'hs_code'=>$hs['hs_code'],
                'he_code'=>$elementValue,
                'he_name'=>$elementValue,
                'he_sort'=>$key,
                'he_add_time'=>date("Y-m-d H:i:s"),
            );
            Service_HsElmmentEdit::add($add);
        }
        if(!$hsOther){
            $add = array(
                'hs_code'=>$hs['hs_code'],
                'he_code'=>'其它',
                'he_name'=>'其它',
                'he_sort'=>$key+1,
                'he_add_time'=>date("Y-m-d H:i:s"),
            );
            Service_HsElmmentEdit::add($add);
        }
    }
    $index++;
    echo $index;
    echo PHP_EOL;

}*/
$b=fopen('elementC0365.csv','a+');
$page =1;
$pageSize=300;
$index = 1;
$hsCheck = array();
$hsElementMap = Service_HsElementMap::getByCondition(array(),array('distinct(product_id) as product_id'),$pageSize,$page);
$db = Common_Common::getAdapter();
$alias = array(
    '包装规格'=>'包装',
    '型号（货号）'=>'型号',
    '用途'=>'用途（其中垫圈、垫片等类似品是否密封用）',
    '材质（海绵橡胶、非海绵橡胶）'=>'材质（海绵橡胶、硫化橡胶等）',
    // '品牌（中文及外文名称）'=>'品牌',
    '包装规格（净重），是否零售包装'=>'包装',
    '型号（货号）'=>'型号',
    '工艺流程（手工、机械、半手工）'=>'工艺流程[手工、机械、半手工]',
    '规格或型号'=>'型号',
    '用途（供婴幼儿食用/适用的年龄阶段）'=>'用途(供婴幼儿食用)',
    '织造方法(针织、钩编，是否为起绒)'=>'织造方法[针织、钩编,是否为起绒',
    '种类(套头衫、开襟衫、背心等)'=>'种类[套头衫、开襟衫、背心等',
    '品牌（中文及外文名称）'=>array('品牌','品牌[中文及外文名称]'),
    '款式（高统等）'=>'款式[高统等]',
    '款式（是否过踝）'=>'款式（高统等）',
    '织造方法（机织等）'=>'织造方法[机织等]',
    '类别（男式）'=>'类别[男式]',
    '种类（大衣、雨衣、短大衣、斗篷、短斗篷及类似品）'=>'种类（防风大衣、短大衣、斗篷等）',
    '面料成分含量'=>'成分含量',
    '种类（衬衫、POLO衫等）'=>'种类（衬衫）',
    '材质（填充物）'=>'材质（包面及填充物）',
    '种类（西服、便服套装、上衣、长裤、马裤、工装裤等）'=>array('种类（防风大衣、短大衣、斗篷等）','种类[西服、便服套装、上衣、长裤、马裤、工装裤等]'),
    '表面材质（皮革、再生皮革、塑料、纺织物等）'=>'表面材质（塑料、纺织物等）'
);
while(!empty($hsElementMap)) {
    foreach ($hsElementMap as $p) {
        $productElement = Service_HsElementMap::getByCondition(array('product_id' => $p['product_id']), '*', 1, 1);
        $product = Service_Product::getByField($p['product_id']);
        // if(!in_array($product['customer_code'],array('C0365'))){
        //     continue;
        // }
        $hsElemnt = Service_HsElement::getByField($productElement[0]['he_id']);
        $hsCode = $hsElemnt['hs_code'];
        // if(isset($hsCheck[$hsCode])){
        //    continue;
        // }
        // $hsCheck[$hsCode]=1;
        // var_dump($hsCode);
        if(!empty($hsCode)) {
            $hsEdit = $db->fetchAll("select * from hs_elmment_edit where hs_code ='{$hsCode}' order by he_sort asc");
            $sysElemenntList = $db->fetchAll("select * from hs_element_map hs left join hs_element he on hs.he_id = he.he_id  where product_id ='{$p['product_id']}' and hs_code ='{$hsCode}' order by he_sort asc");
            // var_dump($hsCode);
            // echo PHP_EOL;
            foreach ($hsEdit as $key=>$heDetail) {
                // $export = array(
                //         "'" . $heDetail['hs_code'],
                //         $heDetail['he_name'],
                //         $heDetail['he_sort'],
                //         $sysElemenntList[$key]['he_name'],
                //         $sysElemenntList[$key]['hem_detail'],
                //         $sysElemenntList[$key]['he_sort']
                //     );
                // fwrite($b, Service_Csv::convert($export, true));
                // var_dump($heDetail['he_name']);
                $heId = Service_HsElement::getByCondition(array('hs_code' => $hsCode, 'he_name' => $heDetail['he_name']));
                if(empty($heId)){
                    if(isset($alias[$heDetail['he_name']])) {
                        if(is_array($alias[$heDetail['he_name']])){
                            foreach($alias[$heDetail['he_name']] as $d){
                                $heId = Service_HsElement::getByCondition(array('hs_code' => $hsCode, 'he_name' => $d));
                                if(!empty($heId)){
                                    break;
                                }
                            }
                        }else {
                            $heId = Service_HsElement::getByCondition(array('hs_code' => $hsCode, 'he_name' => $alias[$heDetail['he_name']]));
                        }
                    }
                }
                // // var_dump($heId);
                $sysMap = array();
                if (!empty($heId)) {
                    $sysMap = Service_HsElementMap::getByCondition(array('product_id' => $p['product_id'], 'he_id' => $heId[0]['he_id']));
                }

                if (!empty($sysMap)) {
                    $add = array(
                        'product_id' => $p['product_id'],
                        'he_id' => $heDetail['he_id'],
                        'hem_detail' => $sysMap[0]['hem_detail'],
                        'hem_add_time' => $sysMap[0]['hem_add_time'],
                    );
                } else {
                    echo $heDetail['he_name'];
                    echo PHP_EOL;
                    $add = array(
                        'product_id' => $p['product_id'],
                        'he_id' => $heDetail['he_id'],
                        'hem_detail' => '',
                        'hem_add_time' => date("Y-m-d H:i:s"),
                    );
                }
                Service_HsElementMapEdit::add($add);
            }
        }
        $index++;
        echo $index;
        echo PHP_EOL;
    }
    $page++;
    // $hsElementMap = array();
     $hsElementMap = Service_HsElementMap::getByCondition(array(),array('distinct(product_id) as product_id'),$pageSize,$page);
}
die();
