package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.SysResponse;
import com.ruoyi.system.mapper.SysFeedbackMapper;
import com.ruoyi.system.domain.SysFeedback;
import com.ruoyi.system.service.ISysFeedbackService;

/**
 * feedbackService业务层处理
 * 
 * @author ruoyi
 * @date 2023-06-11
 */
@Service
public class SysFeedbackServiceImpl extends ServiceImpl<BaseMapper<SysFeedback>,SysFeedback> implements ISysFeedbackService
{
    @Autowired
    private SysFeedbackMapper sysFeedbackMapper;

    /**
     * 查询feedback
     * 
     * @param id feedback主键
     * @return feedback
     */
    @Override
    public SysFeedback selectSysFeedbackById(Long id)
    {
        return sysFeedbackMapper.selectSysFeedbackById(id);
    }

    /**
     * 查询feedback列表
     * 
     * @param sysFeedback feedback
     * @return feedback
     */
    @Override
    public List<SysFeedback> selectSysFeedbackList(SysFeedback sysFeedback)
    {
        return sysFeedbackMapper.selectSysFeedbackList(sysFeedback);
    }

    /**
     * 新增feedback
     * 
     * @param sysFeedback feedback
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysFeedback(SysFeedback sysFeedback)
    {
        int rows = sysFeedbackMapper.insertSysFeedback(sysFeedback);
        insertSysResponse(sysFeedback);
        return rows;
    }

    /**
     * 修改feedback
     * 
     * @param sysFeedback feedback
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSysFeedback(SysFeedback sysFeedback)
    {
        sysFeedbackMapper.deleteSysResponseByFeId(sysFeedback.getId());
        insertSysResponse(sysFeedback);
        return sysFeedbackMapper.updateSysFeedback(sysFeedback);
    }

    /**
     * 批量删除feedback
     * 
     * @param ids 需要删除的feedback主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysFeedbackByIds(Long[] ids)
    {
        sysFeedbackMapper.deleteSysResponseByFeIds(ids);
        return sysFeedbackMapper.deleteSysFeedbackByIds(ids);
    }

    /**
     * 删除feedback信息
     * 
     * @param id feedback主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysFeedbackById(Long id)
    {
        sysFeedbackMapper.deleteSysResponseByFeId(id);
        return sysFeedbackMapper.deleteSysFeedbackById(id);
    }

    /**
     * 新增response信息
     * 
     * @param sysFeedback feedback对象
     */
    public void insertSysResponse(SysFeedback sysFeedback)
    {
        List<SysResponse> sysResponseList = sysFeedback.getSysResponseList();
        Long id = sysFeedback.getId();
        if (StringUtils.isNotNull(sysResponseList))
        {
            List<SysResponse> list = new ArrayList<SysResponse>();
            for (SysResponse sysResponse : sysResponseList)
            {
                sysResponse.setFeId(id);
                list.add(sysResponse);
            }
            if (list.size() > 0)
            {
                sysFeedbackMapper.batchSysResponse(list);
            }
        }
    }
}
