<?php

namespace App\Services\marketing;

use App\Models\marketing\mysql\Marketing;
use App\Models\marketing\mysql\MarketingGoods;
use App\Models\goods\mysql\GoodsSku;
use App\Models\goods\mysql\GoodsSpu;
use App\Models\shop\mysql\SubShop;
use Api\PhpUtils\Validate\Validate;
use Api\PhpServices\Idgen\Idgen;
use App\Exception\custom\MarketingException;
use Api\PhpServices\Sensitive\Sensitive;
class MarketingGoodsService
{
    public static function marketingGoodsList($params)
    {   

        $limit = !empty($params['num']) ? $params['num'] : 20;
        
        if (!empty($params['last_id'])) {
            $where['id[<]'] = $params['last_id'];
        }

        $where['LIMIT'] = $limit;
        $where['ORDER'] = ["create_time" => "DESC"];
        
    	$marketingWhere = [
			"end_time[>]" => date("Y-m-d"),
			"online_status" => 1,
    	];
    	$marketingList = Marketing::getMarketingList($marketingWhere);
    	$marketingId = array_column($marketingList, 'marketing_id');
        $where['marketing_id'] = !empty($marketingId) ? $marketingId : [];
        $data = MarketingGoods::marketingGoodsList($where);
        $goodsSkuId =  array_column($data, 'goods_sku_id');
        $goodsSkuList = GoodsSku::getRecordMaster(['goods_sku_id' => $goodsSkuId]);
        $goodsSkuList = array_column($goodsSkuList,null,'goods_sku_id');
        $lastData = end($data);
        $lastId = !empty($lastData['id']) ? $lastData['id'] : '';

        $shopId = [];
        foreach ($data as $key => $value) {
           $shopId[$key] = !empty($goodsSkuList[$value['goods_sku_id']]['shop_id']) ? $goodsSkuList[$value['goods_sku_id']]['shop_id'] : ''; 
        }

        $subShopList = SubShop::getRecordMaster(['shop_id' => $shopId]);
        $subShopList = array_column($subShopList,null,'shop_id');


        $list = [];
        $i = 0;
        foreach ($data as $key => $value) {
            $list[$i] = $value;
            $list[$i]['life_account_id'] = !empty($goodsSkuList[$value['goods_sku_id']]['life_account_id']) ? $goodsSkuList[$value['goods_sku_id']]['life_account_id'] : '';
            $list[$i]['shop_id'] = !empty($goodsSkuList[$value['goods_sku_id']]['shop_id']) ? $goodsSkuList[$value['goods_sku_id']]['shop_id'] : '';
            $list[$i]['goods_name'] = !empty($goodsSkuList[$value['goods_sku_id']]['goods_name']) ? $goodsSkuList[$value['goods_sku_id']]['goods_name'] : '';
            $list[$i]['goods_desc'] = !empty($goodsSkuList[$value['goods_sku_id']]['desc']) ? $goodsSkuList[$value['goods_sku_id']]['desc'] : '';
            $list[$i]['goods_spu_id'] = !empty($goodsSkuList[$value['goods_sku_id']]['goods_spu_id']) ? $goodsSkuList[$value['goods_sku_id']]['goods_spu_id'] : '';
            $list[$i]['original_price'] = !empty($goodsSkuList[$value['goods_sku_id']]['original_price']) ? $goodsSkuList[$value['goods_sku_id']]['original_price'] : '';
            $list[$i]['price'] = !empty($goodsSkuList[$value['goods_sku_id']]['price']) ? $goodsSkuList[$value['goods_sku_id']]['price'] : '';
            $list[$i]['total_amount_order'] = !empty($goodsSkuList[$value['goods_sku_id']]['total_amount_order']) ? $goodsSkuList[$value['goods_sku_id']]['total_amount_order'] : '';
            $list[$i]['desc_pic_url'] = !empty($goodsSkuList[$value['goods_sku_id']]['desc_pic_url']) ? $goodsSkuList[$value['goods_sku_id']]['desc_pic_url'] : '';
            $shopId = !empty($list[$i]['shop_id']) ? $list[$i]['shop_id'] : '';
            $list[$i]['shop_name'] = !empty($subShopList[$shopId]['shop_name']) ? $subShopList[$shopId]['shop_name'] : '';

            $i++;
        }

        $result = ['result' => $list ,'last_id' => $lastId];
        return $result;
    }
}



