273 lines
14 KiB
PHP
Executable File
273 lines
14 KiB
PHP
Executable File
<?php
|
||
namespace wstmart\common\model;
|
||
use think\Db;
|
||
/**
|
||
* ============================================================================
|
||
* 券数据
|
||
*/
|
||
class UserVouchers extends Base{
|
||
public function startGiveVouchers(){
|
||
set_time_limit(0);
|
||
$sm = Model('common/SysSummary');
|
||
$sysData= $sm->getInfo('toPayFast,toPaySlow');
|
||
//获取代快付值
|
||
$toPayFast = $sysData['toPayFast'];
|
||
//今日代快付释放值
|
||
$payFast = round($toPayFast * (dataConf('fastPayDayScale')*0.01),2);
|
||
//获取代慢付值
|
||
$toPaySlow = $sysData['toPaySlow'];
|
||
//今日付慢付释放值
|
||
$paySlow = round($toPaySlow * (dataConf('slowPayDayScale')*0.01),2);
|
||
//应释放总额
|
||
$toPaySum = $payFast + $paySlow;
|
||
Db::startTrans();
|
||
try{
|
||
$m = Model('common/Table');
|
||
$m->setTable('user_vouchers_summary');
|
||
if(1 == date('j')){
|
||
//每月损耗
|
||
$monthScale = round((100-dataConf('couponsNextMonthInitSacle'))*0.01,2);
|
||
$giveList = $m->getList([],'id, userId, expectedProductNum, expectedCouponsNum');
|
||
foreach ($giveList as &$g) {
|
||
if($g['expectedProductNum'] > 0){
|
||
$decNum = $g['expectedProductNum'] * $monthScale;
|
||
$this->insertVouchersNotice($g['userId'],0,$decNum,0,$desc='预获产品券月耗损',0,1);
|
||
}
|
||
if($g['expectedCouponsNum'] > 0){
|
||
$decNum = $g['expectedCouponsNum'] * $monthScale;
|
||
$this->insertVouchersNotice($g['userId'],0,0,$decNum,$desc='预获优惠券月耗损',0,1);
|
||
}
|
||
}
|
||
}
|
||
$where['isDisabled'] = 0;
|
||
//总预获产品券
|
||
$allPreProduct = $m->getSum($where,'expectedProductNum');
|
||
//总预获优惠券
|
||
$allPreCoupons = $m->getSum($where,'expectedCouponsNum');
|
||
//最大释放总额 预代付值 * 比例
|
||
$maxPayNum = round(($allPreProduct + $allPreCoupons) * (dataConf('couponsMaxGiveSacle')*0.01),2);
|
||
if($toPaySum > $maxPayNum){
|
||
//加入代慢付值
|
||
$addPaySlow = round($toPaySum - $maxPayNum,5);
|
||
$sm->addSysSummary(0,$addPaySlow);
|
||
}
|
||
//扣减系统释放值
|
||
$sm->addSysSummary($payFast,$paySlow,2);
|
||
//取最小值作为总释放额
|
||
$payNum = min($maxPayNum,$toPaySum);
|
||
if($payNum==0) return;
|
||
//每人应释放数
|
||
$personNum = $payNum / ($allPreProduct + $allPreCoupons);
|
||
//将所有未释放的的isGive设为0
|
||
//获取今日0点时间戳
|
||
$today = strtotime(date("Y-m-d"),time());
|
||
$m->updateInfo('giveDate < '.$today,['isGive'=>0]);
|
||
$list = $m->getList(['isDisabled'=>0,'isGive'=>0],'
|
||
id,
|
||
userId,
|
||
expectedProductNum,
|
||
expectedCouponsNum,
|
||
alreadyProductNum,
|
||
alreadyCouponsNum,
|
||
giveDay
|
||
');
|
||
$time = time();
|
||
foreach ($list as &$v) {
|
||
unset($data);
|
||
$minNumer = 15;
|
||
//都小于15不统计,直接加入代慢付
|
||
if($v['expectedProductNum'] <= $minNumer && $v['expectedCouponsNum'] <= $minNumer){
|
||
$toSlowNum = round($personNum * ($v['expectedProductNum'] + $v['expectedCouponsNum']),2);
|
||
$sm->addSysSummary(0,$toSlowNum);
|
||
continue;
|
||
}
|
||
if($v['expectedProductNum'] < $minNumer){//预获产品券小于15;
|
||
$personAlreadyProductNum = 0;
|
||
$personAlreadyCouponsNum = round($personNum * ($v['expectedProductNum'] + $v['expectedCouponsNum']),2);
|
||
}else if($v['expectedCouponsNum'] < $minNumer){//预获优惠券小于15
|
||
$personAlreadyCouponsNum = 0;
|
||
$personAlreadyProductNum = round($personNum * ($v['expectedProductNum'] + $v['expectedCouponsNum']),2);
|
||
}else{//都大于15
|
||
$couponsScale = dataConf('hasCoupousScale');//优惠券比例
|
||
$productScale = 100 - $couponsScale;//产品券比例
|
||
$personAlreadyProductNum = $personNum * round(($v['expectedProductNum'] + $v['expectedCouponsNum']) * ($productScale*0.01),2);
|
||
$personAlreadyCouponsNum = $personNum * round(($v['expectedProductNum'] + $v['expectedCouponsNum']) * ($couponsScale*0.01),2);
|
||
}
|
||
//实际有预获产品券小于应给值
|
||
if($v['expectedProductNum'] < $personAlreadyProductNum){
|
||
$personAlreadyCouponsNum += $personAlreadyProductNum;
|
||
$personAlreadyProductNum = 0;
|
||
}
|
||
//实际有预获优惠券小于应给值
|
||
if($v['expectedCouponsNum'] < $personAlreadyCouponsNum){
|
||
$personAlreadyProductNum += $personAlreadyCouponsNum;
|
||
$personAlreadyCouponsNum = 0;
|
||
}
|
||
//都小于就算了
|
||
if($v['expectedProductNum'] < $personAlreadyProductNum || $v['expectedCouponsNum'] < $personAlreadyCouponsNum){
|
||
$personAlreadyProductNum = 0;
|
||
$personAlreadyCouponsNum = 0;
|
||
}
|
||
|
||
|
||
if($personAlreadyProductNum >= 0.01){
|
||
|
||
$this->insertVouchersNotice($v['userId'],0,$personAlreadyProductNum,0,$desc='分润扣减',0,0);//预获产品券减
|
||
//加入变动记录,最后一位//1产品券2优惠券3旺旺券4现金券
|
||
$this->insertAlreadyVouchersNotice($v['userId'],0,$personAlreadyProductNum,0,'预获产品券转换所得',1,1);
|
||
// Model('common/LogMoneys')->addMoneyLog(0,$v['userId'],0,2,'预获产品券转换所得',1,$personAlreadyProductNum,'dayPay',1);
|
||
$data['alreadyProductNum'] = $v['alreadyProductNum'] + $personAlreadyProductNum;//已获产品券加
|
||
}
|
||
if($personAlreadyCouponsNum >= 0.01){
|
||
$this->insertVouchersNotice($v['userId'],0,0,$personAlreadyCouponsNum,$desc='分润扣减',0,0);//预获优惠券减
|
||
//加入变动记录,最后一位//1产品券2优惠券3旺旺券4现金券
|
||
$this->insertAlreadyVouchersNotice($v['userId'],0,0,$personAlreadyCouponsNum,'预获优惠券转换所得',1,1);
|
||
// Model('common/LogMoneys')->addMoneyLog(0,$v['userId'],0,2,'预获优惠券转换所得',1,$personAlreadyCouponsNum,'dayPay',2);
|
||
$data['alreadyCouponsNum'] = $v['alreadyCouponsNum'] + $personAlreadyCouponsNum;//已获优惠券加
|
||
}
|
||
$data['isGive'] = 1;
|
||
$data['giveDate'] = $time;
|
||
$data['giveDay'] = $v['giveDay'] + 1;
|
||
$m->updateInfo(['id'=>$v['id']],$data);
|
||
}
|
||
Db::commit();
|
||
Db::startTrans();
|
||
// 每次所得值(费、税前)同时扣减“助购预获”值和“预获产品券”值,直至减为负数为止,负数部分按每天0.1%耗损。
|
||
// A、当“助购预获”值为负数时,每天0.1%耗损在“预获产品券”值扣减。
|
||
// B、当“预获产品券”值为负数时,每天0.1%耗损在“预获优惠券”值扣减。
|
||
// C、当“助购预获”值和“预获产品券”值同为负数时,每天0.1%耗损在“预获优惠券”值同时扣减。
|
||
$vm = Model('common/Table');
|
||
$vm->setTable('user_vouchers_summary');
|
||
$data = $vm->whereOr(["helpSaleNum"=>["lt", 0]])->whereOr(["expectedProductNum"=>["lt", 0]])->select();
|
||
foreach ($data as $_each){
|
||
if($_each["expectedProductNum"]<0){
|
||
$count = (float)$_each["expectedProductNum"];
|
||
if($_each["helpSaleNum"] < 0)$count+=(float)$_each["helpSaleNum"];
|
||
$this->insertVouchersNotice($_each["userId"], 0, 0, $count*((float)dataConf("deductionRateWhennegPreCoupons")/100),
|
||
"预获产品券为负,每天耗损预获优惠券", 0);
|
||
}else{
|
||
$count = (float)$_each["helpSaleNum"];
|
||
$this->insertVouchersNotice($_each["userId"], 0, $count*((float)dataConf("deductionRateWhennegPreCoupons")/100),
|
||
0, "助购预获为负,每天耗损预获产品券", 0);
|
||
}
|
||
}
|
||
Db::commit();
|
||
return WSTReturn("ok", 1);
|
||
}catch (\Exception $e) {
|
||
dump($e);
|
||
Db::rollback();errLog($e);
|
||
return WSTReturn($e->getMessage(),-1);
|
||
}
|
||
}
|
||
/**
|
||
* 插入已获会员券值记录
|
||
* @param [type] $userId [会员ID]
|
||
* @param [type] $orderId [订单ID]
|
||
* @param integer $expectedProductNum [预获产品券]
|
||
* @param integer $expectedCouponsNum [预获优惠券]
|
||
* @param string $desc [备注]
|
||
* @param string $isAdd [1增加0扣除]
|
||
* @return [type] [description]
|
||
*/
|
||
public function insertAlreadyVouchersNotice($userId,$orderId,$alreadyProductNum=0,$alreadyCouponsNum=0,$desc='',$isAdd=1,$isShow=1){
|
||
//插入详细表
|
||
$m = Model('common/Table');
|
||
$m->setTable('log_moneys');
|
||
if($alreadyProductNum>0){
|
||
$m->insertInfo(['targetType'=>0,'targetId'=>$userId,'dataSrc'=>1,'money'=>$alreadyProductNum,'moneyName'=>1,'remark'=>$desc,'moneyType'=>$isAdd,'payType'=>'qlgpay','createTime'=>date('Y/m/d H:i:s')]);
|
||
}
|
||
if($alreadyCouponsNum>0){
|
||
$m->insertInfo(['targetType'=>0,'targetId'=>$userId,'dataSrc'=>1,'money'=>$alreadyProductNum,'moneyName'=>2,'remark'=>$desc,'moneyType'=>$isAdd,'payType'=>'qlgpay','createTime'=>date('Y/m/d H:i:s')]);
|
||
|
||
}
|
||
//插入或更新详细表
|
||
$m->setTable('users');
|
||
$summaryInfo = $m->getInfo(['userId'=>$userId],'userId,productNum,couponsNum');
|
||
if(1 == $isAdd){
|
||
$data['productNum'] = $summaryInfo['productNum'] + $alreadyProductNum;
|
||
$data['couponsNum'] = $summaryInfo['couponsNum'] + $alreadyCouponsNum;
|
||
}else{
|
||
$data['productNum'] = $summaryInfo['productNum'] - $alreadyProductNum;
|
||
$data['couponsNum'] = $summaryInfo['couponsNum'] - $alreadyCouponsNum;
|
||
}
|
||
$m->updateInfo(['userId'=>$userId],$data);
|
||
}
|
||
|
||
/**
|
||
* 插入已获会员券值记录
|
||
* @param [type] $userId [会员ID]
|
||
* @param [type] $orderId [订单ID]
|
||
* @param integer $expectedProductNum [预获产品券]
|
||
* @param integer $expectedCouponsNum [预获优惠券]
|
||
* @param string $desc [备注]
|
||
* @param string $isAdd [1增加0扣除]
|
||
* @return [type] [description]
|
||
*/
|
||
public function insertHelpSaleNotice($userId,$orderId,$helpSaleNum=0,$desc='',$isAdd=1,$isShow=1){
|
||
//插入详细表
|
||
$m = Model('common/Table');
|
||
$m->setTable('user_help_get_notice');
|
||
$m->insertInfo(['userId'=>$userId,'orderId'=>$orderId,'helpGet'=>$helpSaleNum,'remark'=>$desc,'isAdd'=>$isAdd,'isShow'=>$isShow,'createTime'=>time()]);
|
||
|
||
|
||
|
||
$m = Model('common/Table');
|
||
$m->setTable('user_vouchers_summary');
|
||
if($summaryInfo = $m->getInfo(['userId'=>$userId],'id,helpSaleNum')){
|
||
if(1 == $isAdd){
|
||
$data['helpSaleNum'] = $summaryInfo['helpSaleNum'] + $helpSaleNum;
|
||
}else{
|
||
$data['helpSaleNum'] = $summaryInfo['helpSaleNum'] - $helpSaleNum;
|
||
}
|
||
$m->updateInfo(['id'=>$summaryInfo['id']],$data);
|
||
}else{
|
||
if(0 == $isAdd){
|
||
$helpSaleNum *= -1;
|
||
}
|
||
$data = compact('userId','helpSaleNum');
|
||
$m->insertInfo($data);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 插入会员券值记录
|
||
* @param [type] $userId [会员ID]
|
||
* @param [type] $orderId [订单ID]
|
||
* @param integer $expectedProductNum [预获产品券]
|
||
* @param integer $expectedCouponsNum [预获优惠券]
|
||
* @param string $desc [备注]
|
||
* @param string $isAdd [1增加0扣除]
|
||
* @return [type] [description]
|
||
*/
|
||
public function insertVouchersNotice($userId,$orderId,$expectedProductNum=0,$expectedCouponsNum=0,$desc='',$isAdd=1,$isShow=1){
|
||
//插入详细表
|
||
$m = Model('common/Table');
|
||
$m->setTable('user_vouchers_notice');
|
||
$m->insertInfo(['userId'=>$userId,'orderId'=>$orderId,'expectedProductNum'=>$expectedProductNum,'expectedCouponsNum'=>$expectedCouponsNum,'remark'=>$desc,'moneyType'=>$isAdd,'isShow'=>$isShow,'createTime'=>time()]);
|
||
//插入或更新详细表
|
||
$m->setTable('user_vouchers_summary');
|
||
if($summaryInfo = $m->getInfo(['userId'=>$userId],'id,expectedProductNum,expectedCouponsNum')){
|
||
if(1 == $isAdd){
|
||
$data['expectedProductNum'] = $summaryInfo['expectedProductNum'] + $expectedProductNum;
|
||
$data['expectedCouponsNum'] = $summaryInfo['expectedCouponsNum'] + $expectedCouponsNum;
|
||
}else{
|
||
$data['expectedProductNum'] = $summaryInfo['expectedProductNum'] - $expectedProductNum;
|
||
$data['expectedCouponsNum'] = $summaryInfo['expectedCouponsNum'] - $expectedCouponsNum;
|
||
}
|
||
$m->updateInfo(['id'=>$summaryInfo['id']],$data);
|
||
}else{
|
||
if(0 == $isAdd){
|
||
$expectedProductNum *= -1;
|
||
$expectedCouponsNum *= -1;
|
||
}
|
||
$data = compact('userId','expectedProductNum','expectedCouponsNum');
|
||
$data['alreadyProductNum'] = 0;
|
||
$data['alreadyCouponsNum'] = 0;
|
||
$data['isGive'] = 1;
|
||
$data['giveDate'] = time();
|
||
$data['giveDay'] = 0;
|
||
$m->insertInfo($data);
|
||
}
|
||
}
|
||
}
|