WorkClient.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\model\work;
  12. use app\model\user\User;
  13. use crmeb\basic\BaseModel;
  14. use crmeb\traits\ModelTrait;
  15. use think\model\concern\SoftDelete;
  16. use think\model\relation\HasMany;
  17. use think\model\relation\HasManyThrough;
  18. use think\model\relation\HasOne;
  19. /**
  20. * 企业微信客户
  21. * Class WorkClient
  22. * @package app\model\work
  23. */
  24. class WorkClient extends BaseModel
  25. {
  26. use ModelTrait, SoftDelete;
  27. /**
  28. * @var string
  29. */
  30. protected $name = 'work_client';
  31. /**
  32. * @var string
  33. */
  34. protected $key = 'id';
  35. /**
  36. * @var string
  37. */
  38. protected $autoWriteTimestamp = 'int';
  39. /**
  40. * @return HasMany
  41. */
  42. public function follow()
  43. {
  44. return $this->hasMany(WorkClientFollow::class, 'client_id', 'id');
  45. }
  46. /**
  47. * @return HasOne
  48. */
  49. public function followOne()
  50. {
  51. return $this->hasOne(WorkClientFollow::class, 'client_id', 'id')->order('createtime', 'asc');
  52. }
  53. /**
  54. * 商城用户关联
  55. * @return HasOne
  56. */
  57. public function user()
  58. {
  59. return $this->hasOne(User::class, 'uid', 'uid');
  60. }
  61. public function chat()
  62. {
  63. return $this->hasOne(WorkGroupChatMember::class, 'userid', 'external_userid')->where('type', 2);
  64. }
  65. /**
  66. * @return HasManyThrough
  67. */
  68. public function tags()
  69. {
  70. return $this->hasManyThrough(
  71. WorkClientFollowTags::class,
  72. WorkClientFollow::class,
  73. 'follow_id',
  74. 'id',
  75. 'client_id',
  76. 'id'
  77. );
  78. }
  79. /**
  80. * @param $query
  81. * @param $value
  82. */
  83. public function searchExternalUseridAttr($query, $value)
  84. {
  85. if (is_array($value)) {
  86. $query->whereIn('external_userid', $value);
  87. } else {
  88. $query->where('external_userid', $value);
  89. }
  90. }
  91. /**
  92. * @param $query
  93. * @param $value
  94. */
  95. public function searchCorpIdAttr($query, $value)
  96. {
  97. $query->where('corp_id', $value);
  98. }
  99. /**
  100. * @param $query
  101. * @param $value
  102. */
  103. public function searchUidAttr($query, $value)
  104. {
  105. if (is_array($value)) {
  106. $query->whereIn('uid', $value);
  107. } else {
  108. $query->where('uid', $value);
  109. }
  110. }
  111. }