StoreService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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\StoreModel;
  13. use App\Models\UserModel;
  14. use App\Models\UserRoleModel;
  15. use App\Services\BaseService;
  16. use Illuminate\Support\Facades\DB;
  17. /**
  18. * 商户店铺管理-服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * @package App\Services\Common
  22. */
  23. class StoreService extends BaseService
  24. {
  25. /**
  26. * 构造函数
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. * AdService constructor.
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new StoreModel();
  34. }
  35. /**
  36. * 列表
  37. * @param $params
  38. * @param int $pageSize
  39. * @return array
  40. */
  41. public function getDataList($params, $pageSize = 15)
  42. {
  43. // 分页查询
  44. $list = $this->model
  45. ->with(['member'])
  46. ->where(['type'=>2,'mark'=>1])
  47. ->where(function ($query) use ($params) {
  48. // 状态筛选
  49. if (isset($params['status']) && $params['status'] > 0) {
  50. $query->where('status', $params['status']);
  51. }
  52. // 名称搜索
  53. if (isset($params['name']) && $params['name']) {
  54. $query->where('name', 'like', "%{$params['name']}%");
  55. }
  56. // 联系电话搜索
  57. if (isset($params['mobile']) && $params['mobile']) {
  58. $query->where('mobile', 'like', "%{$params['mobile']}%");
  59. }
  60. // 姓名搜索
  61. if (isset($params['real_name']) && $params['real_name']) {
  62. $query->where('real_name', 'like', "%{$params['real_name']}%");
  63. }
  64. })
  65. ->orderBy('create_time', 'desc')
  66. ->orderBy('id', 'desc')
  67. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  68. $list = $list->toArray();
  69. // 格式化数据
  70. if (isset($list['data']) && !empty($list['data'])) {
  71. foreach ($list['data'] as &$item) {
  72. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  73. $item['status_text'] = $this->getStatusText($item['status']);
  74. // 添加余额信息
  75. $item['balance'] = isset($item['member']['balance']) ? $item['member']['balance'] : 0;
  76. $item['password'] = '';
  77. }
  78. }
  79. return [
  80. 'msg' => '操作成功',
  81. 'code' => 0,
  82. 'data' => $list['data'],
  83. 'count' => $list['total']
  84. ];
  85. }
  86. /**
  87. * 获取状态文本
  88. * @param int $status
  89. * @return string
  90. */
  91. private function getStatusText($status)
  92. {
  93. $statusMap = [
  94. 1 => '已审核(营业中)',
  95. 2 => '待审核',
  96. 3 => '审核失败'
  97. ];
  98. return isset($statusMap[$status]) ? $statusMap[$status] : '未知';
  99. }
  100. /**
  101. * 查询
  102. * @param $params
  103. * @return \Illuminate\Database\Eloquent\Builder
  104. */
  105. public function getQuery($params)
  106. {
  107. $where = ['mark' => 1];
  108. $status = isset($params['status']) ? $params['status'] : 0;
  109. if ($status > 0) {
  110. $where['status'] = $status;
  111. }
  112. $model = $this->model
  113. ->select('user.*')
  114. ->where($where);
  115. // 搜索条件
  116. $model->where(function ($query) use ($params) {
  117. // 店铺名称搜索
  118. $name = isset($params['name']) ? trim($params['name']) : '';
  119. if ($name) {
  120. $query->where('name', 'like', "%{$name}%");
  121. }
  122. // 联系电话搜索
  123. $phone = isset($params['phone']) ? trim($params['phone']) : '';
  124. if ($phone) {
  125. $query->where('mobile', 'like', "%{$phone}%");
  126. }
  127. // 姓名搜索
  128. $realName = isset($params['real_name']) ? trim($params['real_name']) : '';
  129. if ($realName) {
  130. $query->where('real_name', 'like', "%{$realName}%");
  131. }
  132. });
  133. return $model;
  134. }
  135. /**
  136. * 添加或编辑
  137. * @return array
  138. * @since 2020/11/11
  139. * @author laravel开发员
  140. */
  141. public function edit()
  142. {
  143. $data = request()->all();
  144. // 图片处理
  145. if (isset($data['logo'])) {
  146. $data['logo'] = get_image_path($data['logo']);
  147. }
  148. if (isset($data['business_license'])) {
  149. $data['business_license'] = get_image_path($data['business_license']);
  150. }
  151. if (isset($data['driver_license'])) {
  152. $data['driver_license'] = get_image_path($data['driver_license']);
  153. }
  154. if (empty($data['position'])) {
  155. return message('请填写负责人职位', false);
  156. }
  157. $data['name'] = $data['nickname'];
  158. if (empty($data['name'])) {
  159. return message('请填写商户名称', false);
  160. }
  161. if (empty($data['legal_idcard'])) {
  162. return message('请填写商户法人身份证', false);
  163. }
  164. if (empty($data['credit_code'])) {
  165. return message('请填写商户社会统一信用代码', false);
  166. }
  167. if (empty($data['settle_bank_name'])) {
  168. return message('请填写商户结算开户行', false);
  169. }
  170. $mobile = isset($data['mobile'])?$data['mobile']:'';
  171. if (empty($mobile)) {
  172. return message('请填写商户账号(联系电话)', false);
  173. }
  174. if (empty($data['real_name'])) {
  175. return message('请填写商户负责人姓名', false);
  176. }
  177. // 手机号唯一性验证
  178. $type = isset($data['type']) && $data['type']? $data['type'] : 1;
  179. $id = isset($data['id']) ? $data['id'] : 0;
  180. if ($mobile) {
  181. $checkId = $this->model->where(['mobile' => $mobile,'type'=>$type, 'mark' => 1])->value('id');
  182. if ($checkId && ($id != $checkId)) {
  183. return message('该商户已存在', false);
  184. }
  185. }
  186. var_dump($data);
  187. DB::beginTransaction();
  188. $result = parent::edit($data);
  189. $success = isset($result['success'])? $result['success']:false;
  190. $id = isset($result['data']['id'])? $result['data']['id']:0;
  191. if($success){
  192. $account = [
  193. 'user_id'=>$data['user_id'],
  194. 'store_id'=> $id,
  195. 'username'=> $mobile,
  196. 'avatar'=> $data['logo'],
  197. 'nickname'=> $data['name'],
  198. ];
  199. // 密码处理
  200. if (isset($data['password']) && $data['password']) {
  201. $account['password'] = get_password(trim($data['password']). $mobile);
  202. } else {
  203. // 编辑时如果密码为空则不更新密码
  204. unset($account['password']);
  205. }
  206. $adminId = UserModel::where(['username' => $mobile, 'mark' => 1])->value('id');
  207. // 删除已存在的用户角色关系数据
  208. $userRoleService = new UserRoleService();
  209. $userRole = $adminId?UserRoleModel::where(['user_id'=>$adminId,'role_id'=>6])->first():[];
  210. if ($adminId) {
  211. $account['update_time']=time();
  212. UserModel::where(['id'=>$adminId])->update($account);
  213. }else{
  214. $account['create_time']=time();
  215. if(!$adminId = UserModel::insertGetId($account)){
  216. DB::rollBack();
  217. return message('创建商户管理账户失败', false);
  218. }
  219. }
  220. $userRoleService->deleteUserRole($adminId);
  221. // 插入用户角色关系数据
  222. $userRoleService->insertUserRole($adminId, [6]);
  223. }
  224. DB::commit();
  225. return message('操作成功',true);
  226. }
  227. /**
  228. * 获取当前登录企业信息
  229. * @return array
  230. */
  231. public function getCurrentStoreInfo($storeId)
  232. {
  233. try {
  234. // 查询企业信息
  235. $store = StoreModel::where('id', $storeId)
  236. ->where('mark', 1)
  237. ->with(['member'])
  238. ->first();
  239. if (!$store) {
  240. return ['code' => 1, 'msg' => '企业信息不存在'];
  241. }
  242. // 检查企业状态
  243. if ($store->status !== 1) {
  244. return ['code' => 1, 'msg' => '企业已被禁用'];
  245. }
  246. $data = [
  247. 'user_id' => $store->user_id,
  248. 'name' => $store->nickname,
  249. 'real_name' => $store->realname,
  250. 'phone' => $store->mobile,
  251. 'status' => $store->status
  252. ];
  253. return ['code' => 0, 'msg' => '获取成功', 'data' => $data];
  254. } catch (\Exception $e) {
  255. return ['code' => 1, 'msg' => '获取失败:' . $e->getMessage()];
  256. }
  257. }
  258. /**
  259. * 获取企业详情(重写父类方法)
  260. * @return array
  261. */
  262. public function info()
  263. {
  264. // 记录ID
  265. $id = request()->input("id", 0);
  266. $info = [];
  267. if ($id) {
  268. // 获取企业基本信息(使用关联查询)
  269. $store = $this->model->where(['id' => $id, 'mark' => 1])
  270. ->first();
  271. if ($store) {
  272. $info = $store->toArray();
  273. // 处理图片
  274. if (isset($info['avatar'])) {
  275. $info['avatar'] = get_image_url($info['avatar']);
  276. }
  277. if (isset($info['business_license'])) {
  278. $info['business_license'] = get_image_url($info['business_license']);
  279. }
  280. // 处理时间
  281. if (isset($info['create_time'])) {
  282. $info['create_time'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
  283. }
  284. if (isset($info['update_time'])) {
  285. $info['update_time'] = $info['update_time'] ? datetime($info['update_time'], 'Y-m-d H:i:s') : '';
  286. }
  287. // 添加状态文本
  288. $info['status_text'] = $this->getStatusText($info['status'] ?? 0);
  289. }
  290. }
  291. return message(MESSAGE_OK, true, $info);
  292. }
  293. /**
  294. * 获取店铺选项列表(用于下拉选择)
  295. * @return array
  296. */
  297. public function options()
  298. {
  299. $param = request()->all();
  300. // 用户ID
  301. $keyword = getter($param, "keyword");
  302. $list = $this->model
  303. ->where(['status'=>1,'mark'=>1])
  304. ->where(function ($query) use ($keyword) {
  305. if ($keyword) {
  306. $query->where('name', 'like', "%{$keyword}%")->orWhere('mobile', 'like', "%{$keyword}%");
  307. }
  308. })
  309. ->orderBy('id', 'asc')
  310. ->select(['id', 'name','mobile', 'real_name'])
  311. ->get()
  312. ->toArray();
  313. return $list;
  314. }
  315. /**
  316. * 删除企业(重写父类方法以清除缓存)
  317. * @return array
  318. */
  319. public function delete()
  320. {
  321. $result = parent::delete();
  322. return $result;
  323. }
  324. }