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.
188 lines
6.0 KiB
PHP
188 lines
6.0 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: xmy 280564871@qq.com
|
|
* Date: 2017/4/1
|
|
* Time: 10:16
|
|
*/
|
|
|
|
namespace Mobile\Model;
|
|
use Think\Model;
|
|
class PointShopRecordModel extends Model{
|
|
|
|
/**
|
|
* 构造函数
|
|
* @param string $name 模型名称
|
|
* @param string $tablePrefix 表前缀
|
|
* @param mixed $connection 数据库连接信息
|
|
*/
|
|
public function __construct($name = '', $tablePrefix = '', $connection = '') {
|
|
/* 设置默认的表前缀 */
|
|
$this->tablePrefix ='tab_';
|
|
/* 执行构造方法 */
|
|
parent::__construct($name, $tablePrefix, $connection);
|
|
}
|
|
|
|
/**
|
|
* 列表
|
|
* @param $map
|
|
* @param string $order
|
|
* @param int $p
|
|
* @return mixed
|
|
* author: xmy 280564871@qq.com
|
|
*/
|
|
public function getLists($map,$order="",$p=1){
|
|
$page = intval($p);
|
|
$page = $page ? $page : 1; //默认显示第一页数据
|
|
$row = 10;
|
|
$data['data'] = $this
|
|
->table("tab_point_shop_record as sr")
|
|
->field("sr.id,sr.user_id,sr.good_id,sr.good_name,sr.number,sr.pay_amount,sr.create_time,sr.address,sr.good_key")
|
|
->join("left join tab_user u on u.id = sr.user_id")
|
|
->where($map)
|
|
->order($order)
|
|
->page($page, $row)
|
|
->select();
|
|
$data['count'] = $this
|
|
->table("tab_point_shop_record as sr")
|
|
->where($map)
|
|
->join("left join tab_user u on u.id = sr.user_id")
|
|
->count();
|
|
return $data;
|
|
}
|
|
|
|
public function getUserSpendPoint($user_id){
|
|
$map['user_id'] = $user_id;
|
|
$data = $this->where($map)->sum("pay_amount");
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 购买商品
|
|
* @param $good_id
|
|
* @param $user_id
|
|
* @param $num 购买数量
|
|
* @param $address_id 地址ID
|
|
* @return bool
|
|
* author: xmy 280564871@qq.com
|
|
*/
|
|
public function buy($good_id,$user_id,$num,$address_id=""){
|
|
$good = D("PointShop")->getData($good_id);
|
|
unset($good['cover']);//去掉商品图片字段 否则会更新掉商品图片
|
|
if($good['good_type'] == 2 && $num != 1){
|
|
$this->error = "虚拟商品单次只能兑换一条";
|
|
return false;
|
|
}
|
|
if ($good['number'] < $num){
|
|
$this->error = "库存不足";
|
|
return false;
|
|
}
|
|
$pay_amount = $num * $good['price'];//支付价格
|
|
$user_point = D("User")->where(['id'=>$user_id])->getField("point");
|
|
if($pay_amount > $user_point){
|
|
$this->error = "积分不足";
|
|
return false;
|
|
}
|
|
if($good['good_type'] == 1){
|
|
$address = D("UserAddress")->where(['id'=>$address_id,'user_id'=>$user_id])->find();
|
|
if (empty($address)){
|
|
$this->error = "地址不存在";
|
|
return false;
|
|
}
|
|
$data['user_name'] = $address['name'];
|
|
$data['address'] = $address['city'].$address['address'];
|
|
$data['phone'] = $address['phone'];
|
|
}else{
|
|
//领取兑换码
|
|
$good_key = json_decode($good['good_key']);
|
|
$i = $num;
|
|
while ($i > 0){
|
|
$key[] = array_shift($good_key);
|
|
$i--;
|
|
}
|
|
$good['good_key'] = json_encode($good_key);
|
|
$result['good_key'] = $key;
|
|
$data['good_key'] = json_encode($key);
|
|
}
|
|
//防刷预警
|
|
if(C('BRUSH_POINT') && $user_point >= C('BRUSH_POINT') && C('BRUSH_EMAIL') ){
|
|
$content = "尊敬的用户:<br/> 系统检测到玩家【".get_user_name($user_id)."】账户积分达".$user_point.",超出上限".C('BRUSH_POINT').",可能存在异常,请尽快处理。";
|
|
sendBrushMail(C('BRUSH_EMAIL'),$content);
|
|
}
|
|
//生成购买记录
|
|
$good['number'] -= $num;
|
|
$data['user_id'] = $user_id;
|
|
$data['pay_amount'] = $pay_amount;
|
|
$data["good_id"] = $good['id'];
|
|
$data['good_name'] = $good['good_name'];
|
|
$data['good_type'] = $result['good_type'] = $good['good_type'];
|
|
$data['number'] = $num;
|
|
$data['status'] = 1;
|
|
$data['create_time'] = time();
|
|
$this->startTrans();
|
|
$good_result = D("PointShop")->save($good);
|
|
$user_result = D("User")->where(['id'=>$user_id])->setField(['point'=>$user_point-$pay_amount]);
|
|
$record_result = $this->add($data);
|
|
if($user_result !== false && $record_result !== false && $good_result !== false){
|
|
$this->commit();
|
|
return $result;
|
|
}else{
|
|
$this->rollback();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 积分兑换平台币
|
|
* @param $user_id
|
|
* @param $num 兑换数量
|
|
* @return bool
|
|
* author: xmy 280564871@qq.com
|
|
*/
|
|
public function PointConvertCoin($user_id,$num){
|
|
$pay_amount = $num * 100;//平台币价格
|
|
$user = D("User")->find($user_id);
|
|
$user_point = $user['point'];
|
|
if($pay_amount > $user_point){
|
|
$this->error = "积分不足";
|
|
return false;
|
|
}
|
|
//防刷预警
|
|
if(C('BRUSH_POINT') && $user_point >= C('BRUSH_POINT') && C('BRUSH_EMAIL') ){
|
|
$content = "尊敬的用户:<br/> 系统检测到玩家【".get_user_name($user_id)."】账户积分达".$user_point.",超出上限".C('BRUSH_POINT').",可能存在异常,请尽快处理。";
|
|
sendBrushMail(C('BRUSH_EMAIL'),$content);
|
|
}
|
|
|
|
$data['user_id'] = $user_id;
|
|
$data['pay_amount'] = $pay_amount;
|
|
$data["good_id"] = 0;
|
|
$data['good_name'] = "平台币";
|
|
$data['good_type'] = 3;
|
|
$data['number'] = $num;
|
|
$data['status'] = 1;
|
|
$data['create_time'] = time();
|
|
$this->startTrans();
|
|
$record_result = $this->add($data);
|
|
$user['point'] -= $pay_amount;
|
|
$user['balance'] += $num;
|
|
$user_result = D("User")->save($user);
|
|
if($record_result !== false && $user_result !== false){
|
|
$this->commit();
|
|
if(C('BRUSH_POINT_PTB') && C('BRUSH_EMAIL')){
|
|
$where['user_id'] = $user_id;
|
|
$where['good_type'] = 3;
|
|
$where['create_time'] = total(1,0);
|
|
$point = M('point_shop_record','tab_')->where($where)->sum('number');
|
|
if($point > C('BRUSH_POINT_PTB')){
|
|
$content = "尊敬的用户:<br/> 系统检测到玩家【".get_user_name($user_id)."】于".date('Y年m月d日')."使用积分兑换".$point."个平台币,超出上限".C('BRUSH_POINT_PTB').",可能存在异常,请尽快处理。";
|
|
sendBrushMail(C('BRUSH_EMAIL'),$content);
|
|
}
|
|
}
|
|
return true;
|
|
}else{
|
|
$this->rollback();
|
|
return false;
|
|
}
|
|
|
|
}
|
|
} |