You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cf-sdk/Application/Sdk/Model/PayLimitConfModel.class.php

184 lines
6.2 KiB
PHP

<?php
namespace Sdk\Model;
use Think\Model;
class PayLimitConfModel extends Model {
const PAY_TYPE_ALIPAY = "alipay";
const PAY_TYPE_WXPAY = "wxpay";
const PAY_TYPE_YEEPAY = "yeepay";
const PAY_TYPE_SQPAY = "sqpay";
protected $_validate = array(
);
/* 自动完成规则 */
protected $_auto = array(
);
/**
* 构造函数
* @param string $name 模型名称
* @param string $tablePrefix 表前缀
* @param mixed $connection 数据库连接信息
*/
public function __construct($name = '', $tablePrefix = '', $connection = '') {
/* 设置默认的表前缀 */
$this->tablePrefix ='tab_';
/* 执行构造方法 */
parent::__construct($name, $tablePrefix, $connection);
}
// 累加充值金额
public function sum($userId, $price, $payType) {
$now = date("His");
$nowDate = date('Ymd');
// 找出所有匹配的规则 时间范围满足的所有总流水限额
$limits = $this->where(array(
'start_time' => array('egt', $now),
'end_time' => array('lt', $now),
'pay_type' => $payType
))->select();
foreach ($limits as $k => $v) {
$this->updateClearTime($v, $userId); // 清零 时间、限额
if ($v['limit_type'] == 'all_total' // 总流水
|| ($v['limit_type'] == 'user_total' && $v['user_id'] == $userId) // 单玩家总流水
) {
$this->where(array(
'id' => $v['id']
))->save(array(
'now_value' => $v['now_value'] + $price,
'update_time' => time(),
));
}
if ($v['limit_type'] == 'user_one' && $v['user_id'] == $userId) { // 单玩家单笔
}
if ($v['limit_type'] == 'user_total' && $v['user_id'] == $userId) { // 单玩家总流水
}
}
}
// 检查限额 如果超出返回false
public function check($userId, $price, $payType) {
$now = date("His");
$nowDate = date('Ymd');
// 找出所有匹配的规则 时间范围满足的所有总流水限额
$limits = $this->where(array(
'start_time' => array('egt', $now),
'end_time' => array('lt', $now),
'pay_type' => $payType
))->select();
foreach ($limits as $k => $v) {
$this->updateClearTIme($v, $userId);
if ($v['limit_type'] == 'all_total') { // 总流水
// 每日 每周 每月
if ($v['limit_time_type'] == "day" || $v['limit_time_type'] == "week" || $v['limit_time_type'] == "month") {
if ($v['now_value'] + $price > $v['limit_value']) {
return false;
}
}
// 固定日期
if ($v['limit_time_type'] == 'fix') {
if ($v['fix_date'] == $nowDate) {
if ($v['now_value'] + $price > $v['limit_value']) {
return false;
}
}
}
}
if ($v['limit_type'] == 'user_one' && $v['user_id'] == $userId) { // 单玩家单笔
// 每日 每周 每月
if ($v['limit_time_type'] == "day" || $v['limit_time_type'] == "week" || $v['limit_time_type'] == "month") {
if ($price > $v['limit_value']) {
return false;
}
}
// 固定日期
if ($v['limit_time_type'] == 'fix') {
if ($v['fix_date'] == $nowDate) {
if ($price > $v['limit_value']) {
return false;
}
}
}
}
if ($v['limit_type'] == 'user_total' && $v['user_id'] == $userId) { // 单玩家总流水
// 每日 每周 每月
if ($v['limit_time_type'] == "day" || $v['limit_time_type'] == "week" || $v['limit_time_type'] == "month") {
if ($v['now_value'] + $price > $v['limit_value']) {
return false;
}
}
// 固定日期
if ($v['limit_time_type'] == 'fix') {
if ($v['fix_date'] == $nowDate) {
if ($v['now_value'] + $price > $v['limit_value']) {
return false;
}
}
}
}
}
}
// 更新清零时间
private function updateClearTIme($conf, $userId) {
if (!$conf) return;
$data = array();
$startTime = substr($conf['start_time'], 0, 2) * 3600;
$startTime += substr($conf['start_time'], 2, 2) * 60;
$startTime += substr($conf['start_time'], 4, 2);
$isClear = false;
if ($conf['limit_time_type'] == 'day') {
$startTime += strtotime(date('Ymd'));
// 清零时间小于开始时间 需要清零
if ($conf['clear_time'] < $startTime) {
$isClear = true;
}
}
if ($conf['limit_time_type'] == 'week') {
$week_now = date("w")-1;
$week_start= strtotime("-{$week_now} days",time());
$startTime += $week_start;
// 清零时间小于开始时间 需要清零
if ($conf['clear_time'] < $startTime) {
$isClear = true;
}
}
if ($conf['limit_time_type'] == 'month') {
$startTime += strtotime(date('Ym'));
// 清零时间小于开始时间 需要清零
if ($conf['clear_time'] < $startTime) {
$isClear = true;
}
}
if ($isClear) {
$this->where(array(
'id' => $conf['id']
))->save(array(
'now_value' => 0,
'clear_time' => time(),
));
}
}
}