<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\service;

use app\api\model\Goods as GoodsModel;
use app\api\model\Setting as SettingModel;
use app\api\model\GoodsSku as GoodsSkuModel;
use app\api\model\Category as CategoryModel;
use app\api\service\User as UserService;
use app\common\model\store\Shop as ShopModel;
use app\api\service\sharp\Active as ActiveService;
use app\common\enum\Setting as SettingEnum;
use app\common\service\Goods as GoodsService;
use app\api\model\sharp\Active as SharpActiveModel;
use app\api\model\sharp\ActiveTime as ActiveTimeModel;
use app\api\controller\sharp\Goods as miaosha;
use app\store\model\goods\GoodsPrice as GoodsPriceModel;

/**
 * 商品服务类
 * Class Goods
 * @package app\api\service
 */
class Goods extends GoodsService
{
    /**
     * 获取商品的指定的某个SKU信息
     * @param int $goodsId
     * @param string $goodsSkuId
     * @return GoodsSkuModel|array|null
     */
    public static function getSkuInfo(int $goodsId, string $goodsSkuId)
    {
        $detail = GoodsSkuModel::detail($goodsId, $goodsSkuId);
        if (!empty($detail['image'])) {
            $detail['goods_image'] = $detail['image']['preview_url'];
        }
        return $detail;
    }

    public function getCommonConfig()
    {
        //todo wmc1新品首发背景图 2限时秒杀背景图 3排行榜背景图 4大牌正品是轮播

        switch (request()->get('type')) {
            case 1:
                $data['imgurl'] = 'https://www.saas.njrenzhou.com/uploads/10001/20240131/bdc5f17f3ff8c53ce0853177bc4f1917.png';
                return $data;
                break;
            case 2:
                $data['imgurl'] = 'https://www.saas.njrenzhou.com/uploads/10001/20240131/e8e10653875e779bc72dbee65b8072c0.png';
                return $data;
                break;
            case 3:
                $data['imgurl'] = 'https://www.saas.njrenzhou.com/uploads/10001/20240131/5c4ec6ffeb23223936295e290dc3d1f9.png';
                return $data;
                break;
            case 4:
                $data['imgurl'] = [
                    'https://www.saas.njrenzhou.com/uploads/10001/20240131/5c4ec6ffeb23223936295e290dc3d1f9.png',
                ];
                return $data;
                break;
        }
        return [];
    }

    /**
     * 推荐的商品列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function recommended(): array
    {
        // 获取商品推荐数据
        $setting = $this->getRecommendedSetting();

        // 获取商品数据
        $model = new GoodsModel;
        if ($setting['params']['source'] === 'choice') {
            // 数据来源:手动
            if (empty($setting['params']['goodsIds'])) return [];
            $goodsList = $model->getListByIdsFromApi($setting['params']['goodsIds']);
        } else {
            // 数据来源:自动
            $goodsList = $model->getList([
                'status' => 10,
                'categoryId' => $setting['params']['auto']['category'],
                'sortType' => $setting['params']['auto']['goodsSort'],
            ], $setting['params']['auto']['showNum']);
        }
        if (empty($goodsList) && $goodsList->isEmpty()) {
            return [];
        }
        return $this->formatGoodsList($goodsList);
    }

    //大牌正品列表
    public function brandList($storeid)
    {
        $model = new GoodsModel;

        $goodsList = $model->getList([
            'status' => 10,
            'is_delete' => 0,
            'is_brand' => $_GET['is_brand'],
            'is_new' => $_GET['is_new'],
            'store_id' => $storeid,
            'categoryId' => (int)$_GET['categoryId'],
        ], 15);

        if (empty($goodsList) && $goodsList->isEmpty()) {
            return [];
        }
        $list = $goodsList->toArray();

        $list['data'] = $this->formatGoodsList($goodsList);
        return $list;

    }


    //排行榜分类列表 一级
    public function charts($storeid): array
    {
        $model = new CategoryModel;

        $list = $model->where([
            'parent_id' => 0,
            'status' => 1,
            'is_paihang' => 1,
            'store_id' => $storeid
        ])->order('category_id desc')->select()->toArray();

        return $list;
    }


    public function chartsGoodsList($storeid)
    {
        $model = new GoodsModel;

        $goodsList = $model->getList([
            'status' => 10,
            'is_delete' => 0,
            'paihang' => 1,
            'store_id' => $storeid,
            'categoryId' => (int)$_GET['categoryId'],
        ], 15);

        if (empty($goodsList) && $goodsList->isEmpty()) {
            return [];
        }
        $list = $goodsList->toArray();

        $list['data'] = $this->formatGoodsList($goodsList);
        return $list;

    }

    public function chartsGoodsJing($storeid): array
    {
        $model = new CategoryModel;

        $list = $model->where([
            'parent_id' => 0,
            'status' => 1,
            'is_paihang' => 1,
            'store_id' => $storeid
        ])->order('category_id desc')->select()->toArray();
        $model = new GoodsModel;
        foreach ($list as &$v) {
            $goodsList = $model->getList([
                'status' => 10,
                'is_delete' => 0,
                'paihang' => 1,
                'store_id' => $storeid,
                'categoryId' => $v['category_id'],
            ], 3);
            $list2 = $goodsList->toArray()['data'];

            $v['goods_list'] = $this->formatGoodsList($list2);
        }

        return $list;
    }


    //同城信息
    public function cityInfo($storeid): array
    {
        $model = new ShopModel;
        $info = $model->where([
            'is_delete' => 0,
            'status' => 1,
            'store_id' => $storeid,
            'is_main' => 1,
        ])->field('shop_name,star,shop_hours,summary,phone,
        province_id,city_id,region_id,shop_id,address,latitude,longitude')->find()->toArray();
        //todo wmc
        $info['remark'] = ['手机选购', '电脑选购'];
        $info['star'] = 5;
        $info['fuwu_hours'] = 5;
        $info['fuwu_num'] = 15;
        $info['star'] = 5;
        $info['wechat'] = 'https://www.saas.njrenzhou.com/uploads/10001/20240127/12bb1b1747e652d258443247a5c4777c.png';
        $info['history'] = ['time' => '2022-01-01', 'contenmt' => 'yml与2000年成立'];
        $info['evaluate'] = [
            'image' => 'https://www.saas.njrenzhou.com/uploads/10001/20240116/18adc22ad9ea021ab2586878fe1035b0.png',
            'nickname' => 'wmc',
            'content' => '很好',
        ];
        $info['imageList'] = [
            'https://www.saas.njrenzhou.com/uploads/10001/20240116/18adc22ad9ea021ab2586878fe1035b0.png',
            'https://www.saas.njrenzhou.com/uploads/10001/20240116/18adc22ad9ea021ab2586878fe1035b0.png'

        ];
        return $info;
    }

//    //抢购会场列表
//    public function panicList()
//    {
//        //只拉第一个活动
//        $model = new SharpActiveModel;
//
//        $info = $model->alias('a')
//            ->where([
//                'a.is_delete' => 0,
//                'a.status' => 1,
//                'a.store_id' => $_GET['store_id'],
////                'sharpActiveTime.status' => 1,
//            ])->order(['a.active_id' => 'desc'])->find()->toArray();
//        if (empty($info)) {
//            return [];
//        }
//
//        $info['create_date'] = date('Y-m-d H:i:s', $info['active_date']);
//        $aid = $info['active_id'];
//        $time = ActiveTimeModel::where([
//            'status' => 1,
//            'active_id' => $aid,
//        ])->order('active_time asc')->select()->toArray();
//        if (empty($time)) {
//            return [];
//        }
//
//        // 获取秒杀活动会场首页数据
//        $service = new ActiveService;
//
////        $miaosha = $service->getGoodsListByActiveTimeId((int)$time[0]['active_time_id'])->toArray()['data'] ?? [];
//
//        $info['create_time'] = date('Y-m-d', $info['active_date']) . ' ' . $time[0]['active_time'] . ':00';
//        $info['lats_time'] = strtotime($info['create_time']) - time();
//        $list['info'] = $info;
//        $list['detail'] = $time;
//
//        return $list;
//    }


    /**
     * 格式化商品列表
     * @param $goodsList
     * @return array
     */
    private function formatGoodsList($goodsList): array
    {
        $data = [];
        foreach ($goodsList as $goods) {
            $temp = [
                'goods_id' => $goods['goods_id'],
                'goods_name' => $goods['goods_name'],
                'selling_point' => $goods['selling_point'],
                'goods_image' => $goods['goods_image'],
                'goods_price_min' => $goods['goods_price_min'],
                'goods_price_max' => $goods['goods_price_max'],
                'line_price_min' => $goods['line_price_min'],
                'line_price_max' => $goods['line_price_max'],
                'goods_sales' => $goods['goods_sales'],
                'remaizhishu' => $goods['remaizhishu'],
            ];
            if (UserService::isLogin()) {
                $catService = new \app\store\model\GoodsCategoryRel();
                $catIds = $catService->where(['goods_id' => $goods['goods_id']])->column('category_id');
                $price_list_plus = $price_list_dealer = [];
                foreach ($catIds as $k => $v) {
                    $price_list_plus[] = GoodsPriceModel::getDiscountPrice($v, 1, $goods['goods_price_min']);
                    $price_list_dealer[] = GoodsPriceModel::getDiscountPrice($v, 2, $goods['goods_price_min']);
                }
                //价格判断
                if (UserService::isstore()) {
                    $temp['goods_price_min_plus'] = min($price_list_plus);
                    $temp['goods_price_min_dealer'] = min($price_list_dealer);
                } elseif (UserService::isPlusMember()) {
                    $temp['goods_price_min_plus'] = min($price_list_plus);
                } elseif (UserService::isDealerMember()) {
                    $temp['goods_price_min_dealer'] = min($price_list_dealer);
                }
            }
            $data[] = $temp;
        }
        return $data;
    }


    /**
     * 获取商品推荐设置
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function getRecommendedSetting(): array
    {
        return SettingModel::getItem(SettingEnum::RECOMMENDED);
    }
}