12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use DB;
- use App\Order;
- class SyncMjWarehouse extends Command {
- protected $signature = 'SyncMjWarehouse';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '同步库存';
- protected $limit = 20;
- public function handle()
- {
- $this->SyncMjWarehouse();
- }
- public function SyncMjWarehouse(){
- $params = array();
- $params['eshopCode'] = config('constants.ESHOP_CODE');
- $params['warehouseCode'] = config('constants.WAREHOUSE_CODE'); //仓库编码
- $params['pageIndex'] = '1';
- $params['pageSize'] = $this->limit;
- $mjRes = Order::mjWarehouseSkuGet($params);
- if($mjRes == false){
- exit('获取卖家云数据出错');
- }
- //插入第一页:
- $this->insertData( $mjRes['resultSet']['skuList'] );
- $count = $mjRes['resultSet']['totalNum'];
- $pages = ceil($count / $this->limit);
- //插入其他页面
- for($i=2; $i<=$pages; $i++){
- $params['pageIndex'] = $i;
- $mjRes = Order::mjWarehouseSkuGet($params);
- $this->insertData( $mjRes['resultSet']['skuList'] );
- }
- }
- /**
- * 获取订单信息,可用来判断订单是否存在,同步订单状态
- */
- public function insertData($data){
- foreach($data as $k=>$sku){
- $code = $sku['skuProductCode'];
- $up = array();
- //1.更新商品库存数量(有可能为负数,有可能为0)
- //2.更新商品库存总成本(有可能为空)
- //3.计算商品真实价格
- //4.计算商品外部价格
- $cost = DB::table('goods_skus')->where('code', $code)->select('referenceCost', 'is_weigh')->first(); //获取规格成本/是否称重
- if($cost){
- $quantity = $cost->is_weigh == 1 ? $sku['quantity']/2 : $sku['quantity']; //对应规格数量
- $up['quantity'] = $quantity;
- $up['totalCost'] = isset($sku['totalCost']) ? $sku['totalCost'] : 0;
- if($up['quantity'] != 0 ){
- $up['realPrice'] = $up['totalCost'] / $up['quantity'];
- $up['outPrice'] = ceil($up['realPrice'] * 1.1 * 100)/100;
- } else {
- $up['realPrice'] = 0;
- $up['outPrice'] = 0;
- }
- $up['cost'] = isset($sku['cost']) ? $sku['cost'] : 0;
- $up_re = DB::table('goods_skus')->where('code', $code)->update($up);
- }
- }
- return true;
- }
- }
|