package com.neusoft.smart.pos.order.controller;


import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.dto.trade.request.GetShopOrderDetailsForPosRequest;
import com.neusoft.smart.pos.dto.trade.request.GetTradeShopOrderDetailsRequest;
import com.neusoft.smart.pos.dto.trade.request.GetWxOrdersRequest;
import com.neusoft.smart.pos.dto.trade.response.GetShopOrderDetailsForPosResponse;
import com.neusoft.smart.pos.dto.trade.response.GetShopOrderDetailsResponse;
import com.neusoft.smart.pos.dto.trade.response.GetShopOrderSummaryResponse;
import com.neusoft.smart.pos.dto.trade.response.GetWxOrdersResponse;
import com.neusoft.smart.pos.framework.bean.PaginationExtendBean;
import com.neusoft.smart.pos.framework.constants.CommonErrorCode;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.order.service.OrderStatisticsService;
import com.neusoft.smart.pos.payorder.persistent.domain.PayOrder;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Api(value = "订单统计使用rest接口", description = "订单统计使用rest接口")
@RestController
@RequestMapping(path = "/orderStatistics")
@Validated
public class OrderStatisticsController {

    @Autowired
    private OrderStatisticsService orderStatisticsService;

    @ApiOperation(value = "门店订单汇总查询接口(订单管理页面接口)", notes = "门店订单汇总查询接口(订单管理页面接口)")
    @GetMapping("/shopOrderSummary")
    public ResponseData<PaginationExtendBean<GetShopOrderSummaryResponse>> queryShopOrderSummary(
            @RequestParam(value = "orgId", defaultValue = "0") Integer orgId
            , @RequestParam(value = "shopId") List<Integer> shopIds
            , @RequestParam("offset") Integer offset
            , @RequestParam("limit") Integer limit
            , @RequestParam("isPage") Boolean isPage
            , @RequestParam(value = "startTime",defaultValue = "") String startTime
            , @RequestParam(value = "endTime",defaultValue = "") String endTime) {
        return new ResponseData<>(orderStatisticsService.queryShopOrderSummary(orgId, shopIds,startTime,endTime, offset, limit, isPage));
    }

    @ApiOperation(value = "门店订单详情统计查询接口", notes = "门店订单详情统计查询接口")
    @GetMapping("/shopOrderDetails")
    public ResponseData<GetShopOrderDetailsResponse> queryShopOrderDetails(
            @RequestParam(value = "shopId", defaultValue = "") List<Integer> shopIds
            , @RequestParam(value = "orderNumber", defaultValue = "") String orderNumber
            , @RequestParam(value = "serialNo", defaultValue = "") String serialNo
            , @RequestParam(value = "deviceName", defaultValue = "") String deviceName
            , @RequestParam(value = "memberCardNo", defaultValue = "") String memberCardNo
            , @RequestParam(value = "memberName", defaultValue = "") String memberName
            , @RequestParam(value = "cardNum", defaultValue = "") String cardNum
            , @RequestParam(value = "customerNumber", defaultValue = "") String customerNumber
            , @RequestParam(value = "payType", defaultValue = "") String payType
            , @RequestParam(value = "orderStatus", defaultValue = "") String orderStatus
            , @RequestParam(value = "startTime", defaultValue = "") String startTime
            , @RequestParam(value = "endTime", defaultValue = "") String endTime
            , @RequestParam(value = "dateTime", defaultValue = "") String dateTime
            , @RequestParam(value = "timeMode", defaultValue = "") String timeMode
            , @RequestParam(value = "orderType", defaultValue = "") String orderType
            , @RequestParam("offset") Integer offset
            , @RequestParam("limit") Integer limit
            , @RequestParam(value = "isPage",defaultValue = "true") Boolean isPage) {
        Integer orgId = RequestHeaderUtil.getOrgId();
        if (null == orgId) {
            throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
        }
        val tradeShopOrderDetailsRequest = new GetTradeShopOrderDetailsRequest();
        tradeShopOrderDetailsRequest.setOrgId(orgId);
        tradeShopOrderDetailsRequest.setShopIds(shopIds);
        tradeShopOrderDetailsRequest.setOrderNumber(orderNumber);
        tradeShopOrderDetailsRequest.setSerialNo(serialNo);
        tradeShopOrderDetailsRequest.setDeviceName(deviceName);
        tradeShopOrderDetailsRequest.setMemberCardNo(memberCardNo);
        tradeShopOrderDetailsRequest.setMemberName(memberName);
        tradeShopOrderDetailsRequest.setCardNum(cardNum);
        tradeShopOrderDetailsRequest.setCustomerNumber(customerNumber);
        tradeShopOrderDetailsRequest.setPayType(payType);
        tradeShopOrderDetailsRequest.setOrderStatus(orderStatus);
        tradeShopOrderDetailsRequest.setOrderType(orderType);
        tradeShopOrderDetailsRequest.setStartTime(startTime);
        tradeShopOrderDetailsRequest.setEndTime(endTime);
        tradeShopOrderDetailsRequest.setDateTime(dateTime);
        tradeShopOrderDetailsRequest.setTimeMode(timeMode);
        tradeShopOrderDetailsRequest.setOffset(offset);
        tradeShopOrderDetailsRequest.setLimit(limit);
        tradeShopOrderDetailsRequest.setIsPage(isPage);
        tradeShopOrderDetailsRequest.setDeviceStatus(0);
        return new ResponseData<>(orderStatisticsService.queryShopOrderDetails(tradeShopOrderDetailsRequest));
    }

    @ApiOperation(value = "POS端日交易流水查询接口", notes = "POS端日交易流水查询接口")
    @GetMapping("/shopOrderDetailsForPosNew")
    public ResponseData<GetShopOrderDetailsForPosResponse> queryShopOrderDetailsForPosNew(
            @RequestParam(value = "serialNo", defaultValue = "") String serialNo
            , @RequestParam(value = "dateTime", defaultValue = "") String dateTime
            , @RequestParam(value = "startTime", defaultValue = "") String startTime
            , @RequestParam(value = "endTime", defaultValue = "") String endTime
            , @RequestParam(value = "timeMode", defaultValue = "2") String timeMode
            , @RequestParam(value = "offset", defaultValue = "0") Integer offset
            , @RequestParam(value = "limit", defaultValue = "10") Integer limit
            , @RequestParam(value = "isPage", defaultValue = "true") Boolean isPage) {
        val shopOrderDetailsForPosRequest = new GetShopOrderDetailsForPosRequest();
        shopOrderDetailsForPosRequest.setSerialNo(serialNo);
        shopOrderDetailsForPosRequest.setDateTime(dateTime);
        shopOrderDetailsForPosRequest.setTimeMode(timeMode);
        shopOrderDetailsForPosRequest.setStartTime(startTime);
        shopOrderDetailsForPosRequest.setEndTime(endTime);
        shopOrderDetailsForPosRequest.setOffset(offset);
        shopOrderDetailsForPosRequest.setLimit(limit);
        shopOrderDetailsForPosRequest.setIsPage(isPage);
        return new ResponseData<>(orderStatisticsService.queryShopOrderDetailsForPos(shopOrderDetailsForPosRequest));
    }

    @ApiOperation(value = "POS端日交易流水查询接口", notes = "POS端日交易流水查询接口")
    @GetMapping("/shopOrderDetailsForPos")
    public ResponseData<GetShopOrderDetailsForPosResponse> queryShopOrderDetailsForPos(
            @RequestParam(value = "serialNo", defaultValue = "") String serialNo
            , @RequestParam(value = "dateTime", defaultValue = "") String dateTime
            , @RequestParam(value = "startTime", defaultValue = "") String startTime
            , @RequestParam(value = "endTime", defaultValue = "") String endTime
            , @RequestParam(value = "timeMode", defaultValue = "2") String timeMode
            , @RequestParam(value = "offset", defaultValue = "0") Integer offset
            , @RequestParam(value = "limit", defaultValue = "10") Integer limit
            , @RequestParam(value = "isPage", defaultValue = "true") Boolean isPage) {
        val shopOrderDetailsForPosRequest = new GetShopOrderDetailsForPosRequest();
        shopOrderDetailsForPosRequest.setSerialNo(serialNo);
        shopOrderDetailsForPosRequest.setDateTime(dateTime);
        shopOrderDetailsForPosRequest.setTimeMode(timeMode);
        shopOrderDetailsForPosRequest.setStartTime(startTime);
        shopOrderDetailsForPosRequest.setEndTime(endTime);
        shopOrderDetailsForPosRequest.setOffset(offset);
        shopOrderDetailsForPosRequest.setLimit(limit);
        shopOrderDetailsForPosRequest.setIsPage(isPage);

        GetShopOrderDetailsForPosResponse response = orderStatisticsService.queryShopOrderDetailsForPos(shopOrderDetailsForPosRequest);

        val shopOrderDetails = response.paginationExtendBean.getRows();

        if (null != shopOrderDetails && shopOrderDetails.size() > 0) {
            shopOrderDetails.forEach(shopOrder -> {
                shopOrder.setOrderStatus(transferStatus(shopOrder.getOrderStatus()));
                shopOrder.setOrderStatusName(getOrderStatusName(shopOrder.getOrderStatus()));

                String payType = getOrderPayType(shopOrder.getOrderNumber());
                shopOrder.setPayTypeName(getPayTyperName(payType));
                shopOrder.setPayType(payType);
            });
        }

        return new ResponseData<>(response);
    }

    private String transferStatus(String orderStatus) {
        if(DictionaryConstant.ORDER_UPDATE_STATUS_DFK.getCode().equals(orderStatus)){
            return DictionaryConstant.ORDER_STATUS_DFK.getCode();
        }else if(DictionaryConstant.ORDER_UPDATE_STATUS_ZFWC.getCode().equals(orderStatus)){
            return DictionaryConstant.ORDER_STATUS_YFK.getCode();
        }else if(DictionaryConstant.ORDER_UPDATE_STATUS_DTC.getCode().equals(orderStatus)
                || DictionaryConstant.ORDER_UPDATE_STATUS_TKZ.getCode().equals(orderStatus)){
            return DictionaryConstant.ORDER_STATUS_TKZ.getCode();
        }else if(DictionaryConstant.ORDER_UPDATE_STATUS_DDQX.getCode().equals(orderStatus)){
            return DictionaryConstant.ORDER_STATUS_DDQX.getCode();
        }else if(DictionaryConstant.ORDER_UPDATE_STATUS_TKWC.getCode().equals(orderStatus)
                || DictionaryConstant.ORDER_UPDATE_STATUS_YTC.getCode().equals(orderStatus)){
            return DictionaryConstant.ORDER_STATUS_YTK.getCode();
        }else{
            return orderStatus;
        }
    }

    private String getPayTyperName(String payType) {
        String payTyperName = "";
        if (!StringUtils.isEmpty(payType)) {
            DictionaryConstant dict = DictionaryConstant.getEnumByCode(DictionaryConstant.PAY_TYPE.getCode(), payType);
            payTyperName = dict.getValue();
        }
        return payTyperName;
    }

    private String getOrderPayType(String orderNumber) {
        PayOrder payOrder = orderStatisticsService.getYfkPayOrderInfoByOrderNumber(orderNumber);

        if (payOrder != null) {
            return payOrder.getPayType();
        }
        return "";
    }
//
//    @ApiOperation(value = "微信订单列表查询接口", notes = "微信订单列表查询接口")
//    @GetMapping("/wxOrders")
//    public ResponseData<GetWxOrdersResponse> queryWxOrderDetails(@Valid @ModelAttribute GetWxOrdersRequest wxOrdersRequest, BindingResult bindingResult) {
//        return new ResponseData<>(orderStatisticsService.queryWxOrderList(wxOrdersRequest));
//    }
//
//    @ApiOperation(value = "微信订单查询接口", notes = "微信单个订单查询接口")
//    @GetMapping("/wxOrder")
//    public ResponseData<GetWxOrdersResponse.Order> queryWxOrderWithDetails(@RequestParam(value = "orderNo", defaultValue = "") String orderNo) {
//        return new ResponseData<>(orderStatisticsService.queryWxOrder(orderNo));
//    }

    /**
     * 根据订单状态获取订单状态名称
     *
     * @param orderStatus
     * @return
     */
    private String getOrderStatusName(String orderStatus) {
        String orderStatusName = "";
        if (!StringUtils.isEmpty(orderStatus)) {
            DictionaryConstant dict = DictionaryConstant.getEnumByCode(DictionaryConstant.ORDER_STATUS.getCode(), orderStatus);
            orderStatusName = dict.getValue();
        }
        return orderStatusName;
    }

    /**
     * 根据订单状态获取订单状态名称
     *
     * @param orderStatus
     * @return
     */
    private String getOrderStatusNameNew(String orderStatus) {
        String orderStatusName = "";
        if (!StringUtils.isEmpty(orderStatus)) {
            DictionaryConstant dict = DictionaryConstant.getEnumByCode(DictionaryConstant.ORDER_UPDATE_STATUS.getCode(), orderStatus);
            orderStatusName = dict.getValue();
        }
        return orderStatusName;
    }

}
	