package com.ruoyi.voluntary.service.impl;

import com.ruoyi.common.enums.ECardPayStatus;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.voluntary.domain.BBusiPaymentOrders;
import com.ruoyi.voluntary.mapper.BBusiPaymentOrdersMapper;
import com.ruoyi.voluntary.service.IBBusiPaymentOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 在线电子卡订单Service业务层处理
 * 
 * @author mingxue
 * @date 2022-08-06
 */
@Service
public class BBusiPaymentOrdersServiceImpl implements IBBusiPaymentOrdersService 
{
    @Autowired
    private BBusiPaymentOrdersMapper bBusiPaymentOrdersMapper;

    /**
     * 查询在线电子卡订单
     * 
     * @param id 在线电子卡订单ID
     * @return 在线电子卡订单
     */
    @Override
    public BBusiPaymentOrders selectBBusiPaymentOrdersById(Long id)
    {
        return bBusiPaymentOrdersMapper.selectBBusiPaymentOrdersById(id);
    }

    /**
     * 查询在线电子卡订单列表
     * 
     * @param bBusiPaymentOrders 在线电子卡订单
     * @return 在线电子卡订单
     */
    @Override
    public List<BBusiPaymentOrders> selectBBusiPaymentOrdersList(BBusiPaymentOrders bBusiPaymentOrders)
    {
        return bBusiPaymentOrdersMapper.selectBBusiPaymentOrdersList(bBusiPaymentOrders);
    }

    @Override
    public BBusiPaymentOrders selectBBusiPaymentOrdersNoPaid(String phonenumber)
    {
        BBusiPaymentOrders bBusiPaymentOrders = new BBusiPaymentOrders();
        bBusiPaymentOrders.setPhonenumber(phonenumber);
        bBusiPaymentOrders.setStatus(ECardPayStatus.unpaid.getValue());//状态(-2:已退费,-1:支付失败,0:无效,1:未支付,2:已支付)
        List<BBusiPaymentOrders> list = bBusiPaymentOrdersMapper.selectBBusiPaymentOrdersList(bBusiPaymentOrders);
        if(!CollectionUtils.isEmpty(list)){
            //其他订单全部设置为无效
            for (int i = 0; i < list.size(); i++) {
                if(i!=0){
                    bBusiPaymentOrdersMapper.updateBBusiPaymentOrders(list.get(i).setStatus(ECardPayStatus.payInvalid.getValue()));
                }
            }
            return list.get(0);
        }
        return null;
    }

    @Override
    public BBusiPaymentOrders selectBBusiPaymentOrdersByCardNo(String cardNo) {
        return bBusiPaymentOrdersMapper.selectBBusiPaymentOrdersByCardNo(cardNo);
    }

    /**
     * 新增在线电子卡订单
     * 
     * @param bBusiPaymentOrders 在线电子卡订单
     * @return 结果
     */
    @Override
    public int insertBBusiPaymentOrders(BBusiPaymentOrders bBusiPaymentOrders)
    {
        bBusiPaymentOrders.setCreateTime(DateUtils.getNowDate());
        return bBusiPaymentOrdersMapper.insertBBusiPaymentOrders(bBusiPaymentOrders);
    }

    /**
     * 修改在线电子卡订单
     * 
     * @param bBusiPaymentOrders 在线电子卡订单
     * @return 结果
     */
    @Override
    public int updateBBusiPaymentOrders(BBusiPaymentOrders bBusiPaymentOrders)
    {
        return bBusiPaymentOrdersMapper.updateBBusiPaymentOrders(bBusiPaymentOrders);
    }

    /**
     * 批量删除在线电子卡订单
     * 
     * @param ids 需要删除的在线电子卡订单ID
     * @return 结果
     */
    @Override
    public int deleteBBusiPaymentOrdersByIds(Long[] ids)
    {
        return bBusiPaymentOrdersMapper.deleteBBusiPaymentOrdersByIds(ids);
    }

    /**
     * 删除在线电子卡订单信息
     * 
     * @param id 在线电子卡订单ID
     * @return 结果
     */
    @Override
    public int deleteBBusiPaymentOrdersById(Long id)
    {
        return bBusiPaymentOrdersMapper.deleteBBusiPaymentOrdersById(id);
    }
}
