UserBill.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 app\model\order\StoreOrder;
  13. use crmeb\basic\BaseModel;
  14. use crmeb\traits\ModelTrait;
  15. use think\model;
  16. /**
  17. * Class UserBill
  18. * @package app\model\user
  19. */
  20. class UserBill extends BaseModel
  21. {
  22. use ModelTrait;
  23. /**
  24. * 数据表主键
  25. * @var string
  26. */
  27. protected $pk = 'id';
  28. /**
  29. * 模型名称
  30. * @var string
  31. */
  32. protected $name = 'user_bill';
  33. protected $autoWriteTimestamp = 'int';
  34. protected $createTime = 'add_time';
  35. /**
  36. * 添加时间修改器
  37. * @return int
  38. */
  39. public function setAddTimeAttr()
  40. {
  41. return time();
  42. }
  43. /**
  44. * 添加时间获取器
  45. * @param $value
  46. * @return false|string
  47. */
  48. public function getAddTimeAttr($value)
  49. {
  50. if (!empty($value)) {
  51. if (is_string($value)) {
  52. return $value;
  53. } elseif (is_int($value)) {
  54. return date('Y-m-d H:i:s', (int)$value);
  55. }
  56. }
  57. return '';
  58. }
  59. /**
  60. * 关联订单表
  61. * @return UserBill|model\relation\HasOne
  62. */
  63. public function order()
  64. {
  65. return $this->hasOne(StoreOrder::class, 'id', 'link_id')->field(['id', 'total_num'])->bind(['total_num']);
  66. }
  67. /**
  68. * 关联用户
  69. * @return model\relation\HasOne
  70. */
  71. public function user()
  72. {
  73. return $this->hasOne(User::class, 'uid', 'uid');
  74. }
  75. /**
  76. * 用户uid
  77. * @param Model $query
  78. * @param $value
  79. */
  80. public function searchUidAttr($query, $value)
  81. {
  82. if ($value !== '') {
  83. if (is_array($value))
  84. $query->whereIn('uid', $value);
  85. else
  86. $query->where('uid', $value);
  87. }
  88. }
  89. /**
  90. * 关联id
  91. * @param Model $query
  92. * @param $value
  93. */
  94. public function searchLinkIdAttr($query, $value)
  95. {
  96. if (is_array($value))
  97. $query->whereIn('link_id', $value);
  98. else
  99. $query->where('link_id', $value);
  100. }
  101. /**
  102. * 支出|获得
  103. * @param Model $query
  104. * @param $value
  105. */
  106. public function searchPmAttr($query, $value)
  107. {
  108. if ($value !== '') $query->where('pm', $value);
  109. }
  110. /**
  111. * 种类 now_money:余额 integral:积分 exp:经验
  112. * @param Model $query
  113. * @param $value
  114. */
  115. public function searchCategoryAttr($query, $value)
  116. {
  117. if (is_array($value))
  118. $query->whereIn('category', $value);
  119. else
  120. $query->where('category', $value);
  121. }
  122. /**
  123. * @param Model $query
  124. * @param $value
  125. */
  126. public function searchNotCategoryAttr($query, $value)
  127. {
  128. if (is_array($value))
  129. $query->whereNotIn('category', $value);
  130. else
  131. $query->where('category', '<>', $value);
  132. }
  133. /**
  134. * 类型
  135. * @param Model $query
  136. * @param $value
  137. */
  138. public function searchTypeAttr($query, $value)
  139. {
  140. if (is_array($value))
  141. $query->whereIn('type', $value);
  142. else
  143. $query->where('type', $value);
  144. }
  145. /**
  146. * @param Model $query
  147. * @param $value
  148. */
  149. public function searchNotTypeAttr($query, $value)
  150. {
  151. if (is_array($value))
  152. $query->whereNotIn('type', $value);
  153. else
  154. $query->where('type', '<>', $value);
  155. }
  156. /**
  157. * 状态 0:带确定 1:有效 -1:无效
  158. * @param Model $query
  159. * @param $value
  160. */
  161. public function searchStatusAttr($query, $value)
  162. {
  163. $query->where('status', $value);
  164. }
  165. /**
  166. * 是否收货 0:未收货 1:已收货
  167. * @param Model $query
  168. * @param $value
  169. */
  170. public function searchTakeAttr($query, $value)
  171. {
  172. $query->where('take', $value);
  173. }
  174. /**
  175. * 模糊搜索
  176. * @param Model $query
  177. * @param $value
  178. */
  179. public function searchLikeAttr($query, $value)
  180. {
  181. $query->where(function ($query) use ($value) {
  182. $query->where('id|uid|title', 'like', "%$value%")->whereOr('uid', 'in', function ($query) use ($value) {
  183. $query->name('user')->whereLike('uid|account|nickname|phone', '%' . $value . '%')->field('uid')->select();
  184. });
  185. });
  186. }
  187. /**
  188. * 时间
  189. * @param Model $query
  190. * @param $value
  191. */
  192. public function searchAddTimeAttr($query, $value)
  193. {
  194. if (is_string($value)) $query->whereTime($query, $value);
  195. if (is_array($value) && count($value) == 2) $query->whereTime('add_time', 'between', $value);
  196. }
  197. }