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


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.controller.admin.OrderController;
import com.bohuikeji.delivery.backstage.controller.admin.TPayController;
import com.bohuikeji.delivery.backstage.entity.TOrderExceptionEntity;
import com.bohuikeji.delivery.backstage.service.ITOrderExceptionService;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.entity.back.DataPage;
import com.bohuikeji.frame.module.admin.entity.sys.SysUser;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = {"管理端APP接口——异常订单相关"})
@RestController
@RequestMapping("/api/admin/order/exception")
@Slf4j
public class AdminExceptionOrderApi {

    @Autowired
    ITOrderExceptionService exceptionService;
    @Autowired
    OrderController orderController;
    @Autowired
    TPayController payController;


    @ApiOperation("查询异常订单列表")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "异常状态(待处理,处理历史,全部订单)", dataType = "String", paramType = "query", example = "待处理", required = true)})
    public APIEntity exceptionList(@ApiIgnore @RequestAttribute("JwtAdminId") Integer adminId,DataPage page, String status) {
        page.setDesc("exception_id");
        QueryWrapper<TOrderExceptionEntity> queryWrapper = new QueryWrapper();
        switch (status) {
            case "待处理":
                queryWrapper.eq("exception_status","待处理");
                break;
            case "处理历史":
                queryWrapper.eq("exception_status","处理完成");
                break;
            case "全部订单":break;
            default:
                return APIEntity.fail("参数错误，无法识别异常状态参数！");
        }
        return APIEntity.success(exceptionService.page(page,queryWrapper));
    }

    @ApiOperation("查询异常详情")
    @GetMapping("/get")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "exceptionId", value = "异常Id", dataType = "String", paramType = "query", example = "18", required = true)})
    public APIEntity getException(Integer exceptionId) {
        TOrderExceptionEntity exception=exceptionService.getById(exceptionId);
        if(exception==null){
            return APIEntity.fail("参数错误，无法找到异常信息！");
        }
        return APIEntity.success(exception);
    }


    @ApiOperation("重置为待接单")
    @GetMapping("/reset")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "exceptionId", value = "异常Id", dataType = "String", paramType = "query", example = "18", required = true)})
    public APIEntity ResetException(@ApiIgnore @RequestAttribute("JwtAdminId") Integer id, Integer exceptionId) {
        TOrderExceptionEntity exception=exceptionService.getById(exceptionId);
        if(exception==null){
            return APIEntity.fail("参数错误，无法找到异常信息！");
        }
        return orderController.exceptionReset(exceptionId,exception.getOrderId(),id);
    }

    @ApiOperation("指派派送员处理并更改状态")
    @GetMapping("/assign")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "exceptionId", value = "异常Id", dataType = "String", paramType = "query", example = "18", required = true),
            @ApiImplicitParam(name = "courierId", value = "派送员Id", dataType = "String", paramType = "query", example = "31", required = true),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "String", paramType = "query", example = "待取件", required = true)})

    public APIEntity ResetException(@ApiIgnore @RequestAttribute("JwtAdminId") Integer id,Integer exceptionId, Integer courierId, String status) {
        if(StringUtils.isEmpty(status)){
            return APIEntity.fail("请选择订单状态！");
        }
        TOrderExceptionEntity exception=exceptionService.getById(exceptionId);
        if(exception==null){
            return APIEntity.fail("参数错误，无法找到异常信息！");
        }
        return orderController.exceptionAssign(exceptionId,exception.getOrderId(),courierId,status,id);
    }

    @ApiOperation("取消订单")
    @GetMapping("/chanel")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "exceptionId", value = "异常Id", dataType = "String", paramType = "query", example = "18", required = true)})

    public APIEntity AssignException(@ApiIgnore @RequestAttribute("JwtAdminId") Integer id,Integer exceptionId) {
        TOrderExceptionEntity exception=exceptionService.getById(exceptionId);
        if(exception==null){
            return APIEntity.fail("参数错误，无法找到异常信息！");
        }
        return orderController.exceptionChanel(exceptionId,exception.getOrderId(),id);
    }
    @ApiOperation("完结")
    @GetMapping("/end")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "exceptionId", value = "异常Id", dataType = "String", paramType = "query", example = "18", required = true)})

    public APIEntity EndException(@ApiIgnore @RequestAttribute("JwtAdminId") Integer id,Integer exceptionId) {
        TOrderExceptionEntity exception=exceptionService.getById(exceptionId);
        if(exception==null){
            return APIEntity.fail("参数错误，无法找到异常信息！");
        }
        return orderController.exceptionEnd(exceptionId,exception.getOrderId(),id);
    }
    @ApiOperation("异常订单退款")
    @GetMapping("/refund")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "exceptionId", value = "异常Id", dataType = "String", paramType = "query", example = "18", required = true)})

    public APIEntity refundException(@ApiIgnore @RequestAttribute("JwtAdminId") Integer id,Integer exceptionId) {
        TOrderExceptionEntity exception=exceptionService.getById(exceptionId);
        if(exception==null){
            return APIEntity.fail("参数错误，无法找到异常信息！");
        }
        return payController.aliAndWinRefund(exceptionId,exception.getOrderId(),id);
    }
}
