HigoWalletLog.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace addons\HiGo\common\models;
  3. use addons\HiGo\common\enums\HiGoEnum;
  4. use common\enums\AddonsEnum;
  5. use common\enums\OrderStatusEnum;
  6. use common\models\common\CapitalLog;
  7. use common\models\order\Order;
  8. use common\models\order\OrderDetail;
  9. use common\models\user\User;
  10. use common\enums\StatusEnum;
  11. use Yii;
  12. use Exception;
  13. /**
  14. * This is the model class for table "{{%addons_higo_wallet_log}}".
  15. *
  16. * @property int $id
  17. * @property int $mall_id
  18. * @property int $user_id
  19. * @property string $wallet_type 类型[hi_bei:嗨呗,hi_value:嗨值]
  20. * @property string $change_type 操作类型:add-增加,sub-减少
  21. * @property float $change_num 操作数量
  22. * @property float $after_num 变动后数量
  23. * @property string $desc 变动说明
  24. * @property string $source_table 来源表
  25. * @property int $source_table_id 操作来源业务id
  26. * @property string $from_type 来源类型
  27. * @property int $status 状态,-1:失效,1:正常,2:冻结
  28. * @property int $created_at 添加时间戳
  29. * @property int $updated_at 更新时间戳
  30. * @property string $addon_name 来源插件:默认HiGo
  31. */
  32. class HigoWalletLog extends \common\models\base\BaseActiveRecord
  33. {
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public static function tableName()
  38. {
  39. return '{{%addons_higo_wallet_log}}';
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function rules()
  45. {
  46. return [
  47. [['mall_id', 'user_id', 'source_table_id', 'status', 'created_at', 'updated_at'], 'integer'],
  48. [['change_num', 'after_num'], 'number'],
  49. [['wallet_type'], 'string', 'max' => 20],
  50. [['change_type'], 'string', 'max' => 12],
  51. [['addon_name'], 'string', 'max' => 64],
  52. [['desc', 'source_table', 'from_type'], 'string', 'max' => 255],
  53. ];
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function attributeLabels()
  59. {
  60. return [
  61. 'id' => 'ID',
  62. 'mall_id' => 'Mall ID',
  63. 'user_id' => 'User ID',
  64. 'wallet_type' => 'Wallet Type',
  65. 'change_type' => 'Change Type',
  66. 'change_num' => 'Change Num',
  67. 'after_num' => 'After Num',
  68. 'desc' => 'Desc',
  69. 'source_table' => 'Source Table',
  70. 'source_table_id' => 'Source Table ID',
  71. 'from_type' => 'From Type',
  72. 'status' => 'Status',
  73. 'created_at' => 'Created At',
  74. 'updated_at' => 'Updated At',
  75. ];
  76. }
  77. public function getUser()
  78. {
  79. return $this->hasOne(User::class, ['id' => 'user_id']);
  80. }
  81. /**
  82. * 保存数据
  83. * @param array $params
  84. * @return boolean|self
  85. */
  86. public static function setData(array $params)
  87. {
  88. try {
  89. $model = new self();
  90. $model->loadDefaultValues();
  91. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  92. return $model;
  93. } catch (Exception $e) {
  94. self::$static_error = $e->getMessage();
  95. return false;
  96. }
  97. }
  98. /**
  99. * 检测奖励是否发放过
  100. * @param $mall_id
  101. * @param $user_id
  102. * @param $source_table
  103. * @param $source_id
  104. * @param $from_type
  105. * @return HigoWalletLog|null
  106. */
  107. public static function checkSend($mall_id, $user_id, $source_table, $source_id, $from_type, $wallet_type,$addons_name = HiGoEnum::ADDONS_NAME)
  108. {
  109. return self::findOne(['mall_id' => $mall_id,'user_id' => $user_id,'wallet_type' => $wallet_type,
  110. 'change_type' => CapitalLog::CHANGE_TYPE_ADD,'source_table' => $source_table,'source_table_id' => $source_id,
  111. 'from_type' => $from_type,'status' => StatusEnum::ENABLED,
  112. 'addon_name' => $addons_name
  113. ]);
  114. }
  115. //供公共调用
  116. public static function getOneCapitalLogList($params)
  117. {
  118. if (!isset($params['list']) || !is_array($params['list'])) return [];
  119. $order_id = $params['order_id'] ?? 0;
  120. $order_detail_id = $params['order_detail_id'] ?? null;
  121. $mall_id = $params['mall_id'] ?? 0;
  122. if ($order_id <= 0 && empty($order_detail_id)) return [];
  123. $wallet_type = $params['wallet_type'] ?? '';
  124. if (!in_array($wallet_type, [HiGoEnum::WALLET_TYPE_HI_VALUE, HiGoEnum::WALLET_TYPE_HI_BEI])) {
  125. return [];
  126. }
  127. $order_detail_id_arr = $order_detail_id;
  128. if (!$order_detail_id_arr) {
  129. //查询order_detail
  130. $params = [];
  131. $params['where'] = [
  132. ['order_id' => $order_id],
  133. ['mall_id' => $mall_id],
  134. ];
  135. $order_detail_list = OrderDetail::lists($params);
  136. $order_detail_id_arr = array_column($order_detail_list, 'id');
  137. }
  138. $cond = [];
  139. $cond['where'] = [
  140. ['wallet_type' => $wallet_type],
  141. // ['source_table_id' => $order_id],
  142. // ['source_table' => '{{%order}}'],
  143. //['!=', 'addon_name', HiGoEnum::ADDONS_NAME],
  144. ['mall_id' => $mall_id]
  145. ];
  146. if($order_detail_id_arr){
  147. $cond['where'][] = ['or',
  148. ['and',['source_table_id' => $order_id],['source_table' => Order::tableName()]],
  149. ['and',['source_table_id' => $order_detail_id_arr],['source_table' => 'order_detail']],
  150. ];
  151. }else{
  152. $cond['where'][] = ['source_table_id' => $order_id];
  153. $cond['where'][] = ['source_table' => Order::tableName()];
  154. }
  155. $cond['with'] = ['user' => function ($query) {
  156. $query->select('id,nickname,avatar_url');
  157. }];
  158. $list = self::lists($cond);
  159. $wallet_log_status = HiGoEnum::getWalletLogStatus();
  160. $from_type_map = HiGoEnum::getFromTypeMap($mall_id);
  161. $addons_name_map = AddonsEnum::getAddonsNameMap();
  162. foreach ($list as &$value) {
  163. $value['is_frozen'] = $wallet_log_status[$value['status']] ?? '';
  164. $value['capital_type_text'] = $from_type_map[$value['from_type']] ?? '';
  165. $value['from_type_text'] = $addons_name_map[$value['addon_name']] ?? '';
  166. $value['nickname'] = $value['user']['nickname'];
  167. $value['avatar_url'] = $value['user']['avatar_url'];
  168. unset($value['user']['avatar_url'], $value['user']['nickname']);
  169. }
  170. return $list;
  171. }
  172. }