OrderMessageForm.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\Order;
  9. use app\models\OrderDetail;
  10. use app\models\OrderMessage;
  11. use app\models\Shop;
  12. use app\models\ShopShare;
  13. use app\models\User;
  14. use Yii;
  15. use yii\base\Model;
  16. use yii\data\Pagination;
  17. class OrderMessageForm extends Model
  18. {
  19. public $store_id;
  20. public $limit;
  21. public $page;
  22. public $is_read;
  23. public function rules()
  24. {
  25. return [
  26. [['limit', 'page'], 'integer'],
  27. [['limit'], 'default', 'value' => 5],
  28. [['page'], 'default', 'value' => 1]
  29. ];
  30. }
  31. public function search()
  32. {
  33. if (!$this->validate()) {
  34. return $this->errorResponse;
  35. }
  36. $query = OrderMessage::find()->alias('om')->where([
  37. 'om.store_id' => $this->store_id,
  38. 'om.is_delete' => 0,
  39. 'om.order_type' => [0, 1, 2, 3, 4, 6, 7],
  40. ]);
  41. if ($this->is_read) {
  42. $query->andWhere(['or',['om.is_read' => 0],['om.is_sound'=>0]]);
  43. }
  44. $query->andWhere(['>=','om.created_at', strtotime('2022-09-23')]);
  45. $count = $query->count();
  46. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit]);
  47. $list = $query->orderBy(['om.created_at' => SORT_DESC])
  48. ->limit($pagination->limit)
  49. ->offset($pagination->offset)
  50. ->asArray()
  51. ->all();
  52. $urlManager = \Yii::$app->urlManager;
  53. // 此处考虑在 cache 或 setting 内做缓存,存储 order_message 表的更新时间,以及查询结果
  54. // 插入 order_message 时,在 beforeInsert 内清空缓存
  55. // -- wi1dcard
  56. foreach ($list as &$value) {
  57. $time = time() - $value['created_at'];
  58. if ($time < 60) {
  59. $value['time'] = $time . '秒前';
  60. } elseif ($time < 3600) {
  61. $value['time'] = ceil($time / 60) . '分钟前';
  62. } elseif ($time < 86400) {
  63. $value['time'] = ceil($time / 3600) . '小时前';
  64. } else {
  65. $value['time'] = ceil($time / 86400) . '天前';
  66. }
  67. $value['created_at'] = date("Y-m-d H:i:s", $value['created_at']);
  68. }
  69. return [
  70. 'list' => $list,
  71. 'count' => $count,
  72. 'pagination' => $pagination
  73. ];
  74. }
  75. }