Company.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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::afterInsert(function ($row) {
  32. $data = [
  33. 'user_id' => $row['id'],
  34. ];
  35. Db::name('company_wallet')->insertGetId($data);
  36. });
  37. self::beforeUpdate(function ($row) {
  38. if(isset($row['area_id'])) {
  39. $areaData = Db::name('shopro_area')->where('id',$row['area_id'])->find();
  40. $row['province_name'] = $areaData['province_name'];
  41. $row['city_name'] = $areaData['city_name'];
  42. $row['area_name'] = $areaData['name'];
  43. $row['full_address'] = $row['province_name'].$row['city_name'].$row['area_name'].$row['address'];
  44. }
  45. });
  46. }
  47. public function getStatusList()
  48. {
  49. return ['-1' => __('Status -1'), '0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
  50. }
  51. public function getIsOpenList()
  52. {
  53. return ['0' => __('Is_open 0'), '1' => __('Is_open 1')];
  54. }
  55. public function getStatusTextAttr($value, $data)
  56. {
  57. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  58. $list = $this->getStatusList();
  59. return isset($list[$value]) ? $list[$value] : '';
  60. }
  61. public function getIsOpenTextAttr($value, $data)
  62. {
  63. $value = $value ? $value : (isset($data['is_open']) ? $data['is_open'] : '');
  64. $list = $this->getIsOpenList();
  65. return isset($list[$value]) ? $list[$value] : '';
  66. }
  67. public function wallet()
  68. {
  69. return $this->belongsTo('app\admin\model\Companywallet', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
  70. }
  71. }