1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* Created By Sublime Text 3
*
* @date 2018-09-12 10:21:08
* @authors rlgy <rlgyzhcn@qq.com>
*/
namespace common\models\psources;
use common\core\BaseActiveRecord;
use common\models\psources\Coin;
use Yii;
class CoinRecommend extends BaseActiveRecord
{
public static function getDb()
{
return Yii::$app->get('p_sources');
}
public static function tableName()
{
return '{{%coin_recommend}}';
}
/**
* 获取推荐列表
*
* @param integer $page [description]
* @param integer $limit [description]
* @param array $condition [description]
* @param array $order_by [description]
* @return [type] [description]
*/
public static function getList($page = 1, $limit = 10, $condition = [], $order_by = [], $select = [])
{
$query = self::find();
if (!empty($condition)) {
$query = $query->where($condition);
}
$count = $query->count();
$data = $query->offset(($page - 1) * 10)->limit($limit);
if (!empty($order_by)) {
# code...
$data = $data->orderby($order_by);
}
$data = $data->asArray()->all();
//获取币种详情
$coin_ids = array_column($data, 'cid');
if (empty($select)) {
$coin_info = Coin::getCoinInfoByIds($coin_ids, ['id', 'name'], 'id');
} else {
$coin_info = Coin::getCoinInfoByIds($coin_ids, $select, 'id');
}
foreach ($data as $key => &$value) {
$value['coin'] = $coin_info[$value['cid']]['name'];
}
unset($key, $value);
// $sql = $query->createCommand()->getSql();
$data = ['count' => $count, 'data' => $data];
if ($count > 0) {
$data['code'] = 0;
} else {
$data['code'] = 1;
$data['msg'] = '数据为空';
}
return $data;
}
/**
* 添加推介币种
*
* @param [type] $params [description]
*/
public static function addRecommendCoin($params)
{
$id = $params['id'] ?? null;
$platform_id = $params['platform_id'] ?? 1;
$coin_name = $params['coin'] ?? '';
$recommend_type = $params['recommend'] ?? 1;
$sort = $params['sort'] ?? 0;
$type = $params['type'] ?? 1;
$chain = $params['chain'] ?? '';
$coin = Coin::findOne(['name' => strtoupper($coin_name), 'platform' => strtoupper($chain)]);
if (empty($coin)) {
return ['code' => -1, 'msg' => '币种不存在'];
}
if (!empty($id)) {
$coin_recommend = CoinRecommend::findOne($id);
} else {
$coin_recommend = new CoinRecommend();
$count = self::find()->where(['cid' => $coin->id, 'platform_id' => $platform_id, 'type' => $type, 'chain' => $chain])->count();
if ($count > 0) {
return ['code' => -1, 'msg' => '推荐币种已经存在'];
}
}
$recommend_type = $recommend_type == 1 ? 1 : 2;
$sort = (int) $sort;
$coin_recommend->cid = $coin->id;
$coin_recommend->recommend = $recommend_type;
$coin_recommend->platform_id = $platform_id;
$coin_recommend->sort = $sort;
$coin_recommend->type = $type;
$coin_recommend->chain = $chain;
if ($coin_recommend->save()) {
return ['code' => 0, 'msg' => '保存成功'];
}
return ['code' => -1, 'msg' => '保存失败'];
}
}