<?php

namespace api\logic;


use backend\modules\goods\models\ars\Goods;
use backend\modules\shop\models\ars\Collection;
use Yii;
use Throwable;
use yii\base\Component;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
use yii\web\ServerErrorHttpException;

/**
 * @author iron
 * @email weiriron@gmail.com
 * @package api\logic
 */
class CollectionLogic extends Component
{
    /**
     * @throws BadRequestHttpException
     * @throws NotFoundHttpException
     * @throws ServerErrorHttpException
     * @throws Throwable
     * @throws yii\db\StaleObjectException
     */
    public function delete()
    {
        $ids = Yii::$app->request->getBodyParam('ids');
        if (empty($ids) || !is_array($ids)) {
            throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS);
        }
        foreach ($ids as $id) {
            $collection = $this->findCollection($id);
            if (!$collection) {
                throw new NotFoundHttpException("未找到该收藏");
            }
            if (!$collection->delete()) {
                throw new ServerErrorHttpException('服务器取消收藏失败');
            }
        }
        Yii::$app->getResponse()->setStatusCode(204);
    }

    /**
     * @return bool
     * @throws ServerErrorHttpException
     * @throws Throwable
     * @throws yii\db\StaleObjectException
     */
    public function update()
    {
        $collection = $this->findCollectionByGoods();
        if (!$collection) {
            return $this->create();
        }
        if (!$collection->delete()) {
            throw new ServerErrorHttpException('服务器取消收藏失败');
        }
        return true;
    }

    /*------------------------------------------------------------------------------------------*/
    /**
     * @throws NotFoundHttpException
     * @throws ServerErrorHttpException
     */
    private function create()
    {
        $goods = $this->findGoods();
        $collection = new Collection();
        $collection->goods_id = $goods->id;
        $collection->goods_name = $goods->name;
        $collection->goods_img = $goods->image;
        $collection->goods_price = $goods->price;
        $collection->user_id = Yii::$app->user->getId();
        if (!$collection->save()) {
            throw new ServerErrorHttpException('服务器新建收藏失败');
        }
        return true;
    }

    /**
     * @param integer $id
     * @return array|Collection|null
     */
    private function findCollection($id)
    {
        return Collection::find()
            ->where(['id' => $id])
            ->andWhere(['user_id' => Yii::$app->user->getId()])
            ->one();
    }


    private function findCollectionByGoods()
    {
        $goodsId = Yii::$app->request->getQueryParam('id');
        return Collection::find()
            ->where(['goods_id' => $goodsId])
            ->andWhere(['user_id' => Yii::$app->user->getId()])
            ->one();
    }


    /**
     * @return Goods|null
     * @throws NotFoundHttpException
     */
    private function findGoods()
    {
        $goodsId = Yii::$app->request->getQueryParam('id');
        $goods = Goods::findOne(['id' => $goodsId, 'is_delete' => Goods::IS_DELETE_NO, 'is_sale' => Goods::IS_SALE_YES]);
        if (!$goods) {
            throw new NotFoundHttpException('商品未找到');
        }
        return $goods;
    }
}