|
@@ -79,6 +79,56 @@ class Coupon extends Apic
|
|
|
$this->success('删除成功');
|
|
|
}
|
|
|
|
|
|
+ //核销用户一张卡券
|
|
|
+ public function hexiao(){
|
|
|
+ $user_coupon_id = input('user_coupon_id',0);
|
|
|
+
|
|
|
+ $map = [
|
|
|
+ 'company_id' => $this->auth->company_id,
|
|
|
+ 'id' => $user_coupon_id,
|
|
|
+ ];
|
|
|
+
|
|
|
+ Db::startTrans();
|
|
|
+ $check = Db::name('user_coupons')->where($map)->find();
|
|
|
+ if(empty($check)){
|
|
|
+ Db::rollback();
|
|
|
+ $this->error('不存在的卡券');
|
|
|
+ }
|
|
|
+ if($check['remain'] <= 0){
|
|
|
+ Db::rollback();
|
|
|
+ $this->error('卡券数量不足');
|
|
|
+ }
|
|
|
+ if($check['endtime'] <= time()){
|
|
|
+ Db::rollback();
|
|
|
+ $this->error('卡券已过期');
|
|
|
+ }
|
|
|
+
|
|
|
+ //核销日志
|
|
|
+ $log = [
|
|
|
+ 'user_id' => $check['user_id'],
|
|
|
+ 'company_id' => $check['company_id'],
|
|
|
+ 'coupons_id' => $check['coupons_id'],
|
|
|
+ 'coupon_name' => $check['coupon_name'],
|
|
|
+ 'user_coupon_id' => $check['id'],
|
|
|
+ 'createtime' => time(),
|
|
|
+ ];
|
|
|
+ $log_id = Db::name('user_coupons_log')->insertGetId($log);
|
|
|
+ if(!$log_id){
|
|
|
+ Db::rollback();
|
|
|
+ $this->error('核销失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ //数量减一
|
|
|
+ $rs = Db::name('user_coupons')->where($map)->update(['remain'=>$check['remain']-1]);
|
|
|
+ if($rs === false){
|
|
|
+ Db::rollback();
|
|
|
+ $this->error('核销失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ Db::commit();
|
|
|
+ $this->success('核销完成');
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
|
|
|
|