123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- namespace app\admin\logic;
- use app\model\AccountModel;
- use app\model\CommonModel;
- use think\Exception;
- use think\facade\Db;
- use think\response\Json;
- class AccountLogic extends BaseLogic
- {
- //列表
- public static function list(array $data = []): Json
- {
- $db = AccountModel::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('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($data);
- 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_id'] != '') $where[] = ['company_id', '=', $data['company_id']];
- if ($data['card_id'] != '') $where[] = ['card_id', '=', $data['card_id']];
- $count = Db::name('account_batch_log')
- ->where($where)
- ->count('id');
- $list = Db::name('account_batch_log')
- ->where($where)
- ->field(true)
- ->order('id', 'desc')
- ->page($data['page'], $data['size'])
- ->select()
- ->toArray();
- return json_show(CommonModel::$success, '获取批量添加账户列表成功', ['count' => $count, 'list' => $list]);
- }
- }
|