| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- namespace app\admin\logic;
- use app\admin\model\dao\ShopGoods;
- use app\admin\model\dao\ShopOrder;
- use app\admin\model\dao\UserAddress;
- use think\facade\Db;
- class ShopOrderLogic
- {
- // 0待付款 1待发货 2已发货 3取消订单 4.已完成
- private static $statusMap = [
- ['id' => 0, 'name' => '待付款'],
- ['id' => 1, 'name' => '待发货'],
- // ['id' => 2, 'name' => '已发货'],
- ['id' => 3, 'name' => '取消订单'],
- // ['id' => 4, 'name' => '已完成']
- ];
- /**
- * @return array[]
- */
- public static function getStatusMap(): array
- {
- return self::$statusMap;
- }
- public static function editStatus($orderId, $status)
- {
- $shopOrder = ShopOrder::getOrderById($orderId);
- if (empty($shopOrder)) {
- return "订单不存在";
- }
- if ($shopOrder['status'] != $status) {
- Db::startTrans();
- try {
- $result = ShopOrder::editStatus($orderId, $status);
- if (!$result) {
- Db::rollback();
- return "订单更新状态失败";
- }
- Db::commit();
- } catch (\Exception $exception) {
- Db::rollback();
- return "失败:" . $exception->getMessage();
- }
- }
- return true;
- }
- public static function getList($page, $limit, $where)
- {
- $model = new \app\common\model\ShopOrder();
- foreach ($where as $key => $val) {
- if ($val[0] == 'status') {
- $where[$key][0] = 'o.status';
- }
- if ($val[0] == 'created_time') {
- $where[$key][0] = 'o.created_time';
- $where[$key][2] = sr_getcurtime($where[$key][2]);
- }
- if ($val[0] == 'order_type') {
- $where[$key][0] = 'o.order_type';
- }
- }
- $count = $model
- ->alias('o')
- ->with(['goods'])
- ->where($where)
- // ->where('o.status', '<>', 3)
- ->leftJoin('user u', 'u.id = o.user_id')
- ->leftJoin('shop_order_shipping s', 's.order_id = o.order_id')
- ->field('o.*,u.mobile,s.sp_id,s.sp_name,s.sp_mergename,s.sp_mobile')
- ->count();
- $list = $model
- ->alias('o')
- ->with(['goods'])
- ->where($where)
- // ->where('o.status', '<>', 3)
- ->leftJoin('user u', 'u.id = o.user_id')
- ->leftJoin('shop_order_shipping s', 's.order_id = o.order_id')
- ->field('o.*,u.mobile,s.sp_id,s.sp_name,s.sp_mergename,s.sp_mobile')
- ->order('o.order_id desc')
- ->withAttr('order_sn', function ($val, $data) {
- return '编号:' . $val;
- })
- ->page($page, $limit)
- ->select()->toArray();
- foreach ($list as $k => $v) {
- $goods_id = $v['goods'][0]['goods_id'];
- $goods_info = ShopGoods::getGoodsById($goods_id);
- if ($goods_info) {
- $list[$k]['cost_price'] = $goods_info['cost_price'];
- } else {
- $list[$k]['cost_price'] = 0;
- }
- if ($v['status'] == 5 || $v['status'] == 6) {
- // 新的地址
- $ygOrderInfo = Db::name('yg_order')->where('order_sn', str_replace('编号:', '', $v['order_sn']))->find();
- $newAddress_info = UserAddress::getUserAddress($ygOrderInfo['address_id']);
- $list[$k]['sp_id'] = 99999;
- $list[$k]['sp_mobile'] = $newAddress_info['mobile'];
- $list[$k]['sp_name'] = $newAddress_info['name'];
- $list[$k]['sp_mergename'] = $newAddress_info['mergename'];
- }
- }
- return [$count, $list];
- }
- }
|