package com.kmall.admin.service.impl;

import com.kmall.admin.dao.*;
import com.kmall.admin.entity.*;
import com.kmall.admin.service.OrderWXPayRecordService;
import com.kmall.manager.manager.wechat.WechatUtil;
import com.kmall.common.utils.Constant;
import com.kmall.common.utils.RRException;
import com.kmall.common.utils.wechat.WechatMicropayApiResult;
import com.kmall.common.utils.wechat.WechatRefundApiResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service("orderWXPayRecordService")
public class OrderWXPayRecordServiceImpl
        implements OrderWXPayRecordService {

    @Autowired
    private OrderWXPayRecordDao orderWXPayRecordDao;


    @Override
    public OrderWXPayRecordEntity findByOutTradeNoWX(String outTradeNoWX) {
        if (StringUtils.isEmpty(outTradeNoWX)) {
            throw new RRException("订单编号为空");
        }
        return orderWXPayRecordDao.findByOutTradeNoWX(outTradeNoWX);
    }

    @Override
    public int countByOutTradeNo(String outTradeNo) {
        if (StringUtils.isEmpty(outTradeNo)) {
            throw new RRException("订单编号为空");
        }
        return orderWXPayRecordDao.countByOutTradeNo(outTradeNo);
    }

    @Override
    public List<OrderWXPayRecordEntity> getRecordsByOutTradeNo(String outTradeNo) {
        if (StringUtils.isEmpty(outTradeNo)) {
            throw new RRException("订单编号为空");
        }
        return orderWXPayRecordDao.getRecordByOutTradeNo(outTradeNo);
    }

    @Override
    public OrderWXPayRecordEntity saveRecord(OrderEntity order) {
        if (order == null) {
            throw new RRException("订单信息为空");
        }

        int count = countByOutTradeNo(order.getOrderSn());
        OrderWXPayRecordEntity orderWXPayRecord = new OrderWXPayRecordEntity();
        orderWXPayRecord.setFeeType("CNY");
        orderWXPayRecord.setTotalFee(String.valueOf(order.getActualPrice().multiply(Constant.ONE_HUNDRED).intValue()));
        orderWXPayRecord.setTradeType("MICROPAY");
        orderWXPayRecord.setOutTradeNo(order.getOrderSn());
        orderWXPayRecord.setOutTradeNoWX(order.getOrderSn() + "_" + count);
        orderWXPayRecord.setCreateTime(new Date());
        orderWXPayRecord.setModTime(new Date());
        orderWXPayRecord.setTradeState("NOTPAY");
        orderWXPayRecordDao.save(orderWXPayRecord);
        return orderWXPayRecord;
    }

    @Override
    public int updateRecord(Integer id, WechatMicropayApiResult wechatMicropayApiResult) {
        OrderWXPayRecordEntity orderWXPayRecord = new OrderWXPayRecordEntity();
        orderWXPayRecord.setId(id);
        orderWXPayRecord.setReturnCode(wechatMicropayApiResult.getReturn_code());
        orderWXPayRecord.setReturnMsg(wechatMicropayApiResult.getReturn_msg());
        orderWXPayRecord.setResultCode(wechatMicropayApiResult.getResult_code());
        orderWXPayRecord.setErrCode(wechatMicropayApiResult.getErr_code());
        orderWXPayRecord.setErrCodeDes(wechatMicropayApiResult.getErr_code_des());
        orderWXPayRecord.setOpenid(wechatMicropayApiResult.getOpenid());
        orderWXPayRecord.setIsSubscribe(wechatMicropayApiResult.getIs_subscribe());
        orderWXPayRecord.setTradeType(wechatMicropayApiResult.getTrade_type());
        orderWXPayRecord.setFeeType(wechatMicropayApiResult.getFee_type());
        orderWXPayRecord.setTotalFee(wechatMicropayApiResult.getTotal_fee());
        orderWXPayRecord.setTransactionId(wechatMicropayApiResult.getTransaction_id());
        orderWXPayRecord.setAttach(wechatMicropayApiResult.getAttach());
        orderWXPayRecord.setTimeEnd(wechatMicropayApiResult.getTime_end());
        orderWXPayRecord.setTradeState(wechatMicropayApiResult.getTrade_state());
        orderWXPayRecord.setModTime(new Date());
        orderWXPayRecordDao.update(orderWXPayRecord);
        return 0;
    }

    @Override
    public int updateWXPayRecordTradeState(Integer id, WechatRefundApiResult wechatRefundApiResult) {
        OrderWXPayRecordEntity orderWXPayRecord = new OrderWXPayRecordEntity();
        orderWXPayRecord.setId(id);
        orderWXPayRecord.setTradeState(wechatRefundApiResult.getTrade_state());
        if(WechatUtil.WXTradeState.SUCCESS.getCode().equals(orderWXPayRecord.getTradeState())){
            orderWXPayRecord.setResultCode(orderWXPayRecord.getTradeState());
            orderWXPayRecord.setErrCode("");
            orderWXPayRecord.setErrCodeDes("");
            orderWXPayRecord.setOpenid(wechatRefundApiResult.getOpenid());
            orderWXPayRecord.setIsSubscribe(wechatRefundApiResult.getIs_subscribe());
            orderWXPayRecord.setTradeType(wechatRefundApiResult.getTrade_type());
            orderWXPayRecord.setFeeType(wechatRefundApiResult.getFee_type());
            orderWXPayRecord.setTotalFee(wechatRefundApiResult.getTotal_fee());
            orderWXPayRecord.setTransactionId(wechatRefundApiResult.getTransaction_id());
            orderWXPayRecord.setAttach(wechatRefundApiResult.getAttach());
        }
        orderWXPayRecord.setModTime(new Date());
        orderWXPayRecordDao.update(orderWXPayRecord);
        return 0;
    }
}
