| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- <?php
- namespace addons\BlindBox\common\models;
- use addons\BlindBox\common\enums\BlindBoxEnum;
- use common\enums\AddonsEnum;
- use common\enums\OrderStatusEnum;
- use common\enums\StatusEnum;
- use common\enums\UserWalletEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\order\Order;
- use common\models\user\User;
- use services\common\UserWalletService;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_blind_box_bonus_log_detail".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $bonus_log_id 分红记录id
- * @property int $user_id 会员id
- * @property string $order_no 订单编号
- * @property float $money 订单贡献金额
- * @property float $total_contribution 总贡献值
- * @property float $contribution 订单贡献值
- * @property float $total_bonus 分红总额
- * @property float $order_bonus 订单分红
- * @property int $is_settlement 是否结算:0:否:1:是
- * @property int $is_feedback 是否退款:0:否:1:是
- * @property int $settlement_time 结算时间
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class BlindBoxBonusLogDetail extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_blind_box_bonus_log_detail}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'bonus_log_id', 'user_id', 'is_settlement', 'is_feedback', 'settlement_time', 'status', 'created_at', 'updated_at'], 'integer'],
- [['money', 'total_contribution', 'contribution', 'total_bonus', 'order_bonus'], 'number'],
- [['order_no'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'bonus_log_id' => '分红记录id',
- 'user_id' => '会员id',
- 'order_no' => '订单编号',
- 'money' => '订单贡献金额',
- 'total_contribution' => '总贡献值',
- 'contribution' => '订单贡献值',
- 'total_bonus' => '分红总额',
- 'order_bonus' => '订单分红',
- 'is_settlement' => '是否结算:0:否:1:是',
- 'is_feedback' => '是否退款:0:否:1:是',
- 'settlement_time' => '结算时间',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public static function addData(array $params)
- {
- try {
- $model = self::findOne([
- 'mall_id' => $params['mall_id'],
- 'user_id' => $params['user_id'],
- 'bonus_log_id' => $params['bonus_log_id'],
- 'order_no' => $params['order_no'],
- 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (!empty($model)) throw new \Exception('重复执行');
- $model = new self();
- $model->loadDefaultValues();
- if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
- $res = $model->save();
- if ($res == false) throw new \Exception($model->getErrorMessage());
- return $model;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function settlementOld($mall_id)
- {
- $t = Yii::$app->db->beginTransaction();
- try {
- $basic = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_BLIND_BOX, 'basic');
- if (empty($basic['bonus_time'])) throw new \Exception('结算分红时间没有设置');
- if (empty($basic['is_enable'])) throw new \Exception('没有开启奖金盲盒');
- $time = time();
- $date = date('Y-m-d', $time);
- $bonus_time = $date . ' ' . $basic['bonus_time'] . ':00';
- $bonus_time = strtotime($bonus_time);
- if ($bonus_time > $time) throw new \Exception(date('结算周期:' . 'Y-m-d H:i:s', $bonus_time) . ';此刻时间是:' . date('Y-m-d H:i:s', $time));
- $res = BlindBoxBonusLogDetail::findOne(['mall_id' => $mall_id, 'settlement_time' => $bonus_time]);
- if ($res) throw new \Exception('结算周期:' . date('Y-m-d H:i:s', $bonus_time) . '已经结算过');
- $where = [
- ['mall_id' => $mall_id],
- ['is_settlement' => BlindBoxEnum::UNSETTLED],
- ['<=', 'created_at', $bonus_time]
- ];
- $params = [
- 'where' => $where,
- 'select' => 'user_id,sum(order_bonus) as order_bonus',
- 'group' => 'user_id'
- ];
- $list = BlindBoxBonusLogDetail::lists($params);
- if (empty($list)) throw new \Exception('无符合条件的待结算分红记录');
- $insert_wallet = [];
- foreach ($list as $item) {
- $insert_wallet [] = [
- 'mall_id' => $mall_id, 'user_id' => $item['user_id'],
- 'is_frozen' => false,
- 'wallet_type' => 'commission',
- 'money' => $item['order_bonus'],
- 'desc' => '奖金盲盒-分红结算',
- 'source_table' => self::tableName(),
- 'source_table_id' => 0,
- 'from_type' => AddonsEnum::ADDONS_NAME_BLIND_BOX,
- 'capital_type' => UserWalletEnum::BLIND_BOX_BONUS,
- 'order_no' => '',
- 'contributor_name' => '',
- 'contributor_id' => 0,
- 'contributor_money' => 0,
- ];
- }
- //投递到资产队列
- if ($insert_wallet) {
- $res = UserWalletService::batchHandleByQueue($insert_wallet);
- if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
- foreach ($insert_wallet as $k => $user) {
- $model = BlindBoxUser::findOne(['user_id' => $user['user_id'], 'mall_id' => $mall_id]);
- if (empty($model)) continue;
- $model->total_commission += $user['money'];
- $model->frozen_commission -= $user['money'];
- if ($model->frozen_commission < 0) $model->frozen_commission = 0;
- $res = $model->save();
- if (empty($res)) throw new \Exception($model->getErrorMessage());
- }
- }
- $attributes = ['is_settlement' => BlindBoxEnum::SETTLED, 'settlement_time' => $bonus_time];
- array_unshift($where, 'and');
- $res = BlindBoxBonusLogDetail::updateAll($attributes, $where);
- if (empty($res)) throw new \Exception('更新分红详情表错误');
- $t->commit();
- //插入通知
- self::insertNotice($mall_id, $bonus_time);
- return true;
- } catch (\Exception $e) {
- $t->rollBack();
- echo $mall_id . ':BlindBoxBonusLogDetail:settlement:' . $e->getMessage();
- return false;
- }
- }
- public static function settlement($mall_id, $settle_method_self_order, $bonus_log_id)
- {
- try {
- $time = time();
- $where_detail = [
- ['mall_id' => $mall_id],
- ['is_settlement' => BlindBoxEnum::UNSETTLED],
- ['is_feedback' => BlindBoxEnum::NO_FEEDBACK],
- ['<>', 'bonus_log_id', $bonus_log_id],
- ];
- $params = [
- 'where' => $where_detail,
- 'select' => 'id,user_id, order_bonus,bonus_log_id',
- ];
- $bonus_log_detail_list = BlindBoxBonusLogDetail::lists($params);
- if (empty($bonus_log_detail_list)) return true;
- $insert_wallet = [];
- $ids = [];
- if ($settle_method_self_order == 1) {
- //订单完成
- $bonus_log_ids = array_column($bonus_log_detail_list, 'bonus_log_id');
- $where = [
- ['mall_id' => $mall_id],
- ['id' => $bonus_log_ids],
- ['is_feedback' => BlindBoxEnum::NO_FEEDBACK],
- ];
- $params = [
- 'where' => $where,
- 'select' => 'id,order_no,type',
- ];
- $bonus_log_list = BlindBoxBonusLog::lists($params);
- $order_nos = [];
- foreach ($bonus_log_list as $value) {
- if ($value['type'] == BlindBoxEnum::ORDER_CONSUME) {
- $order_nos[] = $value['order_no'];
- }
- }
- $order_list = Order::lists(['where' => [
- ['order_no' => $order_nos],
- ['order_status' => OrderStatusEnum::ACCOMPLISH]
- ], 'select' => 'order_no']);
- $order_nos = [];
- if (!empty($order_list)) {
- $order_nos = array_column($order_list, 'order_no');
- }
- $bonus_log_ids = [];
- foreach ($bonus_log_list as $val) {
- switch ($val['type']) {
- case BlindBoxEnum::ORDER_CONSUME:
- if (in_array($val['order_no'], $order_nos)) {
- $bonus_log_ids[] = $val['id'];
- }
- break;
- case BlindBoxEnum::DIRECT_CONSUME:
- case BlindBoxEnum::NEW_PEOPLE:
- $bonus_log_ids[] = $val['id'];
- break;
- }
- }
- foreach ($bonus_log_detail_list as $item) {
- if (in_array($item['bonus_log_id'], $bonus_log_ids)) {
- $ids[] = $item['id'];
- if ($item['order_bonus'] > 0) {
- $insert_wallet [] = self::getInsertWalletArr($mall_id, $item['user_id'], $item['order_bonus'], $item['id']);
- }
- }
- }
- } else {
- foreach ($bonus_log_detail_list as $item) {
- $ids[] = $item['id'];
- if ($item['order_bonus'] > 0) {
- $insert_wallet [] = self::getInsertWalletArr($mall_id, $item['user_id'], $item['order_bonus'], $item['id']);
- }
- }
- }
- $where_detail[] = ['id' => $ids];
- //投递到资产队列
- if ($insert_wallet) {
- $res = UserWalletService::batchHandleByQueue($insert_wallet);
- if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
- foreach ($insert_wallet as $k => $user) {
- $model = BlindBoxUser::findOne(['user_id' => $user['user_id'], 'mall_id' => $mall_id]);
- if (empty($model)) continue;
- $model->total_commission += $user['money'];
- $model->frozen_commission -= $user['money'];
- if ($model->frozen_commission < 0) $model->frozen_commission = 0;
- $res = $model->save();
- if (empty($res)) throw new \Exception($model->getErrorMessage());
- }
- }
- $attributes = ['is_settlement' => BlindBoxEnum::SETTLED, 'settlement_time' => $time];
- array_unshift($where_detail, 'and');
- $res = BlindBoxBonusLogDetail::updateAll($attributes, $where_detail);
- if (empty($res)) throw new \Exception('更新分红详情表错误');
- //插入通知
- self::insertNotice($mall_id, $ids);
- return true;
- } catch (\Exception $e) {
- echo $mall_id . ':BlindBoxBonusLogDetail:settlement:' . $e->getMessage() . $e->getFile() . $e->getLine();
- return false;
- }
- }
- protected static function getInsertWalletArr($mall_id, $user_id, $money, $source_table_id)
- {
- return [
- 'mall_id' => $mall_id, 'user_id' => $user_id,
- 'is_frozen' => false,
- 'wallet_type' => 'commission',
- 'money' => $money,
- 'desc' => '奖金盲盒-分红结算',
- 'source_table' => self::tableName(),
- 'source_table_id' => $source_table_id,
- 'from_type' => AddonsEnum::ADDONS_NAME_BLIND_BOX,
- 'capital_type' => UserWalletEnum::BLIND_BOX_BONUS,
- 'order_no' => '',
- 'contributor_name' => '',
- 'contributor_id' => 0,
- 'contributor_money' => 0,
- ];
- }
- public static function insertNotice($mall_id, $ids)
- {
- $time = time();
- $params = [
- 'where' => [
- ['mall_id' => $mall_id],
- ['id' => $ids],
- ['is_settlement' => BlindBoxEnum::SETTLED],
- ],
- 'select' => 'user_id, order_bonus,bonus_log_id,created_at',
- ];
- $list = BlindBoxBonusLogDetail::lists($params);
- $insert_notice = [];
- $user_ids = array_column($list, 'user_id');
- $bonus_log_ids = array_column($list, 'bonus_log_id');
- $user_list = User::lists(['where' => [['id' => $user_ids]], 'index' => 'id', 'select' => 'id,nickname']);
- $bonus_log_list = BlindBoxBonusLog::lists(['where' => [['id' => $bonus_log_ids]], 'index' => 'id', 'select' => 'id,order_no']);
- $recommend_user_log_list = BlindBoxRecommendUserLog::lists(['where' => [['bonus_log_id' => $bonus_log_ids]]]);
- $recommend_user_log_arr = [];
- if (!empty($recommend_user_log_list)) {
- foreach ($recommend_user_log_list as $val) {
- $recommend_user_log_arr[$val['bonus_log_id']][] = $val['user_id'];
- }
- }
- foreach ($list as $item) {
- $content = '尊敬的';
- if (!empty($user_list[$item['user_id']]['nickname'])) {
- $content .= $user_list[$item['user_id']]['nickname'];
- } else {
- $content .= $item['user_id'];
- }
- $order_no = $bonus_log_list[$item['bonus_log_id']]['order_no'] ?? '';
- $user_id_str = '';
- if (!empty($order_no)) {
- $type = $order_no[0];
- if ($type == 't' && isset($recommend_user_log_arr[$item['bonus_log_id']])) {
- $user_id_str = join(',', $recommend_user_log_arr[$item['bonus_log_id']]);
- }
- }
- if (isset($bonus_log_list[$item['bonus_log_id']])) $content .= ',订单:' . $order_no;
- $date = date('m-d', $item['created_at']);
- $content .= ',' . $date . '产生分红' . $item['order_bonus'] . '元。';
- if ($user_id_str) {
- $content .= '直推会员id:' . $user_id_str;
- }
- $insert_notice[] = [
- 'mall_id' => $mall_id,
- 'user_id' => $item['user_id'],
- 'content' => $content,
- 'status' => StatusEnum::ENABLED,
- 'created_at' => $time,
- 'updated_at' => $time,
- ];
- }
- if (!empty($insert_notice)) {
- BlindBoxNotice::setData($insert_notice);
- }
- }
- }
|