// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\PriceLogModel; use App\Services\BaseService; /** * SBT价格记录管理-服务类 * @author laravel开发员 * @since 2020/11/11 * @package App\Services\Common */ class PriceLogService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 */ public function __construct() { $this->model = new PriceLogModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * 获取列表 * @param $params 参数 * @param int $pageSize 分页大小:默认 15 * @return array */ public function getDataList($params, $pageSize = 10, $field = []) { $query = $this->getQuery($params); $list = $query->select($field ? $field : ['a.*']) ->orderBy('a.date','desc') ->orderBy('a.id','desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list ? $list->toArray() : []; if ($list) { foreach ($list['data'] as &$item) { $item['create_time'] = datetime($item['create_time'],'Y-m-d H:i:s'); } } return [ 'pageSize' => $pageSize, 'total' => isset($list['total']) ? $list['total'] : 0, 'list' => isset($list['data']) ? $list['data'] : [] ]; } /** * 查询构造 * @param $params * @return \Illuminate\Database\Eloquent\Builder */ public function getQuery($params) { $where = ['a.mark' => 1]; return $this->model->from('price_logs as a') ->where($where) ->where(function ($query) use($params){ // 日期 $date = isset($params['date']) ? $params['date'] : []; $start = isset($date[0])? $date[0] : ''; $end = isset($date[1])? $date[1] : ''; $end = $start>$end? '' : $end; if ($start) { $query->where('a.date','>=', $start); } if($end){ $query->where('a.date','<=', $end); } $status = isset($params['status'])? $params['status'] : 0; if ($status && is_array($status)) { $query->whereIn('a.status', $status); } else if($status>0){ $query->where('a.status', $status); } }); } }