PriceLogService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\PriceLogModel;
  13. use App\Services\BaseService;
  14. /**
  15. * SBT价格记录管理-服务类
  16. * @author laravel开发员
  17. * @since 2020/11/11
  18. * @package App\Services\Common
  19. */
  20. class PriceLogService extends BaseService
  21. {
  22. // 静态对象
  23. protected static $instance = null;
  24. /**
  25. * 构造函数
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. */
  29. public function __construct()
  30. {
  31. $this->model = new PriceLogModel();
  32. }
  33. /**
  34. * 静态入口
  35. * @return static|null
  36. */
  37. public static function make()
  38. {
  39. if (!self::$instance) {
  40. self::$instance = (new static());
  41. }
  42. return self::$instance;
  43. }
  44. /**
  45. * 获取列表
  46. * @param $params 参数
  47. * @param int $pageSize 分页大小:默认 15
  48. * @return array
  49. */
  50. public function getDataList($params, $pageSize = 10, $field = [])
  51. {
  52. $query = $this->getQuery($params);
  53. $list = $query->select($field ? $field : ['a.*'])
  54. ->orderBy('a.date','desc')
  55. ->orderBy('a.id','desc')
  56. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  57. $list = $list ? $list->toArray() : [];
  58. if ($list) {
  59. foreach ($list['data'] as &$item) {
  60. $item['create_time'] = datetime($item['create_time'],'Y-m-d H:i:s');
  61. }
  62. }
  63. return [
  64. 'pageSize' => $pageSize,
  65. 'total' => isset($list['total']) ? $list['total'] : 0,
  66. 'list' => isset($list['data']) ? $list['data'] : []
  67. ];
  68. }
  69. /**
  70. * 查询构造
  71. * @param $params
  72. * @return \Illuminate\Database\Eloquent\Builder
  73. */
  74. public function getQuery($params)
  75. {
  76. $where = ['a.mark' => 1];
  77. return $this->model->from('price_logs as a')
  78. ->where($where)
  79. ->where(function ($query) use($params){
  80. // 日期
  81. $date = isset($params['date']) ? $params['date'] : [];
  82. $start = isset($date[0])? $date[0] : '';
  83. $end = isset($date[1])? $date[1] : '';
  84. $end = $start>$end? '' : $end;
  85. if ($start) {
  86. $query->where('a.date','>=', $start);
  87. }
  88. if($end){
  89. $query->where('a.date','<=', $end);
  90. }
  91. $status = isset($params['status'])? $params['status'] : 0;
  92. if ($status && is_array($status)) {
  93. $query->whereIn('a.status', $status);
  94. } else if($status>0){
  95. $query->where('a.status', $status);
  96. }
  97. });
  98. }
  99. }