SystemStoreRecordServices.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system\store;
  12. use app\dao\system\store\SystemStoreRecordDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use app\services\system\store\SystemStoreServices;
  16. use app\services\system\store\SystemStoreStaffServices;
  17. /**
  18. * 门店申请记录服务
  19. * Class SystemStoreRecordServices
  20. * @package app\services\system\store
  21. */
  22. class SystemStoreRecordServices extends BaseServices
  23. {
  24. /**
  25. * 构造方法
  26. * SystemStoreRecordServices constructor.
  27. * @param SystemStoreRecordDao $dao
  28. */
  29. public function __construct(SystemStoreRecordDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * 获取门店申请列表
  35. * @param array $where
  36. * @return array
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function getApplyList(array $where)
  42. {
  43. $searchWhere = [];
  44. if (isset($where['keywords']) && $where['keywords'] !== '') {
  45. $searchWhere[] = ['name|phone', 'like', '%' . $where['keywords'] . '%'];
  46. }
  47. if (isset($where['status']) && $where['status'] !== '') {
  48. $searchWhere['status'] = $where['status'];
  49. }
  50. [$page, $limit] = $this->getPageValue();
  51. $list = $this->dao->getApplyList($searchWhere, $page, $limit);
  52. $count = $this->dao->count($searchWhere);
  53. return compact('list', 'count');
  54. }
  55. /**
  56. * 通过门店申请
  57. * @param int $id
  58. * @return bool
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public function approve(int $id)
  64. {
  65. $recordInfo = $this->dao->getApplyInfo($id);
  66. if (!$recordInfo) {
  67. throw new AdminException('申请记录不存在');
  68. }
  69. if ($recordInfo['status'] != 0) {
  70. throw new AdminException('该申请已审核');
  71. }
  72. return $this->transaction(function () use ($recordInfo, $id) {
  73. // 更新申请状态为通过
  74. $res = $this->dao->update($id, ['status' => 1]);
  75. if (!$res) {
  76. throw new AdminException('审核失败');
  77. }
  78. // 创建正式门店记录
  79. $storeServices = app()->make(SystemStoreServices::class);
  80. $storeData = [
  81. 'name' => $recordInfo['name'],
  82. 'phone' => $recordInfo['phone'],
  83. 'address' => $recordInfo['address'],
  84. 'detailed_address' => $recordInfo['detailed_address'],
  85. 'latlng' => $recordInfo['latlng'],
  86. 'latitude' => $recordInfo['latitude'],
  87. 'longitude' => $recordInfo['longitude'],
  88. 'day_time' => $recordInfo['day_time'],
  89. 'valid_time' => $recordInfo['valid_time'],
  90. 'image' => $recordInfo['image'],
  91. 'uid' => $recordInfo['uid'],
  92. 'is_show' => 1,
  93. 'add_time' => time(),
  94. ];
  95. $storeId = $storeServices->save($storeData);
  96. if (!$storeId) {
  97. throw new AdminException('创建门店失败');
  98. }
  99. // 添加用户为店员
  100. $staffServices = app()->make(SystemStoreStaffServices::class);
  101. $userServices = app()->make(\app\services\user\UserServices::class);
  102. // 检查该用户是否已经是店员
  103. if (!$staffServices->count(['uid' => $recordInfo['uid']])) {
  104. // 从user表获取店员的image和staff_name
  105. $userInfo = $userServices->getUserInfo($recordInfo['uid']);
  106. $staffData = [
  107. 'uid' => $recordInfo['uid'],
  108. 'store_id' => $storeId,
  109. 'staff_name' => $userInfo['nickname'] ?? '',
  110. 'avatar' => $userInfo['avatar'] ?? ($userInfo['image'] ?? ''),
  111. 'phone' => $recordInfo['phone'],
  112. 'verify_status' => 1,
  113. 'status' => 1,
  114. 'add_time' => time(),
  115. ];
  116. $staffServices->save($staffData);
  117. }
  118. return true;
  119. });
  120. }
  121. /**
  122. * 拒绝门店申请
  123. * @param int $id
  124. * @return bool
  125. * @throws \think\db\exception\DataNotFoundException
  126. * @throws \think\db\exception\DbException
  127. * @throws \think\db\exception\ModelNotFoundException
  128. */
  129. public function reject(int $id)
  130. {
  131. $recordInfo = $this->dao->getApplyInfo($id);
  132. if (!$recordInfo) {
  133. throw new AdminException('申请记录不存在');
  134. }
  135. if ($recordInfo['status'] != 0) {
  136. throw new AdminException('该申请已审核');
  137. }
  138. // 更新申请状态为拒绝
  139. $res = $this->dao->update($id, ['status' => 2]);
  140. if (!$res) {
  141. throw new AdminException('审核失败');
  142. }
  143. return true;
  144. }
  145. }