<?php
require_once ('config.php');
set_time_limit(0);
$flag = APPLICATION_PATH . '/../data/log/BbcInsertFmlOrderCron.lock';
if (@file_exists($flag)) {
    $ctime = filectime($flag);
    $now   = time();
}
//use Pheanstalk\Pheanstalk;
require 'vendor/autoload.php';
use PhpOffice\PhpWord\PhpWord;

/*$stalk = new Pheanstalk('132.232.48.179', '11300');
//$job = $stalk->watchOnly('qy-messages')->reserve(5);
$job = $stalk->watchOnly('declareInfo')->reserve(5);

//$stalk->useTube('declareInfo')->put('aaa');

$job = $stalk->watchOnly('declareInfo')->reserve(5);
$stalk->delete($job);
var_dump($job);
exit;*/


//$file = '/www/htdocs/inc/lib.inc.jgp';

//$filename = basename($file);

//echo $filename;

//exit;

//获取交易截图
function getTradeimg($row,$path1,$path2){
    $code = $row['orcode'];
    $name = $code.".jpg";
    /*echo $code,$name;
    echo $path1;*/

    if(file_exists($path1.$name)){
        return $path1.$name;
    }
    if(file_exists($path2.$name)){
        return $path2.$name;
    }
    return "";
}
//获取单一窗口截图
function getSingleImg($row,$path){
    $code = $row['hgbh'];
    $name = $code."清单详情.jpg";
    if(file_exists($path.$name)){
        return $path.$name;
    }
    return "";
}
//获取运单截图
function getSfimg($row,$sfpath){
    $code = $row['kddh'];
    $name = $code.".png";
    for($i=1;$i<=41;$i++){
        $path = $sfpath."历史路由-".$i."/";
        if(file_exists($path.$name)){
            return $path.$name;
        }
    }
    return "";
}
//获取神鲸截图
function getshenjinImg($row,$path1,$path2){
    $code = $row['lp'];
    $name = $code.".jpg";
    $name2 = $code.".png";
    if(file_exists($path1.$name)){
        return $path1.$name;
    }
    if(file_exists($path2.$name)){
        return $path2.$name;
    }
    if(file_exists($path1.$name2)){
        return $path1.$name2;
    }
    if(file_exists($path2.$name2)){
        return $path2.$name2;
    }
    //神鲸按订单号查
    $code =  $row['orcode'];
    $name3 = $code.".jpg";
    echo 'path';
    echo $path1.$name3;
    echo $path2.$name3;
    if(file_exists($path1.$name3)){
        var_dump('1111');
        $name = $code.".jpg";
        copy($path1.$name3,$path1.$name);
        return $path1.$name3;
    }
    if(file_exists($path2.$name3)){
        var_dump('222');
        copy($path2.$name3,$path2.$name);
        return $path2.$name3;
    }
    return "";
}


function dealorder($row){
    $path = "F:/baiducloud/百度云同步盘/工作电脑2016/F/工作文档/进口系统/2018年/沙田/处理/2021年/07月/";

    $hgjt1 = $path."/06/hgjt1/";
    $hgjt2 = $path."/06/hgjt2/";

    $qdpath = $path."/06/qd/";
    $sfpath = $path."/06/sf/";

    $sjqd1 = $path."/06/sjqdjt1/";
    $sjqd2 = $path."/06/sjqdjt2/";

    $dealpath = $path."dealresult/";

    try {
        $db = Common_Common::getAdapter();
        $db->beginTransaction();

        $tradeimg = getTradeimg($row,$hgjt1,$hgjt2); //订单截图

        var_dump($tradeimg);

        $singleimg = getSingleImg($row,$qdpath);

        $sfimg = getSfimg($row,$sfpath);

        $sjimg = getshenjinImg($row,$sjqd1,$sjqd2);

        $errStr = "";

        if(empty($tradeimg)){
            $errStr.="|订单截图找不到";
        }
        if(empty($singleimg)){
            $errStr.="|单一截图找不到";
        }
        if(empty($sfimg)){
            $errStr.="|顺丰截图找不到";
        }
        if(empty($sjimg)){
            $errStr.="|神鲸截图找不到";
        }
        if (empty($errStr)){
            $upSql = "update tmporder set result='1',message='处理成功' where id={$row['id']};";
            $dealpath = $dealpath.$row['lp']."/";
            if(!file_exists($dealpath)){
                mkdir($dealpath);
                echo $tradeimg;
                if($tradeimg){
                    copy($tradeimg,$dealpath.basename($tradeimg));
                }
                if($singleimg){
                    copy($singleimg,$dealpath.basename($singleimg));
                }
                if($sfimg){
                    copy($sfimg,$dealpath.basename($sfimg));
                }
                if($sjimg){
                    copy($sjimg,$dealpath.basename($sjimg));
                }
                //copy($sfimg,$dealpath.basename($sfimg));
            }else{
                if(!file_exists($tradeimg)){
                    copy($tradeimg,$dealpath.basename($tradeimg));
                }
                if(!file_exists($singleimg)){
                    copy($singleimg,$dealpath.basename($singleimg));
                }
                if(!file_exists($sfimg)){
                    copy($sfimg,$dealpath.basename($sfimg));
                }
                if(!file_exists($sjimg)){
                    copy($sjimg,$dealpath.basename($sjimg));
                }
            }
            dealword($row,$tradeimg,$singleimg,$sfimg,$sjimg);
            //copy($tradeimg,$)
        }else{
            $upSql = "update tmporder set message='".$errStr."' where id={$row['id']};";
        }



        $db->query($upSql);

        //$db->rollBack();
        $db->commit();


    }catch (Exception $e){
        $r = $e->getMessage();
        $db->rollBack();
    }
}

function dealword($row,$tradeimg,$singleimg,$sfimg,$sjimg){
    //$phpWord = new PhpWord();

    //$wordpath = "";
    $path = "F:/baiducloud/百度云同步盘/工作电脑2016/F/工作文档/进口系统/2018年/沙田/处理/2021年/07月/";
    $file = "F:/baiducloud/百度云同步盘/工作电脑2016/F/工作文档/进口系统/2018年/沙田/处理/2021年/07月/" . "/二线包裹出区流程介绍(2).docx";
    $word = \PhpOffice\PhpWord\IOFactory::load($file);



    $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($file);

    //$orderimg = $file.""

    //订单img
    if($tradeimg){
        $templateProcessor->setImageValue('orderimg',['path' => $tradeimg, 'width' =>800, 'height' => 1500]);
    }
    if($singleimg){
        $templateProcessor->setImageValue('singleimg',['path' => $singleimg,'width' =>900, 'height' => 2500]);
    }
    if($sjimg){
        $templateProcessor->setImageValue('lpimg',['path' => $sjimg,'width' =>900, 'height' => 2500]);
    }
    if($sfimg){
        $templateProcessor->setImageValue('sfimg',['path' => $sfimg,'width' =>900, 'height' => 2500]);
    }



    $wordpath = $path."dealresult/".$row['lp']."/".$row['lp'].".doc";
    //$section = $phpWord->addSection();
    $templateProcessor->saveAs($wordpath);


    //$phpWord->save("test2.doc");

    /*$patch= APPLICATION_PATH.'/../public/images/button_highlight2.png';
    $data = file_get_contents($patch);
    $section->addImage($patch);
    $section->addPageBreak();
    $section->addImage($patch);*/
    //$phpWord->save("test2.doc");

}


function exeorder(){
    $sql = "select * from tmporder where 1 and need='1' and result='-1' ;";
    $db = Common_Common::getAdapter();
    $rows = $db->fetchAll($sql);
    echo "<pre>";
    print_r($rows);
    //exit;
    if(!empty($rows)){
        foreach ($rows as $k=>$row){
            dealorder($row);
            //exit;
        }
    }





}

exeorder();



