Bind.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\admin\controller;
  3. class Bind extends Admin
  4. {
  5. public function index()
  6. {
  7. $where = [];
  8. $orderid = $this->request->get('orderid', '', 'trim');
  9. if (isset($orderid) && !empty($orderid)) {
  10. $where['orderid'] = $orderid;
  11. }
  12. $order = 'id desc';
  13. $couponModel = model('Bind');
  14. $list = $couponModel
  15. ->where($where)
  16. ->order($order)
  17. ->paginate(10, false, ['query' => input('get.')]);
  18. $this->assign('list', $list);
  19. $this->assign('page', $list->render());
  20. return $this->fetch();
  21. }
  22. public function add()
  23. {
  24. if ($this->request->isPost()) {
  25. $data = $this->request->post();
  26. $ids = explode("\r\n", $data['ids']);
  27. $ids = array_map('filterAndTrimInput', $ids);
  28. $ids = array_unique(array_filter($ids));
  29. $pay = model('Pay')->alias('a')->whereIn('a.orderid', $ids)
  30. ->join('nw_game_channel_divide_settle b', 'a.settle_id=b.id', 'left')
  31. ->where('a.channel_id', $data['old_channel_id'])
  32. ->where('a.status', 1)->column('a.id,a.channel_id,a.settle_id,a.orderid,b.first_audit_status,b.second_audit_status', 'a.orderid');
  33. if (!$channel = model('Channel')->where('id', $data['new_channel_id'])->where('level', 3)->where('status', 1)->find()) {
  34. $this->error('记录不存在');
  35. }
  36. $arr = [];
  37. $orderids = [];
  38. foreach ($ids as $k => $v) {
  39. if (isset($pay[$v])) {
  40. if ($pay[$v]['settle_id'] > 0) {
  41. if ($pay[$v]['first_audit_status'] == 1 && $pay[$v]['second_audit_status'] == 1) {
  42. $arr[] = [
  43. 'orderid' => $v,
  44. 'name' => '',
  45. 'state' => '<span style="color: red">订单已结算无法换绑</span>'
  46. ];
  47. } else {
  48. $arr[] = [
  49. 'orderid' => $v,
  50. 'name' => '',
  51. 'state' => '<span style="color: red">订单已申请结算,请先拒绝结算后再换绑</span>'
  52. ];
  53. }
  54. } else {
  55. $orderids[] = $v;
  56. $arr[] = [
  57. 'orderid' => $v,
  58. 'name' => $channel['name'],
  59. 'state' => '<span style="color: #0bb20c">可换绑</span>'
  60. ];
  61. }
  62. } else {
  63. $arr[] = [
  64. 'orderid' => $v,
  65. 'name' => '',
  66. 'state' => '<span style="color: red">订单不存在</span>'
  67. ];
  68. }
  69. }
  70. $this->assign('orderids', implode(',', $orderids));
  71. $this->assign('channel', $channel);
  72. $this->assign('list', $arr);
  73. $this->assign('old_channel_id', $data['old_channel_id']);
  74. return $this->fetch('confirm');
  75. }
  76. $channel = model('Channel')->where('level', 3)->where('status', 1)
  77. ->field('id,name')
  78. ->order('id desc')->select();
  79. $this->assign('channel', $channel);
  80. return $this->fetch();
  81. }
  82. public function confirm()
  83. {
  84. $data = $this->request->post();
  85. if (!$data['orderids']) {
  86. $this->error('订单不存在');
  87. }
  88. if (!$data['new_channel_id']) {
  89. $this->error('新推广关系不存在');
  90. }
  91. $channel = model('Channel')->where('id', $data['new_channel_id'])->where('level', 3)->where('status', 1)->find();
  92. if (!$channel) {
  93. $this->error('推广关系不存在');
  94. }
  95. $old_channel = model('Channel')->where('id', $data['old_channel_id'])->where('level', 3)->where('status', 1)->find();
  96. if (!$old_channel) {
  97. $this->error('推广关系不存在');
  98. }
  99. $orderids = array_map('filterAndTrimInput', explode(',', $data['orderids']));
  100. $orderids = array_unique(array_filter($orderids));
  101. $pay = model('Pay')->alias('a')->whereIn('a.orderid', $orderids)
  102. ->where('a.channel_id', $data['old_channel_id'])
  103. ->where('a.status', 1)->column('a.id,a.channel_id,a.settle_id,a.orderid', 'a.orderid');
  104. if (!$pay) {
  105. $this->error('订单不存在');
  106. }
  107. $bind = [];
  108. $bind_order = [];
  109. foreach ($pay as $k => $v) {
  110. if ($v['settle_id'] == 0) {
  111. $bind_order[] = $v['orderid'];
  112. $bind[] = [
  113. 'orderid' => $v['orderid'],
  114. 'old_channel_id' => $v['channel_id'],
  115. 'new_channel_id' => $data['new_channel_id'],
  116. 'old_channel_name' => $old_channel['name'],
  117. 'new_channel_name' => $channel['name'],
  118. 'create_time' => time(),
  119. 'admin_name' => session('USERNAME')
  120. ];
  121. }
  122. }
  123. if(!$bind_order){
  124. $this->error('订单不存在');
  125. }
  126. model('Common/Pay')->startTrans();
  127. if (!model('Common/Pay')->whereIn('orderid', $bind_order)
  128. ->where('channel_id', $data['old_channel_id'])->update(['channel_id' => $data['new_channel_id']])) {
  129. model('Common/Pay')->rollback();
  130. $this->error('绑定失败');
  131. }
  132. if (model('common/Bind')->insertAll($bind)) {
  133. model('Common/Pay')->commit();
  134. $this->success('绑定成功', url('index'));
  135. } else {
  136. model('Common/Pay')->rollback();
  137. $this->error('绑定失败');
  138. }
  139. }
  140. }