asJson([ 'code' => 1, 'msg' => '参数错误' ]); } $province_name = District::findOne($province)->name ?: ''; $city_name = District::findOne($city)->name ?: ''; $district_name = District::findOne($district)->name ?: ''; if (empty($province_name) || empty($city_name) || empty($district_name)) { return $this->asJson([ 'code' => 1, 'msg' => '参数错误' ]); } // TODO: 申请股东的条件 $check_res = BonusPool::checkApply($store_id, $user_id); if ($check_res['code'] > 0) { return $this->asJson($check_res); } $admin = ShareHolder::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'is_delete' => 0])->andWhere(['in', 'status', [1, 0]])->one(); if ($admin) { return $this->asJson([ 'code' => 1, 'msg' => '当前已成为股东或存在待审核记录' ]); } $share_level = ShareHolderLevel::findOne(['is_default' => 1, 'store_id' => get_store_id(), 'is_delete' => 0]); $share_holder = new ShareHolder(); $share_holder->level_id = $share_level->id; $share_holder->store_id = get_store_id(); $share_holder->user_id = $user_id; $share_holder->name = $name; $share_holder->address = $address; $share_holder->mobile = $mobile; $share_holder->province = $province; $share_holder->city = $city; $share_holder->district = $district; $share_holder->province_name = $province_name; $share_holder->city_name = $city_name; $share_holder->district_name = $district_name; if ($share_holder->save()) { return $this->asJson([ 'code' => 0, 'msg' => '提交成功' ]); } return $this->asJson([ 'code' => 1, 'msg' => $share_holder->errors[0] ]); } /** * 等级 * @return \yii\web\Response */ public function actionHolderLevel() { $user_id = get_user_id(); $store_id = get_store_id(); $share_holder = ShareHolder::findOne(['store_id' => $store_id, 'user_id' => $user_id, 'is_delete' => 0, 'status' => 1]); if (!$share_holder) { return $this->asJson([ 'code' => 1, 'msg' => '不好意思,您不是股东', ]); } $share_holder_level = ShareHolderLevel::findOne($share_holder->level_id); if (!$share_holder_level) { return $this->asJson([ 'code' => 1, 'msg' => '当前数据不存在', ]); } $share_holder_level_arr = ShareHolderLevel::find()->where(['is_delete' => 0, 'store_id' => $store_id])->orderBy('level asc')->asArray()->all(); foreach ($share_holder_level_arr as &$level) { $tmp = []; $condition = Json::decode($level['condition']); //佣金总额 $amount_total = $condition['amount_total']; if (intval($amount_total['is_open'])) { $msg = "佣金总额达到"; $title = "佣金总额"; $total_price = get_user()->total_price; if ($total_price < $amount_total['value']) { $is_ok = 0; } else { $is_ok = 1; } $tmp[] = [ 'status' => $is_ok, 'msg' => $msg, 'title' => $title, 'icon' => 'amount_total', 'require' => $amount_total['value'], 'value' => $total_price ?: 0 ]; } $man = $condition['man']; if ((bool)$man['is_open']) { $share_user_parent_tree = OldUserTreePath::findOne(['parent_id' => $user_id]);//获取当前购买用户的当前等级 foreach ((array)$man['value'] as $m) { $man_data = OldUserTreePath::find()->where([ 'parent_id' => $user_id ])->andWhere([ '!=', 'child_id', $user_id ])->andWhere([ 'AND', [ '>', 'child_level', $share_user_parent_tree->parent_level ], [ '<=', 'child_level', ($share_user_parent_tree->parent_level + (int)$m['level']) ] ])->asArray()->all(); $man_data_count = count($man_data); // $msg = $m['level'] . '级团队总人数达到' . $m['count']; // $man_data = OldUserTreePath::find()->where([ // 'parent_id' => $user_id // ])->andWhere([ // '!=', // 'child_id', // $user_id // ])->asArray()->all(); $man_data_count = $man_data_count ?: 0; $msg = "累计人数"; $title = "团队人数"; if ($man_data_count < $m['count']) { $is_ok = 0; } else { $is_ok = 1; } $tmp[] = [ 'status' => $is_ok, 'msg' => $msg, 'title' => $title, 'icon' => 'man', 'require' => $m['count'], 'value' => $man_data_count, ]; if (isset($m['money'])) { //消费达到$m['money']以上允许升级 $child_id = array_column($man_data, 'child_id'); $pay_price = Order::find()->where( [ 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM, 'user_id' => $child_id ]) ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])->sum('pay_price'); $is_ok = 1; if ($m['money'] > $pay_price) { $is_ok = 0; } $tmp[] = [ 'status' => $is_ok, 'msg' => $m['level'] . '级内团队人数' . $m['count'] . '且团队消费', 'title' => '团队人数&消费金额', 'icon' => 'man', 'require' => $m['money'], 'value' => $pay_price ?: 0, ]; } } } // 团队消费金额 $order = $condition['order']; if ((bool)$order['is_open']) { $share_user_parent_tree = OldUserTreePath::findOne(['parent_id' => $user_id]);//获取当前购买用户的当前等级 foreach ($order['value'] as $o) { $man_data = OldUserTreePath::find()->where([ 'parent_id' => $user_id ])->andWhere([ '!=', 'child_id', $user_id ])->andWhere([ 'AND', [ '>=', 'child_level', $share_user_parent_tree->parent_level ], [ '<=', 'child_level', ($share_user_parent_tree->parent_level + ($o['level'] ?: 0)) ] ])->asArray()->all(); // $man_user_id = array_column($man_data, 'id'); // 删除自己会员ID // $man_user_id = array_diff($man_user_id, [$user_id]); $man_user_id = array_column($man_data, 'child_id'); $man_user_id[] = $user_id; $order_money = Order::find() ->where(['store_id' => $store_id, 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM]) ->andWhere(['in', 'user_id', $man_user_id]) ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]]) ->sum('pay_price'); $msg = $o['level'] . '级内团队总订单金额'; // $msg = "总订单累计"; $title = "团队订单"; if ($order_money < $o['count']) { $is_ok = 0; } else { $is_ok = 1; } $tmp[] = [ 'status' => $is_ok, 'msg' => $msg, 'title' => $title, 'icon' => 'order', 'require' => $o['count'], 'value' => $order_money ?: 0 ]; } } // 自身消费金额 $self = $condition['self']; if ((bool)$self['is_open']) { // 兼容老版本 if (!isset($self['value']['order_number'])) { $self_price = $self['value']; $self['value'] = []; $self['value']['order_number'] = 0; $self['value']['price'] = $self_price; } if ($self['value']['order_number'] == 1) { $order = Order::find() ->where(['store_id' => $store_id, 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM, 'user_id' => $user_id]) ->andWhere(['>=' , 'pay_price', $self['value']['price']]) ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]]) ->one(); // $msg = '自己消费单笔金额达到' . $self['value']['price'] . '元'; $msg = "自己单笔消费金额"; $title = "账户单笔消费"; $max_order = Order::find() ->where(['store_id' => $store_id, 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM, 'user_id' => $user_id]) ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]]) ->orderBy('pay_price desc')->asArray()->one(); if (!$order) { $is_ok = 0; } else { $is_ok = 1; } $tmp[] = [ 'status' => $self['value']['price'] == 0 ? 1 : $is_ok, 'msg' => $msg, 'title' => $title, 'icon' => 'self', 'require' => $self['value']['price'], 'value' => $max_order ? $max_order['pay_price'] : 0, ]; } else { $order_money = Order::find() ->where(['store_id' => $store_id, 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM, 'user_id' => $user_id]) ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]]) ->sum('pay_price'); // $msg = '自己消费金额达到' . $self['value']['price'] . '元'; $msg = "自己消费金额"; $title = "账户消费"; if ($order_money < $self['value']['price']) { $is_ok = 0; } else { $is_ok = 1; } $tmp[] = [ 'status' => $is_ok, 'msg' => $msg, 'title' => $title, 'icon' => 'self', 'require' => $self['value']['price'], 'value' => $order_money ?: 0 ]; } } // 自身充值金额 $recharge = $condition['recharge']; if ((bool)$recharge['is_open']) { $recharge_money = ReOrder::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'is_pay' => 1])->sum('pay_price'); // $msg = '充值金额达到' . $recharge['value'] . '元'; $msg = "充值金额"; $title = "账户充值"; if ($recharge_money < $recharge['value']) { $is_ok = 0; } else { $is_ok = 1; } $tmp[] = [ 'status' => $is_ok, 'msg' => $msg, 'title' => $title, 'icon' => 'recharge', 'require' => $recharge['value'], 'value' => $recharge_money ?: 0 ]; } // 购买指定商品 $goods = $condition['goods']; if ((bool)$goods['is_open']) { // 查找该用户是否买过此商品 $goods_model = Order::find()->alias('o') ->leftJoin(['od' => OrderDetail::tableName()], 'od.order_id=o.id') ->where(['o.store_id' => $store_id, 'o.user_id' => $user_id, 'o.is_delete' => 0, 'o.trade_status' => Order::ORDER_FLOW_CONFIRM]) ->andWhere(['or', ['o.is_pay' => 1], ['o.pay_type' => 2]]) ->andWhere(['od.goods_id' => $goods['value']['id']])->one(); $goods_order_count = Order::find()->alias('o') ->leftJoin(['od' => OrderDetail::tableName()], 'od.order_id=o.id') ->where(['o.store_id' => $store_id, 'o.user_id' => $user_id, 'o.is_delete' => 0, 'o.trade_status' => Order::ORDER_FLOW_CONFIRM]) ->andWhere(['or', ['o.is_pay' => 1], ['o.pay_type' => 2]]) ->andWhere(['od.goods_id' => $goods['value']['id']])->count(); $msg = '购买指定商品'; $title = "购买商品"; // 是否过了售后期 $pay_time = $goods_model->pay_time; if (!$goods_model) { $is_ok = 0; } else { if (time() < ($pay_time + $goods['day'] * 24 * 3600)) { $is_ok = 0; } else { $is_ok = 1; } } $tmp[] = [ 'goods_id' => $goods['value']['id'], 'status' => $is_ok, 'msg' => $msg, 'title' => $title, 'icon' => 'goods', 'require' => 1, 'value' => $goods_order_count ?: 0 ]; } // 等级升级 $shareholder = $condition['shareholder']; if ($shareholder['is_open']) { $num = User::find()->alias('u') ->leftJoin(['h' => ShareHolder::tableName()], 'h.user_id = u.id') ->where([ 'u.old_parent_id' => $user_id, 'h.status' => 1, 'h.is_delete' => 0, ]) ->andWhere(['>=', 'h.level_id', $condition->shareholder->to_level_id]) ->limit($condition->shareholder->num)//感觉不用加这个 下面有数组截取 但是不加怕查询太慢 ->orderBy('h.created_at ASC') ->select('u.id') ->count(); $share_level = ShareHolderLevel::findOne(['id' => $shareholder['to_level_id'], 'store_id' => $store_id, 'is_delete' => 0]); // $msg = '您在本商城发展的股东达到' . $shareholder['num'] . '位等级为'.$share_level->name; $msg = "升级为" . $share_level->name; $title = "团队下级账户升级"; $tmp[] = [ 'status' => $num >= $shareholder['num'], 'msg' => $msg, 'title' => $title, 'icon' => 'man', 'require' => $shareholder['num'], 'value' => $num ]; } $level['text'] = $tmp; } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'level_id' => $share_holder->level_id, 'level_res' => $share_holder_level_arr ] ]); } /** * 团队 * @return \yii\web\Response */ public function actionGroup() { $user_id = get_user_id(); $store_id = get_store_id(); $type = (int)get_params('type'); $team_num = Option::get('team_num', get_store_id(), 'bonus_pool', 0)['value']; \Yii::warning([$user_id, $store_id]); if ($type === 1) {//团队数据/团队中心—链动 $share_user_parent_tree = UserTreePath::findOne(['parent_id' => $user_id]);//获取当前购买用户的当前等级 $man_data = UserTreePath::find()->where([ 'parent_id' => $user_id ])->andWhere([ '!=', 'child_id', $user_id ])->andWhere([ 'AND', [ '>=', 'child_level', $share_user_parent_tree->parent_level ], [ '<=', 'child_level', ($share_user_parent_tree->parent_level + ($team_num ?: 1)) ] ])->asArray()->all(); $child_user_id = array_column($man_data, 'child_id'); $list = User::find()->where(['id' => $child_user_id]) ->select('id, binding, nickname, avatar_url, store_id, parent_id') ->asArray()->all(); // $list = User::find()->alias('u')->where(['u.store_id' => $store_id, 'u.parent_id' => $user_id, 'sh.is_delete' => 0, 'sh.status' => 1]) // ->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile') // ->leftJoin(['sh' => ShareHolder::tableName()], 'sh.user_id = u.id') // ->select('u.id, u.binding, u.nickname, u.avatar_url, u.store_id, u.parent_id') // ->asArray()->all(); } elseif ($type === 2) { $list = User::find()->alias('u')->where(['u.old_parent_id' => $user_id, 'sh.is_delete' => 0, 'sh.is_out' => 1])->andWhere(['IS NOT', 'sh.id', NULL]) ->leftJoin(['sh' => ShareHolder::tableName()], 'u.id = sh.user_id') // ->leftJoin(['u_' => User::tableName()], 'sh.child_user_id = u_.id') ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding') ->select('u.id, u.binding, u.nickname, u.avatar_url, u.store_id, u.parent_id') ->groupBy('u.id')->asArray()->all(); foreach ($list as &$item) { $item['parent_id'] = 0; } $user_id = 0; } else {//股东中心-原推荐关系 $share_user_parent_tree = OldUserTreePath::findOne(['parent_id' => $user_id]);//获取当前购买用户的当前等级 $man_data = OldUserTreePath::find()->where([ 'parent_id' => $user_id ])->andWhere([ '!=', 'child_id', $user_id ])->andWhere([ 'AND', [ '>=', 'child_level', $share_user_parent_tree->parent_level ], [ '<=', 'child_level', ($share_user_parent_tree->parent_level + ($team_num ?: 1)) ] ])->asArray()->all(); $child_user_id = array_column($man_data, 'child_id'); $list = User::find()->where(['id' => $child_user_id])->select('id, binding, nickname, avatar_url, store_id, old_parent_id parent_id') ->asArray()->all(); } $tree = UserTreePath::formatChildren($list, $user_id); return $this->asJson([ 'code' => 0, 'data' => [ 'res' => $tree, 'count' => UserTreePath::$team_count ?: 0 ] ]); } /** * 极差分润订单 * @return \yii\web\Response */ public function actionShareOrder() { $order_no = get_params('order_no'); $name = get_params('name'); $time = get_params('time'); $page = get_params('page', 1); $limit = get_params('limit', 10); $type = get_params('type'); // $query = ShareDetail::find()->alias('sd')->leftJoin(['o' => Order::tableName()], 'o.id=sd.type_id') // ->leftJoin(['u' => User::tableName()], 'u.id=o.user_id') // ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding') // ->where(['sd.store_id' => get_store_id(), 'sd.is_delete' => 0, 'sd.type' => [ShareDetail::TYPE_RANGE_PROFIT, ShareDetail::TYPE_RANGE_PROFIT_OLD, ShareDetail::TYPE_RANGE_PROFIT_OLD_USER_LEVEL], 'sd.user_id' => get_user_id()]); // if ($name) { // $query->andWhere(['like', 'su.name', $name]); // } // if ($order_no) { // $query->andWhere(['o.order_no' => $order_no]); // } // if ($type) { // $query->andWhere(['sd.type' => [2, 5, 6, 9]]); // } // if ($time) { // $query->andWhere([ // 'and', // ['>=', 'o.created_at', strtotime($time . '-01')], // ['<=', 'o.created_at', strtotime(date('Y-m-d', strtotime($time . '-01 +1 month -1 day')))] // ]); // } // $total = $query->sum('sd.money'); //获取团队订单金额数据 $team_num = Option::get('team_num', get_store_id(), 'bonus_pool', 0)['value']; $team_list = User::find()->childs(get_user_id(), true, $team_num)->andWhere(['store_id' => get_store_id()])->select('id')->column(); $query = Order::find()->alias('o') ->leftJoin(['u' => User::tableName()], 'u.id=o.user_id') ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding') ->where(['o.store_id' => get_store_id(), 'o.user_id' => $team_list, 'o.is_delete' => 0, 'o.trade_status' => [Order::ORDER_FLOW_NO_SEND, Order::ORDER_FLOW_SEND, Order::ORDER_FLOW_CONFIRM]]); if ($name) { $query->andWhere(['like', 'su.name', $name]); } if ($order_no) { $query->andWhere(['o.order_no' => $order_no]); } if ($time) { $query->andWhere([ 'and', ['>=', 'o.created_at', strtotime($time . '-01')], ['<=', 'o.created_at', strtotime(date('Y-m-d', strtotime($time . '-01 +1 month -1 day')))] ]); } $total = $query->sum('o.total_price'); $count = $query->count(); $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $limit, 'page' => $page - 1]); $list = $query->select('su.name nickname, su.avatar, o.id, o.pay_time, o.order_no, o.pay_price, o.total_price range_profit, o.is_pay, o.trade_status')->orderBy('o.created_at desc')->groupBy('o.id') ->limit($pagination->limit) ->offset($pagination->offset)->asArray()->all(); foreach ($list as &$value) { $value['goods_list'] = OrderDetail::find()->where(['order_id' => $value['id']])->select('goods_name, num, attr, pic, total_price')->asArray()->all(); } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'total' => $total, 'row_count' => $count, 'page_count' => $pagination->pageCount, 'list' => $list, ] ]); } /** * 极差分润订单 * @return \yii\web\Response */ public function actionShareOrderList() { $order_no = input_params('order_no'); $name = input_params('name'); $type = input_params('type');// 区分链动 $is_area = input_params('is_area', 1); //只显示区域分红 $trade_status = input_params('trade_status'); $query = Order::find()->alias('o')->leftJoin(['sd' => ShareDetail::tableName()], 'sd.type_id = o.id') ->leftJoin(['u' => User::tableName()], 'u.id=o.user_id') ->where(['o.store_id' => get_store_id()]) ->andWhere(['<>', 'o.trade_status', 1]); $query->andWhere(['sd.user_id' => get_user_id()]); if($trade_status > -1){ $query->andWhere(['o.trade_status' => $trade_status]); } if ($name) { $query->andWhere(['like', 'u.nickname', $name]); } $type_where = ['in', 'sd.type', [2, 5, 6, 7, 8, 9]]; if ($type) { $type_where = ['in', 'sd.type', [2, 5, 6, 9]]; } if ($is_area) { $type_where = ['AND', ['sd.type' => 3], ['>', 'sd.money', '0']]; } $type_where = ['OR', $type_where, ['OR', ['>', 'holder_first_price', 0], ['>', 'holder_second_price', 0], ['>', 'holder_third_price', 0], ['>', 'old_holder_first_price', 0], ['>', 'old_holder_second_price', 0], ['>', 'old_holder_third_price', 0]]]; $query->andWhere($type_where); if ($order_no) { $query->andWhere(['o.order_no' => $order_no]); } $query->select('o.id, o.pay_time created_at, o.trade_status, sd.money, o.id type_id, u.nickname, u.avatar_url, o.pay_time, o.order_no, o.id order_id, o.holder_first_price, o.holder_second_price, o.holder_third_price, o.old_holder_first_price, o.old_holder_second_price, o.old_holder_third_price,o.express_price') ->orderBy('o.id desc')->groupBy('o.id'); $list = pagination_make($query); foreach ($list['list'] as &$value) { $value['order_pay_price'] = 0; $order = Order::find()->select('pay_price')->where(['order_no' => $value['order_no']])->one(); if ($order) { $value['order_pay_price'] = $order->pay_price; } $value['goods_list'] = OrderDetail::find()->where(['order_id' => $value['type_id']])->select('goods_name, num, attr, pic, total_price')->asArray()->all(); if ($type) { $value['range_profit'] = ShareDetail::find()->where(['type' => ShareDetail::TYPE_RANGE_PROFIT, 'type_id' => $value['type_id']])->sum('money'); } else { $value['range_profit'] = ShareDetail::find()->where(['type' => [ShareDetail::TYPE_RANGE_PROFIT, ShareDetail::TYPE_RANGE_PROFIT_OLD, ShareDetail::TYPE_RANGE_PROFIT_OLD_USER_LEVEL], 'type_id' => $value['type_id']])->sum('money'); } $value['agent_profit'] = ShareDetail::find()->where(['type' => ShareDetail::TYPE_AREA_PROFIT, 'type_id' => $value['type_id']])->sum('money'); $share_holder_price = bcadd($value['old_holder_first_price'], bcadd($value['old_holder_second_price'], bcadd($value['old_holder_third_price'], bcadd($value['holder_first_price'], bcadd($value['holder_second_price'], $value['holder_third_price'], 2) , 2) , 2) , 2) , 2); $value['share_holder_price'] = '¥' . $share_holder_price; } return $this->asJson([ 'q' => $query->createCommand()->getRawSql(), 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $list['list'], 'pageNo' => $list['pageNo'], 'totalCount' => $list['totalCount'] ] ]); } /** * 股东分销 * @return \yii\web\Response */ public function actionHolderShareOrder() { $time = get_params('time'); $page = get_params('page', 1); $limit = get_params('limit', 10); $user_id = get_user_id(); $query = Order::find()->where(['store_id' => get_store_id(), 'trade_status' => [ Order::ORDER_FLOW_NO_SEND, Order::ORDER_FLOW_SEND, Order::ORDER_FLOW_CONFIRM ]])->andWhere(['OR', ['AND', ['old_parent_id' => $user_id], ['>', 'old_holder_first_price', '0']], ['AND', ['old_parent_id_1' => $user_id], ['>', 'old_holder_second_price', '0']], ['AND', ['old_parent_id_2' => $user_id], ['>', 'old_holder_third_price', '0']], ['AND', ['parent_id' => $user_id], ['>', 'holder_first_price', '0']], ['AND', ['parent_id_1' => $user_id], ['>', 'holder_second_price', '0']], ['AND', ['parent_id_2' => $user_id], ['>', 'holder_third_price', '0']] ]); if ($time) { $query->andWhere([ 'and', ['>=', 'created_at', strtotime($time . '-01')], ['<=', 'created_at', strtotime(date('Y-m-d', strtotime($time . '-01 +1 month -1 day')))] ]); } $total_data = (clone $query)->select('old_parent_id, old_holder_first_price, old_parent_id_1, old_holder_second_price, old_parent_id_2, old_holder_third_price, parent_id, holder_first_price, parent_id_1, holder_second_price, parent_id_2, holder_third_price')->asArray()->all(); $total = 0; foreach ($total_data as $total_item) { if ($total_item['old_parent_id'] == $user_id) { $total = bcadd($total, $total_item['old_holder_first_price'], 2); } if ($total_item['old_parent_id_1'] == $user_id) { $total = bcadd($total, $total_item['old_holder_second_price'], 2); } if ($total_item['old_parent_id_2'] == $user_id) { $total = bcadd($total, $total_item['old_holder_third_price'], 2); } if ($total_item['parent_id'] == $user_id) { $total = bcadd($total, $total_item['holder_first_price'], 2); } if ($total_item['parent_id_1'] == $user_id) { $total = bcadd($total, $total_item['holder_second_price'], 2); } if ($total_item['parent_id_2'] == $user_id) { $total = bcadd($total, $total_item['holder_third_price'], 2); } } $query->select('id, pay_time, order_no, pay_price, is_pay, trade_status, old_parent_id, old_holder_first_price, old_parent_id_1, old_holder_second_price, old_parent_id_2, old_holder_third_price, parent_id, holder_first_price, parent_id_1, holder_second_price, parent_id_2, holder_third_price')->orderBy('created_at desc'); $list = pagination_make($query); foreach ($list['list'] as &$value) { $value['goods_list'] = OrderDetail::find()->where(['order_id' => $value['id']]) ->select('goods_name, num, attr, pic, total_price')->asArray()->all(); $value['money'] = 0; if ($value['old_parent_id'] == $user_id) { $value['source'] = 1; $value['money'] = bcadd($value['money'], $value['old_holder_first_price'], 2); } if ($value['old_parent_id_1'] == $user_id) { $value['source'] = 2; $value['money'] = bcadd($value['money'], $value['old_holder_second_price'], 2); } if ($value['old_parent_id_2'] == $user_id) { $value['source'] = 3; $value['money'] = bcadd($value['money'], $value['old_holder_third_price'], 2); } if ($value['parent_id'] == $user_id) { $value['source'] = 1; $value['money'] = bcadd($value['money'], $value['holder_first_price'], 2); } if ($value['parent_id_1'] == $user_id) { $value['source'] = 2; $value['money'] = bcadd($value['money'], $value['holder_second_price'], 2); } if ($value['parent_id_2'] == $user_id) { $value['source'] = 3; $value['money'] = bcadd($value['money'], $value['holder_third_price'], 2); } $is_send = UserShareMoney::find()->where([ 'type' => 2, 'user_id' => $user_id, 'order_id' => $value['id'], 'source' => $value['source'] ])->asArray()->one(); $is_send_text = '(未发放)'; if ($is_send) { $is_send_text = '(已发放)'; } $value['order_no'] .= $is_send_text; } $list['total'] = $total; return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => $list ]); } // public function actionAuditStatus() { // $user_id = get_user_id(); // $share_holder = ShareHolder::find()->where(['user_id' => $user_id, 'store_id' => get_store_id(), 'is_delete' => 0])->orderBy('id desc')->limit(1)->one(); // if (!$share_holder) { // return $this->asJson([ // 'code' => 0, // 'msg' => '没有审核记录', // 'data' => ['status' => -1] // ]); // } // return $this->asJson([ // 'code' => 0, // 'data' => ['status' => $share_holder->status] // ]); // } /** * 股东中心 * @return \yii\web\Response */ public function actionIndex() { $user_id = get_user_id(); $store_id = get_store_id(); $type = get_params('type'); $share_holder = ShareHolder::findOne(['user_id' => $user_id, 'store_id' => $store_id, 'status' => 1, 'is_delete' => 0]); if (!$share_holder && $user_id) { return $this->asJson([ 'code' => 1, 'msg' => '未有权限,请先申请成为股东' ]); } $total_price = get_user()->total_price; $saas_user = SaasUser::findOne(['mobile' => get_user()->binding]); $share_detail_query = ShareDetail::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'user_id' => $user_id]); $share_detail_query_1 = ShareDetail::find()->alias('sd')->where(['sd.store_id' => $store_id, 'sd.is_delete' => 0, 'sd.user_id' => $user_id]) // ->leftJoin(OrderRefund::tableName() . ' r', "r.order_id = sd.type_id and r.is_delete = 0") // ->andWhere([ // 'or', // 'isnull(r.id)', // ['r.type' => 2], // ['in', 'r.status', [2, 3]] // ]); ->leftJoin(['o' => Order::tableName()], 'sd.type_id = o.id')->andWhere(['!=', 'o.trade_status', 1]); if ($type) { $share_detail_query->andWhere(['type' => [2, 5, 6, 9]]); $share_detail_query_1->andWhere(['in', 'sd.type', [2, 5, 6, 9]]); } $share_detail_query->andWhere(['<=', 'type', ShareDetail::TYPE_RANGE_PROFIT_OLD_USER_LEVEL]); $share_detail_query_1->andWhere(['<=', 'sd.type', ShareDetail::TYPE_RANGE_PROFIT_OLD_USER_LEVEL]); $order_count_q = clone $share_detail_query; $team_num = Option::get('team_num', get_store_id(), 'bonus_pool', 0)['value']; $team_list = User::find()->alias('u')->childs(get_user_id(), true, $team_num)->andWhere(['u.store_id' => get_store_id()])->select('u.id')->column(); $order_count = Order::find()->alias('o') ->leftJoin(['u' => User::tableName()], 'u.id=o.user_id') ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding') ->where(['o.store_id' => get_store_id(), 'o.user_id' => $team_list, 'o.is_delete' => 0, 'o.trade_status' => [Order::ORDER_FLOW_NO_SEND, Order::ORDER_FLOW_SEND, Order::ORDER_FLOW_CONFIRM]])->count(); // $total_price = $order_count_q->sum('money'); // $cash_profit = $order_count_q->andWhere(['is_send' => 1])->sum('money'); $cash_profit = get_user()->price; if ($user_id) { $cash_price = Cash::find()->where(['store_id' => $store_id, 'user_id' => $user_id])->andWhere(['in', 'status', [2, 4]])->sum('price'); $cash_price_apply = Cash::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'cash_type' => 3])->andWhere(['in', 'status', [0, 1]])->sum('price'); // $list = User::find()->alias('u')->where(['u.store_id' => $store_id, 'u.parent_id' => $user_id])->select('u.id, u.binding, u.nickname, u.avatar_url, u.store_id, u.parent_id')->asArray()->all(); $team_count = User::find()->alias('u') ->leftJoin(['sh' => ShareHolder::tableName()], 'sh.user_id = u.id') ->where(['sh.is_delete' => 0, 'sh.status' => 1]) ->andWhere([ 'u.parent_id' => $user_id ]) ->count(); } // $cash = get_user()->price; $agent_data = []; $district = ''; if ($share_holder) { if ((int)$share_holder->agent_type === 1) { $agent_data = [$share_holder->province_id]; } elseif ((int)$share_holder->agent_type === 2) { $agent_data = [$share_holder->province_id, $share_holder->city_id]; } elseif ((int)$share_holder->agent_type === 3) { $agent_data = [$share_holder->province_id, $share_holder->city_id, $share_holder->district_id]; } } $district_ = District::find()->where(['id' => $agent_data])->select('name')->column(); if (!empty($district_)) { $district = implode('', $district_); } $data = [ 'total_price' => $total_price, 'is_send' => $share_detail_query->andWhere(['is_send' => 1])->sum('money') ?: 0, 'no_send' => $share_detail_query_1->andWhere(['is_send' => 0])->sum('money') ?: 0, 'cash_price' => $cash_price ?? 0, 'cash_price_apply' => $cash_price_apply ?? '0.00', 'avatar' => $saas_user ? $saas_user->avatar : get_user()->avatar_url, 'name' => $saas_user ? $saas_user->name : (get_user()->nickname ?: ''), 'level_name' => ShareHolderLevel::findOne($share_holder->level_id)->name ?: '', 'cash_profit' => sprintf("%.2f", $cash_profit), 'team_count' => $team_count ?? 0, 'id' => $user_id, 'order_count' => $order_count, 'agent_type' => (int)$share_holder->agent_type ?: 0, 'district' => $district ]; return $this->asJson([ 'code' => 0, 'data' => $data ]); } //团队数据 public function actionGetTeamInfo() { try { // $saas_user = SaasUser::findOne(['mobile' => get_user()->binding]); $user_id = get_user_id(); $store_id = get_store_id(); if ($user_id) { $share_holder = ShareHolder::findOne(['user_id' => $user_id, 'store_id' => $store_id, 'status' => 1, 'is_delete' => 0]); if (!$share_holder) { return $this->asJson([ 'code' => 1, 'msg' => '未有权限,请先申请成为股东' ]); } // $list = User::find()->alias('u')->where(['u.store_id' => $store_id, 'u.parent_id' => $user_id]) // ->select('u.id, u.binding, u.nickname, u.avatar_url, u.store_id, u.parent_id')->asArray()->all(); $team_count = User::find()->alias('u') ->leftJoin(['sh' => ShareHolder::tableName()], 'sh.user_id = u.id') ->where(['sh.is_delete' => 0, 'sh.status' => 1]) ->andWhere([ 'u.parent_id' => $user_id ]) ->count(); $list = User::findOld()->alias('u')->childs($user_id, false)->andWhere(['u.store_id' => $store_id])->select('u.id, u.binding, u.nickname, u.avatar_url, u.store_id, u.parent_id')->asArray()->all(); UserTreePath::formatChildren($list, $user_id); $team_count = UserTreePath::$team_count; $shareHolderOutList = ShareHolderOutLog::find()->alias('sh')->where(['AND', ['sh.parent_user_id' => $user_id], ['IS NOT', 'u.id', NULL]]) ->leftJoin(['u' => User::tableName()], 'sh.child_user_id = u.id') ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding') ->select('su.name, su.avatar') ->asArray()->all(); // $parentOutList = User::find()->alias('u')->where(['u.old_parent_id' => get_user_id()])->andWhere(['IS NOT', 'sh.id', NULL]) // ->leftJoin(['sh' => ShareHolderOutLog::tableName()], 'u.old_parent_id = sh.parent_user_id') // ->leftJoin(['u_' => User::tableName()], 'sh.child_user_id = u_.id') // ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding') // ->select('su.name, su.avatar') // ->groupBy('u.id') // ->asArray()->all(); $parentOutList = User::find()->alias('u')->where(['u.old_parent_id' => $user_id, 'sh.is_delete' => 0, 'sh.is_out' => 1])->andWhere(['IS NOT', 'sh.id', NULL]) ->leftJoin(['sh' => ShareHolder::tableName()], 'u.id = sh.user_id') // ->leftJoin(['u_' => User::tableName()], 'sh.child_user_id = u_.id') ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding') ->select('su.name, su.avatar') ->groupBy('u.id') ->asArray()->all(); // $parentOutList = ShareHolderOutLog::find()->alias('sh')->where(['AND', ['sh.child_user_id' => $user_id], ['IS NOT', 'u.id', NULL]]) // ->leftJoin(['u' => User::tableName()], 'sh.child_user_id = u.id') // ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding') // ->select('su.name, su.avatar') // ->asArray()->all(); } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'avatar' => $saas_user ? $saas_user->avatar : get_user()->avatar_url, 'name' => $saas_user ? $saas_user->name : get_user()->nickname ?? '', 'id' => $user_id, 'team_count' => $team_count ?? '', 'parent_count' => count($parentOutList ?? []), 'team' => $shareHolderOutList ?? [], 'level_name' => ShareHolderLevel::findOne($share_holder->level_id)->name ?: '' ] ]); } catch (\Exception $e) { return $this->asJson([ 'code' => 1, 'msg' => $e->getMessage() ]); } } /** * 佣金列表 * @return \yii\web\Response */ public function actionProfitList() { $store_id = get_store_id(); $user_id = get_user_id(); $time = get_params('time'); $type = get_params('type'); $status = get_params('status'); $page = get_params('page', 1); $limit = get_params('limit', 20); //区域代理 $is_area = get_params('is_area'); $share_holder = ShareHolder::findOne(['user_id' => $user_id, 'store_id' => $store_id, 'status' => 1, 'is_delete' => 0]); if (!$share_holder && $user_id) { return $this->asJson([ 'code' => 1, 'msg' => '未有权限,请先申请成为股东' ]); } $query = ShareDetail::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'is_delete' => 0])->andWhere(['>','money',0]); if ($type) { $query->andWhere(['type' => [2, 5, 6, 9]]); } if ($is_area) { $query->andWhere(['type' => 3]); } //今日奖励 $t_query = clone $query; $today_money = $t_query->andWhere(['AND', ['>=', 'created_at', strtotime(date('Y-m-d'))], ['<', 'created_at', strtotime(date('Y-m-d') . '+1 day')] ])->select('money')->sum('money'); //总待结算金额 $no_send_query = clone $query; $no_send_money = $no_send_query->andWhere(['is_send' => 0])->select('money')->sum('money'); //总已结算金额 $send_query = clone $query; $send_money = $send_query->andWhere(['is_send' => 1])->select('money')->sum('money'); if (in_array($status, [0, 1]) && !is_null($status)) { $query->andWhere(['is_send' => $status]); } if ($time) { $query->andWhere([ 'and', ['>=', 'created_at', strtotime($time . '-01')], ['<=', 'created_at', strtotime(date('Y-m-d', strtotime($time . '-01 +1 month -1 day')))] ]); } $count = $query->count(); $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $limit, 'page' => $page - 1]); $list = $query->select('*')->orderBy('created_at desc') ->limit($pagination->limit) ->offset($pagination->offset)->asArray()->all(); $range_name = Option::get('range_name', get_store_id(), 'bonus_pool', '')['value']; $holder_name = Option::get('holder_name', get_store_id(), 'bonus_pool', '')['value']; $area_name = Option::get('area_name', get_store_id(), 'bonus_pool', '')['value']; $old_range_name = Option::get('old_range_name', get_store_id(), 'bonus_pool', '')['value']; $direct_range_name = Option::get('direct_range_name', get_store_id(), 'bonus_pool', '')['value']; $old_add_holder_name = Option::get('old_add_holder_name', get_store_id(), 'bonus_pool', '')['value']; $saas_user = get_saas_user(); foreach ($list as &$value) { $value['is_refund'] = $value['type'] == ShareDetail::TYPE_HOLDER_PROFIT ? 0 : Order::is_refund($value['store_id'], $value['type_id']); $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']); $value['send_time'] = $value['send_time'] ? date('Y-m-d H:i:s', $value['send_time']) : ''; $value['profit_name'] = '分红'; if ($value['type'] == ShareDetail::TYPE_HOLDER_PROFIT) { // $value['profit_name'] = $holder_name ?: '股东分红'; $value['profit_name'] = $holder_name ?: '股东分红'; } if ($value['type'] == ShareDetail::TYPE_RANGE_PROFIT) { // $value['profit_name'] = $range_name ?: '级差分红'; $value['profit_name'] = $range_name ?: '级差分红'; } if ($value['type'] == ShareDetail::TYPE_RANGE_PROFIT_OLD) { // $value['profit_name'] = $range_name ?: '级差分红(原关系)'; $value['profit_name'] = $old_range_name ?: '级差分红(原关系)'; } if ($value['type'] == ShareDetail::TYPE_TWO_PLUS_ONE_SHARE_TEAM_PROFIT_OLD) { // $value['profit_name'] = $range_name ?: '直属团队收益分成(原关系)'; $value['profit_name'] = $old_add_holder_name ?: '直属团队收益分成(原关系)'; } if ($value['type'] == ShareDetail::TYPE_TWO_PLUS_ONE_SHARE_TEAM_PROFIT) { // $value['profit_name'] = $range_name ?: '直属团队收益分成'; $value['profit_name'] = $direct_range_name ?: '直属团队收益分成'; } if ($value['type'] == ShareDetail::TYPE_AREA_PROFIT) { // $value['profit_name'] = $area_name ?: '区域分红'; $value['profit_name'] = $area_name ?: '区域分红'; } if ($value['type'] == ShareDetail::TYPE_FROST_PROFIT) { // $value['profit_name'] = $range_name ?: '冻结金额释放'; $value['profit_name'] = '冻结金额释放'; } if ($value['type'] == ShareDetail::TYPE_RANGE_PROFIT_OLD_USER_LEVEL) { // $value['profit_name'] = $range_name ?: '冻结金额释放'; $value['profit_name'] = '商城会员等级分红'; } $value['trade_status'] = 0; if ((int)$value['type'] !== 1) { $order = Order::findOne($value['type_id']); if ($order) { $value['trade_status'] = (int)$order->trade_status; } } } $send_type_where = []; if ($type) { $send_type_where = ['sd.type' => [2, 5, 6, 9]]; } if ($is_area) { $send_type_where = ['sd.type' => 3]; } $total = ShareDetail::find()->alias('sd') ->where(['sd.store_id' => $store_id, 'sd.is_delete' => 0, 'sd.user_id' => $user_id]) ->leftJoin(['o' => Order::tableName()], 'o.id = sd.type_id') ->andWhere(['or', ['!=', 'o.trade_status', 1], ['sd.type_id' => 0]]) ->andWhere(['<=', 'sd.type', ShareDetail::TYPE_RANGE_PROFIT_OLD_USER_LEVEL]) ->andWhere($send_type_where) ->sum('money') ?? '0.00'; return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'total' => $total, 'row_count' => $count, 'page_count' => $pagination->pageCount, 'list' => $list, 'today_money' => $today_money ?? '0.00', 'no_send_money' => $no_send_money ?? '0.00', 'send_money' => $send_money ?? '0.00', 'avatar' => $saas_user ? $saas_user->avatar : get_user()->avatar_url ?? '', 'name' => $saas_user ? $saas_user->name : get_user()->nickname ?? '', 'id' => $user_id, 'level_name' => ShareHolderLevel::findOne($share_holder->level_id)->name ?? '', ] ]); } /** * 获取佣金相关 */ public function actionGetPrice() { $store_id = get_store_id(); $user_id = get_user_id(); $is_area = input_params('is_area'); $share_holder = ShareHolder::findOne(['store_id' => $store_id, 'is_delete' => 0, 'status' => 1, 'user_id' => $user_id]); if (!$share_holder) { return $this->asJson([ 'code' => 1, 'msg' => '未拥有提现权限' ]); } $cash = get_user()->price; //区分区域代理 $groups = 'bonus_pool'; if ($is_area) { $groups = 'bonus_pool_area_agent'; if (intval($share_holder->agent_type) === 0) { return $this->asJson([ 'code' => 1, 'msg' => '未拥有提现权限' ]); } } $shareHolderCashProfit = Option::get('shareHolderCashProfit', $this->store_id, $groups, 0)['value']; $res = [ 'cash_profit' => $cash, 'shareHolderCashProfit' => $shareHolderCashProfit ]; // 新提现方式 $res['cash_method_list'] = []; $saas_user = get_saas_user(); $cash_method = []; $lg = Lg::find()->where(['store_id' => $this->store_id,'user_id'=>$saas_user->id,'status'=>1,'is_delete'=>0])->one(); if (!empty($saas_user->withdraw_method)) { $cash_method = json_decode($saas_user->withdraw_method, true); if ($lg){ //灵工 $cash_method = array_merge($cash_method, [[ 'type' => 'lg' ]]); } if (is_array($cash_method)) { foreach($cash_method as $i => $item){ if ($item['type'] === 'wechat' && Option::get('pay_wechat', $this->store_id, $groups, 0)['value'] == 0) { unset($cash_method[$i]); } if ($item['type'] === 'alipay' && Option::get('pay_alipay', $this->store_id, $groups, 0)['value'] == 0) { unset($cash_method[$i]); } if ($item['type'] === 'bank_card' && Option::get('bank', $this->store_id, $groups, 0)['value'] == 0) { unset($cash_method[$i]); } if ($item['type'] === 'lg' && Option::get('lg', $this->store_id, $groups, 0)['value'] == 0) { unset($cash_method[$i]); } } } }else{ if($lg){ //灵工 $cash_method[]['type'] = 'lg'; $res['cash_method_list'] = array_values($cash_method); } } if(Option::get('remaining_sum', $this->store_id, $groups, 0)['value'] == 1){ $cash_method = array_merge($cash_method, [['type' => 'money']]); } $res['cash_method_list'] = array_values($cash_method); return $this->asJson(['code' => 0, 'msg' => 'success', 'data' => $res]); } /** * 获取提现方式 */ public function actionGetCashMethod() { $saas_user = get_saas_user(); $cash_method = []; $is_area = input_params('is_area'); $groups = 'bonus_pool'; //区分区域代理 if ($is_area) { $groups = 'bonus_pool_area_agent'; } $payTypeList = [ [ 'name' => '微信', 'is_show' => intval(Option::get('pay_wechat', $this->store_id, $groups, 0)['value']), 'type' => 'wechat', 'is_bind' => false ], [ 'name' => '支付宝', 'is_show' => intval(Option::get('pay_alipay', $this->store_id, $groups, 0)['value']), 'type' => 'alipay', 'is_bind' => false ], [ 'name' => '银行卡', 'is_show' => intval(Option::get('bank', $this->store_id, $groups, 0)['value']), 'type' => 'bank_card', 'is_bind' => false ], [ 'name' => '灵工', 'is_show' => intval(Option::get('lg', $this->store_id, $groups, 0)['value']), 'type' => 'lg', 'is_bind' => false ], ]; $lg = Lg::find()->where(['store_id' => $this->store_id,'user_id'=>$saas_user->id,'status'=>1,'is_delete'=>0])->one(); if (!empty($saas_user->withdraw_method)) { $decode = json_decode($saas_user->withdraw_method, true); if ($lg){ //灵工 $decode = array_merge($decode, [[ 'type' => 'lg' ]]); } if (is_array($decode)) { $cash_method = $decode; foreach ($payTypeList as &$pay_item) { foreach ($cash_method as $item) { if ($item['type'] === $pay_item['type']) { $pay_item['is_bind'] = true; $pay_item = array_merge($pay_item, $item); } } } } }else{ if($lg){ //灵工 $cash_method[]['type'] = 'lg'; $res['cash_method_list'] = array_values($cash_method); } } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'list' => $payTypeList, ], ]); } /** * 获取提现方式 */ public function actionDelCashMethod() { $saas_user = get_saas_user(); $type = post_params('type'); if (empty($type)) { return $this->asJson([ 'code' => 1, 'msg' => '参数错误', ]); } if ($type == 'lg'){ $lg = Lg::find()->where(['user_id'=>$saas_user->id,'is_delete'=>0])->one(); if ($lg){ $lg->is_delete = 1; $lg->save(); return $this->asJson([ 'code' => 0, 'msg' => '操作成功', ]); } } if (empty($saas_user->withdraw_method)) { return $this->asJson([ 'code' => 0, 'msg' => '操作成功', ]); } $cash_method = json_decode($saas_user->withdraw_method, true); if (is_array($cash_method)) { foreach ($cash_method as $key => $value) { if ($value['type'] == $type) { unset($cash_method[$key]); } } $saas_user->withdraw_method = json_encode(array_values($cash_method)); } else { $saas_user->withdraw_method = ''; } if ($saas_user->save()) { return $this->asJson([ 'code' => 0, 'msg' => '操作成功', ]); } return $this->asJson([ 'code' => 0, 'msg' => '操作失败', ]); } /** * 添加提现方式 */ public function actionAddCashMethod() { $type = post_params('type'); if (empty($type)) { return $this->asJson([ 'code' => 1, 'msg' => '参数错误', ]); } $name = post_params('name'); $account = post_params('account'); $bank = post_params('bank'); $cash_img = post_params('cash_img'); if (empty($name) || empty($account)) { return $this->asJson([ 'code' => 1, 'msg' => '账号和姓名不能为空', ]); } if ($type == 'bank_card' && empty($bank)) { return $this->asJson([ 'code' => 1, 'msg' => '银行不能为空', ]); } $data = [ 'type' => $type, 'name' => $name, 'account' => $account, 'cash_img' => $cash_img, ]; if ($type == 'bank_card') { $data['bank'] = $bank; } $saas_user = get_saas_user(); if (empty($saas_user->withdraw_method)) { $saas_user->withdraw_method = json_encode(array_values([$data])); } else { $decode = json_decode($saas_user->withdraw_method, true); if (!$decode) { $saas_user->withdraw_method = json_encode(array_values([$data])); } else { foreach ($decode as $key => $value) { if ($value['type'] == $type) { unset($decode[$key]); break; } } $decode[] = $data; $saas_user->withdraw_method = json_encode(array_values($decode)); } } if ($saas_user->save()) { return $this->asJson([ 'code' => 0, 'msg' => '操作成功', ]); } return $this->asJson([ 'code' => 1, 'msg' => '操作失败', ]); } public function actionCashApply() { checkReplay([get_user_id()]); $price = post_params('cash'); $type = post_params('type'); $is_area = post_params('is_area'); if (!$price || !in_array($type, ['alipay', 'bank_card', 'wechat', 'money','lg'])) { return $this->asJson([ 'code' => 1, 'msg' => '参数非法', ]); } $user_id = get_user_id(); $user = get_user(); $store_id = get_store_id(); $groups = 'bonus_pool'; $cash_type = 3; if ($is_area) { $groups = 'bonus_pool_area_agent'; $cash_type = Cash::IS_CASH_TYPE_AREA_AGENT; } $cash_max_day = Option::get('cash_max_day', $store_id, $groups, 0)['value']; $cash_max_single_day = Option::get('cash_max_single_day', $store_id, $groups, 0)['value']; $min_money = Option::get('min_money', $store_id, $groups, 0)['value']; if($price < $min_money){ return $this->asJson([ 'code' => 1, 'msg' => '最小提现'. $min_money .'元' ]); } $cash_sum = Cash::find()->where([ 'store_id' => $store_id, 'is_delete' => 0, 'status' => [0, 1, 2, 5, 4], 'cash_type' => $cash_type, 'user_id' => $user_id ])->andWhere([ 'AND', ['>=', 'created_at', strtotime(date('Y-m-d 00:00:00'))], ['<=', 'created_at', strtotime(date('Y-m-d 23:59:59'))], ])->sum('price'); if ($cash_max_day) { $cash_max_day = $cash_max_day - ($cash_sum ?: 0); if ($price > $cash_max_day) { return $this->asJson([ 'code' => 1, 'msg' => '提现金额不能超过' . $cash_max_day . '元' ]); } } if ($cash_max_single_day) { $cash_max_single_day_ = $cash_max_single_day - ($cash_sum ?: 0); if ($price > $cash_max_single_day_) { return $this->asJson([ 'code' => 1, 'msg' => '每人每天提现金额不能超过' . $cash_max_single_day . '元' ]); } } //提现手续费金额 $shareHolderCashProfit = Option::get('shareHolderCashProfit', get_store_id(), $groups, 0)['value']; $share_holder = ShareHolder::findOne(['store_id' => $store_id, 'is_delete' => 0, 'status' => 1, 'user_id' => $user_id]); if (!$share_holder) { return $this->asJson([ 'code' => 1, 'msg' => '未拥有提现权限' ]); } if ($is_area) { if (intval($share_holder->agent_type) === 0) { return $this->asJson([ 'code' => 1, 'msg' => '未拥有提现权限' ]); } } //计算提现手续费金额 // $shareHolderCashPrice = $price * ($shareHolderCashProfit / 100); if ($price > $user->price) { return $this->asJson([ 'code' => 1, 'msg' => '当前大于可提现金额' ]); } $exit = Cash::find()->andWhere(['=', 'status', 0])->andWhere(['user_id' => $user_id, 'store_id' => $store_id, 'cash_type' => $cash_type])->exists(); if ($exit) { return $this->asJson([ 'code' => 1, 'msg' => '尚有未完成的提现申请' ]); } $t = \Yii::$app->db->beginTransaction(); $cash = new Cash(); $cash->order_no = OrderNo::getOrderNo(OrderNo::ORDER_CASH); $cash->is_delete = 0; $cash->status = 0; $cash->price = $price; $cash->created_at = time(); $cash->user_id = $user_id; $cash->store_id = $store_id; $saas_user = get_saas_user(); if (!empty($saas_user->withdraw_method)) { $withdraw_method = Json::decode($saas_user->withdraw_method); $withdraw_method = array_column($withdraw_method, null, 'type'); if ($type == 'wechat') { $cash->type = 0; $cash->name = $withdraw_method['wechat']['name']; $cash->mobile = $withdraw_method['wechat']['account']; } if ($type == 'alipay') { $cash->type = 1; $cash->name = $withdraw_method['alipay']['name']; $cash->mobile = $withdraw_method['alipay']['account']; } if ($type == 'bank_card') { $cash->type = 2; $cash->name = $withdraw_method['bank_card']['name']; $cash->mobile = $withdraw_method['bank_card']['account']; $cash->bank_name = $withdraw_method['bank_card']['bank']; $cash->bank_branch = $withdraw_method['bank_card']['branch']; } if ($type == 'lg') { //灵工提现 $lg_info = Lg::find()->where(['user_id'=>$saas_user->id,'is_delete'=>0])->one(); $cash->type = 4; $cash->name = $lg_info->name; $cash->mobile = $lg_info->mobile; } }else{ if ($type == 'lg') { //灵工提现 $lg_info = Lg::find()->where(['user_id'=>$saas_user->id,'is_delete'=>0])->one(); $cash->type = 4; $cash->name = $lg_info->name; $cash->mobile = $lg_info->mobile; } } if ($type == 'money') { $cash->type = 3; } $cash->pay_time = 0; $cash->service_charge = $type == 'money' ? 0 : $shareHolderCashProfit; $cash->cash_type = $cash_type; if ($cash->save()) { $user->price -= $cash->price; if (!$user->save()) { $t->rollBack(); return $this->asJson([ 'code' => 1, 'msg' => $user->errors ]); } $auto_money = Option::get('auto_money', $store_id, $groups, 0)['value']; if ($cash_type == 3 && ($cash->type == Cash::TYPE_RECHARGE || ($price <= $auto_money && $auto_money > 0))) { // TODO 在设置金额内,股东 / 2+1佣金提现自动打款 $this->cashConfirm($cash->id, $cash->type == Cash::TYPE_RECHARGE ? 4 : 2, $store_id); } $t->commit(); return $this->asJson([ 'code' => 0, 'msg' => '申请成功' ]); } else { $t->rollBack(); return $this->asJson([ 'code' => 1, 'msg' => $cash->errors ]); } } /** * 打款 * @return \yii\web\Response */ public function cashConfirm($id, $status, $store_id) { $store = Store::findOne($store_id); $cash = Cash::findOne([ 'id' => $id, 'is_delete' => Cash::IS_DELETE_NO, 'store_id' => $store_id ]); if($status == Cash::STATUS_GIVEN){ $store_wechat_cash = json_decode(Option::get('store_wechat_cash', $store_id, 'store')['value'], true) ?: []; $val = 0; foreach ($store_wechat_cash as $value) { if (empty($value)) { $val = 1; break; } else { $val = 2; break; } } if ($store->is_platform_transfers == 0 && $val == 2) { return [ 'code' => 1, 'msg' => '未开启平台转账' ]; } elseif ($store->is_platform_transfers == 1 && $val == 1) { return [ 'code' => 1, 'msg' => '未配置平台微信提现设置' ]; } } if (!$cash->order_no) { $order_no = null; while (true) { $order_no = date('YmdHis') . mt_rand(100000, 999999); $exist_order_no = Cash::find()->where(['order_no' => $order_no])->exists(); if (!$exist_order_no) { break; } } $cash->order_no = $order_no; $cash->save(); } $res = []; $price = ($cash->price - ($cash->price * ($cash->service_charge / 100))); $servePrice = ($cash->price * ($cash->service_charge / 100)); if ($store->store_balance < $price && $store->is_platform_transfers == 1) { return [ 'code' => 1, 'msg' => '店铺剩余提现余额不足' ]; } $wechat_type = 1; $wechat_cash = Option::get('wechat_cash', $store_id, 'store')['value']; // if (empty($wechat_cash)) { // $wechat_cash = Option::get('store_wechat_cash', $store_id, 'store')['value']; // if ($wechat_cash) { // $wechat_type = 0; // } // } foreach ((array)json_decode($wechat_cash, true) as $value) { if (empty($value)) { $wechat_cash = Option::get('store_wechat_cash', $store_id, 'store')['value']; if ($wechat_cash) { $wechat_type = 0; } break; } } if ($status == Cash::STATUS_GIVEN) { //微信自动打款 if ((int)$cash->type === 0) { $cash->status = Cash::STATUS_GIVEN; $cash->pay_time = time(); $cash->pay_type = Cash::PAY_TYPE_WX; $user = User::findOne($cash->user_id); $wechat_open_id = $user->wechat_open_id; $data = [ 'partner_trade_no' => $cash->order_no, 'openid' => $wechat_open_id, 'check_name' => 'NO_CHECK', 'amount' => $price * 100, 'desc' => '转账' ]; // TODO 扣除店铺提现金额 if ($wechat_type == 0) { $before = $store->store_balance; $store->store_balance -= $price; $store->price -= $price; $store->store_withdrawn_cash += $price; $after = $store->store_balance; if ($store->save()) { StoreMoneyLog::saveLog(get_store_id(), StoreMoneyLog::LOG_TYPE_EXPEND, StoreMoneyLog::TYPE_SHAREHOLDER, $price, " ID{$id}:用户提现{$cash->price}元,扣除手续费{$servePrice}元,提现实际扣除{$price}元", $before, $after, $wechat_type); } } $wechat = \Yii::$app->controller->wechatPay; // $res = $wechat->transfer->toBalance($data); $res = (new \app\utils\WechatMerchant\WxV3($wechat))->transferBatches(get_store_id(), $data); //判断是否使用新版本转账 增加转账标识 $wechat_cash = json_decode($wechat_cash, true); if (intval($wechat_cash['is_open']) === 2) { if (!$res['code']) { $cash->is_platform_transfers = intval($store->is_platform_transfers); $cash->wx_cash_type = Cash::WX_CASH_TYPE_NEW; $cash->wx_cash_state = $res['data']['state']; $cash->wx_cash_result_info = json_encode($res['data'], JSON_UNESCAPED_UNICODE); $cash->save(); } } // NoticeSend::CashSuccess($cash->user_id, $user->binding, $price, '微信自动打款', ($cash->price - $price)); } elseif ((int)$cash->type === 1) { $order = (object)[ 'store_id' => $cash->store_id, 'order_no' => $cash->order_no, 'pay_price' => $price, 'name' => $cash->name ]; $result = Alipay::transfer($order, $cash->mobile); if (isset($result['code']) && $result['code'] == 1) { if (strpos($result['msg'], 'aop.invalid-app-auth-token-no-api')) { $result['msg'] = '接口未授权,请前往支付宝开放平台查询是否开通产品或授权支付宝转账产品'; } if (strpos($result['msg'], 'PAYEE_NOT_EXIST')) { $result['msg'] = '收款账号不存在或姓名有误,建议核实账号和姓名是否准确'; } if (strpos($result['msg'], 'BALANCE_IS_NOT_ENOUGH')) { $result['msg'] = '商户余额不足'; } return $result; } else { // TODO 扣除店铺提现金额 if ($wechat_type == 0) { $before = $store->store_balance; $store->store_balance -= $price; $store->price -= $price; $store->store_withdrawn_cash += $price; $after = $store->store_balance; if ($store->save()) { StoreMoneyLog::saveLog(get_store_id(), StoreMoneyLog::LOG_TYPE_EXPEND, StoreMoneyLog::TYPE_SHAREHOLDER, $price, " ID{$id}:用户提现{$cash->price}元,扣除手续费{$servePrice}元,提现实际扣除{$price}元", $before, $after, $wechat_type); } } } $cash->status = Cash::STATUS_GIVEN; $cash->pay_time = time(); $cash->pay_type = Cash::TYPE_ALIPAY; $cash->save(); return [ 'code' => 0, 'msg' => '成功' ]; } elseif ((int)$cash->type === 4) { $user = \app\models\User::findOne($cash->user_id); $saas_user = \app\models\SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]); $lg_info = Lg::find()->where(['user_id' => $saas_user->id, 'status' => 1, 'is_delete' => 0])->one(); $lgApi = new LgApi($cash->store_id); //灵工提现 $post_data = [ 'store_id' => $cash->store_id, 'outTradeNo' => $cash->order_no, //唯一批次号 'accNo' => bcmul($price, 100), 'amt' => bcmul($price, 100), 'name' => $cash->name, 'certCard' => $lg_info->cert_card //身份证号 ]; $result = $lgApi->FlexiblePay($post_data); if (isset($result['status']) && $result['status'] == 999) { return $result; } $cash->status = 6; //灵工待打款 $cash->save(); //灵工提现接口调用后消息队列查询状态 \queue_push(new LgCashJob(['id' => $cash->id, 'store_id' => $store_id, 'type' => 3, 'retry' => 5]), 60); return [ 'code' => 0, 'msg' => '成功' ]; } } elseif ($status == Cash::STATUS_HAND) { //手动打款 // TODO 扣除店铺提现金额 if ($wechat_type == 0) { $before = $store->store_balance; $store->store_balance -= $price; $store->price -= $price; $store->store_withdrawn_cash += $price; $after = $store->store_balance; if ($store->save()) { StoreMoneyLog::saveLog(get_store_id(), StoreMoneyLog::LOG_TYPE_EXPEND, StoreMoneyLog::TYPE_SHAREHOLDER, $price, " ID{$id}:用户提现{$cash->price}元,扣除手续费{$servePrice}元,提现实际扣除{$price}元", $before, $after, $wechat_type); } } // if ((int)$cash->type === 3) { // AccountLog::saveLog($cash->user_id, $price, 2, 1, 0, 0, '股东提现打款'); // } $cash->status = Cash::STATUS_GIVEN; $cash->pay_time = time(); $cash->pay_type = Cash::PAY_TYPE_HAND; $res['result_code'] = "SUCCESS"; // NoticeSend::CashSuccess($cash->user_id, $user->binding, $price, '手动打款', ($cash->price - $price)); } if (isset($res['result_code']) && $res['result_code'] == 'SUCCESS') { $cash->save(); return [ 'code' => 0, 'msg' => '成功' ]; } else { return [ 'code' => 1, 'msg' => !empty($res['err_code_des']) ? $res['err_code_des'] : '请稍后重试', 'data' => $res ]; } } /** * 提现明细 * @return \yii\web\Response */ public function actionCashDetail() { $page = get_params('page', 1); $limit = get_params('limit', 20); $status = get_params('status', -1); $is_area = get_params('is_area', -1); $store_id = get_store_id(); $user_id = get_user_id(); $share_holder = ShareHolder::findOne(['store_id' => $store_id, 'is_delete' => 0, 'status' => 1, 'user_id' => $user_id]); if (!$share_holder) { return $this->asJson([ 'code' => 1, 'msg' => '未拥有提现权限' ]); } if ($is_area) { if (intval($share_holder->agent_type) === 0) { return $this->asJson([ 'code' => 1, 'msg' => '未拥有提现权限' ]); } } $query = Cash::find()->where(['store_id' => $store_id, 'user_id' => $user_id]); $cash_type_where = ['cash_type' => 3]; if ($is_area) { $cash_type_where = ['cash_type' => Cash::IS_CASH_TYPE_AREA_AGENT]; } $query->andWhere($cash_type_where); if ($status != -1) { // 待审核 if ($status == 0) { $query->andWhere(['status' => $status]); } // 已打款 if ($status == 1) { $query->andWhere(['in', 'status', [2, 4]]); } // 已拒绝 if ($status == 2) { $query->andWhere(['status' => 3]); } } $count = $query->count(); $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $limit, 'page' => $page - 1]); $list = $query->select('*')->limit($pagination->limit) ->offset($pagination->offset)->orderBy('created_at desc')->asArray()->all(); foreach ($list as &$value) { $value['price'] = sprintf('%.2f', ($value['price'] - ($value['price'] * ($value['service_charge'] / 100)))); $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']); $value['audit_time'] = date('Y-m-d H:i:s', $value['updated_at']); if ($value['status'] == 2 || $value['status'] == 4) { $value['audit_time'] = date('Y-m-d H:i:s', $value['pay_time']); } } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'list' => $list, 'page_count' => $pagination->pageCount, 'row_count' => $count ] ]); } /** * @return mixed|string * 获取推广海报 */ public function actionGetQrcode() { $form = new ShareQrcodeForm(); $form->store_id = get_store_id(); $form->type = 9; $form->user = get_saas_user(); $form->user_id = get_user_id(); return $this->asJson($form->search()); } public function actionOtherInfo() { return $this->asJson([ 'code' => 0, 'data' => [ 'agreement' => Option::get('holder_agreement', get_store_id(), 'store', '')['value'] ?: '', 'banner' => Option::get('holder_banner', get_store_id(), 'store', '')['value'] ?: [], ] ]); } // 获取冻结佣金列表 public function actionFrostList() { $status = \get_params('status', -1); $user_id = \get_user_id(); $query = ShareHolderFrostLog::find()->alias('shf')->where([ 'parent_user_id' => $user_id, ]); if ($status > -1) { $query->andWhere(['status' => $status]); } $profit_q = clone $query; $send_profit_q = clone $query; $no_send_profit_q = clone $query; $profit = $profit_q->sum('amount') ?? '0.00'; $send_profit = $send_profit_q->andWhere(['status' => 1])->sum('amount') ?? '0.00'; $no_send_profit = $no_send_profit_q->andWhere(['status' => 0])->sum('amount') ?? '0.00'; $query->leftJoin(['o' => Order::tableName()], 'shf.order_id = o.id'); $query->andWhere(['<>', 'o.trade_status', 1]); $query->select('shf.*'); $data = \pagination_make($query, true, 'shf.created_at desc'); $data = array_merge($data, [ 'profit' => $profit, 'send_profit' => $send_profit, 'no_send_profit' => $no_send_profit, ]); foreach ($data['list'] as $key => &$value) { $value['is_refund'] = Order::is_refund($value['store_id'], $value['order_id']); $value['name'] = '未知'; $value['avatar'] = ''; $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']); $user = User::findOne($value['child_user_id']); if ($user && $user->binding) { $saasUser = SaasUser::findOne(['mobile' => $user->binding]); if ($saasUser) { $value['name'] = $saasUser->name; $value['avatar'] = $saasUser->avatar; } } } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => $data, ]); } public function actionGetParentOutList() { try { // $query = ShareHolderOutLog::find()->alias('sh')->where(['AND', ['sh.child_user_id' => $user_id], ['IS NOT', 'u.id', NULL]]) // ->leftJoin(['u' => User::tableName()], 'sh.child_user_id = u.id') // ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding') // ->select('su.name, su.avatar'); $query = User::find()->alias('u')->where(['u.id' => get_user_id()])->andWhere(['IS NOT', 'sh.id', NULL]) ->leftJoin(['sh' => ShareHolderOutLog::tableName()], 'u.old_parent_id = sh.parent_user_id') ->leftJoin(['u_' => User::tableName()], 'sh.child_user_id = u_.id') ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding') ->select('su.name, su.avatar') ->groupBy('u.id'); $pagination = pagination_make($query); $list = $pagination['list']; return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'list' => $list, 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'] ] ]); } catch (\Exception $e) { return $this->asJson([ 'code' => 1, 'msg' => $e->getMessage() ]); } } //判断股东是否达到升级条件 public function actionAuditStatus() { try { $store_id = get_store_id(); $user_id = get_user_id(); $holder_audit = (int)Option::get('holder_audit', get_store_id(), 'store', 0)['value'] ?: 0; //判断是否是股东 $share_holder = ShareHolder::findOne(['user_id' => $user_id, 'is_delete' => 0, 'status' => [0, 1]]); if (!empty($share_holder)) { if ((int)$share_holder->status === 0) { return $this->asJson([ 'code' => 1, 'msg' => '股东申请正在审核中' ]); } //已经是股东 return $this->asJson([ 'code' => 0, 'data' => [ 'is_holder' => 1 ], 'msg' => '股东已经通过申请' ]); } //默认等级 $share_holder_level = ShareHolderLevel::findOne(['store_id' => $store_id, 'status' => 1, 'is_default' => 1]); if (!$share_holder_level) { throw new \Exception('请设置默认等级'); } //是否可申请 $is_apply_holder = 1; //判断是否达成条件 $data = []; $condition = Json::decode($share_holder_level->condition); //团队人数 //TODO 股东人数以及金额 $man = $condition['man']; $data['man'] = [ 'is_open' => (int)$man['is_open'], 'data' => null ]; if ((bool)$man['is_open']) { //团队人数 $count = array_column($man['value'], 'count') ? max(array_column($man['value'], 'count')) : 0; $man_data = OldUserTreePath::find()->where([ 'parent_id' => $user_id ])->andWhere([ '!=', 'child_id', $user_id ])->asArray()->all(); //团队消费 $money = array_column($man['value'], 'money') ? max(array_column($man['value'], 'money')) : 0; //消费达到$m['money']以上允许升级 $child_id = array_column($man_data, 'child_id'); $child_id[] = $user_id; $pay_price = Order::find()->where( [ 'store_id' => $store_id, 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM, 'user_id' => $child_id ]) ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])->sum('pay_price'); if ($count > count($man_data)) { $is_apply_holder = 0; } if ($money > ($pay_price ?? 0)) { $is_apply_holder = 0; } $data['man']['data'] = [ 'count' => [ 'predict' => $count, //预计 'real' => count($man_data), 'status' => (bool)($count <= count($man_data)) ], 'money' => [ 'predict' => $money, 'real' => $pay_price ?? 0, 'status' => (bool)($money <= ($pay_price ?? 0)) ] ]; } //团队订单金额 $team_order = $condition['order']; $data['order'] = [ 'is_open' => (int)$team_order['is_open'], 'data' => null ]; if ((bool)$team_order['is_open']) { $count = array_column($team_order['value'], 'count') ? max(array_column($team_order['value'], 'count')) : 0; $man_data = OldUserTreePath::find()->where([ 'parent_id' => $user_id ])->andWhere([ '!=', 'child_id', $user_id ])->asArray()->all(); $man_user_id = User::findOld()->alias('u')->childs($user_id, true)->andWhere(['u.store_id' => $store_id])->select('u.id')->column(); // if (count($man_data) < 1) { // $msg = '团队金额:' . $o['level'] . '级团队人数为空'; // \Yii::warning(['---------- USER ID: ' . $share['user_id'] . '判断用户是否可以晋升 '. $value->name, $msg]); // $is_level = false; // break; // } // $man_user_id = array_column($man_data, 'id'); // $man_user_id = []; // $man_user_id[] = $user_id; // // foreach($man_data as $val) { // $man_user_id[] = $val['child_id']; // } $order_money = Order::find()->where( [ 'store_id' => $store_id, 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM ]) ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]]) ->andWhere(['in', 'user_id', $man_user_id])->sum('pay_price'); if ($count > ($order_money ?? 0)) { $is_apply_holder = 0; } $data['order']['data'] = [ 'money' => [ 'predict' => $count, 'real' => $order_money ?? 0, 'status' => (bool)($count <= ($order_money ?? 0)) ] ]; } //自身消费金额 $self = $condition['self']; $data['self'] = [ 'is_open' => (int)$self['is_open'], 'data' => null ]; if ((bool)$self['is_open']) { $data['self']['data'] = [ 'single' => null, 'total' => null ]; if ($self['value']['order_number'] == 1) { $order_price = Order::find()->where( [ 'store_id' => $store_id, 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM, 'user_id' => $user_id ]) ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])->max('pay_price'); if ($self['value']['price'] > ($order_price ?? 0)) { $is_apply_holder = 0; } //单笔金额 $data['self']['data']['single'] = [ 'predict' => $self['value']['price'], 'real' => $order_price ?? 0, 'status' => (bool)($self['value']['price'] <= ($order_price ?? 0)) ]; } else{ $order_money = Order::find()->where( [ 'store_id' => $store_id, 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM, 'user_id' => $user_id ])->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])->sum('pay_price'); if ($self['value']['price'] > ($order_money ?? 0)) { $is_apply_holder = 0; } //单笔金额 $data['self']['data']['total'] = [ 'predict' => $self['value']['price'], 'real' => $order_money ?? 0, 'status' => (bool)($self['value']['price'] <= ($order_money ?? 0)) ]; } } //累计充值金额 $recharge = $condition['recharge']; $data['recharge'] = [ 'is_open' => (int)$recharge['is_open'], 'data' => null ]; if ((bool)$recharge['is_open']) { $recharge_money = ReOrder::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'is_pay' => 1])->sum('pay_price'); if ($recharge['value'] > ($recharge_money ?? 0)) { $is_apply_holder = 0; } $data['recharge']['data'] = [ 'money' => [ 'predict' => $recharge['value'], 'real' => $recharge_money ?? 0, 'status' => (bool)($recharge['value'] <= ($recharge_money ?? 0)) ] ]; } //是否购买指定产品 $goods = $condition['goods']; $data['goods'] = [ 'is_open' => (int)$goods['is_open'], 'data' => null ]; if ((bool)$goods['is_open']) { // 查找该用户是否买过此商品 $goods_model = Order::find()->alias('o') ->leftJoin(['od' => OrderDetail::tableName()], 'od.order_id=o.id') ->where( [ 'o.store_id' => $store_id, 'o.user_id' => $user_id, 'o.is_delete' => 0, 'o.is_pay' => 1, 'o.trade_status' => Order::ORDER_FLOW_CONFIRM ]) ->andWhere(['or', ['o.is_pay' => 1], ['o.pay_type' => 2]]) ->andWhere(['od.goods_id' => $goods['value']['id']])->one(); if (!$goods_model) { $is_apply_holder = 0; } $data['goods']['data'] = [ 'num' => [ 'predict' => 0, 'real' => 0, 'status' => (bool)$goods_model ] ]; } //累计充值金额 $amount_total = $condition['amount_total']; $data['amount_total'] = [ 'is_open' => (int)$amount_total['is_open'], 'data' => null ]; if ((bool)$amount_total['is_open']) { $total_price = get_user()->total_price; if ($amount_total['value'] > ($total_price ?? 0)) { $is_apply_holder = 0; } $data['amount_total']['data'] = [ 'money' => [ 'predict' => $amount_total['value'], 'real' => $total_price ?? 0, 'status' => (bool)($amount_total['value'] <= ($total_price ?? 0)) ] ]; } if ($holder_audit === 0 && $is_apply_holder === 1 && $user_id) { //如果未开启强制审核并已经达到升级条件 $is_apply_holder = 2; //直接跳转股东中心 $user = User::findOne($user_id); $saas_user = SaasUser::findOne(['mobile' => $user->binding]); $model = new ShareHolder(); $model->level_id = $share_holder_level->id; $model->store_id = $store_id; $model->user_id = $user_id; $model->name = $saas_user->name; $model->address = ''; $model->mobile = $saas_user->mobile; $model->province = 0; $model->city = 0; $model->district = 0; $model->province_name = ''; $model->city_name = ''; $model->district_name = ''; $model->status = 1; $model->audit_time = time(); $result = $model->save(); if (!$result) { throw new \Exception(json_encode($model->errors)); } //添加股东升级记录 $level_log = new \app\models\ShareHolderLevelLog(); $level_log->before_level = 0; $level_log->after_level = $share_holder_level->id; $level_log->user_id = $user_id; $level_log->store_id = $store_id; $level_log->order_id = 0; $level_log->desc = "用户申请自动升级为股东"; if (!$level_log->save()) { throw new \Exception('等级更新失败'); } BonusPool::checkChildHolderUpdateLevel($user_id, $share_holder_level->id); // BonusPool::checkLevel($store_id, 0, $model->user_id); // BonusPool::ShareHolderLevelJob($store_id, 0, $model->user_id); if ((int)$share_holder_level->member_level > (int)$user->level) { $user->level = $share_holder_level->member_level; } $user->is_holder = 1; $result = $user->save(); if (!$result) { throw new \Exception(json_encode($user->errors)); } } $holder_agreement = Option::get('holder_agreement', get_store_id(), 'store', '')['value']; return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => array_merge($data, [ 'is_holder' => 0, 'is_apply_holder' => $is_apply_holder, 'holder_agreement' => $holder_agreement ]) ]); } catch (\Exception $e) { return $this->asJson([ 'code' => 1, 'msg' => $e->getMessage() ]); } } public function actionDefaultLevelGoods() { try { $store_id = get_store_id(); //默认等级 $share_holder_level = ShareHolderLevel::findOne(['store_id' => $store_id, 'status' => 1, 'is_default' => 1]); $condition = Json::decode($share_holder_level->condition); $goods = $condition['goods']; $goods_ = null; if ((bool)$goods['is_open']) { $goods_ = Goods::find()->where(['id' => $goods['value']['id'], 'status' => 1, 'is_delete' => 0]) ->select('id, name, cover_pic, price, original_price, virtual_sales')->asArray()->all(); foreach ($goods_ as &$item) { $cacheKey = 'actionDefaultLevelGoods' . $item['id']; $cacheVal = cache()->get($cacheKey); if($cacheVal){ $goods_num = $cacheVal; }else{ $goods_num = OrderDetail::find()->alias('od')->where(['od.goods_id' => $item['id'], 'is_pay' => 1]) ->leftJoin(['o' => Order::tableName()], 'o.id = od.order_id') ->andWhere(['AND', ['>=', 'o.created_at', strtotime(date("Y-m"))], ['<', 'o.created_at', strtotime(date("Y") . '-' . (date("m") + 1))] ])->select('od.num')->sum('od.num'); if($goods_num > 300){ cache()->set($cacheKey, $goods_num, 3 * 60); } } $item['sales_num'] = $goods_num ? $goods_num + $item['virtual_sales'] : $item['virtual_sales']; } } return $this->asJson([ 'code' => 0, 'msg' => '获取成功', 'data' => [ 'goods' => $goods_ ] ]); } catch (\Exception $e) { return $this->asJson([ 'code' => 1, 'msg' => $e->getMessage() ]); } } //区域代理申请 public function actionAreaAgentApply() { try { $store_id = get_store_id(); $user_id = get_user_id(); $agent_type = post_params('agent_type'); $province_id = post_params('province_id'); $city_id = post_params('city_id'); $district_id = post_params('district_id'); $town_id = post_params('town_id', 0); $shareHolderModel = ShareHolder::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'is_delete' => 0, 'status' => [0, 1]]) ->andWhere([">", 'agent_type', 0])->asArray()->one(); if ($shareHolderModel) { throw new \Exception('已经为区域代理,无需申请'); } $areaAgentApply = ShareHolderAreaAgentApply::find()->where(['user_id' => $user_id, 'status' => [0, 1], 'is_delete' => 0])->asArray()->one(); if ($areaAgentApply) { throw new \Exception('请勿重复申请'); } if (!in_array($agent_type, [1, 2, 3, 4])) { throw new \Exception('参数错误'); } $is_multi_agent = Option::get('is_multi_agent', $store_id, 'bonus_pool_area_agent', 0)['value']; $where = ['agent_type' => $agent_type, 'is_delete' => 0, 'status' => [0, 1], 'store_id' => $store_id]; switch ($agent_type) { case 1: $where['province_id'] = $province_id; break; case 2: $where['province_id'] = $province_id; $where['city_id'] = $city_id; break; case 3: $where['province_id'] = $province_id; $where['city_id'] = $city_id; $where['district_id'] = $district_id; break; case 4: $where['province_id'] = $province_id; $where['city_id'] = $city_id; $where['district_id'] = $district_id; $where['town_id'] = $town_id; break; } $area_apply = ShareHolderAreaAgentApply::find()->where($where)->one(); debug_log(['id' => $area_apply->id], 'area_share.log'); if ($area_apply && !$is_multi_agent) { throw new \Exception('已经存在相同区域下的代理'); } $share_holder = ShareHolder::find()->where($where)->one(); debug_log(['share_holder_id' => $share_holder->id], 'area_share.log'); if ($share_holder && !$is_multi_agent) { throw new \Exception('已经存在相同区域下的代理'); } $areaAgentApply = new ShareHolderAreaAgentApply(); $areaAgentApply->user_id = $user_id; $areaAgentApply->agent_type = $agent_type; $areaAgentApply->province_id = (int)$province_id; $areaAgentApply->city_id = (int)$city_id; $areaAgentApply->district_id = (int)$district_id; $areaAgentApply->town_id = (int)$town_id; $areaAgentApply->params = json_encode([ 'agent_type' => $agent_type, 'province_id' => $province_id, 'city_id' => $city_id, 'town_id' => $town_id, 'district_id' => $district_id ], JSON_UNESCAPED_UNICODE); $areaAgentApply->store_id = $store_id; if (!$areaAgentApply->save()) { throw new \Exception(json_encode($areaAgentApply->errors, JSON_UNESCAPED_UNICODE)); } return $this->asJson([ 'code' => 0, 'msg' => '提交成功' ]); } catch (\Exception $e) { return $this->asJson([ 'code' => 1, 'msg' => $e->getMessage() ]); } } //获取区域代理申请状态 public function actionGetAreaAgentApply() { $store_id = get_store_id(); // 轮播广告状态 $area_agent_banner_status = Option::get('area_agent_banner_status', $store_id, 'bonus_pool_area_agent', 0)['value']; // 图片列表 $area_agent_banner_images = Option::get('area_agent_banner_images', $store_id, 'bonus_pool_area_agent', '')['value']; if ($area_agent_banner_images) { $area_agent_banner_images = json_decode($area_agent_banner_images, true); } try { $user_id = get_user_id(); $status = 0; $areaAgentApply = ShareHolderAreaAgentApply::find()->where(['user_id' => $user_id, 'is_delete' => 0])->asArray()->one(); if ($areaAgentApply) { $status = $areaAgentApply['status']; } else { $shareHolderModel = ShareHolder::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'is_delete' => 0, 'status' => [0, 1]]) ->andWhere([">", 'agent_type', 0])->asArray()->one(); if ($shareHolderModel) { $status = 1; $areaAgentApply['agent_type'] = $shareHolderModel['agent_type']; $areaAgentApply['province_id'] = $shareHolderModel['province_id']; $areaAgentApply['city_id'] = $shareHolderModel['city_id']; $areaAgentApply['district_id'] = $shareHolderModel['district_id']; } } return $this->asJson([ 'code' => 0, 'msg' => '获取成功', 'data' => [ 'areaAgentApply' => $areaAgentApply, 'agent_type' => $areaAgentApply['agent_type'] ?: 1, 'status' => (int)$status, 'province_id' => $areaAgentApply['province_id'], 'city_id' => $areaAgentApply['city_id'], 'district_id' => $areaAgentApply['district_id'], 'town_id' => $areaAgentApply['town_id'], 'province' => District::findOne($areaAgentApply['province_id'])->name ?: '', 'city' => District::findOne($areaAgentApply['city_id'])->name ?: '', 'district' => District::findOne($areaAgentApply['district_id'])->name ?: '', 'town' => District::findOne($areaAgentApply['town_id'])->name ?: '', 'area_agent_banner_status' => (int)$area_agent_banner_status, 'area_agent_banner_images' => $area_agent_banner_images ?: [] ] ]); } catch (\Exception $e) { return $this->asJson([ 'code' => 0, 'msg' => $e->getMessage(), 'data' => [ 'agent_type' => 1, 'status' => 0, 'province_id' => '', 'city_id' => '', 'district_id' => '', 'province' => '', 'city' => '', 'district' => '', 'area_agent_banner_status' => (int)$area_agent_banner_status, 'area_agent_banner_images' => $area_agent_banner_images ?: [] ] ]); } } //区域代理中心 public function actionGetAreaAgentInfo() { $saas_user = get_saas_user(); $data = [ 'cash_profit' => 0, 'total_price' => 0, 'cash_price' => 0, 'agent_rate' => 0, 'agent_type' => 0, 'avatar' => $saas_user->avatar, 'name' => $saas_user->name, 'mobile' => $saas_user->mobile ]; try { $user = get_user(); $store_id = get_store_id(); $user_id = get_user_id(); $cash_price = Cash::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'cash_type' => Cash::IS_CASH_TYPE_AREA_AGENT])->andWhere(['in', 'status', [2, 4]])->sum('price') ?: '0.00'; $share_holder = ShareHolder::findOne(['store_id' => $store_id, 'user_id' => $user_id, 'is_delete' => 0, 'status' => 1]); $agent_rate = 0; $status = 0; $is_multi_agent = Option::get('is_multi_agent', $store_id, 'bonus_pool_area_agent', 0)['value']; $multi_agent_type1 = $multi_agent_type2 = $multi_agent_type3 = $multi_agent_type4 = 0; if($is_multi_agent){ $multi_agent_type1 = Option::get('multi_agent_type1', $store_id, 'bonus_pool_area_agent', 0)['value']; $multi_agent_type2 = Option::get('multi_agent_type2', $store_id, 'bonus_pool_area_agent', 0)['value']; $multi_agent_type3 = Option::get('multi_agent_type3', $store_id, 'bonus_pool_area_agent', 0)['value']; $multi_agent_type4 = Option::get('multi_agent_type4', $store_id, 'bonus_pool_area_agent', 0)['value']; } if ($share_holder) { $agent_rate = $share_holder->agent_rate; if (intval($is_multi_agent)) { switch (intval($share_holder->agent_type)) { case 2: $agent_rate = $multi_agent_type2; break; case 3: $agent_rate = $multi_agent_type3; break; case 4: $agent_rate = $multi_agent_type4; break; default: $agent_rate = $multi_agent_type1; break; } } else { if (intval($share_holder->agent_rate_status) === 0) { $agent_rate = 0; } } } if (intval($share_holder->agent_type) > 0) { $status = 1; } // 文档要求说只操作区域佣金提现 // 可提现区域代理佣金 = 区域代理佣金 - 已提现区域代理佣金 // 获取区域代理佣金 $area_agent_profit = ShareDetail::find()->where(['user_id' => $user_id, 'is_delete' => 0, 'type' => ShareDetail::TYPE_AREA_PROFIT, 'is_send' => 1]) ->sum('money') ?: '0.00'; $data['status'] = $status; //可提现分红 $data['cash_profit'] = $user->price; //累计分红 $data['total_price'] = $area_agent_profit;//$user->total_price; //已提现 $data['cash_price'] = $cash_price; //佣金比例 $data['agent_rate'] = $agent_rate; //区域代理类型 1省 2市 3区 $data['agent_type'] = $share_holder->agent_type; return $this->asJson([ 'code' => 0, 'msg' => '获取成功', 'data' => $data ]); } catch (\Exception $e) { return $this->asJson([ 'code' => 0, 'msg' => $e->getMessage(), 'data' => [] ]); } } public function actionGetFugouInfo() { $store_id = get_store_id(); $user_id = \get_user_id(); $setting = FuGou::getSetting($store_id); foreach ($setting['fugou_goods'] as $k => $goods) { $g = \app\models\Goods::find()->where(['id' => $goods['id']])->select('id, name, cover_pic, price, goods_num, original_price')->one(); $setting['fugou_goods'][$k] = [ 'id' => $g->id, 'name' => $g->name, 'cover_pic' => $g->cover_pic, 'price' => $g->price, 'goods_num' => $g->goods_num, 'original_price' => $g->original_price, 'fugou_price' => $goods['fugou_price'], 'fugou_num' => 1, 'checked' => 0, ]; } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'setting' => $setting, 'frost_price' => FuGou::getUserFrostPrice($user_id), 'cycle_day' => FuGou::cycleByDay($user_id, $store_id, $setting), 'cycle_price' => FuGou::cycleByPrice($user_id, $store_id, $setting), ], ]); } public function actionFugouCart() { try { $store_id = get_store_id(); $user_id = \get_user_id(); $goods_list = \json_decode(\post_params('goods_list'), true); $res = [[ 'mch_id' => 0, 'goods_list' => [], ]]; FugouCart::deleteAll(['user_id' => $user_id, 'store_id' => $store_id]); foreach ($goods_list as $goods) { $g = Goods::findOne($goods['goods_id']); if (!$g) { throw new \Exception('商品不存在'); } $attr = json_decode($g->attr, true); $modelAttr = []; foreach ($attr[0]['attr_list'] as $item) { if (!empty($item['attr_id'])) { $modelAttr[] = intval($item['attr_id']); } } $model = new FugouCart(); $model->user_id = $user_id; $model->store_id = $store_id; $model->goods_id = $goods['goods_id']; $model->num = $goods['num']; $model->price = $goods['price']; $model->attr = json_encode($modelAttr); if (!$model->save()) { throw new \Exception('添加购物车失败'); } $res[0]['goods_list'][] = [ 'fugou_cart_id' => $model->id, ]; } return $this->asJson([ 'code' => 0, 'data' => $res, ]); } catch (\Exception $e) { return $this->asJson([ 'code' => 1, 'msg' => '下单失败,请稍后重试!', 'err' => $e->getMessage(), 'line' => $e->getLine(), ]); } } public function actionFugouFrostLog() { $user_id = \get_user_id(); $status = \get_params('status', -1); $query = ShareHolderFugouFrostLog::find()->alias('f') ->leftJoin(['o' => Order::tableName()], 'f.order_id = o.id') ->where(['f.user_id' => $user_id]) ->select('f.*, o.order_no'); if ($status > -1) { $query->andWhere(['f.status' => $status]); } $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']); if ($item['updated_at'] > 0) { $item['updated_at'] = date('Y-m-d H:i:s', $item['updated_at']); } } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'list' => $pagination['list'], 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'] ] ]); } }