Order.php 11.8 KB
<?php
/**
 * Created by PhpStorm.
 * User: 86132
 * Date: 2020/11/18
 * Time: 10:07
 */

namespace app\api\controller;


use app\common\controller\Api;
use think\Db;

/**
 * 订单板块接口
 */
class Order extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 订单板块接口
     * @ApiTitle    (生成订单)
     * @ApiSummary  (生成订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Order/InsertOrder)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="address_id", type="integer", required=true, description="地址ID")
     * @ApiParams   (name="json", type="integer", required=true, description="json Goods_id:1,num:10")
     * @ApiParams   (name="total", type="integer", required=true, description="总价")
     * @ApiParams   (name="shopcar_id", type="integer", required=true, description="购物车ID ,拼接成字符串 直接购买购物车ID 传0")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": // 返回订单号 调用支付
    })
     */
    public function InsertOrder()
    {
        $UserId = $this->is_token($this->request->header());
        $params = $this->request->param();
        //Json 转 Array
        $JsonArray = json_decode(htmlspecialchars_decode($params['json']), true);
        //检查商品库存
        foreach ($JsonArray as $k => $v) {
            $this->StockLook($v['Goods_id'], $v['num']);
        }
        /*添加商品到订单*/
        //查询地址
        $AddressArray = Db::name('address')->where('user_id', $UserId)->where('id', $params['address_id'])->find();
        if (empty($AddressArray)) {
            $this->error('地址获取失败', 0);
            die;
        }
        //生成订单号
        $OrderSn = $this->order_sn();
        //计算订单总价
        $Total = $this->Total($UserId, $params['total']);
        $data = [
            'OrderSn' => $OrderSn,
            'user_id' => $UserId,
            'name' => $AddressArray['name'],
            'mobile' => $AddressArray['mobile'],
            'city' => $AddressArray['city'],
            'address' => $AddressArray['address'],
            'address_con' => $AddressArray['address_con'],
            'status' => 0,
            'you' => $params['total'] - $Total,
            'total' => $Total,
            'createtime' => time(),
            'updatetime' => time()
        ];
        $InserOrder = Db::name('order')->insert($data);
        if (!$InserOrder) {
            $this->error('订单生成失败', 0);
            die;
        }
        /*添加商品到订单详情*/
        foreach ($JsonArray as $k => $v) {
            $InserOrderCon = Db::name('order_con')->insert(
                [
                    'OrderSn' => $OrderSn,
                    'Goods_id' => $v['Goods_id'],
                    'num' => $v['num'],
                    'createtime' => time(),
                    'updatetime' => time()
                ]
            );
            if (!$InserOrderCon) {
                $this->error('订单详情生成失败', 0);
                die;
            }
        }
        /*删除购物车*/
        if ($params['shopcar_id'] != 0) {
            if (strstr($params['shopcar_id'], ',')) {
                $ShopCarID = explode(',', $params['shopcar_id']);
            } else {
                $ShopCarID = explode(' ', $params['shopcar_id']);
            }
            foreach ($ShopCarID as $k => $v) {
                $DeleteShopCar = Db::name('shopcar')->where('user_id', $UserId)->where('id', $v)->delete();
                if (!$DeleteShopCar) {
                    $this->error('购物车删除失败', 0);
                    die;
                }
            }
        }
        //查询新用户优惠
        $UserInfo = Db::name('user')->where('id', $UserId)->find();
        if ($UserInfo['is_new'] == 1) {
            Db::name('user')->where('id', $UserId)->update([
                'is_new' => 0
            ]);
        }
        //返回订单号
        $this->success('成功', $OrderSn);
    }


    /**
     * 订单板块接口
     * @ApiTitle    (我的订单)
     * @ApiSummary  (我的订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Order/MyOrder)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="status", type="integer", required=true, description="订单状态:0=待支付,1=待发货,2=待收货,3=已完成,4=退款/售后,5=退款/售后完成,6=已取消,9=全部")
     * @ApiParams   (name="pages", type="integer", required=true, description="pages")
     * @ApiParams   (name="rows", type="integer", required=true, description="rows")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": // 返回订单号 调用支付
    })
     */
    public function MyOrder()
    {
        $UserId = $this->is_token($this->request->header());
        $params = $this->request->param();
        $map = [];
        if ($params['status'] != 9) {
            $map['status'] = ['EQ', $params['status']];
        }
        $OrderArray = Db::name('order')
            ->where('user_id', $UserId)
            ->where($map)
            ->order('updatetime desc')
            ->select();
        if (empty($OrderArray)) {
            $data = [
                'Count' => 0,
                'List' => []
            ];
        } else {
            foreach ($OrderArray as $k => $v) {
                $List[$k]['Id'] = $v['id'];
                $List[$k]['OrderSn'] = $v['OrderSn'];
                $List[$k]['Status'] = $v['status'];
                //支付时间
                $PayTime = Db::name('pay_order')->where('user_id', $UserId)->where('OrderSn', $v['OrderSn'])->find();
                $List[$k]['PayTime'] = $PayTime['pay_time'];
                $List[$k]['Total'] = $v['total'];
                $OrderConArray = Db::name('order_con')
                    ->alias('o')
                    ->where('o.OrderSn', $v['OrderSn'])
                    ->join('goods g', 'g.id=o.Goods_id')
                    ->field('o.Goods_id,g.title,g.image,g.speci,g.price,o.num')
                    ->select();
                if (empty($OrderConArray)) {
                    $this->error('订单拉取失败', 0);
                } else {
                    foreach ($OrderConArray as $key => $value) {
                        $List[$k]['GoodsArray'][$key]['Goods_id'] = $value['Goods_id'];
                        $List[$k]['GoodsArray'][$key]['Title'] = $value['title'];
                        $List[$k]['GoodsArray'][$key]['Image'] = cdnurl($value['image']);
                        $List[$k]['GoodsArray'][$key]['Speci'] = $value['speci'];
                        $List[$k]['GoodsArray'][$key]['Price'] = $value['price'];
                        $List[$k]['GoodsArray'][$key]['Num'] = $value['num'];
                    }
                }
            }
            $data = [
                'Count' => count($OrderArray),
                'List' => $this->page_array($params['rows'], $params['pages'], $List, 0)
            ];
        }
        $this->success('成功', $data);
    }


    /**
     * 订单板块接口
     * @ApiTitle    (操作订单)
     * @ApiSummary  (操作订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Order/OrderCao)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="OrderSn", type="integer", required=true, description="订单号")
     * @ApiParams   (name="status", type="integer", required=true, description="6=取消订单,3=确认收货")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data":
    })
     */
    public function OrderCao()
    {
        $UserId = $this->is_token($this->request->header());
        $params = $this->request->param();
        $res = Db::name('order')->where('user_id', $UserId)->where('OrderSn', $params['OrderSn'])->update(
            [
                'status' => $params['status']
            ]
        );
        //更改上级活动钱包减少待入账 增加待提现
        $FuserId = Db::name('group')->where('user_id', $UserId)->value('fuser_id');
        if (!empty($FuserId)) {
            $HuodongMoney = Db::name('huodong_money')->where('user_id', $FuserId)->find();
            $Total = Db::name('order')->where('OrderSn', $params['OrderSn'])->value('total');
            $res2 = Db::name('huodong_money')->where('user_id', $FuserId)->update(
                [
                    'befor_money' => $HuodongMoney['befor_money'] - $Total,
                    'befor_tixian' => $HuodongMoney['befor_tixian'] + $Total
                ]
            );
            if (!$res2) {
                $this->error('更改上级钱包失败', 0);
                die;
            }
        }
        //更改收益为已入账
        Db::name('money')->where('OrderSn', $params['OrderSn'])->update(['status' => 1]);
        $this->res($res);
    }


    /**
     * 订单板块接口
     * @ApiTitle    (申请售后)
     * @ApiSummary  (申请售后)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Order/Tuikuan)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="OrderSn", type="integer", required=true, description="订单号")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data":
    })
     */
    public function Tuikuan()
    {
        $UserId = $this->is_token($this->request->header());
        $params = $this->request->param();
        $data = [
            'user_id' => $UserId,
            'OrderSn' => $params['OrderSn'],
            'createtime' => time()
        ];
        $res = Db::name('tuikuan')->insert($data);
        if (!$res) {
            $this->error('申请失败', 0);
            die;
        }
        //修改订单状态
        $res2 = Db::name('order')->where('OrderSn', $params['OrderSn'])->update(
            [
                'status' => 4,
                'updatetime' => time()
            ]
        );
        $GuanYiCloud = new GuanYiCloud();
        $GuanYiCloud->GetOrderOid($params['OrderSn']);
        if (!$res2) {
            $this->error('订单状态更改失败', 0);
            die;
        } else {
            $this->success('成功', 1);
        }
    }


    /**
     * 订单板块接口
     * @ApiTitle    (物流查询)
     * @ApiSummary  (物流查询)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Order/LogisticsInquiry)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="OrderSn", type="integer", required=true, description="订单号")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data":
    })
     */
    public function LogisticsInquiry()
    {
        $this->is_token($this->request->header());
        $params = $this->request->param();
        $data = $this->kuaidi100($params['OrderSn']);
        Db::name('order')->where('OrderSn', $params['OrderSn'])->value('ems_company');
        $this->success('成功', $data);
    }
}