UserMoney.php 4.0 KB

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