admin/plugins/red-packet/src/services/RedOrderService.php

315 lines
9.6 KiB
PHP

<?php
namespace Yunshop\RedPacket\services;
use Yunshop\RedPacket\models\QuotaLogsModel;
use Yunshop\RedPacket\models\QuotaManagementModel;
use Yunshop\RedPacket\models\ReceiveLogsModel;
use Yunshop\RedPacket\models\RedPacketGoodsModel;
use Yunshop\StoreCashier\common\models\StoreSetting;
class RedOrderService
{
public $order;
public $ratio;
public $goods_set;
protected $casts = [
'value' => 'json'
];
public function getProportion($order,$is_estimate = 0)
{
$red_packet_order = QuotaLogsModel::where('order_id',$order->id)->first();
if ($red_packet_order) {
\Log::debug('订单已产生分红');
return $red_packet_order->receive_amount;
}
// \Log::debug('订单类型值:',$order->plugin_id);
if (app('plugins')->isEnabled('store-cashier')) {
if ($this->order->plugin_id == 32) {
$order = \Yunshop\StoreCashier\common\models\Order::find($order->id);
}
if ($this->order->plugin_id == 31) {
$order = \Yunshop\StoreCashier\common\models\Order::find($order->id);
}
}
if (app('plugins')->isEnabled('hotel') && $this->order->plugin_id == '33') {
$order = \Yunshop\Hotel\common\models\Order::find($order->id);
}
if ($is_estimate) {
return $this->estimateOrderAward($order);
}
$this->operation($order);
return 0;
}
public function estimateOrderAward($order)
{
$total_amount = 0;
//商品组合
$all_order_goods = $order->hasManyOrderGoods;
//插件设置红包比例
$this->goods_set = $this->getSet();
foreach ($all_order_goods as $key => $value) {
if ($value['payment_amount'] == 0 && !$this->goods_set['order_calculate']) {
continue;
}
//获取商品设置
$scale = RedPacketGoodsModel::where('goods_id',$value['goods_id'])->first();
switch ($order->plugin_id) {
case 39:
$ratio = $this->cashierScale($scale,$value);
break;
case 31:
$ratio = $this->cashierScale($scale,$value);
break;
case 32:
$ratio = $this->storeScale($scale,$value);
break;
case 70:
$ratio = $this->cpsScale($scale,$value);
break;
default:
$ratio = $this->shopScale($scale,$value);
break;
}
if (empty($ratio)) {
continue ;
}
$receive_amount = proportionMath($ratio['goods_price'] , $ratio['member_proportion']);
if($receive_amount > 0){
if ($receive_amount <= 0.01) {
$receive_amount = 0.01;
}
$total_amount += $receive_amount;
}
}
return $total_amount;
}
public function operation($order)
{
//商品组合
$all_order_goods = $order->hasManyOrderGoods;
//插件设置红包比例
$this->goods_set = $this->getSet();
$plugin_id = $order->plugin_id;
foreach ($all_order_goods as $key => $value) {
if ($value['payment_amount'] == 0 && !$this->goods_set['order_calculate']) {
\Log::debug('商品实付金额为0,不产生红包');
continue;
}
//获取商品设置
$scale = RedPacketGoodsModel::where('goods_id',$value['goods_id'])->first();
switch ($plugin_id) {
case 39:
$ratio = $this->cashierScale($scale,$value);
break;
case 31:
$ratio = $this->cashierScale($scale,$value);
break;
case 32:
$ratio = $this->storeScale($scale,$value);
break;
case 70:
$ratio = $this->cpsScale($scale,$value);
break;
default:
$ratio = $this->shopScale($scale,$value);
break;
}
if (empty($ratio)) {
// \Log::debug('比例未设置');
continue ;
}
$this->createRedPacketLogs($ratio,$order);
}
}
//商城,拼团,酒店,供应商:订单
public function shopScale($scale,$goods)
{
$data = $this->redScale($goods);
//商品未开启每日红包比例
if ($scale['is_open'] == 0) {
return $data=[];
}
//商品独立设置红包比例
if ($scale['scale'] > 0) {
$data['member_proportion'] = $scale['scale'];
return $data;
}
return $data;
}
//门店收银台订单
public function cashierScale($scale,$goods)
{
//默认红包比例
$data = $this->redScale($goods);
if ($scale['is_cashier'] == 1 && $scale['cashier_scale'] > 0) {
$data['member_proportion'] = $scale['cashier_scale'];
}
if ($scale['is_cashier'] == 0) {
return $data=[];
}
return $data;
}
//门店订单
public function storeScale($scale,$goods)
{
$data = $this->redScale($goods);
//商品未开启每日红包比例
if ($scale['is_open'] == 0) {
return $data=[];
}
//商品独立设置红包比例
if ($scale['scale'] > 0) {
$data['member_proportion'] = $scale['scale'];
return $data;
}
$setting = StoreSetting::select('value')
->where('store_id',$this->store_id)
->where('key','red_packet')
->first();
$casts = $setting['value'];
if ($casts['is_store'] == 1 && $casts['store_scale'] > 0) {
$data['member_proportion'] = $casts['store_scale'];
}
return $data;
}
//cps订单
public function cpsScale($scale,$goods)
{
//默认红包比例
$data = $this->redScale($goods);
if ($scale['is_cps'] == 1 && $scale['cps_scale'] > 0) {
$data['member_proportion'] = $scale['cps_scale'];
}
if ($scale['is_cps'] == 0) {
return $data=[];
}
return $data;
}
//红包默认比例设置
public function redScale($goods)
{
//判断是否选择商品现价或者商品实付金额
$goods_price = $this->goods_set['order_calculate'] == 1 ?$goods['goods_price'] : $goods['payment_amount'];
$data = [
'member_proportion' => $this->goods_set['member_proportion'],
'goods_price' =>$goods_price
];
return $data;
}
protected function createRedPacketLogs($resources,$order)
{
$receive_amount = proportionMath($resources['goods_price'] , $resources['member_proportion']);
if($receive_amount <= 0){
\Log::debug('红包值小于或等于零时不产生红包',$receive_amount);
return ;
}
$min_amount = 0.01;
if($receive_amount <= $min_amount && $receive_amount > 0){
$receive_amount = $min_amount;
}
$data = [
'uniacid' => \YunShop::app()->uniacid,
'order_id' => $order->id,
'order_sn' => $order->order_sn,
'member_id' => $order->uid,
'quota_proportion' => $resources['member_proportion'],
'amount' => $resources['goods_price'],
'receive_amount' => $receive_amount,
];
\Log::debug('红包生成参数',$data);
QuotaLogsModel::create($data);
$this->getQuota($order);
$this->getNotice($resources['member_proportion'],$order);
}
/**
* @param $scale
* @param $order
* 消息发送
*/
private function getNotice($scale,$order){
\Log::debug('消息发送',$scale);
$member_id = $order->uid;
$receive_amount = proportionMath($order->price , $scale);
//累计红包总额
$red_packet['amount_sum'] = round(QuotaLogsModel::where('member_id',$member_id)->sum('receive_amount'),2);
//产生红包额度
$red_packet['amount'] = $receive_amount;
//领取红包总额
$red_packet['member_receive_amount'] = round(ReceiveLogsModel::where('member_id',$member_id)->sum('receive_amount'),2);
$red_packet['unreceive_amount'] = $red_packet['amount_sum']-$red_packet['member_receive_amount'];
MessageService::enrolMessage($member_id,$red_packet);
}
/**
* 红包额度管理
*/
private function getQuota($order){
\Log::debug('红包额度');
$member = QuotaManagementModel::where('member_id',$order->uid)->count();
if($member < 1){
$data = [
'uniacid' => \YunShop::app()->uniacid,
'member_id' => $order->uid,
];
QuotaManagementModel::create($data);
}
}
/**
* 插件设置红包比例
*/
public function getSet(){
$set = \Setting::get('plugin.red-packet');
if(empty($set)){
return '';
}
$goods_data['goods_id'] = array_column($set['red_packet_goods'],'goods_id');
$goods_data['member_proportion'] = $set['member_proportion'];
$goods_data['order_completed'] = $set['order_completed'];
$goods_data['order_calculate'] = $set['order_calculate'];
return $goods_data;
}
}