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

import com.zm.order.tool.server.dao.OrderDao;
import com.zm.order.tool.server.dao.OrderIndexDao;
import com.zm.order.tool.server.manager.NewHintManager;
import com.zm.order.tool.server.model.OrderDataHandleRequest;
import com.zm.order.tool.server.service.ExceptionConstants;
import com.zm.order.tool.server.service.OrderDataHandleService;
import com.zm.order.tool.server.service.OrderWriteService;
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.beans.factory.annotation.Value;
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;

import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 订单数据处理接口
 * @author zhuxiaofeng
 * @date 2021/9/15 14:06
 */
@Api(value = "订单数据处理", tags = "订单数据处理")
@RestController
@RequestMapping("/qapi/order/handle")
public class OrderDataHandleController {

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

    @Autowired
    private OrderDataHandleService orderDataHandleService;

    @ApiOperation("订单基础信息订正")
    @RequestMapping(value = "/update/base/info", method = RequestMethod.POST)
    public Result updateBaseInfo(@RequestBody OrderDataHandleRequest request) {
        Result result = new Result();
        try {
            result = orderDataHandleService.orderBaseInfoHandle(request);
        } catch (Throwable e) {
            logger.error("OrderDataHandleController[updateBaseInfo]failed,request:{},cause by:{}", request, e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("订单重新路由（1库-0表，2库-0表）")
    @RequestMapping(value = "/rerouting", method = RequestMethod.POST)
    public Result rerouting() {
        Result result = new Result();
        try {
            result = orderDataHandleService.rerouting();
        } catch (Throwable e) {
            logger.error("OrderDataHandleController[rerouting]failed, cause by:{0}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrderIndexDao indexDao;

    @Autowired
    private NewHintManager newHintManager;

    @Value("${new.sharding.db.size}")
    private int dbSize;

    @Value("${new.sharding.tb.size}")
    private int tbSize;

    @Value("${new.sharding.db.size}")
    private int newDbSize;

    @Value("${new.sharding.tb.size}")
    private int newTbSize;

    @Autowired
    private OrderWriteService owService;

    @ApiOperation("订单支付金额处理")
    @RequestMapping(value = "/update/detail/amount", method = RequestMethod.POST)
    public Result updateDetailAmount(@RequestBody OrderDataHandleRequest request) {
        try {
            owService.updateOrderAmount();
        } catch (Throwable e) {
            logger.error("OrderDataHandleController[updateDetailAmount]failed,request:{},cause by:{}", request, e);
        }
        return Result.successNullData();
    }

    @ApiOperation("订单支付明细扩展字段处理")
    @RequestMapping(value = "/update/detail/extra", method = RequestMethod.POST)
    public Result updateDetailAmountExtra(@RequestBody OrderDataHandleRequest request) {
        try {
            owService.updatePayDetailAmount();
        } catch (Throwable e) {
            logger.error("OrderDataHandleController[updateDetailAmount]failed,request:{},cause by:{}", request, e);
        }
        return Result.successNullData();
    }

    @ApiOperation("评价消息推送")
    @RequestMapping(value = "/comment/push", method = RequestMethod.POST)
    public Result commentPush(@RequestBody OrderDataHandleRequest request) {
        try {
            owService.commentMessage();
        } catch (Throwable e) {
            logger.error("OrderDataHandleController[commentPush]failed,request:{},cause by:{}", request, e);
        }
        return Result.successNullData();
    }

    @ApiOperation("商家数据分析")
    @RequestMapping(value = "/seller/data/analysis", method = RequestMethod.POST)
    public Result sellerDataAnalysis(@RequestBody OrderDataHandleRequest request) {
        logger.info("sellerDataAnalysis start!");
        try {
            owService.sellerDataAnalysis();
        } catch (Throwable e) {
            logger.error("OrderDataHandleController[sellerDataAnalysis]failed,request:{},cause by:{}", request, e);
        }
        return Result.successNullData();
    }

    @ApiOperation("同步数据到新ES节点")
    @RequestMapping(value = "/sync/order/new/elasticsearch", method = RequestMethod.POST)
    public Result syncOrderToElasticsearch(@RequestBody OrderDataHandleRequest request) {
        logger.info("syncOrderToElasticsearch start!");
        try {
            owService.syncOrderToElasticsearch();
        } catch (Throwable e) {
            logger.error("OrderDataHandleController[syncOrderToElasticsearch]failed,request:{},cause by:{}", request, e);
        }
        return Result.successNullData();
    }

    @ApiOperation("指定数据同步ES")
    @RequestMapping(value = "/sync/order/new/elasticsearch0", method = RequestMethod.POST)
    public Result syncOrderToElasticsearch0(@RequestBody OrderDataHandleRequest request) {
        logger.info("syncOrderToElasticsearch0 start!");
        try {
            owService.syncOrderToElasticsearch0();
        } catch (Throwable e) {
            logger.error("OrderDataHandleController[syncOrderToElasticsearch]failed,request:{},cause by:{}", request, e);
        }
        return Result.successNullData();
    }

    @ApiOperation("删除多余数据")
    @RequestMapping(value = "/delete/es/data", method = RequestMethod.POST)
    public Result deleteEsData(@RequestBody OrderDataHandleRequest request) {
        logger.info("deleteEsData start!");
        try {
            owService.deleteEsData();
        } catch (Throwable e) {
            logger.error("OrderDataHandleController[deleteEsData]failed,request:{},cause by:{}", request, e);
        }
        return Result.successNullData();
    }

    @ApiOperation("修改商品脏数据")
    @RequestMapping(value = "/update-item-data", method = RequestMethod.POST)
    public Result updateItem(@RequestBody OrderDataHandleRequest request) {
        logger.info("updateItem start!");
        try {
            owService.updateItem();
        } catch (Throwable e) {
            logger.error("OrderDataHandleController[updateItem]failed,request:{},cause by:{}", request, e);
        }
        return Result.successNullData();
    }

    @ApiOperation("订单支付方式处理")
    @RequestMapping(value = "/update/detail/paytype", method = RequestMethod.POST)
    public Result updateOrderPayType(@RequestBody OrderDataHandleRequest request) {
        try {
            owService.updateOrderPayType();
        } catch (Throwable e) {
            logger.error("OrderDataHandleController[updateDetailAmount]failed,request:{},cause by:{}", request, e);
        }
        return Result.successNullData();
    }
}
