文件
mashangyoujiu/Application/Common/Model/VipModel.class.php

533 行
21 KiB
PHP
可执行文件

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
namespace Common\Model;
use Think\Model;
//[1.8.50]
class VipModel extends Model
{
protected $tableName = 'retail_vip';
protected $fields = array(
'id', //int(11) NOT NULL AUTO_INCREMENT,
'openid', //text COMMENT '用户在公众号的openid',
'mobile_phone', //varchar(11) DEFAULT '0' COMMENT '手机号',
'password', //varchar(20) DEFAULT NULL COMMENT '密码固定6位',
'balance', //decimal(10,2) DEFAULT '0.00' COMMENT '余额',
'merchantid', //int(11) DEFAULT NULL COMMENT '商户id',
'storeid', //int(11) DEFAULT NULL COMMENT '门店id 记录在哪个门店注册的会员',
'plus_id', //int(11) DEFAULT '0' COMMENT 'plus会员id',
'plus_type', //tinyint(1) DEFAULT '3' COMMENT '2plus 会员 3常规会员',
'plus_state', //tinyint(1) DEFAULT '2' COMMENT '1plus会员 正常 2plus 会员过期',
'guideid', //int(11) DEFAULT NULL COMMENT '导购员id 默认无导购',
'name', //varchar(30) DEFAULT NULL COMMENT '会员姓名',
'nickname', //varchar(50) DEFAULT NULL COMMENT '微信昵称',
'city', //text COMMENT '城市',
'province', //text COMMENT '省',
'sex', //tinyint(1) DEFAULT '1' COMMENT '性别 1男 2女',
'birthday', //varchar(11) DEFAULT NULL COMMENT '生日',
'integral', //int(11) DEFAULT '0' COMMENT '积分',
'headimgurl', //text COMMENT '头像',
'grade_integral', //int(11) DEFAULT '0' COMMENT '等级积分 只增不减',
'addtime', //varchar(11) DEFAULT NULL COMMENT '首次注册时间',
'is_register', //tinyint(1) DEFAULT '0' COMMENT '0未激活公众号会员 1已激活',
'is_perfect', //tinyint(1) DEFAULT '0' COMMENT '0未完善资料 1已完善',
'cardnum', //text COMMENT '会员卡号',
'cardnum_old', //text COMMENT '旧会员卡号',
'card_key', //varchar(255) DEFAULT '0',
'iswxcode', //tinyint(1) DEFAULT '0' COMMENT '0未添加到微信会员卡 1已添加',
'status', //tinyint(1) DEFAULT '1' COMMENT '会员状态;默认1为正常;1:正常会员,2:删除会员 3冻结 4挂失',
'density_free', //tinyint(1) DEFAULT '2' COMMENT '会员免密支付 1开启 2关闭',
'source_channel', //来源渠道
);
/*
* 查询
* */
public function getFind($map, $field = '*')
{
$res = $this->where($map)->field($field)->find();
return $res;
}
public function getSelect($map, $field)
{
if (!$field) {
$field = '*';
}
$res = $this->where($map)->field($field)->select();
return $res;
}
public function getSelectList($map, $limit)
{
$res = $this->alias('v')
->join('ls_retail_vip_grade as g on g.id=v.gradeid')
->join('ls_retail_store as s on s.id=v.storeid')
->where($map)
->page($limit)
->field('v.mobile_phone,v.name,v.integral,v.addtime,v.overtime,g.grade_name,s.storetag as storename')
->select();
return $res;
}
/**
* 获取某字段
* @param $map
* @param string $fields
* @param $bol
* @return mixed
*/
public function getFindField($map, $fields = 'id', $bol = false)
{
$res = $this->where($map)->getField($fields, $bol);
return $res;
}
public function toSetInc($map, $key, $val)
{
$res = $this->where($map)->setInc($key, $val);
return $res;
}
/*
* 减少
* */
public function toSetDec($map, $key, $val)
{
$res = $this->where($map)->setDec($key, $val);
return $res;
}
/*
* 添加用户
* */
public function addVip($item)
{
$res = $this->add($item);
return $res;
}
/*
* 更新用户信息
* */
public function updateVip($map, $update)
{
$res = $this->where($map)->save($update);
return $res;
}
/**
* 获取数量
* @param $map
* @return mixed
*/
public function getCount($map)
{
$res = $this->where($map)->count();
return $res ? $res : 0;
}
/**
* 获取某字段的sum值
* @param $map
* @param $key
* @return mixed
*/
public function getSum($map, $key)
{
if (!$key) {
return 0;
}
$res = $this->where($map)->sum($key);
return $res ? $res : 0;
}
/*
* 验证会员密码是否正确
* */
public function verifyPassword($vid, $password)
{
$vip = $this->where(array('id' => $vid))->find();
$CryptrModel = new \Addons\Api\Crypto ();
$orignPwd = $CryptrModel->encrypt($password, date('YmdHis', $vip['addtime']));
if ($vip['password'] == $orignPwd) {
return true;
} else {
return false;
}
}
/**
* 2018-09-28 添加会员卡
* @param $data 新增会员信息
* @param $storeid 当前门店id
* @param $merchantid 商户id
*/
/* public function addVipCard($data,$storeid,$merchantid){
if (empty($data)||empty($storeid)||empty($merchantid)){
return 0;//参数信息有误
}else{
$CryptrModel = new \Addons\Api\Crypto ();
//会员卡存在主店id字段,查询出来
$store_map['merchantid'] = $merchantid;
$store_map['store_type'] = 0;
//需要判断该手机号是否在该商户下已被使用
$map['mobile_phone'] = $data['mobile_phone'];
$map['merchantid'] = $merchantid;
$map['status'] = array('neq',2);
$find = $this->where($map)->find();
if ($find){
return 2;//手机号码被占用
}else{
$pwd = substr($data['mobile_phone'],-6);//加密会员卡密码时使用;默认密码手机后六位
//需要判断实体卡是否被使用 PS:实体卡在新增会员卡时非必填项
if ($data['card_key']){
//需要绑定实体卡
$card_map['card_key'] = $data['card_key'];
$card = $this->where($card_map)->find();
if ($card){
return 3;//实体卡已被使用
}else{
//可正常添加新会员
$data['addtime'] = time();
$password = $CryptrModel->encrypt($pwd,date('YmdHis',intval($data['addtime'])));
$data['password'] = $password;
$data['cardnum'] = $data['mobile_phone'];
$res = $this->add($data);
if ($res){
return 1;
}else{
return 4;
}
}
}else{
//不需绑定实体卡
$data['addtime'] = time();
$password = $CryptrModel->encrypt($pwd,date('YmdHis',intval($data['addtime'])));
$data['password'] = $password;
$data['cardnum'] = $data['mobile_phone'];
$res = $this->add($data);
if ($res){
return 1;
}else{
return 4;
}
}
}
}
}*/
//2019-1-10
public function addVipCard($data, $storeid, $merchantid)
{
if (empty($data) || empty($storeid) || empty($merchantid)) {
return 0;//参数信息有误
} else {
if (!empty($data['mobile_phone'])) {
//需要判断该手机号是否在该商户下已被使用
$map['mobile_phone'] = $data['mobile_phone'];
$map['merchantid'] = $merchantid;
$map['status'] = array('neq', 2);
$find = $this->where($map)->find();
if ($find) {
return 2;
}
}
$data['addtime'] = time();
$CryptrModel = new \Addons\Api\Crypto ();
if (!empty($data['password'])) {
//密码
$password = $CryptrModel->encrypt($data['password'], date('YmdHis', intval($data['addtime'])));
} else {
$pwd = substr($data['mobile_phone'], -6);//加密会员卡密码时使用;默认密码手机后六位
$password = $CryptrModel->encrypt($pwd, date('YmdHis', intval($data['addtime'])));
}
if (!empty($data['card_key'])) {
$card_map['card_key'] = $data['card_key'];
$card_map['merchantid'] = $merchantid;
$card = $this->where($card_map)->find();
if ($card) {
return 3;
}
}
//可正常添加新会员
$data['password'] = $password;
$data['cardnum'] = $data['mobile_phone'];
$res = $this->add($data);
if ($res) {
$success['status'] = 1;
$success['id'] = $res;
return $success;
} else {
return 5;
}
}
}
/**
* 查询会员充值档次接口
* @param $merchantid 商户id
* @param $vid 会员id
* @param $money 充值付款金额
* @return array 充值档次数组。修改表后原来的接口要修改拒绝删除
* 便于版本旧的小程序调用
*/
public function getRechargeList($merchantid, $storeid, $vid, $money = 0)
{
$time = time();
$map['merchantid'] = $merchantid;
$map['store_id'] = $storeid;
$map['role_type'] = 3;
$map['scene_type'] = 1;
$map['stime'] = array(array('eq', 0), array('lt', $time), 'or');
$map['etime'] = array(array('eq', 0), array('gt', $time), 'or');
if ($money) {
$map['money'] = array('elt', $money);
$rechargelist = M("vip_recharge")
->where($map)->order('money desc,give desc')
->select();
} else {
$rechargelist = M("vip_recharge")
->where($map)->order('money asc')
->select();
}
foreach ($rechargelist as $k => $v) {
if ($v['limit'] != 0) {
$c = M('recharge_order')
->where(array('vip_rechargeid' => $v['id'], 'type' => 1, 'status' => 1, 'vid' => $vid))
->count();
// 检测单会员充值
if ($c >= $v['limit']) {
unset($rechargelist[$k]);
}
}
// 检测库存
if ($v['maxnum'] != 0 && $v['maxnum'] <= $v['num']) {
unset($rechargelist[$k]);
}
}
$rechargelist = array_values($rechargelist);
return $rechargelist;
}
/**
* 查询会员充值档次接口
* @param $merchantid 商户id
* @param $vid 会员id
* @param $money 充值付款金额
* @param plus会员 $type = 3 常规会员 2plus会员
* @param $recharge_switch_type 2开启 1关闭 成为plus会员活动
* @param $renew_recharge_switch_type 2开启 1关闭 续费plus会员活动
* @return array 充值档次数组。
*
*/
public function getRechargeList_v2($merchantid, $store_id, $vid, $money = 0, $type = 3, $recharge_switch_type, $renew_recharge_switch_type)
{
$time = time();
$map['merchantid'] = $merchantid;
$map['store_id'] = $store_id;
$map['status'] = 1;
$map['stime'] = array(array('eq', 0), array('lt', $time), 'or');
$map['etime'] = array(array('eq', 0), array('gt', $time), 'or');
if ($type == 2) { //plus会员只显示plus充值类型
$map['role_type'] = 2;
} else { //常规会员
$map['role_type'] = 3;
}
if ($money) {
$map['recharge_money'] = array('elt', $money);
$rechargelist = M("vip_recharge")
->where($map)->order('recharge_money desc,give_money desc')
->select();
} else {
$rechargelist = M("vip_recharge")
->where($map)->order('recharge_money asc')
->select();
}
$vipRechargeList = array(); //常规充值活动
$plusVipRechargeList = array(); //成为Plus会员充值活动
$renewPlusRechargeList = array(); //plus会员续费充值活动
foreach ($rechargelist as $k => $v) {
if ($v['limit'] != 0) {
$c = M('recharge_order')
->where(array('vip_rechargeid' => $v['id'], 'type' => 1, 'status' => 1, 'vid' => $vid))
->count();
// 检测单会员充值
if ($c >= $v['limit']) {
unset($rechargelist[$k]);
continue;
}
}
// 检测库存
if ($v['maxnum'] != 0 && $v['maxnum'] <= $v['num']) {
unset($rechargelist[$k]);
continue;
}
if ($v['scene_type'] == 1) {//常规充值活动
array_push($vipRechargeList, $rechargelist[$k]);
} else if ($v['scene_type'] == 2 && $recharge_switch_type == 2) {//成为plus会员活动
array_push($plusVipRechargeList, $rechargelist[$k]);
} else if ($v['scene_type'] == 3 && $renew_recharge_switch_type == 2) {//plus续费活动
array_push($renewPlusRechargeList, $rechargelist[$k]);
}
}
$res['vipRechargeList'] = $vipRechargeList;
$res['plusVipRechargeList'] = $plusVipRechargeList;
$res['renewPlusRechargeList'] = $renewPlusRechargeList;
return $res;
}
/**
* 查询会员充值档次接口
* @param $merchantid 商户id
* @param $vid 会员id
* @param $vip_type 类型 1.普通类型 2.plus过期 3.plus正常
* @param $switch_type plus状态 1.关闭 2.开启
* @param $recharge_switch_type 充值开通plus状态 1.关闭 2.开启
* @param $renew_recharge_switch_type 充值续费plus状态 1.关闭 2.开启
* @return array
*/
public function getRechargeList_v3($merchant_id, $store_id, $vid, $vip_type = 1, $switch_type = 1, $recharge_switch_type = 1, $renew_recharge_switch_type = 1)
{
$vipRechargeList = array(); // 常规充值活动
$plusVipRechargeList = array(); // 成为Plus会员充值活动
$renewPlusRechargeList = array(); // plus会员续费充值活动
// 查询充值活动
$time = time();
$map = array(
'merchant_id' => $merchant_id,
'store_id' => $store_id,
'status' => 1,
'state' => 1,
'online_state' => 1,
'surplus' => array(array('eq', '-1'), array('gt', 0), 'or'),
'start_time' => array(array('eq', ''), array('lt', $time), 'or'),
'end_time' => array(array('eq', ''), array('gt', $time), 'or'),
);
if ($vip_type == 1) { // 普通会员
$map['scene_type'] = array('in', array(1, 2));
} else { // 过期,正常plus
$map['scene_type'] = array('in', array(1, 3));
}
$recharge_list = M("vip_recharge")->where($map)->select();
foreach ($recharge_list as &$val) {
// 判断会员充值限制
if (intval($val['limit_num']) > 0) {
$count = M('recharge_order')->where(array('vip_recharge_id' => $val['id'], 'type' => 1, 'status' => 1, 'vid' => $vid))->count();
if (intval($count) >= intval($val['limit_num'])) {
continue;
}
}
// 判断会员类型
switch ($vip_type) {
case 1:
// 普通会员
if ($val['role_type'] == 3 && $val['scene_type'] == 1) {
array_push($vipRechargeList, $val);
continue;
} else if ($val['role_type'] == 3 && $val['scene_type'] == 2 && $switch_type == 2 && $recharge_switch_type == 2) {
array_push($plusVipRechargeList, $val);
continue;
} else {
continue;
}
break;
case 2:
// 过期plus
if ($val['role_type'] == 3 && $val['scene_type'] == 1) {
array_push($vipRechargeList, $val);
continue;
} else if ($val['role_type'] == 2 && $val['scene_type'] == 3 && $switch_type == 2 && $renew_recharge_switch_type == 2) {
array_push($renewPlusRechargeList, $val);
continue;
} else {
continue;
}
break;
case 3:
// 正常plus
if ($val['role_type'] == 2 && $val['scene_type'] == 1) {
array_push($vipRechargeList, $val);
continue;
} else if ($val['role_type'] == 2 && $val['scene_type'] == 3 && $switch_type == 2 && $renew_recharge_switch_type == 2) {
array_push($renewPlusRechargeList, $val);
continue;
} else {
continue;
}
break;
default:
continue;
break;
}
}
$res['vipRechargeList'] = $vipRechargeList;
$res['plusVipRechargeList'] = $plusVipRechargeList;
$res['renewPlusRechargeList'] = $renewPlusRechargeList;
return $res;
}
/**
* 验证会员充值档次是否能进行。。。
* @param $vid 会员id
* @param $vip_rechargeid 充值档次id
* @return bool 验证通过 返回 true 失败 返回 false 只有true时能继续充值
*/
public function checkRecharge($vid, $vip_rechargeid)
{
$map['id'] = $vip_rechargeid;
$one = M("vip_recharge")
->where($map)->order('recharge_money asc')
->find();
if ($one['limit']) {
$c = M('recharge_order')
->where(array('vip_rechargeid' => $one['vip_rechargeid'], 'type' => 1, 'status' => 1, 'vid' => $vid))
->count();
// 检测单会员充值
if ($c >= $one['limit']) {
return false;
}
}
// 检测库存
if ($one['stock'] != 0 && $one['stock'] <= $one['num']) {
return false;
}
return true;
}
/**
* 验证会员充值档次是否能进行
* @param $vid 会员id
* @param $id 充值档次id
* @return bool
*/
public function checkRecharge_v1($vid, $id)
{
$time = time();
$map = array(
'id' => $id,
'status' => 1,
'state' => 1,
'online_state' => 1,
'surplus' => array(array('eq', '-1'), array('gt', 0), 'or'),
'start_time' => array(array('eq', ''), array('lt', $time), 'or'),
'end_time' => array(array('eq', ''), array('gt', $time), 'or')
);
$data = M("vip_recharge")->where($map)->find();
// 判断活动
if (empty($data)) {
return false;
}
// 判断会员限制数量
if (intval($data['limit_num']) > 0) {
$count = M('recharge_order')->where(array('vip_recharge_id' => $id, 'type' => 1, 'status' => 1, 'vid' => $vid))->count();
if (intval($count) >= intval($data['limit_num'])) {
return false;
}
}
// 验证通过
return true;
}
}