<?php

namespace App\Admin\Repositories;

use App\Models\DistInquiry as Model;
use Carbon\Carbon;
use Dcat\Admin\Repositories\EloquentRepository;

class DistInquiry extends EloquentRepository
{
    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;


    /*
     * 检测状态是否为0，即未处理
     */
    public static function assessTheStatus($ids) {
        $inquirieCount = Model::whereIn('id', $ids)->where('status', '=', 0)->count();
        if ($inquirieCount != count($ids)) {
            return false;
        }
        return true;
    }

    /*
     * 分配询价单
     */
    public static function assignData($ids, $distId) {
        $inquiries = Model::whereIn('id', $ids)->get();
        foreach ($inquiries as $inquiry) {
            $inquiry->dist_id = $distId;
            $inquiry->status = 1;
            $inquiry->allocate_time = Carbon::now();
            $inquiry->save();
        }
    }



    /*
     * 供应商把询价单设置为已处理
     */
    public static function distSetStatusProcessed($ids)
    {
        $result = true;
        foreach ($ids as $id) {
            $id = intval($id);
            $row = Model::find($id);
            if ($row && $row->dist_id == getDistributorId() && $row->status == 1) {
                $row->status = 2;
                $row->save();
            } else {
                $result = false;
            }
        }
        return $result;
    }

    /*
     *  供应商获取未处理的询价单
     */
    public static  function getProcessingNum() {
        return Model::where('status', 0)->count();
    }

}
