<?php
namespace app\api\controller;
use app\models\store\Order;
use app\Request;
use crmeb\services\HttpService;
use crmeb\services\UtilService;

class JuniuController
{
    private static $url = "https://new.juniuzu.com";
    /**
     * 获取商品列表
     * @param Request $request
     */
    public function lst(Request $request)
    {
        list($page,$keyword,$sort_field,$sort_way) = UtilService::postMore(
            [
                ['page',1],
                ['keyword',''],
                ['sort_field','sales_show'],
                ['sort_way','desc'],
            ],$request,true
        );
        $rs = do_request(self::$url.'/api/goods/getlist?'.http_build_query(compact('page','keyword','sort_field','sort_way')),[],null,false);
        return app('json')->success('获取成功',json_decode($rs,true));
    }

    /**
     * 获取商品详情
     * @param Request $request
     */
    public function info(Request $request)
    {
        $id = input('id',0);
        $rs = do_request(self::$url.'/api/goods/getdetail?'.http_build_query(compact('id')),[],null,false);
        return app('json')->success('获取成功',json_decode($rs,true));
    }

    /**
     * 获取物流信息
     * @param Request $request
     * @return mixed
     */
    public function getexpress(Request $request)
    {
        $sn = input('sn',0);
        $rs = do_request(self::$url.'/api/order/getexpress?'.http_build_query(compact('sn')),[],null,false);
        return app('json')->success('获取成功',json_decode($rs,true));
    }
    /**
     * 创建订单
     * @param Request $request
     */
    public function order_create(Request $request)
    {
        $data = UtilService::postMore([
            ['real_name',''],
            ['user_phone',''],
            ['user_address',''],
            ['province',''],
            ['city',''],
            ['district',''],
            ['pay_price',0],
            ['goods_id',0],
            ['goods_name',''],
            ['goods_image',''],
            ['goods_num',''],
            ['spec',[]],
        ],$request);
        $data1 = $data;
        $data['user_address'] = $data['province']." ".$data['city']." ".$data['district']." ".$data['user_address'];
        $data['uid'] = $request->uid();
        $user = $request->user();
        if($data['real_name']=='') return app('json')->fail('输入姓名');
        if($data['user_phone']=='') return app('json')->fail('输入手机号');
        if($data['user_address']=='') return app('json')->fail('输入详细地址');
        if($data['goods_name']=='') return app('json')->fail('商品名称不能为空');
        if($data['goods_image']=='') return app('json')->fail('商品图片不能为空');
        if($data['goods_num']<1) return app('json')->fail('商品数量不能小于1');
        if(bcsub($user['integral'],$data['pay_price'])<0)return app('json')->fail('用户积分不足,不能兑换');
        $rs = Order::order_create($data);
        if($rs)
        {

            $param['items'] = json_encode([['goods_id'=>$data['goods_id'],'num'=>$data['goods_num'],'spec'=>$data['spec']]]);
            $param['province_name'] = $data['province'];
            $param['city_name'] = $data['city'];
            $param['district_name'] = $data['district'];
            $param['address'] = $data1['user_address'];
            $param['consignee'] = $data1['real_name'];
            $param['tel'] = $data1['user_phone'];
            $rs1 = Order::sign($param);
            $rs2 =  HttpService::postRequest(self::$url."/api/order/cooperationadd",$rs1);
            $rs2 = json_decode($rs2,true);
            if(isset($rs2['order_sn']))
            {
                Order::where('id',$rs['id'])->update(['sn'=>$rs2['order_sn'],'rs'=>json_encode($rs2)]);
            }
            else {
                Order::where('id', $rs['id'])->update(['rs' => json_encode($rs2)]);
            }
            return app('json')->success($rs->toarray());
        }
        else
        {
            return app('json')->fail(Order::getErrorInfo());
        }
    }
    public function order_lst(Request $request)
    {
        $where = UtilService::getMore([
            ['page',1],
            ['limit',10],
            ['uid',$request->uid()],
        ],$request);
        $rs = Order::lst($where);
        return app('json')->success($rs);
    }
}