package com.lightinit.hsdataportal.impl;


import com.lightinit.hsdataportal.dictionary.DicOrderState;
import com.lightinit.hsdataportal.entity.Order;
import com.lightinit.hsdataportal.entity.OrderExample;
import com.lightinit.hsdataportal.entity.UserAccount;
import com.lightinit.hsdataportal.mapper.OrderMapper;
import com.lightinit.hsdataportal.mapper.UserAccountMapper;
import com.lightinit.hsdataportal.model.ResultState;
import com.lightinit.hsdataportal.model.ResultStateCode;
import com.lightinit.hsdataportal.service.IAlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

/**
 * @Author: sunxiang
 * @Date: 2018/10/29 10:56
 * @Description:
 **/
@Service
public class AlipayServiceImpl implements IAlipayService {


    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserAccountMapper userAccountMapper;
   /*@Description 根据订单id查询订单
    *@Param
    *@Date 2018/10/29
    *@return
    */
    @Override
    public Order queryUserOrderByOrderNumber(String orderNumber) {
        OrderExample bizOrderExample = new OrderExample();
        bizOrderExample.createCriteria().andOrderNumberEqualTo(orderNumber);
        List<Order> list = orderMapper.selectByExample(bizOrderExample);
        if(list==null||list.size()!=1){
            return null;
        }
        return list.get(0);
    }


    @Override
    public ResultState<String> updateUserOderByOrderNumber(Long id, String orderNumber) {
        ResultState<String> resultState = new ResultState<>();
        try {
            Order order = new Order();
            order.setId(id);
            order.setOrderState(DicOrderState.PAY_SUCCESS);
            orderMapper.updateByPrimaryKeySelective(order);
            //查询订单的业务类型，如果是充值，改变用户账户的金额
            Order order1 = orderMapper.selectBusinessType(orderNumber);
            if("账户充值".equals(order1.getBusinessType())) {
                UserAccount userAccount = new UserAccount();
                UserAccount userAccount1 = userAccountMapper.selectByPrimaryKey(order1.getUserId());
                if(userAccount1==null) {
                    userAccount.setBalance(order1.getOrderAmount());
                    userAccountMapper.insert(userAccount);
                }
                userAccount.setBalance(userAccount1.getBalance().add(order1.getOrderAmount()));
                userAccountMapper.updateByPrimaryKeySelective(userAccount);
            }
            resultState.setData(orderNumber);
            return resultState;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        resultState.setStateCode(ResultStateCode.DB_ERROR);
        resultState.setMsg("biz_order数据库‘支付成功’修改失败，请手动修改id="+id+",或者订单号是:"+orderNumber);
        return resultState;
    }

    /*@Transactional
    @Override
    public ResultState<String> insertBizOrderPayment(BizOrder order, int stateCode) {
        ResultState<String> resultState = new ResultState<>();
        BizOrderPayment payment = new BizOrderPayment();
        payment.setOrderNumber(order.getOrderNumber());
        payment.setPaymentMethod(DicPayType.ALIPAY);
        payment.setPaymentTime(new Date());
        payment.setPaymentResult(stateCode==200? DicPayResult.UPDATE_SUCCESS:DicPayResult.UPDATE_FAIL);
        payment.setRemark(order.getRemark());
        payment.setCreatedAt(new Date());
        int i = bizOrderPaymentMapper.insert(payment);
        if(i<1){
            resultState.setStateCode(ResultStateCode.DB_ERROR);
            resultState.setData(order.getOrderNumber());
            resultState.setMsg("插入biz_order_payment数据库失败，请手动添加该订单！订单号："+order.getOrderNumber());
        }
        resultState.setData(order.getOrderNumber());
        return resultState;
    }*/
}
