| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1;
- use app\models\Cash;
- use app\models\common\InviteCode;
- use app\models\Goods;
- use app\models\Option;
- use app\models\Order;
- use app\models\RcCommissionLog;
- use app\models\Share;
- use app\models\User;
- use app\modules\client\models\ApiModel;
- use yii\helpers\Json;
- class ShareForm extends ApiModel
- {
- public $share;
- public $store_id;
- public $user_id;
- public $name;
- public $mobile;
- public $agree;
- public $form_id;
- /**
- * 场景说明:NONE_CONDITION:无条件; APPLY:需要申请
- * @return array
- */
- public function rules()
- {
- return [
- [['name', 'mobile', 'agree'], 'required', 'on' => 'APPLY'],
- [['agree'], 'integer'],
- [['name', 'mobile', 'form_id'], 'trim'],
- [['mobile'], 'match', 'pattern' => "/\+?\d[\d -]{8,12}\d/", 'message' => '手机号错误', 'on' => 'APPLY']
- ];
- }
- public function attributeLabels()
- {
- return [
- 'name' => '真实姓名',
- 'mobile' => '手机号'
- ];
- }
- public function save()
- {
- if ($this->validate()) {
- $t = \Yii::$app->db->beginTransaction();
- if (($this->agree || $this->agree == 0) && $this->agree != 1) {
- return [
- 'code' => 1,
- 'msg' => '请先阅读并确认分销申请协议'
- ];
- }
- $this->share->attributes = $this->attributes;
- if ($this->share->isNewRecord) {
- $this->share->is_delete = 0;
- $this->share->created_at = time();
- $this->share->store_id = $this->store_id;
- } else {
- if ($this->share->status == 1) {
- return [
- 'code' => 1,
- 'msg '=> '用户已经是分销商'
- ];
- }
- }
- $this->share->user_id = get_user_id();
- $user = User::findOne(['id' => get_user_id(), 'store_id' => $this->store_id]);
- $setting = Option::get('share_basic_setting', $this->store_id);
- $share_setting = $setting ? Json::decode($setting['value']) : [];
- if ($share_setting['share_condition']['value'] != 2) {
- $user->is_distributor = 2;
- $this->share->status = 0;
- $user->time = 0;
- } else {
- $user->is_distributor = 1;
- $this->share->status = 1;
- $user->time = time();
- }
- if (!$user->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- if ($this->share->save()) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '成功'
- ];
- } else {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '网络异常',
- 'data' => $this->errors,
- ];
- }
- } else {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- }
- /**
- * @return array
- * 获取佣金相关信息
- */
- public function getPrice($cash_type = -1)
- {
- $user = User::find()->where(['id' => $this->user_id])->one();
- $query = Cash::find()->where([
- 'store_id' => $this->store_id, 'user_id' => $this->user_id, 'is_delete' => 0
- ]);
- if($cash_type > -1){
- $query->andWhere(['cash_type' => $cash_type]);
- }
- $list = $query->asArray()->all();
- $new_list = [];
- $new_list['total_price'] = $user->total_price;//分销佣金
- $new_list['price'] = $user->price;//可提现
- $new_list['cash_price'] = 0;//已提现
- $new_list['wait_audit'] = 0;//待审核
- $new_list['un_pay'] = 0;//未审核
- $new_list['total_cash'] = 0;//提现明细
- foreach ($list as $index => $value) {
- $new_list['total_cash'] = sprintf('%.2f', ($new_list['total_cash'] + $value['price']));
- if ($value['status'] == 0) {
- $new_list['wait_audit'] = sprintf('%.2f', ($new_list['wait_audit'] + $value['price']));
- } elseif ($value['status'] == 1) {
- $new_list['un_pay'] = sprintf('%.2f', ($new_list['un_pay'] + $value['price']));
- } elseif ($value['status'] == 2 || $value['status'] == 4 || $value['status'] == 5) {
- $new_list['cash_price'] = sprintf('%.2f', ($new_list['cash_price'] + $value['price']));
- }
- }
- return $new_list;
- }
- /**
- * @return array|null|\yii\db\ActiveRecord
- *
- */
- public function getCash()
- {
- $list = User::find()->alias('u')
- ->where(['u.is_delete' => 0, 'u.store_id' => $this->store_id, 'u.id' => $this->user_id])
- ->leftJoin('{{%cash}} c', 'c.user_id=u.id and c.is_delete=0')
- ->select([
- 'u.total_price', 'u.price',
- 'sum(case when c.status = 2 then c.price else 0 end) cash_price',
- 'sum(case when c.status = 1 then c.price else 0 end) un_pay'
- ])->groupBy('c.user_id')->asArray()->one();
- return $list;
- }
- /**
- * 获取分销团队总人数
- * @return array
- */
- public function getTeamCount()
- {
- $setting = Option::get('share_basic_setting', $this->store_id);
- $share_setting = $setting ? Json::decode($setting['value']) : [];
- if (!$share_setting || $share_setting['level']['value'] == 0) {
- return [
- 'team_count' => 0,
- 'team' => []
- ];
- }
- $team = [];
- $first = User::find()->select(['id'])
- ->where(['store_id' => $this->store_id, 'parent_id' => $this->user_id, 'is_delete' => 0])->column();
- $count = count($first);
- $team['f_c'] = $first;
- if ($share_setting['level']['value'] >= 2) {
- $second = User::find()->select(['id'])
- ->where(['store_id' => $this->store_id, 'parent_id' => $first, 'is_delete' => 0])->column();
- $count += count($second);
- $team['s_c'] = $second;
- if ($share_setting['level']['value'] >= 3) {
- $third = User::find()->select(['id'])
- ->where(['store_id' => $this->store_id, 'parent_id' => $second, 'is_delete' => 0])->column();
- $count += count($third);
- $team['t_c'] = $third;
- }
- }
- return [
- 'team_count' => $count,
- 'team' => $team
- ];
- }
- /**
- * @return array
- */
- public function getOrder()
- {
- $arr = $this->getTeamCount();
- $team_arr = $arr['team'];
- $order_money = 0;
- $first_price = Order::find()->alias('o')->where([
- 'o.is_delete' => 0, 'o.store_id' => $this->store_id
- ])->andWhere([
- '<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL
- ])->andWhere([
- 'o.parent_id' => $this->user_id,
- ])->select(['sum(first_price)'])->scalar();
- if ($first_price) {
- $order_money += doubleval($first_price);
- }
- if (!empty($team_arr['s_c'])) {
- $second_price = Order::find()->alias('o')->where([
- 'o.is_delete' => 0, 'o.store_id' => $this->store_id
- ])->andWhere([
- '<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL
- ])->andWhere([
- 'or',
- ['and', ['in', 'o.user_id', $team_arr['s_c']], ['o.parent_id' => $team_arr['f_c'], 'o.parent_id_1' => 0]],
- ['o.parent_id_1' => $this->user_id],
- ])->select(['sum(second_price)'])->scalar();
- if ($second_price) {
- $order_money += doubleval($second_price);
- }
- }
- if (!empty($team_arr['t_c'])) {
- $third_price = Order::find()->alias('o')->where([
- 'o.is_delete' => 0, 'o.store_id' => $this->store_id
- ])->andWhere([
- '<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL
- ])->andWhere([
- 'or',
- ['and', ['in', 'o.user_id', $team_arr['t_c']], ['o.parent_id' => $team_arr['s_c'], 'o.parent_id_1' => 0]],
- ['o.parent_id_2' => $this->user_id],
- ])->select(['sum(third_price)'])->scalar();
- if ($third_price) {
- $order_money += doubleval($third_price);
- }
- }
- $arr['order_money'] = doubleval(sprintf('%.2f', $order_money));
- return $arr;
- }
- /**
- * 获取分销配置
- * @return array
- */
- public function getShareSetting() {
- $setting = Option::get('share_basic_setting', $this->store_id);
- $share_basic_setting = $setting ? Json::decode($setting['value']) : [];
- $money_setting = Option::get('share_money_setting', $this->store_id);
- $share_money_setting = $money_setting ? Json::decode($money_setting['value']) : [];
- $arr['first'] = $share_money_setting['level_one']['value'] ?? '';
- $arr['second'] = $share_money_setting['level_two']['value'] ?? '';
- $arr['third'] = $share_money_setting['level_three']['value'] ?? '';
- $arr['store_id'] = $this->store_id;
- $arr['level'] = $share_basic_setting['level']['value'] ?? '';
- $arr['condition'] = $share_basic_setting['condition']['value'] ?? '';
- $arr['share_condition'] = $share_basic_setting['share_condition']['value'] ?? 2;
- $arr['content'] = $share_basic_setting['content']['value'] ?? '';
- $arr['pay_type'] = $share_basic_setting['pay_type']['value'] ?? '';
- $arr['bank'] = $share_basic_setting['bank']['value'] ?? '';
- $arr['min_money'] = $share_basic_setting['min_money']['value'] ?? '';
- $arr['cash_max_day'] = $share_basic_setting['cash_max_day']['value'] ?? '';
- $arr['agree'] = $share_basic_setting['agree']['value'] ?? '';
- $arr['first_name'] = $share_money_setting['level_one']['text'] ?? '';
- $arr['second_name'] = $share_money_setting['level_two']['text'] ?? '';
- $arr['third_name'] = $share_money_setting['level_three']['text'] ?? '';
- $arr['pic_url_1'] = $share_basic_setting['apply_image']['value'] ?? '';
- $arr['pic_url_2'] = $share_basic_setting['no_checked_image']['value'] ?? '';
- $arr['price_type'] = $share_money_setting['price_type'] ?? '';
- $arr['remaining_sum'] = $share_basic_setting['remaining_sum']['value'] ?? '';
- $arr['share_goods_status'] = $share_basic_setting['share_goods_status']['value'] ?? '';
- $arr['share_goods_id'] = $share_basic_setting['share_goods_id']['value'] ?? '';
- $arr['is_rebate'] = $share_basic_setting['is_rebate']['value'] ?? '';
- $arr['parent_label'] = $share_basic_setting['parent_label']['value'] ?? '';
- $arr['recharge_count'] = RcCommissionLog::find()->where(['parent_id' => get_user_id(), 'store_id' => $this->store_id])->count() ?? 0;
- $arr['yinbao'] = \app\modules\admin\models\pospal\PospalForm::isopen($this->store_id);
- $arr['invite_type'] = $share_basic_setting['invite_type']['value'] ?? 1; //邀请方式 1手机号 2邀请码
- //邀请码
- $arr['invite_code'] = InviteCode::id2Code(get_user_id());
- $goods = null;
- // 无需审核,判断是否需要其他条件
- if ($arr['share_condition'] == 2) {
- $exit = Share::find()->andWhere(['user_id' => get_user_id(), 'store_id' => $this->store_id, 'is_delete' => 0,
- 'status' => 1])->exists();
- if (!$exit && ($share_basic_setting['auto_share_val']['value'] > 0 || $share_basic_setting['share_goods_status']['value'] !=0 )) {
- if ($share_basic_setting['auto_share_val']['value'] > 0) {
- $goods = Goods::find()->select('cover_pic, goods_num, id, is_negotiable, name, original_price, price, product_type')
- ->where(['store_id' => get_store_id(), 'is_delete' => 0, 'status' => 1])
- ->andWhere(['>', 'price', $share_basic_setting['auto_share_val']['value']])->limit(5)->asArray()->one();
- }
- if ((int)$arr['share_goods_status'] === 1) {
- $goods = Goods::find()->select('cover_pic, goods_num, id, is_negotiable, name, original_price, price, product_type')
- ->where(['store_id' => get_store_id(), 'is_delete' => 0, 'status' => 1, 'product_type' => 0])->limit(5)->asArray()->all();
- }
- if ((int)$arr['share_goods_status'] === 2) {
- $ids = \explode(',', $arr['share_goods_id']);
- $goods = Goods::find()->select('cover_pic, goods_num, id, is_negotiable, name, original_price, price, product_type')
- ->where(['id' => $ids])->asArray()->all();
- }
- }
- }
- $arr['share_goods'] = null;
- if ($goods) {
- $arr['share_goods'] = $goods;
- }
- return ['code' => 0, 'msg' => 'success', 'data' => $arr];
- }
- /**
- * 获取分销配置
- * @return array
- */
- public function getShareMsg() {
- $setting = Option::get('share_basic_setting', $this->store_id);
- $share_basic_setting = $setting ? Json::decode($setting['value']) : [];
- $money_setting = Option::get('share_money_setting', $this->store_id);
- $share_money_setting = $money_setting ? Json::decode($money_setting['value']) : [];
- $arr['first'] = $share_money_setting['level_one']['value'] ?? '';
- $arr['second'] = $share_money_setting['level_two']['value'] ?? '';
- $arr['third'] = $share_money_setting['level_three']['value'] ?? '';
- $arr['store_id'] = $this->store_id;
- $arr['level'] = $share_basic_setting['level']['value'] ?? '';
- $arr['condition'] = $share_basic_setting['condition']['value'] ?? '';
- $arr['share_condition'] = $share_basic_setting['share_condition']['value'] ?? '';
- // 无需审核,判断是否需要其他条件
- if ($arr['share_condition'] == 2) {
- $exit = Share::find()->andWhere(['user_id' => get_user_id(), 'store_id' => $this->store_id, 'is_delete' => 0,
- 'status' => 1])->exists();
- if (!$exit && ($share_basic_setting['auto_share_val']['value'] > 0 || $share_basic_setting['share_goods_status']['value'] !=0 )) {
- return [
- 'code' => 1,
- 'msg' => $share_basic_setting['auto_share_val']['value'] > 0 ? '条件不满足,请消费'.$share_basic_setting['auto_share_val']['value'].'元': '请购买指定商品'
- ];
- }
- }
- return ['code' => 0, 'msg' => 'success'];
- }
- }
|