<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 医疗产品属性设置
 * Class Users
 * @package app\admin\model
 */
class MedicalCareProductAttr extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'medical_care_product_attr';

    public $timestamps = false;

    /**
     * Notes:获取产品列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: QJF
     * Date: 2022/9/26
     */
    public static function getProductAttrList(int $page, int $limit,$keywords)
    {
        $list = static::select('*')
            ->orderBy('attr_create_time','DESC')
            ->forPage($page, $limit)
            ->get();

        $count = static::when($keywords != '', function ($query) use ($keywords){
            $query->where('product_name', 'like', '%' . $keywords . '%');
        })->count();
        return [$list, $count];
    }
    //关联产品规格值
    public function Values(){
        return $this->hasMany(MedicalCareProductAttrValue::class,'attr_value_product_attr_id','attr_id');
    }
    //插入规格
    public static function  insertProductAttr($sku,$result,$type){
        if (!empty($sku)) {
            foreach ($sku['specTable'] as $k => $v) {
                $data = array();
                $join_sku_type = '';
                if(!empty($v['data'] )){
                    foreach ($v['data'] as $value){
                        $join_sku_type .= $value['value_name'] . ',';
                    }
                }
                $data['attr_value_product_id'] = $result;
                $data['attr_value_sku'] = rtrim($join_sku_type, ',');//sku_type组合
                $data['attr_value_stock'] = $v['attr_value_stock'];//库存
                $data['attr_value_sales'] = $v['attr_value_sales'];//销量
                $data['attr_value_image'] = isset($v['picArr'][0]['url']) ? $v['picArr'][0]['url'] : '';//规格图片地址
                $data['attr_value_cost'] = $v['attr_value_cost'];//成本价
                $data['attr_value_ot_price'] = $v['attr_value_ot_price'];//原价
                $data['attr_value_price'] = $v['attr_value_price'];//售价
                $data['attr_value_title'] = $v['attr_value_title'];//预约描述
                $data['attr_value_index'] = $v['attr_value_index'];//预约指标
                $data['attr_value_index_msg'] = $v['attr_value_index_msg'];//预约指标意义
                //$data['attr_value_use'] = ($v['attr_value_use'] ? 1 : 0);//是否启用  1启用  0否
                $data['attr_value_use'] = 1;//是否启用  1启用  0否
                $data['attr_value_type'] = $type;//类型 0普通产品  1预约产品
                MedicalCareProductAttrValue::insert($data);
            }
        }
        $sku_type = $sku['specData'];
        if (!empty($sku_type)){
            foreach ($sku_type as $key => $val){
                $data = array();
                $data['attr_name'] = $val['text'];//类别名称
                $sku_type_value = array_column($val['values'], 'text');
                $data['attr_values'] = implode('-!-', $sku_type_value);//类别值
                $data['attr_product_id'] = $result;
                $data['attr_type'] = $type;
                MedicalCareProductAttr::insert($data);
            }
        }
    }
    //删除原来规格
    public static function insertProductAttrDel($product_id)
    {
        //删除规格详情
        $where = [];
        $where['attr_value_product_id']           = $product_id;
        MedicalCareProductAttrValue::where($where)->delete();
        //删除规格分类
        $where1 = [];
        $where1['attr_product_id']           = $product_id;
        MedicalCareProductAttr::where($where1)->delete();
    }
}
