Coupon.php 6.0 KB

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