Recharge.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. $field = ['price','giftprice'];
  43. $data = request_post_hub($field);
  44. $data['company_id'] = $this->auth->company_id;
  45. $data['status'] = 1;
  46. Db::startTrans();
  47. $config_id = Db::name('recharge_config')->insertGetId($data);
  48. if(!$config_id){
  49. Db::rollback();
  50. $this->error('添加失败');
  51. }
  52. //赠送卡券
  53. $gift_data = input('gift_data','','trim');
  54. $gift_data = json_decode(htmlspecialchars_decode($gift_data),true);
  55. if(is_array($gift_data) && !empty($gift_data)){
  56. $recharge_gift = [];
  57. foreach($gift_data as $key => $val){
  58. $recharge_gift[] = [
  59. 'config_id' => $config_id,
  60. 'coupon_id' => $val['coupon_id'],
  61. 'number' => $val['number'],
  62. ];
  63. }
  64. if(!empty($recharge_gift)){
  65. $rs_gift = Db::name('recharge_gift')->insertAll($recharge_gift);
  66. if($rs_gift === false){
  67. Db::rollback();
  68. $this->error('添加失败');
  69. }
  70. }
  71. }
  72. Db::commit();
  73. $this->success('添加成功');
  74. }
  75. //上下架
  76. public function changestatus(){
  77. $id = input('id',0);
  78. $info = Db::name('recharge_config')->where('id',$id)->update(['status'=>0]);
  79. $this->success();
  80. }
  81. //删除
  82. public function delete(){
  83. $id = input('id','');
  84. $check = Db::name('recharge_config')->where('id',$id)->where('company_id',$this->auth->company_id)->find();
  85. if(empty($check)){
  86. $this->error('不存在的储值卡');
  87. }
  88. Db::name('recharge_config')->where('id',$id)->delete();
  89. Db::name('recharge_gift')->where('config_id',$id)->delete();
  90. $this->success('删除成功');
  91. }
  92. }