<?php


namespace App\Services\marketing;


use App\Exception\custom\MarketingException;
use App\Models\goods\mysql\GoodsSku;
use App\Models\goods\mysql\PindanGoodsSku;
use App\Models\marketing\mysql\Marketing;
use App\Models\marketing\mysql\MarketingGoods;
use App\Models\marketing\mysql\MarketingTakePlace;
use App\Models\marketing\mysql\TakePlace;
use App\Services\marketing\MarketingService;

class MarketingForOrderService
{
    /**
     * 活动列表
     * @param $params
     * @return \Api\PhpUtils\Mysql\MysqlBase
     */
    public static function getMarketingList($params)
    {
        $where['marketing_type'] = !empty($params['marketing_type']) ? $params['marketing_type'] : Marketing::MARKETING_TYPE_PINDAN;
        $where['life_account_id'] = MarketingService::getPublicLifeAccountId();
        return Marketing::select(["marketing_id", "marketing_name", "create_time"], $where);
    }

    /**
     * 活动的商品
     * @param $params
     * @return \Api\PhpUtils\Mysql\MysqlBase|array
     * @throws MarketingException
     */
    public static function getMarketingGoodsList($params)
    {
        if (empty($params["marketing_id"])) {
            throw new MarketingException(['cus' => 6]);
        }
        $goodsSkuIdList = MarketingGoods::select(["goods_sku_id"], ["marketing_id" => $params["marketing_id"]]);
        $goodsSkuList = [];
        if (!empty($goodsSkuIdList)) {
            $goodsSkuIdArr = array_column($goodsSkuIdList, "goods_sku_id");
            $goodsSkuList = PindanGoodsSku::select(["goods_sku_id", "goods_name", "create_time"], ["goods_sku_id" => $goodsSkuIdArr]);
        }
        return $goodsSkuList;
    }

    /**
     * 活动的自提点
     * @param $params
     * @return \Api\PhpUtils\Mysql\MysqlBase|array
     * @throws MarketingException
     */
    public static function getMarketingTakePlaceList($params)
    {
        if (!empty($params["marketing_id"])) {
            $takePlaceIdList = MarketingTakePlace::select(["take_place_id"], ["marketing_id" => $params["marketing_id"]]);
            $takePlaceList = [];
            if (!empty($takePlaceIdList)) {
                $takePlaceIdArr = array_column($takePlaceIdList, "take_place_id");
                $takePlaceList = TakePlace::select(["take_place_id", "take_place_name", "create_time", "address"], ["take_place_id" => $takePlaceIdArr]);
            }
        } else {
            $takePlaceList = TakePlace::select(["take_place_id", "take_place_name", "create_time", "address"]);
        }
        return $takePlaceList;
    }
}