ModelTrait.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 crmeb\traits;
  12. use think\Model;
  13. /**
  14. * Trait ModelTrait
  15. * @package crmeb\traits
  16. */
  17. trait ModelTrait
  18. {
  19. /**
  20. * 时间段搜索器
  21. * @param Model $query
  22. * @param $value
  23. */
  24. public function searchTimeAttr($query, $value, $data)
  25. {
  26. if ($value) {
  27. $timeKey = $data['timeKey'] ?? (property_exists($this, 'timeKey') && $this->timeKey ? $this->timeKey : 'add_time');
  28. if (is_array($value)) {
  29. $startTime = $value[0] ?? 0;
  30. $endTime = $value[1] ?? 0;
  31. if ($startTime || $endTime) {
  32. try {
  33. date('Y-m-d', $startTime);
  34. } catch (\Throwable $e) {
  35. $startTime = strtotime($startTime);
  36. }
  37. try {
  38. date('Y-m-d', $endTime);
  39. } catch (\Throwable $e) {
  40. $endTime = strtotime($endTime);
  41. }
  42. if ($startTime == $endTime || $endTime == strtotime(date('Y-m-d', $endTime))) {
  43. $endTime = $endTime + 86400;
  44. }
  45. $query->whereBetween($timeKey, [$startTime, $endTime]);
  46. }
  47. } elseif (is_string($value)) {
  48. switch ($value) {
  49. case 'today':
  50. case 'week':
  51. case 'month':
  52. case 'year':
  53. case 'yesterday':
  54. case 'last year':
  55. case 'last week':
  56. case 'last month':
  57. $query->whereTime($timeKey, $value);
  58. break;
  59. case 'quarter':
  60. [$startTime, $endTime] = $this->getMonth();
  61. $query->whereBetween($timeKey, [strtotime($startTime), strtotime($endTime)]);
  62. break;
  63. case 'lately7':
  64. $query->whereBetween($timeKey, [strtotime("-7 day"), time()]);
  65. break;
  66. case 'lately30':
  67. $query->whereBetween($timeKey, [strtotime("-30 day"), time()]);
  68. break;
  69. default:
  70. if (strstr($value, '-') !== false) {
  71. [$startTime, $endTime] = explode('-', $value);
  72. $startTime = trim($startTime) ? strtotime($startTime) : 0;
  73. $endTime = trim($endTime) ? strtotime($endTime) : 0;
  74. if ($startTime && $endTime) {
  75. if ($startTime == $endTime || $endTime == strtotime(date('Y-m-d', $endTime))) {
  76. $endTime = $endTime + 86400;
  77. }
  78. $query->whereBetween($timeKey, [$startTime, $endTime]);
  79. } else if (!$startTime && $endTime) {
  80. $query->whereTime($timeKey, '<', $endTime + 86400);
  81. } else if ($startTime && !$endTime) {
  82. $query->whereTime($timeKey, '>=', $startTime);
  83. }
  84. }
  85. break;
  86. }
  87. }
  88. }
  89. }
  90. /**
  91. * 获取本季度 time
  92. * @param int $ceil
  93. * @return array
  94. */
  95. public function getMonth(int $ceil = 0)
  96. {
  97. if ($ceil != 0) {
  98. $season = ceil(date('n') / 3) - $ceil;
  99. } else {
  100. $season = ceil(date('n') / 3);
  101. }
  102. $firstday = date('Y-m-01', mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')));
  103. $lastday = date('Y-m-t', mktime(0, 0, 0, $season * 3, 1, date('Y')));
  104. return [$firstday, $lastday];
  105. }
  106. /**
  107. * 获取某个字段内的值
  108. * @param $value
  109. * @param string $filed
  110. * @param string $valueKey
  111. * @param array|string[] $where
  112. * @return mixed
  113. */
  114. public function getFieldValue($value, string $filed, ?string $valueKey = '', ?array $where = [])
  115. {
  116. $model = $this->where($filed, $value);
  117. if ($where) {
  118. $model->where(...$where);
  119. }
  120. return $model->value($valueKey ?: $filed);
  121. }
  122. }