AccountLogic.php 7.7 KB

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