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

import com.zm.order.tool.server.request.OrderDataReviseRequest;
import com.zm.order.tool.server.request.OrderOperateRequest;
import com.zm.order.tool.server.service.DataService;
import com.zm.order.tool.server.service.ExceptionConstants;
import com.zm.order.tool.server.service.TempOrderService;
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.*;

/**
 * 订单数据处理接口
 * @author zhuxiaofeng
 * @date 2021/9/15 14:06
 */
@Api(value = "处理临时表订单数据", tags = "订单数据处理")
@RestController
@RequestMapping("/qapi/temp/order")
@SuppressWarnings("all")
public class TempOrderDataHandleController {

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

    @Autowired
    private TempOrderService tempOrderService;

    @Autowired
    private DataService dataService;

    @ApiOperation("指定数据同步ES")
    @RequestMapping(value = "/sync/es", method = RequestMethod.POST)
    public Result syncEs() {
        Result result = new Result();
        try {
            result = tempOrderService.syncEs();
        } catch (Throwable e) {
            logger.error("TempOrderDataHandleController[syncEs]failed:cause by:{}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("补推消息")
    @RequestMapping(value = "/push/msg", method = RequestMethod.POST)
    public Result pushMsg() {
        Result result = new Result();
        try {
            result = tempOrderService.pushMsg();
        } catch (Throwable e) {
            logger.error("TempOrderDataHandleController[pushMsg]failed:cause by:{}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("烘焙补推消息")
    @RequestMapping(value = "/push/msg/bak", method = RequestMethod.POST)
    public Result pushMsgBaking() {
        Result result = new Result();
        try {
            result = tempOrderService.pushMsgBaking();
        } catch (Throwable e) {
            logger.error("TempOrderDataHandleController[pushMsgBaking]failed:cause by:{}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("指定数据同步ES")
    @RequestMapping(value = "/do/handle", method = RequestMethod.POST)
    public Result doHandle() {
        Result result = new Result();
        try {
           tempOrderService.doHandle();
        } catch (Throwable e) {
            logger.error("TempOrderDataHandleController[syncEs]failed:cause by:{}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("指定数据修改订单状态")
    @RequestMapping(value = "/update/stauts", method = RequestMethod.POST)
    public Result updateStatus() {
        Result result = new Result();
        try {
            result = tempOrderService.update();
        } catch (Throwable e) {
            logger.error("TempOrderDataHandleController[updateStatus]failed:cause by:{}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("指定数据删除")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result delete() {
        Result result = new Result();
        try {
            result = tempOrderService.delete();
        } catch (Throwable e) {
            logger.error("TempOrderDataHandleController[updateStatus]failed:cause by:{}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("巴莉甜甜订单合并")
    @PostMapping(value = "/merge")
    public Result updateOrder() {
        Result result = new Result();
        try {
            dataService.orderMerge();
            result.setStatus(true);
        } catch (Throwable e) {
            logger.error("BusinessOrderToolController[updateOrder] failed!, cause by:{}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("订单优惠表26号之后数据更新")
    @PostMapping(value = "/discount/update/time")
    public Result updateDiscountTime() {
        Result result = new Result();
        try {
            dataService.updateDiscountTime();
            result.setStatus(true);
        } catch (Throwable e) {
            logger.error("BusinessOrderToolController[updateOrder] failed!, cause by:{}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("混果汁数据迁移")
    @PostMapping(value = "/juice/order")
    public Result juiceOrder() {
        Result result = new Result();
        try {
            tempOrderService.juiceOrder();
            result.setStatus(true);
        } catch (Throwable e) {
            logger.error("BusinessOrderToolController[juiceOrder] failed!, cause by:{}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("混果汁数据迁移")
    @PostMapping(value = "/juice/order/del")
    public Result juiceOrderDel() {
        Result result = new Result();
        try {
            tempOrderService.delJuiceOrder();
            result.setStatus(true);
        } catch (Throwable e) {
            logger.error("BusinessOrderToolController[juiceOrder] failed!, cause by:{}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("指定数据同步到临时表")
    @RequestMapping(value = "/sync/temp1", method = RequestMethod.POST)
    public Result syncTemp1() {
        Result result = new Result();
        try {
            result = tempOrderService.syncTemp1();
        } catch (Throwable e) {
            logger.error("TempOrderDataHandleController[updateStatus]failed:cause by:{}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }


    @ApiOperation("临时表数据同步ES")
    @RequestMapping(value = "/sync/temp/es", method = RequestMethod.POST)
    public Result tempSyncEs() {
        Result result = new Result();
        try {
            result = tempOrderService.tempSyncEs();
        } catch (Throwable e) {
            logger.error("TempOrderDataHandleController[updateStatus]failed:cause by:{}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("临时表数据同步ES")
    @RequestMapping(value = "/sync/temp/operate", method = RequestMethod.POST)
    public Result tempSyncOperate(@RequestBody OrderOperateRequest request) {
        Result result = new Result();
        try {
            tempOrderService.selectOrderOperate(request);
        } catch (Throwable e) {
            logger.error("TempOrderDataHandleController[tempSyncOperate]failed:cause by:{}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("删除三个月之前的订单操作日志")
    @RequestMapping(value = "/delete/order/operation", method = RequestMethod.POST)
    public Result deleteOrderOperations(@RequestBody OrderOperateRequest request) {
        Result result = new Result();
        try {
            result = tempOrderService.deleteOrderOperations();
        } catch (Throwable e) {
            logger.error("TempOrderDataHandleController[deleteOrderOperations]failed:cause by:{}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("删除三个月之前的订单推送日志")
    @RequestMapping(value = "/delete/order/push", method = RequestMethod.POST)
    public Result deleteOrderPushLog(@RequestBody OrderOperateRequest request) {
        Result result = new Result();
        try {
            result = tempOrderService.deleteOrderPushLog();
        } catch (Throwable e) {
            logger.error("TempOrderDataHandleController[deleteOrderOperations]failed:cause by:{}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("t97订单迁移")
    @RequestMapping(value = "/transfer/t97", method = RequestMethod.POST)
    public Result transferOrderT97(@RequestBody OrderOperateRequest request) {
        Result result = new Result();
        try {
            tempOrderService.transferOrderT97();
            tempOrderService.transferRefundOrderT97();
        } catch (Throwable e) {
            logger.error("TempOrderDataHandleController[deleteOrderOperations]failed:cause by:{}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("t97退款单迁移")
    @RequestMapping(value = "/transfer/refund/t97", method = RequestMethod.POST)
    public Result transferRefundT97(@RequestBody OrderOperateRequest request) {
        Result result = new Result();
        try {
            tempOrderService.transferRefundOrderT97();
        } catch (Throwable e) {
            logger.error("TempOrderDataHandleController[transferRefundT97]failed:cause by:{}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("t97迁移订单删除")
    @RequestMapping(value = "/transfer/refund/bao/del", method = RequestMethod.POST)
    public Result transferRefundT97Del(@RequestBody OrderOperateRequest request) {
        Result result = new Result();
        try {
            tempOrderService.delBaoZhu();
        } catch (Throwable e) {
            logger.error("TempOrderDataHandleController[transferRefundT97]failed:cause by:{}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("t97迁移订单同步ES")
    @RequestMapping(value = "/transfer/refund/t97/sync", method = RequestMethod.POST)
    public Result transferRefundT97SyncEs(@RequestBody OrderOperateRequest request) {
        Result result = new Result();
        try {
            tempOrderService.orderT97SyncEs();
        } catch (Throwable e) {
            logger.error("TempOrderDataHandleController[transferRefundT97]failed:cause by:{}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }


    @ApiOperation("修复临时订单配送号")
    @RequestMapping(value = "/repeat/temp", method = RequestMethod.POST)
    public Result repeatUpdateDeliveryId(@RequestBody OrderDataReviseRequest request) {
        logger.info("repeatUpdateDeliveryId start!");
        Result result = new Result<>();
        try {
            result = dataService.repeatUpdateDeliveryId();
        } catch (Exception e) {
            logger.error("TemporaryController[repeatID]failed,request:{},cause by: {}", request, e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }
}
