ShopDelivery.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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->withJoin($with,'left')->where($where)->field('sum(num) total_num, sum(weigh) total_weigh, sum(total_price) total_price')->select()->toArray();
  43. $result = ['total' => $list->total(), 'rows' => $list->items(),
  44. 'total_num' => $count[0]['total_num']??0,
  45. 'total_weigh' => $count[0]['total_weigh']??0,
  46. 'total_price' => $count[0]['total_price']??0];
  47. return json($result);
  48. }
  49. /**
  50. * 结算
  51. */
  52. #[Route('GET,POST','settlement')]
  53. public function settlement()
  54. {
  55. $ids =$this->request->post('ids/a', []);
  56. if(empty($ids)) return resp_json(0, '请选着要结算记录');
  57. $result = false;
  58. Db::startTrans();
  59. try {
  60. $result = $this->model->whereIn('id', $ids)->save(['status'=>$this->model::StatusSettlement]);
  61. if($this->callback){
  62. $callback=$this->callback;
  63. $callback($this->model);
  64. }
  65. Db::commit();
  66. } catch (\Exception $e) {
  67. Db::rollback();
  68. $this->error($e->getMessage());
  69. }
  70. if ($result === false) {
  71. $this->error(__('没有新增任何数据'));
  72. }
  73. return resp_json(200,'操作成功');
  74. }
  75. }