AccountLogic.php 7.7 KB

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