Coupon.php 6.2 KB

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