MobileOrder.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\model\MobileOrderOperation;
  5. use app\common\service\Refund;
  6. use think\Db;
  7. /**
  8. *
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class MobileOrder extends Backend
  13. {
  14. /**
  15. * MobileOrder模型对象
  16. * @var \app\admin\model\MobileOrder
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\MobileOrder;
  23. $this->assign('status',\app\common\model\MobileOrder::$status);
  24. }
  25. public function import()
  26. {
  27. parent::import();
  28. }
  29. /**
  30. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  31. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  32. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  33. */
  34. /**
  35. * 查看
  36. */
  37. public function index()
  38. {
  39. //当前是否为关联查询
  40. $this->relationSearch = false;
  41. //设置过滤方法
  42. $this->request->filter(['strip_tags', 'trim']);
  43. if ($this->request->isAjax()) {
  44. //如果发送的来源是Selectpage,则转发到Selectpage
  45. if ($this->request->request('keyField')) {
  46. return $this->selectpage();
  47. }
  48. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  49. $list = $this->model
  50. ->with(['info','operation','operation.admin'])
  51. ->where($where)
  52. ->order($sort, $order)
  53. ->paginate($limit);
  54. foreach ($list as $row) {
  55. $row['pay_link']=request()->domain()."/build/#/oder-detail/{$row['id']}";
  56. }
  57. $extend=[];
  58. $extend['total']=$this->model->where($where)->sum('amount');
  59. $extend['alipay']=$this->model->where($where)->where('pay_type',1)->sum('amount');
  60. $extend['wechat']=$this->model->where($where)->where('pay_type',2)->sum('amount');
  61. $extend['jd']=$this->model->where($where)->where('pay_type',3)->sum('amount');
  62. $extend['di']=$this->model->where($where)->sum('amount_di');
  63. $extend['profit']=$this->model->where($where)->sum('amount-amount_di');
  64. $extend['refund']=$this->model->where($where)->sum('amount_refund');
  65. $result = array("total" => $list->total(), "rows" => $list->items(),'extend'=>$extend);
  66. return json($result);
  67. }
  68. return $this->view->fetch();
  69. }
  70. public function edit($ids = null)
  71. {
  72. $model=$this->model->find($ids);
  73. $row=$model->toArray();
  74. if($this->request->isGet()){
  75. $row['address']=$model->originData()['address'];
  76. $row['city']=\app\common\model\Area::getNameString($row['city'],'/');
  77. $this->assign('row',$row);
  78. return view();
  79. }else{
  80. $data=input('row/a');
  81. if(isset($data['city'])){
  82. $data['city']=\app\common\model\Area::whereIn('name|shortname',str_replace('/',',',$data['city']))->column('id');
  83. }
  84. $this->validate($data,[
  85. 'status'=>'in:'.implode(',',array_keys(\app\common\model\MobileOrder::$status))
  86. ]);
  87. foreach ($data as $key=>$value){
  88. $model[$key]=$value;
  89. }
  90. $model->save();
  91. $this->success('');
  92. }
  93. }
  94. public function refund($ids){
  95. $model=$this->model->find($ids);
  96. $this->assign('row',$model);
  97. if($this->request->isGet()){
  98. return view();
  99. }else{
  100. $data=input('row/a');
  101. $this->validate($data,[
  102. 'amount|金额'=>'require|number|egt:0',
  103. ]);
  104. Db::startTrans();
  105. $model=$this->model->where('id',$ids)->lock(true)->findOrFail();
  106. if($data['amount']>$model['amount']){
  107. $this->error('退款金额不能大于付款金额');
  108. }
  109. $model['amount_refund']=$data['amount'];
  110. $model['refund_no']=session_create_id();
  111. Refund::setType($model)->refund();
  112. $model->save();
  113. Db::commit();
  114. $this->success();
  115. }
  116. }
  117. public function status(){
  118. return \app\common\model\MobileOrder::$status;
  119. }
  120. public function pay_type(){
  121. return \app\common\model\MobileOrder::$payTypes;
  122. }
  123. public function add_operation(){
  124. $id=input('ids/d');
  125. if($this->request->isGet()){
  126. return view();
  127. }else{
  128. $content=input('row.content');
  129. $this->validate(compact('content'),[
  130. 'content'=>'max:250',
  131. ]);
  132. MobileOrderOperation::create([
  133. 'mobile_order_id'=>$id,
  134. 'admin_id'=>$this->auth->id,
  135. 'content'=>$content,
  136. ]);
  137. $this->success();
  138. }
  139. }
  140. }