<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/12/4
 * Time: 下午2:08
 */

namespace App\Modules\Product\Services;


use App\Modules\Product\Models\Product;

class ProductService
{

    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,$transform=true) {
        $res = Product::detail($id);
        if($res->type == 'NEW_USER' && $transform){
            $res->type = 'TICKET_RECHARGE';
        }
        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]);
        }
    }
}