Index.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace app\common\model\supplier;
  3. use think\Model;
  4. use traits\model\SoftDelete;
  5. use app\common\enum\StatusEnum;
  6. class Index extends Model
  7. {
  8. use SoftDelete;
  9. // 表名
  10. protected $name = 'shop_goods_supplier';
  11. // 自动写入时间戳字段
  12. protected $autoWriteTimestamp = 'integer';
  13. // 定义时间戳字段名
  14. protected $createTime = 'createtime';
  15. protected $updateTime = 'updatetime';
  16. protected $deleteTime = 'deletetime';
  17. // 追加属性
  18. protected $append = [
  19. 'status_text'
  20. ];
  21. /**
  22. * 获取状态列表
  23. */
  24. public function getStatusList()
  25. {
  26. return StatusEnum::getMap();
  27. }
  28. /**
  29. * 获取状态文本
  30. */
  31. public function getStatusTextAttr($value, $data)
  32. {
  33. $status = $value ?: ($data['status'] ?? StatusEnum::ENABLED);
  34. $list = $this->getStatusList();
  35. return $list[$status] ?? '';
  36. }
  37. /**
  38. * 根据名称获取供应商
  39. */
  40. public static function getByName($name)
  41. {
  42. return self::where('name', $name)->find();
  43. }
  44. /**
  45. * 获取供应商选项列表
  46. */
  47. public static function getOptions()
  48. {
  49. return self::where('status', StatusEnum::ENABLED)
  50. ->order('weigh desc, id desc')
  51. ->field('id,name')
  52. ->select();
  53. }
  54. protected static function init()
  55. {
  56. self::afterInsert(function ($row) {
  57. if (!$row['weigh']) {
  58. $pk = $row->getPk();
  59. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  60. }
  61. });
  62. }
  63. public function category()
  64. {
  65. return $this->belongsTo('app\common\model\supplier\Category', 'category_id', 'id');
  66. }
  67. }