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.app.dto.evaluate.FindEvaluateDTO;
import org.jeecg.modules.system.app.dto.evaluate.FindEvaluateRequestDTO;
import org.jeecg.modules.system.app.dto.evaluate.FindEvaluateResponseDTO;
import org.jeecg.modules.system.app.entity.Evaluate;
import org.jeecg.modules.system.app.mapper.AppCoursesMapper;
import org.jeecg.modules.system.app.mapper.EvaluateMapper;
import org.jeecg.modules.system.app.service.IEvaluateService;
import org.jeecg.modules.system.app.utils.StringMasker;
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 EvaluateServiceImpl extends ServiceImpl<EvaluateMapper, Evaluate> implements IEvaluateService {

    @Autowired
    EvaluateMapper evaluateMapper;
    @Autowired
    private AppCoursesMapper appCoursesMapper;




    @Override
    public IPage<FindEvaluateResponseDTO> findPage(Integer pageNo, Integer pageSize, FindEvaluateRequestDTO findEvaluateRequestDTO) {
        Page<FindEvaluateResponseDTO> page = new Page<FindEvaluateResponseDTO>(pageNo, pageSize);
        IPage<FindEvaluateResponseDTO> findEvaluateResponseDTOIPage= evaluateMapper.findPage(page, findEvaluateRequestDTO);
        if (findEvaluateResponseDTOIPage!=null&&findEvaluateResponseDTOIPage.getRecords()!=null){
            for (FindEvaluateResponseDTO record : findEvaluateResponseDTOIPage.getRecords()) {
                List<String> strings = new ArrayList<>();
                if (StringUtils.isNotEmpty(record.getImages())&&record.getImages().indexOf(",")>0){
                    strings = Arrays.asList(record.getImages().split(","));
                }else {
                    strings.add(record.getImages());
                }
                if (record.getIsAnonymous()!=null&&record.getIsAnonymous()==0){
                    String username = record.getUsername();
                    String mask = StringMasker.mask(username);
                    record.setUsername(mask);
                }
                record.setImageList(strings);
            }
        }
        return findEvaluateResponseDTOIPage;
    }

    @Override
    public IPage<FindEvaluateResponseDTO> findByOrderPage(FindEvaluateDTO findEvaluateDTO) {
        Page<FindEvaluateResponseDTO> page = new Page<FindEvaluateResponseDTO>(findEvaluateDTO.getPageNo(), findEvaluateDTO.getPageSize());
        IPage<FindEvaluateResponseDTO> findEvaluateResponseDTOIPage=null;
        List<String> coursesIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(findEvaluateDTO.getSiteId())){
            findEvaluateResponseDTOIPage = evaluateMapper.findBySiteId(page,findEvaluateDTO.getSiteId());
        } else if (StringUtils.isNotEmpty(findEvaluateDTO.getInstructorId())) {
            coursesIds = appCoursesMapper.findByInstructorId(findEvaluateDTO.getInstructorId());
        } else if (StringUtils.isNotEmpty(findEvaluateDTO.getCoursesId())) {
            coursesIds.add(findEvaluateDTO.getCoursesId());
        }
        if (coursesIds!=null&&!coursesIds.isEmpty()){
            findEvaluateResponseDTOIPage = evaluateMapper.findByCoursesId(page,coursesIds);
        }
        return findEvaluateResponseDTOIPage;
    }
}
