杨总惠通宝
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
htb_backend/app/common/repositories/user/UserAssetsLogRepository.php

736 lines
28 KiB

<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\common\repositories\user;
use app\common\dao\user\UserAssetsLogDao;
use app\common\repositories\BaseRepository;
use app\common\repositories\store\order\StoreOrderBaseRepository;
use app\common\repositories\store\product\ProductAssistRepository;
use app\common\repositories\store\product\ProductRepository;
use app\common\repositories\system\groupData\GroupDataRepository;
use app\common\repositories\system\merchant\MerchantCategoryRepository;
use app\common\repositories\system\merchant\MerchantRepository;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Log;
/**
* @mixin UserAssetsLogDao
*/
class UserAssetsLogRepository extends BaseRepository
{
const ASSET_TYPE_CONSUME = 1; // 消费积分
const ASSET_TYPE_SHARE_POINT = 2; // 分红点
const ASSET_TYPE_WELFARE = 3; // 福利积分
const ASSET_TYPE_HUITONG = 4; // 惠通宝
const ASSET_TYPE_CONTRIBUTION = 5; // 贡献值
const CHANGE_TYPE_ORDER = 1; // 个人下单
const CHANGE_TYPE_SIGN = 2; // 签到
const CHANGE_TYPE_SHARE = 3; // 消费积分兑换分红点
const CHANGE_TYPE_SHARE_GET = 4; // 分红点返佣
const CHANGE_TYPE_SPREAD_GET = 5; // 推广返佣
const CHANGE_TYPE_CULTIVATE = 6; // 培育奖
const CHANGE_TYPE_AGENT = 7; // 区域代理奖
const CHANGE_TYPE_HUITONG = 8; // 惠通宝兑换消费积分
const CHANGE_TYPE_HUITONG_TO = 9; // 转让他人
const CHANGE_TYPE_HUITONG_GET = 10; // 他人转让
const STATUS_FROZEN = 0; // 冻结
const STATUS_SUCCESS = 1; // 成功
const STATUS_REFUND = 2; // 退款
const STATUS_USED = 3; // 使用积分
public function __construct(UserAssetsLogDao $dao, UserAssetsRepository $userAssetsRepository, ProductRepository $productRepository, GroupDataRepository $groupRepository)
{
$this->dao = $dao;
$this->userAssetsRepository = $userAssetsRepository;
$this->productRepository = $productRepository;
$this->groupDataRepository = $groupRepository;
}
/**
* notes 获取资产变动类型
* @return string[]
* @create 2024/3/15 15:30
* @update 2024/3/15 15:30
* @author zhangkxiang
* @editor
*/
public function getChangeType()
{
return array(
self::CHANGE_TYPE_ORDER => '个人下单',
self::CHANGE_TYPE_SIGN => '签到',
self::CHANGE_TYPE_SHARE => '消费积分兑换分红点',
self::CHANGE_TYPE_SHARE_GET => '分红点返佣',
self::CHANGE_TYPE_SPREAD_GET => '推广返佣',
self::CHANGE_TYPE_CULTIVATE => '培育奖',
self::CHANGE_TYPE_AGENT => '区域代理奖',
self::CHANGE_TYPE_HUITONG => '惠通宝兑换消费积分',
self::CHANGE_TYPE_HUITONG_TO => '转让他人',
self::CHANGE_TYPE_HUITONG_GET => '他人转让',
);
}
public function getList(array $where, $page, $limit)
{
$query = $this->dao->search($where);
$count = $query->count();
$list = $query->page($page, $limit)->with(['spread' => function ($query) {
$query->field('uid,nickname,avatar');
}])->select();
return compact('count', 'list');
}
/**
* notes 用户支付订单时间处理
* @param $groupOrder
* @create 2024/3/15 17:22
* @update 2024/3/15 17:22
* @author zhangkxiang
* @editor
*
* {"name":"福利积分--项目经理", "value":10, "key":"welfare_project"}
* {"name":"福利积分--一星经理", "value":30, "key":"welfare_project_1"}
* {"name":"福利积分--二星经理", "value":50, "key":"welfare_project_2"}
* {"name":"福利积分--三星经理", "value":80, "key":"welfare_project_3"}
* {"name":"贡献值--用户", "value":1, "key":"contribution_user"}
* {"name":"贡献值--项目经理", "value":1, "key":"contribution_project"}
* {"name":"贡献值--一星经理", "value":2, "key":"contribution_project_1"}
* {"name":"贡献值--二星经理", "value":3, "key":"contribution_project_2"}
* {"name":"贡献值--三星经理", "value":4, "key":"contribution_project_3"}
* {"name":"惠通宝--用户", "value":5, "key":"huitong_user"}
* {"name":"惠通宝--项目经理", "value":10, "key":"huitong_project"}
* {"name":"惠通宝--一星经理", "value":30, "key":"huitong_project_1"}
* {"name":"惠通宝--二星经理", "value":50, "key":"huitong_project_2"}
* {"name":"惠通宝--三星经理", "value":80, "key":"huitong_project_3"}
* {"name":"福利积分--区域代理--区域代理奖", "value":1, "key":"welfare_agent"}
* {"name":"福利积分--区域代理--推荐商家奖", "value":1, "key":"welfare_agent_recommend"}
* {"name":"福利积分--项目经理--推荐商家奖", "value":1, "key":"welfare_project_recommend"}
* {"name":"福利积分--商户--推荐商家奖", "value":1, "key":"welfare_merchant_recommend"}
* {"name":"惠通宝增值幅度比例配置", "value":10, "key":"huitong_add"}
* {"name":"福利积分--存在分红点用户奖励", "value":10, "key":"share_award"}
*/
public function userPayEvent($groupOrder)
{
// 配置信息
$config = $this->userAssetsRepository->getConfig();
$baseList = array();
foreach ($groupOrder['orderList'] as $orderItem) {
$base = 0;
foreach ($orderItem['orderProduct'] as $orderProduct) {
// 获取商品详情(奖励积分
$product = $this->productRepository->detail($orderProduct['product_id'], null);
Log::info("product" . json_encode($product));
$rate = floatval($orderProduct['total_price']) / floatval($orderProduct['product_price']) * 100;
if ($rate >= $product['cash_rate']) {
$base += $product['base'];
}
}
// 1. 用户资产
$this->userAssets($orderItem, $config, $base);
if ($base) {
$baseList[] = array('order_id' => $orderItem['order_id'], 'base' => $base);
// 2. 项目经理资产
$startProjectUid = $this->projectAssets($orderItem, $config, $base);
// 3. 培育经理资产
$this->breedAssets($startProjectUid, $orderItem['order_id'], $config, $base);
// 4.区域代理资产
$this->agentAssets($orderItem['district_id'], $orderItem['order_id'], $config, $base);
// 5. 推荐资产
$this->recommendAssets($orderItem['mer_id'], $orderItem['order_id'], $config, $base);
}
// 6. 商户和平台资产
$this->merchantAndPlatformAssets($orderItem);
}
if ($baseList) {
app(StoreOrderBaseRepository::class)->insertBase($baseList);
}
}
public function merchantAndPlatformAssets($orderItem)
{
/**
* @var UserRepository $userRepository
*/
$userRepository = app(UserRepository::class);
$user = $userRepository->searchOne(['mer_id' => $orderItem['mer_id']]);
if ($user) {
$logList = array();
$welfare = $consume = $huitong = $contribution = 0;
$consume = round($orderItem['total_price'] * $orderItem['commission_rate'] / 100, 2);
$consume = $this->_getValue($consume);
$logList[] = array(
'uid' => $user['uid'],
'asset_type' => self::ASSET_TYPE_CONSUME,
'type' => self::CHANGE_TYPE_ORDER,
'status' => self::STATUS_FROZEN,
'order_id' => $orderItem['order_id'],
'count' => $consume,
'mer_id' => $orderItem['mer_id'],
);
if ($orderItem['integral']) {
$welfare = round($orderItem['integral'] * (100 - $orderItem['commission_rate']) / 100, 2);
$welfare = $this->_getValue($welfare);
$logList[] = array(
'uid' => $user['uid'],
'asset_type' => self::ASSET_TYPE_WELFARE,
'type' => self::CHANGE_TYPE_ORDER,
'status' => self::STATUS_FROZEN,
'order_id' => $orderItem['order_id'],
'count' => $welfare,
'mer_id' => $orderItem['mer_id'],
);
}
$this->addLog($logList);
$this->userAssetsRepository->changeEvent($user['uid'], self::STATUS_FROZEN, compact('consume', 'welfare', 'huitong', 'contribution'));
}
if ($orderItem['integral']) {
$welfare = round($orderItem['integral'] * $orderItem['commission_rate'] / 100, 2);
$welfare = $this->_getValue($welfare);
$log = array(
'uid' => 0,
'asset_type' => self::ASSET_TYPE_WELFARE,
'type' => self::CHANGE_TYPE_ORDER,
'status' => self::STATUS_FROZEN,
'order_id' => $orderItem['order_id'],
'count' => $welfare,
'mer_id' => 0,
);
$this->addLog([$log]);
}
}
/**
* notes 推荐资产
* @param $merId
* @param $orderId
* @param $config
* @param $base
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @create 2024/3/17 23:08
* @update 2024/3/17 23:08
* @author zhangkxiang
* @editor
*/
public function recommendAssets($merId, $orderId, $config, $base)
{
$consume = $welfare = $huitong = $contribution = 0;
/**
* @var MerchantRepository $merRepository
*/
$merRepository = app(MerchantRepository::class);
$merchant = $merRepository->get($merId);
if (empty($merchant)) {
return;
}
if ($merchant['spread_uid'] == 0) {
return;
}
/**
* @var UserRepository $userRepository
*/
$userRepository = app(UserRepository::class);
$user = $userRepository->get($merchant['spread_uid']);
if (empty($user)) {
return;
}
$log = array(
'uid' => $user['uid'],
'asset_type' => self::ASSET_TYPE_WELFARE,
'type' => self::CHANGE_TYPE_SPREAD_GET,
'status' => self::STATUS_FROZEN,
'order_id' => $orderId,
);
if ($user['agent_district_id']) {
// 区域代理
$orderWelfare = round($base * $config['welfare_agent_recommend'] / 100, 2);
$welfare = $this->_getValue($orderWelfare);
$ext = array('district_id' => $user['agent_district_id'], 'name' => "区域代理推荐商家");
} elseif ($user['group_id']) {
// 项目经理
$orderWelfare = round($base * $config['welfare_project_recommend'] / 100, 2);
$welfare = $this->_getValue($orderWelfare);
$ext = array('group_id' => $user['group_id'], 'name' => "项目经理推荐商家");
} elseif ($user['mer_id']) {
// 商家
$orderWelfare = round($base * $config['welfare_merchant_recommend'] / 100, 2);
$welfare = $this->_getValue($orderWelfare);
$ext = array('mer_id' => $user['mer_id'], 'name' => "商家推荐商家");
} else {
return;
}
$log['count'] = $welfare;
$log['ext'] = $ext;
$this->addLog([$log]);
$this->userAssetsRepository->changeEvent($user['uid'], self::STATUS_FROZEN, compact('consume', 'welfare', 'huitong', 'contribution'));
}
/**
* notes 区域推荐奖励
* @param $districtId
* @param $orderId
* @param $config
* @param $base
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @create 2024/3/17 22:49
* @update 2024/3/17 22:49
* @author zhangkxiang
* @editor
*/
public function agentAssets($districtId, $orderId, $config, $base)
{
$consume = $welfare = $huitong = $contribution = 0;
/**
* @var UserRepository $userRepository
*/
$userRepository = app(UserRepository::class);
$user = $userRepository->getWhere(array('agent_district_id' => $districtId));
if (empty($user)) {
return;
}
// 1. 项目经理的福利积分
$orderWelfare = round($base * $config['welfare_agent'] / 100, 2);
$welfare = $this->_getValue($orderWelfare);
$logList[] = array(
'uid' => $user['uid'],
'asset_type' => self::ASSET_TYPE_WELFARE,
'type' => self::CHANGE_TYPE_AGENT,
'status' => self::STATUS_FROZEN,
'order_id' => $orderId,
'count' => $welfare,
'ext' => array('district_id' => $districtId, 'name' => "区域代理"),
);
$this->addLog($logList);
$this->userAssetsRepository->changeEvent($user['uid'], self::STATUS_FROZEN, compact('consume', 'welfare', 'huitong', 'contribution'));
}
/**
* notes
* @param $startProjectUid
* @param $orderId
* @param $config
* @param $base
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @create 2024/3/17 19:32
* @update 2024/3/17 19:32
* @author zhangkxiang
* @editor
*/
public function breedAssets($startProjectUid, $orderId, $config, $base)
{
$consume = 0;
$userRepository = app(UserRepository::class);
$userProject = $userRepository->get($startProjectUid);
if (empty($userProject)) {
return;
}
if ($userProject['group_id'] > 4 or $userProject['group_id'] <= 1) {
return;
}
$group = $userProject['group_id'] - 1;
// 1. 项目经理的福利积分
$orderWelfare = round($base * $config['welfare_project'] * $config['welfare_project_' . $group] / 10000, 2);
$welfare = $this->_getValue($orderWelfare);
$logList[] = array(
'uid' => $userProject['uid'],
'asset_type' => self::ASSET_TYPE_WELFARE,
'type' => self::CHANGE_TYPE_CULTIVATE,
'status' => self::STATUS_FROZEN,
'order_id' => $orderId,
'count' => $welfare,
);
// 2. 项目经理的惠通宝
$orderHuitong = round($base * $config['huitong_project'] * $config['huitong_project_' . $group] / 10000, 2);
$huitong = $this->_getValue($orderHuitong);
$logList[] = array(
'uid' => $userProject['uid'],
'asset_type' => self::ASSET_TYPE_HUITONG,
'type' => self::CHANGE_TYPE_CULTIVATE,
'status' => self::STATUS_FROZEN,
'order_id' => $orderId,
'count' => $huitong,
);
// 3. 项目经理的贡献值
$orderContribution = round($base * $config['contribution_project'] * $config['contribution_project_' . $group] / 10000, 2);
$contribution = $this->_getValue($orderContribution);
$logList[] = array(
'uid' => $userProject['uid'],
'asset_type' => self::ASSET_TYPE_CONTRIBUTION,
'type' => self::CHANGE_TYPE_CULTIVATE,
'status' => self::STATUS_FROZEN,
'order_id' => $orderId,
'count' => $contribution,
);
$this->addLog($logList);
$this->userAssetsRepository->changeEvent($userProject['uid'], self::STATUS_FROZEN, compact('consume', 'welfare', 'huitong', 'contribution'));
}
/**
* notes
* @param $orderItem
* @param $config
* @param $base
* @create 2024/3/17 19:32
* @update 2024/3/17 19:32
* @author zhangkxiang
* @editor
*/
public function projectAssets($orderItem, $config, $base)
{
$consume = 0;
$uid = $orderItem['uid'];
$orderId = $orderItem['order_id'];
$userProject = null;
$userRepository = app(UserRepository::class);
$user = $userRepository->get($uid);
if (empty($user)) {
return 0;
}
// 查找第一个项目经理
while (!$userProject) {
$user = $userRepository->get($user['spread_uid']);
if (empty($user)) {
return 0;
}
if ($user['group_id']) {
$userProject = $user;
}
}
if (empty($userProject)) {
return 0;
}
$logList = array();
// 1. 项目经理的福利积分
$orderWelfare = round($base * $config['welfare_project'] / 100, 2);
$welfare = $this->_getValue($orderWelfare);
$logList[] = array(
'uid' => $userProject['uid'],
'asset_type' => self::ASSET_TYPE_WELFARE,
'type' => self::CHANGE_TYPE_SPREAD_GET,
'status' => self::STATUS_FROZEN,
'order_id' => $orderId,
'count' => $welfare,
);
// 2. 项目经理的惠通宝
$orderHuitong = round($base * $config['huitong_project'] / 100, 2);
$huitong = $this->_getValue($orderHuitong);
$logList[] = array(
'uid' => $userProject['uid'],
'asset_type' => self::ASSET_TYPE_HUITONG,
'type' => self::CHANGE_TYPE_SPREAD_GET,
'status' => self::STATUS_FROZEN,
'order_id' => $orderId,
'count' => $huitong,
);
// 3. 项目经理的贡献值
$orderContribution = round($base * $config['contribution_project'] / 100, 2);
$contribution = $this->_getValue($orderContribution);
$logList[] = array(
'uid' => $userProject['uid'],
'asset_type' => self::ASSET_TYPE_CONTRIBUTION,
'type' => self::CHANGE_TYPE_SPREAD_GET,
'status' => self::STATUS_FROZEN,
'order_id' => $orderId,
'count' => $contribution,
);
$this->addLog($logList);
$this->userAssetsRepository->changeEvent($userProject['uid'], self::STATUS_FROZEN, compact('consume', 'welfare', 'huitong', 'contribution'));
return $userProject['spread_uid'];
}
/**
* notes 用户资产变动
* @param $orderItem
* @param $config
* @param $base
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @create 2024/3/17 17:06
* @update 2024/3/17 17:06
* @author zhangkxiang
* @editor
*/
public function userAssets($orderItem, $config, $base)
{
$logList = array();
$welfare = $huitong = $contribution = 0;
// 1. 本人的消费积分
$logList[] = array(
'uid' => $orderItem['uid'],
'asset_type' => self::ASSET_TYPE_CONSUME,
'type' => self::CHANGE_TYPE_ORDER,
'status' => self::STATUS_FROZEN,
'order_id' => $orderItem['order_id'],
'count' => $orderItem['pay_price'],
);
$consume = $orderItem['pay_price'];
// 如果该订单奖励基数大于0
if ($base) {
// 2. 本人的福利积分
$orderWelfare = round($base * 0.1, 2);
$welfare = $this->_getValue($orderWelfare);
$logList[] = array(
'uid' => $orderItem['uid'],
'asset_type' => self::ASSET_TYPE_WELFARE,
'type' => self::CHANGE_TYPE_ORDER,
'status' => self::STATUS_FROZEN,
'order_id' => $orderItem['order_id'],
'count' => $welfare,
);
// 3. 本人的惠通宝
$orderHuitong = round($base * $config['huitong_user'] / 100, 2);
$huitong = $this->_getValue($orderHuitong);
$logList[] = array(
'uid' => $orderItem['uid'],
'asset_type' => self::ASSET_TYPE_HUITONG,
'type' => self::CHANGE_TYPE_ORDER,
'status' => self::STATUS_FROZEN,
'order_id' => $orderItem['order_id'],
'count' => $huitong,
);
// 4. 本人的贡献值
$orderContribution = round($base * $config['contribution_user'] / 100, 2);
$contribution = $this->_getValue($orderContribution);
$logList[] = array(
'uid' => $orderItem['uid'],
'asset_type' => self::ASSET_TYPE_CONTRIBUTION,
'type' => self::CHANGE_TYPE_ORDER,
'status' => self::STATUS_FROZEN,
'order_id' => $orderItem['order_id'],
'count' => $contribution,
);
}
$this->addLog($logList);
$this->userAssetsRepository->changeEvent($orderItem['uid'], self::STATUS_FROZEN, compact('consume', 'welfare', 'huitong', 'contribution'));
// 5. 本人使用的福利积分
if ($orderItem['integral']) {
$consume = $welfare = $huitong = $contribution = 0;
$log = array('uid' => $orderItem['uid'],
'asset_type' => self::ASSET_TYPE_WELFARE,
'type' => self::CHANGE_TYPE_ORDER,
'status' => self::STATUS_USED,
'order_id' => $orderItem['order_id'],
'count' => $orderItem['integral'],
);
$this->addLog([$log]);
$this->userAssetsRepository->changeEvent($orderItem['uid'], self::STATUS_USED, compact('consume', 'welfare', 'huitong', 'contribution'));
}
}
/**
* notes 订单收货事件处理
* @param $orderItem
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @create 2024/3/19 15:03
* @update 2024/3/19 15:03
* @author zhangkxiang
* @editor
*/
public function userTakeEvent($orderItem)
{
// 订单发送冻结资产发放
$refundList = $this->_getOrderData($orderItem['order_id'], self::STATUS_FROZEN);
if ($refundList) {
foreach ($refundList as $refund) {
$this->userAssetsRepository->changeEvent($refund['uid'], self::STATUS_SUCCESS, $refund);
}
}
// 更新该订单的所有数据为退款状态
$this->dao->updateOrderStatus($orderItem['order_id'], self::STATUS_SUCCESS);
}
/**
* notes 订单退款事件处理
* @param $orderItem
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @create 2024/3/19 11:01
* @update 2024/3/19 11:01
* @author zhangkxiang
* @editor
*/
public function userRefundEvent($orderItem)
{
// 订单发送冻结资产收回
$refundList = $this->_getOrderData($orderItem['order_id'], self::STATUS_FROZEN);
if ($refundList) {
foreach ($refundList as $refund) {
$this->userAssetsRepository->changeEvent($refund['uid'], self::STATUS_REFUND, $refund);
}
}
// 返回用户支付的福利积分
$usedList = $this->_getOrderData($orderItem['order_id'], self::STATUS_USED);
if ($usedList) {
foreach ($usedList as $used) {
if ($used['welfare'] > 0) {
$info = $this->userAssetsRepository->get($used['uid']);
$this->userAssetsRepository->update($used['uid'], array('welfare' => $info['welfare'] + $used['welfare']));
}
}
}
// 更新该订单的所有数据为退款状态
$this->dao->updateOrderStatus($orderItem['order_id'], self::STATUS_REFUND);
}
private function _getOrderData($orderId, $status)
{
$data = $this->dao->orderData($orderId, ['status' => $status]);
// 订单发送冻结资产收回
if (!$data->isEmpty()) {
$list = array();
foreach ($data as $item) {
if (!isset($list[$item['uid']])) {
$list[$item['uid']] = array(
'uid' => $item['uid'],
'consume' => 0,
'welfare' => 0,
'huitong' => 0,
'contribution' => 0,
);
}
switch ($item['asset_type']) {
case self::ASSET_TYPE_CONSUME:
$list[$item['uid']]['consume'] += $item['count'];
break;
case self::ASSET_TYPE_WELFARE:
$list[$item['uid']]['welfare'] += $item['count'];
break;
case self::ASSET_TYPE_HUITONG:
$list[$item['uid']]['huitong'] += $item['count'];
break;
case self::ASSET_TYPE_CONTRIBUTION:
$list[$item['uid']]['contribution'] += $item['count'];
break;
}
}
return $list;
}
return array();
}
private function _getValue($value)
{
if ($value < 0.01) {
return 0.01;
}
return $value;
}
/**
* notes 插入变动日志,如果小于0.01则插入0.01
* @param $list
* @create 2024/3/17 22:09
* @update 2024/3/17 22:09
* @author zhangkxiang
* @editor
*/
public function addLog($list)
{
foreach ($list as $key => $item) {
$list[$key]['ext'] = $item['ext'] ?? array();
}
$this->dao->insertAll($list);
}
/**
* notes 获取资产变动日志
* @param $uid
* @param $where
* @param $page
* @param $limit
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @create 2024/3/18 17:37
* @update 2024/3/18 17:37
* @author zhangkxiang
* @editor
*/
public function list($uid, $where, $page, $limit)
{
$query = $this->dao->search($uid, $where);
$count = $query->count();
$data = $query->page($page, $limit)->order('id desc')->select();
$list = array();
$changeType = $this->getChangeType();
foreach ($data as $item) {
$item['type'] = $changeType[$item['type']];
$list[] = $item;
}
return compact('count', 'list');
}
}