package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Feedback;
import com.ruoyi.system.domain.FeedbackFile;
import com.ruoyi.system.mapper.FeedbackFileMapper;
import com.ruoyi.system.mapper.FeedbackMapper;
import com.ruoyi.system.service.IFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 问题反馈Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-21
 */
@Service
public class FeedbackServiceImpl implements IFeedbackService
{
    @Autowired
    private FeedbackMapper feedbackMapper;
    @Autowired
    private FeedbackFileMapper feedbackFileMapper;

    /**
     * 查询问题反馈
     * 
     * @param feedbackId 问题反馈ID
     * @return 问题反馈
     */
    @Override
    public Feedback selectFeedbackById(Long feedbackId)
    {
        return feedbackMapper.selectFeedbackById(feedbackId);
    }

    /**
     * 查询问题反馈列表
     * 
     * @param feedback 问题反馈
     * @return 问题反馈
     */
    @Override
    public List<Feedback> selectFeedbackList(Feedback feedback)
    {
        return feedbackMapper.selectFeedbackList(feedback);
    }

    /**
     * 新增问题反馈
     * 
     * @param feedback 问题反馈
     * @return 结果
     */
    @Override
    public int insertFeedback(Feedback feedback)
    {
        feedback.setCreateTime(DateUtils.getNowDate());
        int result= feedbackMapper.insertFeedback(feedback);
        //往附件表中添加
        if(StringUtils.isNotEmpty(feedback.getFileName()) && StringUtils.isNotEmpty(feedback.getFilePath())){

            String[] fileNames = feedback.getFileName().split(",");
            String[] filePaths = feedback.getFilePath().split(",");
            if(fileNames.length == filePaths.length){
                List<FeedbackFile> fileInfoList = new ArrayList<FeedbackFile>();
                for(int i = 0; i < fileNames.length; i++){
                    FeedbackFile fileInfo = new FeedbackFile();
                    fileInfo.setFileName(fileNames[i]);
                    fileInfo.setFilePath(filePaths[i]);
                    fileInfo.setFeedbackId(feedback.getFeedbackId());
                    fileInfoList.add(fileInfo);
                }
                if(fileInfoList.size() > 0){
                   feedbackFileMapper.batchFileInfo(fileInfoList);
                }
            }
        }
        return result;
    }

    /**
     * 修改问题反馈
     * 
     * @param feedback 问题反馈
     * @return 结果
     */
    @Override
    public int updateFeedback(Feedback feedback)
    {
        feedback.setUpdateTime(DateUtils.getNowDate());
        return feedbackMapper.updateFeedback(feedback);
    }

    /**
     * 删除问题反馈对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteFeedbackByIds(String ids)
    {
        return feedbackMapper.deleteFeedbackByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除问题反馈信息
     * 
     * @param feedbackId 问题反馈ID
     * @return 结果
     */
    @Override
    public int deleteFeedbackById(Long feedbackId)
    {
        return feedbackMapper.deleteFeedbackById(feedbackId);
    }
}
