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

import com.alibaba.fastjson.JSON;
import com.aliyun.openservices.shade.com.google.common.base.Throwables;
import com.zm.order.tool.server.dto.ChooseDTO;
import com.zm.order.tool.server.dto.MetadataDTO;
import com.zm.order.tool.server.dto.NewOrderDTO;
import com.zm.order.tool.server.dto.OrderDetailDTO;
import com.zm.order.tool.server.dto.OrderOperateDTO;
import com.zm.order.tool.server.dto.OrderStatusFlowsDTO;
import com.zm.order.tool.server.dto.UpdateMetadataDTO;
import com.zm.order.tool.server.helper.NewPageServiceHelper;
import com.zm.order.tool.server.model.PageWrapper;
import com.zm.order.tool.server.query.ListMetadataQuery;
import com.zm.order.tool.server.query.ListStatusFlowsQuery;
import com.zm.order.tool.server.query.OrderQuery;
import com.zm.order.tool.server.query.SearchOrderQuery;
import com.zm.order.tool.server.request.ActivationStatusRequest;
import com.zm.order.tool.server.request.CompletedAtRequest;
import com.zm.order.tool.server.request.CreateMetadataRequest;
import com.zm.order.tool.server.request.CreateStatusFlowsRequest;
import com.zm.order.tool.server.request.DeletedMetadataRequest;
import com.zm.order.tool.server.request.DeletedStatusRequest;
import com.zm.order.tool.server.request.GetOrderDetailRequest;
import com.zm.order.tool.server.request.ListChooseRequest;
import com.zm.order.tool.server.request.ListMetadataRequest;
import com.zm.order.tool.server.request.ListOrderOperationsRequest;
import com.zm.order.tool.server.request.ListOrderRequest;
import com.zm.order.tool.server.request.ListStatusFlowsRequest;
import com.zm.order.tool.server.request.UpdateMetadataRequest;
import com.zm.order.tool.server.response.ChooseVO;
import com.zm.order.tool.server.response.MetadataVO;
import com.zm.order.tool.server.response.NewOrderVO;
import com.zm.order.tool.server.response.OrderDetailVO;
import com.zm.order.tool.server.response.OrderOperateVO;
import com.zm.order.tool.server.response.OrderStatusFlowsVO;
import com.zm.order.tool.server.service.ExceptionConstants;
import com.zm.order.tool.server.service.NewPageService;
import com.zm.order.tool.server.utils.ConvertUtils;
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;

import java.util.List;

/**
 * @author 陈标
 * @Description
 * @Date 2020/7/20 0020 9:49
 */

@Api(value = "订单运维工具", tags = "新页面服务")
@RestController
@RequestMapping("/qapi/order-tool/new")
public class NewPageController {

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

    @Autowired
    private NewPageService newPageService;
    @Autowired
    private NewPageServiceHelper helper;


    @ApiOperation("接口测试")
    @RequestMapping(value = "/get/test", method = RequestMethod.GET)
    public Result testGet() {
        Result result = new Result();
        result.setStatus(true);
        return result;
    }

    @ApiOperation("查询订单")
    @RequestMapping(value = "/order/list", method = RequestMethod.POST)
    public Result<List<NewOrderVO>> listOrder(@RequestBody ListOrderRequest request) {
        Result<List<NewOrderVO>> result = new Result<>();
        try {
            SearchOrderQuery searchOrderQuery = ConvertUtils.convertObject(request, SearchOrderQuery.class);
            Result<List<NewOrderDTO>> listResult = newPageService.listOrder(searchOrderQuery);
            if (!listResult.isStatus()) {
                result.setException(listResult.exceptionInfo());
                return result;
            }
            List<NewOrderVO> newOrderVOList = ConvertUtils.convertList(listResult.getData(), NewOrderVO.class);
            result.setData(newOrderVOList);
            result.setStatus(true);
        } catch (Throwable throwable) {
            logger.error("NewPageController[listOrder] failed, request:{}, cause by:{}",
                    request.toString(), Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }


    @ApiOperation("完成订单")
    @RequestMapping(value = "/complete/order", method = RequestMethod.POST)
    public Result completeOrder(@RequestBody CompletedAtRequest request) {
        Result result = new Result();
        try {
            SearchOrderQuery searchOrderQuery = ConvertUtils.convertObject(request, SearchOrderQuery.class);
            result = newPageService.completeOrder(searchOrderQuery);
        } catch (Throwable throwable) {
            logger.error("NewPageController[completeOrder] failed, request:{}, cause by:{}",
                    request.toString(), Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }


    @ApiOperation("订单状态流查询")
    @RequestMapping(value = "/list/orderStatus", method = RequestMethod.POST)
    public Result<PageWrapper<OrderStatusFlowsVO>> listOrderStatus(@RequestBody ListStatusFlowsRequest request) {
        Result<PageWrapper<OrderStatusFlowsVO>> result = new Result<>();
        PageWrapper<OrderStatusFlowsVO> pageWrapper = new PageWrapper<>();
        try {
            ListStatusFlowsQuery query = helper.buildListStatusFlowsQuery(request);
            Result<Integer> countResult = newPageService.countStatusFlows(query);
            if (!countResult.isStatus()) {
                result.setException(countResult.exceptionInfo());
                return result;
            }
            Integer count = countResult.getData();
            if (count > 0) {
                Result<List<OrderStatusFlowsDTO>> listResult = newPageService.listOrderStatusFlows(query);
                if (!listResult.isStatus()) {
                    result.setException(listResult.exceptionInfo());
                    return result;
                }
                List<OrderStatusFlowsVO> orderStatusFlowsVOList = helper.buildOrderStatusFlowsVO(listResult.getData());
                pageWrapper.setData(orderStatusFlowsVOList);
                pageWrapper.setTotal(count);
            } else {
                pageWrapper.setTotal(0);
            }
            result.setData(pageWrapper);
            result.setStatus(true);
        } catch (Throwable throwable) {
            logger.error("NewPageController[listOrderStatus] failed, request:{}, cause by:{}",
                    request.toString(), Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("订单状态流创建")
    @RequestMapping(value = "/create/orderStatus", method = RequestMethod.POST)
    public Result createOrderStatus(@RequestBody CreateStatusFlowsRequest request) {
        Result result = new Result<>();
        try {
            OrderStatusFlowsDTO orderStatusFlowsDTO = helper.buildOrderStatusFlowsDTO(request);
            result = newPageService.createOrderStatusFlows(orderStatusFlowsDTO);
        } catch (Throwable throwable) {
            logger.error("NewPageController[createOrderStatus] failed, request:{}, cause by:{}",
                    request.toString(), Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("查看订单操作日志")
    @RequestMapping(value = "/list/operations", method = RequestMethod.POST)
    public Result<List<OrderOperateVO>> listOperation(@RequestBody ListOrderOperationsRequest request) {
        Result<List<OrderOperateVO>> result = new Result<>();
        try {
            Result<List<OrderOperateDTO>> getResult = newPageService.listOrderOperate(request.getOrderNo());
            if (!getResult.isStatus()) {
                result.setException(getResult.exceptionInfo());
                return result;
            }
            List<OrderOperateDTO> data = getResult.getData();
            List<OrderOperateVO> orderOperateVO = ConvertUtils.convertList(data, OrderOperateVO.class);
            result.setData(orderOperateVO);
            result.setStatus(true);
        } catch (Throwable throwable) {
            logger.error("NewPageController[listOperation] failed, request:{}, cause by:{}",
                    request.toString(), Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }


    @ApiOperation("查询订单详情")
    @RequestMapping(value = "/orderDetail", method = RequestMethod.POST)
    public Result<String> getOrderDetail(@RequestBody GetOrderDetailRequest request) {
        Result<String> result = new Result<>();
        try {
            OrderQuery query = ConvertUtils.convertObject(request, OrderQuery.class);
            Result<OrderDetailDTO> orderDetail = newPageService.getOrderDetail(query);
            if (!orderDetail.isStatus()) {
                result.setException(orderDetail.exceptionInfo());
                return result;
            }
            OrderDetailVO orderDetailVO = ConvertUtils.convertObject(orderDetail.getData(), OrderDetailVO.class);
            result.setData(JSON.toJSONString(orderDetailVO));
            result.setStatus(true);
        } catch (Throwable throwable) {
            logger.error("NewPageController[listOperation] failed, request:{}, cause by:{}",
                    request.toString(), Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("激活状态流(状态流页面)")
    @RequestMapping(value = "/activation", method = RequestMethod.POST)
    public Result activation(@RequestBody ActivationStatusRequest request) {
        Result result = new Result<>();
        try {
            result = newPageService.activated(request.getIdList());
        } catch (Throwable throwable) {
            logger.error("NewPageController[activated] failed, request:{}, cause by:{}",
                    request.toString(), Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("删除状态流(状态流页面)")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result delete(@RequestBody DeletedStatusRequest request) {
        Result result = new Result<>();
        try {
            result = newPageService.delete(request.getIdList());
        } catch (Throwable throwable) {
            logger.error("NewPageController[delete] failed, request:{}, cause by:{}",
                    request.toString(), Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }


    @ApiOperation("创建元数据(元数据页面)")
    @RequestMapping(value = "/create/metadata", method = RequestMethod.POST)
    public Result createMetadata(@RequestBody CreateMetadataRequest request) {
        Result result = new Result<>();
        try {
            MetadataDTO metadataDTO = ConvertUtils.convertObject(request, MetadataDTO.class);
            result = newPageService.createMetadata(metadataDTO);
        } catch (Throwable throwable) {
            logger.error("NewPageController[createMetadata] failed, request:{}, cause by:{}",
                    request.toString(), Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }


    @ApiOperation("元数据查询(元数据页面)")
    @RequestMapping(value = "/list/metadata", method = RequestMethod.POST)
    public Result<PageWrapper<MetadataVO>> listMetadata(@RequestBody ListMetadataRequest request) {
        Result<PageWrapper<MetadataVO>> result = new Result<>();
        PageWrapper<MetadataVO> pageWrapper = new PageWrapper<>();
        try {
            ListMetadataQuery query = ConvertUtils.convertObject(request, ListMetadataQuery.class);
            Result<Integer> countResult = newPageService.countMetadata(query);
            if (!countResult.isStatus()) {
                result.setException(countResult.exceptionInfo());
                return result;
            }
            Integer count = countResult.getData();
            if (count > 0) {
                Result<List<MetadataDTO>> listResult = newPageService.listMetadata(query);
                if (!listResult.isStatus()) {
                    result.setException(listResult.exceptionInfo());
                    return result;
                }
                List<MetadataVO> metadataVOList = ConvertUtils.convertList(listResult.getData(), MetadataVO.class);
                pageWrapper.setData(metadataVOList);
                pageWrapper.setTotal(count);
            } else {
                pageWrapper.setTotal(0);
            }
            result.setData(pageWrapper);
            result.setStatus(true);
        } catch (Throwable throwable) {
            logger.error("NewPageController[listMetadata] failed, request:{}, cause by:{}",
                    request.toString(), Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }


    @ApiOperation("订单状态显示(状态流页面下拉框调用)")
    @RequestMapping(value = "/list/status", method = RequestMethod.POST)
    public Result<List<ChooseVO>> listStatus(@RequestBody ListChooseRequest request) {
        Result<List<ChooseVO>> result = new Result<>();
        try {
            ListMetadataQuery query = ConvertUtils.convertObject(request, ListMetadataQuery.class);
            Result<List<ChooseDTO>> listResult = newPageService.listStatus(query);
            if (!listResult.isStatus()) {
                result.setException(listResult.exceptionInfo());
                return result;
            }
            List<ChooseVO> voList = ConvertUtils.convertList(listResult.getData(), ChooseVO.class);
            result.setData(voList);
            result.setStatus(true);
        } catch (Throwable throwable) {
            logger.error("NewPageController[listStatus] failed, request:{}, cause by:{}",
                    request.toString(), Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("订单操作类型(状态流页面下拉框调用)")
    @RequestMapping(value = "/list/operation", method = RequestMethod.POST)
    public Result<List<ChooseVO>> listOperation(@RequestBody ListChooseRequest request) {
        Result<List<ChooseVO>> result = new Result<>();
        try {
            ListMetadataQuery query = ConvertUtils.convertObject(request, ListMetadataQuery.class);
            Result<List<ChooseDTO>> listResult = newPageService.listOperation(query);
            if (!listResult.isStatus()) {
                result.setException(listResult.exceptionInfo());
                return result;
            }
            List<ChooseVO> voList = ConvertUtils.convertList(listResult.getData(), ChooseVO.class);
            result.setData(voList);
            result.setStatus(true);
        } catch (Throwable throwable) {
            logger.error("NewPageController[listStatus] failed, request:{}, cause by:{}",
                    request.toString(), Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }


    @ApiOperation("订单单据类型(状态流页面下拉框调用)")
    @RequestMapping(value = "/list/orderType", method = RequestMethod.POST)
    public Result<List<ChooseVO>> listOrderType(@RequestBody ListChooseRequest request) {
        Result<List<ChooseVO>> result = new Result<>();
        try {
            ListMetadataQuery query = ConvertUtils.convertObject(request, ListMetadataQuery.class);
            Result<List<ChooseDTO>> listResult = newPageService.listOrderType(query);
            if (!listResult.isStatus()) {
                result.setException(listResult.exceptionInfo());
                return result;
            }
            List<ChooseVO> voList = ConvertUtils.convertList(listResult.getData(), ChooseVO.class);
            result.setData(voList);
            result.setStatus(true);
        } catch (Throwable throwable) {
            logger.error("NewPageController[listOrderType] failed, request:{}, cause by:{}",
                    request.toString(), Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }


    @ApiOperation("删除元数据(元数据页面)")
    @RequestMapping(value = "/delete/metadata", method = RequestMethod.POST)
    public Result deleteMetadata(@RequestBody DeletedMetadataRequest request) {
        Result result = new Result<>();
        try {
            result = newPageService.deleteMetadata(request.getIdList());
        } catch (Throwable throwable) {
            logger.error("NewPageController[deleteMetadata] failed, request:{}, cause by:{}",
                    request.toString(), Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }


    @ApiOperation("修改元数据(元数据页面)")
    @RequestMapping(value = "/update/metadata", method = RequestMethod.POST)
    public Result updateMetadata(@RequestBody UpdateMetadataRequest request) {
        Result result = new Result<>();
        try {
            UpdateMetadataDTO updateMetadataDTO = ConvertUtils.convertObject(request, UpdateMetadataDTO.class);
            result = newPageService.updateMetadata(updateMetadataDTO);
        } catch (Throwable throwable) {
            logger.error("NewPageController[updateMetadata] failed, request:{}, cause by:{}",
                    request.toString(), Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }
}
