Address.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace app\admin\model\shop;
  3. use think\Model;
  4. use traits\model\SoftDelete;
  5. use app\common\Enum\StatusEnum;
  6. class Address extends Model
  7. {
  8. use SoftDelete;
  9. // 表名
  10. protected $name = 'shop_user_address';
  11. // 自动写入时间戳字段
  12. protected $autoWriteTimestamp = 'int';
  13. // 定义时间戳字段名
  14. protected $createTime = 'createtime';
  15. protected $updateTime = 'updatetime';
  16. protected $deleteTime = 'deletetime';
  17. // 追加属性
  18. protected $append = [];
  19. public static function init()
  20. {
  21. parent::init();
  22. self::beforeWrite(function ($row) {
  23. $changed = $row->getChangedData();
  24. if (isset($changed['isdefault']) && $changed['isdefault']) {
  25. $info = \app\admin\model\shop\Address::where('isdefault', 1)->where('user_id', $row['user_id'])->find();
  26. if ($info && (!isset($row['id']) || $info['id'] != $row['id'])) {
  27. $info->isdefault = 0;
  28. $info->save();
  29. }
  30. }
  31. });
  32. }
  33. public function getStatusList()
  34. {
  35. return StatusEnum::getMap();
  36. }
  37. public function getStatusTextAttr($value, $data)
  38. {
  39. $value = $value ?: ($data['status'] ?? '');
  40. $list = $this->getStatusList();
  41. return $list[$value] ?? '';
  42. }
  43. public function user()
  44. {
  45. return $this->hasOne('\\app\\common\\model\\User', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
  46. }
  47. public function province()
  48. {
  49. return $this->hasOne('Area', 'id', 'province_id', [], 'LEFT')->setEagerlyType(0);
  50. }
  51. public function city()
  52. {
  53. return $this->hasOne('Area', 'id', 'city_id', [], 'LEFT')->setEagerlyType(0);
  54. }
  55. public function area()
  56. {
  57. return $this->hasOne('Area', 'id', 'area_id', [], 'LEFT')->setEagerlyType(0);
  58. }
  59. }