luck-draw/app/model/Sku.php

86 lines
2.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace app\model;
use think\Model;
use think\model\relation\HasOne;
class Sku extends Base
{
// SKU 状态
public const STATUS_ON = '1'; // 出售中
public const STATUS_OFF = '0'; // 已下架
// SKU 积分兑换限制
public const ALLOW_ONLY_BUY = '0';
public const ALLOW_BUY_AND_SCORE = '1';
public const ALLOW_ONLY_SCORE = '2';
public static function statusTextList(): array
{
return [
(string)self::STATUS_ON => '在售',
(string)self::STATUS_OFF => '已下架',
];
}
public static function allowScoreTextList(): array
{
return [
(string)self::ALLOW_ONLY_BUY => '购买',
(string)self::ALLOW_BUY_AND_SCORE => '购买或积分兑换',
(string)self::ALLOW_ONLY_SCORE => '积分兑换',
];
}
public static function onAfterInsert(Model $item)
{
$item->sort = $item->id;
$item->save();
}
/**
* 模型关联:商品分类
*
* @return HasOne
*/
public function category(): HasOne
{
return $this->hasOne(SkuCategory::class, 'id', 'category_id');
}
/**
* 生成 SKU coding
* 年份 + 2位随机码 + 时间戳(秒[10位] + 微妙[4位]
* @length 18
*/
public static function generateCoding(): string
{
$randStr = str_pad(mt_rand(1,99), 2, '0', STR_PAD_LEFT);
$prefix = date('y').$randStr;
$mt = microtime(true);
list($time, $micro) = explode('.', $mt);
$micro = str_pad($micro, 4, '0', STR_PAD_RIGHT);
$micro = substr($micro, 0, 4);
return $prefix.$time.$micro;
}
/**
* @return HasOne
*/
public function spu(): HasOne
{
return $this->hasOne(Spu::class, 'id', 'spu_id');
}
/**
* 关联活动商品
*
* @return HasOne
*/
public function activity(): HasOne
{
return $this->hasOne(SpuActivity::class, 'id', 'spu_activity_id');
}
}