<?php

namespace App\Services\Pay;


use App\Dao\SendOrder\SendOrderDao;
use App\Models\Product\Product;

class ProductService
{
    private $sendOrderDao;

    public function __construct(
        SendOrderDao $sendOrderDao
    )
    {
        $this->sendOrderDao = $sendOrderDao;
    }


    public static function getChargeProduct($template_type = 1)
    {
        $res = Product::getChargeProduct($template_type);
        return $res;
    }
    
    public static function addProduct($params = [])
    {
        $res = Product::create($params);
        return $res;
    }
    
    public static function getProductSingle($id)
    {
        $res = Product::detail($id);

        return $res;
    }
    
    public static function updateProductDefault($id, $default)
    {
        if ($default) {
            Product::where('id', '!=', 0)->update(['is_default' => 0]);
            Product::where('id', $id)->update(['is_default' => 1]);
        }
        if ($default == 0) {
            Product::where('id', $id)->update(['is_default' => $default]);
        }
    }
    
    public static function updateProductEnabled($id, $enabled)
    {
        if ($enabled !== false) {
            Product::where('id', $id)->update(['is_enabled' => $enabled]);
        }
    }
    
    public static function findProductsByIds($ids)
    {
        if (empty($ids)) {
            return [];
        }
        return Product::getProductsByIds($ids);
    }

    /**
     * 派单信息
     *
     * @param $id
     * @return array
     */
    public function getSendOrderById($id): array
    {
        return $this->sendOrderDao->getSendOrderById($id);
    }
}
