UserBill.php 5.5 KB

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