Coupon.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace app\api\controller\company;
  3. use app\common\controller\Apic;
  4. use think\Db;
  5. /**
  6. * 优惠券
  7. */
  8. class Coupon extends Apic
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = '*';
  12. //优惠券管理
  13. public function lists(){
  14. $status = input('status',1);
  15. $where = [
  16. 'company_id' => $this->auth->company_id,
  17. 'status' => $status,
  18. ];
  19. $list = Db::name('coupons')->where($where)->order('id desc')->autopage()->select();
  20. $this->success(1,$list);
  21. }
  22. //新增
  23. public function add(){
  24. $field = ['name','info','days'];
  25. $data = request_post_hub($field);
  26. $data['company_id'] = $this->auth->company_id;
  27. $data['createtime'] = time();
  28. $data['status'] = 1;
  29. Db::name('coupons')->insertGetId($data);
  30. $this->success('添加成功');
  31. }
  32. //上下架
  33. public function changestatus(){
  34. $id = input('id',0);
  35. $info = Db::name('coupons')->where('id',$id)->update(['status'=>0]);
  36. $this->success(1,$info);
  37. }
  38. //详情
  39. public function info(){
  40. $id = input('id',0);
  41. $info = Db::name('coupons')->where('id',$id)->find();
  42. $this->success(1,$info);
  43. }
  44. //编辑
  45. public function edit(){
  46. $id = input('id','');
  47. $check = Db::name('coupons')->where('id',$id)->where('company_id',$this->auth->company_id)->find();
  48. if(empty($check)){
  49. $this->error('不存在的卡券');
  50. }
  51. //
  52. $field = ['name','info','days'];
  53. $data = request_post_hub($field);
  54. Db::name('coupons')->where('id',$id)->update($data);
  55. $this->success('编辑成功');
  56. }
  57. //删除
  58. public function delete(){
  59. $id = input('id','');
  60. $check = Db::name('coupons')->where('id',$id)->where('company_id',$this->auth->company_id)->find();
  61. if(empty($check)){
  62. $this->error('不存在的优惠券');
  63. }
  64. Db::name('coupons')->where('id',$id)->delete();
  65. $this->success('删除成功');
  66. }
  67. //核销用户一张卡券
  68. public function hexiao(){
  69. $user_coupon_id = input('user_coupon_id',0);
  70. $map = [
  71. 'company_id' => $this->auth->company_id,
  72. 'id' => $user_coupon_id,
  73. ];
  74. Db::startTrans();
  75. $check = Db::name('user_coupons')->where($map)->find();
  76. if(empty($check)){
  77. Db::rollback();
  78. $this->error('不存在的卡券');
  79. }
  80. if($check['remain'] <= 0){
  81. Db::rollback();
  82. $this->error('卡券数量不足');
  83. }
  84. if($check['endtime'] <= time()){
  85. Db::rollback();
  86. $this->error('卡券已过期');
  87. }
  88. //核销日志
  89. $log = [
  90. 'user_id' => $check['user_id'],
  91. 'company_id' => $check['company_id'],
  92. 'coupons_id' => $check['coupons_id'],
  93. 'coupon_name' => $check['coupon_name'],
  94. 'user_coupon_id' => $check['id'],
  95. 'createtime' => time(),
  96. ];
  97. $log_id = Db::name('user_coupons_log')->insertGetId($log);
  98. if(!$log_id){
  99. Db::rollback();
  100. $this->error('核销失败');
  101. }
  102. //数量减一
  103. $rs = Db::name('user_coupons')->where($map)->update(['remain'=>$check['remain']-1]);
  104. if($rs === false){
  105. Db::rollback();
  106. $this->error('核销失败');
  107. }
  108. Db::commit();
  109. $this->success('核销完成');
  110. }
  111. }