Files
liche/common/libraries/entity/Credits_entity.php
T
2022-07-25 15:12:13 +08:00

127 lines
4.3 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: lcc
* Date: 2022/07/14
* Time: 09:40
*/
class Credits_entity{
private $ci;
public function __construct(){
$this->ci = get_instance();
$this->ci->load->model('app/liche/app_liche_users_model');
$this->ci->load->model('app/liche/app_liche_credits_log_model');
}
/**
* 积分变动
* @param $uid 用户id
* @param $credit 积分
* @param $type 类型 0活动 1派单成功 2完成试驾 3开票 4后台录入 5兑换商品
* @param $target_id 来源id
* @param $remark 备注
* @param $jsondata 其它json数据
* @return array
*/
public function change($uid,$credit,$type=0,$target_id=0,$remark='',$jsondata=[]){
if(!$uid || !$credit){
return ['code'=>0 ,'msg'=>'参数错误'];
}
$row = $this->ci->app_liche_users_model->get(['id'=>$uid]);
if(!$row){
return ['code'=>0 ,'msg'=>'用户不存在'];
}
try {
$this->ci->db->trans_begin();
$up_where = [
'id' => $uid
];
$credit_left = $row['credits']+$row['freeze_credits'];
if($credit>0){ //增加积分
$up_credit = [
"credits = credits+{$credit}" => null
];
$credit_left += $credit;
$up_credit['expire_time'] = time()+365*24*60*60;
}else{ //扣除积分
$credit_out = abs($credit);
$up_credit = [
"credits = credits-{$credit_out}" => null
];
$up_where["credits >="] = abs($credit_out);
$credit_left -= $credit_out;
}
$this->ci->app_liche_users_model->update($up_credit,$up_where);
$user_aff_rows = $this->ci->app_liche_users_model->db->affected_rows();
if(!$user_aff_rows){
throw new Exception('积分不足');
}
$add_log = [
'uid' => $uid,
'type' => $type,
'credit_left' => $credit_left,
'c_time' => time()
];
if($credit>0){
$add_log['credit_in'] = $credit;
}else{
$add_log['credit_out'] = abs($credit);
}
$target_id && $add_log['target_id'] = $target_id;
$remark && $add_log['remark'] = $remark;
$jsondata && $add_log['jsondata'] = json_encode($jsondata,JSON_UNESCAPED_UNICODE);
$this->ci->app_liche_credits_log_model->add($add_log);
$log_aff_rows = $this->ci->app_liche_users_model->db->affected_rows();
if(!$log_aff_rows){
throw new Exception('积分不足');
}
$this->ci->db->trans_commit();
return ['code'=>1,'msg'=>'操作成功'];
}catch (Exception $e){
$this->ci->db->trans_rollback();
return ['code'=>0,'msg'=>$e->getMessage()];
}
}
/**
* 冻结积分
* @param $uid
* @param $credit
* @param $type (0冻结资金 1解冻资金)
* @return array
*/
public function freeze($uid,$credit,$type=0){
$row = $this->ci->app_liche_users_model->get(['id'=>$uid]);
$where = [
'id' => $uid
];
if($type){
if($row['freeze_credits']<$credit){
return ['code'=>0,'msg'=>'积分不足'];
}
$update = [
"credits = credits+{$credit}" => null,
"freeze_credits = freeze_credits-{$credit}" => null
];
$where['freeze_credits>='] = $credit;
}else{
if($row['credits']<$credit){
return ['code'=>0,'msg'=>'冻结积分不足'];
}
$update = [
"credits = credits-{$credit}" => null,
"freeze_credits = freeze_credits+{$credit}" => null
];
$where['credits>='] = $credit;
}
$this->ci->app_liche_users_model->update($update,$where);
$aff_rows = $this->ci->app_liche_users_model->db->affected_rows();
if($aff_rows){
return ['code'=>1,'msg'=>'操作成功'];
}else{
return ['code'=>0,'msg'=>'操作失败'];
}
}
}