<?php

namespace Modules\Mall\Models\Traits;

use Exception;
use Modules\Mall\Models\Express;
use Modules\Mall\Models\Reason;
use Modules\Mall\Models\Shop;

trait ShopActions
{

    /**
     * Notes   : 关闭店铺
     * @Date   : 2021/5/7 4:58 下午
     * @Author : < Jason.C >
     * @return bool
     * @throws \Exception
     */
    public function close(): bool
    {
        if ($this->status != Shop::STATUS_NORMAL) {
            throw new Exception('当前状态不可关闭');
        }

        $this->status = Shop::STATUS_CLOSE;

        return $this->save();
    }

    /**
     * Notes   : 开启店铺
     * @Date   : 2021/5/7 5:02 下午
     * @Author : < Jason.C >
     * @return bool
     * @throws \Exception
     */
    public function open(): bool
    {
        if ($this->status != Shop::STATUS_CLOSE) {
            throw new Exception('当前状态不可开启');
        }

        $this->status = Shop::STATUS_NORMAL;

        return $this->save();
    }

    /**
     * Notes   : 通过申请
     * @Date   : 2021/5/7 5:03 下午
     * @Author : < Jason.C >
     * @return bool
     * @throws \Exception
     */
    public function pass(): bool
    {
        if ($this->status != Shop::STATUS_APPLYING) {
            throw new Exception('当前状态不可审核' . $this->status);
        }

        // 店铺审核通过后，增加物流的关联

        $expressIds = Express::where('status', 1)->pluck('id');
        $this->expresses()->attach($expressIds);
        $reasonIds = Reason::where('status', 1)->pluck('id');
        $this->reasons()->attach($reasonIds);

        $this->status = Shop::STATUS_CLOSE;

        return $this->save();
    }

    /**
     * Notes   : 驳回申请
     * @Date   : 2021/5/7 5:03 下午
     * @Author : < Jason.C >
     * @param  string  $reason
     * @return bool
     * @throws \Exception
     */
    public function reject(string $reason): bool
    {
        if ($this->status != Shop::STATUS_APPLYING) {
            throw new Exception('当前状态不可驳回');
        }

        $this->status        = Shop::STATUS_REJECT;
        $this->reject_reason = $reason;

        return $this->save();
    }

}