Files

137 lines
3.8 KiB
PHP

<?php
namespace app\api\model\app\physical;
use think\Model;
use app\admin\model\app\physical\SkuPrice;
class Goods extends Model
{
protected $name = 'app_physical_goods';
protected $autoWriteTimestamp = 'integer';
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = false;
protected $append = [
'status_text',
'spec_type_text'
];
public function getStatusList()
{
return [1 => '上架', 0 => '下架'];
}
public function getSpecTypeList()
{
return ['single' => '单规格', 'multi' => '多规格'];
}
public function getStatusTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
$list = $this->getStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getSpecTypeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['spec_type']) ? $data['spec_type'] : '');
$list = $this->getSpecTypeList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getCarouselAttr($value, $data)
{
$value = $value ? $value : (isset($data['carousel']) ? $data['carousel'] : '');
if (empty($value)) {
return [];
}
$carousel = json_decode($value, true);
return is_array($carousel) ? $carousel : [];
}
public function getParamsAttr($value, $data)
{
$value = $value ? $value : (isset($data['params']) ? $data['params'] : '');
if (empty($value)) {
return [];
}
$params = json_decode($value, true);
return is_array($params) ? $params : [];
}
public function skus()
{
return $this->hasMany('app\admin\model\app\physical\Sku', 'goods_id', 'id');
}
public function skuPrices()
{
return $this->hasMany('app\admin\model\app\physical\SkuPrice', 'goods_id', 'id');
}
public static function getPayDetail($goodsIdSku, $count = 1, $extend = '')
{
$goodsIdSkuArr = explode('_', $goodsIdSku);
$goodsId = $goodsIdSkuArr[0] ?? 0;
$skuId = $goodsIdSkuArr[1] ?? 0;
$data = self::where([
'id' => $goodsId,
'status' => 1
])->find();
if (!$data) {
return false;
}
$data = $data->toArray();
$specType = $data['spec_type'] ?? 'single';
if ($specType == 'single') {
$skuPrice = SkuPrice::where([
'goods_id' => $goodsId,
'uniacid' => UNIACID
])->order('id', 'asc')->find();
if (!$skuPrice) {
return false;
}
$skuPrice = $skuPrice->toArray();
$data['price'] = $skuPrice['price'];
$data['price_marking'] = $skuPrice['original_price'] ?: $skuPrice['price'];
$data['stock'] = $skuPrice['stock'];
$data['sku_text'] = $skuPrice['goods_sku_text'] ?? '';
} else {
$skuPrice = SkuPrice::where([
'id' => $skuId,
'goods_id' => $goodsId,
'uniacid' => UNIACID
])->find();
if (!$skuPrice) {
return false;
}
$skuPrice = $skuPrice->toArray();
$data['price'] = $skuPrice['price'];
$data['price_marking'] = $skuPrice['original_price'] ?: $skuPrice['price'];
$data['stock'] = $skuPrice['stock'];
$data['sku_text'] = $skuPrice['goods_sku_text'] ?? '';
}
$data['name'] = $data['name'];
$data['title'] = $data['title'] ?? '';
$data['type'] = 'physical';
$data['count'] = $count;
$data['extend'] = $extend;
return $data;
}
}