杨总惠通宝
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/UserAssetsRepository.php

199 lines
7.2 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\UserAssetsDao;
use app\common\repositories\BaseRepository;
use app\common\repositories\store\order\StoreOrderBaseRepository;
use app\common\repositories\system\groupData\GroupDataRepository;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
/**
* @mixin UserAssetsDao
*/
class UserAssetsRepository extends BaseRepository
{
public function __construct(UserAssetsDao $dao)
{
$this->dao = $dao;
}
public function getConfig(){
// 配置信息
/**
* @var GroupDataRepository $groupDataRepository
*/
$groupDataRepository = app()->make(GroupDataRepository::class);
$config = $groupDataRepository->getGroupDataLst(0, 100, 1, 100);
return array_column($config['list'], "value", 'key');
}
/**
* notes 获取用户资产
* @param $uid
* @return array
* @create 2024/3/18 14:48
* @update 2024/3/18 14:48
* @author zhangkxiang
* @editor
*/
public function assets($uid)
{
$item = $this->dao->get($uid);
if ($item) {
return $item->toArray();
}
return array(
'uid' => 0,
'consume' => 0.00,
'consume_frozen' => 0.00,
'share_point' => 0.00,
'welfare' => 0.00,
'welfare_frozen' => 0.00,
'huitong' => 0.00,
'huitong_frozen' => 0.00,
'contribution' => 0.00,
'contribution_frozen' => 0.00,
);
}
/**
* notes 用户资产变更
* @param $uid
* @param $status
* @param $data
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @create 2024/3/15 17:23
* @update 2024/3/15 17:23
* @author zhangkxiang
* @editor
*/
public function changeEvent($uid, $status, $data = array())
{
$assets = array();
$info = $this->dao->get($uid);
$data['consume'] = $data['consume'] ?? 0;
$data['welfare'] = $data['welfare'] ?? 0;
$data['huitong'] = $data['huitong'] ?? 0;
$data['contribution'] = $data['contribution'] ?? 0;
if ($info) {
// 购买订单获得冻结资产
if ($status == UserAssetsLogRepository::STATUS_FROZEN) {
$assets = array(
'consume_frozen' => $info['consume_frozen'] + $data['consume'],
'welfare_frozen' => $info['welfare_frozen'] + $data['welfare'],
'huitong_frozen' => $info['huitong_frozen'] + $data['huitong'],
'contribution_frozen' => $info['contribution_frozen'] + $data['contribution'],
);
} elseif ($status == UserAssetsLogRepository::STATUS_SUCCESS) {
$assets = array(
'consume' => $info['consume'] + $data['consume'],
'welfare' => $info['welfare'] + $data['welfare'],
'huitong' => $info['huitong'] + $data['huitong'],
'contribution' => $info['contribution'] + $data['contribution'],
'consume_frozen' => $info['consume_frozen'] - $data['consume'],
'welfare_frozen' => $info['welfare_frozen'] - $data['welfare'],
'huitong_frozen' => $info['huitong_frozen'] - $data['huitong'],
'contribution_frozen' => $info['contribution_frozen'] - $data['contribution'],
);
} elseif ($status == UserAssetsLogRepository::STATUS_REFUND) {
$assets = array(
'consume_frozen' => $info['consume_frozen'] - $data['consume'],
'welfare_frozen' => $info['welfare_frozen'] - $data['welfare'],
'huitong_frozen' => $info['huitong_frozen'] - $data['huitong'],
'contribution_frozen' => $info['contribution_frozen'] - $data['contribution'],
);
} elseif ($status == UserAssetsLogRepository::STATUS_USED) {
$assets = array(
'welfare' => $info['welfare'] - $data['welfare'],
);
}
$this->dao->update($uid, $assets);
} else {
$assets = array(
'uid' => $uid,
'consume_frozen' => $data['consume'] ?? 0,
'welfare_frozen' => $data['welfare'] ?? 0,
'huitong_frozen' => $data['huitong'] ?? 0,
'contribution_frozen' => $data['contribution'] ?? 0,
);
$this->dao->create($assets);
}
}
/**
* notes 分红福利积分
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @create 2024/3/19 21:03
* @update 2024/3/19 21:03
* @author zhangkxiang
* @editor
*/
public function sharePoint(){
$config = $this->getConfig();
/**
* @var StoreOrderBaseRepository $orderBase
*/
$orderBase = app()->make(StoreOrderBaseRepository::class);
$count = $orderBase->getTotalBase(date('Y-m-d', strtotime('-1day')));
if(empty($count)){
return ;
}
$shareData = $this->dao->getShareData();
if(!$shareData['total']){
return ;
}
$base = round($config['share_award'] * $count / 100, 2);
if($base < 0.01){
$base = 0.01;
}
$logList = array();
foreach ($shareData['list'] as $item){
$welfare = $base / $shareData['total'] * $item['share_point'];
if($welfare < 0.01){
$welfare = 0.01;
}
$logList[] = array(
'uid' => $item['uid'],
'asset_type' => UserAssetsLogRepository::ASSET_TYPE_WELFARE,
'type' => UserAssetsLogRepository::CHANGE_TYPE_SHARE_GET,
'status' => UserAssetsLogRepository::STATUS_SUCCESS,
'count' => $welfare,
);
// 增加福利积分
$this->changeEvent($item['uid'],UserAssetsLogRepository::STATUS_SUCCESS, array('welfare' => $welfare));
}
// 增加记录
$logRepository = app()->make(UserAssetsLogRepository::class);
$logRepository->addLog($logList);
}
}