package com.zm.order.tool.server.controller;

import com.alibaba.fastjson.JSON;
import com.zm.order.tool.server.constants.ListConst3;
import com.zm.order.tool.server.constants.ListConst6;
import com.zm.order.tool.server.dao.OrderDao;
import com.zm.order.tool.server.model.Order;
import com.zm.order.tool.server.newdatasource.NewDataSourceOrderDao;
import com.zm.order.tool.server.request.BusinessOrderUpdateRequest;
import com.zm.order.tool.server.service.*;
import com.zm.util.dto.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 订单脚本
 *
 * @author NianChang
 * @date 2021/02/20 9:48 上午
 */
@Api(value = "枚举值转换器", tags = "枚举值转换器")
@RestController
@SuppressWarnings("all")
@RequestMapping("/qapi/convert")
public class OrderConvertController {

    private static final Logger logger = LoggerFactory.getLogger(OrderConvertController.class);

    @Autowired
    private OrderRoutingService routeService;

    @Autowired
    private OrderRoutingRefundService orderRoutingRefundService;

    @Autowired
    private OrderRouting2Service route2Service;

    @Autowired
    private OrderRouting3Service route3Service;

    @Autowired
    private OrderRouting4Service route4Service;

    @Autowired
    private OrderRouting5Service route5Service;

    @Autowired
    private OrderRouting6Service route6Service;

    @Autowired
    private OrderRouting7Service route7Service;

    @Autowired
    private OrderRouting8Service route8Service;

    @ApiOperation("订单转换枚举值")
    @RequestMapping(value = "/order", method = RequestMethod.POST)
    public Result orderConvert(@RequestBody Order order) {
        logger.info("orderConvert start!");
        try {
            routeService.orderConvert();
        } catch (Exception e) {
            logger.error("TemporaryController[orderConvert]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }
    @ApiOperation("订单转换枚举值")
    @RequestMapping(value = "/order/fix", method = RequestMethod.POST)
    public Result orderConvertFix(@RequestBody Order order) {
        logger.info("orderConvertFix start!");
        try {
            routeService.orderConvertFix();
        } catch (Exception e) {
            logger.error("TemporaryController[orderConvert]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());

        }
        return Result.successNullData();
    }

    @ApiOperation("新库是老枚举修复")
    @RequestMapping(value = "/order/fix/new", method = RequestMethod.POST)
    public Result orderConvertFixNew(@RequestBody Order order) {
        logger.info("orderConvertFixNew start!");
        try {
            routeService.orderConvertFixNew();
        } catch (Exception e) {
            logger.error("TemporaryController[orderConvertFixNew]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("新库是老枚举修复（单个订单）")
    @RequestMapping(value = "/order/fix/type", method = RequestMethod.POST)
    public Result orderConvertFixType(@RequestBody Order order) {
        logger.info("orderConvertFixNew start!");
        try {
            routeService.orderConvertFixType(order);
        } catch (Exception e) {
            logger.error("TemporaryController[orderConvertFixNew]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("订单转换枚举值")
    @RequestMapping(value = "/order-2", method = RequestMethod.POST)
    public Result orderConvert2(@RequestBody Order order) {
        logger.info("orderConvert2 start!");
        try {
            route2Service.orderConvert();
        } catch (Exception e) {
            logger.error("TemporaryController[orderConvert2]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("订单转换枚举值")
    @RequestMapping(value = "/order-3", method = RequestMethod.POST)
    public Result orderConvert3(@RequestBody Order order) {
        logger.info("orderConvert3 start!");
        try {
            route3Service.orderConvert();
        } catch (Exception e) {
            logger.error("TemporaryController[orderConvert3]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("订单转换枚举值")
    @RequestMapping(value = "/order-4", method = RequestMethod.POST)
    public Result orderConvert4(@RequestBody Order order) {
        logger.info("orderConvert4 start!");
        try {
            route4Service.orderConvert();
        } catch (Exception e) {
            logger.error("TemporaryController[orderConvert4]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("订单转换枚举值")
    @RequestMapping(value = "/order-5", method = RequestMethod.POST)
    public Result orderConvert5(@RequestBody Order order) {
        logger.info("orderConvert5 start!");
        try {
            route5Service.orderConvert();
        } catch (Exception e) {
            logger.error("TemporaryController[orderConvert5]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("订单转换枚举值")
    @RequestMapping(value = "/order-6", method = RequestMethod.POST)
    public Result orderConvert6(@RequestBody Order order) {
        logger.info("orderConvert6 start!");
        try {
            route6Service.orderConvert();
        } catch (Exception e) {
            logger.error("TemporaryController[orderConvert6]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("订单转换枚举值")
    @RequestMapping(value = "/order-7", method = RequestMethod.POST)
    public Result orderConvert7(@RequestBody Order order) {
        logger.info("orderConvert7 start!");
        try {
            route7Service.orderConvert();
        } catch (Exception e) {
            logger.error("TemporaryController[orderConvert7]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("订单转换枚举值")
    @RequestMapping(value = "/order-8", method = RequestMethod.POST)
    public Result orderConvert8(@RequestBody Order order) {
        logger.info("orderConvert8 start!");
        try {
            route8Service.orderConvert();
        } catch (Exception e) {
            logger.error("TemporaryController[orderConvert8]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("订单转换枚举值-补偿脚本")
    @RequestMapping(value = "/repair-order", method = RequestMethod.POST)
    public Result repairOrder(@RequestBody Order order) {
        logger.info("repairOrder start!");
        try {
            routeService.repairOrder();
        } catch (Exception e) {
            logger.error("TemporaryController[orderConvert]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("订单全量同步ES新节点")
    @RequestMapping(value = "/sync/es", method = RequestMethod.POST)
    public Result syncEs(@RequestBody Order order) {
        logger.info("syncEs start!");
        try {
//            routeService.syncEs();

            logger.info("sync_es_end! ");
        } catch (Exception e) {
            logger.error("TemporaryController[orderConvert]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private NewDataSourceOrderDao newDataSourceOrderDao;

    @ApiOperation("单条订单信息对比")
    @RequestMapping(value = "/order/compare", method = RequestMethod.POST)
    public Result<String> orderCompare(@RequestBody Order order) {
        Order order1 = orderDao.selectByOrderNo(order.getOrderNo(), order.getUserId());
        Order order2 = newDataSourceOrderDao.selectByOrderNo(order.getOrderNo(), order.getUserId());
        String s = JSON.toJSONString(order1) + "------" + JSON.toJSONString(order2);
        return Result.successData(s);
    }

    @ApiOperation("支付明细转换枚举值")
    @RequestMapping(value = "/pay-detail", method = RequestMethod.POST)
    public Result payConvert(@RequestBody Order order) {
        logger.info("payConvert start!");
        try {
            routeService.payConvert();
        } catch (Exception e) {
            logger.error("TemporaryController[payConvert]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }


    @ApiOperation("单笔订单支付明细转换枚举值")
    @RequestMapping(value = "/pay-detail-fix", method = RequestMethod.POST)
    public Result syncPayFromOldByOrderNo(@RequestBody BusinessOrderUpdateRequest request) {
        logger.info("syncPayFromOldByOrderNo start!");
        try {
            routeService.syncPayFromOldByOrderNo(request.getOrderNo());
        } catch (Exception e) {
            logger.error("syncPayFromOldByOrderNo[syncPayFromOldByOrderNo]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }


    @ApiOperation("订单合并问题修复")
    @RequestMapping(value = "/merge/fix", method = RequestMethod.POST)
    public Result orderMergeFix(@RequestBody BusinessOrderUpdateRequest request) {
        logger.info("orderMergeFix start!");
        try {
            routeService.orderMergeFix(request.getOrderNo());
        } catch (Exception e) {
            logger.error("orderMergeFix[orderMergeFix]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }


    @ApiOperation("退款单转换枚举值")
    @RequestMapping(value = "/refund", method = RequestMethod.POST)
    public Result refundConvert(@RequestBody Order order) {
        logger.info("refundConvert start!");
        try {
            orderRoutingRefundService.refundConvertNew();

            orderRoutingRefundService.commentConvert();

            orderRoutingRefundService.itemCommentConvert();
        } catch (Exception e) {
            logger.error("TemporaryController[refundConvert]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @Autowired
    private OrderRoutingRefundFixService orderRoutingRefundFixService;
    @ApiOperation("从临时库补退款单")
    @RequestMapping(value = "/refund/fix", method = RequestMethod.POST)
    public Result refundConvertFix(@RequestBody Order order) {
        logger.info("refundConvertFix start!");
        try {
            orderRoutingRefundFixService.refundConvertFix();
        } catch (Exception e) {
            logger.error("TemporaryController[refundConvert]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("评价转换枚举值")
    @RequestMapping(value = "/comment/", method = RequestMethod.POST)
    public Result commentConvert(@RequestBody Order order) {
        logger.info("commentConvert start!");
        try {
        } catch (Exception e) {
            logger.error("TemporaryController[commentConvert]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("评价转换枚举值")
    @RequestMapping(value = "/comment/temp", method = RequestMethod.POST)
    public Result commentConvertTemp(@RequestBody Order order) {
        logger.info("commentConvert start!");
        try {
            orderRoutingRefundService.commentConvert();
        } catch (Exception e) {
            logger.error("TemporaryController[commentConvert]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("商品评价转换枚举值")
    @RequestMapping(value = "/item-comment", method = RequestMethod.POST)
    public Result itemCommentConvert(@RequestBody Order order) {
        logger.info("itemCommentConvert start!");
        try {
//            routeService.itemCommentConvert();
        } catch (Exception e) {
            logger.error("TemporaryController[itemCommentConvert]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("校验订单新数据结构")
    @RequestMapping(value = "/check/order", method = RequestMethod.POST)
    public Result checkOrderConvert(@RequestBody Order order) {
        logger.info("orderConvert start!");
        try {
            routeService.checkOrderConvert();
        } catch (Exception e) {
            logger.error("TemporaryController[checkOrderConvert]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @Autowired
    private  TempOrderRoutingServiceImpl service;

    @ApiOperation("订单数据缺失补偿")
    @RequestMapping(value = "/order/repair", method = RequestMethod.POST)
    public Result orderRepair(@RequestBody Order order) {
        logger.info("orderRepair start!");
        try {
//            routeService.orderRepair();
            service.orderConvertFixNewOne(order.getOrderNo());
        } catch (Exception e) {
            logger.error("TemporaryController[orderRepair]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("指定数据从老库同步到新哭")
    @RequestMapping(value = "/order/repair-new", method = RequestMethod.POST)
    public Result orderRepairNew(@RequestBody Order order) {
        logger.info("orderRepairNew start!");
        try {
//            routeService.orderRepair();
            for (String s : ListConst3.list) {
                service.orderConvertFixNewOne(s);
            }
        } catch (Exception e) {
            logger.error("TemporaryController[orderRepair]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("单个评价数据缺失补偿")
    @RequestMapping(value = "/comment/repair", method = RequestMethod.POST)
    public Result commentRepair(@RequestBody Order order) {
        logger.info("commentRepair start!");
        try {
            service.handlerEachComment(order.getOrderNo());
        } catch (Exception e) {
            logger.error("TemporaryController[commentRepair]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    /**  --------------   */

    @ApiOperation("订单同步冷库")
    @RequestMapping(value = "/history/order", method = RequestMethod.POST)
    public Result historyOrder(@RequestBody Order order) {
        logger.info("historyOrder start!");
        try {
//            routeService.historyOrder();
        } catch (Exception e) {
            logger.error("TemporaryController[historyOrder]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("支付明细同步冷库")
    @RequestMapping(value = "/history/pay-detail", method = RequestMethod.POST)
    public Result historyPay(@RequestBody Order order) {
        logger.info("historyPay start!");
        try {
//            routeService.historyPay();
        } catch (Exception e) {
            logger.error("TemporaryController[historyPay]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("退款单同步冷库")
    @RequestMapping(value = "/history/refund", method = RequestMethod.POST)
    public Result historyRefund(@RequestBody Order order) {
        logger.info("historyRefund start!");
        try {
//            routeService.historyRefund();
        } catch (Exception e) {
            logger.error("TemporaryController[historyRefund]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("评价同步冷库")
    @RequestMapping(value = "/history/comment", method = RequestMethod.POST)
    public Result historyComment(@RequestBody Order order) {
        logger.info("historyComment start!");
        try {
//            routeService.historyComment();
        } catch (Exception e) {
            logger.error("TemporaryController[historyComment]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("商品评价同步冷库")
    @RequestMapping(value = "/history/item-comment", method = RequestMethod.POST)
    public Result itemComment(@RequestBody Order order) {
        logger.info("historyItemComment start!");
        try {
//            routeService.historyItemComment();
        } catch (Exception e) {
            logger.error("TemporaryController[historyItemComment]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("补合并订单数据")
    @RequestMapping(value = "/order/merge-repair", method = RequestMethod.POST)
    public Result mergeRepair(@RequestBody Order order) {
        logger.info("mergeRepair start!");
        try {
//            routeService.mergeRepair();
        } catch (Exception e) {
            logger.error("TemporaryController[mergeRepair]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("扫描订单缺失数据")
    @RequestMapping(value = "/order/loss", method = RequestMethod.POST)
    public Result orderLoss(@RequestBody Order order) {
        logger.info("orderLoss start!");
        try {
            routeService.orderLoss(order);
        } catch (Exception e) {
            logger.error("TemporaryController[orderLoss]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("扫描支付缺失数据")
    @RequestMapping(value = "/pay/loss", method = RequestMethod.POST)
    public Result payLoss(@RequestBody Order order) {
        logger.info("payLoss start!");
        try {
//            routeService.payLoss();
        } catch (Exception e) {
            logger.error("TemporaryController[payLoss]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("扫描退款缺失数据")
    @RequestMapping(value = "/refund/loss", method = RequestMethod.POST)
    public Result refundLoss(@RequestBody Order order) {
        logger.info("refundLoss start!");
        try {
//            routeService.refundLoss();
        } catch (Exception e) {
            logger.error("TemporaryController[refundLoss]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }

    @ApiOperation("订单新节点全量数据check")
    @RequestMapping(value = "/order/check", method = RequestMethod.POST)
    public Result orderCheck(@RequestBody Order order) {
        logger.info("orderCheck start!");
        try {
//            routeService.orderCheck();
        } catch (Exception e) {
            logger.error("TemporaryController[orderCheck]failed,request:{},cause by: {}", e);
            return Result.failureData(e.getMessage());
        }
        return Result.successNullData();
    }
}
