<?php

use Api\PhpUtils\Http\Request;
use App\Base\Job;
use App\Exception\custom\InterfaceException;
use App\Models\goods\mysql\Category;
use \App\Models\goods\mysql\GoodsSku;
use \App\Services\goods\GoodsService;
use \App\Services\goods\ElasticGoodService;
use Api\PhpUtils\Log\DaemonLog;
use Api\PhpServices\JwUser\JwUser;
use App\Services\shop\ShopService;

/**
 * 同步商品数据到es
 * php public/cli.php goodstoes index
 * Class GoodsToEsController
 */
class GoodstoesController extends Job
{
    public function indexAction()
    {
        $this->initEsIndex();

        $goodsSkuIds = $this->getGoodsSkuIdList();
        $this->toEs($goodsSkuIds);
    }

    private function getGoodsSkuIdList()
    {
        return GoodsSku::select("goods_sku_id", ['LIMIT' => 10]);
    }

    private function toEs($goodsSkuIds)
    {
        if (!empty($goodsSkuIds)) {
            foreach ($goodsSkuIds as $goodsSkuId) {
                echo "当前正在同步的商品sku_id: $goodsSkuId \n";
                $this->updateGoodsInfoToEs($goodsSkuId);
            }
        }
    }

    private function initEsIndex()
    {
        //删除原index
        ElasticGoodService::deleteIndex();
        //创建空index
        ElasticGoodService::createIndex();
    }

    /**
     * 写入到es
     * @param $goodsSkuId
     * @return array|callable
     * @throws InterfaceException
     */
    public function updateGoodsInfoToEs($goodsSkuId)
    {
        $goodsSkuInfoList = GoodsSku::getRecordMaster(["goods_sku_id" => $goodsSkuId]);
        if (!empty($goodsSkuInfoList)) {
            $goodsSkuInfo = $goodsSkuInfoList[0];
            $nameList = Category::select("name", ["category_id" => [$goodsSkuInfo["category_1_id"], $goodsSkuInfo["category_2_id"]]]);
            //生活号信息
            $url = config('interface', 'merchant.lifeaccount.get_life_account_by_id');
            if (empty($url)) {
                throw new InterfaceException(['cus' => 0]);
            }
            $lifeAccountRes = (new Request())->get($url, ["life_account_id" => $goodsSkuInfo["life_account_id"]]);
            //门店信息
            $subShopList = ShopService::getRelationShop($goodsSkuId);

            $latitude = empty($subShopList[0]["latitude"]) ? "" : $subShopList[0]["latitude"];
            $longitude = empty($subShopList[0]["longitude"]) ? "" : $subShopList[0]["longitude"];
            if (!empty($longitude) && !empty($latitude) && !empty($subShopList)) {
                foreach ($subShopList as $subShop) {
                    $esId = $goodsSkuId."_".$subShop["sub_shop_id"];
                    $data = [
                        "goods_sku_id" => $goodsSkuInfo["goods_sku_id"],
                        "goods_desc_pic_url" => $goodsSkuInfo["desc_pic_url"],
                        "goods_name" => $goodsSkuInfo["goods_name"],
                        "inventory_rest" => $goodsSkuInfo["inventory_rest"],
                        "total_amount_sold" => $goodsSkuInfo["total_amount_sold"],
                        "original_price" => empty($goodsSkuInfo["original_price"]) ? "" :  (string)($goodsSkuInfo["original_price"] / 100),
                        "price" => $goodsSkuInfo["price"] / 100,
                        "price_sort" => (int)$goodsSkuInfo["price"],
                        "audit_status" => $goodsSkuInfo["audit_status"],
                        "online_status" => $goodsSkuInfo["online_status"],
                        "desc" => $goodsSkuInfo["desc"],
                        "category_1_id" => $goodsSkuInfo["category_1_id"],
                        "category_2_id" => $goodsSkuInfo["category_2_id"],
                        "category_1_name" => $nameList[0],
                        "category_2_name" => $nameList[1],
                        "life_account_id" => $goodsSkuInfo["life_account_id"],
                        "life_account_name" => empty($lifeAccountRes["response"]["result"]["life_account_name"]) ? "" : $lifeAccountRes["response"]["result"]["life_account_name"],
                        "life_account_icon" => empty($lifeAccountRes["response"]["result"]["life_account_icon"]) ? "" : $lifeAccountRes["response"]["result"]["life_account_icon"],
                        "shop_name" => $subShop["shop_name"],
                        "shop_address" => $subShop["address"],
                        "shop_location" => $latitude.",".$longitude,
                        "update_time" => strtotime($goodsSkuInfo["update_time"]),
                    ];
                    return ElasticGoodService::indexDoc($esId, $data);
                }

            }
        }
    }

}