123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- namespace app\admin\logic;
- use app\model\AccountBatchLogModel;
- use app\model\AccountModel;
- use app\model\CommonModel;
- use app\model\VideoModel;
- use think\Exception;
- use think\exception\ValidateException;
- use think\facade\Config;
- 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.starttime,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
- {
- $username_prefix = env('account_username_prefix');
- $rs = AccountModel::field('id,username')
- ->where('is_del', CommonModel::$del_normal)
- ->where('username', $username_prefix . $data['username'])
- ->findOrEmpty()
- ->isEmpty();
- if (!$rs) return json_show(CommonModel::$error_param, '该账号已存在');
- $pwd = randomkeys(6);
- $salt = randomkeys(6);
- $date = date('Y-m-d H:i:s');
- $res = AccountModel::create([
- 'username' => $username_prefix . $data['username'],
- 'pwd' => $pwd,
- 'salt' => $salt,
- 'password' => get_password($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' => date('Y-m-d 23:59:59', strtotime($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::alias('a')
- ->field('a.*,b.title company_title,c.title card_title')
- ->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.id' => $id, 'a.is_del' => CommonModel::$del_normal])
- ->append(['video_list'])
- ->withAttr('video_list', function ($val, $data) {
- return VideoModel::field('id,video_name')
- ->whereIn('id', $data['video_ids'])
- ->where('is_del', CommonModel::$del_normal)
- ->select()
- ->toArray();
- })
- ->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
- {
- $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, '批量添加账户失败');
- }
- //删除
- 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'] . '%'];
- if ($data['status'] != '') $where[] = ['a.status', '=', $data['status']];
- $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.status,a.reason,a.addtime')
- ->order('a.id', 'desc')
- ->page($data['page'], $data['size'])
- ->select()
- ->toArray();
- return json_show(CommonModel::$success, '获取批量添加账户列表成功', ['count' => $count, 'list' => $list]);
- }
- //修改密码
- public static function changePasswod(array $data = []): Json
- {
- $rs = AccountModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
- ->field('id')
- ->findOrEmpty()
- ->isEmpty();
- if ($rs) throw new ValidateException('该账户不存在');
- $salt = randomkeys(6);
- $password = get_password($data['new_password'], $salt);
- $da = [
- 'pwd' => $data['new_password'],
- 'salt' => $salt,
- 'password' => $password,
- 'updaterid' => self::$uid,
- 'updater' => self::$uname,
- 'updatetime' => date('Y-m-d H:i:s'),
- ];
- $rs = AccountModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
- ->save($da);
- return $rs ? json_show(CommonModel::$success, '更改密码成功') : json_show(CommonModel::$error_param, '更改密码失败');
- }
- }
|