AdvertService.php 4.0 KB

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