<?php
/**
 * Created by PhpStorm.
 * User: boyer
 * Date: 2020/10/20
 * Time: 18:44
 */
require_once '../config.php';


$file = "D:/资料/资料信息/修改赠品法一法二.xlsx";
$file = mb_convert_encoding($file,'gbk');
$title =[
    '法一数量'=>"hs_1",
    '货品编码'=>'product_id',
    'hs_code'=>'hs_code',
    '法二数量'=>'hs_2',
];
$data = Common_Upload::readXlsFromTitle($file,$title);
foreach ($data as $key=>$value){
    $db = Common_Common::getAdapter();
    try {
        $db->beginTransaction();
        $product = Service_Product::getByCondition([
            'product_sku'=>$value['product_id'],
            'customer_code'=>'C0426'
        ]);
        $product = $product[0];
        $hsUom = Service_HsUom::getByField($product['hs_code'],'hs_code');
        if(!empty($hsUom)){
            $uomMapAdd = [
                'product_id'=>$product['product_id'],
                'hu_id'=>$hsUom['hu_id'],
                'hum_quantity_law'=>$value['hs_1'],
                'hum_quantity_second'=>$value['hs_2'],
                'hum_add_time'=>date("Y-m-d H:i:s")
            ];
            $uomMap = Service_HsUomMap::getByField($product['product_id'],'product_id');
            if(empty($uomMap)){
                $log = array(
                    'product_id' => $product['product_id'],
                    'pl_type' => 0,
                    'pl_ip' => '127.0.0.1',
                    'user_id' => 60,
                    'pl_statu_pre' => $product['product_status'],
                    'pl_statu_now' => 1,
                    // 'pl_note' => "修改条码错误：{$product['product_barcode']}->{$newHsCode}",
                    'pl_note' => '添加法一数量'.$value['hs_1'].'添加法二数量'.$value['hs_2'],
                    'pl_add_time' => date("Y-m-d H:i:s"),
                );
                Service_ProductLog::add($log);
                Service_HsUomMap::add($uomMapAdd);
            }
        }
        $db->commit();
    }catch (Exception $e){
        echo $e;
        $db->rollBack();
    }
}

die();
$file = "D:/资料/资料信息/非保正品2.xlsx";
$file = mb_convert_encoding($file,'gbk');
$title =[
    '规格型号'=>"special",
    '货品编码'=>'product_id',
    'hs_code'=>'hs_code',
    '赠品类型'=>'gift_flag',
    '采购单价（RMB）'=>'price'
];
$data = Common_Upload::readXlsFromTitle($file,$title);
foreach ($data as $key=>$value){
    $db = Common_Common::getAdapter();
    try {
        $db->beginTransaction();
        $product = Service_Product::getByCondition([
            'product_sku'=>$value['product_id'],
            'customer_code'=>'C0426'
        ]);
        $product = $product[0];
        $update = [
            'special' => $value['special'],
            'gift_flag' => $value['gift_flag'],
            'product_declared_value' => $value['price'],
            'product_status' => 1,
        ];
        var_dump($update);
        if (!$product['goods_id']) {
            $update['goods_id'] = Service_CustomerProcess::generateGoodsId($product['customer_id']);
        }
        var_dump($update);
        if (Service_Product::update($update, $product['product_id'])) {

            $note = "修改赠品信息：";
            $note .= "规格型号：" . $product['special'] . '->' . $update['special'].';';
            $note .= "赠品标志：" . $product['gift_flag'] . '->' . $update['gift_flag'].';';
            $note .= "申报价值：" . $product['product_declared_value'] . '->' . $update['product_declared_value'].';';
            $note .= "状态：" . $product['product_status'] . '->' . $update['product_status'].';';
            $log = array(
                'product_id' => $product['product_id'],
                'pl_type' => 0,
                'pl_ip' => '127.0.0.1',
                'user_id' => 60,
                'pl_statu_pre' => $product['product_status'],
                'pl_statu_now' => 1,
                // 'pl_note' => "修改条码错误：{$product['product_barcode']}->{$newHsCode}",
                'pl_note' => $note,
                'pl_add_time' => date("Y-m-d H:i:s"),
            );
            Service_ProductLog::add($log);
        }
        $db->commit();
    }catch (Exception $e){
        echo $e;
        $db->rollBack();
    }
}