package com.hwrj.cloud.portal.service.impl;

import com.hwrj.cloud.portal.mapper.OmsOrderMapper;
import com.hwrj.cloud.portal.mapper.OmsOrderPayMapper;
import com.hwrj.cloud.portal.model.OmsOrder;
import com.hwrj.cloud.portal.model.OmsOrderPay;
import com.hwrj.cloud.portal.model.UmsMember;
import com.hwrj.cloud.portal.service.OmsOrderPayService;
import com.hwrj.cloud.portal.service.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class OmsOrderPayServiceImpl implements OmsOrderPayService {
    @Autowired
    private OmsOrderPayMapper omsOrderPayMapper;
    @Autowired
    private UmsMemberService memberService;

    @Autowired
    private OmsOrderMapper omsOrderMapper;

    @Override
    public void insert(OmsOrder omsOrder, String orderNum) {
        UmsMember currentMember = memberService.getCurrentMember();
        OmsOrderPay record = new OmsOrderPay();
        record.setId(orderNum);
        record.setOrderId(omsOrder.getId());
        record.setPayMoney(omsOrder.getPayAmount());
        record.setStatus(0);
        record.setCreateId(currentMember.getId());
        record.setCreateUser(currentMember.getNickname());
        record.setCreateTime(new Date());
        record.setPayType(2);
        omsOrderPayMapper.insertSelective(record);
    }

    @Override
    @Transactional
    public void update(String valueOf) {
        long start=System.currentTimeMillis();

        OmsOrderPay omsOrderPay = omsOrderPayMapper.selectByPrimaryKey(valueOf);
        if(omsOrderPay.getStatus().intValue()!=1){
            long onr=System.currentTimeMillis();
            System.out.println("查询>>>>>>>>>>>>>>:"+(onr-start)/1000+":秒");
            omsOrderPay.setStatus(1);
            omsOrderPay.setUpdateTime(new Date());
            omsOrderPayMapper.updateByPrimaryKeySelective(omsOrderPay);
            long two=System.currentTimeMillis();
            System.out.println("更新1>>>>>>>>>>>>>>:"+(two-onr)/1000+":秒");
            Long orderId = omsOrderPay.getOrderId();
            OmsOrder order =new OmsOrder();
            order.setId(orderId);
            //设置为微信支付
            order.setPayType(2);
            //设置为待发货
            order.setStatus(1);
            //设置支付时间
            order.setPaymentTime(new Date());
            omsOrderMapper.updateByPrimaryKeySelective(order);
            long tree=System.currentTimeMillis();
            System.out.println("更新2>>>>>>>>>>>>>>:"+(tree-two)/1000+":秒");
        }
    }
}
