package com.neusoft.smart.pos.payorder.service;

import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.dto.order.request.*;
import com.neusoft.smart.pos.dto.order.response.*;
import com.neusoft.smart.pos.dto.payorder.request.PostCreatePayOrderRequest;
import com.neusoft.smart.pos.dto.payorder.response.PostPayOrderResponse;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.framework.utils.IdGenerateUtils;
import com.neusoft.smart.pos.framework.utils.ListUtils;
import com.neusoft.smart.pos.payorder.constants.PayErrorCode;
import com.neusoft.smart.pos.payorder.persistent.domain.PayOrder;
import com.neusoft.smart.pos.payorder.repository.PayOrderRepository;
import com.neusoft.smart.pos.utils.SequenceUtils;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service("orderService")
public class PayOrderService {

    @Autowired
    private PayOrderRepository payOrderRepository;

    public PostPayOrderResponse createPayOrder(PostCreatePayOrderRequest createPayOrderRequest) {
        createPayOrderRequest.setPayType(DictionaryConstant.PAY_STATUS_WFK.getCode());
        PayOrder payOrder = savePayOrder(createPayOrderRequest, null);
        val payOrderResponse = new com.neusoft.smart.pos.dto.payorder.response.PostPayOrderResponse();
        BeanUtils.copyProperties(payOrder, payOrderResponse);
        return payOrderResponse;
    }



    /**
     * 订单信息创建方法（含普通订单、充值订单、微信点餐订单）
     *
     * @return
     */
    private PayOrder savePayOrder(PostCreatePayOrderRequest createPayOrderRequest, String tradeId) {
        //1.根据订单类型校验参数信息,并获取集团及门店ID
        validateCreateOrderParams(createPayOrderRequest);

        //2.生成当前订单号
        if (StringUtils.isEmpty(tradeId)) {
            tradeId =  IdGenerateUtils.generateTradeId();    //当前时间毫秒数+5位随机数
        }

//        //3.整理orderDetails数据,并保存
//        if(null != tradeOrderRequest.getOrderDetails()){
//            saveOrderTransations(orderNumber,tradeOrderRequest.getOrderDetails());
//        }

        PayOrder payOrder = new PayOrder();
        BeanUtils.copyProperties(createPayOrderRequest, payOrder);
        payOrder.setTradeId(tradeId);

        payOrderRepository.createPayOrder(payOrder);
        return payOrder;
    }
//
//    /**
//     * 普通订单订单信息保存方法
//     *
//     * @return
//     */
//    public PostOrderResponse saveOrder(PostSaveOrderRequest saveOrderRequest) {
//        OrderMaster orderMaster = new OrderMaster();
//        BeanUtils.copyProperties(saveOrderRequest, orderMaster);
//
//        orderMasterRepository.updateOrder(orderMaster);
//        val orderResponse = new com.neusoft.smart.pos.dto.order.response.PostOrderResponse();
//        BeanUtils.copyProperties(orderMaster, orderResponse);
//        return orderResponse;
//    }
//
//    /**
//     * 普通订单订单取消方法
//     *
//     * @return
//     */
//    public PutCancelOrderResponse cancleOrder(PutCancelOrderRequest cancleOrderRequest) {
//        boolean bResult = orderMasterRepository.updateOrderStatusByOrderNumber(cancleOrderRequest.getOrderNumber(), DictionaryConstant.ORDER_UPDATE_STATUS_DFK.getCode());
//        val cancelOrderResponse = new com.neusoft.smart.pos.dto.order.response.PutCancelOrderResponse(bResult);
//        return cancelOrderResponse;
//    }
//
//    /**
//     * 普通订单自动计算并更新订单状态方法
//     *
//     * @return
//     */
//    public PutUpdateOrderStatusResponse updateOrderStatus(PutUpdateOrderStatusRequest updateOrderStatusRequest) {
//        String orderStatus = orderMasterRepository.computeOrderStatus(updateOrderStatusRequest.getOrderNumber());
//        boolean bResult = orderMasterRepository.updateOrderStatusByOrderNumber(updateOrderStatusRequest.getOrderNumber(), orderStatus);
//        val updateOrderStatusResponse = new com.neusoft.smart.pos.dto.order.response.PutUpdateOrderStatusResponse(bResult);
//        return updateOrderStatusResponse;
//    }
//
//    /**
//     * 设置订单指定状态方法
//     *
//     * @return
//     */
//    public PutSetOrderStatusResponse setOrderStatus(PutSetOrderStatusRequest setOrderStatusRequest) {
//        boolean bResult = orderMasterRepository.updateOrderStatusByOrderNumber(setOrderStatusRequest.getOrderNumber(), setOrderStatusRequest.getOrderStatus());
//        val setOrderStatusResponse = new com.neusoft.smart.pos.dto.order.response.PutSetOrderStatusResponse(bResult);
//        return setOrderStatusResponse;
//    }
//
//    /**
//     * 获得订单全部信息方法
//     *
//     * @return
//     */
//    public GetAllOrderInfoResponse getOrderInfoByOrderNumber(String orderNumber) {
//        OrderMaster orderMaster = orderMasterRepository.queryOrderByOrderNumber(orderNumber);
//        val orderInfoResponse = new com.neusoft.smart.pos.dto.order.response.GetAllOrderInfoResponse();
//        BeanUtils.copyProperties(orderMaster, orderInfoResponse);
//        return orderInfoResponse;
//    }
//
//    /**
//     * 获得订单全部商品信息方法
//     *
//     * @return
//     */
//    public GetOrderDetailsResponse getOrderGoodsListByOrderNumber(String orderNumber) {
//        GetOrderDetailsResponse orderDetailsResponse = new GetOrderDetailsResponse();
//        List<OrderTransaction> orderDetailsList = orderTransactionRepository.queryOrderDetails(orderNumber);
//        List<GetOrderDetailsModelResponse> orderDetailsModelList = ListUtils.convertListToList(orderDetailsList,GetOrderDetailsModelResponse.class);
//        orderDetailsResponse.setOrderDetails(orderDetailsModelList);
//        return orderDetailsResponse;
//    }
//
//    public PostDoPayOrderResponse doPay(PostDoPayOrderRequest doPayOrderRequest) {
//        return null;
//    }
//
//
    /**
     * 根据订单类型校验创建订单相关参数信息,并存入集团ID、门店ID
     */
    private void validateCreateOrderParams(PostCreatePayOrderRequest createPayOrderRequest) {
        //支付类型：现金 银行卡 微信 支付宝 云闪付 虚拟货币
        if (StringUtils.isEmpty(createPayOrderRequest.getPayType())) {
            //订单类型不能为空
            throw new BusinessCommonException(PayErrorCode.PAY_TYPE_IS_EMPTY);
        }
        if (null == createPayOrderRequest.getPayAmount()) {
            //订单支付金额不能为空
            throw new BusinessCommonException(PayErrorCode.PAY_AMOUNT_IS_EMPTY);
        }
        if (createPayOrderRequest.getPayAmount().doubleValue() < 0) {
            //订单支付金额输入错误
            throw new BusinessCommonException(PayErrorCode.PAY_AMOUNT_ERROR);
        }

//        DictionaryConstant dict = DictionaryConstant.getEnumByCode(DictionaryConstant.ORDER_TYPE.getCode(),tradeOrderRequest.getOrderType());
//        switch(dict){
//            case ORDER_TYPE_ZC:
//                //POS端正常订单
//                validateOrderParamsByZC(tradeOrderRequest);
//                break;
//            case ORDER_TYPE_HYCZ:
//                //会员卡充值订单
//                validateOrderParamsByHYCZ(tradeOrderRequest);
//                break;
//            case ORDER_TYPE_WXDC:
//                //微信点餐订单
//                validateOrderParamsByWXDC(tradeOrderRequest);
//                break;
//            default:
//                //订单类型错误
//                throw new BusinessCommonException(TradeErrorCode.ORDER_TYPE_ERROR);
//        }
    }
}
