205 lines
7.2 KiB
PHP
205 lines
7.2 KiB
PHP
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\common\repositories\user;
|
|
|
|
use app\common\dao\user\IntegralDao;
|
|
use app\common\model\store\order\StoreOrder;
|
|
use app\common\repositories\BaseRepository;
|
|
use app\common\repositories\system\merchant\MerchantRepository;
|
|
use think\facade\Log;
|
|
|
|
/**
|
|
* Common: ...
|
|
* Author: wu-hui
|
|
* Time: 2023/11/07 10:34
|
|
* Class IntegralRepository
|
|
* @package app\common\repositories\user
|
|
*/
|
|
class IntegralRepository extends BaseRepository
|
|
{
|
|
/**
|
|
* @var IntegralDao
|
|
*/
|
|
protected $dao;
|
|
|
|
/**
|
|
* IntegralRepository constructor.
|
|
* @param IntegralDao $dao
|
|
*/
|
|
public function __construct(IntegralDao $dao)
|
|
{
|
|
$this->dao = $dao;
|
|
}
|
|
/**
|
|
* Common: 积分变更
|
|
* Author: wu-hui
|
|
* Time: 2023/11/07 10:45
|
|
* @param int $uid
|
|
* @param int $mer_id
|
|
* @param float $changeNumber
|
|
*/
|
|
public function changeIntegral(int $uid, int $mer_id, float $changeNumber){
|
|
$info = $this->dao->findOrCreate([
|
|
'uid' => $uid,
|
|
'mer_id' => $mer_id
|
|
]);
|
|
$info->number = (float)sprintf("%.2f",$info->number + $changeNumber);
|
|
$info->save();
|
|
}
|
|
/**
|
|
* Common: 获取当前商户积分、平台积分及平台转换后的积分数量
|
|
* Author: wu-hui
|
|
* Time: 2023/11/07 18:01
|
|
* @param int $uid
|
|
* @param int $mer_id
|
|
* @return float[]
|
|
*/
|
|
public function getUseIntegral(int $uid,int $mer_id):array{
|
|
$convert_rate = (float)app()->make(MerchantRepository::class)->getSearch(['mer_id' => $mer_id])->value('mer_integral_merchant_rate');
|
|
return [
|
|
'mer_integral' => (float)$this->getMerIntegral($uid,$mer_id),// 当前商户积分
|
|
'convert_rate' => (float)$convert_rate,// 平台积分转商户积分转换比例
|
|
];
|
|
}
|
|
/**
|
|
* Common: 获取指定商户积分
|
|
* Author: wu-hui
|
|
* Time: 2023/11/07 17:38
|
|
* @param int $uid
|
|
* @param int $mer_id
|
|
* @return float
|
|
*/
|
|
public function getMerIntegral(int $uid,int $mer_id):float{
|
|
$integralInfo = $this->dao->findOrCreate([
|
|
'uid' => $uid,
|
|
'mer_id' => $mer_id
|
|
]);
|
|
|
|
return (float)$integralInfo->number;
|
|
}
|
|
/**
|
|
* Common: 获取平台积分
|
|
* Author: wu-hui
|
|
* Time: 2023/11/07 18:01
|
|
* @param int $uid
|
|
* @param int $mer_id
|
|
* @return array
|
|
*/
|
|
public function getIntegral(int $uid,int $mer_id = 0):array{
|
|
// 获取平台积分
|
|
$integral = $this->getMerIntegral($uid,(int)0);
|
|
// 是否转换为某个商户的商户积分
|
|
$convert_integral = 0;
|
|
$convert_rate = 0;
|
|
if($mer_id > 0 && $integral > 0){
|
|
$convert_rate = (float)app()->make(MerchantRepository::class)->getSearch(['mer_id' => $mer_id])->value('mer_integral_merchant_rate');
|
|
if($convert_rate > 0) $convert_integral = (float)sprintf("%.2f",$integral / $convert_rate);
|
|
}
|
|
|
|
return compact('integral','convert_integral','convert_rate');
|
|
}
|
|
/**
|
|
* Common: 获取商户积分列表
|
|
* Author: wu-hui
|
|
* Time: 2023/11/10 14:20
|
|
* @param int $uid
|
|
* @param int $page
|
|
* @param int $limit
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function getMerIntegralList(int $uid,int $page,int $limit):array{
|
|
$query = $this->dao->getSearch(['uid'=>$uid])
|
|
->where('mer_id','>',0)
|
|
->with(['mer'=>function($query){
|
|
$query->field('mer_id,mer_name,mer_avatar,mer_integral_platform_rate')->bind(['mer_name','mer_avatar','mer_integral_platform_rate']);
|
|
}])
|
|
->order('create_time DESC');
|
|
$count = $query->count();
|
|
$list = $query->page($page,$limit)->select();
|
|
|
|
return compact('count','list');
|
|
}
|
|
/**
|
|
* Common: 获取全部持有积分信息
|
|
* Author: wu-hui
|
|
* Time: 2023/11/10 17:02
|
|
* @param int $uid
|
|
* @param int $page
|
|
* @param int $limit
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function getAllIntegralList(array $params,int $page,int $limit):array{
|
|
$query = $this->dao->getSearch([])
|
|
->when((int)$params['uid'] > 0,function($query) use ($params){
|
|
$query->where('uid', (int)$params['uid']);
|
|
})
|
|
->when((int)$params['mer_id'] > 0,function($query) use ($params){
|
|
$query->where('mer_id', (int)$params['mer_id']);
|
|
})
|
|
->with([
|
|
'mer' => function($query){
|
|
$query->field('mer_id,mer_name,mer_avatar')->bind(['mer_name','mer_avatar']);
|
|
},
|
|
'user' => function($query){
|
|
$query->field('uid,nickname,avatar')->bind(['nickname','avatar']);
|
|
}
|
|
])
|
|
->order('create_time DESC');
|
|
$count = $query->count();
|
|
$list = $query->page($page,$limit)->select();
|
|
|
|
return compact('count','list');
|
|
}
|
|
/**
|
|
* Common: 持有积分统计
|
|
* Author: wu-hui
|
|
* Time: 2023/11/18 14:06
|
|
* @param array $params
|
|
* @return array|array[]
|
|
*/
|
|
public function getStat(array $params){
|
|
$where = [];
|
|
if((int)$params['uid'] > 0) $where['uid'] = (int)$params['uid'];
|
|
if((int)$params['mer_id'] > 0) $where['mer_id'] = (int)$params['mer_id'];
|
|
|
|
$orderQuery = app()->make(StoreOrder::class)
|
|
->when((int)$params['uid'] > 0,function($query) use ($params){
|
|
$query->where('uid', (int)$params['uid']);
|
|
})
|
|
->when((int)$params['mer_id'] > 0,function($query) use ($params){
|
|
$query->where('mer_id', (int)$params['mer_id']);
|
|
});
|
|
|
|
// 商户信息统计
|
|
$holdMerIntegral = (float)$this->dao->getSearch([])->where($where)->where('mer_id','>',0)->sum('number');
|
|
$useMerIntegral = (float)$orderQuery->sum('use_mer_integral');
|
|
if((int)$params['mer_id'] > 0){
|
|
return [
|
|
['className' => 'el-icon-coin','count' => $holdMerIntegral,'field' => '分','name' => '持有本商户积分(总)'],
|
|
['className' => 'el-icon-coin','count' => $useMerIntegral,'field' => '分','name' => '已使用本商户积分(总)'],
|
|
];
|
|
}
|
|
// 平台信息统计
|
|
$holdPlatformIntegral = (float)$this->dao->getSearch([])->where($where)->where('mer_id','=',0)->sum('number');
|
|
$usePlatformIntegral = (float)$orderQuery->sum('use_platform_integral');
|
|
|
|
return [
|
|
['className' => 'el-icon-coin','count' => $holdPlatformIntegral,'field' => '分','name' => '持有平台积分(总)'],
|
|
['className' => 'el-icon-coin','count' => $usePlatformIntegral,'field' => '分','name' => '已使用平台积分(总)'],
|
|
['className' => 'el-icon-coin','count' => $holdMerIntegral,'field' => '分','name' => '持有商户积分(总)'],
|
|
['className' => 'el-icon-coin','count' => $useMerIntegral,'field' => '分','name' => '已使用商户积分(总)'],
|
|
];
|
|
}
|
|
|
|
|
|
}
|