| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745 |
- <?php
- namespace app\modules\admin\models;
- use app\models\CashExt;
- use app\models\District;
- use app\models\Option;
- use app\models\SaasUser;
- use app\models\ShareHolder;
- use app\models\ShareHolderAreaAgentApply;
- use app\models\ShareHolderLevel;
- use app\models\User;
- use app\utils\Share\BonusPool;
- use yii\base\Model;
- class AreaDividendForm extends Model
- {
- public $id;
- public $store_id;
- public $is_open_area; //是否开启区域分红
- public $area_agent_banner_status; //是否开启申请页面banner图轮播
- public $area_agent_banner_images; //申请页面banner图
- public $area_name; //自定义区域分红名称
- public $status; //审核状态
- public $name;
- public $start_time;
- public $end_time;
- public $agent_rate;
- public $agent_rate_status;
- public $shareHolderCashProfit;
- public $pay_wechat;
- public $pay_alipay;
- public $bank;
- public $remaining_sum;
- public $lg;
- public $cash_max_day;
- public $cash_max_single_day;
- public $min_money;
- public $agent_type;
- public $province_id;
- public $city_id;
- public $district_id;
- public $town_id;
- public $cash_price_type;
- public $cash_price_amount;
- public $cash_price_integral;
- public $cash_price_balance;
- public $export;
- public function rules()
- {
- return [
- [['store_id', 'is_open_area', 'area_agent_banner_status', 'status', 'agent_rate_status', 'agent_type', 'province_id', 'city_id', 'district_id','export'], 'integer'],
- [['area_name', 'name', 'start_time', 'end_time', 'cash_price_type'], 'string'],
- [['agent_rate', 'shareHolderCashProfit', 'pay_wechat', 'pay_alipay', 'bank', 'lg', 'cash_max_day', 'cash_max_single_day', 'min_money',
- 'remaining_sum', 'cash_price_amount', 'cash_price_integral', 'cash_price_balance'], 'number'],
- [['area_agent_banner_images', 'id', 'town_id'], 'safe']
- ];
- }
- //获取配置信息
- public function getAreaAgentConfig() {
- // 区域分红状态
- $is_open_area = Option::get('is_open_area', $this->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()
- ];
- }
- }
- }
|