Recharge.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace app\api\controller\company;
  3. use app\common\controller\Apic;
  4. use think\Db;
  5. /**
  6. * 储值卡
  7. */
  8. class Recharge 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('recharge_config')->where($where)->order('id desc')->select();
  20. //追加赠送
  21. if(!empty($list)){
  22. $config_ids = array_column($list,'id');
  23. $gift = Db::name('recharge_gift')->alias('gift')
  24. ->field('gift.*,coupons.name,coupons.info,coupons.days')
  25. ->join('coupons','gift.coupon_id = coupons.id','LEFT')
  26. ->where('gift.config_id','IN',$config_ids)
  27. ->where('coupons.status',1)
  28. ->select();
  29. foreach($list as $key => &$val){
  30. $val['gift'] = [];
  31. foreach($gift as $k => $v){
  32. if($val['id'] == $v['config_id']){
  33. $val['gift'][] = $v;
  34. }
  35. }
  36. }
  37. }
  38. $this->success(1,$list);
  39. }
  40. //新增
  41. public function add(){
  42. //验证
  43. if($this->auth->type != 1){
  44. $this->error('只有门店老板才能设置');
  45. }
  46. $field = ['price','giftprice'];
  47. $data = request_post_hub($field);
  48. $data['company_id'] = $this->auth->company_id;
  49. $data['status'] = 1;
  50. Db::startTrans();
  51. $config_id = Db::name('recharge_config')->insertGetId($data);
  52. if(!$config_id){
  53. Db::rollback();
  54. $this->error('添加失败');
  55. }
  56. //赠送卡券
  57. $gift_data = input('gift_data','','trim');
  58. $gift_data = json_decode(htmlspecialchars_decode($gift_data),true);
  59. if(is_array($gift_data) && !empty($gift_data)){
  60. $recharge_gift = [];
  61. foreach($gift_data as $key => $val){
  62. $recharge_gift[] = [
  63. 'config_id' => $config_id,
  64. 'coupon_id' => $val['coupon_id'],
  65. 'number' => $val['number'],
  66. ];
  67. }
  68. if(!empty($recharge_gift)){
  69. $rs_gift = Db::name('recharge_gift')->insertAll($recharge_gift);
  70. if($rs_gift === false){
  71. Db::rollback();
  72. $this->error('添加失败');
  73. }
  74. }
  75. }
  76. Db::commit();
  77. $this->success('添加成功');
  78. }
  79. //上下架
  80. public function changestatus(){
  81. //验证
  82. if($this->auth->type != 1){
  83. $this->error('只有门店老板才能设置');
  84. }
  85. $id = input('id',0);
  86. $info = Db::name('recharge_config')->where('id',$id)->update(['status'=>0]);
  87. $this->success();
  88. }
  89. //删除
  90. public function delete(){
  91. //验证
  92. if($this->auth->type != 1){
  93. $this->error('只有门店老板才能设置');
  94. }
  95. $id = input('id','');
  96. $check = Db::name('recharge_config')->where('id',$id)->where('company_id',$this->auth->company_id)->find();
  97. if(empty($check)){
  98. $this->error('不存在的储值卡');
  99. }
  100. Db::name('recharge_config')->where('id',$id)->delete();
  101. Db::name('recharge_gift')->where('config_id',$id)->delete();
  102. $this->success('删除成功');
  103. }
  104. }