<?php


namespace Daemon;


use Api\PhpServices\Daemon\DaemonServiceInterface;
use App\Models\goods\mysql\GoodsSku;
use App\Services\goods\GoodsService;
use Api\PhpUtils\Log\DaemonLog;
class Goods implements DaemonServiceInterface
{
    public function run()
    {
        sleep(2);
        $orderData = [
            'online_status' => 1,
            'expiration_time[<]' => date("Y-m-d H:i:s",time()),
            'LIMIT' => 100,
        ];
        $goodsList = GoodsSku::getRecords($orderData);
        DaemonLog::info('DaemonServiceInterface_goods', json_encode($goodsList));
        foreach ($goodsList as $key => $value) {
            $where = [];
            $where['goods_sku_id'] = !empty($value['goods_sku_id']) ? $value['goods_sku_id'] : '';
            $colums = [
                'online_status' => 2,
            ];  
            GoodsSku::save($colums, $where);
            //更新es
            GoodsService::updateGoodsInfoToEs($where['goods_sku_id']);
        }

        $this->onlineGoods();
        $this->onlineOffGoods();
    }


    public function stock()
    {
        $orderData = [
            'inventory_rest' => 0,
            'LIMIT' => 100,
        ];
        $goodsList = GoodsSku::getRecords($orderData);
        DaemonLog::info('DaemonServiceInterface_goods', json_encode($goodsList));
        foreach ($goodsList as $key => $value) {
            $where = [];
            $where['goods_sku_id'] = !empty($value['goods_sku_id']) ? $value['goods_sku_id'] : '';
            $colums = [
                'online_status' => 2,
            ];  
            GoodsSku::save($colums, $where);
            //更新es
            GoodsService::updateGoodsInfoToEs($where['goods_sku_id']);
        }
    }

    /**
     *
     * 自动上线
     */
    public function onlineGoods()
    {
        sleep(2);
        $orderData = [
            'online_type' => 2,
            'online_status' => [0,2],
            'audit_status' => 1,
            'online_start_time[<]' => date("Y-m-d H:i:s",time()),
            'online_end_time[>]' => date("Y-m-d H:i:s",time()),
            'LIMIT' => 100,
        ];
        $goodsList = GoodsSku::getRecords($orderData);
        DaemonLog::info('DaemonServiceOnline_goods', json_encode($goodsList));
        foreach ($goodsList as $key => $value) {
            $where = [];
            $where['goods_sku_id'] = !empty($value['goods_sku_id']) ? $value['goods_sku_id'] : '';
            $colums = [
                'online_status' => 1,
            ];  
            GoodsSku::save($colums, $where);
            //更新es
            GoodsService::updateGoodsInfoToEs($where['goods_sku_id']);

        }
    }

    /**
     *
     * 自动下架
     */
    public function onlineOffGoods()
    {

        sleep(2);
        $orderData = [
            'online_type' => 2,
            'online_status' => 1,
            'audit_status' => 1,
            'online_end_time[<]' => date("Y-m-d H:i:s",time()),
            'LIMIT' => 100,
        ];

        $goodsList = GoodsSku::getRecords($orderData);
        DaemonLog::info('DaemonServiceOnlineOff_goods', json_encode($goodsList));
        foreach ($goodsList as $key => $value) {
            $where = [];
            $where['goods_sku_id'] = !empty($value['goods_sku_id']) ? $value['goods_sku_id'] : '';
            $colums = [
                'online_status' => 2,
            ];  
            GoodsSku::save($colums, $where);
            //更新es
            GoodsService::updateGoodsInfoToEs($where['goods_sku_id']);
        }

    }
}