StoreService.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\GoodsModel;
  13. use App\Models\StoreModel;
  14. use App\Models\UserModel;
  15. use App\Models\MemberModel;
  16. use App\Services\BaseService;
  17. use App\Services\RedisService;
  18. use Illuminate\Support\Facades\DB;
  19. /**
  20. * 商家店铺管理-服务类
  21. * @author laravel开发员
  22. * @since 2020/11/11
  23. * @package App\Services\Common
  24. */
  25. class StoreService extends BaseService
  26. {
  27. /**
  28. * 构造函数
  29. * @author laravel开发员
  30. * @since 2020/11/11
  31. * AdService constructor.
  32. */
  33. public function __construct()
  34. {
  35. $this->model = new StoreModel();
  36. }
  37. /**
  38. * 列表
  39. * @param $params
  40. * @param int $pageSize
  41. * @return array
  42. */
  43. public function getDataList($params, $pageSize = 15)
  44. {
  45. // 分页查询
  46. $list = $this->model
  47. ->where('mark', 1)
  48. ->where(function ($query) use ($params) {
  49. // 商家ID筛选
  50. if (isset($params['store_id']) && $params['store_id'] > 0) {
  51. $query->where('id', $params['store_id']);
  52. }
  53. // 状态筛选
  54. if (isset($params['status']) && $params['status'] > 0) {
  55. $query->where('status', $params['status']);
  56. }
  57. // 分类筛选
  58. if (isset($params['category_id']) && $params['category_id'] > 0) {
  59. $query->where('category_id', $params['category_id']);
  60. }
  61. // 用户状态筛选
  62. if (isset($params['user_status']) && $params['user_status'] > 0) {
  63. $query->whereHas('member', function ($q) use ($params) {
  64. $q->where('status', $params['user_status'])->where('mark', 1);
  65. });
  66. }
  67. // 管理员状态筛选
  68. if (isset($params['admin_status']) && $params['admin_status'] > 0) {
  69. $query->whereHas('user', function ($q) use ($params) {
  70. $q->where('status', $params['admin_status'])->where('mark', 1);
  71. });
  72. }
  73. // 店铺名称搜索
  74. if (isset($params['name']) && $params['name']) {
  75. $query->where(function ($q) use ($params) {
  76. $q->where('name', 'like', "%{$params['name']}%")
  77. ->orWhere('phone', 'like', "%{$params['name']}%");
  78. });
  79. }
  80. // 联系电话搜索
  81. if (isset($params['phone']) && $params['phone']) {
  82. $query->where('phone', 'like', "%{$params['phone']}%");
  83. }
  84. // 姓名搜索
  85. if (isset($params['real_name']) && $params['real_name']) {
  86. $query->where('real_name', 'like', "%{$params['real_name']}%");
  87. }
  88. })
  89. ->with(['user', 'member', 'category'])
  90. ->orderBy('create_time', 'desc')
  91. ->orderBy('id', 'desc')
  92. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  93. $list = $list->toArray();
  94. // 格式化数据
  95. if (isset($list['data']) && !empty($list['data'])) {
  96. foreach ($list['data'] as &$item) {
  97. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  98. $item['status_text'] = $this->getStatusText($item['status']);
  99. $item['user_status'] = isset($item['user']['status']) ? $item['user']['status'] : 1;
  100. $item['member_status'] = isset($item['member']['status']) ? $item['member']['status'] : 1;
  101. $item['category_name'] = isset($item['category']['name']) ? $item['category']['name'] : '未分类';
  102. // 处理门头照图片
  103. if (isset($item['logo']) && $item['logo']) {
  104. $item['logo'] = get_image_url($item['logo']);
  105. }
  106. // 添加余额信息
  107. $item['balance'] = isset($item['member']['balance']) ? $item['member']['balance'] : 0;
  108. // 添加会员状态信息
  109. $item['member_user_status'] = isset($item['member']['status']) ? $item['member']['status'] : 1;
  110. }
  111. }
  112. return [
  113. 'msg' => '操作成功',
  114. 'code' => 0,
  115. 'data' => $list['data'],
  116. 'count' => $list['total']
  117. ];
  118. }
  119. /**
  120. * 获取状态文本
  121. * @param int $status
  122. * @return string
  123. */
  124. private function getStatusText($status)
  125. {
  126. $statusMap = [
  127. 1 => '已审核(营业中)',
  128. 2 => '待审核',
  129. 3 => '审核失败'
  130. ];
  131. return isset($statusMap[$status]) ? $statusMap[$status] : '未知';
  132. }
  133. /**
  134. * 查询
  135. * @param $params
  136. * @return \Illuminate\Database\Eloquent\Builder
  137. */
  138. public function getQuery($params)
  139. {
  140. $where = ['mark' => 1];
  141. $status = isset($params['status']) ? $params['status'] : 0;
  142. if ($status > 0) {
  143. $where['status'] = $status;
  144. }
  145. // 分类筛选
  146. $categoryId = isset($params['category_id']) ? intval($params['category_id']) : 0;
  147. if ($categoryId > 0) {
  148. $where['category_id'] = $categoryId;
  149. }
  150. // 用户状态筛选(member表)
  151. $userStatus = isset($params['user_status']) ? intval($params['user_status']) : 0;
  152. // 管理员状态筛选(user表)
  153. $adminStatus = isset($params['admin_status']) ? intval($params['admin_status']) : 0;
  154. $model = $this->model
  155. ->with(['user', 'member', 'category'])
  156. ->select('stores.*')
  157. ->where($where);
  158. // 如果筛选用户状态,使用 whereHas 关联 member 表
  159. if ($userStatus > 0) {
  160. $model->whereHas('member', function ($query) use ($userStatus) {
  161. $query->where('status', $userStatus)->where('mark', 1);
  162. });
  163. }
  164. // 如果筛选管理员状态,使用 whereHas 关联 user 表
  165. if ($adminStatus > 0) {
  166. $model->whereHas('user', function ($query) use ($adminStatus) {
  167. $query->where('status', $adminStatus)->where('mark', 1);
  168. });
  169. }
  170. // 搜索条件
  171. $model->where(function ($query) use ($params) {
  172. // 店铺名称搜索
  173. $name = isset($params['name']) ? trim($params['name']) : '';
  174. if ($name) {
  175. $query->where('name', 'like', "%{$name}%");
  176. }
  177. // 联系电话搜索
  178. $phone = isset($params['phone']) ? trim($params['phone']) : '';
  179. if ($phone) {
  180. $query->where('phone', 'like', "%{$phone}%");
  181. }
  182. // 姓名搜索
  183. $realName = isset($params['real_name']) ? trim($params['real_name']) : '';
  184. if ($realName) {
  185. $query->where('real_name', 'like', "%{$realName}%");
  186. }
  187. });
  188. return $model;
  189. }
  190. /**
  191. * 添加或编辑
  192. * @return array
  193. * @since 2020/11/11
  194. * @author laravel开发员
  195. */
  196. public function edit()
  197. {
  198. $data = request()->all();
  199. // 图片处理
  200. if (isset($data['logo'])) {
  201. $data['logo'] = get_image_path($data['logo']);
  202. }
  203. if (empty($data['name'])) {
  204. return message('请填写店铺名称', false);
  205. }
  206. return parent::edit($data);
  207. }
  208. /**
  209. * 审核商家
  210. * @param int $userId 操作人ID
  211. * @param array $params 审核参数
  212. * @return array
  213. */
  214. public function confirm($userId, $params)
  215. {
  216. $id = isset($params['id']) ? intval($params['id']) : 0;
  217. $status = isset($params['status']) ? intval($params['status']) : 0;
  218. $remark = isset($params['remark']) ? trim($params['remark']) : '';
  219. if (!$id) {
  220. $this->error = '商家ID不能为空';
  221. return false;
  222. }
  223. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  224. if (!$info) {
  225. $this->error = '商家信息不存在';
  226. return false;
  227. }
  228. // 审核通过
  229. if ($status == 1) {
  230. // 生成随机密码(8位数字字母组合)
  231. $password = $this->generatePassword();
  232. // 更新商家状态为已审核
  233. $updateData = [
  234. 'status' => 1,
  235. 'confirm_remark' => $remark ?: '审核通过',
  236. 'update_time' => time()
  237. ];
  238. // 如果商家关联了用户,更新用户密码
  239. if ($info->user_id > 0) {
  240. $userModel = new \App\Models\UserModel();
  241. $userModel->where('id', $info->user_id)->update([
  242. 'password' => md5($password),
  243. 'update_time' => time()
  244. ]);
  245. }
  246. $this->model->where('id', $id)->update($updateData);
  247. $this->error = '审核通过,商家密码:' . $password;
  248. return true;
  249. }
  250. // 审核驳回
  251. elseif ($status == 3) {
  252. if (empty($remark)) {
  253. $this->error = '驳回理由不能为空';
  254. return false;
  255. }
  256. $this->model->where('id', $id)->update([
  257. 'status' => 3,
  258. 'confirm_remark' => $remark,
  259. 'update_time' => time()
  260. ]);
  261. $this->error = '审核驳回成功';
  262. return true;
  263. } else {
  264. $this->error = '审核状态参数错误';
  265. return false;
  266. }
  267. }
  268. /**
  269. * 生成随机密码
  270. * @param int $length 密码长度
  271. * @return string
  272. */
  273. private function generatePassword($length = 8)
  274. {
  275. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  276. $password = '';
  277. for ($i = 0; $i < $length; $i++) {
  278. $password .= $chars[mt_rand(0, strlen($chars) - 1)];
  279. }
  280. return $password;
  281. }
  282. /**
  283. * 获取当前登录商家信息
  284. * @return array
  285. */
  286. public function getCurrentStoreInfo($storeId)
  287. {
  288. try {
  289. // 查询商家信息
  290. $store = StoreModel::where('id', $storeId)
  291. ->where('mark', 1)
  292. ->with(['member'])
  293. ->first();
  294. if (!$store) {
  295. return ['code' => 1, 'msg' => '商家信息不存在'];
  296. }
  297. // 检查商家状态
  298. if ($store->status !== 1) {
  299. return ['code' => 1, 'msg' => '商家已被禁用'];
  300. }
  301. // 获取会员信息
  302. $member = MemberModel::find($store->user_id);
  303. if (!$member) {
  304. return ['code' => 1, 'msg' => '商家关联的会员信息不存在'];
  305. }
  306. $data = [
  307. 'user_id' => $store->user_id,
  308. 'name' => $store->name,
  309. 'real_name' => $store->real_name,
  310. 'phone' => $store->phone,
  311. 'balance' => $member->balance,
  312. 'status' => $store->status
  313. ];
  314. return ['code' => 0, 'msg' => '获取成功', 'data' => $data];
  315. } catch (\Exception $e) {
  316. return ['code' => 1, 'msg' => '获取失败:' . $e->getMessage()];
  317. }
  318. }
  319. /**
  320. * 获取商家详情(重写父类方法)
  321. * @return array
  322. */
  323. public function info()
  324. {
  325. // 记录ID
  326. $id = request()->input("id", 0);
  327. $info = [];
  328. if ($id) {
  329. // 获取商家基本信息(使用关联查询)
  330. $store = $this->model->with(['user', 'member', 'category'])
  331. ->where(['id' => $id, 'mark' => 1])
  332. ->first();
  333. if ($store) {
  334. $info = $store->toArray();
  335. // 处理图片
  336. if (isset($info['logo'])) {
  337. $info['logo'] = get_image_url($info['logo']);
  338. }
  339. if (isset($info['business_license'])) {
  340. $info['business_license'] = get_image_url($info['business_license']);
  341. }
  342. if (isset($info['other_photo'])) {
  343. $info['other_photo'] = get_image_url($info['other_photo']);
  344. }
  345. // 处理时间
  346. if (isset($info['create_time'])) {
  347. $info['create_time'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
  348. }
  349. if (isset($info['update_time'])) {
  350. $info['update_time'] = $info['update_time'] ? datetime($info['update_time'], 'Y-m-d H:i:s') : '';
  351. }
  352. // 获取分类信息
  353. if (isset($info['category']) && !empty($info['category'])) {
  354. $info['category_name'] = $info['category']['name'] ?? '未分类';
  355. $info['category_status'] = $info['category']['status'] ?? 1;
  356. } else {
  357. $info['category_name'] = '未分类';
  358. $info['category_status'] = 1;
  359. }
  360. // 获取用户信息(user表)
  361. if (isset($info['user']) && !empty($info['user'])) {
  362. $info['user_realname'] = $info['user']['realname'] ?? '';
  363. $info['user_nickname'] = $info['user']['nickname'] ?? '';
  364. $info['user_status'] = $info['user']['status'] ?? 1;
  365. } else {
  366. $info['user_realname'] = '';
  367. $info['user_nickname'] = '';
  368. $info['user_status'] = 1;
  369. }
  370. // 获取会员信息(member表)
  371. if (isset($info['member']) && !empty($info['member'])) {
  372. $info['member_realname'] = $info['member']['realname'] ?? '';
  373. $info['member_nickname'] = $info['member']['nickname'] ?? '';
  374. $info['member_mobile'] = $info['member']['mobile'] ?? '';
  375. $info['member_status'] = $info['member']['status'] ?? 1;
  376. } else {
  377. $info['member_realname'] = '';
  378. $info['member_nickname'] = '';
  379. $info['member_mobile'] = '';
  380. $info['member_status'] = 1;
  381. }
  382. // 添加状态文本
  383. $info['status_text'] = $this->getStatusText($info['status'] ?? 0);
  384. }
  385. }
  386. return message(MESSAGE_OK, true, $info);
  387. }
  388. /**
  389. * 设置状态(禁用/启用)
  390. * @param array $params
  391. * @return array
  392. */
  393. public function setStatus($params)
  394. {
  395. $id = isset($params['id']) ? intval($params['id']) : 0;
  396. $status = isset($params['status']) ? intval($params['status']) : 0;
  397. if (!$id) {
  398. $this->error = '商家ID不能为空';
  399. return false;
  400. }
  401. // status: 1-启用, 0-禁用
  402. // user表: status 1-正常 2-禁用
  403. // member表: status 1-正常 2-冻结(不可登录)
  404. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  405. if (!$info) {
  406. $this->error = '商家信息不存在';
  407. return false;
  408. }
  409. // 如果商家关联了用户,同时禁用/启用用户和会员
  410. if ($info->user_id > 0) {
  411. // 转换状态:前端传入 1-启用 0-禁用
  412. // user表需要:1-正常 2-禁用
  413. // member表需要:1-正常 2-冻结
  414. $userStatus = $status == 1 ? 1 : 2;
  415. $memberStatus = $status == 1 ? 1 : 2;
  416. // 使用事务确保数据一致性
  417. DB::beginTransaction();
  418. try {
  419. // 更新 user 表状态(stores.user_id = user.id)
  420. UserModel::where('id', $info->user_id)->update([
  421. 'status' => $userStatus,
  422. 'update_time' => time()
  423. ]);
  424. // 更新 member 表状态(user.id = member.id)
  425. MemberModel::where('id', $info->user_id)->update([
  426. 'status' => $memberStatus,
  427. 'update_time' => time()
  428. ]);
  429. DB::commit();
  430. } catch (\Exception $e) {
  431. DB::rollBack();
  432. $this->error = '操作失败:' . $e->getMessage();
  433. return false;
  434. }
  435. }
  436. $this->error = $status == 1 ? '启用成功' : '禁用成功';
  437. return true;
  438. }
  439. /**
  440. * 获取店铺选项列表(用于下拉选择)
  441. * @return array
  442. */
  443. public function options()
  444. {
  445. $list = $this->model
  446. ->where('mark', 1)
  447. ->where('status', 1) // 只返回启用状态的店铺
  448. ->orderBy('sort', 'desc')
  449. ->orderBy('id', 'asc')
  450. ->select(['id', 'name', 'user_id'])
  451. ->get()
  452. ->toArray();
  453. return $list;
  454. }
  455. }