AccountLogic.php 6.1 KB

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