<?php


namespace app\api\controller;


use addons\epay\library\Service;
use app\api\model\GoodsComment;
use app\api\model\GoodsSpec;
use app\api\model\SpecValue;
use app\api\model\Third;
use app\api\model\UserAddress;
use app\api\model\UserCoupon;
use app\common\controller\Api;
use think\Db;
use think\exception\PDOException;
use Yansongda\Pay\Pay;

/**
 * 订单
 */
class Order extends Api
{
    protected $noNeedLogin = ['friendPay','commentOrderDetail'];
    protected $noNeedRight = ['*'];

    /**
     * @ApiTitle    (订单列表)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="integer", required=true, description="类型1全部2待付款3待发货4待收货5待评价")
     * @ApiParams   (name="page", type="integer", required=true, description="页数")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    'data':
        "total": 1, 总条数
        "per_page": 5, 每页数量
        "current_page": 1, 当前页
        "last_page": 1, 最后一页
        "data": [
            {
            "id": 1, 订单id
            "order_no": "adsadasdas", 订单编号
            "discount_price": "0.00", 优惠金额
            "pay_price": "0.00", 实付价格
            "pay_status": "10", 10未支付20已支付
            "freight_status": "10", 10未发货20已发货
            "receipt_status": "10", 10未收货20已收货
            "order_status": "10", 10进行中20已取消30已完成
            "total_sum": 0, 商品总数量
            "goods": [
                {
                "goods_name": "asdasd", 商品名
                "goods_attr": "1", 规格名
                "total_num": 0, 数量
                "total_price": "0.00", 价格
                "image_text": "" 图片
                },
                {
                "goods_name": "asdasd",
                "goods_attr": "1",
                "total_num": 0,
                "total_price": "0.00",
                "image_text": ""
                }
            ],
            "createtime_text": "" 时间
            }
        ]
    })
     */
    public function orderList()
    {
        $type = $this->request->post('type');
        $page = $this->request->post('page',1);
        if (!in_array($type,[1,2,3,4,5])) $this->error('type参数不合法');
        if (!is_numeric($page)) $this->error('页数不合法');
        switch ($type){
            case 1:
                $where = [
                    'fa_litestore_order.status' => 'normal',
                    'fa_litestore_order.user_id' => $this->auth->id,
                ];
                break;
            case 2:
                $where = [
                    'fa_litestore_order.status' => 'normal',
                    'fa_litestore_order.user_id' => $this->auth->id,
                    'fa_litestore_order.pay_status' => '10',
                ];
                break;
            case 3:
                $where = [
                    'fa_litestore_order.status' => 'normal',
                    'fa_litestore_order.user_id' => $this->auth->id,
                    'fa_litestore_order.freight_status' => '10',
                ];
                break;
            case 4:
                $where = [
                    'fa_litestore_order.status' => 'normal',
                    'fa_litestore_order.user_id' => $this->auth->id,
                    'fa_litestore_order.receipt_status' => '10',
                ];
                break;
            default :
                $where = [
                    'fa_litestore_order.status' => 'normal',
                    'fa_litestore_order.user_id' => $this->auth->id,
                    'fa_litestore_order.receipt_status' => '20',
                ];
                break;
        }

        $model = new \app\api\model\Order();
        $list = $model
            ->with(['goods'])
            ->where($where)
            ->order('id','desc')
            ->paginate(5,false,['page'=>$page])
            ->each(function ($item,$key){
                $sum = 0;
                foreach ($item->getRelation('goods')as $key => $value){
                    $sum += $value['total_num'];
                    $value->visible(['goods_name','goods_attr','total_num','total_price']);
                }
                $item['total_sum'] = $sum;
                $item->visible([
                    'goods','total_sum','order_no','id','pay_price',
                    'discount_price','pay_status','order_status','receipt_status',
                    'freight_status'
                ]);
            });
        $this->success('订单列表',$list);
    }

    /**
     * @ApiTitle    (下单页面)
     * @ApiSummary ([{id:113 goods_id:22 goods_sku_id:106 number:2} {id:114 goods_id:23 goods_sku_id:66 number:2}])
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="data_json", type="string", required=true, description="下单的商品json数据")
     * @ApiParams   (name="id", type="integer", required=false, description="购物车或我常买id 此值不传 json数组注释用")
     * @ApiParams   (name="goods_id", type="integer", required=false, description="商品id 此值不传 json数组注释用")
     * @ApiParams   (name="goods_sku_id", type="integer", required=false, description="规格id 此值不传 json数组注释用")
     * @ApiParams   (name="number", type="integer", required=false, description="购买数量 此值不传 json数组注释用")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    'data':
        "list": [
            {
            "id": 113, 购物车或我常买id
            "goods_id": 22, 商品id
            "goods_name": "Mate 20 华为 HUAWEI ", 商品名称
            "spec_type": "20", 20多规格10单规格
            "sku_id": 106, 规格id
            "sku_price": "6099.00", 规格单价
            "sku_name": "极光色 8GB+128GB", 规格名称
            "number": 2, 数量
            "image_text": "", 图片
            },
            {
            "id": 114,
            "goods_id": 23,
            "goods_name": "MacBook Pro 13寸",
            "spec_type": "20",
            "sku_id": 66,
            "sku_price": "12688.00",
            "sku_name": "天空灰",
            "number": 2,
            "image_text": "",
            }
        ],
        "price": "37574.00" 总价
    })
     */
    public function orderCalculation()
    {
        $json = $this->request->param('data_json');
        if (!$json) $this->error('data_json参数不能为空');
        $json = str_replace('&quot;','"',$json);
//        $json = '[{"goods_id":22,"goods_sku_id":106,"number":2},{"goods_id":23,"goods_sku_id":66,"number":2}]';
        $data = json_decode($json,true);
        $goodsmodel = new \app\api\model\Goods();
        $skumodel = new GoodsSpec();
        $specmodel = new SpecValue();
        $goods_array = []; //商品列表
        $sum_price = 0; //总价格
        foreach ($data as $key => $value){
            if (!is_numeric($value['goods_id']) || !is_numeric($value['goods_sku_id']) || !is_numeric($value['number'])){
                $this->error('参数不合法');
            }
            $goods = $goodsmodel->where('goods_id',$value['goods_id'])->field('goods_id,goods_name,image,spec_type')->find();
            if (!$goods) $this->error('商品不存在');
            $goods->visibleAppend(['image_text']);
            $goods->hidden(['image']);
            $sku = $skumodel->where('goods_spec_id',$value['goods_sku_id'])
                ->field('goods_spec_id,spec_sku_id,goods_price')->find();
            if (!$sku) $this->error('商品规格不存在');
            $sku->unsetAppend();

            $goods['sku_id'] = $sku['goods_spec_id'];
            $goods['sku_price'] = $sku['goods_price'];
            if ($goods['spec_type'] == 10){
                $goods['sku_name'] =''; //规格名
            }else{
                $ids = explode('_',$sku['spec_sku_id']);
                $sku_name = $specmodel->whereIn('id',$ids)->column('spec_value');
                $goods['sku_name'] = implode(' ',$sku_name);  //规格名
            }
            $goods['number'] = $value['number'];
            $goods['id'] = $value['id'];
            $goods_array[] = $goods;
            $sum_price = bcadd($sum_price,bcmul($sku['goods_price'],$value['number'],2),2);
        }
        $addressmodel = new UserAddress();
        $address = $addressmodel->where('user_id',$this->auth->id)->where('normal_status','1')->find();
        $express_price = 0;
        if ($address){
            // 计算运费
            $lat1 = $address['lat'];
            $lng1 = $address['lng'];
            $admin_address = Db::name('admin_address')->find();
            $lat2 = $admin_address['lat'];
            $lng2 = $admin_address['lng'];
            $express_price = $this->distancePrice($lat1,$lng1,$lat2,$lng2);
            if ($express_price === false)  $express_price = 0;
        }else{
            $address = [];
        }
        $sum_price += $express_price;
        $this->success('下单页详情',['list'=>$goods_array,'price'=>$sum_price,'order_price'=>$sum_price-$express_price,'express_price'=>$express_price,'address'=>$address]);
    }

    /**
     * @ApiTitle    (找人付页面)
     * @ApiMethod   (POST)
     * @ApiParams   (name="order_id", type="integer", required=true, description="订单id")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    'data':
        detail:
            pay_price:实付款
            "goods": [
                {
                "goods_id": 22, 商品id
                "goods_name": "Mate 20 华为 HUAWEI ", 商品名称
                "spec_type": "20", 20多规格10单规格
                "sku_id": 106, 规格id
                "sku_price": "6099.00", 规格单价
                "sku_name": "极光色 8GB+128GB", 规格名称
                "number": 2, 数量
                "image_text": "", 图片
                },
                {
                "goods_id": 23,
                "goods_name": "MacBook Pro 13寸",
                "spec_type": "20",
                "sku_id": 66,
                "sku_price": "12688.00",
                "sku_name": "天空灰",
                "number": 2,
                "image_text": "",
                }
            ],
        "user": 用户信息
    })
     */
    public function friendPay()
    {
        $order_id = $this->request->post('order_id');
        if (!$order_id) $this->error('订单id参数不能为空');
        $ordermodel = new \app\api\model\Order();
        $order = $ordermodel
            ->with(['goods'])
            ->where('id',$order_id)
            ->find();
        $user = \app\api\model\User::get($order['user_id']);
        $user->visible(['nickname']);
        $this->success('下单页详情',['detail'=>$order,'user'=>$user]);
    }

    /**
     * @ApiTitle    (判断库存)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="sku_id", type="integer", required=true, description="规格id")
     * @ApiParams   (name="number", type="integer", required=true, description="购买数量")
     * @ApiReturn   ({
    'code':'1',
    'msg':'SUCCESS'
    'data': code 等于1表示未超出最大值
    })
     */
    public function checkStock()
    {
        $sku_id = $this->request->post('sku_id');
        $number = $this->request->post('number');
        if (!$number) $this->error('请选择购买数量');
        if (!$sku_id) $this->error('请选择规格');
        $model = new GoodsSpec();
        $sku = $model::get($sku_id);
        if (!$sku) $this->error('规格已失效,请重新选择');
        if ($sku['stock_num'] < $number) $this->error('已超出最大库存');
        $this->success('SUCCESS');
    }

    /**
     * @ApiTitle    (计算运费)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="address_id", type="integer", required=true, description="地址id")
     * @ApiReturn   ({
    'code':'1',
    'msg':'计算运费'
    'data':
        "price": "37574.00" 总价
    })
     */
    public function freightCalculation()
    {
        $address_id = $this->request->post('address_id');
        if (!$address_id) $this->error('请选择地址');
        $model = new UserAddress();
        $address = $model::get($address_id);
        if (!$address) $this->error('地址不存在');
        $lat1 = $address['lat'];
        $lng1 = $address['lng'];
        $admin_address = Db::name('admin_address')->find();
        $lat2 = $admin_address['lat'];
        $lng2 = $admin_address['lng'];
        $sum_price = $this->distancePrice($lat1,$lng1,$lat2,$lng2);
        if ($sum_price === false) $this->error('地址超出配送距离');
        $this->success('计算运费',['price'=>$sum_price]);
    }

    /**
     * @ApiTitle    (选择优惠券)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="price", type="float", required=true, description="订单价格")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": [
        {
            "id": 1,
            "user_id": 1,
            "coupon_id": 1,
            "name": "手动阀手动阀",
            "price": "1.00", 优惠券金额
            "full_price": "10.00", 满减金额
            "createtime": 111122244,
            "endtime": 1641869388,
            "status": "0",
            "endtime_text": "2022年01月11日到期"
        }
    ]
    })
     */
    public function chooseCoupon()
    {
        $price = $this->request->post('price');
        if (!is_numeric($price)) $this->error('订单价格不合法');
        $model = new UserCoupon();
        $list = $model
            ->where('status','0')
            ->where('user_id',$this->auth->id)
            ->where('full_price','<',$price)
            ->select();
        $this->success('用户优惠券列表',$list);
    }

    /**
     * @ApiTitle    (下单)
     * @ApiMethod   (POST)
     * @ApiSummary ([{goods_id:22 goods_sku_id:106 number:2} {goods_id:23 goods_sku_id:66 number:2}])
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="data_json", type="string", required=true, description="下单的商品json数据")
     * @ApiParams   (name="goods_id", type="integer", required=false, description="商品id 此值不传 json数组注释用")
     * @ApiParams   (name="goods_sku_id", type="integer", required=false, description="规格id 此值不传 json数组注释用")
     * @ApiParams   (name="number", type="integer", required=false, description="购买数量 此值不传 json数组注释用")
     * @ApiParams   (name="coupon_id", type="integer", required=true, description="优惠券id 无优惠券传0")
     * @ApiParams   (name="address_id", type="integer", required=true, description="地址id")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    'data':

    })
     */
    public function addOrder()
    {
        $json = $this->request->param('data_json');
        $coupon_id = $this->request->param('coupon_id');
        $address_id = $this->request->param('address_id');
        if (!$json) $this->error('data_json参数不能为空');
//        $json = '[{"goods_id":22,"goods_sku_id":106,"number":2},{"goods_id":23,"goods_sku_id":66,"number":2}]';
        $address = Db::name('user_address')
            ->where('id',$address_id)
            ->where('user_id',$this->auth->id)
            ->find();
        if (!$address) $this->error('地址不存在');
        $coupon = [];
        if ($coupon_id > 0){
            $coupon = Db::name('user_coupon')
                ->where('id',$coupon_id)
                ->where('user_id',$this->auth->id)
                ->find();
            if (!$coupon) $this->error('优惠券不存在');
        }
        $json = str_replace('&quot;','"',$json);
        $data = json_decode($json,true);
        $goodsmodel = new \app\api\model\Goods();
        $skumodel = new GoodsSpec();
        $ordermodel = new \app\api\model\Order();
        $ordergoodsmodel = new \app\api\model\OrderGoods();
        $orderaddressmodel = new \app\api\model\OrderAddress();
        $cartmodel = new \app\api\model\Cart();
        $specmodel = new SpecValue();
        $goods_array = []; //商品列表
        $sum_price = 0; //总价格
        $user_id = $this->auth->id;

        foreach ($data as $key => $value){
            if (!is_numeric($value['goods_id']) || !is_numeric($value['goods_sku_id']) || !is_numeric($value['number'])){
                $this->error('参数不合法');
            }
            $goods = $goodsmodel->where('goods_id',$value['goods_id'])->find();
            if (!$goods) $this->error('商品不存在');
            $sku = $skumodel->where('goods_spec_id',$value['goods_sku_id'])->find();
            if (!$sku) $this->error('商品规格不存在');
            if ($sku['stock_num'] < $value['number']) $this->error('库存不足');
            if ($goods['spec_type'] == 10){
                $sku_name =''; //规格名
            }else{
                $ids = explode('_',$sku['spec_sku_id']);
                $sku_name = $specmodel->whereIn('id',$ids)->column('spec_value');
                $sku_name = implode(' ',$sku_name);  //规格名
            }

            // 组装订单商品数组
            $goods_array[] = [
                'goods_id' => $goods['goods_id'],
                'goods_name' => $goods['goods_name'],
                'image' => $goods['image'],
                'deduct_stock_type' => $goods['deduct_stock_type'],
                'spec_type' => $goods['spec_type'],
                'spec_sku_id' => $sku['spec_sku_id'],
                'goods_spec_id' => $sku['goods_spec_id'],
                'goods_attr' => $sku_name,
                'content' => $goods['content'],
                'goods_no' => $goods['number'],
                'goods_price' => $sku['goods_price'],
                'goods_weight' => $sku['goods_weight'],
                'total_num' => $value['number'],
                'total_price' => bcmul($sku['goods_price'],$value['number'],2),
                'user_id' => $user_id,
            ];

            $sum_price = bcadd($sum_price,bcmul($sku['goods_price'],$value['number'],2),2);
        }

        // 计算运费
        $lat1 = $address['lat'];
        $lng1 = $address['lng'];
        $admin_address = Db::name('admin_address')->find();
        $lat2 = $admin_address['lat'];
        $lng2 = $admin_address['lng'];
        $distance_price = $this->distancePrice($lat1,$lng1,$lat2,$lng2);
        if ($distance_price === false) $this->error('地址超出配送距离');

        if ($coupon !== [] && $coupon['full_price'] < $sum_price) $this->error('优惠券不可使用');

        // 订单地址
        $order_address = [
            'name' => $address['username'],
            'phone' => $address['mobile'],
            'address' => $address['address'],
            'detail' => $address['address_detail'],
            'lng' => $address['lng'],
            'lat' => $address['lat'],
            'user_id' => $user_id,
        ];


        $sum_price += $distance_price; // 订单总价
        $order_no = 'LQ-'.time().mt_rand(1000,9999); // 订单号
        $couponprice = isset($coupon['price'])?$coupon['price']:0;  // 优惠价格
        // 订单信息
        $order = [
            'order_no' => $order_no,
            'total_price' => $sum_price,  // 订单总价
            'discount_price' => $couponprice,  // 优惠价格
            'pay_price' => bcadd($sum_price,$couponprice,2),  // 支付总价
            'express_price' => $distance_price,  // 骑手费用
            'user_id' => $user_id
        ];

        // 添加订单
        try {
            Db::startTrans();
            $ordermodel->save($order);
            foreach ($goods_array as $key => &$value){
                $value['order_id'] = $ordermodel->id;
                // 减少库存
                if ($value['deduct_stock_type'] == 10){
                    $skumodel->where('goods_spec_id',$value['goods_spec_id'])->setDec('stock_num',$value['total_num']);
                }
            }
            $ordergoodsmodel->saveAll($goods_array);
            $order_address['order_id'] = $ordermodel->id;
            $orderaddressmodel->save($order_address);
            // 删除购物车
            $goodsids = [];
            $skuids = [];
            foreach ($goods_array as $key => $value){
                $goodsids[] = $value['goods_id'];
                $skuids[] = $value['goods_spec_id'];
            }
            $cartmodel->whereIn('goods_id',$goodsids)
                ->whereIn('sku_id',$skuids)
                ->where('user_id',$this->auth->id)
                ->delete();
            Db::commit();
        }catch (PDOException $e){
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success('下单成功',$ordermodel->id);
    }

    /**
     * @ApiTitle    (支付订单)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="订单ID")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function pay()
    {
        $order_id = $this->request->post('id');
        $friend_id = $this->request->post('user_id');
        if (!is_numeric($order_id) || ($friend_id && !is_numeric($friend_id))) $this->error('参数不合法');

        $model = new \app\api\model\Order();
        $order = $model::get($order_id);
        if (!$order) $this->error('订单不存在');
        $openid = Third::get(['user_id'=>$this->auth->id]);
        if (!$openid) $this->error('未注册用户');
        $params = [
            'type'         => 'wechat',
            'orderid'      => $order['order_no'],
            'title'        => '订单号-'.$order['order_no'],
//            'amount'       => $order['pay_price'],
            'amount'       => 0.01,
            'method'       => 'miniapp',
            'openid'       => $openid['openid'],
            'notifyurl'    => $this->request->domain().'/api/notify/orderNotify/type/wechat',
            'returnurl'    => '',
        ];
        $result = Service::submitOrder($params);
        $order->pay_user_id = $this->auth->id;
        $order->isUpdate()->save();
        $this->success('支付信息',$result);
    }


    /**
     * @ApiTitle    (确认收货)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="订单ID")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function takeover()
    {
        $order_id = $this->request->post('id');
        if (!is_numeric($order_id)) $this->error('参数不合法');

        $model = new \app\api\model\Order();
        $order = $model::get($order_id);
        if (!$order) $this->error('订单不存在');
        if ($order['receipt_status'] == 20) $this->error('订单已收货,请勿重复提交');
        if ($order['rider_status'] == 10) $this->error('骑手未送达,请稍后提交');
        $order->receipt_status = '20';
        $order->receipt_time = time();
        $order->isUpdate()->save();
        $this->success('收货成功');
    }


    /**
     * @ApiTitle    (提醒发货)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="订单ID")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function remind()
    {
        $order_id = $this->request->post('id');
        if (!is_numeric($order_id)) $this->error('参数不合法');

        $model = new \app\api\model\Order();
        $model->where('id',$order_id)->update(['remind_status'=>'20']);
        $this->success('提醒成功');
    }


    /**
     * @ApiTitle    (取消订单)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="订单ID")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function cancelOrder()
    {
        $order_id = $this->request->post('id');
        if (!is_numeric($order_id)) $this->error('参数不合法');

        $model = new \app\api\model\Order();
        $order = $model->where('id',$order_id)->find();
        if ($order['freight_status'] == 20) $this->error('订单已发货无法取消');
        $goodsmodel = new \app\api\model\OrderGoods();
        $skumodel = new \app\api\model\GoodsSpec();
        // 增加库存
        $list = $goodsmodel->where('order_id',$order['id'])->select();
        foreach ($list as $key => $value){
            if ($value['deduct_stock_type'] == 10){
                $skumodel->where('goods_spec_id',$value['goods_spec_id'])->setInc('stock_num',$value['total_num']);
            }
        }
        // 已支付就退款
        if($order['pay_status'] == '20'){
            $config = Service::getConfig('wechat');
            $config['app_id'] = $config['miniapp_id'];
            $config['notify_url'] = $this->request->domain().'api/notify/refund/type/wechat';
            $config['return_url'] = '';
            $wechat = Pay::wechat($config);
            $refund_no = 'LQ-'.time().mt_rand(10000,99999);
            $param = [
//            'total_fee' => $order['pay_price'],
//            'refund_fee' => $order['pay_price'],
                'total_fee' => 1,
                'refund_fee' => 1,
                'out_trade_no' => $order['order_no'],
                'out_refund_no' => $refund_no
            ];
            $wechat->refund($param);
            $order->refund_no = $refund_no;
            $order->refund_time = time();
        }
        // 状态变为已取消
        $order->order_status = '20';
        $order->status = 'hidden';
        $order->isUpdate(true)->save();
        $this->success('取消成功');
    }

    /**
     * @ApiTitle    (评价订单详情页)
     * @ApiMethod   (POST)
     * @ApiParams   (name="id", type="integer", required=true, description="订单ID")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    'data':
        order_id:订单id
        goods_name:商品名称
        goods_id:商品id
        goods_attr:规格描述
        image_text:商品图片
    })
     */
    public function commentOrderDetail()
    {
        $order_id = $this->request->post('id');
        if (!is_numeric($order_id)) $this->error('订单id参数不合法');

        $model = new \app\api\model\OrderGoods();
        $list = $model
            ->where('order_id',$order_id)
            ->field('order_id,goods_name,goods_id,goods_attr,image')
            ->select();

        $this->success('商品列表',$list);
    }

    /**
     * @ApiTitle    (评价订单)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="data_json", type="string", required=true, description="评价数据 json对象")
     * @ApiParams   (name="order_id", type="integer", required=false, description="订单ID 不传")
     * @ApiParams   (name="score", type="integer", required=false, description="评价分数 不传")
     * @ApiParams   (name="comment", type="string", required=false, description="评价内容 不传")
     * @ApiParams   (name="goods_id", type="string", required=false, description="商品id 不传")
     * @ApiParams   (name="images", type="string", required=false, description="评价图片逗号隔开 不传")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function commentOrder()
    {
        $data = $this->request->post('data_json');
        if (!$data) $this->error('data_json参数缺失');

        $data = json_decode($data,true);
        $params = [];
        $orderids = [];
        foreach ($data as $key => $value){
            if (!is_numeric($value['order_id'])) $this->error('订单id参数不合法');
            if (!is_numeric($value['goods_id'])) $this->error('商品id参数不合法');
            if (!is_numeric($value['score']) || $value['score']>5 || $value['score']<1) $this->error('评价分数不合法');
            if (!$value['comment']) $this->error('请填写评价内容');
            if ($value['images']){
                $arr = explode(',',$value['images']);
                if (count($arr) > 3) $this->error('请上传最多3张评价图片');
            }
            $params[] = [
                'user_id' => $this->auth->id,
                'order_id' => $value['order_id'],
                'goods_id' => $value['goods_id'],
                'comment' => $value['comment'],
                'images' => $value['images'],
                'score' => $value['score'],
            ];
            $orderids[] = $value['order_id'];
        }

        $ordermodel = new \app\api\model\Order();
        $ordermodel->whereIn('id',$orderids)->isUpdate()->save(['order_status'=>'30']);
        $commentmodel = new GoodsComment();
        $commentmodel->isUpdate(false)->saveAll($params);
        $this->success('评价成功');
    }


    /**
     * @ApiTitle    (删除订单)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="order_id", type="integer", required=true, description="订单ID")
     * @ApiReturn   ({
    'code':'1',
    'msg':'SUCCESS'
    })
     */
    public function delOrder()
    {
        $order_id = $this->request->post('order_id');
        if (!is_numeric($order_id)) $this->error('order_id参数不合法');
        $ordermodel = new \app\api\model\Order();
        $ordermodel->where('id',$order_id)->isUpdate()->save(['status'=>'hidden']);
        $this->success('SUCCESS');
    }

}