Coupon.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 gethexiao(){
  69. $code = input('code','');
  70. $code = explode('_',$code);
  71. $action = $code[0];
  72. $id = $code[1];
  73. if($action != 'hexiaocoupon' && $action != 'hexiaoorder'){
  74. $this->error('错误的核销码');
  75. }
  76. if($action == 'hexiaocoupon'){
  77. $map = [
  78. 'company_id' => $this->auth->company_id,
  79. 'id' => $id,
  80. 'remain' => ['gt',0],
  81. ];
  82. $check = Db::name('user_coupons')->field('id,coupon_name,coupon_info,endtime,getfrom')->where($map)->find();
  83. if(!empty($check)){
  84. $check['action'] = 'hexiaocoupon';
  85. }
  86. }else{
  87. $map = [
  88. 'company_id' => $this->auth->company_id,
  89. 'id' => $id,
  90. 'ordertype' => 3,
  91. 'status' => 2, //2=已支付,待处理
  92. ];
  93. $check = Db::name('order')->field('id,package_info,package_price')->where($map)->find();
  94. if(!empty($check)){
  95. $check['action'] = 'hexiaoorder';
  96. }
  97. }
  98. $this->success(1,$check);
  99. //$this->$action($id);
  100. }
  101. //统一核销
  102. public function hexiao(){
  103. $code = input('code','');
  104. $code = explode('_',$code);
  105. $action = $code[0];
  106. $id = $code[1];
  107. if($action != 'hexiaocoupon' && $action != 'hexiaoorder'){
  108. $this->error('错误的核销码');
  109. }
  110. $this->$action($id);
  111. }
  112. //核销用户一张卡券
  113. public function hexiaocoupon($user_coupon_id){
  114. $map = [
  115. 'company_id' => $this->auth->company_id,
  116. 'id' => $user_coupon_id,
  117. ];
  118. Db::startTrans();
  119. $check = Db::name('user_coupons')->where($map)->lock(true)->find();
  120. if(empty($check)){
  121. Db::rollback();
  122. $this->error('不存在的卡券');
  123. }
  124. if($check['remain'] <= 0){
  125. Db::rollback();
  126. $this->error('卡券数量不足');
  127. }
  128. if($check['endtime'] <= time()){
  129. Db::rollback();
  130. $this->error('卡券已过期');
  131. }
  132. //核销日志
  133. $log = [
  134. 'user_id' => $check['user_id'],
  135. 'company_id' => $check['company_id'],
  136. 'coupons_id' => $check['coupons_id'],
  137. 'coupon_name' => $check['coupon_name'],
  138. 'user_coupon_id' => $check['id'],
  139. 'staff_id' => $this->auth->id,
  140. 'createtime' => time(),
  141. ];
  142. $log_id = Db::name('user_coupons_log')->insertGetId($log);
  143. if(!$log_id){
  144. Db::rollback();
  145. $this->error('核销失败');
  146. }
  147. //数量减一
  148. $rs = Db::name('user_coupons')->where($map)->update(['remain'=>$check['remain']-1]);
  149. if($rs === false){
  150. Db::rollback();
  151. $this->error('核销失败');
  152. }
  153. Db::commit();
  154. $this->success('卡券核销完成');
  155. }
  156. //核销用户的套餐订单
  157. public function hexiaoorder($order_id){
  158. $map = [
  159. 'company_id' => $this->auth->company_id,
  160. 'id' => $order_id,
  161. 'ordertype' => 3,
  162. 'status' => 2, //2=已支付,待处理
  163. ];
  164. Db::startTrans();
  165. $check = Db::name('order')->where($map)->lock(true)->find();
  166. if(empty($check)){
  167. Db::rollback();
  168. $this->error('不存在的订单');
  169. }
  170. //数量减一
  171. $rs = Db::name('order')->where($map)->update(['status'=>3,'hexiaotime'=>time()]);
  172. if($rs === false){
  173. Db::rollback();
  174. $this->error('核销失败');
  175. }
  176. Db::commit();
  177. $this->success('订单核销完成');
  178. }
  179. }