| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use Yii;
- use yii\behaviors\TimestampBehavior;
- use app\modules\admin\models\wechat_mp\CardForm;
- use app\utils\IotCloudHelper;
- /**
- * This is the model class for table "{{%account_log}}".
- *
- * @property integer $id
- * @property integer $store_id
- * @property integer $order_type
- * @property integer $order_id
- * @property integer $user_id
- * @property integer $amount
- * @property string $desc
- * @property integer $before
- * @property integer $after
- * @property string $operator
- * @property integer $operator_id
- * @property integer $log_type
- * @property integer $type
- * @property integer $operator_type
- * @property string $pic_url
- * @property string $explain
- * @property integer $created_at
- * @property integer $from
- * @property integer $from_user_id
- * @property integer $profit
- * @property integer $saas_id
- */
- class AccountLog extends \yii\db\ActiveRecord
- {
- /**
- * 收入类型
- */
- CONST LOG_TYPE_INCOME = 1;
- /**
- * 支出类型
- */
- CONST LOG_TYPE_EXPEND = 2;
- public static $type_budget = [
- self::LOG_TYPE_INCOME,
- self::LOG_TYPE_EXPEND
- ];
- /**
- * 充值订单
- */
- CONST TYPE_RECHARGE_ORDER = 0;
- /**
- * 商城订单
- */
- CONST TYPE_PLATFORM_ORDER = 1;
- /**
- * 秒杀订单
- */
- CONST TYPE_MS_ORDER = 2;
- /**
- * 拼团订单
- */
- CONST TYPE_PT_ORDER = 3;
- /**
- * 商城订单退款
- */
- CONST TYPE_PLATFORM_REFUND_ORDER = 4;
- /**
- * 充值订单退款(减余额)
- */
- CONST TYPE_RECHARGE_REFUND_ORDER = 41;
- /**
- * 商城秒杀退款
- */
- CONST TYPE_MS_REFUND_ORDER = 5;
- /**
- * 商城拼团退款
- */
- CONST TYPE_PT_REFUND_ORDER = 6;
- /**
- * 商城预约退款
- */
- CONST TYPE_YY_REFUND_ORDER = 7;
- /**
- * 预约订单
- */
- CONST TYPE_YY_ORDER = 8;
- /**
- * 预约订单
- */
- CONST TYPE_SCRATCH_ORDER = 9;
- /**
- * 大转盘
- */
- CONST TYPE_POND_ORDER = 10;
- /**
- * 供货商订单
- */
- CONST TYPE_SUPPLIER_ORDER = 11;
- /**
- * 会员购买订单
- */
- CONST TYPE_LEVEL_ORDER = 12;
- /**
- * 当面付
- */
- CONST TYPE_SCAN_CODE_PAY = 13;
- /**
- * saas积分商品订单
- */
- CONST TYPE_SAAS_INTEGRAL_ORDER = 14;
- /**
- * saas积分商品订单
- */
- CONST TYPE_BALANCE_TO_COMMISSION = 15;
- /**
- * 签到送积分
- */
- CONST TYPE_REGISTER_INTEGRAL = 16;
- /**
- * 问答专区提问
- */
- CONST TYPE_QUESTION_INTEGRAL = 17;
- /**
- * 问答专区看视频广告
- */
- const TYPE_QUESTION_AD = 18;
- /**
- * 充值上级佣金
- */
- const TYPE_RECHARGE_COMMISSION = 20;
- /**
- * 充值分销佣金
- */
- const TYPE_DISTRIBUTION_COMMISSION = 21;
- /**
- * p大转盘抽奖
- */
- const TYPE_POND_COMMISSION = 23;
- /**
- * 采购金
- */
- const TYPE_CLOUD_INVENTORY_PURCHASE_MONEY = 25;
- const TYPE_PURCHASE_CLOUD_INVENTORY_PURCHASE_MONEY = 26;
- /**
- * 贡献积分
- */
- const TYPE_COIN = 3;
- /**
- * 余额
- */
- const TYPE_BALANCE = 2;
- /**
- * 积分
- */
- const TYPE_INTEGRAL = 1;
- /**
- * 商品余额抵扣
- */
- const TYPE_GOODS_BALANCE = 19;
- /**
- * 微信
- */
- CONST TYPE_WECHAT = 3;
- /**
- * 支付宝
- */
- CONST TYPE_ALIPAY = 4;
- /**
- * 采购金
- */
- CONST TYPE_PURCHASE_MONEY = 5;
- public static $valid_type = [
- self::TYPE_BALANCE,
- self::TYPE_INTEGRAL,
- self::TYPE_WECHAT,
- self::TYPE_ALIPAY,
- self::TYPE_PURCHASE_MONEY
- ];
- /**
- * 后台改动
- */
- CONST TYPE_OPERATOR_BACK = 2;
- /**
- * 前台变化
- */
- CONST TYPE_OPERATOR_NORMAL= 1;
- /**
- * AI变化
- */
- CONST TYPE_OPERATOR_AI= 3;
- /**
- * @var int[]
- */
- CONST FROM_TYPE_ORIGINAL = 0; //原始
- CONST FROM_TYPE_GIFT = 1; //赠送
- CONST FROM_TYPE_GIFTED_GIFT = 1; //被赠送
- CONST FROM_TYPE_COMMISSION_TO_BALANCE = 2; //佣金转余额
- CONST FROM_TYPE_RECHARGE = 3; //充值
- CONST FROM_TYPE_TRANSFER_PURCHASE_MONEY = 4; //云库存结算余额转采购金
- public static $type_order = [
- self::TYPE_RECHARGE_ORDER,
- self::TYPE_PLATFORM_ORDER,
- self::TYPE_MS_ORDER,
- self::TYPE_PT_ORDER,
- self::TYPE_PLATFORM_REFUND_ORDER,
- self::TYPE_MS_REFUND_ORDER,
- self::TYPE_PT_REFUND_ORDER
- ];
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%account_log}}';
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['user_id', 'type', 'amount', 'desc', 'order_type', 'before', 'after',
- 'operator_type', 'log_type', 'type'], 'required'],
- [['user_id', 'type', 'created_at', 'order_type', 'order_id', 'operator_type', 'log_type', 'operator_id', 'from', 'from_user_id', 'saas_id'
- ], 'integer'],
- [['amount', 'before', 'after', 'profit'], 'number'],
- [['desc', 'pic_url', 'explain', 'operator'], 'string'],
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'user_id' => 'User ID',
- 'log_type' => '类型:1=收入,2=支出',
- 'type' => '类型:2=余额,1=积分',
- 'amount' => '变动数',
- 'desc' => '变动说明',
- 'before' => '变动前',
- 'after' => '变动后',
- 'operator' => '操作者',
- 'operator_id' => '操作者id',
- 'operator_type' => '1:前台,2:后台',
- 'pic_url' => '图片',
- 'explain' => '说明',
- 'created_at' => '添加时间',
- 'order_type' => '订单类型 0--充值 1--商城订单 2--秒杀订单 3--拼团订单 4--商城订单退款 5--秒杀订单退款 6--拼团订单退款 7--后台改动,15、团购券, 23、大转盘抽奖',
- 'order_id' => '订单ID',
- 'from' => '是否为转增',
- 'saas_id' => '联盟用户'
- ];
- }
- /**
- * 仅适用客户端使用
- * @param $user_id
- * @param $amount
- * @param $type
- * @param $log_type
- * @param int $order_type
- * @param int $order_id
- * @param string $desc
- * @return bool
- */
- public static function saveLog($user_id, $amount, $type, $log_type, $order_type = 0, $order_id = 0, $desc = "", $from = 0, $from_user_id = 0, $profit = 0)
- {
- $form = new self();
- $user_info = User::findOne($user_id);
- $form->store_id = $user_info->store_id;
- $form->user_id = $user_id;
- $form->amount = $amount;
- $form->type = $type;
- $form->before = $type == AccountLog::TYPE_INTEGRAL ? $user_info->integral : $user_info->money;
- if ($log_type == AccountLog::LOG_TYPE_INCOME) {
- if ($type == AccountLog::TYPE_INTEGRAL) {
- $form->after = $user_info->integral + $amount;
- } else {
- $form->after = $user_info->money + $amount;
- }
- } else {
- if ($type == AccountLog::TYPE_INTEGRAL) {
- $form->after = $user_info->integral - $amount;
- } else {
- $form->after = $user_info->money - $amount;
- }
- }
- $form->desc = $desc;
- $form->order_type = $order_type;
- $form->order_id = $order_id;
- $form->operator = '';
- $form->operator_id = 0;
- $form->log_type = $log_type;
- $form->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
- $form->created_at = time();
- if ($from) {
- $form->from = $from;
- }
- if ($from_user_id) {
- $form->from_user_id = $from_user_id;
- }
- if ($profit) {
- $form->profit = $profit;
- }
- if ($form->save()) {
- if ($type == AccountLog::TYPE_INTEGRAL) {
- if ($log_type == AccountLog::LOG_TYPE_INCOME) {
- $user_info->integral += $amount;
- } else {
- $user_info->integral -= $amount;
- }
- } else {
- if ($log_type == AccountLog::LOG_TYPE_INCOME) {
- $user_info->money += $amount;
- } else {
- $user_info->money -= $amount;
- }
- }
- $save = $user_info->save();
- if(!$save){
- \Yii::error([__METHOD__, $user_info->getErrors()]);
- }
- return $save;
- }
- }
- public function afterSave($insert, $changedAttributes)
- {
- parent::afterSave($insert, $changedAttributes);
- if ($insert) {
- self::wxCardSync($this, $changedAttributes);
- self::voiceSendMessage($this);
- }
- }
- public static function wxCardSync($form, $ca = [])
- {
- \Yii::error([__METHOD__, $form, $ca]);
- if ($form->type != AccountLog::TYPE_INTEGRAL) {
- return true;
- }
- $cf = new CardForm();
- $bind = $cf->getBindByUserId($form->user_id, $form->store_id);
- if (!$bind) {
- return true;
- }
- $amount = $form->amount;
- if ($form->log_type != AccountLog::LOG_TYPE_INCOME) {
- $amount = -$amount;
- }
- $cf->updateUser($bind, [
- "record_bonus" => $form->desc,
- "bonus" => $form->after,
- "add_bonus" => $amount,
- ]);
- return true;
- }
- // 余额支付语音播报
- public static function voiceSendMessage($form)
- {
- if (
- $form->log_type == self::LOG_TYPE_EXPEND &&
- $form->type == self::TYPE_BALANCE &&
- $form->operator_type == self::TYPE_OPERATOR_NORMAL &&
- $form->order_id > 0
- ) {
- $store = Store::findOne($form->store_id);
- if ($store && !empty($store->device_name)) {
- $total_fee = $form->amount;
- IotCloudHelper::sendMessage($form->store_id, '{"cmd":"voice","msg":"会员卡消费收款'.$total_fee.'元","msgid":"'.$form->created_at . $form->order_id.'"}');
- }
- }
- }
- }
|