// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\AdvertModel; use App\Models\UserModel; use App\Services\BaseService; use App\Services\RedisService; /** * 用户挂单广告-服务类 * Class AdvertService * @package App\Services\Common */ class AdvertService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @since 2020/11/10 * AdvertService constructor. */ public function __construct() { $this->model = new AdvertModel(); $this->userModel = new UserModel(); } /** * 静态入口 * @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 = 15) { $where = ['a.mark' => 1]; $type = isset($params['type'])? $params['type'] : 1; $businessId = isset($params['business_id'])? $params['business_id'] : 0; if($type>0){ $where['a.type'] = $type; } if($businessId>0){ $where['a.business_id'] = $businessId; } $list = $this->model->from('advert as a') ->leftJoin('member as m', 'm.id', '=', 'a.business_id') ->where($where) ->where('a.expired_at', '>', date('Y-m-d H:i:s')) ->select(['a.*', 'm.username','m.realname']) ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list? $list->toArray() :[]; if($list){ $field = $type == 1? 'usdt_buy_price' : 'usdt_sell_price'; $marketPrice = \App\Services\ConfigService::make()->getConfigByCode($field); foreach($list['data'] as &$item){ $expiredAt = isset($item['expired_at']) && $item['expired_at']? strtotime($item['expired_at']) : 0; $item['expired'] = max(0,$expiredAt-time()); $item['expired_text'] = $expiredAt>0? formatTime($expiredAt - time()) : '已过期'; // 固定价格和市场浮动价格处理 $item['logo_index'] = intval($item['business_id']%5); $item['username_text'] = $item['username']? mb_substr($item['username'], 0,1, 'utf-8'):''; $item['username_hidden'] = $item['username']? format_account($item['username']):''; $item['create_time_text'] = $item['create_time']? datetime($item['create_time']):''; $item['limit_min'] = moneyFormat($item['limit_min'], 2); $item['limit_max'] = moneyFormat($item['limit_max'], 2); $priceType = isset($item['price_type'])? intval($item['price_type']) : 0; $item['real_price'] = $priceType==2? moneyFormat($item['price']+$marketPrice, 2) : $item['price']; } } return [ 'pageSize'=> $pageSize, 'total'=>isset($list['total'])? $list['total'] : 0, 'list'=> isset($list['data'])? $list['data'] : [] ]; } /** * 获取资料详情 * @param $where * @param array $field */ public function getInfo($where, array $field = []) { $field = $field ? $field : ['*']; if (is_array($where)) { $info = $this->model->where($where)->select($field)->first(); } else { $info = $this->model->where(['id' => (int)$where])->select($field)->first(); } $info = $info ? $info->toArray() : []; return $info; } /** * @param $userId * @param $data * @return array */ public function saveData($userId, $data){ $memberInfo = MemberService::make()->getInfo($userId); $tradePassword = isset($memberInfo['trade_password'])? $memberInfo['trade_password'] : ''; $password = isset($data['trade_password'])?$data['trade_password']: ''; // 交易密码 if (empty($tradePassword)) { return message(2015, false); } if(get_password($password.md5($password.'otc')) != $tradePassword){ return message(2016, false); } // 开始时间 if ($data['pay_type']) { $data['pay_type'] = implode(',', $data['pay_type']); } $data['business_id'] = $userId; return parent::edit($data); // TODO: Change the autogenerated stub } }