UserBrokerage.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\user;
  12. use app\model\order\StoreOrder;
  13. use crmeb\basic\BaseModel;
  14. use crmeb\traits\ModelTrait;
  15. use think\Model;
  16. class UserBrokerage extends BaseModel
  17. {
  18. use ModelTrait;
  19. /**
  20. * 数据表主键
  21. * @var string
  22. */
  23. protected $pk = 'id';
  24. /**
  25. * 模型名称
  26. * @var string
  27. */
  28. protected $name = 'user_brokerage';
  29. /**
  30. * 关联订单表
  31. * @return UserBill|model\relation\HasOne
  32. */
  33. public function order()
  34. {
  35. return $this->hasOne(StoreOrder::class, 'id', 'link_id')->field(['id', 'total_num'])->bind(['total_num']);
  36. }
  37. /**
  38. * 关联用户
  39. * @return model\relation\HasOne
  40. */
  41. public function user()
  42. {
  43. return $this->hasOne(User::class, 'uid', 'uid');
  44. }
  45. /**
  46. * 用户uid
  47. * @param Model $query
  48. * @param $value
  49. */
  50. public function searchUidAttr($query, $value)
  51. {
  52. if ($value !== '') {
  53. if (is_array($value))
  54. $query->whereIn('uid', $value);
  55. else
  56. $query->where('uid', $value);
  57. }
  58. }
  59. /**
  60. * 关联id
  61. * @param Model $query
  62. * @param $value
  63. */
  64. public function searchLinkIdAttr($query, $value)
  65. {
  66. if (is_array($value))
  67. $query->whereIn('link_id', $value);
  68. else
  69. $query->where('link_id', $value);
  70. }
  71. /**
  72. * 支出|获得
  73. * @param Model $query
  74. * @param $value
  75. */
  76. public function searchPmAttr($query, $value)
  77. {
  78. if ($value !== '') $query->where('pm', $value);
  79. }
  80. /**
  81. * 类型
  82. * @param Model $query
  83. * @param $value
  84. */
  85. public function searchTypeAttr($query, $value)
  86. {
  87. if (is_array($value))
  88. $query->whereIn('type', $value);
  89. else
  90. $query->where('type', $value);
  91. }
  92. /**
  93. * @param Model $query
  94. * @param $value
  95. */
  96. public function searchNotTypeAttr($query, $value)
  97. {
  98. if (is_array($value))
  99. $query->whereNotIn('type', $value);
  100. else
  101. $query->where('type', '<>', $value);
  102. }
  103. /**
  104. * 状态 0:带确定 1:有效 -1:无效
  105. * @param Model $query
  106. * @param $value
  107. */
  108. public function searchStatusAttr($query, $value)
  109. {
  110. $query->where('status', $value);
  111. }
  112. /**
  113. * 是否收货 0:未收货 1:已收货
  114. * @param Model $query
  115. * @param $value
  116. */
  117. public function searchTakeAttr($query, $value)
  118. {
  119. $query->where('take', $value);
  120. }
  121. /**
  122. * 模糊搜索
  123. * @param Model $query
  124. * @param $value
  125. */
  126. public function searchLikeAttr($query, $value)
  127. {
  128. $query->where(function ($query) use ($value) {
  129. $query->where('uid|title', 'like', "%$value%")->whereOr('uid', 'in', function ($query) use ($value) {
  130. $query->name('user')->whereLike('uid|account|nickname|phone', '%' . $value . '%')->field('uid')->select();
  131. });
  132. });
  133. }
  134. /**
  135. * 时间
  136. * @param Model $query
  137. * @param $value
  138. */
  139. public function searchAddTimeAttr($query, $value)
  140. {
  141. if (is_string($value)) $query->whereTime($query, $value);
  142. if (is_array($value) && count($value) == 2) $query->whereTime('add_time', 'between', $value);
  143. }
  144. }