ShopOrderLogic.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace app\admin\logic;
  3. use app\admin\model\dao\ShopGoods;
  4. use app\admin\model\dao\ShopOrder;
  5. use app\admin\model\dao\UserAddress;
  6. use think\facade\Db;
  7. class ShopOrderLogic
  8. {
  9. // 0待付款 1待发货 2已发货 3取消订单 4.已完成
  10. private static $statusMap = [
  11. ['id' => 0, 'name' => '待付款'],
  12. ['id' => 1, 'name' => '待发货'],
  13. // ['id' => 2, 'name' => '已发货'],
  14. ['id' => 3, 'name' => '取消订单'],
  15. // ['id' => 4, 'name' => '已完成']
  16. ];
  17. /**
  18. * @return array[]
  19. */
  20. public static function getStatusMap(): array
  21. {
  22. return self::$statusMap;
  23. }
  24. public static function editStatus($orderId, $status)
  25. {
  26. $shopOrder = ShopOrder::getOrderById($orderId);
  27. if (empty($shopOrder)) {
  28. return "订单不存在";
  29. }
  30. if ($shopOrder['status'] != $status) {
  31. Db::startTrans();
  32. try {
  33. $result = ShopOrder::editStatus($orderId, $status);
  34. if (!$result) {
  35. Db::rollback();
  36. return "订单更新状态失败";
  37. }
  38. Db::commit();
  39. } catch (\Exception $exception) {
  40. Db::rollback();
  41. return "失败:" . $exception->getMessage();
  42. }
  43. }
  44. return true;
  45. }
  46. public static function getList($page, $limit, $where)
  47. {
  48. $model = new \app\common\model\ShopOrder();
  49. foreach ($where as $key => $val) {
  50. if ($val[0] == 'status') {
  51. $where[$key][0] = 'o.status';
  52. }
  53. if ($val[0] == 'created_time') {
  54. $where[$key][0] = 'o.created_time';
  55. $where[$key][2] = sr_getcurtime($where[$key][2]);
  56. }
  57. if ($val[0] == 'order_type') {
  58. $where[$key][0] = 'o.order_type';
  59. }
  60. }
  61. $count = $model
  62. ->alias('o')
  63. ->with(['goods'])
  64. ->where($where)
  65. // ->where('o.status', '<>', 3)
  66. ->leftJoin('user u', 'u.id = o.user_id')
  67. ->leftJoin('shop_order_shipping s', 's.order_id = o.order_id')
  68. ->field('o.*,u.mobile,s.sp_id,s.sp_name,s.sp_mergename,s.sp_mobile')
  69. ->count();
  70. $list = $model
  71. ->alias('o')
  72. ->with(['goods'])
  73. ->where($where)
  74. // ->where('o.status', '<>', 3)
  75. ->leftJoin('user u', 'u.id = o.user_id')
  76. ->leftJoin('shop_order_shipping s', 's.order_id = o.order_id')
  77. ->field('o.*,u.mobile,s.sp_id,s.sp_name,s.sp_mergename,s.sp_mobile')
  78. ->order('o.order_id desc')
  79. ->withAttr('order_sn', function ($val, $data) {
  80. return '编号:' . $val;
  81. })
  82. ->page($page, $limit)
  83. ->select()->toArray();
  84. foreach ($list as $k => $v) {
  85. $goods_id = $v['goods'][0]['goods_id'];
  86. $goods_info = ShopGoods::getGoodsById($goods_id);
  87. if ($goods_info) {
  88. $list[$k]['cost_price'] = $goods_info['cost_price'];
  89. } else {
  90. $list[$k]['cost_price'] = 0;
  91. }
  92. if ($v['status'] == 5 || $v['status'] == 6) {
  93. // 新的地址
  94. $ygOrderInfo = Db::name('yg_order')->where('order_sn', str_replace('编号:', '', $v['order_sn']))->find();
  95. $newAddress_info = UserAddress::getUserAddress($ygOrderInfo['address_id']);
  96. $list[$k]['sp_id'] = 99999;
  97. $list[$k]['sp_mobile'] = $newAddress_info['mobile'];
  98. $list[$k]['sp_name'] = $newAddress_info['name'];
  99. $list[$k]['sp_mergename'] = $newAddress_info['mergename'];
  100. }
  101. }
  102. return [$count, $list];
  103. }
  104. }