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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
/**
* Created By Sublime Text 3
*
* @date 2018-09-06 14:07:10
* @authors rlgy <rlgyzhcn@qq.com>
*/
namespace common\models\psources;
use common\core\BaseActiveRecord;
use Yii;
use common\business\Chain33Business;
use backend\jobs\QueryTransJob;
/**
* @property integer $id
* @property integer $uid
* @property integer $mid
* @property integer $list_id
* @property string $txhash
* @property string $mobile
* @property string $coin
* @property double $amount
* @property string $to_address
* @property integer $check_first_status
* @property integer $check_first_uid
* @property integer $check_second_status
* @property integer $check_second_uid
* @property integer $status
* @property string $company_name
* @property string $create_time
* @property string $update_time
*/
class CoinReleaseCheck extends BaseActiveRecord
{
const SCENARIOS_CHECK = 'check'; //审核列表筛选
const SCENARIOS_SEARCH = 'search'; //查询列表筛选
const CHECK_FIRST_STATUS_NORMAL = 1; // 初审未审核
const CHECK_FIRST_STATUS_SUCCEED = 2; // 初审审核通过
const CHECK_FIRST_STATUS_FAILD = 3; // 初审审核未通过
const CHECK_SECOND_STATUS_NORMAL = 1; // 复审未审核
const CHECK_SECOND_STATUS_SUCCEED = 2; // 复审审核通过
const CHECK_SECOND_STATUS_FAILD = 3; // 复审审核未通过
const STATUS_NORMAL = 0; // 提币申请审核未通过
const STATUS_PADDING = 1; // 提币申请审核完成,提币未完成
const STATUS_CANCEL = 2; // 提币申请被撤提
const STATUS_FAILD = 3; // 提币失败
const STATUS_SUCCEED = 4; // 提币成功
const STATUS_AUTO_CHECKING = 5; //自动审核中
const STATUS_AUTO_FAILD = 6; //自动审核失败
const STATUS_AUTO_EXCEPTION = 7; //自动审核异常(区块链异常)
public $start_time = '';
public $end_time = '';
public static function getDb()
{
return Yii::$app->get('p_sources');
}
public static function tableName()
{
return '{{%coin_release_check}}';
}
public function formName()
{
return '';
}
public function attributeLabels()
{
return [
'id' => 'ID',
'uid' => 'UID',
'list_id' => '记录ID',
'txhash' => '交易txhash',
'mobile' => '手机',
'coin' => '币种',
'to_address' => '对方地址',
'check_first_status' => '初审状态',
'check_first_uid' => '初审员',
'check_second_status' => '复审状态',
'check_second_uid' => '复审员',
'create_time' => '创建时间',
'update_time' => '更新时间',
'start_time' => '提交开始时间',
'end_time' => '提交结束时间',
'status' => '审核状态',
];
}
public function sercians()
{
return [
self::SCENARIOS_CHECK => ['coin', 'start_time', 'end_time', 'mobile'],
self::SCENARIOS_SEARCH => ['status', 'coin', 'check_first_uid', 'check_second_uid', 'start_time', 'end_time', 'mobile'],
];
}
public function rules()
{
return [
[['id', 'uid', 'check_first_status', 'check_first_uid', 'check_second_status', 'check_second_uid', 'status', 'list_id'], 'integer'],
[['mobile', 'coin', 'to_address', 'create_time', 'update_time', 'start_time', 'end_time'], 'string', 'max' => 255],
[['end_time'], 'compare', 'compareAttribute' => 'start_time', 'operator' => '>=', 'skipOnEmpty' => true],
[['coin', 'start_time', 'end_time', 'mobile'], 'default', 'value' => '', 'on' => self::SCENARIOS_CHECK],
[['status', 'coin', 'check_first_uid', 'check_second_uid', 'start_time', 'end_time', 'mobile'], 'default', 'value' => '', 'on' => self::SCENARIOS_SEARCH],
];
}
/**
* 初审
*
* @param int $status
* @throws \Exception
* @return bool
*/
public function check_first(int $status)
{
if ($this->check_second_status != CoinReleaseCheck::CHECK_SECOND_STATUS_NORMAL) {
throw new \Exception('已经复核的申请不允许初审', -1);
}
if (self::CHECK_FIRST_STATUS_FAILD == $status) {
$this->check_first_status = $status;
return $this->check_cancel();
} else {
$this->check_first_uid = Yii::$app->user->id;
$this->check_first_status = $status;
return $this->save();
}
}
/**
* @param int $status
* @param string $address 提币地址
* @throws \Exception
* @return bool
*/
public function check_second($status, $address = '')
{
if (self::CHECK_FIRST_STATUS_SUCCEED != $this->check_first_status) {
throw new \Exception('初审未通过!', -1);
}
if (CoinReleaseCheck::CHECK_SECOND_STATUS_FAILD == $status) {
$this->check_second_status = $status;
return $this->check_cancel();
} else {
//开启事务
$trans = self::getDb()->beginTransaction();
try {
$this->check_second_uid = Yii::$app->user->id;
$this->check_second_status = $status;
$this->status = self::STATUS_PADDING;
//区块链转币
if (strtoupper($this->coin) == 'BTY') {
$isToken = false;
} else {
$isToken = true;
}
$walletpasswd = Yii::$app->redis->get('wallet_passwd');
$node_params = Yii::$app->params['chain_parallel']['secondary'];
$unChain = Chain33Business::unLockWallet($walletpasswd, $node_params);
if(false == $unChain['result']['isOK']){
throw new \Exception($unChain['result']['msg'], -1);
}
$result = Chain33Business::tradeToken(
(int)$this->amount,
'',
1000,
$isToken,
trim($this->to_address),
$this->coin,
$node_params
);
// $result = Chain33Business::transToken(
// trim($address),
// trim($this->to_address),
// (int)$this->amount,
// "锁仓释放",
// $isToken,
// $this->coin
// );
if (is_array($result)) {
$trans->rollBack();
throw new \Exception(Yii::t('backend', $result['msg']), $result['code']);
}
$this->txhash = $result;
if ($this->save(false)) {
Yii::$app->queue->delay(15)->push(new QueryTransJob([
'txhash' => $result,
'lid' => $this->list_id,
'cid' => $this->id
]));
$trans->commit();
return true;
} else {
throw new \Exception('修改审核状态失败', -1);
}
} catch (\Exception $exception) {
throw $exception;
}
}
}
/**
* 提币审核撤提
*
* @return bool
* @throws \Exception
*/
public function check_cancel()
{
//开启事务
$trans = self::getDb()->beginTransaction();
try {
$this->status = self::STATUS_CANCEL;
if (empty($this->check_first_uid)) {
$this->check_first_uid = Yii::$app->user->id;
}
if (empty($this->check_second_uid)) {
$this->check_second_uid = Yii::$app->user->id;
}
if ($this->save(false)) {
//修改提币记录
$coin_release_list = CoinReleaseList::findOne($this->list_id);
if ($coin_release_list) {
$coin_release_list->status = 2;//提币失败
if ($coin_release_list->save(false)) {
//返还用户资产
$coin_release_member = CoinReleaseMember::findOne($this->mid);
$coin_release_member->release += $this->amount;
$coin_release_member->output -= $this->amount;
if ($coin_release_member->save(false)) {
$trans->commit();
return true;
} else {
throw new \Exception('返还资产失败', -1);
}
} else {
throw new \Exception('修改提币记录失败', -1);
}
} else {
throw new \Exception('获取提币记录失败', -1);
}
} else {
throw new \Exception('修改审核状态失败', -1);
}
} catch (\Exception $exception) {
$trans->rollBack();
throw $exception;
}
}
}