| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\StoreModel;
- use App\Models\UserModel;
- use App\Models\UserRoleModel;
- use App\Services\BaseService;
- use Illuminate\Support\Facades\DB;
- /**
- * 商户店铺管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Common
- */
- class StoreService extends BaseService
- {
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * AdService constructor.
- */
- public function __construct()
- {
- $this->model = new StoreModel();
- }
- /**
- * 列表
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getDataList($params, $pageSize = 15)
- {
- // 分页查询
- $list = $this->model
- ->with(['member'])
- ->where(['mark'=>1])
- ->where(function ($query) use ($params) {
- // 状态筛选
- if (isset($params['status']) && $params['status'] > 0) {
- $query->where('status', $params['status']);
- }
- // 分类
- if (isset($params['category_id']) && $params['category_id'] > 0) {
- $query->where('category_id', $params['category_id']);
- }
- // 名称搜索
- if (isset($params['name']) && $params['name']) {
- $query->where('name', 'like', "%{$params['name']}%");
- }
- // 联系电话搜索
- if (isset($params['mobile']) && $params['mobile']) {
- $query->where('mobile', 'like', "%{$params['mobile']}%");
- }
- // 姓名搜索
- if (isset($params['real_name']) && $params['real_name']) {
- $query->where('real_name', 'like', "%{$params['real_name']}%");
- }
- })
- ->orderBy('create_time', 'desc')
- ->orderBy('id', 'desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list->toArray();
- // 格式化数据
- if (isset($list['data']) && !empty($list['data'])) {
- foreach ($list['data'] as &$item) {
- $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
- $item['status_text'] = $this->getStatusText($item['status']);
- // 添加余额信息
- $item['balance'] = isset($item['member']['balance']) ? $item['member']['balance'] : 0;
- $item['password'] = '';
- }
- }
- return [
- 'msg' => '操作成功',
- 'code' => 0,
- 'data' => $list['data'],
- 'count' => $list['total']
- ];
- }
- /**
- * 获取状态文本
- * @param int $status
- * @return string
- */
- private function getStatusText($status)
- {
- $statusMap = [
- 1 => '已审核(营业中)',
- 2 => '待审核',
- 3 => '审核失败'
- ];
- return isset($statusMap[$status]) ? $statusMap[$status] : '未知';
- }
- /**
- * 查询
- * @param $params
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function getQuery($params)
- {
- $where = ['mark' => 1];
- $status = isset($params['status']) ? $params['status'] : 0;
- if ($status > 0) {
- $where['status'] = $status;
- }
- $model = $this->model
- ->select('user.*')
- ->where($where);
- // 搜索条件
- $model->where(function ($query) use ($params) {
- // 店铺名称搜索
- $name = isset($params['name']) ? trim($params['name']) : '';
- if ($name) {
- $query->where('name', 'like', "%{$name}%");
- }
- // 联系电话搜索
- $phone = isset($params['phone']) ? trim($params['phone']) : '';
- if ($phone) {
- $query->where('mobile', 'like', "%{$phone}%");
- }
- // 姓名搜索
- $realName = isset($params['real_name']) ? trim($params['real_name']) : '';
- if ($realName) {
- $query->where('real_name', 'like', "%{$realName}%");
- }
- });
- return $model;
- }
- /**
- * 添加或编辑
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function edit()
- {
- $data = request()->all();
- // 图片处理
- if (isset($data['logo'])) {
- $data['logo'] = get_image_path($data['logo']);
- }
- if (isset($data['business_license'])) {
- $data['business_license'] = get_image_path($data['business_license']);
- }
- if (isset($data['driver_license'])) {
- $data['driver_license'] = get_image_path($data['driver_license']);
- }
- if (empty($data['name'])) {
- return message('请填写商户名称', false);
- }
- if (empty($data['real_name'])) {
- return message('请填写商户法人姓名', false);
- }
- if (empty($data['legal_idcard'])) {
- return message('请填写商户法人身份证', false);
- }
- if (empty($data['credit_code'])) {
- return message('请填写商户社会统一信用代码', false);
- }
- if (empty($data['settle_bank_name'])) {
- return message('请填写商户结算开户行', false);
- }
- $mobile = isset($data['mobile'])?$data['mobile']:'';
- if (empty($mobile)) {
- return message('请填写商户账号(联系电话)', false);
- }
- // 手机号唯一性验证
- $categoryId = isset($data['category_id']) && $data['category_id']? $data['category_id'] : 1;
- $id = isset($data['id']) ? $data['id'] : 0;
- if ($mobile) {
- $checkId = $this->model->where(['mobile' => $mobile,'category_id'=>$categoryId, 'mark' => 1])->value('id');
- if ($checkId && ($id != $checkId)) {
- return message('该商户已存在', false);
- }
- }
- DB::beginTransaction();
- $result = parent::edit($data);
- $success = isset($result['success'])? $result['success']:false;
- $id = isset($result['data']['id'])? $result['data']['id']:0;
- if($success){
- $account = [
- 'type'=> 2,
- 'user_id'=>$data['user_id'],
- 'store_id'=> $id,
- 'username'=> $mobile,
- 'mobile'=> $mobile,
- 'avatar'=> $data['logo'],
- 'realname'=> $data['real_name'],
- 'nickname'=> $data['name'],
- ];
- // 密码处理
- if (isset($data['password']) && $data['password']) {
- $account['password'] = get_password(trim($data['password']). $mobile);
- } else {
- // 编辑时如果密码为空则不更新密码
- unset($account['password']);
- }
- $adminId = UserModel::where(['username' => $mobile, 'mark' => 1])->value('id');
- // 删除已存在的用户角色关系数据
- $userRoleService = new UserRoleService();
- $userRole = $adminId?UserRoleModel::where(['user_id'=>$adminId,'role_id'=>6])->first():[];
- if ($adminId) {
- $account['update_time']=time();
- UserModel::where(['id'=>$adminId])->update($account);
- }else{
- $account['create_time']=time();
- if(!$adminId = UserModel::insertGetId($account)){
- DB::rollBack();
- return message('创建商户管理账户失败', false);
- }
- }
- if(empty($userRole)){
- $userRoleService->deleteUserRole($adminId);
- // 插入用户角色关系数据
- $userRoleService->insertUserRole($adminId, [6]);
- }
- }
- DB::commit();
- return message('操作成功',true);
- }
- /**
- * 获取当前登录企业信息
- * @return array
- */
- public function getCurrentStoreInfo($storeId)
- {
- try {
- // 查询企业信息
- $store = StoreModel::where('id', $storeId)
- ->where('mark', 1)
- ->with(['member'])
- ->first();
- if (!$store) {
- return ['code' => 1, 'msg' => '企业信息不存在'];
- }
- // 检查企业状态
- if ($store->status !== 1) {
- return ['code' => 1, 'msg' => '企业已被禁用'];
- }
- $data = [
- 'user_id' => $store->user_id,
- 'name' => $store->nickname,
- 'real_name' => $store->realname,
- 'phone' => $store->mobile,
- 'status' => $store->status
- ];
- return ['code' => 0, 'msg' => '获取成功', 'data' => $data];
- } catch (\Exception $e) {
- return ['code' => 1, 'msg' => '获取失败:' . $e->getMessage()];
- }
- }
- /**
- * 获取企业详情(重写父类方法)
- * @return array
- */
- public function info()
- {
- // 记录ID
- $id = request()->input("id", 0);
- $info = [];
- if ($id) {
- // 获取企业基本信息(使用关联查询)
- $store = $this->model->where(['id' => $id, 'mark' => 1])
- ->first();
- if ($store) {
- $info = $store->toArray();
- // 处理图片
- if (isset($info['avatar'])) {
- $info['avatar'] = get_image_url($info['avatar']);
- }
- if (isset($info['business_license'])) {
- $info['business_license'] = get_image_url($info['business_license']);
- }
- // 处理时间
- if (isset($info['create_time'])) {
- $info['create_time'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
- }
- if (isset($info['update_time'])) {
- $info['update_time'] = $info['update_time'] ? datetime($info['update_time'], 'Y-m-d H:i:s') : '';
- }
- // 添加状态文本
- $info['status_text'] = $this->getStatusText($info['status'] ?? 0);
- }
- }
- return message(MESSAGE_OK, true, $info);
- }
- /**
- * 获取店铺选项列表(用于下拉选择)
- * @return array
- */
- public function options()
- {
- $param = request()->all();
- // 用户ID
- $keyword = getter($param, "keyword");
- $list = $this->model
- ->where(['status'=>1,'mark'=>1])
- ->where(function ($query) use ($keyword) {
- if ($keyword) {
- $query->where('name', 'like', "%{$keyword}%")->orWhere('mobile', 'like', "%{$keyword}%");
- }
- })
- ->orderBy('id', 'asc')
- ->select(['id', 'name','mobile', 'real_name'])
- ->get()
- ->toArray();
- return $list;
- }
- /**
- * 删除企业(重写父类方法以清除缓存)
- * @return array
- */
- public function delete()
- {
- $param = request()->all();
- $ids = getter($param, "id");
- $result = parent::delete();
- $success = isset($result['success'])?$result['success']:false;
- if($success){
- if(is_array($ids)){
- $uids = UserModel::whereIn('store_id', $ids)->pluck('id');
- UserModel::whereIn('store_id', $ids)->update(['mark'=>0,'update_time'=>time()]);
- UserRoleModel::whereIn("user_id", $uids)->delete();
- }else{
- $uid = UserModel::where('store_id', $ids)->value('id');
- UserModel::where('store_id', $ids)->update(['mark'=>0,'update_time'=>time()]);
- UserRoleModel::where("user_id", $uid)->delete();
- }
- }
- $this->model->where(['mark'=>0])->where('update_time','<=', time()-600)->delete();
- return $result;
- }
- }
|