<?php

namespace app\api\controller\activity;

use app\models\store\StoreDescription;
use app\models\store\StoreProductAttr;
use app\models\store\StoreProductAttrValue;
use app\models\store\StoreVisit;
use app\models\system\SystemAttachment;
use app\models\store\StoreProductRelation;
use app\models\store\StoreProductReply;
use app\models\store\StoreSeckill;
use app\Request;
use crmeb\services\GroupDataService;
use crmeb\services\QrcodeService;
use crmeb\services\SystemConfigService;
use crmeb\services\UtilService;
use Exception;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

/**
 * 秒杀商品类
 * Class StoreSeckillController
 * @package app\api\controller\activity
 */
class StoreSeckillController
{
    /**
     * 秒杀商品时间区间
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        //秒杀时间段
        $seckillTime = GroupDataService::getData('routine_seckill_time', 0, true, $request->mer_id()) ?? [];
        $seckillTimeIndex = 0;
        if (count($seckillTime)) {
            foreach ($seckillTime as $key => &$value) {
                $currentHour = date('H');
                $activityEndHour = bcadd((int)$value['time'], (int)$value['continued'], 0);
                if ($activityEndHour > 24) {
                    $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
                    $value['state'] = '即将开始';
                    $value['status'] = 2;
                    $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
                } else {
                    if ($currentHour >= (int)$value['time'] && $currentHour < $activityEndHour) {
                        $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
                        $value['state'] = '抢购中';
                        $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
                        $value['status'] = 1;
                        if (!$seckillTimeIndex) $seckillTimeIndex = $key;
                    } else if ($currentHour < (int)$value['time']) {
                        $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
                        $value['state'] = '即将开始';
                        $value['status'] = 2;
                        $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
                    } else if ($currentHour >= $activityEndHour) {
                        $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
                        $value['state'] = '已结束';
                        $value['status'] = 0;
                        $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
                    }
                }
            }
        }
        $data['lovely'] = SystemConfigService::get('seckill_header_banner', '', false, $request->mer_id());
        if (strstr($data['lovely'], 'http') === false && strlen(trim($data['lovely']))) $data['lovely'] = SystemConfigService::get('site_url', '', false, $request->mer_id()) . $data['lovely'];
        $data['lovely'] = str_replace('\\', '/', $data['lovely']);
        $data['seckillTime'] = $seckillTime;
        $data['seckillTimeIndex'] = $seckillTimeIndex;
        return app('json')->successful($data);
    }

    /**
     * 秒杀商品列表
     * @param Request $request
     * @param $time
     * @return mixed
     * @throws Exception
     */
    public function lst(Request $request, $time)
    {
        list($page, $limit) = UtilService::getMore([
            ['page', 0],
            ['limit', 0],
        ], $request, true);
        if (!$time) return app('json')->fail('参数错误');
        $seckillInfo = StoreSeckill::seckillList($time, $page, $limit, $request->mer_id());
        if (count($seckillInfo)) {
            foreach ($seckillInfo as $key => &$item) {
                if ($item['quota'] > 0) {
                    $quota = StoreProductAttrValue::where('product_id', $item['id'])->where('type', 1)->value('SUM(quota)');
                    $percent = (int)bcmul(bcdiv(bcsub($item['quota'], $quota), $item['quota'], 2), 100, 0);
                    $item['percent'] = $percent;
                    $item['stock'] = $quota;
                } else {
                    $item['percent'] = 100;
                    $item['stock'] = 0;
                }
                $item['price'] = floatval($item['price']);
                $item['ot_price'] = floatval($item['ot_price']);

            }
        }
        return app('json')->successful($seckillInfo);
    }

    /**
     * 秒杀商品详情
     * @param Request $request
     * @param $id
     * @param int $time
     * @return mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function detail(Request $request, $id, $time = 0)
    {
        $mer_id = $request->mer_id();
        $storeInfo = StoreSeckill::getValidProduct($id, '*', $request->mer_id());
        if ($storeInfo)
            $storeInfo = $storeInfo->hidden(['cost', 'add_time', 'is_del'])->toArray();
        else
            $storeInfo = [];
        if (!$id || !$storeInfo) return app('json')->fail('商品不存在或已下架!');
        $siteUrl = sys_config('site_url', '', $mer_id);
        $storeInfo['image'] = set_file_url($storeInfo['image'], $siteUrl);
        $storeInfo['image_base'] = set_file_url($storeInfo['image'], $siteUrl);
        $storeInfo['code_base'] = QrcodeService::getWechatQrcodePath($id . '_seckill_detail_wap.jpg', '/pages/activity/goods_seckill_details/index?id=' . $id . '&time=' . $time . '&status=1');
        $uid = $request->uid();
        $storeInfo['userLike'] = StoreProductRelation::isProductRelation($id, $uid, 'like', 'product_seckill');
        $storeInfo['like_num'] = StoreProductRelation::productRelationNum($id, 'like', 'product_seckill');
        $storeInfo['userCollect'] = StoreProductRelation::isProductRelation($storeInfo['product_id'], $uid, 'collect');
        $storeInfo['uid'] = $uid;
        $storeInfo['description'] = StoreDescription::getDescription($id, 1);
        $data['storeInfo'] = $storeInfo;
        StoreVisit::setView($uid, $id, $storeInfo['product_id'], 'viwe');
        $data['reply'] = StoreProductReply::getRecProductReply($storeInfo['product_id']);
        $data['replyCount'] = StoreProductReply::productValidWhere()->where('product_id', $storeInfo['product_id'])->count();
        if ($data['replyCount']) {
            $goodReply = StoreProductReply::productValidWhere()->where('product_id', $storeInfo['product_id'])->where('product_score', 5)->count();
            $data['replyChance'] = $goodReply;
            if ($goodReply) {
                $data['replyChance'] = bcdiv($goodReply, $data['replyCount'], 2);
                $data['replyChance'] = bcmul($data['replyChance'], 100, 2);
            }
        } else $data['replyChance'] = 100;
        list($productAttr, $productValue) = StoreProductAttr::getProductAttrDetail($id, $uid, 0, 1);
        foreach ($productValue as $k => $v) {
            $productValue[$k]['product_stock'] = StoreProductAttrValue::where('product_id', $storeInfo['product_id'])->where('suk', $v['suk'])->where('type', 0)->value('stock');
        }
        $data['productAttr'] = $productAttr;
        $data['productValue'] = $productValue;
        return app('json')->successful($data);
    }

    /**
     * 获取秒杀小程序二维码
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function code(Request $request, $id, $stop_time = '')
    {
        $url = QrcodeService::getRoutineQrcodePath($id, $request->uid(), 2, compact('stop_time'), $request->mer_id());
        if ($url) {
            return app('json')->success(['code' => $url]);
        } else {
            return app('json')->success(['code' => '']);
        }
    }
}