123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?php
- namespace app\api\controller\company;
- use app\common\controller\Apic;
- use think\Db;
- /**
- * 优惠券
- */
- class Coupon extends Apic
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = '*';
- //优惠券管理
- public function lists(){
- $status = input('status',1);
- $where = [
- 'company_id' => $this->auth->company_id,
- 'status' => $status,
- ];
- $list = Db::name('coupons')->where($where)->order('id desc')->autopage()->select();
- $this->success(1,$list);
- }
- //新增
- public function add(){
- $field = ['name','info','days'];
- $data = request_post_hub($field);
- $data['company_id'] = $this->auth->company_id;
- $data['createtime'] = time();
- $data['status'] = 1;
- Db::name('coupons')->insertGetId($data);
- $this->success('添加成功');
- }
- //上下架
- public function changestatus(){
- $id = input('id',0);
- $info = Db::name('coupons')->where('id',$id)->update(['status'=>0]);
- $this->success();
- }
- //详情
- public function info(){
- $id = input('id',0);
- $info = Db::name('coupons')->where('id',$id)->find();
- $this->success(1,$info);
- }
- //编辑
- public function edit(){
- $id = input('id','');
- $check = Db::name('coupons')->where('id',$id)->where('company_id',$this->auth->company_id)->find();
- if(empty($check)){
- $this->error('不存在的卡券');
- }
- //
- $field = ['name','info','days'];
- $data = request_post_hub($field);
- Db::name('coupons')->where('id',$id)->update($data);
- $this->success('编辑成功');
- }
- //删除
- public function delete(){
- $id = input('id','');
- $check = Db::name('coupons')->where('id',$id)->where('company_id',$this->auth->company_id)->find();
- if(empty($check)){
- $this->error('不存在的优惠券');
- }
- Db::name('coupons')->where('id',$id)->delete();
- $this->success('删除成功');
- }
- //统一核销,获取信息
- public function gethexiao(){
- $code = input('code','','trim');
- $code = htmlspecialchars_decode($code);
- $code = explode('_',$code);
- if(!is_array($code) || !isset($code[0]) || !isset($code[1])){
- $this->error('错误的核销码');
- }
- $action = $code[0];
- $id = $code[1];
- if($action != 'coupon' && $action != 'order'){
- $this->error('错误的核销码');
- }
- if($action == 'coupon'){
- $map = [
- 'company_id' => $this->auth->company_id,
- 'check_code' => $id,
- 'remain' => ['gt',0],
- ];
- $check = Db::name('user_coupons')->field('id,coupon_name as name,endtime,getfrom')->where($map)->find();
- if(!empty($check)){
- $check['action'] = $action;
- }
- }else{
- $map = [
- 'company_id' => $this->auth->company_id,
- 'check_code' => $id,
- 'ordertype' => 3,
- 'status' => 1, //1=待核销
- ];
- $check = Db::name('order')->field('id,server_info as name,pay_fee as price')->where($map)->find();
- if(!empty($check)){
- $check['getfrom'] = '购买';
- $check['action'] = $action;
- }
- }
- if(empty($check))
- {
- $this->error('错误的核销码');
- }
- $this->success(1,$check);
- }
- //统一核销,提交
- public function hexiao(){
- $code = input('code','');
- $code = explode('_',$code);
- $action = $code[0];
- $id = $code[1];
- if($action != 'coupon' && $action != 'order'){
- $this->error('错误的核销码');
- }
- $this->$action($id);
- }
- //核销用户一张卡券
- public function coupon($user_coupon_id){
- $map = [
- 'company_id' => $this->auth->company_id,
- 'check_code' => $user_coupon_id,
- ];
- Db::startTrans();
- $check = Db::name('user_coupons')->where($map)->lock(true)->find();
- if(empty($check)){
- Db::rollback();
- $this->error('不存在的卡券');
- }
- if($check['remain'] <= 0){
- Db::rollback();
- $this->error('卡券数量不足');
- }
- if($check['endtime'] > 0 && $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'],
- 'staff_id' => $this->auth->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('卡券核销完成');
- }
- //核销用户的套餐订单
- public function order($order_id){
- $map = [
- 'company_id' => $this->auth->company_id,
- 'check_code' => $order_id,
- 'ordertype' => 3,
- ];
- Db::startTrans();
- $check = Db::name('order')->where($map)->lock(true)->find();
- if(empty($check)){
- Db::rollback();
- $this->error('不存在的套餐');
- }
- if($check['status'] != 1){
- Db::rollback();
- $this->error('该套餐已经核销过了');
- }
- $rs = Db::name('order')->where($map)->update(['status'=>2,'hexiao_time'=>time(),'staff_id'=>$this->auth->id]);
- if($rs === false){
- Db::rollback();
- $this->error('核销失败');
- }
- Db::commit();
- $this->success('核销完成');
- }
- }
|