<?php


namespace App\Form\Services;

use App\Services\CommonUserBaseService;
use App\Web\Facades\ProductFacade;


class FormProductDetailService extends CommonUserBaseService
{
    protected $cache = true;

    protected $cacheBucket = 'FormProductDetail:';

    public function relationProductDetail($params){
        if(!empty($params['product_ids'])){
            foreach ($params['product_ids'] as $value){
                $data['form_product_id'] = $params['form_product_id'];
                $data['product_id'] = $value;
                $this->addData($data);
            }
            return true;
        }else{
            return false;
        }
    }
    public function addData($item){
        $add = [];
        $add['product_id'] = $item['product_id'];
        $add['form_product_id'] = $item['form_product_id'];
        $add['status'] = 0;
        if(!empty($item['id']) && $item['id'] > 0){
            $id = $item['id'];
            $this->updateBy([
                'id' => $id
            ], $add);
        } else {
            $id = $this->save($add)->id;
        }
        return $id;
    }
    /**
     * 根据FormProductId 获取产品详情
     * */
    public function getListByRelativeIds($formProductIds){
        $formProductDetailData= $this->model->whereIn('form_product_id',$formProductIds)
            ->selectRaw('id,form_product_id,product_id')->get()->toArray();
        foreach ($formProductDetailData as $value){
            $productIds[$value['form_product_id']][]=$value['product_id'];
        }
        foreach ($productIds as $key=>$ids){
            $data[$key] = ProductFacade::getProductByIds($ids);
        }
        return $data;
    }
}
