package com.xhpc.payment.service.impl;

import com.xhpc.common.core.constant.StatusConstants;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.payment.domain.XhpcRechargeOrder;
import com.xhpc.payment.mapper.XhpcRechargeOrderMapper;
import com.xhpc.payment.service.IXhpcRechargeOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 充值订单信息 服务层
 *
 * @author ruoyi
 */
@Service
public class XhpcRechargeOrderServiceImpl implements IXhpcRechargeOrderService {

    @Autowired
    private XhpcRechargeOrderMapper xhpcRechargeOrderMapper;

    @Autowired
    private XhpcUserAccountStatementServiceImpl xhpcUserAccountStatementService;

    /**
     * 更新 充值订单
     *
     * @param xhpcRechargeOrder 充值订单
     */
    @Override
    @Transactional
    public int update(XhpcRechargeOrder xhpcRechargeOrder) {
        return xhpcRechargeOrderMapper.update(xhpcRechargeOrder);
    }


    /**
     * 充值订单详情
     *
     * @param rechargeOrderId 充值订单id
     * @return 结果
     */
    @Override
    public Map<String, Object> info(Long rechargeOrderId) {
        return xhpcRechargeOrderMapper.info(rechargeOrderId);
    }

    /**
     * 充值订单分页列表
     *
     * @param phone
     * @param rechargeOrderNumber
     * @param status
     * @param createTimeStart
     * @param createTimeEnd
     * @return
     */
    @Override
    public List<Map<String, Object>> page(String phone, String rechargeOrderNumber, String status, String createTimeStart, String createTimeEnd,Integer type) {
        return xhpcRechargeOrderMapper.page(phone, rechargeOrderNumber, status, createTimeStart, createTimeEnd,type);
    }

    @Override
    public String sumMoney(String phone, String rechargeOrderNumber, String status, String createTimeStart, String createTimeEnd, Integer type) {

        return xhpcRechargeOrderMapper.sumMoney(phone, rechargeOrderNumber, status, createTimeStart, createTimeEnd, type);
    }

    /**
     * 新增 充值订单
     *
     * @param appUserId C端用户id
     * @param amount    充值金额
     * @param type      充值渠道（1微信 2支付宝）
     * @return
     */
    @Override
    @Transactional
    public XhpcRechargeOrder addRechargeOrder(String appUserId, BigDecimal amount, String type, String orderNumber) {
        XhpcRechargeOrder xhpcRechargeOrder = new XhpcRechargeOrder();
        xhpcRechargeOrder.setUserId(Long.parseLong(appUserId));
        xhpcRechargeOrder.setAmount(amount);
        xhpcRechargeOrder.setRechargeOrderNumber(orderNumber);
        xhpcRechargeOrder.setType(Integer.parseInt(type));
        xhpcRechargeOrder.setCreateTime(new Date());
        xhpcRechargeOrderMapper.insert(xhpcRechargeOrder);
        return xhpcRechargeOrder;
    }

    /**
     * 修改订单状态 充值订单
     *
     * @param rechargeOrderId 充值订单id
     * @param status          状态（0待支付 1充值成功，2充值失败）
     * @param type            充值渠道（1微信 2支付宝）
     * @return
     */
    @Override
    @Transactional
    public void updateRechargeOrder(Long rechargeOrderId, String type, Integer status, String paymentNumber) {
        XhpcRechargeOrder xhpcRechargeOrder = new XhpcRechargeOrder();
        xhpcRechargeOrder.setRechargeOrderId(rechargeOrderId);
        xhpcRechargeOrder.setStatus(status);
        if (StatusConstants.OPERATION_WX_TYPE.equals(type)) {
            xhpcRechargeOrder.setPrepayId(paymentNumber);
        } else {
            xhpcRechargeOrder.setAlipayNumber(paymentNumber);
        }
//        if (StatusConstants.RECHARGE_ORDER_STATUS_SUCCESS == xhpcRechargeOrder.getStatus()) {
//            Map<String, Object> map = xhpcRechargeOrderMapper.info(rechargeOrderId);
//            String amount = StringUtils.valueOf(map.get("amount"));
//            String userId = StringUtils.valueOf(map.get("userId"));
//            xhpcUserAccountStatementService.add(rechargeOrderId, amount, userId, Integer.parseInt(type));
//        }
        xhpcRechargeOrderMapper.update(xhpcRechargeOrder);
    }

    /**
     * 充值订单详情
     *
     * @param rechargeOrderNumber 充值订单编号
     * @return 结果
     */
    @Override
    public XhpcRechargeOrder infoRechargeOrderNumber(String rechargeOrderNumber) {
        return xhpcRechargeOrderMapper.infoRechargeOrderNumber(rechargeOrderNumber);
    }

    @Override
    public void updateRechargeOrderStatus() {
        xhpcRechargeOrderMapper.updateRechargeOrderStatus();
    }

}
