Coupon.php 5.8 KB

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