ShopDelivery.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. //,'shops', 'variety', 'specs'
  20. $this->relationField=['customer','user', 'shops'];
  21. }
  22. /**
  23. * 查看
  24. */
  25. #[Route('GET,JSON','index')]
  26. public function index()
  27. {
  28. if (false === $this->request->isAjax()) {
  29. return $this->fetch();
  30. }
  31. if($this->request->post('selectpage')){
  32. return $this->selectpage();
  33. }
  34. [$where, $order, $limit, $with] = $this->buildparams();
  35. $list = $this->model
  36. ->withJoin($with,'left')
  37. //如果没有使用operate filter过滤的情况下,推荐使用with关联,可以提高查询效率
  38. // ->with($with)
  39. ->where($where)
  40. ->order($order)
  41. ->paginate($limit);
  42. // $list = $this->model
  43. // ->alias('d')
  44. // ->join('user u',"d.user_id = u.id", "LEFT")
  45. // ->field('d.*,COALESCE(u.nickname, "无") as nickname')
  46. // ->where($where)
  47. // ->order($order)
  48. // ->paginate($limit);
  49. //表格底部要有汇总:发货数量汇总 重量汇总 总价汇总
  50. $count = $this->model
  51. ->withJoin($with,'left')
  52. ->where($where)
  53. ->field('sum(num) total_num, sum(weigh) total_weigh, sum(total_price) total_price')
  54. // ->group('shop_delivery.id')
  55. ->select()
  56. ->toArray();
  57. $result = ['total' => $list->total(), 'rows' => $list->items(),
  58. 'total_num' => $count[0]['total_num']??0,
  59. 'total_weigh' => $count[0]['total_weigh']??0,
  60. 'total_price' => $count[0]['total_price']??0];
  61. return json($result);
  62. }
  63. /**
  64. * 结算
  65. */
  66. #[Route('GET,POST','settlement')]
  67. public function settlement()
  68. {
  69. $ids =$this->request->post('ids/a', []);
  70. if(empty($ids)) return resp_json(0, '请选着要结算记录');
  71. $result = false;
  72. Db::startTrans();
  73. try {
  74. $result = $this->model->whereIn('id', $ids)->save(['status'=>$this->model::StatusSettlement]);
  75. if($this->callback){
  76. $callback=$this->callback;
  77. $callback($this->model);
  78. }
  79. Db::commit();
  80. } catch (\Exception $e) {
  81. Db::rollback();
  82. $this->error($e->getMessage());
  83. }
  84. if ($result === false) {
  85. $this->error(__('没有新增任何数据'));
  86. }
  87. return resp_json(200,'操作成功');
  88. }
  89. }