AccountLogic.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace app\admin\logic;
  3. use app\model\AccountModel;
  4. use app\model\CommonModel;
  5. use think\Exception;
  6. use think\facade\Db;
  7. use think\response\Json;
  8. class AccountLogic extends BaseLogic
  9. {
  10. //列表
  11. public static function list(array $data = []): Json
  12. {
  13. $db = AccountModel::alias('a')
  14. ->leftJoin('company b', 'b.id=a.company_id AND b.is_del=' . CommonModel::$del_normal)
  15. ->leftJoin('card c', 'c.id=a.card_id AND c.is_del=' . CommonModel::$del_normal)
  16. ->where('a.is_del', CommonModel::$del_normal);
  17. if ($data['username'] != '') $db->whereLike('a.username', '%' . $data['username'] . '%');
  18. if ($data['name'] != '') $db->whereLike('a.name', '%' . $data['name'] . '%');
  19. if ($data['mobile'] != '') $db->whereLike('a.mobile', '%' . $data['mobile'] . '%');
  20. $count = $db->count('a.id');
  21. $list = $db
  22. ->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')
  23. ->order('a.id', 'desc')
  24. ->page($data['page'], $data['size'])
  25. ->select()
  26. ->toArray();
  27. return json_show(CommonModel::$success, '获取账户列表成功', ['count' => $count, 'list' => $list]);
  28. }
  29. //添加
  30. public static function add(array $data = []): Json
  31. {
  32. $rs = AccountModel::field('id,username')
  33. ->where('is_del', CommonModel::$del_normal)
  34. ->where('username', $data['username'])
  35. ->findOrEmpty()
  36. ->isEmpty();
  37. if (!$rs) return json_show(CommonModel::$error_param, '该账号已存在');
  38. $pwd = randomkeys(6);
  39. $salt = randomkeys(4);
  40. $date = date('Y-m-d H:i:s');
  41. $res = AccountModel::create([
  42. 'username' => $data['username'],
  43. 'pwd' => $pwd,
  44. 'salt' => $salt,
  45. 'password' => getPassword($pwd, $salt),
  46. 'company_id' => $data['company_id'],
  47. 'card_id' => $data['card_id'],
  48. 'video_ids' => implode(',', $data['video_ids']),
  49. 'status' => AccountModel::$status_not_active,
  50. 'is_del' => CommonModel::$del_normal,
  51. 'starttime' => $data['starttime'],
  52. 'expiretime' => $data['expiretime'],
  53. 'createrid' => self::$uid,
  54. 'creater' => self::$uname,
  55. 'addtime' => $date,
  56. 'updaterid' => self::$uid,
  57. 'updater' => self::$uname,
  58. 'updatetime' => $date,
  59. ])->save();
  60. return $res ? json_show(CommonModel::$success, '账户添加成功') : json_show(CommonModel::$error_param, '账户添加失败');
  61. }
  62. //读取
  63. public static function read(int $id = 0): Json
  64. {
  65. $res = AccountModel::field(true)
  66. ->where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  67. ->withAttr('video_ids', function ($val) {
  68. return explode(',', $val);
  69. })
  70. ->findOrEmpty()
  71. ->toArray();
  72. return $res ? json_show(CommonModel::$success, '获取账户详情成功', $res) : json_show(CommonModel::$error_param, '获取账户详情失败');
  73. }
  74. //修改
  75. public static function edit(array $data = []): Json
  76. {
  77. $date = date('Y-m-d H:i:s');
  78. $data['video_ids'] = implode(',', $data['video_ids']);
  79. $res = AccountModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  80. ->save(array_merge($data, [
  81. 'updaterid' => self::$uid,
  82. 'updater' => self::$uname,
  83. 'updatetime' => $date,
  84. ]));
  85. return $res ? json_show(CommonModel::$success, '账户修改成功') : json_show(CommonModel::$error_param, '账户修改失败');
  86. }
  87. //批量添加账户
  88. public static function batchAdd(array $data = []): Json
  89. {
  90. Db::startTrans();
  91. try {
  92. $rs = AccountModel::field('id,username')
  93. ->where('is_del', CommonModel::$del_normal)
  94. ->whereLike('username', $data['username_prefix'] . $data['username_year'] . '____')
  95. ->findOrEmpty();
  96. if (!$rs->isEmpty()) throw new Exception($rs->username . '账号已存在');
  97. $date = date('Y-m-d H:i:s');
  98. for ($i = 0; $i <= 9999; $i++) {
  99. $pwd = randomkeys(6);
  100. $salt = randomkeys(4);
  101. Db::name('account')->insert([
  102. 'username' => $data['username_prefix'] . $data['username_year'] . str_pad($i, 4, '0', STR_PAD_LEFT),
  103. 'pwd' => $pwd,
  104. 'salt' => $salt,
  105. 'password' => getPassword($pwd, $salt),
  106. 'company_id' => $data['company_id'],
  107. 'card_id' => $data['card_id'],
  108. 'status' => AccountModel::$status_not_active,
  109. 'is_del' => CommonModel::$del_normal,
  110. 'starttime' => $data['starttime'],
  111. 'expiretime' => $data['expiretime'],
  112. 'createrid' => self::$uid,
  113. 'creater' => self::$uname,
  114. 'addtime' => $date,
  115. 'updaterid' => self::$uid,
  116. 'updater' => self::$uname,
  117. 'updatetime' => $date,
  118. ]);//比insertAll方法快2/3
  119. }
  120. Db::name('account_batch_log')->insert($data);
  121. Db::commit();
  122. return json_show(CommonModel::$success, '10000个账户批量添加成功');
  123. } catch (Exception $exception) {
  124. Db::rollback();
  125. return json_show(CommonModel::$error_param, '批量添加账户失败,' . $exception->getMessage());
  126. }
  127. }
  128. //删除
  129. public static function delete(int $id = 0): Json
  130. {
  131. $rs = AccountModel::where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  132. ->where('status', '<>', AccountModel::$status_activated)
  133. ->save(['is_del' => CommonModel::$del_deleted, 'updatetime' => date('Y-m-d H:i:s')]);
  134. return $rs ? json_show(CommonModel::$success, '删除成功') : json_show(CommonModel::$error_param, '删除失败,该账户不存在或不允许删除');
  135. }
  136. //列表
  137. public static function batchLog(array $data = []): Json
  138. {
  139. $where = [];
  140. if ($data['company_id'] != '') $where[] = ['company_id', '=', $data['company_id']];
  141. if ($data['card_id'] != '') $where[] = ['card_id', '=', $data['card_id']];
  142. $count = Db::name('account_batch_log')
  143. ->where($where)
  144. ->count('id');
  145. $list = Db::name('account_batch_log')
  146. ->where($where)
  147. ->field(true)
  148. ->order('id', 'desc')
  149. ->page($data['page'], $data['size'])
  150. ->select()
  151. ->toArray();
  152. return json_show(CommonModel::$success, '获取批量添加账户列表成功', ['count' => $count, 'list' => $list]);
  153. }
  154. }