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('a.is_del', CommonModel::$del_normal); if ($data['username'] != '') $db->whereLike('a.username', '%' . $data['username'] . '%'); if ($data['name'] != '') $db->whereLike('a.name', '%' . $data['name'] . '%'); if ($data['mobile'] != '') $db->whereLike('a.mobile', '%' . $data['mobile'] . '%'); $count = $db->count('a.id'); $list = $db ->field('a.id,a.username,a.status,a.name,b.title company_title,c.title card_title,a.mobile,a.remark,a.activetime,a.expiretime') ->order('a.id', 'desc') ->page($data['page'], $data['size']) ->select() ->toArray(); return json_show(CommonModel::$success, '获取账户列表成功', ['count' => $count, 'list' => $list]); } //添加 public static function add(array $data = []): Json { $rs = AccountModel::field('id,username') ->where('is_del', CommonModel::$del_normal) ->where('username', $data['username']) ->findOrEmpty() ->isEmpty(); if (!$rs) return json_show(CommonModel::$error_param, '该账号已存在'); $pwd = randomkeys(6); $salt = randomkeys(4); $date = date('Y-m-d H:i:s'); $res = AccountModel::create([ 'username' => $data['username'], 'pwd' => $pwd, 'salt' => $salt, 'password' => getPassword($pwd, $salt), 'company_id' => $data['company_id'], 'card_id' => $data['card_id'], 'video_ids' => implode(',', $data['video_ids']), 'status' => AccountModel::$status_not_active, 'is_del' => CommonModel::$del_normal, 'starttime' => $data['starttime'], 'expiretime' => $data['expiretime'], 'createrid' => self::$uid, 'creater' => self::$uname, 'addtime' => $date, 'updaterid' => self::$uid, 'updater' => self::$uname, 'updatetime' => $date, ])->save(); return $res ? json_show(CommonModel::$success, '账户添加成功') : json_show(CommonModel::$error_param, '账户添加失败'); } //读取 public static function read(int $id = 0): Json { $res = AccountModel::field(true) ->where(['id' => $id, 'is_del' => CommonModel::$del_normal]) ->withAttr('video_ids', function ($val) { return explode(',', $val); }) ->findOrEmpty() ->toArray(); return $res ? json_show(CommonModel::$success, '获取账户详情成功', $res) : json_show(CommonModel::$error_param, '获取账户详情失败'); } //修改 public static function edit(array $data = []): Json { $date = date('Y-m-d H:i:s'); $data['video_ids'] = implode(',', $data['video_ids']); $res = AccountModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal]) ->save(array_merge($data, [ 'updaterid' => self::$uid, 'updater' => self::$uname, 'updatetime' => $date, ])); return $res ? json_show(CommonModel::$success, '账户修改成功') : json_show(CommonModel::$error_param, '账户修改失败'); } //批量添加账户 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' => $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' => $data['expiretime'], '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()); } } //删除 public static function delete(int $id = 0): Json { $rs = AccountModel::where(['id' => $id, 'is_del' => CommonModel::$del_normal]) ->where('status', '<>', AccountModel::$status_activated) ->save(['is_del' => CommonModel::$del_deleted, 'updatetime' => date('Y-m-d H:i:s')]); return $rs ? json_show(CommonModel::$success, '删除成功') : json_show(CommonModel::$error_param, '删除失败,该账户不存在或不允许删除'); } //添加账户列表 public static function batchLog(array $data = []): Json { $where = []; if ($data['company_title'] != '') $where[] = ['b.title', 'like', '%' . $data['company_title'] . '%']; if ($data['card_title'] != '') $where[] = ['c.title', 'like', '%' . $data['card_title'] . '%']; $count = Db::name('account_batch_log') ->alias('a') ->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) ->count('a.id'); $list = Db::name('account_batch_log') ->alias('a') ->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') ->order('a.id', 'desc') ->page($data['page'], $data['size']) ->select() ->toArray(); return json_show(CommonModel::$success, '获取批量添加账户列表成功', ['count' => $count, 'list' => $list]); } }