<?php

use App\Base\Base;
use App\Services\marketing\MarketingGoodsService;
use App\Exception\custom\DistributorException;
use App\Models\goods\mysql\GoodsSku;
use Helpers\Aes;
use Api\PhpServices\JwUser\JwUser;
use Api\PhpServices\Ksy\Ksyun;
use Api\PhpServices\ShopImage\GetImage;
use App\Services\goods\GoodsService;

class MarketinggoodsController extends Base
{
    const KS3_BASE_URL = 'ks3-cn-beijing.ksyun.com';
    /**
     * 获取全部生效营销活动商品列表
     *
     */
    public function marketing_goods_listAction()
    {
        $params = $this->params;

        $list = MarketingGoodsService::marketingGoodsList($params);
        $this->success($list);
    }

    public function generate_share_goodsAction()
    {

 
        $params = $this->params;
        $userId = !empty($params['user_id']) ? $params['user_id'] : '';
        $marketingId = !empty($params['marketing_id']) ? $params['marketing_id'] : '';
        $goodsSkuId = !empty($params['goods_sku_id']) ? $params['goods_sku_id'] : '';
        $appid = !empty($params['appid']) ? $params['appid'] : 'merchant-b';
        $content = $userId .'_' .$marketingId;

        if (empty($userId)) {
             throw new DistributorException(['cus'=>14]);
        }

        // if (empty($marketingId)) {
        //      throw new DistributorException(['cus'=>15]);
        // }

        if (empty($goodsSkuId)) {
             throw new DistributorException(['cus'=>16]);
        }

        $goodsShareId = Aes::encrypt($content);
 
        $jwUser = (new JwUser())->getUserByUserId(['user_id' => $userId]);
        $avatar = '';
        $nickName = '';
        if (!empty($jwUser['data']) && $jwUser['code'] == 0) {
            $avatar = !empty($jwUser['data']['avatar']) ? $jwUser['data']['avatar'] : '';
            $nickName = !empty($jwUser['data']['nickName']) ? $jwUser['data']['nickName'] : '';
        }

        $goodsSkuInfo =  GoodsSku::getRecord(['goods_sku_id' => $goodsSkuId]);

        $goodsName = !empty($goodsSkuInfo['goods_name']) ? $goodsSkuInfo['goods_name'] : '';
        $oldPrice = !empty($goodsSkuInfo['original_price']) ? $goodsSkuInfo['original_price']/100 : '0.00';
        $price = !empty($goodsSkuInfo['price']) ? $goodsSkuInfo['price']/100 : "0.00";

        $goodsImg = "";

        if (!empty($goodsSkuInfo["desc_pic_url"])) {
            //$url = explode(",", $goodsSkuInfo["desc_pic_url"]);
            //$ksyun = Ksyun::getDownUrl($image, 'merchant-b', 'bp-yidian', 60);
            //$goodsImg = Ksyun::getPicUrl($appid,"goods_temp",$url[0],200,200,'jpeg');
            //$descPicUrlList = array_values($ksyun);
            $goodsImg = GoodsService::getUrlList($goodsSkuInfo["desc_pic_url"]);
            $goodsImg = !empty($goodsImg[0]) ? $goodsImg[0] : '';
        }

        $url = config('interface','goods.wx.appgc');
        if(empty($url)){
            throw new InterfaceException(['cus'=>0]);
        }

        $goods_share_url = "$url/#/tk/i/$goodsSkuId/$goodsShareId";

        $data['goods_share_id'] = $goods_share_url;
        $data['share_img_url'] = $goodsImg;
        $data['share_data'] = [
            'goods_img' => $goodsImg,
            'price' => $price,
            'original_price' => $oldPrice,
            'goods_name' => $goodsName,
            'nick_name' => $nickName,
            'avatar' => $avatar,
        ];

        $getImage = new GetImage();
        $buyCount =1;
        $getImage->getGoods($avatar,$goodsImg,$nickName, $goodsName,$goods_share_url,$price, $oldPrice,$buyCount);

        //$result = ['result' => $data];
        //$this->success($result);
    }

    public function get_marketing_goods_listAction()
    {
        $params = $this->params;
        $list = MarketingGoodsService::getMarketingGoodsList($params);
        $this->success($list);
    }
    
 

}