package org.jeecg.modules.system.app.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.system.app.dto.FindPageRequestDTO;
import org.jeecg.modules.system.app.dto.FindResponseDTO;
import org.jeecg.modules.system.app.entity.AppFeedback;
import org.jeecg.modules.system.app.enums.FeedbackTypeEnum;
import org.jeecg.modules.system.app.mapper.AppFeedbackMapper;
import org.jeecg.modules.system.app.service.IAppFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Description: 意见反馈
 * @Author: jeecg-boot
 * @Date:   2025-07-07
 * @Version: V1.0
 */
@Service
public class AppFeedbackServiceImpl extends ServiceImpl<AppFeedbackMapper, AppFeedback> implements IAppFeedbackService {

    @Autowired
    AppFeedbackMapper appFeedbackMapper;

    @Override
    public IPage<FindResponseDTO> findPage(Integer pageNo,Integer pageSize, FindPageRequestDTO findPageRequestDTO) {
        Page<FindResponseDTO> page = new Page<FindResponseDTO>(pageNo, pageSize);
        IPage<FindResponseDTO> findPage = appFeedbackMapper.findPage(page, findPageRequestDTO);
        if (findPage!=null){
            for (FindResponseDTO record : findPage.getRecords()) {
                if (StringUtils.isNotEmpty(record.getFeedbackImg())){
                    imgSplit(record);
                }
                String feedbackName =null;
                FeedbackTypeEnum byCode = FeedbackTypeEnum.getByCode(record.getFeedbackType());
                if (byCode!=null){
                    feedbackName =byCode.getName();
                }
                record.setFeedbackName(feedbackName);
            }
        }
        return findPage;
    }

    @Override
    public FindResponseDTO FindById(String id) {
        FindResponseDTO findResponseDTO = appFeedbackMapper.findById(id);
        if (findResponseDTO!=null&&StringUtils.isNotEmpty(findResponseDTO.getFeedbackImg())){
            imgSplit(findResponseDTO);
        }
        String feedbackName =null;
        FeedbackTypeEnum byCode = FeedbackTypeEnum.getByCode(findResponseDTO.getFeedbackType());
        if (byCode!=null){
            feedbackName =byCode.getName();
        }
        findResponseDTO.setFeedbackName(feedbackName);
        return findResponseDTO;
    }


    private static void imgSplit(FindResponseDTO findResponseDTO ){
        List<String> strings = new ArrayList<>();
        if (findResponseDTO.getFeedbackImg().indexOf(",")>0){
            strings = Arrays.asList(findResponseDTO.getFeedbackImg().split(","));
        }else {
            strings.add(findResponseDTO.getFeedbackImg());

        }
        findResponseDTO.setFeedbackImgList(strings);
    }
}
