Commit 56dcefed authored by shajiaiming's avatar shajiaiming

fix

parent 343e2798
......@@ -2,10 +2,12 @@
namespace api\controllers;
use common\components\Tools;
use Yii;
use api\base\BaseController;
use common\models\psources\CoinAirDropTransfer;
use common\models\psources\CoinAddress;
use common\models\psources\CoinPlatformCoins;
use Yii;
use common\models\psources\CoinAirDropTransfer;
class PlatformCoinsController extends BaseController
{
......@@ -164,4 +166,41 @@ class PlatformCoinsController extends BaseController
return ['code' => 0, 'data' => null, 'msg' => '数据导入成功'];
}
public function actionRelation()
{
$data = Yii::$app->request->post();
if (empty($data)) {
$this->code = -1;
$this->msg = '参数错误';
goto doEnd;
}
$item_array = [];
foreach ($data as $val) {
$address = $val['address'] ?? null;
$mobile = $val['mobile'] ?? null;
if (empty($address) || empty($mobile)) continue;
$count = CoinAddress::find()->where(['address' => $address, 'mobile' => $mobile])->count();
if ($count > 0) continue;
$item_array[] = [
$address,
$mobile,
];
}
if (empty($item_array)) {
goto doEnd;
}
$result = CoinAddress::batchImport(Tools::super_unique($item_array));
if (!$result) {
$this->code = -1;
$this->msg = '数据导入失败';
goto doEnd;
}
doEnd :
return ['code' => $this->code, 'data' => $this->data, 'msg' => $this->msg];
}
}
\ No newline at end of file
......@@ -157,6 +157,35 @@ class Tools
return $array;
}
public static function assoc_unique($arr, $key)
{
$tmp_arr = array();
foreach ($arr as $k => $v) {
if (in_array($v[$key], $tmp_arr)) {
unset($arr[$k]);
} else {
$tmp_arr[] = $v[$key];
}
}
$arr = array_values($arr); //sort函数对数组进行排序
return $arr;
}
public static function super_unique($array, $recursion = false)
{
// 序列化数组元素,去除重复
$result = array_map('unserialize', array_unique(array_map('serialize', $array)));
// 递归调用
if ($recursion) {
foreach ($result as $key => $value) {
if (is_array($value)) {
$result[$key] = super_unique($value);
}
}
}
return $result;
}
public static function getDatesFromRange($start, $end, $format = 'Y-m-d')
{
// Declare an empty array
......
<?php
namespace common\models\psources;
use Yii;
use common\core\BaseActiveRecord;
class CoinAddress extends BaseActiveRecord
{
//定义场景
const SCENARIOS_CREATE = 'create';
public static function getDb()
{
return Yii::$app->get('p_sources');
}
public static function tableName()
{
return '{{%coin_address}}';
}
public function rules()
{
return [
[['address', 'mobile'], 'required'],
];
}
public function scenarios()
{
$scenarios = [
self:: SCENARIOS_CREATE => ['address', 'mobile'],
];
return array_merge(parent:: scenarios(), $scenarios);
}
/**
* 导入数据
*
* @param array $datas
* @return boolean
*/
public static function batchImport(array $datas)
{
if (!is_array($datas)) {
return false;
}
try {
$result = self::getDb()->createCommand()->batchInsert(self::tableName(), ['address', 'mobile'], $datas)->execute();
return $result;
} catch (\Exception $e) {
var_dump($e->getMessage());exit;
}
return false;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment