272 行
12 KiB
PHP
可执行文件
272 行
12 KiB
PHP
可执行文件
<?php
|
||
|
||
namespace Common\Model;
|
||
//[1.4.70]
|
||
|
||
/**
|
||
* 充值活动列表模型
|
||
* Class RetailVipRechargeModel
|
||
* @package Common\Model
|
||
*/
|
||
class RetailVipRechargeModel extends BaseModel
|
||
{
|
||
protected $tableName = 'vip_recharge';
|
||
const ID = 'id'; //int(11) NOT NULL AUTO_INCREMENT COMMENT '主键id',
|
||
const MERCHANT_ID = 'merchant_id'; //int(11) DEFAULT '0' COMMENT '商户id',
|
||
const STORE_ID = 'store_id'; //int(11) DEFAULT '0' COMMENT '门店id',
|
||
const RECHARGE_MONEY = 'recharge_money'; //decimal(11,2) DEFAULT '0.00' COMMENT '充值金额',
|
||
const GIVE_MONEY = 'give_money'; //decimal(11,2) DEFAULT '0.00' COMMENT '赠送金额',
|
||
const STOCK = 'stock'; //int(11) DEFAULT '0' COMMENT '活动库存',
|
||
const SURPLUS = 'surplus'; //int(11) DEFAULT '-1' COMMENT '剩余数量 -1.无限制 0.剩余次数 0 其他为剩余数量',
|
||
const LIMIT_NUM = 'limit_num'; //int(11) DEFAULT '0' COMMENT '会员参与活动次数',
|
||
const START_TIME = 'start_time'; //varchar(13) DEFAULT '' COMMENT '生效时间',
|
||
const END_TIME = 'end_time'; //varchar(13) DEFAULT '0' COMMENT '结束时间',
|
||
const ROLE_TYPE = 'role_type'; //tinyint(1) DEFAULT '0' COMMENT '角色 3常规会员 2plus会员
|
||
const SCENE_TYPE = 'scene_type'; //tinyint(1) DEFAULT '1' COMMENT '充值类型 1普通充值活动 2成为Plus会员充值活动 3续费plus会员充值活动',
|
||
const COUPON_ID = 'coupon_id'; //int(11) DEFAULT '0' COMMENT '优惠券id或券包id',
|
||
const COUPON_TYPE = 'coupon_type'; //tinyint(1) DEFAULT '0' COMMENT '0未绑定优惠活动 1单张优惠券 2优惠包',
|
||
const STATUS = 'status'; //tinyint(1) DEFAULT '1' COMMENT '状态:1,正常,2,删除。',
|
||
const ADD_TIME = 'add_time'; //varchar(20) DEFAULT '' COMMENT '添加时间',
|
||
const STATE = 'state'; // `state` tinyint(1) DEFAULT '1' COMMENT '0.关闭 1.启用',
|
||
const LINE_STATE = 'line_state'; // `line_state` tinyint(1) DEFAULT '0' 'COMMENT 线下充值 1.开启 2.关闭'
|
||
const ONLINE_STATE = 'online_state'; // `online_state` tinyint(1) DEFAULT '0' COMMENT '线上充值 1.开启 2.关闭'
|
||
const TIME = 'time'; // `online_state` int(6) DEFAULT '365' COMMENT 'plus会员有效时长'
|
||
const AGING_REPAY = 'aging_repay'; // tinyint(1) DEFAULT '0' COMMENT '分期偿还:0.关闭,1.开启.',
|
||
const AGING_TIME = 'aging_time'; // varchar(20) DEFAULT '' NULL COMMENT '分期数',
|
||
|
||
/**
|
||
* 获取充值活动
|
||
* @param int $merchant_id 商户id
|
||
* @param int $store_id 门店id
|
||
* @param int $vip_id 会员id
|
||
* @param int $vip_type 类型 1.普通类型 2.plus过期 3.plus正常
|
||
* @param int $type 0.线下 1.线上
|
||
* @return array|bool $give_money|false
|
||
*/
|
||
public function getRechargeActivity($merchant_id, $store_id, $vip_id, $vip_type = 1, $type = 0)
|
||
{
|
||
if (!$merchant_id || !$store_id) {
|
||
return false;
|
||
}
|
||
|
||
// 获取商户plus设置
|
||
$RPModel = D('RetailPlusSet');
|
||
$plus_set = $RPModel->getFind(array($RPModel::MERCHANT_ID => $merchant_id), array($RPModel::SWITCH_TYPE, $RPModel::RECHARGE_SWITCH_TYPE, $RPModel::RENEW_RECHARGE_SWITCH_TYPE));
|
||
// 判断商户plus设置
|
||
$switch_type = $plus_set[$RPModel::SWITCH_TYPE] ?: 1;
|
||
$recharge_switch_type = $plus_set[$RPModel::RECHARGE_SWITCH_TYPE] ?: 1;
|
||
$renew_recharge_switch_type = $plus_set[$RPModel::RENEW_RECHARGE_SWITCH_TYPE] ?: 1;
|
||
|
||
// 查询充值活动
|
||
$time = time();
|
||
$map = array(
|
||
$this::MERCHANT_ID => $merchant_id,
|
||
$this::STORE_ID => $store_id,
|
||
$this::STATUS => 1,
|
||
$this::STATE => 1,
|
||
$this::SURPLUS => array(array('eq', '-1'), array('gt', 0), 'or'),
|
||
$this::START_TIME => array(array('eq', ''), array('lt', $time), 'or'),
|
||
$this::END_TIME => array(array('eq', ''), array('gt', $time), 'or'),
|
||
);
|
||
// 判断线上线下
|
||
if ($type == 0) { // 线下
|
||
$map[$this::LINE_STATE] = 1;
|
||
} else { // 线上
|
||
$map[$this::ONLINE_STATE] = 1;
|
||
}
|
||
// 判断会员类型
|
||
if ($vip_type == 1) { // 普通会员
|
||
$map[$this::SCENE_TYPE] = array('in', array(1, 2));
|
||
} else { // 过期,正常plus
|
||
$map[$this::SCENE_TYPE] = array('in', array(1, 3));
|
||
}
|
||
$field = array(
|
||
$this::ID,
|
||
$this::RECHARGE_MONEY,
|
||
$this::GIVE_MONEY,
|
||
$this::LIMIT_NUM,
|
||
$this::ROLE_TYPE,
|
||
$this::SCENE_TYPE,
|
||
$this::TIME,
|
||
$this::AGING_REPAY,
|
||
$this::AGING_TIME,
|
||
);
|
||
$recharge_list = $this->getSelect($map, $field);
|
||
if (!$recharge_list) {
|
||
// 暂无充值活动
|
||
return false;
|
||
}
|
||
// 处理数据,定义空数组
|
||
$vip_recharge_activity = array();
|
||
$plus_recharge_activity = array();
|
||
// 充值记录
|
||
$ROModel = D('RetailRechargeOrder');
|
||
$map2 = array(
|
||
$ROModel::MERCHANT_ID => $merchant_id,
|
||
$ROModel::STORE_ID => $store_id,
|
||
$ROModel::VID => $vip_id,
|
||
$ROModel::STATUS => 1,
|
||
);
|
||
|
||
// 获取商户充值设置
|
||
$MerchantModel = new RetailMerchantModel();
|
||
$map = array(
|
||
$MerchantModel::ID => $merchant_id,
|
||
$MerchantModel::STATUS => 1
|
||
);
|
||
$field = array($MerchantModel::AGING_CYCLE, $MerchantModel::AGING_REPAY, $MerchantModel::AGING_TIME);
|
||
$setting = $MerchantModel->getFind($map, $field);
|
||
|
||
foreach ($recharge_list as &$val) {
|
||
// 判断会员充值限制
|
||
if (intval($val[$this::LIMIT_NUM]) > 0) {
|
||
$map3 = $map2;
|
||
$map3[$ROModel::VIP_RECHARGE_ID] = $val[$this::ID];
|
||
$recharge_count = $ROModel->getCount($map3);
|
||
// 大于充值限制
|
||
if (intval($recharge_count) >= intval($val[$this::LIMIT_NUM])) {
|
||
continue;
|
||
}
|
||
}
|
||
$val['aging_repay_name'] = $val['aging_repay'] == 1 ? '分期到账' : '直接到账';
|
||
$val['aging_cycle_name'] = $setting['aging_cycle'] == 0 ? '日' : '月';
|
||
$val['active_aging_time'] = $val['aging_time'];
|
||
$val['aging_time'] = $setting['aging_time'];
|
||
// 判断会员类型
|
||
switch ($vip_type) {
|
||
case 1:
|
||
// 普通会员
|
||
if ($val[$this::ROLE_TYPE] == 3 && $val[$this::SCENE_TYPE] == 1) {
|
||
array_push($vip_recharge_activity, $val);
|
||
continue;
|
||
} else if ($val[$this::ROLE_TYPE] == 3 && $val[$this::SCENE_TYPE] == 2 && $switch_type == 2 && $recharge_switch_type == 2) {
|
||
array_push($plus_recharge_activity, $val);
|
||
continue;
|
||
} else {
|
||
continue;
|
||
}
|
||
break;
|
||
case 2:
|
||
// 过期plus
|
||
if ($val[$this::ROLE_TYPE] == 3 && $val[$this::SCENE_TYPE] == 1) {
|
||
array_push($vip_recharge_activity, $val);
|
||
continue;
|
||
} else if ($val[$this::ROLE_TYPE] == 2 && $val[$this::SCENE_TYPE] == 3 && $switch_type == 2 && $renew_recharge_switch_type == 2) {
|
||
array_push($plus_recharge_activity, $val);
|
||
continue;
|
||
} else {
|
||
continue;
|
||
}
|
||
break;
|
||
case 3:
|
||
// 正常plus
|
||
if ($val[$this::ROLE_TYPE] == 2 && $val[$this::SCENE_TYPE] == 1) {
|
||
array_push($vip_recharge_activity, $val);
|
||
continue;
|
||
} else if ($val[$this::ROLE_TYPE] == 2 && $val[$this::SCENE_TYPE] == 3 && $switch_type == 2 && $renew_recharge_switch_type == 2) {
|
||
array_push($plus_recharge_activity, $val);
|
||
continue;
|
||
} else {
|
||
continue;
|
||
}
|
||
break;
|
||
default:
|
||
continue;
|
||
break;
|
||
}
|
||
}
|
||
$res = array(
|
||
'vip_recharge_activity' => $vip_recharge_activity,
|
||
'plus_recharge_activity' => $plus_recharge_activity
|
||
);
|
||
return $res;
|
||
}
|
||
|
||
/**
|
||
* 获取最大赠送金额
|
||
* @param int $merchant_id 商户id
|
||
* @param int $store_id 门店id
|
||
* @param int $money 充值金额
|
||
* @param int $type 类型 0.线下 1.线上
|
||
* @param int $user_type 用户类型 2.plus会员 3.普通会员
|
||
* @return array|bool $res|false
|
||
*/
|
||
public function getMaxGiveMoney($merchant_id, $store_id, $money, $type, $user_type = 3)
|
||
{
|
||
if (!$merchant_id || !$store_id) {
|
||
return false;
|
||
}
|
||
// 查询充值活动
|
||
$time = time();
|
||
$map = array(
|
||
$this::MERCHANT_ID => $merchant_id,
|
||
$this::STORE_ID => $store_id,
|
||
$this::STATUS => 1,
|
||
$this::STATE => 1,
|
||
$this::RECHARGE_MONEY => array('elt', $money),
|
||
$this::SURPLUS => array(array('eq', '-1'), array('gt', 0), 'or'),
|
||
$this::START_TIME => array(array('eq', ''), array('lt', $time), 'or'),
|
||
$this::END_TIME => array(array('eq', ''), array('gt', $time), 'or'),
|
||
$this::ROLE_TYPE => $user_type,
|
||
$this::SCENE_TYPE => 1,
|
||
);
|
||
|
||
// 判断线上线下
|
||
if ($type == 0) { // 线下
|
||
$map[$this::LINE_STATE] = 1;
|
||
} else { // 线上
|
||
$map[$this::ONLINE_STATE] = 1;
|
||
}
|
||
$field = array($this::ID, $this::RECHARGE_MONEY, $this::GIVE_MONEY, $this::SURPLUS, $this::LIMIT_NUM, $this::AGING_REPAY, $this::AGING_TIME);
|
||
$recharge_act = $this->getOrderFind($map, $field, 'give_money desc');
|
||
return $recharge_act ? $recharge_act : 0;
|
||
}
|
||
|
||
|
||
/**
|
||
* 验证会员充值档次是否能进行,并返回充值档次信息
|
||
* @param $vid 会员id
|
||
* @param $recharge_id 充值档次id
|
||
* @param $merchant_id 商户id
|
||
* @param $type 验证类型 1线上 2线下
|
||
* @return bool
|
||
*/
|
||
public function checkRecharge_v1($vid, $recharge_id, $merchant_id, $type)
|
||
{
|
||
$time = time();
|
||
$map = array(
|
||
$this::ID => $recharge_id,
|
||
$this::STATUS => 1,
|
||
$this::STATE => 1,
|
||
$this::SURPLUS => array(array('eq', '-1'), array('gt', 0), 'or'),
|
||
$this::START_TIME => array(array('eq', ''), array('lt', $time), 'or'),
|
||
$this::END_TIME => array(array('eq', ''), array('gt', $time), 'or'),
|
||
);
|
||
if ($type == 1) {
|
||
$map[$this::ONLINE_STATE] = 1;
|
||
} else {
|
||
$map[$this::LINE_STATE] = 1;
|
||
}
|
||
$data = $this->getFind($map);
|
||
// 判断活动
|
||
if (empty($data)) {
|
||
return false;
|
||
}
|
||
// 判断会员限制数量
|
||
if (intval($data[$this::LIMIT_NUM]) > 0) {
|
||
$RechargeOrderModel = D('RetailRechargeOrder');
|
||
$map = array(
|
||
$RechargeOrderModel::VID => $vid,
|
||
$RechargeOrderModel::MERCHANT_ID => $merchant_id,
|
||
$RechargeOrderModel::STATUS => 1,
|
||
$RechargeOrderModel::VIP_RECHARGE_ID => $recharge_id,
|
||
);
|
||
$count = $RechargeOrderModel->getCount($map);
|
||
if ($count >= intval($data[$this::LIMIT_NUM])) {
|
||
return false;
|
||
}
|
||
}
|
||
// 验证通过
|
||
return $data;
|
||
}
|
||
} |