Order.php 15.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
<?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="water_num", type="integer", required=true, description="使用氧气泡泡【新增参数】")
     * @ApiParams   (name="tick_id", type="integer", required=true, description="优惠券ID 未使用传0 【新增参数】")
     * @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());
//        //是否首次下单
//        $FirstPay = Db::name('order')->where('user_id', $UserId)->where('status', 'IN', '1,2,3,4,5')->find();
//        if (empty($FirstPay)) {
//            Db::name('user')->where('id', $UserId)->update(
//                [
//                    'is_new' => 1,
//                    'updatetime' => time()
//                ]
//            );
//        }
        $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 = $params['total'];
        if ($params['tick_id'] != 0) {
            //更改优惠券
            Db::name('user_tick')->where('user_id', $UserId)->where('id', $params['tick_id'])->update(
                [
                    'status' => 1,
                    'updatetime' => time()
                ]
            );
            $tick = '是';
        } else {
            $tick = '否';
        }
        if ($params['water_num'] != 0) {
            //用户消耗氧气泡泡
            $WaterUserNum = Db::name('user')->where('id', $UserId)->find();
            if ($WaterUserNum['num'] < $params['water_num']) {
                $this->error('用户积分不足', 0);
            }
            //更改用户积分
            Db::name('user')->where('id', $UserId)->update(['num' => $WaterUserNum['num'] - $params['water_num']]);
            //添加用户积分记录
            $WaterUserNumPay = [
                'user_id' => $UserId,
                'status' => 0,
                'type' => 11,
                'num' => $params['water_num'],
                'up_num' => $WaterUserNum['num'] - $params['water_num'],
                'createtime' => time()
            ];
            Db::name('num')->insert($WaterUserNumPay);
        }
        $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,
            'water_num' => $params['water_num'],
            'tick_id' => $params['tick_id'],
            'tick' => $tick,
            '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('status', 'NEQ', 6)
            ->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();
        if ($params['status'] == 6) {
            $OrderInfo = Db::name('order')->where('user_id', $UserId)->where('OrderSn', $params['OrderSn'])->find();
            if ($OrderInfo['you'] > 0) {
                Db::name('user')->where('id', $UserId)->update(
                    [
                        'is_new' => 1
                    ]
                );
            }
            //判断 待支付订单 取消 不减少积分
            $UserNum = Db::name('user')->where('id', $UserId)->find();
            if ($OrderInfo['status'] != 0) {
                //减少积分
                Db::name('user')->where('id', $UserId)->update(['num' => $UserNum['num'] - floor($OrderInfo['total']) + $OrderInfo['water_num']]);
                //13=订单退款积分扣除
                $NumData = [
                    'user_id' => $UserId,
                    'status' => 0,
                    'type' => 13,
                    'num' => floor($OrderInfo['total']),
                    'up_num' => $UserNum['num'] - floor($OrderInfo['total']) + $OrderInfo['water_num'],
                    'createtime' => time()
                ];
            } else {
                //消耗积分归还
                Db::name('user')->where('id', $UserId)->update(['num' => $UserNum['num'] + $OrderInfo['water_num']]);
                //12=订单退还积分
                $NumData = [
                    'user_id' => $UserId,
                    'status' => 1,
                    'type' => 12,
                    'num' => $OrderInfo['water_num'],
                    'up_num' => $UserNum['num'] + $OrderInfo['water_num'],
                    'createtime' => time()
                ];
            }
            if ($OrderInfo['water_num'] != 0) {
                Db::name('num')->insert($NumData);
            }
            //优惠券归还
            if ($OrderInfo['tick_id'] != 0) {
                Db::name('user_tick')->where('id', $OrderInfo['tick_id'])->update(['status' => 0]);
            }
        } else {
            //更改上级活动钱包减少待入账 增加待提现
            $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]);
        }
        $res = Db::name('order')->where('user_id', $UserId)->where('OrderSn', $params['OrderSn'])->update(
            [
                'status' => $params['status']
            ]
        );
        $this->success('成功', 1);
    }


    /**
     * 订单板块接口
     * @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();
        $Oid = $GuanYiCloud->GetOrderOid($params['OrderSn']);
        $GuanYiCloud->OrderUpdate($params['OrderSn'], $Oid);
        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);
    }
}