$item['mom_app_uid']])->where(['order_sn' => $item['order_sn']])->first(); if (!empty($logObj)) { // 存在. $isOldLog = true; $oldLogObj = clone $logObj; // 克隆老订单. $logObj->updated_at = $item['updated_at']; $logObj->paid_at = $item['paid_at']; $logObj->invite_uid = $item['invite_uid']; $logObj->transaction_no = $item['transaction_no']; $logObj->pay_status = $item['pay_status']; $logObj->order_status = $item['order_status']; $logObj->is_deduction = $item['is_deduction']; $logObj->save(); } else { // 不存在,新建. $logObj = PayLogsModel::create($item); } if (!empty($logObj->_id)) { $eventParams[] = [ 'log_id' => $logObj->_id, 'old_log' => [ 'pay_status' => $isOldLog ? $oldLogObj->pay_status : 0, 'order_status' => $isOldLog ? $oldLogObj->order_status : 0, ] ]; } } // 触发事件. event(new InnerUserOrderIncEvent($eventParams)); return true; } /** * 随机扣量标记. * @param array $item * @return array */ public function oneRandDeduction(array $item) { //订单成功才参与扣量 if ($item['pay_status'] != 1 && $item['order_status'] != 1) { return $item; } $deductionConfig = app(ConfigRepoistory::class)->pickByKeys(['preferential_order', 'user_deduction_switch'], 'sys'); // 判断总扣规则是否开启 if ($deductionConfig['user_deduction_switch']['svalue'] == 0) { return $item; } $iTodayPayOrder = PayLogsModel::query()->where([ 'channel_uid' => $item['channel_uid'], 'pay_status' => 1, 'order_status' => 1, ])->count(); //每日前多少单不扣 if ($iTodayPayOrder < $deductionConfig['preferential_order']['svalue']) { return $item; } //代理扣量验证 $agentInfo = app(AgentRepository::class)->findBy('uid', $item['agent_uid'], ['deduction_switch', 'jdata', 'preferential_order']); if ($agentInfo->deduction_switch == 1) { if ($iTodayPayOrder >= $agentInfo->preferential_order) { $aData = json_decode($agentInfo->jdata, true); $item['is_deduction'] = $this->isDeduction($item, $aData); } } else if ($agentInfo->deduction_switch == 0) { // 总扣量规则 $ruleData = app(AppDeductionRuleRepository::class)->findWhere(['app_uid' => $item['mom_app_uid'], 'deduction_type' => 'cps', 'deduction_rule' => 2])->keyBy('deduction_method'); $userSuccessOrder = PayLogsModel::query()->where(['user_uid' => $item['user_uid'], 'pay_status' => 1, 'order_status' => 1])->count(); if ($userSuccessOrder > 0) { if (isset($ruleData[2])) { $aData = json_decode($ruleData[2]->jdata, true); $item['is_deduction'] = $this->isDeduction($item, $aData, 1); } } else { //首充扣量 if (isset($ruleData[1])) { $aData = json_decode($ruleData[1]->jdata, true); $item['is_deduction'] = $this->isDeduction($item, $aData, 2); } } } else { $item['is_deduction'] = 0; } return $item; } /** * 判断是否扣量 * * @param array $item * @param array $aData * @param string $method * @return int */ private function isDeduction(array $item,array $aData, $method = '') { $isDeduction = 0; $scope = []; $deduction = []; $deductionPrice = $aData['deduction_price']; $deductionNumber = $aData['deduction_number']; foreach ($deductionPrice as $key => $val) { if ($item['real_amount'] >= $val['min_price'] && $item['real_amount'] <= $val['max_price']) { $scope = $val; $deduction = $deductionNumber[$key]; } } //如果没有符合的金额规则,就返回 if (empty($scope) || $deduction['deduction'] == 0) { return $isDeduction; } $hashKey = RedisKeysConstant::DEDUCTION_RULES_HASH . $item['mom_app_uid'] . $item['channel_uid'] . $method; //获取扣量标记 $areaKey = $scope['min_price'] . '-' . $scope['max_price']; $deductionMark = Redis::hget($hashKey, $areaKey); $mark = [0,0]; if ($deductionMark) { $mark = explode('-', $deductionMark); //如果一轮规则达到 且扣量完成 则删除并初始化标记 if ($mark[0] == $deduction['number'] && $mark[1] == $deduction['deduction']) { Redis::hDel($hashKey, $areaKey); $mark = [0,0]; } //如果扣量达到,但是一轮还没结束则直接返回 if ($mark[1] == $deduction['deduction']) { // $mark[0] + 1 参与订单总数加1 Redis::hset($hashKey, $areaKey, $mark[0] + 1 . '-' . $mark[1]); return $isDeduction; } } //必扣计算 if (($deduction['number'] - $mark[0]) <= ($deduction['deduction'] - $mark[1])) { $isDeduction = 1; } else { $boxItems = [ 1 => ['name' => 0, 'rate' => 80], 2 => ['name' => 1, 'rate' => 20] ]; $luckyBox = new LuckyBox(); foreach ($boxItems as $id => $items) { $card = new IdCard(); $card->setId($id)->setRate($items['rate']); $luckyBox->add($card); } $boxValue = $boxItems[$luckyBox->draw()->getId()]; $isDeduction = $boxValue['name']; } if ($isDeduction == 1) { $mark[1] += 1; } $mark[0] += 1; Redis::hset($hashKey, $areaKey, $mark[0] . '-' . $mark[1]); return $isDeduction; } }