// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\api\controller\relations; use app\api\controller\Controller; use app\api\model\ApplySeller; use app\api\model\HireMgs; use app\api\model\OrderGoods; use app\api\model\shop\Shops; use app\api\model\User; use app\api\model\user\BonusHistory; use app\api\model\user\CommissionsDetail; use app\api\service\User as UserService; use app\common\library\helper; use app\common\model\PushLog; use think\facade\Log; use app\common\model\store\Setting as SettingModel; use think\facade\Validate; /** * 推广中心控制器 * Class Promotion * @package app\api\controller */ class Promotion extends Controller { private $userId; public function initialize(){ $this->userId = UserService::getCurrentLoginUserId(); } /** * 推广中心 * @return array|\think\response\Json */ public function centerIndex() { //$userId = UserService::getCurrentLoginUserId(); $userInfo = User::with(['avatar'=>function($query){ $query->field('file_id,file_type,storage,domain,file_path'); },'shop'])->where('user_id',$this->userId) ->field('user_id,mobile,nick_name,avatar_id,role,shop_id,upper_user_id,can_withdraw_money,have_withdrew_money,ktxyj_amount,seller_grade,seller_expire') ->find(); $shops = $userInfo->shops->toArray(); $userInfo = $userInfo->toArray(); $userInfo['shops'] = $shops; $upper_user = null; //推广订单金额 $userInfo['give_out_money'] = OrderGoods::sumGiveOutOrder($this->userId); //我的收益金 $userInfo['my_profits'] = helper::bcsub($userInfo['ktxyj_amount'],0,2); $userInfo['my_bonus'] = BonusHistory::getCurrYearBonus($this->userId); //直推人数 $userInfo['subordinates'] = User::countSubordinates($this->userId,$userInfo['shop']['shop_id'] ?? 0,$userInfo['role']); $userInfo['sellers'] = $userInfo['binders'] = 0; if ($userInfo['role'] == User::SHOP_MG){ //门店关联消费者,只有店长可见 $userInfo['binders'] = User::countShopBinder($userInfo['shop_id']); //店员数量,只有店长可见 $userInfo['sellers'] = User::countShopSellers($userInfo['shop_id']); } $ApplySeller = ApplySeller::where('user_id',$this->userId)->find(); $userInfo['apply_state'] = 9; $userInfo['apply_refuse_reason'] = ''; if($ApplySeller){ $userInfo['apply_state'] = $ApplySeller['state']; $userInfo['apply_refuse_reason'] = $ApplySeller['refuse_reason']; } //推荐官信息 $userInfo['distributor_expire'] = ''; $userInfo['distributor_grade_ratio'] = null; $userInfo['sub_sales_volume'] = $userInfo['sub_comms_count'] = 0; if ($userInfo['role'] == 99){ $userInfo['distributor_expire'] = date("Y-m-d H:i:s",$userInfo['seller_expire']); $userInfo['distributor_grade_ratio'] = (SettingModel::getItem('distributor_grade'))['distributor'][$userInfo['seller_grade'] -1 ]; //下级推荐官销售额 $userInfo['sub_sales_volume'] = CommissionsDetail::sumUserCommSalesVolume($this->userId); //下级推荐官人数 $userInfo['sub_comms_count'] = User::where('upper_user_id',$this->userId)->where('role',User::COMMISSION_USER)->count(); } return $this->renderSuccess($userInfo); } /** * 门店老板切换门店 * @return array|\think\response\Json * @throws \app\common\exception\BaseException */ public function switchShop(){ $shopId = intval(request()->post('shopId',0)); $userId = UserService::getCurrentLoginUserId(); if (User::switchBindShop($userId,$shopId)){ return $this->renderSuccess(); } return $this->renderSuccess('系统繁忙'); } /** * 我的收益金 * @param $status * @return array|\think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function myProfitsList($status){ if ($status!=0 && $status !=1){ return $this->renderError('参数有误'); } $data = CommissionsDetail::getCommsList($this->userId,$status); return $this->renderSuccess($data); } /** * 我的收益金 按订单 * @param $status * @return array|\think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function myProfitsListOrder($status){ if ($status!=0 && $status !=1 && $status!=2){ return $this->renderError('参数有误'); } $data = CommissionsDetail::getCommsListOrder($this->userId,$status); return $this->renderSuccess($data); } /** * 我的收益金-退款订单 */ public function refundOrderList() { $data = CommissionsDetail::getRefundOrderList($this->userId); return $this->renderSuccess($data); } /** * 直推用户/我的顾客 * @return array|\think\response\Json */ public function straightUsers(){ $keyword = strval(request()->post('keyword','')); $data = User::getBindersDetail($this->userId,1,$keyword); return $this->renderSuccess($data); } /** * 门店关联消费者 * @return array|\think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function shopRelateUsers(){ $keyword = strval(request()->post('keyword','')); $shopId = intval(request()->post('shopId',0)); $data = User::getBindersDetail($this->userId,2,$keyword,$shopId); if (!$data){ return $this->renderSuccess([]); } return $this->renderSuccess($data); } /** * 推广订单列表 旧20220507准备废弃 * @return array|\think\response\Json * @throws \app\common\exception\BaseException */ public function promotionOrders(){ $dataType = strval(request()->post('dataType',''));//订单状态(all已付款,received已付款待收货,completed已完成,close已失效) $keyword = strval(request()->post('keyword','')); $type = intval(request()->post('timeType',0)); $from = strval(request()->post('from','')); $to = strval(request()->post('to','')); $shopId = intval(request()->post('shopId',0)); $userId= UserService::getCurrentLoginUserId(); $data = CommissionsDetail::getCommissionOrders($userId,$dataType,$type,$from,$to,$keyword,$shopId); return $this->renderSuccess($data); } /** * 推广订单列表 新 * @return array|\think\response\Json * @throws \app\common\exception\BaseException */ public function promotionOrdersNew(){ $dataType = strval(request()->post('dataType',''));//订单状态(all已付款,received已付款待收货,completed已完成,close已失效) $keyword = strval(request()->post('keyword','')); $type = intval(request()->post('timeType',0)); $from = strval(request()->post('from','')); $to = strval(request()->post('to','')); $shopId = intval(request()->post('shopId',0)); $isDown = intval(request()->post('isDown',0)); $userId= UserService::getCurrentLoginUserId(); $data = CommissionsDetail::getCommissionOrdersNew($userId,$dataType,$type,$from,$to,$keyword,$shopId,$isDown); return $this->renderSuccess($data); } /** * 新增员工业绩榜单模块 * @param $month * @return array|\think\response\Json * @throws \think\db\exception\DbException */ public function staffAchievements(){ //$user = User::detail( $this->userId)->toArray(); $shopId = intval($this->request->get('shopId',0)); //$shopId = $user['shop_id']; $type = request()->get('month',1);//1本周,2本月,3本年 $to = time(); if ($type == 1){ $w = date('w',time()) - 1; //求得当前星期 $from = time() - $w * 60 * 60 * 24; $from = date("Y-m-d",$from)." 00:00:00"; $from = strtotime($from); }elseif($type == 2){ $month = date('Y-m',time()); $from = strtotime($month); }else{ $month = date('Y',time()).'-01-01 '.'00:00:00'; $from = strtotime($month); } $aches = User::staffAches($shopId,$from,$to); return $this->renderSuccess($aches); } /** * 推广中心-我的奖励金 */ public function bonusHistoryList($type = 0) { $model = new BonusHistory(); $list = $model->getList($type); // 奖励金结算日 commission_days + 5 $refundSetting = SettingModel::getItem('refund'); $bonus_clearing_day = $refundSetting['commission_days'] + 5; return $this->renderSuccess(compact('list', 'bonus_clearing_day')); } /** * 推广中心-我的奖励金-门店明细 */ public function shopBonusList() { $type = intval($this->request->post('type',0)); $year = intval($this->request->post('year',0)); $month = intval($this->request->post('month',0)); $curr_month = false; if($year == intval(date('Y')) && $month == intval(date('m'))){ $curr_month = true; } $model = new BonusHistory(); $list = $model->getShopBonusList($month,$year,$type); // 奖励金结算日 commission_days + 5 return $this->renderSuccess(compact('list','curr_month')); } /** * 门店详情 * @param $shopId * @return array|\think\response\Json */ public function shopDetails($shopId){ $shop = []; if (!$shopId)return $this->renderSuccess(compact('shop')); //门店销售额 $from = strtotime(date('Y-m-01 00:00:00')); $shopM = new Shops(); $shop = $shopM->where('shop_id',$shopId)->field('shop_id,shop_name,province_id,city_id,region_id')->find(); //$shop['rec_sum'] = helper::bcadd($shopM->getRecOrdersAllSum($shopId,$from,time()),0,2); $shop['rec_sum'] = OrderGoods::sumShopGiveOutOrder($shopId,$from,time()); $shop['binders'] = User::countShopBinder($shopId); //店员数量 $shop['sellers'] = User::countShopSellers($shopId); return $this->renderSuccess(compact('shop')); } /** * 店员交接,离职 * @return array */ public function sellerHandover(){ $user = User::detail( $this->userId)->toArray(); if ($user['role'] != User::SHOP_MG && $user['role'] != User::SHOP_BOSS){ return $this->renderError('无法进行此操作'); } $shopId = intval($this->request->post('shopId',0)); if (!$shopId)return $this->renderError('门店信息不存在'); $oldSeller = intval($this->request->post('fromUserId',0)); $toSeller = intval($this->request->post('toUserId',0)); if (!$oldSeller || !$toSeller || ($oldSeller==$toSeller)){ return $this->renderError('参数有误'); } $m = new User(); try { $count = $m->consumersHandover($oldSeller,$toSeller,$shopId); $m->updateUserRole($oldSeller); }catch (\Exception $e){ log_record($e->getMessage(),'error'); return $this->renderError('系统繁忙'); } $old = User::where('user_id',$oldSeller)->find(); //if(env('APP_DEBUG') == false) { } PushLog::addSmsMon('【公明腊肠官方体验店】您原店员身份已失效,为使您的账户中的收益金保持不变,我们为您保留了提现入口,您可以继续提现~', $old['mobile'], 1); $list = ['count'=>$count]; return $this->renderSuccess($list); } /** * 聘请店长按钮 * @param $shopId * @return array|\think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function hireManagerBtn($shopId){ $shopM = new Shops(); $shop = $shopM->where('shop_id',$shopId)->field('shop_id,shop_name,manager_user_id,province_id,city_id,region_id')->find(); $shop->manager = null; if ($shop->manager_user_id){ $shop->manager = User::find($shop->manager_user_id); } return $this->renderSuccess(compact('shop')); } /** * 提交聘请店长 * @param $shopId * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function hireManager(){ $shopId = intval($this->request->post('shopId',0)); if (!$shopId)return $this->renderError('门店信息不存在'); $shop = Shops::where('shop_id',$shopId)->find(); if (empty($shop)){ return $this->renderError('门店信息不存在'); } if ($shop['boss_user_id'] != $this->userId){ return $this->renderError('您不是本店老板哦'); } $data['name'] = strval($this->request->post('name','')); $data['mobile'] = strval($this->request->post('mobile','')); $validate = Validate::rule('mobile', 'require|mobile')->rule([ 'name' => 'require|max:25' ]); if (!$validate->check($data)) { return $this->renderError($validate->getError()); } $user = User::where('mobile','=',$data['mobile'])->find(); $model = new HireMgs(); $data['user_id'] = $this->userId; $data['shop_id'] = $shopId; $data['mg_name'] = $data['name']; $data['mg_mobile'] = $data['mobile']; if (empty($user)){ $model->save($data); $res = ['rid'=>$model->id]; return $this->renderSuccess(compact('res')); } if ($user['role'] == User::SHOP_SELLER && $user['shop_id'] == $shopId){ //如果是本店店员 //判断是否有下级顾客 $c = User::where('bind_shop_id',$shopId)->where('upper_user_id',$user['user_id'])->count(); if ($c){ return $this->renderError('请先办理店员交接,转移顾客'); }else{ $model->save($data); $res = ['rid'=>$model->id]; return $this->renderSuccess(compact('res')); } }elseif (($user['role'] == User::SHOP_SELLER && $user['shop_id'] != $shopId) || $user['role'] == User::SHOP_BOSS){ return $this->renderError('您邀请的店长已担任其他门店角色'); }elseif ($user['role'] == User::SHOP_MG && $user['shop_id'] == $shopId){ return $this->renderError('您邀请的用户已是店长,请勿重复邀请'); }elseif(($user['role'] == User::SHOP_MG && $user['shop_id'] != $shopId) || ($user['role'] >User::SHOP_SELLER && $user['role'] < User::COMMISSION_USER)){ return $this->renderError('您邀请的店长已担任其他门店角色'); }elseif($user['role'] == User::COMMISSION_USER){ return $this->renderError('您邀请的用户已是分销员,不支持升级店长'); }elseif ($user['role'] == User::NORMAL_USER){ // 如有上级,切断上级 User::where('user_id',$user['user_id'])->update(['upper_user_id'=>0,'bind_shop_id'=>0]); $model->save($data); $res = ['rid'=>$model->id]; return $this->renderSuccess(compact('res')); }else{ return $this->renderError('保存失败'); } return $this->renderError('保存失败'); } /** * 店长离职-解聘店长 * @return array|\think\response\Json */ public function managerDimission(){ $shopId = intval($this->request->get('shopId',0)); if (!$shopId)return $this->renderError('门店信息不存在'); $shop = Shops::where('shop_id',$shopId)->find(); if (empty($shop)){ return $this->renderError('门店信息不存在'); } if ($shop['boss_user_id'] != $this->userId){ return $this->renderError('您不是本店老板哦'); } if (!$shop['manager_user_id']){ return $this->renderError('您还没未本店聘请店长哦'); } $mg = User::where('user_id',$shop['manager_user_id'])->find(); if (!$mg){ return $this->renderError('店长信息读取异常'); } /* User::where('user_id',$shop['manager_user_id'])->update(['role'=>User::NORMAL_USER,'shop_id'=>0]); Shops::where('shop_id',$shopId)->update(['manager_user_id'=>0]);*/ $shopM = new Shops(); $shopM->dissMg($shop['manager_user_id'],$shopId); PushLog::addSmsMon('【公明腊肠官方体验店】您原店长身份已失效,为使您的账户中的收益金保持不变,我们为您保留了提现入口,您可以继续提现~',$mg['mobile'],1); return $this->renderSuccess(); } /** * 店长加入团队 * @return array|\think\response\Json */ public function managerJoinShop(){ $rid = intval($this->request->get('rid',0)); if (!$rid)return $this->renderError('参数错误'); $user = User::detail( $this->userId); $record = HireMgs::where('id',$rid)->where('status',0)->find(); if (!$record){ return $this->renderError('邀请链接已失效'); } if ($user['role'] == User::SHOP_MG && $user['shop_id'] == $record['shop_id']){ return $this->renderError('您已是此门店店长'); } /* if ($user['role'] != User::NORMAL_USER){ return $this->renderError('您不是普通用户或已担任其他门店角色'); }*/ if ($user['mobile'] != $record['mg_mobile']){ return $this->renderError('不合要求'); } $userM = new User(); if ($userM->mgShopJoin($user['user_id'],$rid,$record['shop_id']) == false){ return $this->renderError('系统繁忙'); } $shop = Shops::where('shop_id',$record['shop_id'])->field('shop_id,shop_name')->find(); return $this->renderSuccess(['shop_id'=>$shop['shop_id'],'shop_name'=>$shop['shop_name']]); } /** * 员工列表 * @return array|\think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function staffList(){ $shopId = intval($this->request->get('shopId',0)); if (!$shopId)return $this->renderError('门店信息不存在'); $res = User::where('shop_id',$shopId)->where('role',User::SHOP_SELLER) ->field('user_id,nick_name,mobile')->select(); return $this->renderSuccess(compact('res')); } }