代码改变世界

多账户拉取

2022-04-07 18:13  天心PHP  阅读(67)  评论(0编辑  收藏  举报
public function actionPushamazonlisting(){
$id = Yii::app()->request->getParam('id');
$num = 5000;
$page = Yii::app()->request->getParam('page')?Yii::app()->request->getParam('page'):0;
if($id){
$adjust = new YunyiAmazonAdjustNew();
if($page==0){
$adjust->deleteAll("account_id={$id} and platform=1");
}
$account = YbModel::model('AmazonAccount')->findByPk($id);
$model = YbModel::model('AmazonListingAll');//prifit,库龄库存,
$currency= $model->currency[$account->site];
$rate= YbModel::model('CurrencyRate')->find("currency='{$currency}'")->rate;
$data = $model->getDbConnection()->createCommand()
->select("t.account_id,seller_sku,t.asin1,t.item_name,status,fulfillment_channel,p.sku,open_date,t.update_date,price,p.new_price,p.pack_product_length as l,p.pack_product_width as w,p.pack_product_height as h,p.gross_product_weight as weight")
->from($model->tableName().' t')
// ->Join('dee.yunyi_out_sku s','t.seller_sku=s.platform_sku and t.account_id=s.account_id')
->leftJoin('yibai_product p','p.sku=t.sku')
->where("t.account_id={$id} and p.product_status!=99")
->limit($num,$page*$num)
->queryAll();
$seller_skus = array_column($data,'seller_sku');
$seller_skus_str = MHelper::simplode($seller_skus);
if ($account->site == 'sp') {
$country_code = 'ES';
}elseif ($account->site == 'uk'){
$country_code = 'GB';
} else {
$country_code = strtoupper($account->site);
}
$warehouse_name = "";
if(in_array(strtoupper($country_code),['DE','FR','ES','IT','PL','SE','NL','TR'])){
$warehouse_name = "FBA-EU仓";
}else if(in_array(strtoupper($country_code),['US','CA','MX'])){
$warehouse_name = "FBA-NA仓";
}else if(in_array(strtoupper($country_code),['JP'])){
$warehouse_name = "FBA-JP仓";
}else if(in_array(strtoupper($country_code),['UK'])){
$warehouse_name = "FBA-UK仓";
}
$fba_data = [];
if(!empty($warehouse_name)){
$fba_skus = array_column($data,'sku');
$fba_skus = array_values(array_unique($fba_skus));
$fba_skus_str = MHelper::simplode($fba_skus);
$fba_list = $model->getDbConnection()->createCommand()
->select('sku,feight')->from('yibai_product.yibai_amazon_fba_feight')
->where("sku in ({$fba_skus_str}) and warehouse_name = '{$warehouse_name}'")->queryAll();
$fba_data = array_column($fba_list,'feight','sku');
}
$age = $model->getDbConnection()->createCommand()
->select('inv_age_0_to_90_days,inv_age_91_to_180_days,inv_age_181_to_270_days,inv_age_271_to_365_days,inv_age_365_plus_days,sku')
->from('yibai_product.yibai_amazon_fba_inventory_aged_new')
->where("account_id={$id} and sku in ({$seller_skus_str})")
->queryAll();
$age = array_column($age,null,'sku');
$param = $fbm_seller_skus = $fba_seller_skus=[];
foreach ($data as $key => $val) {
if ($val['fulfillment_channel'] == 'DEF') {
$fbm_seller_skus[] = $val['sku'];
}elseif($val['fulfillment_channel'] == 'AMA'){
$fba_seller_skus[$val['seller_sku']] = $val['sku'];
$param[$val['sku']]=[
'l' => $val['l'],
'w' => $val['w'],
'h' => $val['h'],
'weight' => $val['weight'],
'cost' => $val['new_price'],
];
}
unset($data[$key]['l']);
unset($data[$key]['w']);
unset($data[$key]['h']);
unset($data[$key]['weight']);
}

$fbm_seller_skus = array_unique(array_filter($fbm_seller_skus));
if (!empty($fbm_seller_skus)) {
$logic_fees = MHelper::getLogisticFees($fbm_seller_skus, 1, 'Amazon', 1, array($country_code),'192.168.21.130/api/wish-more-sku-freight');
}
$products_cost=[];
if(!empty($fba_seller_skus)){
$products_cost = YbModel::model('AmazonListingAll')->getProductCostListNew($id, array_keys($fba_seller_skus), $fba_seller_skus,$param);
}
$cost_rates = $account->plat_rate + $account->advert_rate + $account->refund_rate + $account->exchange_rate + $account->report_rate + $account->tariff_rate + $account->vat_rate;
$handing_time = YbModel::model('AmazonListingOptAttr')->queryPairs('seller_sku,handing_time',"account_id={$id}");
$lists=[];
$insert_lists = [];

foreach($data as $val){
$list=[];
$list=$val;
$seller_sku = $val['seller_sku'];
$list['account_name']= $account->account_name;
$list['platform'] = 1;
$list['handing_time'] = $handing_time[$val['seller_sku']];
$list['inv_age_0_to_90_days'] = $age[$val['seller_sku']]['inv_age_0_to_90_days']?$age[$val['seller_sku']]['inv_age_0_to_90_days']:0;
$list['inv_age_91_to_180_days'] = $age[$val['seller_sku']]['inv_age_91_to_180_days']?$age[$val['seller_sku']]['inv_age_91_to_180_days']:0;
$list['inv_age_181_to_270_days'] = $age[$val['seller_sku']]['inv_age_181_to_270_days']?$age[$val['seller_sku']]['inv_age_181_to_270_days']:0;
$list['inv_age_271_to_365_days'] = $age[$val['seller_sku']]['inv_age_271_to_365_days']?$age[$val['seller_sku']]['inv_age_271_to_365_days']:0;
$list['inv_age_365_plus_days'] = $age[$val['seller_sku']]['inv_age_365_plus_days']?$age[$val['seller_sku']]['inv_age_365_plus_days']:0;
if ($val['fulfillment_channel'] == 'DEF') {
$logic_fee = $logic_fees['code'] == 0 ? ($logic_fees['data'][$val['sku']][$country_code]['register_freight'] > 0 ? $logic_fees['data'][$val['sku']][$country_code]['register_freight'] : $logic_fees['data'][$val['sku']][$country_code]['surface_mail_freight']) : 0;
$cost = $val['new_price'];
$cost_all = round(($cost + $logic_fee) / $rate + ($val['price'] * $account->plat_rate / 100),2);
$list['first_fee'] = round($logic_fee/$rate,2);
$list['last_fee'] = 0;
if ($logic_fee) {
$profit = 100 * round(($val['price'] - $cost_all) / $val['price'], 3);
}else{
$cost_all = 0 ;
$profit = 0;
}
} else {
if($val['fulfillment_channel'] == 'AMA'){ //fba 运费
if(empty($warehouse_name) || empty($val['sku'])){
$list['first_fee'] = "0.00";
}else{
$list['first_fee'] = (isset($fba_data[$val['sku']]) && $rate > 0 ) ? $fba_data[$val['sku']] : "0.00";
}
//$fba_tc_feight = $model->getTcFeight($country_code, $val['sku']);//头程(平台币)
//$list['first_fee'] = (!empty($fba_tc_feight) && $rate > 0) ? round($fba_tc_feight/$rate,2) : "0.00";
}else{
$list['first_fee'] = $products_cost[$seller_sku]['sku_stock_fee'];//头程(平台币)
}
$list['last_fee'] = $products_cost[$seller_sku]['fba_cost'];//尾程(平台币)
$other_cost = $products_cost[$seller_sku]['sku_cost'] + $products_cost[$seller_sku]['sku_stock_fee'] + $products_cost[$seller_sku]['fba_cost'];
$cost_all = YbModel::model('AmazonListingAll')->getProductCost($other_cost, $val['price'], $cost_rates);
if($val['new_price']==0 || $products_cost[$seller_sku]['sku_stock_fee']==0 || $products_cost[$seller_sku]['fba_cost']==0){
$profit=0;
}else{
$profit = 100 * round(($val['price'] - $cost_all) / $val['price'], 3);
}
}

$list['profit'] = $profit;
$list['cost_all'] = $cost_all;
$list['rate'] = $rate;
$list['update_time'] = time();
$lists[]=$list;
unset($list['item_name']);
$insert_lists[] = $list;
}
if($adjust->batchInsertAll($adjust->tableName(),array_keys($insert_lists[0]),$insert_lists)){
foreach($lists as $key=>$val){
$lists[$key]['fba_fbm_diff'] = $account->fba_fbm_diff;
}
$datas = array_chunk($lists,1000);
unset($lists);
$e_name = 'amazon_listing_info'; //交换机名固定
$conn_args = Yii::app()->params['rabbit_jumia'];
$connection = new AMQPConnection($conn_args);
$connection->connect() or die("Cannot connect to the broker!\n");
$channel = new AMQPChannel($connection); // 建立一个 Channel信道

$exchange = new AMQPExchange($channel); //创建交换机
$exchange->setName($e_name); //设置交换机的名字
$exchange->setType(AMQP_EX_TYPE_FANOUT); //fanout类型 Exchange 类型: direct fanout topic
$exchange->setFlags(AMQP_DURABLE); //持久化
$exchange->declareExchange();
foreach($datas as $dat){
$exchange->publish(json_encode(array_values($dat),JSON_UNESCAPED_UNICODE), "");
}
}
if(count($insert_lists)==$num){
$url = sprintf('%s/services/yunyi/amazonapi/pushamazonlisting/id/%s/page/%s', $_SERVER['HTTP_HOST'], $id,$page+1);
echo $url;echo '<br/>';
MHelper::curl_post_async($url);
}else{
$accountss = Yii::app()->memcache->get('amazon_listing_line');
$accountss = json_decode($accountss,true);
$line = $id%10;
if(!empty($accountss[$line])){
$account_id = array_shift($accountss[$line]);
Yii::app()->memcache->set('amazon_listing_line',json_encode($accountss),5*3600);
$url = sprintf('%s/services/yunyi/amazonapi/pushamazonlisting/id/%s', $_SERVER['HTTP_HOST'], $account_id);
echo $url;echo '<br/>';
MHelper::curl_post_async($url);
}else{
echo 'done';exit();
}
}
}else{
if(Yii::app()->request->getParam('type')=='delete'){
Yii::app()->memcache->delete('amazon_listing_line');
}
$accountss = Yii::app()->memcache->get('amazon_listing_line');
$lines = json_decode($accountss,true);
if(empty($lines)){
$account = YbModel::model('AmazonAccount');
$accounts = $account->getDbConnection()->createCommand()
->select('id,account_name,MOD(id,10)as line')
->from($account->tableName())
->where('status=1')
->order('id asc')
->queryAll();
$accounts_line=array_column($accounts,null,'id');
foreach($accounts_line as $val){
$lines[$val['line']][] = $val['id'];
}
//Yii::app()->memcache->set('amazon_listing_line',json_encode($lines),5*3600);
}
for($i=0;$i<10;$i++){
if(!empty($lines[$i])){
$account_id = array_shift($lines[$i]);
Yii::app()->memcache->set('amazon_listing_line',json_encode($lines),5*3600);
$url = sprintf('%s/services/yunyi/amazonapi/pushamazonlisting/id/%s', $_SERVER['HTTP_HOST'], $account_id);
echo $url;echo '<br/>';
MHelper::curl_post_async($url);
}else{
continue;
}
}


}

}

得到数据


select('id,account_name,MOD(id,10)as line')

foreach($accounts_line as $val){
$lines[$val['line']][] = $val['id'];
}
组合得到数据
Array
(
    [7] => Array
        (
            [0] => 7
            [1] => 17
        )

    [8] => Array
        (
            [0] => 8
            [1] => 18
            [2] => 28
        )

    [9] => Array
        (
            [0] => 9
            [1] => 19
            [2] => 29
        )

    [0] => Array
        (
            [0] => 10
            [1] => 20
            [2] => 30
        )

    。。。。。

)

//取十个进行消费
for($i=0;$i<10;$i++){
if(!empty($lines[$i])){
$account_id = array_shift($lines[$i]); // 删除数组中的第一个元素(red),并返回被删除元素的值:
Yii::app()->memcache->set('amazon_listing_line',json_encode($lines),5*3600);
$url = sprintf('%s/services/yunyi/amazonapi/pushamazonlisting/id/%s', $_SERVER['HTTP_HOST'], $account_id);
echo $url;echo '<br/>';
MHelper::curl_post_async($url);
}else{
continue;
}
}
//继续消费同一类型的
$accountss = Yii::app()->memcache->get('amazon_listing_line');
$accountss = json_decode($accountss,true);
$line = $id%10;
if(!empty($accountss[$line])){
$account_id = array_shift($accountss[$line]);
Yii::app()->memcache->set('amazon_listing_line',json_encode($accountss),5*3600);
$url = sprintf('%s/services/yunyi/amazonapi/pushamazonlisting/id/%s', $_SERVER['HTTP_HOST'], $account_id);
echo $url;echo '<br/>';
MHelper::curl_post_async($url);
}else{
echo 'done';exit();
}
 

 大数据按账户执行单线程执行

/*
     * 处理云羿sku和亿迈sku
     * /services/walmart/walmartlisting/updateyimaisku/account_id/1
     */
    public function actionUpdateyimaisku(){
        set_time_limit('3600');
        $account_id = Yii::app()->request->getParam('account_id');
        $type = Yii::app()->request->getParam('type');
        $model = new WalmartListing();
        $yimaimodel = new WalmartListingAllsYimaiSku();
        $dates = date('Y-m-d H:i:s');
        $keys = "Walmart_yimai_sku";
        if($type=='see'){
            $accountss = Yii::app()->memcache->get($keys);
            $lines = json_decode($accountss,true);
            VHelper::dump($lines);exit();
        }
        if($type=='del'){
            Yii::app()->memcache->delete($keys);
            die('删除换成成功');
        }
        if($account_id){
              $sql = "select id,account_id,seller_sku,yimai_update from yibai_walmart_listing where account_id={$account_id} and yimai_update is null limit 10000";
              $list = $model->getDbConnection()->createCommand($sql)->queryAll();
              if(!$list){
                  $accountss = Yii::app()->memcache->get($keys);
                  if(!$accountss){
                      die('处理完成');
                  }
                  $accountss = json_decode($accountss,true);
                  if(count(array_filter($accountss))==0){
                      die('处理完成');
                  }
                  $line = $account_id%10;
                  unset($accountss[$line][0]);
                  $accountss[$line] = array_values($accountss[$line]);
                  Yii::app()->memcache->set($keys,json_encode($accountss),12*3600);
                  if(!empty($accountss[$line])){
                      $account_id = $accountss[$line][0];
                      $url = sprintf('%s/services/walmart/walmartlisting/updateyimaisku/account_id/%s', $_SERVER['HTTP_HOST'], $account_id);
                      echo $url;echo '<br/>';
                      MHelper::curl_post_async($url);
                      die(1);
                  }else{
                      $line = $line+1;
                      if($line>9){
                          die('处理完成');
                      }
                      $account_id = $accountss[$line][0];
                      $url = sprintf('%s/services/walmart/walmartlisting/updateyimaisku/account_id/%s', $_SERVER['HTTP_HOST'], $account_id);
                      echo $url;echo '<br/>';
                      MHelper::curl_post_async($url);
                      die(1);
                  }
              }
              $seller_sku_list = array_unique(array_column($list, 'seller_sku'));
              $seller_sku_str = MHelper::simplode($seller_sku_list);
              $yimailist = $yimaimodel->queryPairs('seller_sku,sku,skutype',"salesplatform=11 and seller_sku in({$seller_sku_str})");
              foreach ($list as $val){
                   if(isset($yimailist[$val['seller_sku']])){
                       if($yimailist[$val['seller_sku']]['skutype']==0){//正常的
                           $model->updateAll(['yunyi_sku'=>$yimailist[$val['seller_sku']]['sku'],'order_sku'=>$yimailist[$val['seller_sku']]['sku'],'order_sku_type'=>$yimailist[$val['seller_sku']]['skutype'],'yimai_update'=>$dates],"id=".$val['id']);
                       }elseif($yimailist[$val['seller_sku']]['skutype']==1){
                           $seller = explode('-',$yimailist[$val['seller_sku']]['sku']);
                           if(array_pop($seller)=='YBFX'){
                               $model->updateAll(['yimai_sku'=>implode('-',$seller),'order_sku'=>$yimailist[$val['seller_sku']]['sku'],'order_sku_type'=>$yimailist[$val['seller_sku']]['skutype'],'yimai_update'=>$dates],"id=".$val['id']);
                           }else{
                               $model->updateAll(['yunyi_sku'=>implode('-',$seller),'order_sku'=>$yimailist[$val['seller_sku']]['sku'],'order_sku_type'=>$yimailist[$val['seller_sku']]['skutype'],'yimai_update'=>$dates],"id=".$val['id']);
                           }
                       }
                   }else{
                       $model->updateAll(['yimai_update'=>$dates],"id=".$val['id']);
                   }
              }
              $url = sprintf('%s/services/walmart/walmartlisting/updateyimaisku/account_id/%s', $_SERVER['HTTP_HOST'], $account_id);
              echo $url;echo '<br/>';
              MHelper::curl_post_async($url);
        }else{
            $accountss = Yii::app()->memcache->get($keys);
            $lines = json_decode($accountss,true);
            if(count(array_filter($lines))==0){
                Yii::app()->memcache->delete($keys);
            }
            if(empty($lines)){
                $account = new WalmartAccount();
                $accounts = $account->getDbConnection()->createCommand()
                    ->select('id,account_name,MOD(id,10)as line')
                    ->from($account->tableName())
                    ->order('id asc')
                    ->queryAll();
                $accounts_line=array_column($accounts,null,'id');
                foreach($accounts_line as $val){
                    $lines[$val['line']][] = $val['id'];
                }
            }
            if(count(array_filter($lines))==0){
                die('处理完成');
            }
            for($i=0;$i<10;$i++){
                if(!empty($lines[$i]) && count($lines[$i])>0){
                    $account_id = $lines[$i][0];
                    Yii::app()->memcache->set($keys,json_encode($lines),12*3600);
                    $url = sprintf('%s/services/walmart/walmartlisting/updateyimaisku/account_id/%s', $_SERVER['HTTP_HOST'], $account_id);
                    echo $url;echo '<br/>';
                    MHelper::curl_post_async($url);
                    break;//一次只抛出一个线程
                }else{
                    continue;
                }
            }
        }
    }