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'); $res = AccountModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal]) ->save([ 'username' => $data['username'], 'company_id' => $data['company_id'], 'card_id' => $data['card_id'], 'video_ids' => implode(',', $data['video_ids']), 'starttime' => $data['starttime'], 'expiretime' => $data['expiretime'], '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 { $usernames = []; $date = date('Y-m-d H:i:s'); for ($i = $data['suffix_start']; $i <= $data['suffix_end']; $i++) { $pwd = randomkeys(6); $salt = randomkeys(4); $usernames[] = [ 'username' => $data['username_prefix'] . 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, ]; } $rs = AccountModel::field('id,username') ->where('is_del', CommonModel::$del_normal) ->whereIn('username', array_column($usernames, 'username')) ->findOrEmpty(); if (!$rs->isEmpty()) return json_show(CommonModel::$error_param, $rs->username . '账号已存在'); $res = Db::name('account')->insertAll($usernames); return $res ? json_show(CommonModel::$success, $res . '个账户批量添加成功') : json_show(CommonModel::$error_param, '批量添加账户失败'); } //删除 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, '删除失败,该账户不存在或不允许删除'); } }