package com.jianshui.invoice.service.impl;

import java.util.List;
import com.jianshui.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.jianshui.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.jianshui.invoice.domain.Redinfodetail;
import com.jianshui.invoice.mapper.RedinfoMapper;
import com.jianshui.invoice.domain.Redinfo;
import com.jianshui.invoice.service.IRedinfoService;

/**
 * 红字信息表Service业务层处理
 * 
 * @author Gong Quanlin
 * @date 2022-03-29
 */
@Service
public class RedinfoServiceImpl implements IRedinfoService 
{
    @Autowired
    private RedinfoMapper redinfoMapper;

    /**
     * 查询红字信息表
     * 
     * @param id 红字信息表主键
     * @return 红字信息表
     */
    @Override
    public Redinfo selectRedinfoById(Long id)
    {
        return redinfoMapper.selectRedinfoById(id);
    }

    /**
     * 查询红字信息表列表
     * 
     * @param redinfo 红字信息表
     * @return 红字信息表
     */
    @Override
    public List<Redinfo> selectRedinfoList(Redinfo redinfo)
    {
        return redinfoMapper.selectRedinfoList(redinfo);
    }

    /**
     * 新增红字信息表
     * 
     * @param redinfo 红字信息表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRedinfo(Redinfo redinfo)
    {
        redinfo.setCreateTime(DateUtils.getNowDate());
        int rows = redinfoMapper.insertRedinfo(redinfo);
        insertRedinfodetail(redinfo);
        return rows;
    }

    /**
     * 修改红字信息表
     * 
     * @param redinfo 红字信息表
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRedinfo(Redinfo redinfo)
    {
        redinfo.setUpdateTime(DateUtils.getNowDate());
        redinfoMapper.deleteRedinfodetailByRedinfoId(redinfo.getId());
        insertRedinfodetail(redinfo);
        return redinfoMapper.updateRedinfo(redinfo);
    }

    /**
     * 批量删除红字信息表
     * 
     * @param ids 需要删除的红字信息表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRedinfoByIds(Long[] ids)
    {
        redinfoMapper.deleteRedinfodetailByRedinfoIds(ids);
        return redinfoMapper.deleteRedinfoByIds(ids);
    }

    /**
     * 删除红字信息表信息
     * 
     * @param id 红字信息表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRedinfoById(Long id)
    {
        redinfoMapper.deleteRedinfodetailByRedinfoId(id);
        return redinfoMapper.deleteRedinfoById(id);
    }

    /**
     * 新增红字信息表详情信息
     * 
     * @param redinfo 红字信息表对象
     */
    public void insertRedinfodetail(Redinfo redinfo)
    {
        List<Redinfodetail> redinfodetailList = redinfo.getRedinfodetailList();
        Long id = redinfo.getId();
        if (StringUtils.isNotNull(redinfodetailList))
        {
            List<Redinfodetail> list = new ArrayList<Redinfodetail>();
            for (Redinfodetail redinfodetail : redinfodetailList)
            {
                redinfodetail.setRedinfoId(id);
                list.add(redinfodetail);
            }
            if (list.size() > 0)
            {
                redinfoMapper.batchRedinfodetail(list);
            }
        }
    }
}
