<?php
require_once '../config.php';
echo "<pre>";
$file = "F:/baiducloud/百度云同步盘/工作电脑2016/F/工作文档/进口系统/2018年/沙田/处理/2022年/02月/25/63底账维护申报价格.xls";
$file = mb_convert_encoding($file,'gbk');

function updateone($row){
    $result = [
        'state'=>0,
        'message'=>'',
    ];
    $db = Common_Common::getAdapter();
    try {
        $db->beginTransaction();
        $bilCondition = array(
            'warehous_account_no'=>$row['warehous_account_no'],
            'recorde_no'=>$row['recorde_no']
        );
        if($row['warehous_account_no']!='T5225W000063'){
            throw new Exception("备案序号{$row['recorde_no']},账册序号不是63");
        }
        $books = Service_BillBook::getByCondition($bilCondition,"*");
        if(empty($books)){
            throw new Exception("备案序号{$row['recorde_no']},不存在");
        }
        if(count($books)>1){
            throw new Exception("备案序号{$row['recorde_no']},不止一条记录");
        }
        if(!(is_numeric($row['recorde_no']) && $row['recorde_no']>0)){
            throw new Exception("备案序号{$row['recorde_no']},必须有效");
        }
        if(!(is_numeric($row['decalar_price']) && $row['decalar_price']>0)){
            throw new Exception("备案序号{$row['recorde_no']},价格必须大于0");
        }
        $currency_hs_code = "";
        if($row['decalar_currency']!=''){
            $currency = Service_Currency::getByField($row['decalar_currency'],'currency_code');
            if(empty($currency)||$currency['currency_hs_code']==''){
                throw new Exception("备案序号{$row['recorde_no']},币制不能为空");
            }else{
                $currency_hs_code = $currency['currency_hs_code'];
            }
        }else{
            throw new Exception("备案序号{$row['recorde_no']},币制不能为空");
        }
        if($currency_hs_code==''){
            throw new Exception("备案序号{$row['recorde_no']},币制不能为空");
        }
        //以下是更新数据
        $billBook = $books[0];
        $updateRow = array(
            'decalar_price'=>$row['decalar_price'],
            'decalar_currency'=>$currency_hs_code
        );
        /*var_dump($updateRow);
        var_dump($billBook);*/
        Service_BillBook::update($updateRow, $billBook['id']);
        $newBill = Service_BillBook::getByField($billBook['id']);
        $userId = '1';
        $log = array(
            'sys_add_time' => date('Y-m-d H:i:s'),
            'operator_ip' => '127.0.0.1',
            'operator_id' => $userId,
            'bill_book_id' => $billBook['id'],
            'goods_id' => $billBook['goods_code'],
            'json_row_before' => $billBook['json_row'],
            'json_row' => json_encode($newBill),
        );
        //var_dump($log);
        Service_BillBookLog::add($log);
        $db->commit();
        //$db->rollBack();
        $result['state'] = 1;
        $result['message'] = "success";
    }catch (Exception $e){
        echo $e;
        $db->rollBack();
        $result['state'] = 0;
        $result['message'] = $e->getMessage();
    }
    return $result;
}

function batchupdatebill(){
    $file = "F:/baiducloud/tmp/63-change.xls";
    //$file = mb_convert_encoding($file,'gbk');
    var_dump(file_exists($file));
    //exit;
    $title =[
        '账册号'=>"warehous_account_no",
        '备案序号'=>'recorde_no',
        '价格'=>'decalar_price',
        '币制'=>'decalar_currency',
    ];
    $data = Common_Upload::readXlsFromTitle($file,$title);
    //print_r($data);
    $respath = dirname($file)."/file.csv";
    //exit;
    foreach ($data as $key=>$value){
        //$db = Common_Common::getAdapter();
        try {
            $res = updateone($value);
            if($res['state']==1){
                file_put_contents($respath,$value['recorde_no'].","."success"."\r\n",FILE_APPEND);
            }else{
                file_put_contents($respath,$value['recorde_no'].","."failse".$res['message']."\r\n",FILE_APPEND);
            }
        }catch (Exception $e){
            echo $e;
        }
    }
}

batchupdatebill();

