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; 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->nickname . " 后台操作账号:" . $user->nickname . " 积分扣除:" . $this->integral . " 积分"; } elseif ($this->recharge_type == AccountLog::LOG_TYPE_INCOME) { $accountLog->desc = "管理员: " . $this->admin->nickname . " 后台操作账号:" . $user->nickname . " 积分充值:" . $this->integral . " 积分"; } $accountLog->amount = $this->integral; $accountLog->created_at = time(); $accountLog->operator = $this->admin->nickname ? $this->admin->nickname : '未知'; $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->nickname . " 后台操作账号:" . $user->nickname . " 余额充值:" . $this->money . " 元"; $accountLog->log_type = AccountLog::LOG_TYPE_INCOME; break; case AccountLog::LOG_TYPE_EXPEND: if ($user->money < $this->money) { return [ 'code' => 1, 'msg' => '扣除数值大于当前用户余额' ]; } $user->money -= $this->money; $accountLog->desc = "管理员: " . $this->admin->nickname . " 后台操作账号:" . $user->nickname . " 余额扣除:" . $this->money . " 元"; $accountLog->log_type = AccountLog::LOG_TYPE_EXPEND; break; default: return [ 'code' => 1, 'msg' => '网络异常,请刷新重试' ]; } if ($user->save()) { $accountLog->amount = $this->money; $accountLog->operator = $this->admin->nickname; $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], ]; } } } return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } /** * 充值记录展示 */ public function getRechargeList() { if ($this->type == AccountLog::TYPE_INTEGRAL) { return $this->getIntegralRechargeList(); } $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.created_at 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']; } } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $list, 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], ], ]; } /** * 充值记录展示 */ public function getIntegralRechargeList() { $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']; return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $list, 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], ], ]; } }