| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880 |
- <?php
- /**
- * 积分拓客
- */
- namespace addons\ScoreExpansion\common\logic;
- use addons\Redpack\common\enums\RedpackEnum;
- use addons\Redpack\common\models\Redpack;
- use addons\Redpack\common\models\RedpackGoodsSetting;
- use addons\Redpack\common\models\RedpackUserCountDay;
- use addons\Redpack\common\models\RedpackUserGrab;
- use addons\ScoreExpansion\common\enums\ScoreExpansionEnum;
- use addons\ScoreExpansion\common\models\ScoreExpansion;
- use addons\ScoreExpansion\common\models\ScoreExpansionGoodsSetting;
- use addons\ScoreExpansion\common\models\ScoreExpansionRateTask;
- use addons\ScoreExpansion\common\models\ScoreExpansionUserCountDay;
- use addons\ScoreExpansion\common\models\ScoreExpansionUserGrab;
- use addons\ScoreExpansion\common\models\ScoreExpansionWallet;
- use addons\ScoreExpansion\common\models\ScoreExpansionWalletLog;
- use addons\ScoreExpansion\common\services\SettingService;
- use addons\Store\common\models\Store;
- use addons\Store\common\models\StoreCashierOrder;
- use addons\Store\common\models\StoreOrder;
- use addons\Store\common\models\StoreOrderDetail;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\models\order\OrderMarketing;
- use common\models\user\User;
- use common\traits\ErrorTrait;
- use yii\db\Exception;
- use Yii;
- use yii\helpers\Json;
- class ScoreExpansionLogic
- {
- use ErrorTrait;
- /**
- * 判断活动限制
- * @Author: lun
- * @DateTime: 2022/3/21 0021 17:24
- * @Copyright: copyright (c) 2021 广东七件事集团
- */
- public static function checkUserLimit($redpack, $user_level)
- {
- if (empty($redpack['limit_user_level'])) throw new \Exception('您暂无参与活动权限');
- $limit_user_level = Json::decode($redpack['limit_user_level'], true);
- if (!in_array($user_level, $limit_user_level)) throw new \Exception('您暂无参与活动权限');
- }
- /**
- * 红包金额变动
- * @Author: lun
- * @DateTime: 2022/3/22 0022 9:56
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param int $user_id
- */
- public static function gradRedpack($user_id, ScoreExpansion $redpack)
- {
- $trans = Yii::$app->db->beginTransaction();
- try {
- $time_type = $redpack['time_type'];// 活动类型[1=每日红包,2=时间段红包]
- $date = date('ymd', time());
- // 活动-每日抢红包,每天都有一次无需邀请人抢红包机会
- // 活动-时间段抢红包,在这个时间段内,最多只有一次无需邀请人可抢红包机会
- // 获取用户是否首次抢红包
- if ($time_type == 1) {
- $is_first_grad = ScoreExpansionUserCountDay::find()->where(['rid' => $redpack->id, 'user_id' => $user_id, 'date' => $date, 'status' => StatusEnum::ENABLED])->asArray()->one();
- } else {
- $is_first_grad = ScoreExpansionUserCountDay::find()->where(['rid' => $redpack->id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED])->asArray()->one();
- }
- $award = $redpack->award;// 活动奖金池剩余金额
- // 今日抢红包统计
- $today_grad = ScoreExpansionUserCountDay::find()->where(['rid' => $redpack->id, 'user_id' => $user_id, 'date' => $date, 'status' => StatusEnum::ENABLED])->asArray()->one();
- // 解析红包规则内容
- $rule_values = Json::decode($redpack->rule_values, true);
- if (empty($is_first_grad)) {
- // 首次抢红包金额配置
- $money = rand($rule_values['first_integral_start'] * 100, $rule_values['first_integral_end'] * 100);
- } else {
- // 非首次抢到红包,检查最后一次抢红包次数是否是第一次抢,不是则找到今日最后一次抢的统计内容
- // 判断今日抢红包次数是否已经超出
- if ($today_grad['grab_num_total'] >= $rule_values['times']) throw new \Exception('今日抢积分次数已用完');
- // 判断可抢次数是否还足够
- $user_grad_num = empty($today_grad['inviter_num']) ? 0 : floor($today_grad['inviter_num'] / $rule_values['everyday_invitation']);// 用户已抢红包次数
- if ($user_grad_num - $today_grad['grab_num_total'] + 1 <= 0) throw new \Exception('今日抢积分次数不足');
- $money = rand($rule_values['everyday_invitation_start'] * 100, $rule_values['everyday_invitation_end'] * 100);
- }
- // 将奖励金额单位改为元
- $money = $money / 100;
- // 扣减奖金池
- if ($redpack->integral_type != ScoreExpansionEnum::FREEZE_TO_ACTIVE) {
- // 判断可得金额是否大于奖金池剩余金额
- if ($money >= $award) $money = $award;
- $redpack->award = $redpack->award - $money;
- }
- // 判断用户曾经是否抢过红包
- $has_grab = ScoreExpansionUserGrab::find()->where(['mall_id' => $redpack->mall_id, 'rid' => $redpack->id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED])->asArray()->one();
- empty($has_grab) && $redpack->grab_user_num = $redpack->grab_user_num + 1;
- // 红包表数据统计汇总
- $redpack->grab_integral = $redpack->grab_integral + $money;
- if (!$redpack->save()) throw new Exception($redpack->getErrorMessage());
- // 增加用户红包金额
- switch ($redpack->integral_type) {
- case ScoreExpansionEnum::FREEZE_TO_ACTIVE :
- $is_frozen = false;
- $desc = '抢冻结到激活积分';
- // 将冻结红包的金额先减扣掉
- $wallet = ScoreExpansionWallet::setData([
- 'mall_id' => $redpack->mall_id,
- 'user_id' => $user_id,
- 'is_frozen' => true,
- 'is_deduct' => true,
- 'integral' => -$money,
- 'source_table' => ScoreExpansion::briefTableName(),
- 'source_table_id' => $redpack->id,
- 'from_type' => ScoreExpansionEnum::FROM_RECEIVE,
- 'desc' => $desc,
- ]);
- if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
- $money = $wallet['money'];
- if ($money <= 0) throw new \Exception("暂无可激活的冻结积分");
- break;
- case ScoreExpansionEnum::INCREMENT_FREEZE :
- $is_frozen = true;
- $desc = '抢冻结积分';
- break;
- case ScoreExpansionEnum::INCREMENT_ACTIVE :
- $is_frozen = false;
- $desc = '抢激活积分';
- break;
- }
- // 增加用户的红包余额
- $res = ScoreExpansionWallet::setData([
- 'mall_id' => $redpack->mall_id,
- 'user_id' => $user_id,
- 'is_frozen' => $is_frozen,
- 'integral' => $money,
- 'source_table' => ScoreExpansion::briefTableName(),
- 'source_table_id' => $redpack->id,
- 'from_type' => ScoreExpansionEnum::FROM_RECEIVE,
- 'desc' => $desc,
- ]);
- if ($res == false) throw new Exception(ScoreExpansionWallet::getStaticError());
- // 增加抢红包次数
- $count_day = [
- 'mall_id' => $redpack->mall_id,
- 'rid' => $redpack->id,
- 'user_id' => $user_id,
- 'grab_num' => empty($today_grad['grab_num_total']) ? $rule_values['times'] - 1 : $rule_values['times'] - $today_grad['grab_num_total'] - 1,
- 'grab_num_total' => empty($today_grad['grab_num_total']) ? 1 : $today_grad['grab_num_total'] + 1,
- ];
- !empty($today_grad['id']) && $count_day['id'] = $today_grad['id'];
- $res = ScoreExpansionUserCountDay::setData($count_day);
- if ($res == false) throw new Exception(ScoreExpansionUserCountDay::getStaticError());
- // 增加抢红包数据
- $res = ScoreExpansionUserGrab::setData([
- 'mall_id' => $redpack->mall_id,
- 'rid' => $redpack->id,
- 'user_id' => $user_id,
- 'grab_money' => $money,
- ]);
- if ($res == false) throw new Exception(ScoreExpansionUserGrab::getStaticError());
- $trans->commit();
- return $money;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- $trans->rollBack();
- return false;
- }
- }
- /**
- * 赠送积分
- * @Author: lun
- * @DateTime: 2022/3/26 0026 14:51
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $order_id
- * @param $order_status 1=支付完成,2=订单完成
- * @param $order_type order=平台, StoreOrder=商户
- * @return bool
- */
- public static function giveRedpack($order_id, $order_status = 2, $order_type = "order")
- {
- try {
- // 查询订单
- $select = "mall_id,goods_id,user_id,goods_price,adjust_money,is_evaluate,num";
- $source_table = Order::tableName();
- //判断来源 订单表
- if ($order_type == 'order') {
- $orderData = Order::findOne(['id' => $order_id]);
- $order_details = OrderDetail::find()->select($select)->where(['order_id' => $order_id])->indexBy("goods_id")->asArray()->all();
- $source_table = Order::tableName();
- } elseif ($order_type == 'StoreOrder') {
- $orderData = StoreOrder::findOne(['order_no' => $order_id]);
- $order_id = $orderData->id;
- $orderData = StoreOrder::findOne(['id' => $order_id]);
- $order_details = StoreOrderDetail::find()->select($select)->where(['order_id' => $order_id])->indexBy("goods_id")->asArray()->all();
- $source_table = StoreOrder::tableName();
- }
- if (empty($orderData)) throw new \Exception("订单id={$order_id}的订单数据为空");
- if (empty($order_details)) throw new \Exception("订单id={$order_id}的订单详情数据为空");
- $user_id = $orderData->user_id;
- $mall_id = $orderData->mall_id;
- //20230404
- $user_data = User::findOne(['id' => $user_id]);
- //2024/09/13 liuzhantao 加上注释且优化条件
- // “赠送积分方式” 配置说明:
- // 当配置为“1”:订单支付后,有两种条件
- // 条件一. 订单确认收货且完成评价后,这笔订单赠送的冻结积分变为激活积分(释放所有积分)
- // 条件二. 订单确认收货但未完成评价,过n天后,释放所有积分
- // 当配置为“2”:订单支付后将不能获得任何积分,要订单完成后才能获得
- // 获取配置【1=支付完成,2=订单完成】
- $give_type = Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_SCORE_EXPANSION, 'basic.give_type');
- if ($give_type == 2 && $order_status != 2) { // 当配置为“2”,需要订单完成才能有积分,所以就直接return,反之配置为“1”,继续执行下面的
- return true;
- }
- //循环订单商品列表,没有单独设置则应用全局设置20230404
- //获取全局设置
- $config = Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_SCORE_EXPANSION, 'basic');
- $setting_data = [];
- if (!empty($config['buy_frozen_integral'])) {
- $config['buy_frozen_integral'] = json_decode($config['buy_frozen_integral'], true);
- $config['buy_frozen_integral']['parent'] = 0;
- $setting_data[] = $config['buy_frozen_integral'];
- }
- if (!empty($config['buy_integral'])) {
- $config['buy_integral'] = json_decode($config['buy_integral'], true);
- $config['buy_integral']['parent'] = 0;
- $setting_data[] = $config['buy_integral'];
- }
- if (!empty($config['parent_frozen_integral'])) {
- $config['parent_frozen_integral'] = json_decode($config['parent_frozen_integral'], true);
- $config['parent_frozen_integral']['parent'] = 1;
- $setting_data[] = $config['parent_frozen_integral'];
- }
- if (!empty($config['parent_integral'])) {
- $config['parent_integral'] = json_decode($config['parent_integral'], true);
- $config['parent_integral']['parent'] = 1;
- $setting_data[] = $config['parent_integral'];
- }
- foreach ($order_details as $goodsDetail) {
- //判断单独设置
- $list = '';
- if ($order_type == 'order') {
- $list = ScoreExpansionGoodsSetting::find()->where(['goods_id' => $goodsDetail['goods_id'], 'is_enable' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED])->asArray()->all();
- }
- if (!empty($list)) {
- foreach ($list as $item) {
- // 计算可赠送的红包金额
- $pay_money = $goodsDetail['goods_price'] + $goodsDetail['adjust_money'];
- $money = $item['is_percent'] == 0 ? $item['give'] : bcmul(($item['give'] / 100), $pay_money, 2);
- if ($money <= 0) continue;
- //判断受益者20230404
- $desc = '购物赠送';
- if ($item['parent'] == 1) {
- $user_id = $user_data['parent_id'];
- $desc = '直推下级购物赠送';
- if($user_id <= 0) continue;
- }
- // 订单完成 && 完成评价 && 符合条件一,把冻结的积分转换为激活积分
- if ($order_status == 2 && $give_type == 1) {
- if ($orderData->is_comment != 1 || $config['change_activation_integral'] != 1) return true; // 没有评价 或 不符合条件一,就return
- if ($item['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN) {
- // 增加激活积分
- $frozenWallet = ScoreExpansionWallet::setData([
- 'mall_id' => $item['mall_id'],
- 'user_id' => $user_id,
- 'is_frozen' => false,
- 'integral' => $money,
- 'source_table' => Order::tableName(),
- 'source_table_id' => $order_id,
- 'from_type' => ScoreExpansionEnum::FROM_GOODS,
- 'desc' => $desc,
- ]);
- if ($frozenWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
- //扣减冻结积分
- $activationWallet = ScoreExpansionWallet::setData([
- 'mall_id' => $item['mall_id'],
- 'user_id' => $user_id,
- 'is_frozen' => true,
- 'integral' => -$money,
- 'source_table' => Order::tableName(),
- 'source_table_id' => $order_id,
- 'from_type' => ScoreExpansionEnum::FROM_GOODS,
- 'desc' => $desc,
- ]);
- if ($activationWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
- //修改定时任务状态
- $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]);
- $task->is_work = 1;
- if (!$task->save()) {
- throw new Exception(ScoreExpansionRateTask::getStaticError());
- }
- }
- } else {
- // 插入用户积分表
- $wallet = ScoreExpansionWallet::setData([
- 'mall_id' => $item['mall_id'],
- 'user_id' => $user_id,
- 'is_frozen' => $item['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN ? true : false,
- 'integral' => $money,
- 'source_table' => Order::tableName(),
- 'source_table_id' => $order_id,
- 'from_type' => ScoreExpansionEnum::FROM_GOODS,
- 'desc' => $desc,
- ]);
- if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
- if ($give_type == 2 && $order_status == 2) {
- //修改定时任务状态
- $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]);
- $task->is_work = 1;
- if (!$task->save()) {
- throw new Exception(ScoreExpansionRateTask::getStaticError());
- }
- }
- }
- }
- } else {
- foreach ($setting_data as $setting) {
- // 计算可赠送的红包金额
- $pay_money = $goodsDetail['goods_price'] + $goodsDetail['adjust_money'];
- $money = $setting['is_percent'] == 0 ? $setting['give'] * $goodsDetail['num'] : bcmul(($setting['give'] / 100), $pay_money, 2);
- if ($money <= 0) continue;
- //判断受益者20230404
- $desc = '购物赠送';
- if ($setting['parent'] == 1) {
- $user_id = $user_data['parent_id'];
- $desc = '直推下级购物赠送';
- if($user_id <= 0) continue;
- }
- //订单完成并且完成评价时 把冻结的积分转换为激活积分
- if ($order_status == 2 && $give_type == 1) {
- if ($orderData->is_comment != 1) return true;
- if ($setting['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN) {
- // 增加激活积分
- $frozenWallet = ScoreExpansionWallet::setData([
- 'mall_id' => $goodsDetail['mall_id'],
- 'user_id' => $user_id,
- 'is_frozen' => false,
- 'integral' => $money,
- 'source_table' => $source_table,
- 'source_table_id' => $order_id,
- 'from_type' => ScoreExpansionEnum::FROM_GOODS,
- 'desc' => $desc,
- ]);
- if ($frozenWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
- //扣减冻结积分
- $activationWallet = ScoreExpansionWallet::setData([
- 'mall_id' => $goodsDetail['mall_id'],
- 'user_id' => $user_id,
- 'is_frozen' => true,
- 'integral' => -$money,
- 'source_table' => $source_table,
- 'source_table_id' => $order_id,
- 'from_type' => ScoreExpansionEnum::FROM_GOODS,
- 'desc' => $desc,
- ]);
- if ($activationWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
- //修改定时任务状态
- $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]);
- $task->is_work = 1;
- if (!$task->save()) {
- throw new Exception(ScoreExpansionRateTask::getStaticError());
- }
- }
- } else {
- // 插入用户积分表
- $wallet = ScoreExpansionWallet::setData([
- 'mall_id' => $goodsDetail['mall_id'],
- 'user_id' => $user_id,
- 'is_frozen' => $setting['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN ? true : false,
- 'integral' => $money,
- 'source_table' => $source_table,
- 'source_table_id' => $order_id,
- 'from_type' => ScoreExpansionEnum::FROM_GOODS,
- 'desc' => $desc,
- ]);
- if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
- if ($give_type == 2 && $order_status == 2) {
- //修改定时任务状态
- $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]);
- $task->is_work = 1;
- if (!$task->save()) {
- throw new Exception(ScoreExpansionRateTask::getStaticError());
- }
- }
- }
- }
- }
- }
- //循环订单商品列表,没有单独设置则应用全局设置20230404 end
- // 获取设置了红包的商品
- // $list = ScoreExpansionGoodsSetting::find()->where(['goods_id' => $goods_ids, 'is_enable' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED])->asArray()->all();true;
- // if (empty($list)) return true;
- // $orderData = Order::findOne(['id'=>$order_id]);
- // foreach ($list as $item) {
- // $is_evaluate = $order_details[$item['goods_id']]['is_evaluate'];
- // // 计算可赠送的红包金额
- // $pay_money = $order_details[$item['goods_id']]['goods_price'] + $order_details[$item['goods_id']]['adjust_money'];
- // $money = $item['is_percent'] == 0 ? $item['give'] : bcmul(($item['give'] / 100), $pay_money, 2);
- // if ($money <= 0) continue;
- //
- // //判断受益者20230404
- // if($item['parent'] == 1) $user_id = $user_data['parent_id'];
- //
- // //订单完成并且完成评价时 把冻结的积分转换为激活积分
- // if($order_status == 2 && $give_type == 1){
- // if($orderData->is_comment != 1) return true;
- // if($item['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN){
- // // 增加激活积分
- //// $money = $item['give'];
- // $frozenWallet = ScoreExpansionWallet::setData([
- // 'mall_id' => $item['mall_id'],
- // 'user_id' => $user_id,
- // 'is_frozen' => false,
- // 'integral' => $money,
- // 'source_table' => Order::tableName(),
- // 'source_table_id' => $order_id,
- // 'from_type' => ScoreExpansionEnum::FROM_GOODS,
- // 'desc' => '购物赠送',
- // ]);
- // if ($frozenWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
- // //扣减冻结积分
- // $activationWallet = ScoreExpansionWallet::setData([
- // 'mall_id' => $item['mall_id'],
- // 'user_id' => $user_id,
- // 'is_frozen' => true,
- // 'integral' => -$money,
- // 'source_table' => Order::tableName(),
- // 'source_table_id' => $order_id,
- // 'from_type' => ScoreExpansionEnum::FROM_GOODS,
- // 'desc' => '购物赠送',
- // ]);
- // if ($activationWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
- // //修改定时任务状态
- // $task = ScoreExpansionRateTask::findOne(['order_id'=>$order_id]);
- // $task->is_work = 1;
- // if(!$task->save()){
- // throw new Exception(ScoreExpansionRateTask::getStaticError());
- // }
- // }
- // }else{
- // // 插入用户积分表
- // $wallet = ScoreExpansionWallet::setData([
- // 'mall_id' => $item['mall_id'],
- // 'user_id' => $user_id,
- // 'is_frozen' => $item['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN ? true : false,
- // 'integral' => $money,
- // 'source_table' => Order::tableName(),
- // 'source_table_id' => $order_id,
- // 'from_type' => ScoreExpansionEnum::FROM_GOODS,
- // 'desc' => '购物赠送',
- // ]);
- // if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
- //
- // if($give_type == 2 && $order_status == 2){
- // //修改定时任务状态
- // $task = ScoreExpansionRateTask::findOne(['order_id'=>$order_id]);
- // $task->is_work = 1;
- // if(!$task->save()){
- // throw new Exception(ScoreExpansionRateTask::getStaticError());
- // }
- // }
- // }
- // }
- return true;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 红包返回
- * @Author: lun
- * @DateTime: 2022/9/21 0021 9:54
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $order_detail_id
- * @param array $params
- * @return bool
- */
- public static function backRedpack($order_detail_id, $params = [])
- {
- $trans = Yii::$app->db->beginTransaction();
- try {
- $deduct = OrderMarketing::find()->select('deduct_currency')->where(['order_detail_id' => $order_detail_id, 'status' => StatusEnum::ENABLED])->scalar();
- if (empty($deduct)) {
- $trans->commit();
- return true;
- }
- $deduct_arr = Json::decode($deduct, true);
- foreach ($deduct_arr as $key => $item) {
- // 判断里面是否存在激活积分跟冻结积分
- if (in_array($key, [ScoreExpansionEnum::WALLET_INTEGRAL, ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN]) && $item['money'] > 0) {
- $wallet = ScoreExpansionWallet::setData([
- 'mall_id' => $params['mall_id'],
- 'user_id' => $params['user_id'],
- 'is_frozen' => $key == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN ? true : false,
- 'integral' => $item['money'],
- 'source_table' => $params['source_table'],
- 'source_table_id' => $params['source_table_id'],
- 'from_type' => $params['from_type'],
- 'desc' => $params['desc'] ?? '',
- 'no_icr_total' => true,// 不增加积分累计总额
- ]);
- if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
- }
- }
- $trans->commit();
- return true;
- } catch (\Exception $e) {
- $trans->rollBack();
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 收银台赠送积分
- * @Author: lun
- * @DateTime: 2022/3/26 0026 14:51
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $order_id
- * @param $order_status 1=支付完成,2=订单完成
- * @param $order_type order=平台, StoreOrder=商户
- * @return bool
- */
- public static function giveCashierRedpack($order_id, $order_status = 2)
- {
- try {
- // 查询订单
- $source_table = StoreCashierOrder::tableName();
- $orderData = StoreCashierOrder::findOne(['id' => $order_id]);
- if (empty($orderData)) throw new \Exception("订单id={$order_id}的订单数据为空");
- //判断来源 商户 门店 平台20230404
- $user_id = $orderData->user_id;
- $mall_id = $orderData->mall_id;
- //20230404
- $user_data = User::findOne(['id' => $user_id]);
- //循环订单商品列表,没有单独设置则应用全局设置20230404
- //获取全局设置
- $config = Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_SCORE_EXPANSION, 'basic');
- $setting_data = [];
- if (!empty($config['buy_frozen_integral'])) {
- $config['buy_frozen_integral'] = json_decode($config['buy_frozen_integral'], true);
- $config['buy_frozen_integral']['parent'] = 0;
- $setting_data[] = $config['buy_frozen_integral'];
- }
- if (!empty($config['buy_integral'])) {
- $config['buy_integral'] = json_decode($config['buy_integral'], true);
- $config['buy_integral']['parent'] = 0;
- $setting_data[] = $config['buy_integral'];
- }
- if (!empty($config['parent_frozen_integral'])) {
- $config['parent_frozen_integral'] = json_decode($config['parent_frozen_integral'], true);
- $config['parent_frozen_integral']['parent'] = 1;
- $setting_data[] = $config['parent_frozen_integral'];
- }
- if (!empty($config['parent_integral'])) {
- $config['parent_integral'] = json_decode($config['parent_integral'], true);
- $config['parent_integral']['parent'] = 1;
- $setting_data[] = $config['parent_integral'];
- }
- foreach ($setting_data as $setting) {
- // 计算可赠送的红包金额
- $pay_money = $orderData['pay_money'];
- $money = $setting['is_percent'] == 0 ? $setting['give'] : bcmul(($setting['give'] / 100), $pay_money, 2);
- if ($money <= 0) continue;
- //判断受益者
- $desc = '购物赠送';
- if ($setting['parent'] == 1) {
- $user_id = $user_data['parent_id'];
- $desc = '直推下级购物赠送';
- }
- // 插入用户积分表
- $wallet = ScoreExpansionWallet::setData([
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'is_frozen' => $setting['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN ? true : false,
- 'integral' => $money,
- 'source_table' => $source_table,
- 'source_table_id' => $order_id,
- 'from_type' => ScoreExpansionEnum::FROM_GOODS,
- 'desc' => $desc,
- ]);
- if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
- }
- //循环订单商品列表,没有单独设置则应用全局设置20230404 end
- return true;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 店铺赠送积分
- * @param $order_id
- * @param $order_status
- * @param $order_type
- * @return bool
- */
- public static function giveStoreRedpack($order_id, $order_status = 2, $order_type = 'order')
- {
- try {
- // 查询订单
- $select = "mall_id,goods_id,user_id,goods_price,adjust_money,is_evaluate";
- $source_table = Order::tableName();
- //判断来源 订单表
- if ($order_type == 'order') {
- $orderData = Order::findOne(['id' => $order_id]);
- $order_details = OrderDetail::find()->select($select)->where(['order_id' => $order_id])->indexBy("goods_id")->asArray()->all();
- $source_table = Order::tableName();
- } elseif ($order_type == 'StoreOrder') {
- $orderData = StoreOrder::findOne(['id' => $order_id]);
- $order_details = StoreOrderDetail::find()->select($select)->where(['order_id' => $order_id])->indexBy("goods_id")->asArray()->all();
- $source_table = StoreOrder::tableName();
- }
- if (empty($orderData)) throw new \Exception("订单id={$order_id}的订单数据为空");
- if (empty($order_details)) throw new \Exception("订单id={$order_id}的订单详情数据为空");
- //查找店铺持有者的user_id
- if(empty($orderData->store_id)) throw new \Exception("订单id={$order_id}的店铺id为空");
- $storeData = Store::findOne(['id'=>$orderData->store_id]);
- $user_id = $storeData->user_id;
- $mall_id = $storeData->mall_id;
- //20230404
- $user_data = User::findOne(['id' => $user_id]);
- //当配置为1时 订单完成 将订单获得的冻结积分转变为激活积分
- //当配置为2时 订单支付完成将不能获得任何积分
- // 获取配置【1=支付完成,2=订单完成】
- $give_type = Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_SCORE_EXPANSION, 'basic.give_type');
- if ($give_type == 2 && $order_status != 2) {
- return true;
- }
- //循环订单商品列表,没有单独设置则应用全局设置20230404
- //获取全局设置
- $config = Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_SCORE_EXPANSION, 'basic');
- $setting_data = [];
- //门店设置
- if(!empty($config['store_frozen_integral'])){
- $config['store_frozen_integral'] = json_decode($config['store_frozen_integral'],true);
- $config['store_frozen_integral']['parent'] = 0;
- $setting_data[] = $config['store_frozen_integral'];
- }
- if(!empty($config['store_integral'])){
- $config['store_integral'] = json_decode($config['store_integral'],true);
- $config['store_integral']['parent'] = 0;
- $setting_data[] = $config['store_integral'];
- }
- if(!empty($config['parent_store_frozen_integral'])){
- $config['parent_store_frozen_integral'] = json_decode($config['parent_store_frozen_integral'],true);
- $config['parent_store_frozen_integral']['parent'] = 1;
- $setting_data[] = $config['parent_store_frozen_integral'];
- }
- if(!empty($config['parent_store_integral'])){
- $config['parent_store_integral'] = json_decode($config['parent_store_integral'],true);
- $config['parent_store_integral']['parent'] = 1;
- $setting_data[] = $config['parent_store_integral'];
- }
- foreach ($order_details as $goodsDetail) {
- //判断单独设置
- $list = '';
- if ($order_type == 'order') {
- $list = ScoreExpansionGoodsSetting::find()->where(['goods_id' => $goodsDetail['goods_id'], 'is_enable' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED])->asArray()->all();
- }
- if (!empty($list)) {
- foreach ($list as $item) {
- // 计算可赠送的红包金额
- $pay_money = $goodsDetail['goods_price'] + $goodsDetail['adjust_money'];
- $money = $item['is_percent'] == 0 ? $item['give'] : bcmul(($item['give'] / 100), $pay_money, 2);
- if ($money <= 0) continue;
- //判断受益者20230404
- $desc = '购物赠送';
- if ($item['parent'] == 1) {
- $user_id = $user_data['parent_id'];
- $desc = '直推下级购物赠送';
- }
- //订单完成并且完成评价时 把冻结的积分转换为激活积分
- if ($order_status == 2 && $give_type == 1) {
- if ($orderData->is_comment != 1) return true;
- if ($item['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN) {
- // 增加激活积分
- $frozenWallet = ScoreExpansionWallet::setData([
- 'mall_id' => $item['mall_id'],
- 'user_id' => $user_id,
- 'is_frozen' => false,
- 'integral' => $money,
- 'source_table' => Order::tableName(),
- 'source_table_id' => $order_id,
- 'from_type' => ScoreExpansionEnum::FROM_GOODS,
- 'desc' => $desc,
- ]);
- if ($frozenWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
- //扣减冻结积分
- $activationWallet = ScoreExpansionWallet::setData([
- 'mall_id' => $item['mall_id'],
- 'user_id' => $user_id,
- 'is_frozen' => true,
- 'integral' => -$money,
- 'source_table' => Order::tableName(),
- 'source_table_id' => $order_id,
- 'from_type' => ScoreExpansionEnum::FROM_GOODS,
- 'desc' => $desc,
- ]);
- if ($activationWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
- //修改定时任务状态
- $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]);
- $task->is_work = 1;
- if (!$task->save()) {
- throw new Exception(ScoreExpansionRateTask::getStaticError());
- }
- }
- } else {
- // 插入用户积分表
- $wallet = ScoreExpansionWallet::setData([
- 'mall_id' => $item['mall_id'],
- 'user_id' => $user_id,
- 'is_frozen' => $item['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN ? true : false,
- 'integral' => $money,
- 'source_table' => Order::tableName(),
- 'source_table_id' => $order_id,
- 'from_type' => ScoreExpansionEnum::FROM_GOODS,
- 'desc' => $desc,
- ]);
- if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
- if ($give_type == 2 && $order_status == 2) {
- //修改定时任务状态
- $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]);
- $task->is_work = 1;
- if (!$task->save()) {
- throw new Exception(ScoreExpansionRateTask::getStaticError());
- }
- }
- }
- }
- } else {
- foreach ($setting_data as $setting) {
- // 计算可赠送的红包金额
- $pay_money = $goodsDetail['goods_price'] + $goodsDetail['adjust_money'];
- $money = $setting['is_percent'] == 0 ? $setting['give'] : bcmul(($setting['give'] / 100), $pay_money, 2);
- if ($money <= 0) continue;
- //判断受益者20230404
- $desc = '购物赠送';
- if ($setting['parent'] == 1) {
- $user_id = $user_data['parent_id'];
- $desc = '直推下级购物赠送';
- }
- //订单完成并且完成评价时 把冻结的积分转换为激活积分
- if ($order_status == 2 && $give_type == 1) {
- if ($orderData->is_comment != 1) return true;
- if ($setting['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN) {
- // 增加激活积分
- $frozenWallet = ScoreExpansionWallet::setData([
- 'mall_id' => $goodsDetail['mall_id'],
- 'user_id' => $user_id,
- 'is_frozen' => false,
- 'integral' => $money,
- 'source_table' => $source_table,
- 'source_table_id' => $order_id,
- 'from_type' => ScoreExpansionEnum::FROM_GOODS,
- 'desc' => $desc,
- ]);
- if ($frozenWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
- //扣减冻结积分
- $activationWallet = ScoreExpansionWallet::setData([
- 'mall_id' => $goodsDetail['mall_id'],
- 'user_id' => $user_id,
- 'is_frozen' => true,
- 'integral' => -$money,
- 'source_table' => $source_table,
- 'source_table_id' => $order_id,
- 'from_type' => ScoreExpansionEnum::FROM_GOODS,
- 'desc' => $desc,
- ]);
- if ($activationWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
- //修改定时任务状态
- $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]);
- $task->is_work = 1;
- if (!$task->save()) {
- throw new Exception(ScoreExpansionRateTask::getStaticError());
- }
- }
- } else {
- // 插入用户积分表
- $wallet = ScoreExpansionWallet::setData([
- 'mall_id' => $goodsDetail['mall_id'],
- 'user_id' => $user_id,
- 'is_frozen' => $setting['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN ? true : false,
- 'integral' => $money,
- 'source_table' => $source_table,
- 'source_table_id' => $order_id,
- 'from_type' => ScoreExpansionEnum::FROM_GOODS,
- 'desc' => $desc,
- ]);
- if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
- if ($give_type == 2 && $order_status == 2) {
- //修改定时任务状态
- $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]);
- $task->is_work = 1;
- if (!$task->save()) {
- throw new Exception(ScoreExpansionRateTask::getStaticError());
- }
- }
- }
- }
- }
- }
- //循环订单商品列表,没有单独设置则应用全局设置20230404 end
- return true;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
|