store_id, 'bonus_pool')['value']; //是否开启区域多代理 $is_multi_agent = Option::get('is_multi_agent', $this->store_id, 'bonus_pool_area_agent', 0)['value']; $multi_agent_type1 = Option::get('multi_agent_type1', $this->store_id, 'bonus_pool_area_agent', 0)['value']; $multi_agent_type2 = Option::get('multi_agent_type2', $this->store_id, 'bonus_pool_area_agent', 0)['value']; $multi_agent_type3 = Option::get('multi_agent_type3', $this->store_id, 'bonus_pool_area_agent', 0)['value']; $multi_agent_type4 = Option::get('multi_agent_type4', $this->store_id, 'bonus_pool_area_agent', 0)['value']; // 申请设置 // 轮播广告状态 $area_agent_banner_status = Option::get('area_agent_banner_status', $this->store_id, 'bonus_pool_area_agent', 0)['value']; // 图片列表 $area_agent_banner_images = Option::get('area_agent_banner_images', $this->store_id, 'bonus_pool_area_agent', '')['value']; $area_agent_banner_images = json_decode($area_agent_banner_images, true); //客户端名称自定义 $area_name = Option::get('area_name', $this->store_id, 'bonus_pool', '')['value']; $shareHolderCashProfit = Option::get('shareHolderCashProfit', $this->store_id, 'bonus_pool_area_agent', '')['value']; $pay_wechat = Option::get('pay_wechat', $this->store_id, 'bonus_pool_area_agent', '')['value']; $pay_alipay = Option::get('pay_alipay', $this->store_id, 'bonus_pool_area_agent', '')['value']; $bank = Option::get('bank', $this->store_id, 'bonus_pool_area_agent', '')['value']; $remaining_sum = Option::get('remaining_sum', $this->store_id, 'bonus_pool_area_agent', 0)['value']; $lg = Option::get('lg', $this->store_id, 'bonus_pool_area_agent', '')['value']; $cash_max_day = Option::get('cash_max_day', $this->store_id, 'bonus_pool_area_agent', '')['value']; $cash_max_single_day = Option::get('cash_max_single_day', $this->store_id, 'bonus_pool_area_agent', '')['value']; $min_money = Option::get('min_money', $this->store_id, 'bonus_pool_area_agent', '')['value']; $cash_price_type = Option::get('cash_price_type', $this->store_id, 'bonus_pool_area_agent', '1')['value']; $cash_price_amount = Option::get('cash_price_amount', $this->store_id, 'bonus_pool_area_agent', 100)['value']; $cash_price_integral = Option::get('cash_price_integral', $this->store_id, 'bonus_pool_area_agent', 0)['value']; $cash_price_balance = Option::get('cash_price_balance', $this->store_id, 'bonus_pool_area_agent', 0)['value']; return [ 'code' => 0, 'msg' => '获取成功', 'data' => [ 'is_open_area' => $is_open_area, 'is_multi_agent' => $is_multi_agent, 'multi_agent_type1' => $multi_agent_type1, 'multi_agent_type2' => $multi_agent_type2, 'multi_agent_type3' => $multi_agent_type3, 'multi_agent_type4' => $multi_agent_type4, 'area_agent_banner_status' => (int)$area_agent_banner_status, 'area_agent_banner_images' => $area_agent_banner_images ?: [], 'area_name' => $area_name, 'shareHolderCashProfit' => $shareHolderCashProfit, 'pay_wechat' => $pay_wechat, 'pay_alipay' => $pay_alipay, 'bank' => $bank, 'remaining_sum' => $remaining_sum, 'cash_max_day' => $cash_max_day, 'cash_max_single_day' => $cash_max_single_day, 'min_money' => $min_money, 'lg' => $lg, 'cash_price_type' => $cash_price_type, 'cash_price_amount' => $cash_price_amount, 'cash_price_integral' => $cash_price_integral, 'cash_price_balance' => $cash_price_balance, ] ]; } //配置信息 public function setAreaAgentConfig($params = []) { try { $is_open_area = $this->is_open_area; $is_multi_agent = $params['is_multi_agent'] ?? 0; $multi_agent_type1 = $params['multi_agent_type1'] ?? 0; $multi_agent_type2 = $params['multi_agent_type2'] ?? 0; $multi_agent_type3 = $params['multi_agent_type3'] ?? 0; $multi_agent_type4 = $params['multi_agent_type4'] ?? 0; $area_agent_banner_status = $this->area_agent_banner_status; $area_name = $this->area_name; $area_agent_banner_images = json_encode($this->area_agent_banner_images); $shareHolderCashProfit = $this->shareHolderCashProfit; $pay_wechat = $this->pay_wechat; $pay_alipay = $this->pay_alipay; $bank = $this->bank; $remaining_sum = $this->remaining_sum; $cash_max_day = $this->cash_max_day; $cash_max_single_day = $this->cash_max_single_day; $min_money = $this->min_money; $store_id = $this->store_id; $lg = $this->lg; $cash_price_type = $this->cash_price_type ?? '1'; $cash_price_amount = $this->cash_price_amount ?? 100; $cash_price_integral = $this->cash_price_integral ?? 0; $cash_price_balance = $this->cash_price_balance ?? 0; // $total_profit = 0; // $cash_price_type_ = explode(',', $cash_price_type); // if (in_array(CashExt::CASH_PRICE_TYPE_AMOUNT, $cash_price_type_)) { // $total_profit = bcadd($total_profit, $cash_price_amount, 2); // } // if (in_array(CashExt::CASH_PRICE_TYPE_INTEGRAL, $cash_price_type_)) { // $total_profit = bcadd($total_profit, $cash_price_integral, 2); // } // if (in_array(CashExt::CASH_PRICE_TYPE_BALANCE, $cash_price_type_)) { // $total_profit = bcadd($total_profit, $cash_price_balance, 2); // } // if ($total_profit != 100) { // return [ // 'code' => 1, // 'msg' => '佣金/积分/余额比例设置错误' // ]; // } $key = [ // 'is_open_area', 'is_multi_agent', 'multi_agent_type1', 'multi_agent_type2', 'multi_agent_type3', 'multi_agent_type4', 'area_agent_banner_status', 'area_agent_banner_images', // 'area_name', 'shareHolderCashProfit', "pay_wechat", "pay_alipay", "bank", 'remaining_sum', "lg", "cash_max_day", "cash_max_single_day", "min_money", 'cash_price_type', 'cash_price_amount', 'cash_price_integral', 'cash_price_balance', ]; Option::set($key, [ // $is_open_area, $is_multi_agent, $multi_agent_type1, $multi_agent_type2, $multi_agent_type3, $multi_agent_type4, $area_agent_banner_status, $area_agent_banner_images, // $area_name, $shareHolderCashProfit, $pay_wechat, $pay_alipay, $bank, $remaining_sum, $lg, $cash_max_day, $cash_max_single_day, $min_money, $cash_price_type, $cash_price_amount, $cash_price_integral, $cash_price_balance ], $store_id, 'bonus_pool_area_agent'); Option::set(['is_open_area', 'area_name'], [$is_open_area, $area_name], $store_id, 'bonus_pool'); return [ 'code' => 0, 'msg' => '保存成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //处理区域代理申请 public function handleAreaAgentApply() { try { $ids = $this->id; $ids = explode(',', $ids); $status = $this->status; $store_id = $this->store_id; foreach ($ids as $id) { $agent_apply = ShareHolderAreaAgentApply::findOne(['id' => $id, 'is_delete' => 0]); if (!$agent_apply) { throw new \Exception('数据不存在'); } if (in_array($status, [1, 2]) && intval($agent_apply->status) !== 0) { throw new \Exception('不可重复操作'); } if (!in_array($status, [1, 2, 3])) { throw new \Exception('状态错误'); } if (in_array($status, [1, 2])) { $agent_apply->status = $status; $agent_apply->apply_time = time(); } else { $agent_apply->is_delete = 1; } if (!$agent_apply->save()) { throw new \Exception($agent_apply->errors, JSON_UNESCAPED_UNICODE); } $name = '区域代理'; $mobile = ''; $user = User::findOne($agent_apply->user_id); $saas_user = SaasUser::findOne(['mobile' => $user->binding]); if ($saas_user) { $name = $saas_user->name; $mobile = $saas_user->mobile; } // $params = json_decode($agent_apply->params, true); if (intval($status) === 1) { $shareHolderModel = ShareHolder::findOne(['store_id' => $store_id, 'user_id' => $agent_apply->user_id, 'is_delete' => 0]); if (!$shareHolderModel) { $currentLevel = ShareHolderLevel::findOne(['store_id' => $store_id, 'is_default' => 1, 'is_delete' => 0]); $shareHolderModel = new ShareHolder(); $shareHolderModel->level_id = $currentLevel->id ?: 0; $shareHolderModel->store_id = $store_id; $shareHolderModel->user_id = $agent_apply->user_id; $shareHolderModel->name = $name; $shareHolderModel->address = ''; $shareHolderModel->mobile = $mobile; $shareHolderModel->status = 1; $shareHolderModel->audit_time = time(); $shareHolderModel->province_id = $shareHolderModel->province = ($agent_apply->province_id ?: 0); $shareHolderModel->city_id = $shareHolderModel->city = ($agent_apply->city_id ?: 0); $shareHolderModel->district_id = $shareHolderModel->district = ($agent_apply->district_id ?: 0); $shareHolderModel->town_id = $shareHolderModel->town_id = ($agent_apply->town_id ?: 0); $shareHolderModel->province_name = District::findOne($shareHolderModel->province_id)->name ?: ''; $shareHolderModel->city_name = District::findOne($shareHolderModel->city_id)->name ?: ''; $shareHolderModel->district_name = District::findOne($shareHolderModel->district_id)->name ?: ''; $shareHolderModel->town_name = District::findOne($shareHolderModel->town_id)->name ?: ''; if (!$shareHolderModel->save()) { throw new \Exception($shareHolderModel->errors, JSON_UNESCAPED_UNICODE); } // BonusPool::checkChildHolderUpdateLevel($agent_apply->user_id, $currentLevel->id); // BonusPool::checkLevel($store_id, 0, $agent_apply->user_id); BonusPool::ShareHolderLevelJob($store_id, 0, $agent_apply->user_id); if ((int)$currentLevel->member_level > (int)$user->level) { $user->level = $currentLevel->member_level; } $user->is_holder = 1; if (!$user->save()) { throw new \Exception($user->errors, JSON_UNESCAPED_UNICODE); } } $shareHolderModel->province_id = $shareHolderModel->province = $agent_apply->province_id; $shareHolderModel->city_id = $shareHolderModel->city = $agent_apply->city_id; $shareHolderModel->district_id = $shareHolderModel->district = $agent_apply->district_id; $shareHolderModel->town_id = $shareHolderModel->town_id = $agent_apply->town_id; $shareHolderModel->province_name = District::findOne($agent_apply->province_id)->name ?: ''; $shareHolderModel->city_name = District::findOne($agent_apply->city_id)->name ?: ''; $shareHolderModel->district_name = District::findOne($agent_apply->district_id)->name ?: ''; $shareHolderModel->town_name = District::findOne($agent_apply->town_id)->name ?: ''; $shareHolderModel->agent_type = $agent_apply->agent_type; $shareHolderModel->agent_time = time(); if (!$shareHolderModel->save()) { throw new \Exception($shareHolderModel->errors, JSON_UNESCAPED_UNICODE); } $agent_apply->share_holder_id = $shareHolderModel->id; if (!$agent_apply->save()) { throw new \Exception($agent_apply->errors, JSON_UNESCAPED_UNICODE); } } } return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //区域代理申请列表 public function getAreaAgentApplyList() { $store_id = $this->store_id; $name = $this->name; $status = $this->status; $start_time = $this->start_time; $end_time = $this->end_time; $query = ShareHolderAreaAgentApply::find()->alias('s')->where(['s.is_delete' => 0, 's.store_id' => $store_id]) ->leftJoin(['u' => User::tableName()], 's.user_id = u.id') ->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile'); if ($status !== null && in_array($status, [0, 1])) { $query->andWhere(['s.status' => $status]); } if ($name) { $query->andWhere(['LIKE', 'su.name', $name]); } if ($start_time) { $start_time = strtotime($start_time); $query->andWhere(['>=', 's.created_at', $start_time]); } if ($end_time) { $end_time = strtotime($end_time); $query->andWhere(['<=', 's.created_at', $end_time]); } $query->select('s.id, su.name, su.avatar, su.mobile, s.params, s.created_at, s.status'); $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']); $item['params'] = json_decode($item['params'], true); $item['status'] = (int)$item['status']; $item['agent_type'] = (int)$item['params']['agent_type']; $item['province'] = ''; $item['city'] = ''; $item['district'] = ''; switch ($item['agent_type']) { case 1: $item['province'] = District::findOne(['id' => $item['params']['province_id']])->name; break; case 2: $item['province'] = District::findOne(['id' => $item['params']['province_id']])->name; $item['city'] = District::findOne(['id' => $item['params']['city_id']])->name; break; case 3: $item['province'] = District::findOne(['id' => $item['params']['province_id']])->name; $item['city'] = District::findOne(['id' => $item['params']['city_id']])->name; $item['district'] = District::findOne(['id' => $item['params']['district_id']])->name; break; case 4: $item['province'] = District::findOne(['id' => $item['params']['province_id']])->name; $item['city'] = District::findOne(['id' => $item['params']['city_id']])->name; $item['district'] = District::findOne(['id' => $item['params']['district_id']])->name; $item['town'] = District::findOne(['id' => $item['params']['town_id']])->name; break; } } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $pagination['list'], 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], ] ]; } // 获取代理管理 public function getAreaAgentList($params = []) { $store_id = $this->store_id; $name = $this->name; $status = $this->status; $start_time = $this->start_time; $end_time = $this->end_time; $query = ShareHolder::find()->alias('sh')->where(['sh.store_id' => $store_id, 'sh.is_delete' => 0, 'sh.status' => 1, 'u.is_delete' => 0]) ->andWhere(['AND', ['IS NOT', 'u.id', NULL], ['>', 'agent_type', 0]]) ->leftJoin(['u' => User::tableName()], 'u.id=sh.user_id') ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding'); if ($name) { $query->andWhere(['like', 'su.name', $name]); } if ($status !== null && in_array($status, [0, 1])) { $query->andWhere(['agent_rate_status' => $status]); } if ($params['agent_type'] > 0) { $query->andWhere(['agent_type' => $params['agent_type']]); } if ($params['province_id'] > 0) { $query->andWhere(['province_id' => $params['province_id']]); } if ($params['city_id'] > 0) { $query->andWhere(['city_id' => $params['city_id']]); } if ($params['district_id'] > 0) { $query->andWhere(['district_id' => $params['district_id']]); } if ($params['town_id'] > 0) { $query->andWhere(['town_id' => $params['town_id']]); } if ($start_time) { $start_time = strtotime($start_time); $query->andWhere(['>=', 'sh.created_at', $start_time]); } if ($end_time) { $end_time = strtotime($end_time); $query->andWhere(['<=', 'sh.created_at', $end_time]); } $query->select('sh.id, su.name, su.avatar, su.mobile, sh.agent_type, sh.agent_rate, sh.agent_rate_status, sh.created_at, sh.agent_time, sh.province_id ,sh.city_id ,sh.district_id ,sh.town_id'); $pagination = pagination_make($query); if($this->export = input_params('export', 0)){ $export_list = $query->asArray()->all(); if ($export_list){ foreach ($export_list as &$value){ $district = District::find(); switch ($value['agent_type']) { case 1: $address_arr = $district->andWhere(['id' => $value['province_id']])->select('name')->column(); break; case 2: $address_arr = $district->andWhere(['id' => [$value['province_id'], $value['city_id']]])->select('name')->column(); break; case 3: $address_arr = $district->andWhere(['id' => [$value['province_id'], $value['city_id'], $value['district_id']]])->select('name')->column(); break; case 4: $address_arr = $district->andWhere(['id' => [$value['province_id'], $value['city_id'], $value['district_id'], $value['town_id']]])->select('name')->column(); break; } $value['address'] = ''; if (!empty($address_arr)) { $value['address'] = implode('', $address_arr); } } } return $this->export($export_list); } foreach ($pagination['list'] as &$item) { $item['created_at'] = date('Y-m-d H:i:s', $item['agent_time'] ? : $item['created_at']); $item['agent_time'] = date('Y-m-d H:i:s', $item['agent_time']); $item['agent_rate_status'] = (int)$item['agent_rate_status']; $item['agent_type'] = (int)$item['agent_type']; $district = District::find(); switch ($item['agent_type']) { case 1: $address_arr = $district->andWhere(['id' => $item['province_id']])->select('name')->column(); break; case 2: $address_arr = $district->andWhere(['id' => [$item['province_id'], $item['city_id']]])->select('name')->column(); break; case 3: $address_arr = $district->andWhere(['id' => [$item['province_id'], $item['city_id'], $item['district_id']]])->select('name')->column(); break; case 4: $address_arr = $district->andWhere(['id' => [$item['province_id'], $item['city_id'], $item['district_id'], $item['town_id']]])->select('name')->column(); break; } $item['address'] = ''; if (!empty($address_arr)) { $item['address'] = implode('', $address_arr); } $item['province'] = District::findOne($item['province_id'])->name ?: ''; $item['city'] = District::findOne($item['city_id'])->name ?: ''; $item['district'] = District::findOne($item['district_id'])->name ?: ''; $item['town'] = District::findOne($item['town_id'])->name ?: ''; $item['province_id'] = $item['province_id'] ?: ''; $item['city_id'] = $item['city_id'] ?: ''; $item['district_id'] = $item['district_id'] ?: ''; $item['town_id'] = $item['town_id'] ?: ''; } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $pagination['list'], 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], ] ]; } private function export($list) { $rows = [[ 'ID', '用户名称', '手机号', '代理级别', '地址', '佣金比例', '通过时间', '是否开启', ]]; foreach($list as $item){ $r = [ $item['id'], $item['name'], $item['mobile'], $item['agent_type']==1?'省':($item['agent_type']==2?'市':($item['agent_type']==3?'区':'镇')), $item['address'], $item['agent_rate'], $item['agent_time']? date('Y-m-d H:i:s', $item['agent_time']):'', $item['agent_rate_status'] == 1 ? '开启' : '关闭', ]; $rows[] = $r; } $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow() ->addRows($rows)->toBrowser(); } public function setAgentRate() { try { $ids = $this->id; $store_id = $this->store_id; $ids = explode(',', $ids); $agent_rate = $this->agent_rate; $agent_rate_status = $this->agent_rate_status; $agent_type = $this->agent_type; $province_id = $this->province_id; $city_id = $this->city_id; $district_id = $this->district_id; $town_id = $this->town_id ?: 0; $is_multi_agent = Option::get('is_multi_agent', $store_id, 'bonus_pool_area_agent', 0)['value']; switch ($agent_type) { case 1: if (empty($province_id)) { throw new \Exception('缺失省级参数'); } break; case 2: if (empty($province_id) || empty($city_id)) { throw new \Exception('缺失省市级参数'); } break; case 3: if (empty($province_id) || empty($city_id) || empty($district_id)) { throw new \Exception('缺失省市区级参数'); } break; case 4: if (empty($province_id) || empty($city_id) || empty($district_id) || empty($town_id)) { throw new \Exception('缺失省市区镇级参数'); } break; } foreach ($ids as $id) { $shareHolder = ShareHolder::findOne($id); if (!$shareHolder) { throw new \Exception('数据不存在'); } if (isset($agent_rate)) { $shareHolder->agent_rate = $agent_rate; } if ($agent_rate_status !== null && in_array($agent_rate_status, [0, 1])) { $shareHolder->agent_rate_status = $agent_rate_status; } if (intval($agent_rate_status) === 2) { // $shareHolder->is_delete = 1; $shareHolder->agent_type = 0; $shareHolder->province_id = $shareHolder->province = 0; $shareHolder->city_id = $shareHolder->city = 0; $shareHolder->district_id = $shareHolder->district = 0; } if ($agent_type) { if($shareHolder->agent_type == 0){ $shareHolder->agent_time = time(); } $shareHolder->agent_type = $agent_type; $shareHolder->province_id = $shareHolder->province = $province_id ?: 0; $shareHolder->city_id = $shareHolder->city = $city_id ?: 0; $shareHolder->district_id = $shareHolder->district = $district_id ?: 0; $shareHolder->town_id = $town_id ?: 0; $query = ShareHolder::find()->where(['agent_type' => $agent_type, 'is_delete' => 0, 'store_id' => $store_id]);//check_share_holder switch ($agent_type) { case 1: $query->andWhere(['province_id' => $province_id]); break; case 2: $query->andWhere(['province_id' => $province_id, 'city_id' => $city_id]); break; case 3: $query->andWhere(['province_id' => $province_id, 'city_id' => $city_id, 'district_id' => $district_id]); break; case 4: $query->andWhere(['province_id' => $province_id, 'city_id' => $city_id, 'district_id' => $district_id, 'town_id' => $town_id]); break; } $check_share_holder = $query->andWhere(['<>', 'id', $id])->one(); if ($check_share_holder && !$is_multi_agent) { throw new \Exception('存在相同区域的代理'); } } if (!$shareHolder->save()) { throw new \Exception($shareHolder->errors, JSON_UNESCAPED_UNICODE); } $area_agent_apply = ShareHolderAreaAgentApply::findOne(['user_id' => $shareHolder->user_id, 'is_delete' => 0]); if ($area_agent_apply && intval($agent_rate_status) === 2) { $area_agent_apply->is_delete = 1; if (!$area_agent_apply->save()) { throw new \Exception($area_agent_apply->errors, JSON_UNESCAPED_UNICODE); } } } return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }