WorkMember.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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\label\UserLabel;
  13. use app\model\user\label\UserLabelRelation;
  14. use crmeb\basic\BaseModel;
  15. use crmeb\traits\ModelTrait;
  16. use think\model\relation\HasManyThrough;
  17. use think\model\relation\HasOne;
  18. /**
  19. * 企业微信成员
  20. * Class WorkMember
  21. * @package app\model\work
  22. */
  23. class WorkMember extends BaseModel
  24. {
  25. use ModelTrait;
  26. /**
  27. * @var string
  28. */
  29. protected $name = 'work_member';
  30. /**
  31. * @var string
  32. */
  33. protected $key = 'id';
  34. /**
  35. * @var string
  36. */
  37. protected $autoWriteTimestamp = 'int';
  38. public function department()
  39. {
  40. return $this->hasManyThrough(WorkDepartment::class, WorkMemberRelation::class, 'member_id', 'department_id', 'id', 'department');
  41. }
  42. /**
  43. * 主部门
  44. * @return HasOne
  45. */
  46. public function mastareDepartment()
  47. {
  48. return $this->hasOne(WorkDepartment::class, 'department_id', 'main_department')->field(['department_id', 'name'])->bind(['mastare_department_name' => 'name']);
  49. }
  50. /**
  51. * @return \think\model\relation\HasMany
  52. */
  53. public function departmentRelation()
  54. {
  55. return $this->hasMany(WorkMemberRelation::class, 'member_id', 'id');
  56. }
  57. /**
  58. * @return HasOne
  59. */
  60. public function chat()
  61. {
  62. return $this->hasOne(WorkGroupChatMember::class, 'userid', 'userid')->where('type', 1);
  63. }
  64. /**
  65. * @return HasOne
  66. */
  67. public function clientFollow()
  68. {
  69. return $this->hasOne(WorkClientFollow::class, 'userid', 'userid');
  70. }
  71. /**
  72. * @return HasManyThrough
  73. */
  74. public function tags()
  75. {
  76. return $this->hasManyThrough(
  77. UserLabel::class,
  78. UserLabelRelation::class,
  79. 'phone',
  80. 'id',
  81. 'mobile',
  82. 'id'
  83. );
  84. }
  85. /**
  86. * @param $value
  87. * @return false|string
  88. */
  89. public function setDirectLeaderAttr($value)
  90. {
  91. return is_string($value) ? $value : json_encode($value);
  92. }
  93. /**
  94. * @param $value
  95. * @return mixed
  96. */
  97. public function getDirectLeaderAttr($value)
  98. {
  99. return json_decode($value, true);
  100. }
  101. /**
  102. * @param $query
  103. * @param $value
  104. */
  105. public function searchUseridAttr($query, $value)
  106. {
  107. if (is_array($value)) {
  108. $query->whereIn('userid', $value);
  109. } else {
  110. $query->where('userid', $value);
  111. }
  112. }
  113. /**
  114. * 企业id查询
  115. * @param $query
  116. * @param $value
  117. */
  118. public function searchCorpIdAttr($query, $value)
  119. {
  120. $query->where('corp_id', $value);
  121. }
  122. }