ShopDelivery.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller\shop;
  4. use app\common\controller\Backend;
  5. use think\annotation\route\Group;
  6. use app\admin\traits\Actions;
  7. use think\annotation\route\Route;
  8. use think\facade\Db;
  9. use app\common\model\ShopDelivery as ShopDeliveryModel;
  10. #[Group("shop/shop_delivery")]
  11. class ShopDelivery extends Backend
  12. {
  13. use Actions;
  14. protected function _initialize()
  15. {
  16. parent::_initialize();
  17. $this->model = new ShopDeliveryModel();
  18. $this->assign('platformList', site_config('addonsd.platform_list'));
  19. $this->relationField=['customer','user','shops', 'variety', 'specs'];
  20. }
  21. /**
  22. * 查看
  23. */
  24. #[Route('GET,JSON','index')]
  25. public function index()
  26. {
  27. if (false === $this->request->isAjax()) {
  28. return $this->fetch();
  29. }
  30. if($this->request->post('selectpage')){
  31. return $this->selectpage();
  32. }
  33. [$where, $order, $limit, $with] = $this->buildparams();
  34. $list = $this->model
  35. ->withJoin($with,'left')
  36. //如果没有使用operate filter过滤的情况下,推荐使用with关联,可以提高查询效率
  37. //->with($with)
  38. ->where($where)
  39. ->order($order)
  40. ->paginate($limit);
  41. //表格底部要有汇总:发货数量汇总 重量汇总 总价汇总
  42. $count = $this->model
  43. ->withJoin($with,'left')
  44. ->where($where)
  45. ->field('sum(num) total_num, sum(weigh) total_weigh, sum(total_price) total_price')
  46. ->group('shop_delivery.id')
  47. ->select()
  48. ->toArray();
  49. $result = ['total' => $list->total(), 'rows' => $list->items(),
  50. 'total_num' => $count[0]['total_num']??0,
  51. 'total_weigh' => $count[0]['total_weigh']??0,
  52. 'total_price' => $count[0]['total_price']??0];
  53. return json($result);
  54. }
  55. /**
  56. * 结算
  57. */
  58. #[Route('GET,POST','settlement')]
  59. public function settlement()
  60. {
  61. $ids =$this->request->post('ids/a', []);
  62. if(empty($ids)) return resp_json(0, '请选着要结算记录');
  63. $result = false;
  64. Db::startTrans();
  65. try {
  66. $result = $this->model->whereIn('id', $ids)->save(['status'=>$this->model::StatusSettlement]);
  67. if($this->callback){
  68. $callback=$this->callback;
  69. $callback($this->model);
  70. }
  71. Db::commit();
  72. } catch (\Exception $e) {
  73. Db::rollback();
  74. $this->error($e->getMessage());
  75. }
  76. if ($result === false) {
  77. $this->error(__('没有新增任何数据'));
  78. }
  79. return resp_json(200,'操作成功');
  80. }
  81. }