package com.bonus.canteen.core.basic.service.impl;

import java.util.List;
import com.bonus.canteen.core.basic.domain.BasicCanteenSuggestionPicture;
import com.bonus.canteen.core.basic.mapper.BasicCanteenSuggestionPictureMapper;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.basic.mapper.BasicCanteenSuggestionMapper;
import com.bonus.canteen.core.basic.domain.BasicCanteenSuggestion;
import com.bonus.canteen.core.basic.service.IBasicCanteenSuggestionService;
import org.springframework.util.CollectionUtils;

/**
 * 投诉建议 Service业务层处理
 * 
 * @author xsheng
 * @date 2025-05-25
 */
@Service
public class BasicCanteenSuggestionServiceImpl implements IBasicCanteenSuggestionService {
    @Autowired
    private BasicCanteenSuggestionMapper basicCanteenSuggestionMapper;
    @Autowired
    BasicCanteenSuggestionPictureMapper basicCanteenSuggestionPictureMapper;

    /**
     * 查询投诉建议 
     * 
     * @param suggestionId 投诉建议 主键
     * @return 投诉建议 
     */
    @Override
    public BasicCanteenSuggestion selectBasicCanteenSuggestionBySuggestionId(Long suggestionId) {
        List<BasicCanteenSuggestionPicture> suggestionPictures = basicCanteenSuggestionPictureMapper.selectBasicCanteenSuggestionPictureBySuggestionId(suggestionId);
        BasicCanteenSuggestion basicCanteenSuggestion = basicCanteenSuggestionMapper.selectBasicCanteenSuggestionBySuggestionId(suggestionId);
        basicCanteenSuggestion.setSuggestionPictures(suggestionPictures);
        return basicCanteenSuggestion;
    }

    /**
     * 查询投诉建议 列表
     * 
     * @param basicCanteenSuggestion 投诉建议 
     * @return 投诉建议 
     */
    @Override
    public List<BasicCanteenSuggestion> selectBasicCanteenSuggestionList(BasicCanteenSuggestion basicCanteenSuggestion) {
        List<BasicCanteenSuggestion> suggestions = basicCanteenSuggestionMapper.selectBasicCanteenSuggestionList(basicCanteenSuggestion);
        for (BasicCanteenSuggestion suggestion : suggestions) {
            List<BasicCanteenSuggestionPicture> suggestionPictures = basicCanteenSuggestionPictureMapper.selectBasicCanteenSuggestionPictureBySuggestionId(suggestion.getSuggestionId());
            suggestion.setSuggestionPictures(suggestionPictures);
        }
        return suggestions;
    }

    /**
     * 新增投诉建议 
     * 
     * @param basicCanteenSuggestion 投诉建议 
     * @return 结果
     */
    @Override
    public int insertBasicCanteenSuggestion(BasicCanteenSuggestion basicCanteenSuggestion) {
        basicCanteenSuggestion.setCreateTime(DateUtils.getNowDate());
        basicCanteenSuggestion.setCreateBy(SecurityUtils.getUsername());
        try {
            int count = basicCanteenSuggestionMapper.insertBasicCanteenSuggestion(basicCanteenSuggestion);
            long suggestionId = basicCanteenSuggestion.getSuggestionId();
            List<BasicCanteenSuggestionPicture> suggestionPictures = basicCanteenSuggestion.getSuggestionPictures();
            if (!CollectionUtils.isEmpty(suggestionPictures)) {
                for (int i = 0; i < suggestionPictures.size(); i++) {
                    BasicCanteenSuggestionPicture suggestionPicture = new BasicCanteenSuggestionPicture();
                    suggestionPicture.setSuggestionId(suggestionId);
                    suggestionPicture.setImgUrl(suggestionPictures.get(i).getImgUrl());
                    suggestionPicture.setCreateTime(DateUtils.getNowDate());
                    suggestionPicture.setCreateBy(SecurityUtils.getUsername());
                    basicCanteenSuggestionPictureMapper.insertBasicCanteenSuggestionPicture(suggestionPicture);
                }
            }
            return count;
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 修改投诉建议 
     * 
     * @param basicCanteenSuggestion 投诉建议 
     * @return 结果
     */
    @Override
    public int updateBasicCanteenSuggestion(BasicCanteenSuggestion basicCanteenSuggestion) {
        basicCanteenSuggestion.setUpdateTime(DateUtils.getNowDate());
        try {
            return basicCanteenSuggestionMapper.updateBasicCanteenSuggestion(basicCanteenSuggestion);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 批量删除投诉建议 
     * 
     * @param suggestionIds 需要删除的投诉建议 主键
     * @return 结果
     */
    @Override
    public int deleteBasicCanteenSuggestionBySuggestionIds(Long[] suggestionIds) {
        return basicCanteenSuggestionMapper.deleteBasicCanteenSuggestionBySuggestionIds(suggestionIds);
    }

    /**
     * 删除投诉建议 信息
     * 
     * @param suggestionId 投诉建议 主键
     * @return 结果
     */
    @Override
    public int deleteBasicCanteenSuggestionBySuggestionId(Long suggestionId) {
        return basicCanteenSuggestionMapper.deleteBasicCanteenSuggestionBySuggestionId(suggestionId);
    }
}
