DoubleCopyCommission.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace addons\DoubleCopy\common\models;
  3. use addons\DoubleCopy\common\enums\DoubleCopyEnum;
  4. use common\enums\StatusEnum;
  5. use common\models\common\Addons;
  6. use common\models\mall\MallAddons;
  7. use common\models\user\User;
  8. use Yii;
  9. use yii\db\Exception;
  10. /**
  11. * This is the model class for table "{{%addons_double_copy_commission}}".
  12. *
  13. * @property int $id
  14. * @property int $mall_id
  15. * @property int $user_id
  16. * @property int $buy_uid
  17. * @property int $is_frozen 是否冻结[0:否,1:是]
  18. * @property int $order_id 订单ID
  19. * @property string $order_no 订单编号
  20. * @property float $pay_money 支付金额,单位:元
  21. * @property float $money 金额,单位:元
  22. * @property string $goods_name 商品名称
  23. * @property string $pic_url 商品封面
  24. * @property float $price 商品价格
  25. * @property string $source_table 来源表
  26. * @property int $source_table_id 操作来源业务id
  27. * @property string $capital_type 资金类型
  28. * @property int $settlement_type 结算状态[-1:已失效,0:未结算,1:已结算,2:部分结算]
  29. * @property int $frozen_status 是否已转移到冻结基金钱包[0=否,1=是]
  30. * @property int $setting 生成数据时配置
  31. * @property int $status 状态[-1:删除;0:禁用;1启用]
  32. * @property int $created_at 添加时间戳
  33. * @property int $updated_at 更新时间戳
  34. * @property string $reward_currency 奖励类型:佣金-commission,积分-score
  35. * @property string $operation_type 操作类型(主要是为了标识扣减原因)
  36. * @property string $operation_id 配合operation_type使用
  37. */
  38. class DoubleCopyCommission extends \common\models\base\BaseActiveRecord
  39. {
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public static function tableName()
  44. {
  45. return '{{%addons_double_copy_commission}}';
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function rules()
  51. {
  52. return [
  53. [['mall_id', 'user_id', 'buy_uid', 'order_id', 'is_frozen', 'source_table_id', 'settlement_type', 'frozen_status', 'status', 'created_at', 'updated_at', 'operation_id'], 'integer'],
  54. [['pay_money', 'money', 'price'], 'number'],
  55. [['order_no'], 'string', 'max' => 60],
  56. [['source_table'], 'string', 'max' => 100],
  57. [['capital_type'], 'string', 'max' => 255],
  58. [['setting'], 'string'],
  59. [['reward_currency'], 'string'],
  60. [['goods_name'],'string'],
  61. [['operation_type'], 'string', 'max' => 50],
  62. ];
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function attributeLabels()
  68. {
  69. return [
  70. 'id' => 'ID',
  71. 'mall_id' => 'Mall ID',
  72. 'user_id' => 'User ID',
  73. 'buy_uid' => '下单用户id',
  74. 'is_frozen' => '是否冻结[0:否,1:是]',
  75. 'order_id' => '订单ID',
  76. 'order_no' => '订单编号',
  77. 'pay_money' => '支付金额,单位:元',
  78. 'money' => '金额,单位:元',
  79. 'goods_name' => '商品名称',
  80. 'pic_url' => '商品图片',
  81. 'price' => '商品金额',
  82. 'source_table' => '来源表',
  83. 'source_table_id' => '操作来源业务id',
  84. 'capital_type' => '资金类型',
  85. 'settlement_type' => '结算状态[-1:已失效,0:未结算,1:已结算,2:部分结算]',
  86. 'frozen_status' => '是否已转移到冻结基金钱包[0=否,1=是]',
  87. 'setting' => '生成数据时配置',
  88. 'status' => '状态[-1:删除;0:禁用;1启用]',
  89. 'created_at' => '添加时间戳',
  90. 'updated_at' => '更新时间戳',
  91. 'operation_type' => '操作类型',
  92. 'operation_id' => '操作ID,配合operation_type使用',
  93. ];
  94. }
  95. /**
  96. * 获取用户
  97. * @Author: lun
  98. * @DateTime: 2022/5/14 0014 15:31
  99. * @Copyright: copyright (c) 2021 广东七件事集团
  100. * @return \yii\db\ActiveQuery
  101. */
  102. public function getUser()
  103. {
  104. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,nickname,mobile,avatar_url,parent_id');
  105. }
  106. /**
  107. * 关联链动用户
  108. * @Author: lun
  109. * @DateTime: 2022/5/14 0014 17:47
  110. * @Copyright: copyright (c) 2021 广东七件事集团
  111. * @return \yii\db\ActiveQuery
  112. */
  113. public function getLduser()
  114. {
  115. return $this->hasOne(DoubleCopyUser::class, ['user_id' => 'user_id'])->andWhere(['<>', 'status', StatusEnum::DELETE])->select('user_id,level');
  116. }
  117. /**
  118. * 获取下单用户
  119. * @Author: lun
  120. * @DateTime: 2022/5/14 0014 15:31
  121. * @Copyright: copyright (c) 2021 广东七件事集团
  122. * @return \yii\db\ActiveQuery
  123. */
  124. public function getBuyuser()
  125. {
  126. return $this->hasOne(User::class, ['id' => 'buy_uid'])->select('id,nickname,mobile,avatar_url,parent_id');
  127. }
  128. public static function getAddons(array $params){
  129. $addons_name = $params['addons_name'];
  130. $is_install = MallAddons::isInstall($params['mall_id'], $addons_name);
  131. if($is_install){
  132. $detail = Addons::getOne([['name' =>$addons_name], ['is_online' => 1]], true);
  133. return [
  134. 'addons_name'=>$addons_name,
  135. 'title'=>$detail['title'],
  136. ];
  137. }
  138. return [];
  139. }
  140. /**
  141. * 获取未结算的冻结基金
  142. * @Author:lun
  143. * @Date:2023-12-04 10:04
  144. * @Copyright:copyright(c)2023 广东七件事集团
  145. * @param $user_id
  146. * @return bool|int|mixed|string
  147. */
  148. public static function getNoSettlementForzen($user_id, $reward_currency = 'commission')
  149. {
  150. return self::find()->where(['user_id' => $user_id, 'is_frozen' => StatusEnum::ENABLED, 'reward_currency' => $reward_currency, 'settlement_type' => [DoubleCopyEnum::NOT_SETTLEMENT,DoubleCopyEnum::PART_SETTLED], 'frozen_status' => StatusEnum::DISABLED, 'status' => StatusEnum::ENABLED])->sum('money') ?? 0;
  151. }
  152. public static function setData($params)
  153. {
  154. try {
  155. $model = new self();
  156. $model->loadDefaultValues();
  157. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  158. return $model->toArray();
  159. } catch (\Exception $e) {
  160. self::setStaticError($e->getMessage());
  161. return false;
  162. }
  163. }
  164. /**
  165. * 处理已结算的冻结基金,将状态改为已转入冻结基金钱包
  166. * @Author:lun
  167. * @Date:2024-03-16 11:37
  168. * @Copyright:copyright(c)2024 广东七件事集团
  169. * @return void
  170. */
  171. public static function handleFrozenStatus()
  172. {
  173. $part = 1;
  174. $limit = 1000;
  175. while ($frozen_order_ids = self::find()->where(['capital_type' => DoubleCopyEnum::SECOND_PRIZE, 'settlement_type' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED])->select('order_id')->orderBy('id asc')
  176. ->offset(($part - 1) * $limit)->limit($limit)->column()) {
  177. $frozen_order_ids = implode(',', $frozen_order_ids);
  178. echo $frozen_order_ids.'<br>';
  179. self::updateAll(['frozen_status' => StatusEnum::ENABLED], "order_id in({$frozen_order_ids}) and capital_type='".DoubleCopyEnum::FREEZE_FUND."'");
  180. $part ++;
  181. }
  182. }
  183. }