| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\constants\OptionSetting;
- use app\models\AccountLog;
- use app\models\Address;
- use app\models\common\InviteCode;
- use app\models\IntegralAppreciationUser;
- use app\models\IntegralAppreciationUserIntegralLog;
- use app\models\Level;
- use app\models\Option;
- use app\models\ReOrder;
- use app\models\LevelOrder;
- use app\models\SaaSLeaguePriceLog;
- use app\models\Store;
- use app\models\UserLabel;
- use Exception;
- use app\models\User;
- use app\models\SaasUser;
- use app\utils\Notice\NoticeSend;
- use yii\base\Model;
- use yii\db\ActiveQuery;
- use yii\db\Expression;
- use yii\db\Query;
- use Yii;
- use yii\helpers\Json;
- class AccountLogForm extends Model
- {
- public $export;
- public $admin;
- public $store_id;
- public $money;
- public $integral;
- public $desc;
- public $explain;
- public $type;
- public $recharge_type;
- public $user_id;
- public $is_we7;
- public $is_ind;
- public $pic_url;
- public $dateStart;
- public $dateEnd;
- public $name;
- public $saas_user_id;
- public $log_type;
- /**
- * @return array the validation rules.
- */
- public function rules()
- {
- return [
- [['explain', 'name'], 'trim'],
- [['explain', 'name', 'dateStart', 'dateEnd'], 'string'],
- [['type', 'recharge_type', 'user_id', 'log_type'], 'integer'],
- [['money', 'integral'], 'number'],
- [['export', 'desc'], 'safe'],
- ];
- }
- /**
- * 充值积分或者余额
- * @return array
- * @throws \yii\base\Exception
- */
- public function recharge()
- {
- if (
- !in_array($this->type, AccountLog::$valid_type)
- || !in_array($this->recharge_type, AccountLog::$type_budget)
- ) {
- return [
- 'code' => 1,
- 'msg' => '参数错误'
- ];
- }
- $user = User::findOne(['id' => $this->user_id, 'store_id' => $this->store_id]);
- if (!$user) {
- return [
- 'code' => 1,
- 'msg' => '用户不存在,或已删除',
- ];
- }
- if ($this->validate()) {
- // 积分
- if ($this->type == AccountLog::TYPE_INTEGRAL) {
- if (empty($this->integral)) {
- return [
- 'code' => 1,
- 'msg' => '积分设置不正确'
- ];
- }
- $accountLog = new AccountLog();
- $accountLog->store_id = get_store_id();
- $accountLog->user_id = $user->id;
- // TODO : admin
- // if ($this->is_we7) {
- // $admin = \Yii::$app->admin->identity;
- // } elseif ($this->is_ind) {
- // $admin = \Yii::$app->admin->identity;
- // } else {
- // $admin = \Yii::$app->mchRoleAdmin->identity;
- // }
- if ($this->recharge_type == AccountLog::LOG_TYPE_EXPEND) {
- if ($this->integral > $user->integral) {
- return [
- 'code' => 1,
- 'msg' => '用户当前积分不足',
- ];
- }
- $user->integral -= $this->integral;
- } elseif ($this->recharge_type == AccountLog::LOG_TYPE_INCOME) {
- $user->integral += $this->integral;
- $user->total_integral += $this->integral;
- }
- if (!$user->save()) {
- foreach ($user->errors as $error) {
- return [
- 'code' => 1,
- 'msg' => $error
- ];
- }
- }
- if ($this->recharge_type == AccountLog::LOG_TYPE_EXPEND) {
- $accountLog->desc = "管理员: " . $this->admin->username . " 后台操作账号:" . $user->nickname
- . " 积分扣除:" . $this->integral . " 积分";
- } elseif ($this->recharge_type == AccountLog::LOG_TYPE_INCOME) {
- $accountLog->desc = "管理员: " . $this->admin->username . " 后台操作账号:" . $user->nickname
- . " 积分充值:" . $this->integral . " 积分";
- }
- $accountLog->amount = $this->integral;
- $accountLog->created_at = time();
- $accountLog->operator = $this->admin->username ? $this->admin->username : '未知';
- $accountLog->store_id = $this->store_id;
- $accountLog->operator_id = $this->admin->id;
- $accountLog->order_type = AccountLog::TYPE_RECHARGE_ORDER;
- if ($this->recharge_type == AccountLog::LOG_TYPE_EXPEND) {
- $accountLog->before = $user->integral + $this->integral;
- $accountLog->after = $user->integral;
- } else {
- $accountLog->before = $user->integral - $this->integral;
- $accountLog->after = $user->integral;
- }
- $accountLog->log_type = $this->recharge_type;
- $accountLog->type = AccountLog::TYPE_INTEGRAL;
- $accountLog->operator_type = AccountLog::TYPE_OPERATOR_BACK;
- $accountLog->explain = $this->explain;
- if ($accountLog->save()) {
- return [
- 'code' => 0,
- 'msg' => '操作成功',
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '操作失败',
- ];
- }
- }
- // 余额
- if ($this->type == AccountLog::TYPE_BALANCE) {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => '参数有误'
- ];
- }
- $this->money = floatval($this->money);
- if ($this->money < 0) {
- return [
- 'code' => 1,
- 'msg' => '输入数值不能小于0'
- ];
- }
- $accountLog = new AccountLog();
- $accountLog->store_id = $this->store_id;
- $accountLog->user_id = $this->user_id;
- $accountLog->pic_url = $this->pic_url;
- $accountLog->explain = $this->explain;
- switch ($this->recharge_type) {
- case AccountLog::LOG_TYPE_INCOME:
- $user->money += $this->money;
- $accountLog->desc = "管理员: " . $this->admin->username . " 后台操作账号:"
- . $user->nickname . " 余额充值:" . $this->money . " 元";
- $accountLog->log_type = AccountLog::LOG_TYPE_INCOME;
- $desc = '充值';
- break;
- case AccountLog::LOG_TYPE_EXPEND:
- if ($user->money < $this->money) {
- return [
- 'code' => 1,
- 'msg' => '扣除数值大于当前用户余额'
- ];
- }
- $user->money -= $this->money;
- $accountLog->desc = "管理员: " . $this->admin->username . " 后台操作账号:" . $user->nickname
- . " 余额扣除:" . $this->money . " 元";
- $accountLog->log_type = AccountLog::LOG_TYPE_EXPEND;
- $desc = '扣除';
- break;
- default:
- return [
- 'code' => 1,
- 'msg' => '网络异常,请刷新重试'
- ];
- }
- if($this->desc){
- $accountLog->desc = $this->desc;
- }
- if ($user->save()) {
- // NoticeSend::UserChang($this->user_id, $user->binding, $this->money, $desc);
- $accountLog->amount = $this->money;
- $accountLog->operator = $this->admin->username;
- $accountLog->operator_id = $this->admin->id;
- $accountLog->created_at = time();
- $accountLog->type = AccountLog::TYPE_BALANCE;
- $accountLog->order_type = AccountLog::TYPE_RECHARGE_ORDER;
- if ($this->recharge_type == AccountLog::LOG_TYPE_EXPEND) {
- $accountLog->before = $user->money + $this->money;
- $accountLog->after = $user->money;
- } else {
- $accountLog->before = $user->money - $this->money;
- $accountLog->after = $user->money;
- }
- $accountLog->log_type = $this->recharge_type;
- $accountLog->operator_type = AccountLog::TYPE_OPERATOR_BACK;
- if (!$accountLog->save()) {
- // Yii::error($accountLog->errors);
- return [
- 'code' => 1,
- 'msg' => '记录日志错误'
- ];
- } else {
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- }
- } else {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- }
- //增值积分
- if ($this->type == AccountLog::TYPE_INTEGRAL_POOL) {
- if (empty($this->integral)) {
- return [
- 'code' => 1,
- 'msg' => '积分设置不正确'
- ];
- }
- $accountLog = new AccountLog();
- $accountLog->store_id = get_store_id();
- $accountLog->user_id = $user->id;
- $integral_appreciation_user = IntegralAppreciationUser::findOne(['user_id' => $user->id]);
- if (!$integral_appreciation_user) {
- $integral_appreciation_user = new IntegralAppreciationUser();
- $integral_appreciation_user->user_id = $user->id;
- $integral_appreciation_user->store_id = get_store_id();
- }
- //扣除
- if ($this->recharge_type == AccountLog::LOG_TYPE_EXPEND) {
- if(!$integral_appreciation_user || $integral_appreciation_user->integral < $this->integral){
- return [
- 'code' => 1,
- 'msg' => '扣除积分不足'
- ];
- }
- $integral_appreciation_user->integral-=$this->integral;
- } elseif ($this->recharge_type == AccountLog::LOG_TYPE_INCOME) {
- $integral_appreciation_user->integral += $this->integral;
- $integral_appreciation_user->total_integral += $this->integral;
- }
- if(!$integral_appreciation_user->save()){
- return [
- 'code' => 1,
- 'msg' => '操作失败'
- ];
- }
- if ($this->recharge_type == AccountLog::LOG_TYPE_EXPEND) {
- $accountLog->desc = "管理员: " . $this->admin->username . " 后台操作账号:" . $user->nickname
- . " 增值积分扣除:" . $this->integral . " 积分";
- IntegralAppreciationUserIntegralLog::saveIntegralLog(
- $integral_appreciation_user->id,
- $this->integral,
- IntegralAppreciationUserIntegralLog::TYPE_EXPEND,
- IntegralAppreciationUserIntegralLog::SOURCE_TYPE_TRANSFER_EXPEND,
- "后台扣除"
- );
- } elseif ($this->recharge_type == AccountLog::LOG_TYPE_INCOME) {
- $accountLog->desc = "管理员: " . $this->admin->username . " 后台操作账号:" . $user->nickname
- . " 增值积分充值:" . $this->integral . " 积分";
- IntegralAppreciationUserIntegralLog::saveIntegralLog(
- $integral_appreciation_user->id,
- $this->integral,
- IntegralAppreciationUserIntegralLog::TYPE_INCOME,
- IntegralAppreciationUserIntegralLog::SOURCE_TYPE_TRANSFER_INCOME,
- "后台充值"
- );
- }
- $accountLog->amount = $this->integral;
- $accountLog->created_at = time();
- $accountLog->operator = $this->admin->username ? $this->admin->username : '未知';
- $accountLog->store_id = $this->store_id;
- $accountLog->operator_id = $this->admin->id;
- $accountLog->order_type = AccountLog::TYPE_RECHARGE_ORDER;
- if ($this->recharge_type == AccountLog::LOG_TYPE_EXPEND) {
- $accountLog->before = $integral_appreciation_user->integral + $this->integral;
- $accountLog->after = $integral_appreciation_user->integral;
- } else {
- $accountLog->before = $integral_appreciation_user->integral - $this->integral;
- $accountLog->after = $integral_appreciation_user->integral;
- }
- $accountLog->log_type = $this->recharge_type;
- $accountLog->type = AccountLog::TYPE_INTEGRAL_POOL;
- $accountLog->operator_type = AccountLog::TYPE_OPERATOR_BACK;
- $accountLog->explain = $this->explain;
- if ($accountLog->save()) {
- return [
- 'code' => 0,
- 'msg' => '操作成功',
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '操作失败',
- ];
- }
- }
- }
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- public function batchRecharge()
- {
- $type = input_params('type', AccountLog::TYPE_BALANCE);
- $admin = Yii::$app->jwt->getAdmin();
- $store_id = $this->store_id;
- $data = [
- 'explain' => '',
- 'money' => 0,
- 'integral' => 0,
- 'recharge_type' => AccountLog::LOG_TYPE_INCOME,
- 'type' => $type,
- 'user_id' => 0,
- ];
- set_time_limit(0);
- $filename = $_FILES['excel']['name'];
- $tmpname = $_FILES['excel']['tmp_name'];
- $path = \Yii::$app->basePath . '/web/temp/';
- if(!is_dir($path)){
- mkdir($path);
- }
- $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
- if (($ext != 'xlsx') && ($ext != 'xls')) {
- return [
- 'code' => 1,
- 'msg' => '请上传excel文件'
- ];
- }
- $file = time() . $this->store_id . '.' . $ext;
- $uploadfile = $path . $file;
- move_uploaded_file($tmpname, $uploadfile);
- $rows = \Spatie\SimpleExcel\SimpleExcelReader::create($uploadfile)
- ->getRows();
- $err = [];
- $rows->each(function (array $item) use ($admin, $store_id, $data, &$err) {
- try{
- $mobile = trim($item['手机号']);
- $nickname = trim($item['昵称']);
- $change = trim($item['余额增减数额']);
- $desc = trim($item['余额变动备注']);
- $parent_phone = trim($item['推荐人']);
- $level_name = trim($item['会员等级']);
- $user = User::findOne(['binding' => $mobile, 'is_delete' => 0, 'store_id' => $store_id]);
- $saas_user = SaasUser::findOne(['mobile' => $mobile, 'is_delete' => 0]);
- if(!$user){
- if (!$saas_user) {
- $saas_user = new SaasUser();
- $saas_user->access_token = \Yii::$app->security->generateRandomString();
- $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';;
- $saas_user->mobile = $mobile ?: '';
- $saas_user->name = $nickname ? $nickname : substr_replace($mobile, '******', 3, 6);
- $saas_user->store_id = $store_id;
- if (!$saas_user->save()) {
- throw new \Exception(array_shift($saas_user->getFirstErrors()));
- }
- }
- $user = new User();
- $user->access_token = \Yii::$app->security->generateRandomString();
- $user->binding = $saas_user->mobile;
- $user->nickname = $saas_user->name;
- $user->avatar_url = $saas_user->avatar;
- $user->username = \Yii::$app->security->generateRandomString();
- $user->password = \Yii::$app->security->generatePasswordHash(\Yii::$app->security->generateRandomString(), 5);
- $user->auth_key = \Yii::$app->security->generateRandomString();
- $user->store_id = $store_id;
- if ($parent_phone) {
- $parent = User::findOne(['binding' => $parent_phone, 'is_delete' => 0, 'store_id' => $store_id]);
- if ($parent) {
- $user->parent_id = $parent->id;
- $user->old_parent_id = $parent->id;
- }
- }
- if ($level_name) {
- $level = Level::findOne(['store_id' => $store_id, 'name' => $level_name, 'is_delete' => 0]);
- if ($level) {
- $user->level = $level->level;
- }
- }
- if (!$user->save()) {
- throw new \Exception(array_shift($user->getFirstErrors()));
- }
- } else {
- // 已经存在,就更新
- if($nickname && $nickname != $saas_user->name){
- $saas_user->name = $nickname;
- if (!$saas_user->save()) {
- throw new \Exception(array_shift($saas_user->getFirstErrors()));
- }
- }
- $isEdit = false;
- if($nickname && $nickname != $user->nickname){
- $isEdit = true;
- $user->nickname = $nickname;
- }
- if ($parent_phone) {
- $parent = User::findOne(['binding' => $parent_phone, 'is_delete' => 0, 'store_id' => $store_id]);
- if ($parent && $parent->id != $user->old_parent_id) {
- if ($user->parent_id == $user->old_parent_id) {
- $user->parent_id = $parent->id;
- }
- $user->old_parent_id = $parent->id;
- $isEdit = true;
- }
- }
- if ($level_name) {
- $level = Level::findOne(['store_id' => $store_id, 'name' => $level_name, 'is_delete' => 0]);
- if ($level && $level->level != $user->level) {
- $user->level = $level->level;
- $isEdit = true;
- }
- }
- if ($isEdit) {
- if (!$user->save()) {
- throw new \Exception(array_shift($user->getFirstErrors()));
- }
- }
- }
- if ($change) {
- $recharge_type = ($change > 0) ? AccountLog::LOG_TYPE_INCOME : AccountLog::LOG_TYPE_EXPEND;
- $data['user_id'] = $user['id'];
- $data['desc'] = $desc;
- $data['recharge_type'] = $recharge_type;
- if($data['type'] == AccountLog::TYPE_BALANCE){
- $data['money'] = abs($change);
- if($recharge_type == AccountLog::LOG_TYPE_EXPEND && $user->money < $data['money']){
- $data['money'] = $user->money;
- }
- if($data['money'] == 0){
- return;
- }
- }
- if($data['type'] == AccountLog::TYPE_INTEGRAL){
- $data['integral'] = abs($change);
- if($recharge_type == AccountLog::LOG_TYPE_EXPEND && $user->integral < $data['integral']){
- $data['integral'] = $user->integral;
- }
- if($data['integral'] == 0){
- return;
- }
- }
- $form = new AccountLogForm();
- $form->store_id = $store_id;
- $form->admin = $admin;
- $form->attributes = $data;
- $res = $form->recharge();
- }
- } catch (\Exception $e){
- debug_log($e->getMessage());
- $res = [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- if($res['code'] != 0){
- $err[] = array_merge($res, ['mobile' => $mobile]);
- }
- });
- $count = count($err);
- @unlink($uploadfile);
- return [
- 'code' => $count ? 1 : 0,
- 'msg' => "操作完成" . ($err ? ",失败{$count}条数据" : ''),
- 'err' => $err,
- ];
- }
- /**
- * 联盟券记录
- */
- public function getLeagueList()
- {
- $query = SaaSLeaguePriceLog::find()->where(['saas_user_id' => $this->saas_user_id, 'role' => SaaSLeaguePriceLog::ROLE_USER])->orderBy('id desc');
- if ($this->log_type !== null && in_array($this->log_type, [0, 1])) {
- $query->andWhere(['send_or_take_type' => $this->log_type]);
- }
- $pagination = pagination_make($query);
- $saas_model = (new SaasUsersForm())->getUserInfo(['id' => $this->saas_user_id])['data'];
- $list = $pagination['list'];
- foreach ($list as & $arr) {
- $store = Store::findOne($arr['store_id']);
- if($store){
- $arr['store_name'] = $store->name;
- }else{
- $arr['store_name'] = '/';
- }
- $arr['addtime'] = date('Y-m-d H:i:s',$arr['addtime']);
- if($arr['type'] == 0){
- $arr['type_str'] = '商城转赠';
- }else if($arr['type'] == 1){
- $arr['type_str'] = '平台修改';
- }else if($arr['type'] == 2){
- $arr['type_str'] = '消费获赠';
- }else if($arr['type'] == 3){
- $arr['type_str'] = '订单取消退回';
- }else if($arr['type'] == 4){
- $arr['type_str'] = '下单抵扣';//支出
- }
- if($arr['send_or_take_type'] == 0){
- $arr['send_or_take_type_str'] = '收入';
- }else{
- $arr['send_or_take_type_str'] = '支出';
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'saas_user_id' => $this->saas_user_id,
- 'details' => $saas_model,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ],
- ];
- }
- /**
- * 充值记录展示
- */
- public function getRechargeList()
- {
- if ($this->type == AccountLog::TYPE_INTEGRAL) {
- return $this->getIntegralRechargeList();
- }
- if ($this->type == 3) {
- return $this->getLevelOrderList();
- }
- if ($this->user_id) {
- $query = AccountLog::find()->alias('al')->where(['al.type' => $this->type, 'al.store_id' => $this->store_id, 'al.user_id' => $this->user_id])->leftJoin(['u' => User::tableName()], 'u.id=' . $this->user_id);
- $details = $this->getUserInfo();
- } else {
- $query = AccountLog::find()->alias('al')->where([
- 'al.type' => $this->type, 'al.store_id' => $this->store_id
- ])->leftJoin(['u' => User::tableName()], 'u.id=al.user_id');
- }
- if ($this->name) {
- $query->andWhere(['like', 'u.nickname', $this->name]);
- }
- if ($this->dateStart) {
- $query->andWhere(['>=', 'al.created_at', strtotime($this->dateStart)]);
- }
- if ($this->dateEnd) {
- $query->andWhere(['<=', 'al.created_at', strtotime($this->dateEnd)]);
- }
- $query->orderBy('al.id desc')->select('al.*, u.nickname, u.avatar_url');
- $pagination = pagination_make($query);
- $list = $pagination['list'];
- foreach ($list as $key => $value) {
- $list[$key]['send_price'] = 0.00;
- if (
- $value['order_type'] == AccountLog::TYPE_RECHARGE_ORDER
- && $value['operator_type'] == AccountLog::TYPE_OPERATOR_NORMAL
- ) {
- $reOrder = ReOrder::find()->where([
- 'id' => $value['order_id'], 'is_pay' => ReOrder::IS_PAY,
- 'is_delete' => ReOrder::NOT_DELETE
- ])->asArray()->One();
- $list[$key]['send_price'] = $reOrder['send_price'];
- }
- $list[$key]['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
- }
- $level = $this->getLevelList();
- $user = User::findOne($details['id']);
- $userLabel = UserLabel::getLabel($user->store_id, $user->user_label_id);
- $details['user_label_id'] = empty($user->user_label_id) ? [] : explode(',',trim($user->user_label_id,','));
- $details['user_label_name'] = $userLabel->label_name ?: '';
- $details['nickname'] = $user->nickname;
- $details['avatar_url'] = $user->avatar_url;
- $details['address_list'] = [];
- //id转换成推荐码
- $details['invite_code'] = InviteCode::id2Code($details['id']);
- 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($saas_user){
- // $details['nickname'] = $saas_user->name;
- // $details['avatar_url'] = $saas_user->avatar;
- $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'];
- }
- $details['address_list'] = $address_list;
- }
- $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]);
- if(!$group_data){
- foreach($val['default'] as &$item){
- $item['value'] = 0;
- }
- }else{
- $group_data_val = Json::decode($group_data->value);
- foreach($val['default'] as $k => &$item){
- if(isset($group_data_val[$k])){
- $item['value'] = $group_data_val[$k]['value'];
- continue;
- }
- $item['value'] = 0;
- }
- }
- $details['payment'] = $val['default'];
- $details['$group_data'] = $group_data;
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'detail' => $details,
- 'level' => $level,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ],
- ];
- }
- private function export($list) {
- $rows = [[
- '类型',
- '数额',
- '更改前',
- '更改后',
- '操作时间',
- '操作者',
- '说明',
- ]];
- foreach($list as $item){
- $r = [
- $item['log_type'] == 2 ? '支出' : '收入',
- $item['amount'],
- $item['before'],
- $item['after'],
- date('Y-m-d H:i:s',$item['created_at']),
- $item['operator'],
- $item['desc'],
- ];
- $rows[] = $r;
- }
- $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
- ->addRows($rows)->toBrowser();
- }
- /**
- * 充值记录展示
- */
- public function getIntegralRechargeList()
- {
- if ($this->user_id) {
- $query = AccountLog::find()->alias('al')->where(['al.type' => $this->type, 'al.store_id' => $this->store_id, 'al.user_id' => $this->user_id]);
- $details = $this->getUserInfo();
- } else {
- $query = AccountLog::find()->alias('al')->where(
- ['al.type' => $this->type, 'al.store_id' => $this->store_id]
- )->leftJoin(['u' => User::tableName()], 'u.id=al.user_id')
- ->select('al.*, u.nickname, u.avatar_url');
- }
- if ($this->name) {
- $query->andWhere(['like', 'u.nickname', $this->name]);
- }
- if ($this->dateStart) {
- $query->andWhere(['>=', 'al.created_at', strtotime($this->dateStart)]);
- }
- if ($this->dateEnd) {
- $query->andWhere(['<=', 'al.created_at', strtotime($this->dateEnd)]);
- }
- $query->orderBy('al.created_at desc');
- $pagination = pagination_make($query);
- $list = $pagination['list'];
- if($this->export){
- return $this->export($list);
- }
- $level = $this->getLevelList();
- $user = User::findOne($details['id']);
- $details['nickname'] = $user->nickname;
- $details['avatar_url'] = $user->avatar_url;
- 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($saas_user){
- $details['nickname'] = $saas_user->name;
- $details['avatar_url'] = $saas_user->avatar;
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'level' => $level,
- 'detail' => $details,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ],
- ];
- }
- /**
- * 获取个人详情
- * @return void
- */
- public function getUserInfo()
- {
- $data = User::find()->alias('u')->where([
- 'u.id' => $this->user_id,
- 'u.type' => User::USER_TYPE_NORMAL,
- 'u.store_id' => $this->store_id,
- 'u.is_delete' => User::USER_NOT_DELETE,
- ])->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding')
- ->select('u.*,su.avatar sass_avatar,su.name sass_name')->asArray()->one();
- $data['user_label_id'] = $data['user_label_id'] == 0 ? [] : explode(',',trim($data['user_label_id'],','));
- // 上级id
- // $data['nickname'] = !empty($data['sass_name']) ? $data['sass_name'] : $data['nickname'];
- // $data['avatar_url'] = !empty($data['sass_avatar']) ? $data['sass_avatar'] : $data['avatar_url'];
- $user = User::findOne($data['parent_id']);
- // $user = SaasUser::find()->where(['mobile' => User::findOne($data['parent_id'])->binding])
- // ->select('name')->asArray()->One();
- $data['parent_name'] = !empty($user['nickname']) ? $user['nickname'] : '总店';
- $data['old_parent_name'] = '总店';
- if ($data['old_parent_id'] > 0) {
- // 这里该用用户表的昵称
- $oldParent = User::findOne($data['old_parent_id']);
- // $oldUser = SaasUser::find()->where(['mobile' => User::findOne($data['old_parent_id'])->binding])
- // ->select('name')->asArray()->One();
- $data['old_parent_name'] = !empty($oldParent->nickname) ? $oldParent->nickname : '总店';
- }
- $data['init_parent_name'] = '总店';
- if ($data['init_parent_id'] > 0) {
- $initUser = User::findOne($data['init_parent_id']);
- // $initUser = SaasUser::find()->where(['mobile' => User::findOne($data['init_parent_id'])->binding])
- // ->select('name')->asArray()->One();
- $data['init_parent_name'] = !empty($initUser['nickname']) ? $initUser['nickname'] : '总店';
- }
- // 获取会员等级
- $level = Level::findOne(['level' => $data['level'], 'store_id' => $this->store_id, 'is_delete' => 0]);
- $data['l_name'] = $data['level'] == '-1' ? '普通用户' : $level['name'];
- $data['user_level_expires'] = $data['user_level_expires'] > 0 ? date('Y年m月d日 H:i:s', $data['user_level_expires'] ) : '永久有效';
- return $data;
- }
- /**
- * 获取会员等级列表
- */
- public function getLevelList() {
- // 会员列表
- $level = Level::find()->select('level, name')->where([
- 'store_id' => $this->store_id,
- 'is_delete' => Level::NOT_DELETE,
- 'status' => Level::STATUS_TRUE
- ])->orderBy('level ASC')->asArray()->all();
- $level = array_merge([['level' => '-1', 'name' => '普通用户']], $level);
- return $level;
- }
- /**
- * 用户会员记录列表
- * @return array
- */
- public function getLevelOrderList() {
- if ($this->user_id) {
- $query = LevelOrder::find()->alias('ro')->where([
- 'ro.store_id' => $this->store_id,
- 'ro.is_delete' => LevelOrder::NOT_DELETE,
- 'ro.is_pay' => LevelOrder::IS_PAY,
- 'ro.user_id' => $this->user_id
- ])
- ->leftJoin(['u' => User::tableName()], 'u.id='.$this->user_id);
- $details = $this->getUserInfo();
- } else {
- return [
- 'code' => 1,
- 'msg' => '用户参数错误',
- ];
- }
- $currentQuery = Level::find()->where(['store_id' => $this->store_id, 'is_delete' => Level::NOT_DELETE])
- ->andWhere('level=ro.current_level')->select('name');
- $afterQuery = Level::find()->where(['store_id' => $this->store_id,'is_delete' => Level::NOT_DELETE])
- ->andWhere('level=ro.after_level')->select('name');
- $query->orderBy('ro.pay_time desc');
- $query->select(['ro.*', 'u.nickname', 'u.platform', 'after_name' => $afterQuery, 'current_name' => $currentQuery]);
- $pagination = pagination_make($query);
- // return $pagination;
- $list = $pagination['list'];
- foreach ($list as $k => $v) {
- $list[$k]['pay_time'] = date('Y-m-d H:i:s', $v['pay_time']);
- if (!$v['current_name']) {
- $list[$k]['current_name'] = '普通会员';
- }
- }
- $level = $this->getLevelList();
- $user = User::findOne($details['id']);
- $details['nickname'] = $user->nickname;
- $details['avatar_url'] = $user->avatar_url;
- 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($saas_user){
- $details['nickname'] = $saas_user->name;
- $details['avatar_url'] = $saas_user->avatar;
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'detail' => $details,
- 'level' => $level,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount']
- ],
- ];
- }
- }
|