package com.ruoyi.applet.contoller;


import com.cyl.h5.service.H5OrderService;
import com.cyl.manager.oms.service.OrderService;
import com.ruoyi.applet.pojo.vo.PetDate;
import com.ruoyi.applet.service.IMallOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.model.domain.AppletOrder;
import com.ruoyi.model.domain.AppletPet;
import com.ruoyi.model.service.IAppletOrderService;
import com.ruoyi.model.service.IAppletPetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "订单模块 - 接单大厅流程")
@RestController
@RequestMapping("/applet/orderItem")
public class ApiAppletOrderItemController extends BaseController {

    @Autowired
    private IAppletOrderService appletOrderService;
    @Autowired
    private IAppletPetService appletPetService;
    @Autowired
    private IMallOrderService mallOrderService;
    //下单小程序：那边来的订单信息
    @Autowired
    private H5OrderService h5OrderService;
    @Autowired
    private OrderService service;


    //接单大厅流程
    @ApiOperation("小程序-接单大厅流程")
    @GetMapping("/list")
    public AjaxResult outDateList(AppletOrder appletOrder){
        if(appletOrder.getType()==null){
            return AjaxResult.error("type状态不能为空");
        }
        if(appletOrder.getUserIdJson() == null){
            return AjaxResult.error("userIdJson用户信息标识不能为空");
        }
        startPage();

        List<AppletOrder> list = null;
        if(appletOrder.getType().equals("0")){
            list = appletOrderService.selectAppletOrderListLikeUserIdJson(appletOrder);
        }else{
            list = appletOrderService.selectAppletOrderList(appletOrder);
        }
        for (AppletOrder order:list){
            order.setH5OrderVO(h5OrderService.orderDetail(order.getOrderId()));
            order.setManagerOrderDetailVO(service.selectById(order.getOrderId()));
        }
        TableDataInfo dataTable = getDataTable(list);
        return AjaxResult.success(dataTable);
    }

























    //接单大厅 - 根据订单标识查询订单详情的接口
    @ApiOperation("接单大厅 - 根据订单标识查询订单详情的接口")
    @GetMapping("/getByOrderId")
    public AjaxResult getByOrderId(AppletOrder appletOrder){
        if(appletOrder.getId() == null){
            return AjaxResult.error("订单标识不能为空");
        }
        AppletOrder appletOrder1 = appletOrderService.selectAppletOrderById(appletOrder.getId());
        List<AppletPet> appletPets = appletPetService.selectAppletPetListByOrderId(appletOrder1.getId());
        PetDate petDate = new PetDate();
        petDate.setDate("12-12");
        petDate.setTitle("上门喂养");
        petDate.setNum(10);

        //比例
        Map<String,Object> map = new HashMap<>();
        map.put("order",appletOrder1);
        map.put("petList",appletPets);
        map.put("petDate",petDate);
//        map.put("");





        return AjaxResult.success("接单大厅查看详情",map);
    }


    //接单大厅 - 拒绝、无法接单
    @ApiOperation("接单大厅 - 拒绝、无法接单")
    @PostMapping("/endByOrderId")
    public AjaxResult endByOrderId(@RequestBody AppletOrder appletOrder){
        if(appletOrder.getId() == null){
            return AjaxResult.error("订单标识不能为空");
        }
        if(appletOrder.getUserId() == null){
            return AjaxResult.error("userId用户信息标识不能为空");
        }
        if(appletOrder.getReason() == null){
            return AjaxResult.error("无法接单原因不能为空");
        }
        appletOrder.setStatus(2);
        appletOrderService.updateAppletOrder(appletOrder);
        return AjaxResult.success("拒单成功",appletOrder);
    }



    //接单大厅 - 立即接单
    @ApiOperation("接单大厅 - 立即接单")
    @PostMapping("/startByOrderId")
    public AjaxResult startByOrderId(@RequestBody AppletOrder appletOrder){
        if(appletOrder.getId() == null){
            return AjaxResult.error("订单标识不能为空");
        }
        if(appletOrder.getUserId() == null){
            return AjaxResult.error("userId用户信息标识不能为空");
        }
        appletOrder.setStatus(1);
        appletOrderService.updateAppletOrder(appletOrder);
        return AjaxResult.success("接单成功",appletOrder);
    }



    //我的订单流程
    @ApiOperation("小程序-我的订单流程")
    @GetMapping("/myList")
    public AjaxResult myList(AppletOrder appletOrder){
        if(appletOrder.getType()==null){
            return AjaxResult.error("type状态不能为空");
        }
        if(appletOrder.getUserId() == null){
            return AjaxResult.error("userId用户信息标识不能为空");
        }
        List<AppletOrder> list = appletOrderService.selectAppletOrderList(appletOrder);

        return AjaxResult.success("我的订单数据列表",list);
    }







    //测试派单
    @ApiOperation("派单")
    @GetMapping("/dispatch/{orderId}")
    public AjaxResult dispatch(@PathVariable("orderId")Long orderId){
        mallOrderService.dispatch(orderId);
        return AjaxResult.success("派单成功");
    }










}
