package com.bohuikeji.delivery.backstage.controller.api.jd;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.entity.TOrderJd;
import com.bohuikeji.delivery.backstage.service.ITOrderJdService;
import com.bohuikeji.delivery.backstage.service.ITOrderService;
import com.bohuikeji.delivery.backstage.service.third.JdServer;
import com.bohuikeji.delivery.backstage.util.capacity.JdUtils;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.entity.back.DataPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 京东配送业务接口
 * @author Admin
 */
@Api(tags = {"【京东】订单相关接口"})
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/jd")
public class JdApi {

    final ITOrderJdService jdService;
    final ITOrderService orderService;

    /**
     * 询价下单
     * @param order
     * @param isEstimate
     * @return
     */
    @ApiOperation(value = "京东（询价&下单）",notes = "询价、下单，二合一接口<br>参数：order，为订单信息对象，必填，详见其中参数。<br>参数：isEstimate，设置询价还是下单，非必填，默认true，该需要携带至url中")
    @PostMapping("/order/estimate")
    public APIEntity orderQuery(@RequestBody @ApiParam(value = "下单实体",required = true) TOrderJd order,
                                @ApiParam("是否是询价") @RequestParam("isEstimate") Boolean isEstimate) {
        log.info("参数1={}",order);
        log.info("参数2={}",isEstimate);
        return APIEntity.success();
    }

    /**
     * 客户查询订单列表
     * @param id
     * @param page
     * @param orderStatus
     * @return
     */
    @ApiOperation("分页查询我的订单列表")
    @ApiOperationSupport(ignoreParameters = {"id","records","total","searchCount"})
    @ApiImplicitParams({
            @ApiImplicitParam(name="orderStatus",value="订单状态(全部,进行中,已完成)",dataType="String", paramType = "query")})
    @PostMapping("/order/list/page")
    public APIEntity myOrders(@RequestAttribute("JwtUserId") Integer id, DataPage page, String orderStatus){
        return APIEntity.success();
    }

    /**
     * 获取订单信息
     * @param orderNum
     * @return
     */
    @ApiOperation(value = "获取订单详情",notes = "返回订单信息、物流信息、快递员信息(如果有)")
    @GetMapping("/order/detail")
    public APIEntity orderDetail(@ApiParam(value = "订单号",required = true) @RequestParam("orderNum") String orderNum) {
        return APIEntity.success(JdUtils.queryTrace(orderService.getByNum(orderNum).getThirdNum()));
    }

    /**
     * 取消京东订单
     * @return
     */
    @ApiOperation(value = "取消订单",notes = "未取件的订单立即取消，进入运输环节的订单将进行异步拦截，订单状态变为拦截中，拦截结果短信和消息中心通知客户。")
    @GetMapping("/order/cancel")
    public APIEntity orderCancel(@ApiParam(value = "订单号",required = true) @RequestParam("orderNum") String orderNum) {
        return APIEntity.success();
    }

    /**
     * 查询订单位置轨迹
     * @param orderNum
     * @return
     */
    @ApiOperation(value = "查询订单位置轨迹",notes = "返回GPS数组，前端用地图组件渲染路径")
    @GetMapping("/order/locations")
    public  APIEntity gpsList(@ApiParam(value = "订单号",required = true) @RequestParam("orderNum") String orderNum){
        return APIEntity.success();
    }

    @ApiOperation(value = "查询客户需要补充支付的订单及付款信息")
    @GetMapping("/order/pay/fill")
    public APIEntity customerPrice(@RequestAttribute("JwtUserId") Integer id){
        //获取京东订单列表
        List<TOrder> list=orderService.list(new QueryWrapper<TOrder>()
                .eq("player_id",id)
                .eq("is_third",1)
                .eq("third_name", JdServer.NAME).notIn("order_status","待接单","待支付","已取消","已退款"));
        if (list==null||list.size()<1){
            return APIEntity.fail("您没有需要补充支付的订单");
        }
        /**
         * 遍历需要支付的订单
         */
        list.forEach(o->{
            JSONObject res=JdUtils.queryOrderPrice(o.getThirdNum());
        });
        return APIEntity.success();

    }

}
