浏览代码

Merge branch 'dev_wf' of wufeng/fuse into version1.5

wufeng 2 年之前
父节点
当前提交
a869cbbbad

+ 105 - 0
app/admin/command/HandleBatchAccount.php

@@ -0,0 +1,105 @@
+<?php declare(strict_types=1);
+
+namespace app\admin\command;
+
+use app\model\AccountBatchLogModel;
+use app\model\AccountModel;
+use app\model\CommonModel;
+use think\console\Command;
+use think\console\Input;
+use think\console\Output;
+use think\Exception;
+use think\facade\Cache;
+use think\facade\Config;
+use think\facade\Db;
+
+//处理批量添加的账户数据
+class HandleBatchAccount extends Command
+{
+    private static $account_username_prefix = '';
+
+    protected function configure()
+    {
+//        parent::configure(); // TODO: Change the autogenerated stub
+        $this->setName('HandleBatchAccount')->setDescription('处理批量添加的账户数据');
+
+    }
+
+
+    protected function execute(Input $input, Output $output)
+    {
+
+        $key = 'handle_batch_account';
+        $redis = Cache::store('redis')->get($key);
+
+        if (!$redis) {
+
+            Cache::set($key, 1, 60);
+
+            $rs = AccountBatchLogModel::field(true)
+                ->where('status', AccountBatchLogModel::$status_wait_handle)
+                ->findOrEmpty()
+                ->toArray();
+
+            if (!empty($rs)) {
+
+                //处理中
+                $this->call($rs['id'], AccountBatchLogModel::$status_handle_ing);
+
+                try {
+
+                    self::$account_username_prefix = env('account_username_prefix');
+
+                    $temp = AccountModel::field('id,username')
+                        ->where('is_del', CommonModel::$del_normal)
+                        ->whereLike('username', self::$account_username_prefix . $rs['username_prefix'] . $rs['username_year'] . '____')
+                        ->findOrEmpty()
+                        ->toArray();
+                    if (!empty($temp)) throw new Exception($temp['username'] . '账号已存在');
+
+                    $sql = $this->getSql($rs);
+
+                    $num = Db::execute($sql);
+
+                    //处理成功
+                    $this->call($rs['id'], AccountBatchLogModel::$status_handle_success);
+
+                    $output->writeln('处理成功,共添加了' . $num . '个账号');
+
+                } catch (Exception $exception) {
+                    //处理失败
+                    $this->call($rs['id'], AccountBatchLogModel::$status_handle_fail, $exception->getMessage());
+                }
+            }
+
+            Cache::set($key, 0);
+        }
+
+    }
+
+
+    private function getSql(array $rs = []): string
+    {
+        $date = date('Y-m-d H:i:s');
+        $insert = 'INSERT INTO `' . Config::get('database.connections.mysql.database') . '`.`' . Config::get('database.connections.mysql.prefix') . 'account` (`username`,`pwd`,`salt`,`password`,`company_id`,`card_id`,`status`,`is_del`,`starttime`,`expiretime`,`createrid`,`creater`,`addtime`,`updaterid`,`updater`,`updatetime`) VALUES ';
+        for ($i = 0; $i <= 9999; $i++) {
+            $pwd = randomkeys(6);
+            $salt = randomkeys(4);
+
+            //拼接SQL语句
+            $insert .= "('" . self::$account_username_prefix . $rs['username_prefix'] . $rs['username_year'] . str_pad((string)$i, 4, '0', STR_PAD_LEFT) . "','" . $pwd . "','" . $salt . "','" . getPassword($pwd, $salt) . "'," . $rs['company_id'] . "," . $rs['card_id'] . "," . AccountModel::$status_not_active . "," . CommonModel::$del_normal . ",'" . $rs['starttime'] . "','" . $rs['expiretime'] . "'," . $rs['createrid'] . ",'" . $rs['creater'] . "','" . $date . "'," . $rs['createrid'] . ",'" . $rs['creater'] . "','" . $date . "'),";
+        }
+
+        return substr($insert, 0, -1) . ';';
+    }
+
+
+    //处理结果的回调
+    private function call(int $id = 0, int $status = 0, string $reason = ''): bool
+    {
+        return (bool)AccountBatchLogModel::where('id', $id)
+            ->save(['status' => $status, 'reason' => $reason, 'updatetime' => date('Y-m-d H:i:s')]);
+
+    }
+
+}

+ 0 - 80
app/admin/command/PushDataToSupplier.php

@@ -1,80 +0,0 @@
-<?php declare(strict_types=1);
-
-namespace app\api\command;
-
-use think\console\Command;
-use think\console\Input;
-use think\console\Output;
-use think\facade\Cache;
-use think\facade\Config;
-use think\facade\Db;
-
-//推送数据到供应商api
-class PushDataToSupplier extends Command
-{
-    private $key = 'gys_push_data';//供应商推送数据的key,本目录下配置文件读取不到,所以指定在这里,轻易不要修改,要和其他地方保持一致
-
-    protected function configure()
-    {
-
-//        parent::configure(); // TODO: Change the autogenerated stub
-        $this->setName('PushDataToSupplier')->setDescription('推送数据到供应商api');
-
-    }
-
-
-    protected function execute(Input $input, Output $output)
-    {
-        $da = Cache::store('redis')->handler()->rpop($this->key);
-//        $da = json_encode([
-//            'supplierNo' => 'QS2203150147019298',
-//            'type' => 'cgd',//cgd销售订单,online上线结果
-//            'data' => ['name' => '张三'],
-//        ], JSON_UNESCAPED_UNICODE);
-
-        if ($da) {
-
-            $da = json_decode($da, true);
-
-            //有推送地址
-            $res = Db::name('development')
-                ->field('id,push_url')
-                ->where('supplierNo', $da['supplierNo'])
-                ->where('push_url', '<>', '')
-                ->withAttr('push_url', function ($val) {
-                    return explode(',', $val);
-                })
-                ->findOrEmpty();
-
-            if ($res) {
-                $insert = [];
-                foreach ($res['push_url'] as $url){
-                    $response = curl_request($url,$da);
-                    $insert[]=[
-                        'supplierNo'=>$da['supplierNo'],
-                        'push_url'=>$url,
-                        'type'=>$da['type'],
-                        'data'=>json_encode($da,JSON_UNESCAPED_UNICODE),
-                        'response'=>$response
-                    ];
-                }
-                if($insert) Db::name('push_log')->insertAll($insert);
-            }
-        }
-
-//        return parent::execute($input, $output); // TODO: Change the autogenerated stub
-
-        //订阅?
-
-        //哪些数据?
-
-        //订单
-        //产品上线的状态
-
-
-        //数据格式
-
-
-    }
-
-}

+ 0 - 1
app/admin/config/common.php

@@ -3,5 +3,4 @@
 return [
     'expire'=>1800,//token的有效时长
     'default_password'=>'123456',//运营账号默认密码
-    'account_username_prefix'=>'CJ1745',//卡号前面的固定值前缀
 ];

+ 13 - 49
app/admin/logic/AccountLogic.php

@@ -2,6 +2,7 @@
 
 namespace app\admin\logic;
 
+use app\model\AccountBatchLogModel;
 use app\model\AccountModel;
 use app\model\CommonModel;
 use think\Exception;
@@ -38,9 +39,11 @@ class AccountLogic extends BaseLogic
     //添加
     public static function add(array $data = []): Json
     {
+        $username_prefix = env('account_username_prefix');
+
         $rs = AccountModel::field('id,username')
             ->where('is_del', CommonModel::$del_normal)
-            ->where('username', $data['username'])
+            ->where('username', $username_prefix.$data['username'])
             ->findOrEmpty()
             ->isEmpty();
         if (!$rs) return json_show(CommonModel::$error_param, '该账号已存在');
@@ -50,7 +53,7 @@ class AccountLogic extends BaseLogic
         $date = date('Y-m-d H:i:s');
 
         $res = AccountModel::create([
-            'username' => Config::get('common.account_username_prefix') . $data['username'],
+            'username' => $username_prefix . $data['username'],
             'pwd' => $pwd,
             'salt' => $salt,
             'password' => getPassword($pwd, $salt),
@@ -108,52 +111,13 @@ class AccountLogic extends BaseLogic
     //批量添加账户
     public static function batchAdd(array $data = []): Json
     {
-        Db::startTrans();
-
-        try {
-
-            $rs = AccountModel::field('id,username')
-                ->where('is_del', CommonModel::$del_normal)
-                ->whereLike('username', $data['username_prefix'] . $data['username_year'] . '____')
-                ->findOrEmpty();
-            if (!$rs->isEmpty()) throw new Exception($rs->username . '账号已存在');
-
-            $date = date('Y-m-d H:i:s');
-
-            for ($i = 0; $i <= 9999; $i++) {
-                $pwd = randomkeys(6);
-                $salt = randomkeys(4);
-                Db::name('account')->insert([
-                    'username' => Config::get('common.account_username_prefix') . $data['username_prefix'] . $data['username_year'] . str_pad($i, 4, '0', STR_PAD_LEFT),
-                    'pwd' => $pwd,
-                    'salt' => $salt,
-                    'password' => getPassword($pwd, $salt),
-                    'company_id' => $data['company_id'],
-                    'card_id' => $data['card_id'],
-                    'status' => AccountModel::$status_not_active,
-                    'is_del' => CommonModel::$del_normal,
-                    'starttime' => $data['starttime'],
-                    'expiretime' => date('Y-m-d', strtotime($data['expiretime']) + 3600 * 24),
-                    'createrid' => self::$uid,
-                    'creater' => self::$uname,
-                    'addtime' => $date,
-                    'updaterid' => self::$uid,
-                    'updater' => self::$uname,
-                    'updatetime' => $date,
-                ]);//比insertAll方法快2/3
-
-            }
-
-            Db::name('account_batch_log')->insert(array_merge($data, ['addtime' => date('Y-m-d H:i:s')]));
-
-            Db::commit();
-
-            return json_show(CommonModel::$success, '10000个账户批量添加成功');
-
-        } catch (Exception $exception) {
-            Db::rollback();
-            return json_show(CommonModel::$error_param, '批量添加账户失败,' . $exception->getMessage());
-        }
+        $rs = AccountBatchLogModel::create(array_merge($data,[
+                'status'=>AccountBatchLogModel::$status_wait_handle,
+                'addtime'=>date('Y-m-d H:i:s'),
+            ]))->save();
+
+        return $rs?json_show(CommonModel::$success,'批量添加账户成功'):json_show(CommonModel::$error_param,'批量添加账户失败');
+
     }
 
     //删除
@@ -185,7 +149,7 @@ class AccountLogic extends BaseLogic
             ->leftJoin('company b', 'b.id=a.company_id AND b.is_del=' . CommonModel::$del_normal)
             ->leftJoin('card c', 'c.id=a.card_id AND c.is_del=' . CommonModel::$del_normal)
             ->where($where)
-            ->field('a.id,b.title company_title,c.title card_title,a.username_prefix,a.username_year,a.starttime,a.expiretime,a.addtime')
+            ->field('a.id,b.title company_title,c.title card_title,a.username_prefix,a.username_year,a.starttime,a.expiretime,a.status,a.reason,a.addtime')
             ->order('a.id', 'desc')
             ->page($data['page'], $data['size'])
             ->select()

+ 19 - 0
app/model/AccountBatchLogModel.php

@@ -0,0 +1,19 @@
+<?php
+
+namespace app\model;
+
+use think\Model;
+
+class AccountBatchLogModel extends Model
+{
+
+    protected $table = 'fc_account_batch_log';
+    protected $pk = 'id';
+
+    //状态
+    public static $status_wait_handle = 1;//待处理
+    public static $status_handle_ing = 2;//处理中
+    public static $status_handle_success = 3;//处理成功
+    public static $status_handle_fail = 4;//处理失败
+
+}