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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.entity.TCourier;
import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.entity.TOrderExceptionEntity;
import com.bohuikeji.delivery.backstage.entity.TOrderTransfer;
import com.bohuikeji.delivery.backstage.service.ITCourierService;
import com.bohuikeji.delivery.backstage.service.ITOrderService;
import com.bohuikeji.delivery.backstage.service.ITOrderTransferService;
import com.bohuikeji.frame.module.admin.base.ApiController;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.entity.back.DataPage;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.time.LocalDateTime;

@Api(tags = {"转单接口"})
@RestController
@RequestMapping("/api/order/transfer")
public class TOrderTransferApi extends ApiController {
    @Autowired
    ITOrderTransferService itOrderTransferService;
    @Autowired
    ITOrderService orderService;
    @Autowired
    ITCourierService courierService;

    @PostMapping("save")
    @ApiOperation("快递员转单")
    @Transactional
    public APIEntity save(@RequestBody TOrderTransfer orderTransfer){
        // 转单前首先确定订单的状态
        if(orderTransfer.getOrderId() == null){
            return APIEntity.fail("转单失败，订单参数异常。或订单不存在");
        }
        TOrder order = orderService.getById(orderTransfer.getOrderId());
        if (order == null){
            return APIEntity.fail("转单失败，订单不存在");
        }
        // 订单在 异常状态 待接单状态 完成状态 不可以转单
        if("异常".equals(order.getOrderStatus())){
            return APIEntity.fail("转单失败，该订单状态异常不能转单");
        }

        if ("已完成".equals(order.getOrderStatus())){
            return APIEntity.fail("转单失败，该订单状态已完成不能转单");
        }

        if ("待接单".equals(order.getOrderStatus())){
            return APIEntity.fail("转单失败，该订单属于待接单状态");
        }

        // 记录转单信息
        orderTransfer.setCreateDate(LocalDateTime.now());
        itOrderTransferService.save(orderTransfer);
        // 修改订单的派送员
        //order.setOrderStatus("待派送");
        order.setCourierId(orderTransfer.getPassiveCourierId());
        orderService.updateById(orderService.extractExtract(order));
        TCourier tCourier = courierService.getById(orderTransfer.getCourierId());
        orderService.curAppPush(order,"订单转单提醒","您有来自:"+tCourier.getCourierName()+"的转单，请及时处理");
        return APIEntity.success("转单成功！请再次确保与您需要转单的骑手联系过");
    }


    @ApiOperation("分页查询转单信息（我转给别人的/别人转给我的）")
    @GetMapping("/getTransferInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name="isPassive",value="查询类型（1表示我转给别人的；2表示别人转给我的）", paramType = "query",required=true)})
    public APIEntity getPassiveOrder(@ApiIgnore @RequestAttribute("JwtUserId") Integer id,DataPage page,Integer isPassive){
        if (id == null){
            return APIEntity.fail("参数异常");
        }
        if (isPassive == 1){
            QueryWrapper<TOrderTransfer> wrapper = new QueryWrapper<>();
            wrapper.eq("courier_id",id);
            return APIEntity.success(itOrderTransferService.page(page,wrapper));
        }
        if (isPassive == 2){
            QueryWrapper<TOrderTransfer> wrapper = new QueryWrapper<>();
            wrapper.eq("passive_courier_id",id);
            return APIEntity.success(itOrderTransferService.page(page,wrapper));
        }
        return APIEntity.fail("参数无效");
    }

}
