HandleBatchAccount.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php declare(strict_types=1);
  2. namespace app\admin\command;
  3. use app\model\AccountBatchLogModel;
  4. use app\model\AccountModel;
  5. use app\model\CommonModel;
  6. use think\console\Command;
  7. use think\console\Input;
  8. use think\console\Output;
  9. use think\Exception;
  10. use think\facade\Cache;
  11. use think\facade\Config;
  12. use think\facade\Db;
  13. //处理批量添加的账户数据
  14. class HandleBatchAccount extends Command
  15. {
  16. private static $account_username_prefix = '';
  17. protected function configure()
  18. {
  19. // parent::configure(); // TODO: Change the autogenerated stub
  20. $this->setName('HandleBatchAccount')->setDescription('处理批量添加的账户数据');
  21. }
  22. protected function execute(Input $input, Output $output)
  23. {
  24. $key = 'handle_batch_account';
  25. $redis = Cache::store('redis')->get($key);
  26. if (!$redis) {
  27. Cache::set($key, 1, 60);
  28. $rs = AccountBatchLogModel::field(true)
  29. ->where('status', AccountBatchLogModel::$status_wait_handle)
  30. ->findOrEmpty()
  31. ->toArray();
  32. if (!empty($rs)) {
  33. //处理中
  34. $this->call($rs['id'], AccountBatchLogModel::$status_handle_ing);
  35. try {
  36. self::$account_username_prefix = env('account_username_prefix');
  37. $temp = AccountModel::field('id,username')
  38. ->where('is_del', CommonModel::$del_normal)
  39. ->whereLike('username', self::$account_username_prefix . $rs['username_prefix'] . $rs['username_year'] . '____')
  40. ->findOrEmpty()
  41. ->toArray();
  42. if (!empty($temp)) throw new Exception($temp['username'] . '账号已存在');
  43. $sql = $this->getSql($rs);
  44. $num = Db::execute($sql);
  45. //处理成功
  46. $this->call($rs['id'], AccountBatchLogModel::$status_handle_success);
  47. $output->writeln('处理成功,共添加了' . $num . '个账号');
  48. } catch (Exception $exception) {
  49. //处理失败
  50. $this->call($rs['id'], AccountBatchLogModel::$status_handle_fail, $exception->getMessage());
  51. }
  52. }
  53. Cache::set($key, 0);
  54. }
  55. }
  56. private function getSql(array $rs = []): string
  57. {
  58. $date = date('Y-m-d H:i:s');
  59. $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 ';
  60. for ($i = 0; $i <= 9999; $i++) {
  61. $pwd = randomkeys(6);
  62. $salt = randomkeys(6);
  63. //拼接SQL语句
  64. $insert .= "('" . self::$account_username_prefix . $rs['username_prefix'] . $rs['username_year'] . str_pad((string)$i, 4, '0', STR_PAD_LEFT) . "','" . $pwd . "','" . $salt . "','" . get_password($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 . "'),";
  65. }
  66. return substr($insert, 0, -1) . ';';
  67. }
  68. //处理结果的回调
  69. private function call(int $id = 0, int $status = 0, string $reason = ''): bool
  70. {
  71. return (bool)AccountBatchLogModel::where('id', $id)
  72. ->save(['status' => $status, 'reason' => $reason, 'updatetime' => date('Y-m-d H:i:s')]);
  73. }
  74. }