| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <?php
- namespace addons\HiGo\common\models;
- use addons\HiGo\common\enums\HiGoEnum;
- use common\enums\AddonsEnum;
- use common\enums\OrderStatusEnum;
- use common\models\common\CapitalLog;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\models\user\User;
- use common\enums\StatusEnum;
- use Yii;
- use Exception;
- /**
- * This is the model class for table "{{%addons_higo_wallet_log}}".
- *
- * @property int $id
- * @property int $mall_id
- * @property int $user_id
- * @property string $wallet_type 类型[hi_bei:嗨呗,hi_value:嗨值]
- * @property string $change_type 操作类型:add-增加,sub-减少
- * @property float $change_num 操作数量
- * @property float $after_num 变动后数量
- * @property string $desc 变动说明
- * @property string $source_table 来源表
- * @property int $source_table_id 操作来源业务id
- * @property string $from_type 来源类型
- * @property int $status 状态,-1:失效,1:正常,2:冻结
- * @property int $created_at 添加时间戳
- * @property int $updated_at 更新时间戳
- * @property string $addon_name 来源插件:默认HiGo
- */
- class HigoWalletLog extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_higo_wallet_log}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'source_table_id', 'status', 'created_at', 'updated_at'], 'integer'],
- [['change_num', 'after_num'], 'number'],
- [['wallet_type'], 'string', 'max' => 20],
- [['change_type'], 'string', 'max' => 12],
- [['addon_name'], 'string', 'max' => 64],
- [['desc', 'source_table', 'from_type'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'user_id' => 'User ID',
- 'wallet_type' => 'Wallet Type',
- 'change_type' => 'Change Type',
- 'change_num' => 'Change Num',
- 'after_num' => 'After Num',
- 'desc' => 'Desc',
- 'source_table' => 'Source Table',
- 'source_table_id' => 'Source Table ID',
- 'from_type' => 'From Type',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id']);
- }
- /**
- * 保存数据
- * @param array $params
- * @return boolean|self
- */
- public static function setData(array $params)
- {
- try {
- $model = new self();
- $model->loadDefaultValues();
- if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
- return $model;
- } catch (Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 检测奖励是否发放过
- * @param $mall_id
- * @param $user_id
- * @param $source_table
- * @param $source_id
- * @param $from_type
- * @return HigoWalletLog|null
- */
- public static function checkSend($mall_id, $user_id, $source_table, $source_id, $from_type, $wallet_type,$addons_name = HiGoEnum::ADDONS_NAME)
- {
- return self::findOne(['mall_id' => $mall_id,'user_id' => $user_id,'wallet_type' => $wallet_type,
- 'change_type' => CapitalLog::CHANGE_TYPE_ADD,'source_table' => $source_table,'source_table_id' => $source_id,
- 'from_type' => $from_type,'status' => StatusEnum::ENABLED,
- 'addon_name' => $addons_name
- ]);
- }
- //供公共调用
- public static function getOneCapitalLogList($params)
- {
- if (!isset($params['list']) || !is_array($params['list'])) return [];
- $order_id = $params['order_id'] ?? 0;
- $order_detail_id = $params['order_detail_id'] ?? null;
- $mall_id = $params['mall_id'] ?? 0;
- if ($order_id <= 0 && empty($order_detail_id)) return [];
- $wallet_type = $params['wallet_type'] ?? '';
- if (!in_array($wallet_type, [HiGoEnum::WALLET_TYPE_HI_VALUE, HiGoEnum::WALLET_TYPE_HI_BEI])) {
- return [];
- }
- $order_detail_id_arr = $order_detail_id;
- if (!$order_detail_id_arr) {
- //查询order_detail
- $params = [];
- $params['where'] = [
- ['order_id' => $order_id],
- ['mall_id' => $mall_id],
- ];
- $order_detail_list = OrderDetail::lists($params);
- $order_detail_id_arr = array_column($order_detail_list, 'id');
- }
- $cond = [];
- $cond['where'] = [
- ['wallet_type' => $wallet_type],
- // ['source_table_id' => $order_id],
- // ['source_table' => '{{%order}}'],
- //['!=', 'addon_name', HiGoEnum::ADDONS_NAME],
- ['mall_id' => $mall_id]
- ];
- if($order_detail_id_arr){
- $cond['where'][] = ['or',
- ['and',['source_table_id' => $order_id],['source_table' => Order::tableName()]],
- ['and',['source_table_id' => $order_detail_id_arr],['source_table' => 'order_detail']],
- ];
- }else{
- $cond['where'][] = ['source_table_id' => $order_id];
- $cond['where'][] = ['source_table' => Order::tableName()];
- }
- $cond['with'] = ['user' => function ($query) {
- $query->select('id,nickname,avatar_url');
- }];
- $list = self::lists($cond);
- $wallet_log_status = HiGoEnum::getWalletLogStatus();
- $from_type_map = HiGoEnum::getFromTypeMap($mall_id);
- $addons_name_map = AddonsEnum::getAddonsNameMap();
- foreach ($list as &$value) {
- $value['is_frozen'] = $wallet_log_status[$value['status']] ?? '';
- $value['capital_type_text'] = $from_type_map[$value['from_type']] ?? '';
- $value['from_type_text'] = $addons_name_map[$value['addon_name']] ?? '';
- $value['nickname'] = $value['user']['nickname'];
- $value['avatar_url'] = $value['user']['avatar_url'];
- unset($value['user']['avatar_url'], $value['user']['nickname']);
- }
- return $list;
- }
- }
|