| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\constants\OptionSetting;
- use app\models\Address;
- use app\models\Cash;
- use app\models\common\InviteCode;
- use app\models\District;
- use app\models\Level;
- use app\models\Mch;
- use app\models\Option;
- use app\models\Order;
- use app\models\SaasUser;
- use app\models\ShareDetail;
- use app\models\Shop;
- use app\models\User;
- use app\models\UserCard;
- use app\models\UserLabel;
- use app\models\UserShareMoney;
- use app\utils\Export;
- use app\utils\ExportList;
- use Spatie\SimpleExcel\SimpleExcelWriter;
- use Yii;
- use yii\base\Model;
- use app\models\PospalUser;
- use app\models\Share;
- use app\models\ShareHolderParentOutLog;
- class UserForm extends Model
- {
- public $store_id;
- public $md_id;
- public $ids;
- public $price;
- public $contact_way;
- public $comments;
- public $blacklist;
- public $level;
- public $id;
- public $parent_id;
- public $old_parent_id;
- public $init_parent_id;
- public $is_saas_clerk;
- public $type;
- public $name;
- public $phone;
- public $dateStart;
- public $dateEnd;
- public $flag;
- public $fields;
- public $is_admin = -1;
- public $is_yinbao = 0;
- public $user_remark;
- public $payment;
- public $binding;
- public $user_id;
- public $mobile;
- public $detail;
- public $province_id;
- public $city_id;
- public $district_id;
- public $is_default;
- public $latitude;
- public $longitude;
- public $is_export;
- public $remark_name;
- public $money_month_min;
- public $password;
- public $nickname;
- //订单数量
- public $orderCountStart;
- public $orderCountEnd;
- //订单金额
- public $priceTotalStart;
- public $priceTotalEnd;
- //余额
- public $moneyStart;
- public $moneyEnd;
- //积分
- public $integralStart;
- public $integralEnd;
- public $user_label_id;
- public $user_label_name;
- /**
- * @return array the validation rules.
- */
- public function rules()
- {
- return [
- [['comments', 'name', 'dateStart', 'dateEnd', 'phone', 'flag', 'type', 'ids', 'user_remark', 'mobile', 'detail', 'password', 'user_label_name', 'nickname'], 'string'],
- [['name', 'dateStart', 'dateEnd', 'phone', 'flag', 'remark_name', 'password', 'nickname'], 'trim'],
- [['id', 'blacklist', 'level', 'parent_id', 'is_saas_clerk', 'is_admin', 'is_yinbao', 'old_parent_id', 'is_export', 'user_id', 'province_id', 'city_id', 'district_id', 'store_id', 'md_id'], 'integer'],
- [['price', 'latitude', 'longitude', 'orderCountStart', 'orderCountEnd', 'priceTotalStart', 'priceTotalEnd', 'moneyStart', 'moneyEnd', 'integralStart', 'integralEnd'], 'number'],
- [['fields', 'payment', 'is_default', 'money_month_min', 'init_parent_id','user_label_id'], 'safe'],
- [['contact_way', 'binding'], 'match', 'pattern' => '/^1[3456789]\d{9}$/', 'message' => '手机号码格式不正确。'],
- ];
- }
- public function getUserList()
- {
- $query = User::find()->alias('u')->where([
- 'u.type' => User::USER_TYPE_NORMAL,
- 'u.store_id' => $this->store_id,
- 'u.is_delete' => User::USER_NOT_DELETE,
- ])->andWhere(['<>', 'u.binding' , ''])
- ->leftJoin(SaasUser::tableName() . ' su', 'su.mobile=u.binding');
- if($this->is_yinbao){
- $query->leftJoin(['pu' => PospalUser::tableName()], 'pu.user_id = u.id')->leftJoin(['o' => \app\models\OrderPospal::find()
- ->where(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])
- ->andWhere(['is_delete' => 0])
- ->andWhere(['is_pay' => Order::IS_PAY_TRUE, 'store_id' => $this->store_id])->select('user_id,
- count( id ) AS order_count,
- sum( pay_price ) AS pay_price_sum')->groupBy('user_id')], 'o.user_id = u.id')
- ->andWhere(['>', 'pu.id', 0]);
- } else {
- $query->leftJoin(['o' => Order::find()
- ->where(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])
- ->andWhere(['is_delete' => 0])
- ->andWhere(['is_pay' => Order::IS_PAY_TRUE, 'store_id' => $this->store_id, 'is_recycle' => 0])->select('user_id,
- count( id ) AS order_count,
- sum( pay_price ) AS pay_price_sum')->groupBy('user_id')], 'o.user_id = u.id');
- }
- // 手机号 TODO:这是啥东西判断???看不懂,先注释了
- // if ($this->type == 'mobile') {
- // if ($this->name) {
- // $query->andWhere(['or', ['like', 'u.contact_way', $this->name], ['like', 'u.binding', $this->name]]);
- // }
- // }
- // //名称或者昵称
- // if ($this->type == 'type') {
- // if ($this->name) {
- // $query->andWhere(['or', ['like', 'u.nickname', $this->name], ['like', 'su.name', $this->name]]);
- // }
- // }
- if (intval($this->orderCountStart) > 0) {
- $query->andWhere(['>=', 'o.order_count', $this->orderCountStart]);
- }
- if (intval($this->orderCountEnd) > 0) {
- $query->andWhere(['<=', 'o.order_count', $this->orderCountEnd]);
- }
- if (intval($this->priceTotalStart) > 0) {
- $query->andWhere(['>=', 'o.pay_price_sum', $this->priceTotalStart]);
- }
- if (intval($this->priceTotalEnd) > 0) {
- $query->andWhere(['<=', 'o.pay_price_sum', $this->priceTotalEnd]);
- }
- if (floatval($this->moneyStart) > 0) {
- $query->andWhere(['>=', 'u.money', $this->moneyStart]);
- }
- if (floatval($this->moneyEnd) > 0) {
- $query->andWhere(['<=', 'u.money', $this->moneyEnd]);
- }
- if (floatval($this->integralStart) > 0) {
- $query->andWhere(['>=', 'u.integral', $this->integralStart]);
- }
- if (floatval($this->integralEnd) > 0) {
- $query->andWhere(['<=', 'u.integral', $this->integralEnd]);
- }
- if (isset($this->money_month_min)) {
- $query->andWhere(['>', 'u.money_month', $this->money_month_min]);
- }
- if ($this->user_id) {
- $query->andWhere(['u.id' => $this->user_id]);
- }
- if ($this->phone) {
- $query->andWhere(['like', 'u.binding', $this->phone]);
- }
- if ($this->user_remark) {
- $query->andWhere(['like', 'u.user_remark', $this->user_remark]);
- }
- if ($this->name) {
- $query->andWhere(['OR', ['like', 'su.name', $this->name], ['like', 'u.nickname', $this->name]]);
- }
- // 日期
- if ($this->dateStart) {
- $query->andWhere(['>=', 'u.created_at', strtotime($this->dateStart)]);
- }
- if ($this->dateEnd) {
- $query->andWhere(['<=', 'u.created_at', strtotime($this->dateEnd)]);
- }
- if ($this->is_admin == 1) {
- $query->andWhere(['u.is_admin' => 1]);
- } elseif ($this->is_admin == 0) {
- $query->andWhere(['u.is_admin' => 0]);
- }
- if (!empty($this->user_label_id)) {
- $query->andWhere(['like','user_label_id' , ','.$this->user_label_id.',']);
- }
- // $orderQuery = Order::find()->where([
- // 'store_id' => $this->store_id, 'is_delete' => 0,
- // 'mch_id' => 0
- // ])->andWhere('user_id = u.id')->andWhere(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])->select('count(1)');
- // $cardQuery = UserCard::find()->where(['store_id' => $this->store_id, 'is_delete' => 0])
- // ->andWhere('user_id = u.id')->select('count(1)');
- // 等级
- if ($this->level || $this->level === '0' || $this->level === 0) {
- $query->andWhere(['u.level' => $this->level]);
- }
- // if ($this->flag === Export::EXPORT) {
- // $orderConsumeQuery = Order::find()->where(['store_id' => $this->store_id, 'is_delete' => 0])->andWhere(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])->andWhere('user_id = u.id')->select('sum(pay_price)');
- // $list = $query->select([
- // 'u.*', 's.name shop_name', 'l.name l_name', 'card_count' => $cardQuery, 'order_count' => $orderQuery,
- // 'orderConsume' => $orderConsumeQuery,
- // ])->orderBy('u.created_at DESC')->asArray()->all();
- // $export = new ExportList();
- // $export->fields = $this->fields;
- // $export->UserExportData($list);
- // }
- $slectExt = [];
- if($this->is_yinbao){
- $slectExt = ['pu.json pu_json'];
- }
- $query->groupBy('u.id')->orderBy('u.id DESC');
- if (intval($this->is_export) === 1) {
- return $this->exportUserInfo($query);
- }
- $query->select(array_merge(['u.*', 'o.order_count', 'o.pay_price_sum'], $slectExt));
- $pagination = pagination_make($query);
- $list = $pagination['list'];
- // 会员列表
- $level = Level::find()->select('level, name')->where([
- 'store_id' => $this->store_id,
- 'is_delete' => Level::NOT_DELETE,
- 'status' => Level::STATUS_TRUE
- ])->asArray()->all();
- $level[] = ['level' => '-1', 'name' => '普通用户'];
- foreach ($list as $key => &$value) {
- //id转化成邀请码
- $list[$key]['invite_code'] = InviteCode::id2Code($value['id']);
- $level_ = Level::findOne(['level' => $value['level'], 'is_delete' => 0, 'store_id' => $this->store_id]);
- if ($level_) {
- $value['l_name'] = $level_->name;
- }
- if(!empty($value['user_label_id'])){
- $userLabelRes = UserLabel::getLabelNames($this->store_id, trim($value['user_label_id'],','));
- $value['user_label_name'] = $userLabelRes ?: [];
- $value['user_label_id'] = explode(',',trim($value['user_label_id'],','));
- }else{
- $value['user_label_name'] = [];
- }
- if (empty($value['l_name'])) {
- $list[$key]['l_name'] = '普通用户';
- }
- // 25-4-23分支要求秀嘎 这里不管就显示商城的头像和昵称
- $value['saas_name'] = $value['nickname'];
- $value['saas_avatar'] = $value['avatar_url'];
- // if (empty($value['binding'])) {
- // $value['saas_name'] = $value['nickname'];
- // $value['saas_avatar'] = $value['avatar_url'];
- // } else {
- $sass_user = SaasUser::findOne(['mobile' => $value['binding']]);
- if ($sass_user) {
- $value['saas_id'] = $sass_user->id;
- }
- // }
- $user = User::find()->where(['store_id' => $this->store_id, 'id' => $value['parent_id']])
- ->select('nickname')->asArray()->One();
- // 成交订单数
- // $list[$key]['orderCount'] = Order::find()
- // ->where(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])
- // ->andWhere(['is_delete' => 0])
- // ->andWhere(['is_pay' => Order::IS_PAY_TRUE, 'store_id' => $this->store_id, 'user_id' => $value['id']])
- // ->count();
- //
- // // 成交金额
- // $list[$key]['priceTotal'] = sprintf('%.2f', Order::find()->where(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])
- // ->andWhere(['is_pay' => Order::IS_PAY_TRUE, 'store_id' => $this->store_id, 'user_id' => $value['id']])
- // ->sum('pay_price'));
- $list[$key]['orderCount'] = $list[$key]['order_count'] ?: 0;
- $list[$key]['priceTotal'] = $list[$key]['pay_price_sum'] ?: '0.00';
- unset($list[$key]['order_count'], $list[$key]['pay_price_sum']);
- $list[$key]['parent_name'] = !empty($user['nickname']) ? $user['nickname'] : '总店';
- $list[$key]['nickname'] = $value['nickname'] ?: $value['saas_name'];
- $list[$key]['avatar_url'] = !empty($value['saas_avatar']) ? $value['saas_avatar'] : $value['avatar_url'];
- $list[$key]['create_at_text'] = date('Y-m-d H:i:s', $value['created_at']);
- if (strpos($value['binding'], 'X')) {
- $value['binding'] = '';
- }
- //门店增加收银台 查询商品列表逻辑
- if (!empty($this->md_id) && $this->md_id > 0) {
- $list[$key]['binding'] = substr_replace($value['binding'], '****', 3, 4);
- }
- // TODO: 其他数据
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'level' => $level,
- 'data' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ],
- ];
- }
- /**
- * 查看用户详情列表(积分、余额、会员记录)
- * @return array
- */
- public function getUserIntegralRechargeList()
- {
- $query = User::find()->orderBy('created_at DESC');
- $pagination = pagination_make($query);
- $list = $pagination['list'];
- foreach ($list as $key => $value) {
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ],
- ];
- }
- /**
- * 更新用户信息
- * @return array
- * @throws \yii\base\Exception
- */
- public function updateUserInfo()
- {
- try {
- if ($this->validate()) {
- if ($this->id == $this->parent_id || $this->id == $this->old_parent_id || $this->id == $this->init_parent_id) {
- return [
- 'code' => 1,
- 'msg' => '不能添加自己为上级'
- ];
- }
- $user = User::findOne(['id' => $this->id, 'is_delete' => User::USER_NOT_DELETE]);
- if ($user->binding != $this->binding) {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $is = User::findOne(['binding' => $this->binding, 'is_delete' => 0]);
- if ($is) {
- throw new \Exception('该手机号已被使用');
- }
- $users = User::find()->where(['binding' => $user->binding, 'is_delete' => 0])->all();
- foreach ($users as $userItem) {
- $userItem->binding = $this->binding;
- $userItem->save();
- }
- $saasUser = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
- if ($saasUser) {
- $saasUser->mobile = $this->binding;
- $saasUser->save();
- }
- $t->commit();
- } catch (\Exception $ex) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $ex->getMessage(),
- ];
- }
-
- }
- $oldParentChange = 0;
- $parentChange = 0;
- $beforeOldParentId = $user->old_parent_id;
- $beforeParentId = $user->parent_id;
- if($user->old_parent_id != $this->old_parent_id){
- $oldParentChange = 1;
- }
- if($user->parent_id != $this->parent_id){
- $parentChange = 1;
- }
- debug_log('parentChange => ' . $parentChange, 'aaaa.log');
- debug_log('oldParentChange => ' . $oldParentChange, 'aaaa.log');
- $diff_price = bcsub($this->price, $user->price, 2);
- if ($diff_price > 0) {
- $user->total_price = bcadd($user->total_price, $diff_price, 2);
- }
- if (!empty($this->password)) {
- $user->password = \Yii::$app->security->generatePasswordHash($this->password);
- }
- if (!empty($this->nickname)) {
- $user->nickname = $this->nickname;
- }
- $user->level = $this->level;
- $user->price = $this->price;
- $user->parent_id = $this->parent_id;
- $user->old_parent_id = $this->old_parent_id;
- $user->init_parent_id = $this->init_parent_id;
- $user->contact_way = $this->contact_way;
- $user->remark_name = $this->remark_name;
- $user->comments = $this->comments;
- $user->blacklist = $this->blacklist;
- $user->is_saas_clerk = $this->is_saas_clerk;
- if ($this->user_label_id) {
- $user->user_label_id = ','.implode(',',$this->user_label_id).',';
- }
- $group_setting = OptionSetting::getOptionSetting();
- $group_arr = $group_setting['pay']['list'] ?: [];
- foreach ($group_arr as $val) {
- if ($val['name'] == 'payment') {
- $group_data = Option::findOne(['store_id' => $this->store_id, 'group' => 'user', 'name' => $val['name'].'_'.$user->id]);
- $payment[] = [
- 'name' => $group_data ? $group_data->name : $val['name'].'_'.$user->id,
- 'text' => $val['text'],
- 'group' => $group_data ? $group_data->group : 'user',
- 'type' => $val['type'],
- 'value' => $this->payment,
- 'updated_at' => $group_data ? $group_data->updated_at : 0,
- ];
- $empty = 1;
- foreach($this->payment as $item){
- if($item['value']){
- $empty = 0;
- break;
- }
- }
- if($empty){
- if($group_data){
- Option::deleteAll(['store_id' => $this->store_id, 'group' => $payment[0]['group'], 'name' => $payment[0]['name']]);
- }
- }else{
- $res = Option::setGroup('user',$payment, $user->store_id);
- }
- }
- }
- $levelData = Level::findOne(['level' => $this->level, 'promotion_status' => 1, 'store_id' => $this->store_id, 'is_delete' => Level::NOT_DELETE]);
- if (!empty($levelData) && $user->is_distributor != 1) {
- $user->is_distributor = 1;
- $share = Share::findOne(['user_id' => $user->id, 'store_id' => $user->store_id, 'is_delete' => 0]);
- if (!$share) {
- $share = new Share();
- $share->user_id = $user->id;
- $share->mobile = $user->binding;
- $share->name = $user->nickname;
- $share->is_delete = 0;
- $share->store_id = $user->store_id;
- $share->status = 1;
- $share->created_at = time();
- if (!$share->save()) {
- throw new \Exception('添加用户分销商信息失败' . $share->getErrors());
- }
- }
- }
- if ($user->save()) {
- $oldParentChange && \app\utils\Share\BonusPool::addShareHolderParentOutLog($user, $beforeOldParentId, ShareHolderParentOutLog::getConditionName(11), 1);
- $parentChange && \app\utils\Share\BonusPool::addShareHolderParentOutLog($user, $beforeParentId, ShareHolderParentOutLog::getConditionName(11), 0);
- if ($diff_price != 0) {
- if ($diff_price > 0) {
- $str = '后台修改佣金-增加';
- $type = 0;
- } else {
- $str = '后台修改佣金-减少';
- $type = 1;
- }
- $diff_price = abs($diff_price);
- UserShareMoney::set($diff_price, $user->id, 0, $type, 10, $user->store_id, 0, $str);
- }
- return [
- 'code' => 0,
- 'msg' => '更新成功'
- ];
- }
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => '更新失败'.$e->getMessage(),
- ];
- }
- }
- /**
- * 删除管理员
- * @return array|bool
- */
- public function delAdmin()
- {
- $id = $this->id;
- $user = User::findOne($id);
- if (empty($user)) {
- return [
- 'code' => 1,
- 'msg' => '未选择任何数据'
- ];
- }
- $user->is_admin = 0;
- if (!$user->save()) {
- return [
- 'code' => 1,
- 'msg' => '删除失败' . $user->errors
- ];
- }
- return [
- 'code' => 0,
- 'msg' => '删除成功'
- ];
- }
- /**
- * 导出用户数据
- */
- public function exportUserInfo($query) {
- $query->select('u.id, su.id saas_id, su.name saas_name, u.binding, u.level l_name, u.created_at, u.price, u.total_price, u.integral, u.total_integral, u.money, u.nickname');
- $list = $query->asArray()->all();
- foreach ($list as $key => &$value) {
- $level = Level::findOne(['level' => $value['l_name'], 'is_delete' => 0, 'store_id' => $this->store_id]);
- $value['l_name'] = $level->name ?: '普通用户';
- if (empty($value['binding'])) {
- $value['saas_name'] = $value['nickname'];
- }
- // 成交订单数
- $value['orderCount'] = count(Order::find()
- ->where(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])
- ->andWhere(['is_pay' => Order::IS_PAY_TRUE, 'store_id' => $this->store_id, 'user_id' => $value['id']])
- ->asArray()->all());
- // 成交金额
- $value['priceTotal'] = sprintf('%.2f', Order::find()->where(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])
- ->andWhere(['is_pay' => Order::IS_PAY_TRUE, 'store_id' => $this->store_id, 'user_id' => $value['id']])
- ->sum('pay_price'));
- // $list[$key]['parent_name'] = !empty($user['nickname']) ? $user['nickname'] : '总店';
- // $list[$key]['nickname'] = $value['nickname'] ?: $value['saas_name'];
- $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
- if (strpos($value['binding'], 'X')) {
- $value['binding'] = '';
- }
- unset($list[$key]['nickname']);
- // TODO: 其他数据
- }
- $list = array_values($list);
- $list = array_merge([
- [
- 'id' => "主键",
- 'saas_id' => '联盟用户主键',
- 'saas_name' => '用户昵称',
- 'binding' => "手机号",
- 'l_name' => "用户等级",
- 'created_at' => "创建时间",
- 'price' => "可提现佣金",
- 'total_price' => "总佣金",
- 'integral' => "积分",
- 'total_integral' => "总积分",
- 'money' => "钱包余额",
- 'orderCount' => "订单总数量",
- 'priceTotal' => "订单总金额",
- ]
- ], $list);
- $writer = SimpleExcelWriter::streamDownload('store_user_' . time() . '.xlsx')->noHeaderRow()
- ->addRows($list)->toBrowser();
- }
- public function addAddress() {
- try {
- $user_id = $this->user_id;
- $name = $this->name;
- $mobile = $this->mobile;
- $detail = $this->detail;
- $province_id = $this->province_id;
- $city_id = $this->city_id;
- $district_id = $this->district_id;
- $is_default = $this->is_default;
- $store_id = $this->store_id;
- $id = $this->id;
- $latitude = $this->latitude;
- $longitude = $this->longitude;
- $user = User::findOne($user_id);
- $saas_user = SaasUser::findOne(['mobile' => $user->binding]);
- $address = Address::findOne($id) ?: new Address();
- $address->store_id = $store_id;
- $address->user_id = $saas_user->id;
- $address->is_delete = Address::DELETE_STATUS_FALSE;
- $address->addtime = time();
- $address->name = trim($name);
- $address->mobile = $mobile;
- $address->detail = trim($detail);
- $province = District::findOne($province_id);
- if (!$province) {
- throw new \Exception('省份数据错误,请重新选择');
- }
- $address->province_id = $province->id;
- $address->province = $province->name;
- if ($this->longitude) {
- $address->longitude = $this->longitude;
- }
- if ($this->latitude) {
- $address->latitude = $this->latitude;
- }
- $city = District::findOne($city_id);
- if (!$city) {
- throw new \Exception('城市数据错误,请重新选择');
- }
- $address->city_id = $city->id;
- $address->city = $city->name;
- $district = District::findOne($district_id);
- if (!$district) {
- throw new \Exception('地区数据错误,请重新选择');
- }
- $address->district_id = $district->id;
- $address->district = $district->name;
- $address->is_default = $is_default ?: 0;
- $address->latitude = $latitude ?: '0.00';
- $address->longitude = $longitude ?: '0.00';
- if (!$address->save()) {
- throw new \Exception(json_encode($address->errors, JSON_UNESCAPED_UNICODE));
- }
- if (intval($is_default)) {
- $this->id = $address->id;
- $result = $this->setUserAddressStatus();
- if ($result['code'] !== 0) {
- return $result;
- }
- }
- $address = Address::find()->where([
- 'user_id' => $saas_user->id,
- 'is_delete' => 0,
- ])->asArray()->all();
- return [
- 'code' => 0,
- 'msg' => "设置成功",
- 'data' => $address
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- public function setUserAddressStatus() {
- try {
- $type = intval($this->type);
- $id = $this->id;
- $address = Address::findOne(['is_delete' => 0, 'id' => $id]);
- if (empty($address)) {
- throw new \Exception('查询失败');
- }
- if ($type) {
- //删除
- $address->is_delete = 1;
- } else {
- //设置为默认
- $user_id = $address->user_id;
- Address::updateAll(['is_default' => 0], ['user_id' => $user_id, 'is_delete' => 0]);
- $address = Address::findOne($id);
- $address->is_default = 1;
- }
- if (!$address->save()) {
- throw new \Exception(json_encode($address->errors, JSON_UNESCAPED_UNICODE));
- }
- return [
- 'code' => 0,
- 'msg' => '设置成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- public function getUserAddress() {
- try {
- $user_id = $this->user_id;
- $user = User::findOne($user_id);
- if (empty($user)) {
- throw new \Exception('用户查询失败');
- }
- if($user->binding){
- $saas_user = SaasUser::findOne(['mobile' => $user->binding]);
- }else{
- if($user->alipay_open_id){
- $saas_user = SaasUser::findOne(['ali_user_id' => $user->alipay_open_id]);
- }
- }
- if(empty($saas_user)) {
- throw new \Exception('用户查询失败');
- }
- $address_list = Address::find()->where(['user_id' => $saas_user->id, 'is_delete' => 0])->select('name, mobile, province, city, district, town, village, detail, is_default, id, latitude, longitude, province_id, city_id, district_id, town_id, village_id')->orderBy('addtime desc')->asArray()->all();
- foreach ($address_list as &$item) {
- $item['is_default'] = (int)$item['is_default'];
- }
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => [
- 'address_list' => $address_list
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function userPriceTotal($params) {
- $store_id = $this->store_id;
- $user_id = $this->user_id;
- if($params['user_id']){
- $user_id = $params['user_id'];
- }
- $query = User::find()->where(['store_id' => $store_id, 'is_delete' => 0]);
- if ($user_id) {
- $query->andWhere(['id' => $user_id]);
- }
- if ($params['mobile']) {
- $query->andWhere(['like', 'binding', $params['mobile']]);
- }
- if ($params['name']) {
- $query->andWhere(['!=', 'binding', ''])->andWhere(['binding' => SaasUser::find()->select('mobile')->where(['like', 'name', $params['name']])]);
- }
- $sum = (clone $query)->select('SUM(price) sum_price, SUM(total_price) sum_total_price')->asArray()->one();
- $query->orderBy('id DESC');
- $list = pagination_make($query);
- foreach($list['list'] as &$item){
- $item['saasUser'] = $item['binding'] ? SaasUser::findOne(['mobile' => $item['binding']]) : null;
- $item['sum']['sumUserShareMoney'] = (float)\app\models\UserShareMoney::find()->where(['user_id' => $item['id'], 'is_delete' => 0, 'type' => 0])->sum('money');
- $item['sum']['sumShareHold'] = (float)\app\models\ShareDetail::find()->where(['user_id' => $item['id'], 'is_send' => 1, 'is_delete' => 0, 'type' => [
- \app\models\ShareDetail::TYPE_HOLDER_PROFIT,
- \app\models\ShareDetail::TYPE_RANGE_PROFIT,
- \app\models\ShareDetail::TYPE_SHARE_PROFIT,
- \app\models\ShareDetail::TYPE_RANGE_PROFIT_OLD,
- \app\models\ShareDetail::TYPE_RANGE_PROFIT_OLD_USER_LEVEL,
- ]])->sum('money');
- $item['sum']['sumTwoPlusOne'] = (float)\app\models\ShareDetail::find()->where(['user_id' => $item['id'], 'is_send' => 1, 'is_delete' => 0, 'type' => [
- \app\models\ShareDetail::TYPE_TWO_PLUS_ONE_SHARE_PROFIT,
- \app\models\ShareDetail::TYPE_TWO_PLUS_ONE_SHARE_TEAM_PROFIT,
- \app\models\ShareDetail::TYPE_TWO_PLUS_ONE_SHARE_TEAM_PROFIT_OLD,
- ]])->sum('money');
- $item['sum']['sumAreaProfit'] = (float)\app\models\ShareDetail::find()->where(['user_id' => $item['id'], 'is_send' => 1, 'type' => \app\models\ShareDetail::TYPE_AREA_PROFIT, 'is_delete' => 0])->sum('money');
- $item['sum']['sumActivityRebateOrderN'] = (float)\app\models\ActivityRebateOrderNLog::find()->where(['user_id' => $item['id'], 'status' => 1, 'is_delete' => 0, 'finished' => 1])->sum('price');
- $item['sum']['sumFreeQueue'] = (float)\app\models\QueueLog::find()->where(['user_id' => $item['id']])->sum('returned_money');
- $item['sum']['sumActivityOrderRebateSelf'] = (float)\app\models\ActivityOrderRebateSelfLog::find()->where(['user_id' => $item['id'], 'send_type' => 1, 'is_send' => 1])->sum('price_send');
- $item['sum']['sumReportPoolLevel'] = (float)\app\models\ReportPoolLevel::find()->where(['user_id'=>$item['id'], 'is_send'=>1, 'is_delete'=>0])->sum('dividend_money');
- $item['sum']['sumVideoGoodsShare'] = (float)\app\models\VideoGoodsShare::find()->where(['author_user_id' => $item['id'], 'is_send' => 1])->sum('proportion');
- $item['sum']['sumWorker'] = (float)\app\models\WorkerOrderExt::find()->where(['worker_id' => \app\models\Worker::find()->select('id')->where(['user_id' => $item['id']]), 'worker_is_price' => 1])->sum('worker_price');
- $item['sum']['sumBalanceToCommission'] = (float)\app\models\AccountLog::find()->where([
- 'user_id'=>$item['id'], 'order_type'=>\app\models\AccountLog::TYPE_BALANCE_TO_COMMISSION,
- 'type'=>\app\models\AccountLog::TYPE_BALANCE, 'log_type'=>\app\models\AccountLog::LOG_TYPE_EXPEND,
- ])->sum('amount');
- $item['sum']['sumTeamBonus'] = (float)\app\models\ShareDetail::find()->where(['user_id' => $item['id'], 'is_send' => 1, 'is_delete' => 0, 'type' => [
- \app\models\ShareDetail::TYPE_TEAM_BONUS_RANGE_PROFIT,
- \app\models\ShareDetail::TYPE_TEAM_BONUS_SAME_LEVEL,
- \app\models\ShareDetail::TYPE_TEAM_BONUS_PARENT,
- \app\models\ShareDetail::TYPE_TEAM_BONUS_THANKS,
- ]])->sum('money');
- $item['_sum_sum'] = round(array_sum($item['sum']), 2);
- }
- return [
- 'list' => $list,
- 'sum' => $sum,
- ];
- }
- /**
- * 获取用户佣金列表
- */
- public function getUserPrice() {
- try {
- $user_id = $this->user_id;
- $store_id = $this->store_id;
- $pageNo = get_params('pageNo', 1);
- $pageSize = get_params('pageSize', \Yii::$app->params['pageSize']);
- $cashTable = Cash::tableName();
- $userShareMoneyTable = UserShareMoney::tableName();
- $shareDetailTable = ShareDetail::tableName();
- // price_type = 0 收入类型 1支出类型
- // status 如果是支出类型 0--申请中 1--确认申请 2--已打款 3--驳回 4--手动打款 5--余额通过 收入类型 则一直为2已打款
- // table_type
- $sql = "select * from (SELECT 0 AS table_type, money, `desc`, 0 AS `price_type`, 2 AS `status`, 0 AS type, source, created_at FROM {$userShareMoneyTable} WHERE store_id = {$store_id} AND user_id = {$user_id}
- UNION ALL SELECT 1 AS table_type, money, `desc`, 0 AS `price_type`, 2 AS `status`, type, 0 AS source, send_time AS created_at FROM {$shareDetailTable} WHERE store_id = {$store_id} AND user_id = {$user_id} AND is_send = 1
- UNION ALL SELECT 2 AS table_type, price AS money, '提现申请' AS `desc`, 1 AS `price_type`, `status`, 0 AS type, 0 as source, created_at FROM {$cashTable} WHERE store_id = {$store_id} AND user_id = {$user_id} AND status != 3) AS o ORDER BY o.created_at DESC";
- $pageInitNum = ($pageNo - 1) * $pageSize;
- $count = \Yii::$app->db->createCommand($sql)->query()->count();
- $sql .= " LIMIT {$pageSize} OFFSET {$pageInitNum}";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- foreach ($list as &$item) {
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- $item['status'] = intval($item['status']);
- $item['price_type'] = intval($item['price_type']);
- $item['status_name'] = "已打款";
- if ($item['price_type']) {
- $item['status_name'] = Cash::getStatusName($item['status']);
- }
- if (!intval($item['table_type'])) {
- if (empty($item['desc'])) {
- $item['desc'] = UserShareMoney::getSourceName($item['source'], $store_id);
- }
- }
- if (intval($item['table_type']) === 1) {
- if (empty($item['desc'])) {
- $item['desc'] = ShareDetail::typeName($item['type']);
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'pageNo' => $pageNo,
- 'totalCount' => $count,
- ],
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|