| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1\admin;
- use app\models\AccountLog;
- use app\models\ReOrder;
- use app\models\User;
- use yii\base\Model;
- use Yii;
- class AccountLogForm extends Model
- {
- public $admin;
- public $store_id;
- public $money;
- public $integral;
- 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;
- /**
- * @return array the validation rules.
- */
- public function rules()
- {
- return [
- [['explain', 'name'], 'trim'],
- [['explain', 'name', 'dateStart', 'dateEnd'], 'string'],
- [['type', 'recharge_type', 'user_id'], 'integer'],
- [['money', 'integral'], 'number'],
- ];
- }
- /**
- * 充值积分或者余额
- * @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;
- 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'],
- ],
- ];
- }
- }
|