AdvertService.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\Api;
  12. use App\Models\AdvertModel;
  13. use App\Services\BaseService;
  14. /**
  15. * 用户挂单广告-服务类
  16. * Class AdvertService
  17. * @package App\Services\Api
  18. */
  19. class AdvertService extends BaseService
  20. {
  21. // 静态对象
  22. protected static $instance = null;
  23. /**
  24. * 构造函数
  25. * @since 2020/11/10
  26. * AdvertService constructor.
  27. */
  28. public function __construct()
  29. {
  30. $this->model = new AdvertModel();
  31. }
  32. /**
  33. * 静态入口
  34. * @return static|null
  35. */
  36. public static function make()
  37. {
  38. if (!self::$instance) {
  39. self::$instance = (new static());
  40. }
  41. return self::$instance;
  42. }
  43. /**
  44. * 获取挂单广告列表
  45. * @param $params 参数
  46. * @param int $pageSize 分页大小:默认 15
  47. * @return array
  48. */
  49. public function getDataList($params, $pageSize = 15)
  50. {
  51. $where = ['a.mark' => 1, 'a.status' => 1];
  52. $type = isset($params['type'])? $params['type'] : 1;
  53. if($type>0){
  54. $where['type'] = $type;
  55. }
  56. $list = $this->model->from('advert as a')
  57. ->leftJoin('member as m', 'm.id', '=', 'a.business_id')
  58. ->where($where)
  59. ->where('a.expired_at', '>', date('Y-m-d H:i:s'))
  60. ->select(['a.*', 'm.username'])
  61. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  62. $list = $list? $list->toArray() :[];
  63. if($list){
  64. $field = $type == 1? 'usdt_buy_price' : 'usdt_sell_price';
  65. $marketPrice = \App\Services\ConfigService::make()->getConfigByCode($field);
  66. foreach($list['data'] as &$item){
  67. $expiredAt = isset($item['expired_at']) && $item['expired_at']? strtotime($item['expired_at']) : 0;
  68. $item['expired'] = max(0,$expiredAt-time());
  69. $item['expired_text'] = $expiredAt>0? formatTime($expiredAt - time()) : '已过期';
  70. // 固定价格和市场浮动价格处理
  71. $item['source_price'] = $item['price'];
  72. $item['logo_index'] = intval($item['business_id']%5);
  73. $item['username_text'] = $item['username']? mb_substr($item['username'], 0,1, 'utf-8'):'';
  74. $item['limit_min'] = moneyFormat($item['limit_min'], 2);
  75. $item['limit_max'] = moneyFormat($item['limit_max'], 2);
  76. $priceType = isset($item['price_type'])? intval($item['price_type']) : 0;
  77. $item['price'] = $priceType==2? moneyFormat($item['price']+$marketPrice, 2) : $item['price'];
  78. }
  79. }
  80. return [
  81. 'pageSize'=> $pageSize,
  82. 'total'=>isset($list['total'])? $list['total'] : 0,
  83. 'list'=> isset($list['data'])? $list['data'] : []
  84. ];
  85. }
  86. }