Packagemove.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 套餐转让申请
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Packagemove extends Backend
  11. {
  12. /**
  13. * Packagemove模型对象
  14. * @var \app\admin\model\Packagemove
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\Packagemove;
  21. $this->view->assign("statusList", $this->model->getStatusList());
  22. }
  23. /**
  24. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  25. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  26. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  27. */
  28. /**
  29. * 查看
  30. */
  31. public function index()
  32. {
  33. //当前是否为关联查询
  34. $this->relationSearch = true;
  35. //设置过滤方法
  36. $this->request->filter(['strip_tags', 'trim']);
  37. if ($this->request->isAjax()) {
  38. //如果发送的来源是Selectpage,则转发到Selectpage
  39. if ($this->request->request('keyField')) {
  40. return $this->selectpage();
  41. }
  42. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  43. $list = $this->model
  44. ->with(['usera','order','userb'])
  45. ->where($where)
  46. ->order($sort, $order)
  47. ->paginate($limit);
  48. foreach ($list as $row) {
  49. $row->getRelation('usera')->visible(['firstname','lastname']);
  50. $row->getRelation('order')->visible(['order_no']);
  51. $row->getRelation('userb')->visible(['firstname','lastname']);
  52. }
  53. $result = array("total" => $list->total(), "rows" => $list->items());
  54. return json($result);
  55. }
  56. return $this->view->fetch();
  57. }
  58. /**
  59. * 审核
  60. */
  61. public function audit(){
  62. $id = input('id');
  63. $info = Db::name('package_move')
  64. ->where('id',$id)
  65. ->find();
  66. if ($this->request->isPost()) {
  67. $params = $this->request->post('row/a');
  68. $status = input('status',0);
  69. $data = [
  70. 'status' => $status,
  71. 'auditremark' => input('auditremark',''),
  72. 'audittime' => time(),
  73. 'updatetime' => time(),
  74. ];
  75. $data = array_merge($params,$data);
  76. Db::startTrans();
  77. $rs = Db::name('package_move')->where('id',$id)->update($data);
  78. if($rs === false){
  79. Db::rollback();
  80. $this->error('保存失败');
  81. }
  82. if($status == 1){
  83. //老订单
  84. $package_info = Db::name('package_order')->where('id',$info['package_order_id'])->lock(true)->find();
  85. //剩余课时判断
  86. $new_remain = $package_info['remain'] - $info['sessions'];
  87. if($new_remain < 0){
  88. Db::rollback();
  89. $this->error('原套餐订单的剩余课程已不足转出');
  90. }
  91. //老订单修改
  92. $package_update = [
  93. 'remain' => $new_remain,
  94. 'updatetime' => time(),
  95. ];
  96. $order_rs = Db::name('package_order')->where('id',$info['package_order_id'])->update($package_update);
  97. if($order_rs === false){
  98. Db::rollback();
  99. $this->error('转让失败');
  100. }
  101. //生成新订单
  102. $new_order = $package_info;
  103. unset($new_order['id']);
  104. $new_order['order_no'] = 'Z'.$package_info['order_no'];//转让订单特殊订单号
  105. $new_order['user_id'] = $info['to_user_id'];
  106. $new_order['sessions'] = $info['sessions'];
  107. $new_order['remain'] = $info['sessions'];
  108. $new_order['createtime'] = time();
  109. $new_order['updatetime'] = time();
  110. $new_order['is_gift'] = 0;
  111. $new_order['notice_status'] = 0;
  112. $new_order['remark'] = '来自转让';
  113. $order_id = Db::name('package_order')->insertGetId($new_order);
  114. if(!$order_id){
  115. Db::rollback();
  116. $this->error('转让失败');
  117. }
  118. Db::commit();
  119. $this->success('转让成功');
  120. }elseif($status == 2){
  121. Db::commit();
  122. $this->success('审核完成');
  123. }
  124. //多余
  125. Db::commit();
  126. $this->success('修改完成');
  127. }
  128. $this->assign('row',$info);
  129. return $this->view->fetch();
  130. }
  131. }