1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace app\common\model\supplier;
- use think\Model;
- use traits\model\SoftDelete;
- use app\common\enum\StatusEnum;
- class Index extends Model
- {
- use SoftDelete;
-
- // 表名
- protected $name = 'shop_goods_supplier';
-
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'integer';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = 'deletetime';
- // 追加属性
- protected $append = [
- 'status_text'
- ];
-
- /**
- * 获取状态列表
- */
- public function getStatusList()
- {
- return StatusEnum::getMap();
- }
-
- /**
- * 获取状态文本
- */
- public function getStatusTextAttr($value, $data)
- {
- $status = $value ?: ($data['status'] ?? StatusEnum::ENABLED);
- $list = $this->getStatusList();
- return $list[$status] ?? '';
- }
-
- /**
- * 根据名称获取供应商
- */
- public static function getByName($name)
- {
- return self::where('name', $name)->find();
- }
-
- /**
- * 获取供应商选项列表
- */
- public static function getOptions()
- {
- return self::where('status', StatusEnum::ENABLED)
- ->order('weigh desc, id desc')
- ->field('id,name')
- ->select();
- }
- protected static function init()
- {
- self::afterInsert(function ($row) {
- if (!$row['weigh']) {
- $pk = $row->getPk();
- $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
- }
- });
- }
- public function category()
- {
- return $this->belongsTo('app\common\model\supplier\Category', 'category_id', 'id');
- }
-
- }
|