package com.hai.service.pay.impl;


import com.alibaba.fastjson.JSONObject;
import com.hai.config.QianZhuConfig;
import com.hai.dao.HighGasOrderPushMapper;
import com.hai.entity.HighChildOrder;
import com.hai.entity.HighGasOrderPush;
import com.hai.entity.HighOrder;
import com.hai.entity.HighUser;
import com.hai.enum_type.GoodsType;
import com.hai.enum_type.OrderPushType;
import com.hai.enum_type.PayType;
import com.hai.service.HighOrderService;
import com.hai.service.HighUserService;
import com.hai.service.pay.PayService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Service("kfcOrderService")
public class KfcOrderService implements PayService {

    @Resource
    private HighOrderService highOrderService;

    @Resource
    private HighGasOrderPushMapper highGasOrderPushMapper;

    @Resource
    private HighUserService highUserService;

    @Override
    public void paySuccess(Map<String, String> map, String payType) throws Exception {
        if (payType.equals("Alipay")) {
            return;
        }
        if (payType.equals("WechatPay")) {
            HighOrder order = highOrderService.getOrderByOrderNo(map.get("out_trade_no"));
            if (order != null && order.getOrderStatus() == 1) {
                order.setPaySerialNo(map.get("transaction_id")); // 支付流水号
                order.setPayRealPrice(new BigDecimal(map.get("total_fee")).divide(new BigDecimal("100"))); // 实付金额
                order.setPayTime(new Date()); // 支付时间
                order.setPayModel(2);         // 支付模式：1 金币，2 第三方平台，3 混合
                order.setPayType(2);          // 支付方式： 1：支付宝  2：微信  3：金币
                order.setOrderStatus(2);      // 订单状态：1 待支付  2 已支付  3.已完成  4. 已退款  5.已取消

                for (HighChildOrder childOrder : order.getHighChildOrderList()) {
                    childOrder.setChildOrdeStatus(2);  // 子订单状态：1 待支付  2 已支付  3.已完成  4. 已退款  5.已取消
                }
                highOrderService.updateOrder(order);

                // 支付给千猪
                JSONObject object = QianZhuConfig.payKfcOrder(order.getOrderNo());

                // 推送订单记录
                HighGasOrderPush highGasOrderPush = new HighGasOrderPush();
                highGasOrderPush.setType(OrderPushType.type3.getType());
                highGasOrderPush.setOrderNo(order.getOrderNo());
                highGasOrderPush.setCreateTime(new Date());
                highGasOrderPush.setRequestContent(order.getOrderNo());
                highGasOrderPush.setReturnContent(object.toJSONString());
                highGasOrderPushMapper.insert(highGasOrderPush);
            }
        }
    }
}
