package com.ruoyi.voluntary.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.voluntary.domain.BusiCardsOpenDetail;
import com.ruoyi.voluntary.mapper.BusiCardsOpenMapper;
import com.ruoyi.voluntary.domain.BusiCardsOpen;
import com.ruoyi.voluntary.service.IBusiCardsOpenService;

/**
 * 开卡Service业务层处理
 * 
 * @author voluntary
 * @date 2022-12-03
 */
@Service
public class BusiCardsOpenServiceImpl implements IBusiCardsOpenService 
{
    @Autowired
    private BusiCardsOpenMapper busiCardsOpenMapper;

    /**
     * 查询开卡
     * 
     * @param id 开卡主键
     * @return 开卡
     */
    @Override
    public BusiCardsOpen selectBusiCardsOpenById(Long id)
    {
        return busiCardsOpenMapper.selectBusiCardsOpenById(id);
    }

    /**
     * 查询开卡列表
     * 
     * @param busiCardsOpen 开卡
     * @return 开卡
     */
    @Override
    public List<BusiCardsOpen> selectBusiCardsOpenList(BusiCardsOpen busiCardsOpen)
    {
        return busiCardsOpenMapper.selectBusiCardsOpenList(busiCardsOpen);
    }

    /**
     * 新增开卡
     * 
     * @param busiCardsOpen 开卡
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBusiCardsOpen(BusiCardsOpen busiCardsOpen)
    {
        busiCardsOpen.setCreateTime(DateUtils.getNowDate());
        int rows = busiCardsOpenMapper.insertBusiCardsOpen(busiCardsOpen);
        insertBusiCardsOpenDetail(busiCardsOpen);
        return rows;
    }

    /**
     * 修改开卡
     * 
     * @param busiCardsOpen 开卡
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBusiCardsOpen(BusiCardsOpen busiCardsOpen)
    {
        busiCardsOpenMapper.deleteBusiCardsOpenDetailByOpenId(busiCardsOpen.getId());
        insertBusiCardsOpenDetail(busiCardsOpen);
        return busiCardsOpenMapper.updateBusiCardsOpen(busiCardsOpen);
    }

    /**
     * 批量删除开卡
     * 
     * @param ids 需要删除的开卡主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBusiCardsOpenByIds(Long[] ids)
    {
        busiCardsOpenMapper.deleteBusiCardsOpenDetailByOpenIds(ids);
        return busiCardsOpenMapper.deleteBusiCardsOpenByIds(ids);
    }

    /**
     * 删除开卡信息
     * 
     * @param id 开卡主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBusiCardsOpenById(Long id)
    {
        busiCardsOpenMapper.deleteBusiCardsOpenDetailByOpenId(id);
        return busiCardsOpenMapper.deleteBusiCardsOpenById(id);
    }

    /**
     * 新增开卡详情信息
     * 
     * @param busiCardsOpen 开卡对象
     */
    public void insertBusiCardsOpenDetail(BusiCardsOpen busiCardsOpen)
    {
        List<BusiCardsOpenDetail> busiCardsOpenDetailList = busiCardsOpen.getBusiCardsOpenDetailList();
        Long id = busiCardsOpen.getId();
        if (StringUtils.isNotNull(busiCardsOpenDetailList))
        {
            List<BusiCardsOpenDetail> list = new ArrayList<BusiCardsOpenDetail>();
            for (BusiCardsOpenDetail busiCardsOpenDetail : busiCardsOpenDetailList)
            {
                busiCardsOpenDetail.setOpenId(id);
                list.add(busiCardsOpenDetail);
            }
            if (list.size() > 0)
            {
                busiCardsOpenMapper.batchBusiCardsOpenDetail(list);
            }
        }
    }
}
