Company.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. use think\Db;
  5. class Company extends Model
  6. {
  7. // 表名
  8. protected $name = 'company';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'integer';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. protected $deleteTime = false;
  15. // 追加属性
  16. protected $append = [
  17. 'status_text',
  18. 'is_open_text'
  19. ];
  20. protected static function init()
  21. {
  22. self::beforeInsert(function ($row) {
  23. if(isset($row['area_id'])) {
  24. $areaData = Db::name('shopro_area')->where('id',$row['area_id'])->find();
  25. $row['province_name'] = $areaData['province_name'];
  26. $row['city_name'] = $areaData['city_name'];
  27. $row['area_name'] = $areaData['name'];
  28. $row['full_address'] = $row['province_name'].$row['city_name'].$row['area_name'].$row['address'];
  29. }
  30. });
  31. self::beforeUpdate(function ($row) {
  32. if(isset($row['area_id'])) {
  33. $areaData = Db::name('shopro_area')->where('id',$row['area_id'])->find();
  34. $row['province_name'] = $areaData['province_name'];
  35. $row['city_name'] = $areaData['city_name'];
  36. $row['area_name'] = $areaData['name'];
  37. $row['full_address'] = $row['province_name'].$row['city_name'].$row['area_name'].$row['address'];
  38. }
  39. });
  40. }
  41. public function getStatusList()
  42. {
  43. return ['-1' => __('Status -1'), '0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
  44. }
  45. public function getIsOpenList()
  46. {
  47. return ['0' => __('Is_open 0'), '1' => __('Is_open 1')];
  48. }
  49. public function getStatusTextAttr($value, $data)
  50. {
  51. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  52. $list = $this->getStatusList();
  53. return isset($list[$value]) ? $list[$value] : '';
  54. }
  55. public function getIsOpenTextAttr($value, $data)
  56. {
  57. $value = $value ? $value : (isset($data['is_open']) ? $data['is_open'] : '');
  58. $list = $this->getIsOpenList();
  59. return isset($list[$value]) ? $list[$value] : '';
  60. }
  61. public function wallet()
  62. {
  63. return $this->belongsTo('app\admin\model\company\Wallet', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
  64. }
  65. }