diff --git a/app/common/dao/store/platformCommission/LegumesLogDao.php b/app/common/dao/store/platformCommission/LegumesLogDao.php new file mode 100644 index 0000000..d3b3671 --- /dev/null +++ b/app/common/dao/store/platformCommission/LegumesLogDao.php @@ -0,0 +1,19 @@ +dao = $dao; + } + + + + +} diff --git a/app/common/repositories/store/platformCommission/LegumesRepository.php b/app/common/repositories/store/platformCommission/LegumesRepository.php index dbf2119..bb42020 100644 --- a/app/common/repositories/store/platformCommission/LegumesRepository.php +++ b/app/common/repositories/store/platformCommission/LegumesRepository.php @@ -34,16 +34,17 @@ class LegumesRepository extends BaseRepository{ // 获取周期内基金池金额 $totalIntegralReleaseMoney = (float)app()->make(RecordRepository::class)->getTotalIntegralReleaseMoney($startTime,$endTime); if($totalIntegralReleaseMoney <= 0) throw new Exception('基金池金额为0'); + $legumesPrice = $set['legumes_price'] <= 0 ? 0.5 : $set['legumes_price']; // 计算豆豆数量 文创豆=平台抽成的销售额10%➗首次价格0.5 - $legumesNum = sprintf("%.2f",$totalIntegralReleaseMoney / $set['legumes_price']); - // 明日价格 下次文创豆价格=基金池金额(消费者的70%)➗文创豆数量 + $legumesNum = (float)sprintf("%.2f",$totalIntegralReleaseMoney / $legumesPrice); + // 明日价格 下次文创豆价格=基金池金额(消费者的70%)➗ 文创豆数量 $allIntegralReleaseMoney = (float)app()->make(RecordRepository::class)->getTotalIntegralReleaseMoney(); - $allLegumes = $this->dao->getSearch([])->sum('legumes_num'); - $tomorrowLegumesPrice = sprintf("%.2f",$allIntegralReleaseMoney / $allLegumes); + $allLegumes = (float)$this->dao->getSearch([])->sum('legumes_num'); + $tomorrowLegumesPrice = sprintf("%.2f",$allIntegralReleaseMoney / ($allLegumes + $legumesNum)); // 记录 $recordId = Legumes::insertGetId([ 'total_platform_commission_money' => $totalIntegralReleaseMoney, - 'legumes_price' => $set['legumes_price'], + 'legumes_price' => $legumesPrice, 'legumes_num' => $legumesNum, 'tomorrow_legumes_price' => $tomorrowLegumesPrice, 'start_time' => $startTime, @@ -62,12 +63,24 @@ class LegumesRepository extends BaseRepository{ Db::commit(); }catch(Exception $e){ Db::rollback(); - Log::info('合伙人佣金结算 - 错误:'.$e->getMessage()); + Log::info('计算昨天产生的豆豆总数 - 错误:'.$e->getMessage()); } } + /** + * Common: 获取某条信息 + * Author: wu-hui + * Time: 2023/12/29 9:24 + * @param $id + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function getLegumes($id){ + $info = $this->dao->get($id); + return $info ? $info->toArray() : []; + } - - } diff --git a/app/jobs/store/platformCommission/SparateLegumesJob.php b/app/jobs/store/platformCommission/SparateLegumesJob.php index dc7c929..a0f5fde 100644 --- a/app/jobs/store/platformCommission/SparateLegumesJob.php +++ b/app/jobs/store/platformCommission/SparateLegumesJob.php @@ -3,11 +3,15 @@ namespace app\jobs\store\platformCommission; +use app\common\model\store\order\StoreOrder; +use app\common\model\store\platformCommission\LegumesLog; +use app\common\repositories\store\platformCommission\LegumesLogRepository; +use app\common\repositories\store\platformCommission\LegumesRepository; use crmeb\interfaces\JobInterface; use think\facade\Log; /** - * Common: 给昨日消费者分豆豆 + * Common: 给本周期内消费者分豆豆 * Author: wu-hui * Time: 2023/12/28 19:01 * Class SparateLegumesJob @@ -17,15 +21,41 @@ class SparateLegumesJob implements JobInterface{ public function fire($job,$data){ try{ - Log::info('给昨日消费者分豆豆 - 开始处理: '.var_export($data,1)); - - - - - - - + Log::info('给本周期内消费者分豆豆 - 开始处理: '.var_export($data,1)); + // 获取周期信息 + $cycleLegumes = app()->make(LegumesRepository::class)->getLegumes($data['legumes_id']); + if(!$cycleLegumes) throw new \Exception('信息不存在'); + if((int)$cycleLegumes['status'] != 0) throw new \Exception('当前豆豆已分配!'); + // 获取时间段所有消费用户 + $orderList = StoreOrder::whereNotIn('status',[-1,11]) + ->field('order_id,uid,(sum(pay_price) + sum(use_platform_integral_price)) as sum_money') + ->whereBetweenTime('pay_time',$cycleLegumes['start_time'],$cycleLegumes['end_time']) + ->group('order_id') + ->select() + ->toArray(); + $totalMoney = array_sum(array_column($orderList,'sum_money')); + $insertData = []; + foreach($orderList as $orderInfo){ + // 计算订单金额占比 必须进行100的偏移计算 + $rate = (float)sprintf("%.3f",$orderInfo['sum_money'] / $totalMoney * 100); + $getLegumes = sprintf("%.3f",$cycleLegumes['legumes_num'] * $rate / 100); + // 信息记录 + $insertData[] = [ + 'legumes_id' => $data['legumes_id'], + 'order_id' => $orderInfo['order_id'], + 'cycle_total_legumes' => $cycleLegumes['legumes_num'], + 'total_sales_money' => $totalMoney, + 'order_money' => $orderInfo['sum_money'], + 'order_money_rate' => $rate, + 'get_legumes' => $getLegumes, + ]; + } + // 添加数据 + if(count($insertData) > 0) app()->make(LegumesLogRepository::class)->insertAll($insertData); + app()->make(LegumesRepository::class)->update($data['legumes_id'],[ + 'status' => 1 + ]); } catch(\Exception $e){ $data['error_msg'] = $e->getMessage(); @@ -35,7 +65,7 @@ class SparateLegumesJob implements JobInterface{ } public function failed($data){ - Log::info('给昨日消费者分豆豆 - 失败(failed): '.var_export($data,1)); + Log::info('给本周期内消费者分豆豆 - 失败(failed): '.var_export($data,1)); }