package com.vote.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vctgo.common.core.exception.ServiceException;
import com.vctgo.common.core.text.Convert;
import com.vctgo.common.core.utils.ServletUtils;
import com.vctgo.common.mybatisplus.constant.MybatisPageConstants;
import com.vctgo.system.api.RemoteBranchKindergartenService;
import com.vctgo.system.api.RemoteFileService;
import com.vctgo.system.api.RemotePlatformUserService;
import com.vctgo.system.api.RemoteUserService;
import com.vctgo.system.api.domain.FileInfo;
import com.vctgo.system.api.domain.SysPlatformUser;
import com.vctgo.system.api.domain.SysUser;
import com.vctgo.system.api.domain.dto.SysBranchKindergartenDto;
import com.vctgo.system.api.domain.vo.VoteFileVo;
import com.vote.system.domain.*;
import com.vote.system.domain.dto.VoteSubmissionDto;
import com.vote.system.domain.dto.VoteSubmissionItemDto;
import com.vote.system.domain.vo.VoteSubmissionCountVo;
import com.vote.system.domain.vo.VoteSubmissionItemVo;
import com.vote.system.domain.vo.VoteSubmissionVo;
import com.vote.system.mapper.*;
import com.vote.system.service.VoteSubmissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.vote.system.util.ConstantUtils.*;

/**
 * @author MacBook
 * @description 针对表【vote_submission】的数据库操作Service实现
 * @createDate 2024-08-20 15:39:44
 */
@Service
public class VoteSubmissionServiceImpl extends ServiceImpl<VoteSubmissionMapper, VoteSubmission>
        implements VoteSubmissionService {

    @Autowired
    private VoteSubmissionMapper voteSubmissionMapper;

    @Autowired
    private VoteWorkTypesMapper voteWorkTypesMapper;

    @Autowired
    private VoteEventWorkSettingsMapper voteEventWorkSettingsMapper;

    @Autowired
    private VoteEvaluationTasksMapper voteEvaluationTasksMapper;

    @Autowired
    private VoteEventEvaluationRoundsMapper voteEventEvaluationRoundsMapper;

    @Autowired
    private VoteEventAwardsMapper voteEventAwardsMapper;

    @Autowired
    private VoteRegistrationsMapper voteRegistrationsMapper;

    @Autowired
    private VoteScoringItemMapper voteScoringItemMapper;

    @Autowired
    private VoteSubmissionItemMapper voteSubmissionItemMapper;


    @Autowired
    private RemoteFileService remoteFileService;

    @Autowired
    private RemoteBranchKindergartenService remoteBranchKindergartenService;

    @Autowired
    private RemotePlatformUserService remotePlatformUserService;

    @Autowired
    private VoteEventRegistrationWorkScoresMapper voteEventRegistrationWorkScoresMapper;

    @Autowired
    private VoteHistoryScoreMapper voteHistoryScoreMapper;


    /**
     * 分页查询 参赛作品列表
     *
     * @param voteSubmissionDto
     * @return
     */
    @Override
    public IPage<VoteSubmissionVo> selectPage(VoteSubmissionDto voteSubmissionDto) {
        long pageNum = Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_NUM), 1L);
        long pageSize = Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_SIZE), 10L);
        Page<VoteSubmissionVo> page = new Page<>(pageNum, pageSize);
        List<VoteSubmission> voteSubmissions = getVoteSubmissions(voteSubmissionDto);
        if (voteSubmissions.isEmpty()) {
            page.setRecords(Collections.emptyList());
            page.setTotal(voteSubmissions.size());
            return page;
        }

        //获取工作类型数据
        Map<Long, VoteEventWorkSettings> workSettingsMap = voteEventWorkSettingsMapper.selectList(new LambdaQueryWrapper<>())
                .stream().collect(Collectors.toMap(VoteEventWorkSettings::getId, Function.identity()));
        // 构建返回的VO列表
        List<VoteSubmissionVo> voList = voteSubmissions.stream()
                .map(item -> {
                    VoteSubmissionVo vo = new VoteSubmissionVo();
                    vo.setId(item.getId());
                    vo.setName(item.getName());
                    LambdaQueryWrapper<VoteRegistrations> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(VoteRegistrations::getUid, item.getUserId()).eq(VoteRegistrations::getEventId, item.getEventId());
                    List<VoteRegistrations> registrations = voteRegistrationsMapper.selectList(wrapper);
                    vo.setCommitName(registrations.get(0).getName());
                    vo.setCommitTime(item.getCreateAt());
                    vo.setPhone(registrations.get(0).getPhone());
                    vo.setStatus(item.getStatus());
                    //设置类型名称
                    VoteEventWorkSettings workSettings = workSettingsMap.get(item.getWorkSettingsId());
                    VoteWorkTypes workTypes = voteWorkTypesMapper.selectById(workSettings.getWorkTypeId());
                    vo.setWorkTypeName(workTypes != null ? workTypes.getName() : "暂未设置类型");
                    SysBranchKindergartenDto data = getSysBranchKindergartenDto(item);
                    vo.setLocationName(data.getKindergartenName());
                    return vo;
                }).collect(Collectors.toList());
        page.setRecords(voList);
        page.setTotal(voList.size());
        return page;
    }

    //获取园所名称
    private SysBranchKindergartenDto getSysBranchKindergartenDto(VoteSubmission item) {
        return remoteBranchKindergartenService.getById(item.getLocationId()).getData();
    }


    //构建查询条件方法
    private List<VoteSubmission> getVoteSubmissions(VoteSubmissionDto voteSubmissionDto) {
        List<Long> userIds = new ArrayList<>();
        boolean locationFilterApplied = false;
        boolean keywordFilterApplied = false; // 新增标志
        LambdaQueryWrapper<VoteRegistrations> queryWrapper = new LambdaQueryWrapper<>();
        if (voteSubmissionDto.getProvinceId() != null) {
            queryWrapper.eq(VoteRegistrations::getProvinceId, voteSubmissionDto.getProvinceId());
            if (voteSubmissionDto.getCityId() != null) {
                queryWrapper.eq(VoteRegistrations::getCityId, voteSubmissionDto.getCityId());
            }
            if (voteSubmissionDto.getDistrictId() != null) {
                queryWrapper.eq(VoteRegistrations::getDistrictId, voteSubmissionDto.getDistrictId());
            }
            if (voteSubmissionDto.getLocationId() != null) {
                queryWrapper.eq(VoteRegistrations::getLocationId, voteSubmissionDto.getLocationId());
            }
            userIds = voteRegistrationsMapper.selectList(queryWrapper).stream()
                    .map(VoteRegistrations::getUid)
                    .collect(Collectors.toList());
            locationFilterApplied = true;
        }
        LambdaQueryWrapper<VoteSubmission> wrapper = new LambdaQueryWrapper<>();
        if (voteSubmissionDto.getEventId() == null) {
            throw new RuntimeException("活动id为空 暂时无法查询");
        } else {
            wrapper.eq(VoteSubmission::getEventId, voteSubmissionDto.getEventId());
        }
        if (voteSubmissionDto.getWorkSettingsId() != null) {
            wrapper.eq(VoteSubmission::getWorkSettingsId, voteSubmissionDto.getWorkSettingsId());
        }
        if (locationFilterApplied && !userIds.isEmpty()) {
            wrapper.in(VoteSubmission::getUserId, userIds);
        }
        if (voteSubmissionDto.getStatus() != null) {
            wrapper.eq(VoteSubmission::getStatus, voteSubmissionDto.getStatus());
        }
        // 修改关键词查询逻辑
        if (voteSubmissionDto.getKeyword() != null) {
            LambdaQueryWrapper<VoteRegistrations> registrationsWrapper = new LambdaQueryWrapper<>();
            registrationsWrapper.like(VoteRegistrations::getName, voteSubmissionDto.getKeyword())
                    .or()
                    .like(VoteRegistrations::getPhone, voteSubmissionDto.getKeyword());

            List<Long> filteredUserIds = voteRegistrationsMapper.selectList(registrationsWrapper).stream()
                    .map(VoteRegistrations::getUid)
                    .collect(Collectors.toList());

            if (!filteredUserIds.isEmpty()) {
                wrapper.in(VoteSubmission::getUserId, filteredUserIds);
                keywordFilterApplied = true; // 标记关键词过滤已应用
            }
        }
        if (voteSubmissionDto.getRoundId() != null) {
            wrapper.eq(VoteSubmission::getRoundId, voteSubmissionDto.getRoundId());
        }
        if (voteSubmissionDto.getLocationId() != null) {
            wrapper.eq(VoteSubmission::getLocationId, voteSubmissionDto.getLocationId());
        }
        if (voteSubmissionDto.getPromoteStatus() != null) {
            wrapper.eq(VoteSubmission::getPromoteStatus, voteSubmissionDto.getPromoteStatus());
        }
        wrapper.orderByDesc(VoteSubmission::getCreateAt);
        // 执行查询
        List<VoteSubmission> submissions = voteSubmissionMapper.selectList(wrapper);
        // 如果查询结果为空且没有其他条件，返回空列表
        if (submissions.isEmpty() && !locationFilterApplied && !keywordFilterApplied) {
            return new ArrayList<>();
        }
        return submissions;
    }


    /**
     * 查询单个详情
     *
     * @param id
     * @return
     */
    @Override
    public VoteSubmissionVo selectById(Long id) {
        VoteSubmission submission = voteSubmissionMapper.selectById(id);
        if (submission == null) {
            throw new NoSuchElementException("VoteSubmission not found with id: " + id);
        }

        VoteSubmissionVo vo = new VoteSubmissionVo();
        String string = submission.getFile() + "," + submission.getImages() + "," + submission.getVideo();
        List<FileInfo> fileInfoList = remoteFileService.selectUrlByIds(string).getData();
        // 检查 fileInfoList 是否为 null 或者为空
        if (fileInfoList == null || fileInfoList.isEmpty()) {
            // 如果 fileInfoList 为空，可以设置一些默认值或记录日志
            vo.setVideoFiles(Collections.emptyList());
            vo.setDocumentFiles(Collections.emptyList());
            vo.setImageFiles(Collections.emptyList());
        } else {
            // 初始化图片 URL 列表
            List<VoteFileVo> imageFiles = new ArrayList<>();
            List<VoteFileVo> videoFiles = new ArrayList<>();
            List<VoteFileVo> documentFiles = new ArrayList<>();
            for (FileInfo fileInfo : fileInfoList) {
                VoteFileVo fileVo = new VoteFileVo();
                fileVo.setFileUrl(fileInfo.getFilePreviewUrl()); // 设置文件预览URL
                fileVo.setFileName(fileInfo.getFileStorageName());

                switch (fileInfo.getFileType()) {
                    case "1":  // 文件
                        documentFiles.add(fileVo);
                        break;
                    case "2":  // 图片类型
                        imageFiles.add(fileVo);
                        break;
                    case "3":  // 视频类型
                        videoFiles.add(fileVo);
                        break;
                    default:
                        break;
                }
            }

            vo.setVideoFiles(videoFiles)
                    .setDocumentFiles(documentFiles)
                    .setImageFiles(imageFiles);
        }
        vo.setName(submission.getName())
                .setMainAuthor(submission.getMainAuthor())
                .setTeamWorker(submission.getTeamWorker());

        if (CREATE_TYPE_PERSONAL.equals(submission.getCreateType())) {
            vo.setCreateType(CREATE_TYPE_PERSONAL_DESC);
        } else {
            vo.setCreateType(CREATE_TYPE_TEAM_DESC);
        }

        if (submission.getAgeSegment() != null && !submission.getAgeSegment().isEmpty()) {
            StringBuilder builder = new StringBuilder();
            String[] split = submission.getAgeSegment().split(",");
            for (String s : split) {
                if (s.equals(ZHONG_BAN_CODE)) {
                    builder.append(ZHONG_BAN_DESC);
                } else if (s.equals(DA_BAN_CODE)) {
                    builder.append(DA_BAN_DESC);
                } else {
                    builder.append(XIAO_BAN_DESC);
                }
            }
            vo.setAgeSegment(builder.toString());
        } else {
            vo.setAgeSegment("");
        }

        return vo;
    }

    /**
     * 评选结果 分页查询
     */
    @Override
    public IPage<VoteSubmissionVo> getResult(VoteSubmissionDto voteSubmissionDto) {
        // 获取分页参数
        long pageNum = Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_NUM), 1L);
        long pageSize = Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_SIZE), 10L);

        // 获取作品列表和专家数据
        List<VoteSubmission> voteSubmissions = getVoteSubmissions(voteSubmissionDto);
        if (voteSubmissions == null) {
            voteSubmissions = Collections.emptyList(); // 防止空指针
        }

        // 将专家列表转换为以judgeId为键，专家姓名为值的Map
        Map<String, String> judgeMap = Optional.ofNullable(remotePlatformUserService.processExpertList().getData())
                .orElse(Collections.emptyList()).stream()
                .collect(Collectors.toMap(
                        user -> user.getUserId().toString(),
                        SysPlatformUser::getNickName
                ));

        // 预先生成 submissionId 到 judgeId 的映射
        Map<Long, String> submissionJudgeMap = new HashMap<>();
        for (VoteSubmission submission : voteSubmissions) {
            String judgeId = Optional.ofNullable(voteEvaluationTasksMapper.selectOne(
                    new LambdaQueryWrapper<VoteEvaluationTasks>()
                            .eq(VoteEvaluationTasks::getSubmissionId, submission.getId())
                            .eq(VoteEvaluationTasks::getRoundId, submission.getRoundId())
            )).map(VoteEvaluationTasks::getJudgeId).orElse(null);
            submissionJudgeMap.put(submission.getId(), judgeId);
        }


        // 预先生成 submissionId 到 score 的映射
        Map<Long, BigDecimal> submissionScoreMap = new HashMap<>();
        for (VoteSubmission submission : voteSubmissions) {
            BigDecimal score = BigDecimal.ZERO;
            try {
                VoteEventRegistrationWorkScores workScore = voteEventRegistrationWorkScoresMapper.selectOne(
                        new LambdaQueryWrapper<VoteEventRegistrationWorkScores>()
                                .eq(VoteEventRegistrationWorkScores::getEventId, submission.getEventId())
                                .eq(VoteEventRegistrationWorkScores::getWorkSettingsId, submission.getWorkSettingsId())
                                .eq(VoteEventRegistrationWorkScores::getRoundId, submission.getRoundId())
                                .eq(VoteEventRegistrationWorkScores::getSubmissionId, submission.getId())
                );
                score = (workScore != null && workScore.getScore() != null) ? workScore.getScore() : BigDecimal.ZERO;
            } catch (Exception e) {
                // 处理异常，记录日志或进行其他处理
            }
            submissionScoreMap.put(submission.getId(), score);
        }

        // 将VoteSubmission转换为VoteSubmissionVo
        List<VoteSubmissionVo> vos = voteSubmissions.stream()
                .map(submission -> {
                    VoteSubmissionVo vo = new VoteSubmissionVo();
                    vo.setName(submission.getName())
                            .setId(submission.getId());
                    LambdaQueryWrapper<VoteRegistrations> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(VoteRegistrations::getUid, submission.getUserId()).eq(VoteRegistrations::getEventId, submission.getEventId());
                    List<VoteRegistrations> registrations = voteRegistrationsMapper.selectList(wrapper);
                    vo.setCommitName(registrations.get(0).getName());
                    vo.setPhone(registrations.get(0).getPhone());

                    VoteEventWorkSettings eventWorkSettings = voteEventWorkSettingsMapper.selectById(submission.getWorkSettingsId());
                    vo.setWorkTypeName(eventWorkSettings.getAlias());

                    // 根据PromoteStatus设置状态
                    vo.setPromoteStatus(Optional.ofNullable(submission.getPromoteStatus()).orElse(UNKNOWN));

                    if (submission.getAward() != null && !submission.getAward().isEmpty()) {
                        vo.setAward(submission.getAward());
                    } else {
                        vo.setAward(PENDING);
                    }
                    // 设置园所名称
                    SysBranchKindergartenDto data = getSysBranchKindergartenDto(submission);
                    vo.setLocationName(data != null ? data.getKindergartenName() : null);

                    // 从映射表中获取并设置专家姓名
                    vo.setJudge(judgeMap.get(submissionJudgeMap.get(submission.getId())));

                    // 从映射表中获取并设置分数
                    vo.setScore(submissionScoreMap.get(submission.getId()));
                    return vo;
                })
                .sorted(Comparator.comparing(VoteSubmissionVo::getScore, Comparator.nullsLast(Comparator.reverseOrder()))) // 按分数降序排序
                .collect(Collectors.toList());

        // 为每个项目分配排序序号
        for (int i = 0; i < vos.size(); i++) {
            vos.get(i).setSort(i + 1);
        }
        // 进行分页
        Page<VoteSubmissionVo> page = new Page<>(pageNum, pageSize);
        List<VoteSubmissionVo> paginatedRecords = vos.stream()
                .skip((pageNum - 1) * pageSize)
                .limit(pageSize)
                .collect(Collectors.toList());
        page.setRecords(paginatedRecords);
        page.setTotal(vos.size());

        return page;
    }

    /**
     * 评选结果 （评选轮） 全部作品类型数量
     */
    @Override
    public VoteSubmissionCountVo getAllSubmissionCount(VoteSubmissionDto voteSubmissionDto) {
        VoteSubmissionCountVo countVo = new VoteSubmissionCountVo();
        List<VoteSubmission> voteSubmissions = getVoteSubmissions(voteSubmissionDto);
        List<VoteSubmission> pendingCount = voteSubmissions.stream().filter(voteSubmission -> voteSubmission.getPromoteStatus().equals(PENDING_1)).collect(Collectors.toList());
        List<VoteSubmission> promoteCount = voteSubmissions.stream().filter(voteSubmission -> voteSubmission.getPromoteStatus().equals(PROMOTE_0)).collect(Collectors.toList());
        countVo.setPendingCount(pendingCount.size());
        countVo.setAllSubmission(voteSubmissions.size());
        countVo.setPromoteCount(promoteCount.size());
        return countVo;
    }

    /**
     * 评选结果 （评选轮） 晋级
     */
    @Override
    @Transactional
    public int promote(VoteSubmissionDto voteSubmissionDto) {
        VoteSubmission submission = voteSubmissionMapper.selectById(voteSubmissionDto.getId());
        if (submission.getPromoteStatus().equals(PROMOTE_0)) {
            throw new ServiceException("已晋级的作品不能继续晋级");
        }
        VoteEventWorkSettings eventWorkSettings = voteEventWorkSettingsMapper.selectById(voteSubmissionDto.getWorkSettingsId());
        String[] split = eventWorkSettings.getRoundsId().split(",");
        List<Long> roundIds = Arrays.stream(split).map(Long::parseLong).collect(Collectors.toList());
        //创建修改对象
        VoteSubmission voteSubmission = new VoteSubmission();
        voteSubmission.setId(voteSubmissionDto.getId())
                .setPromoteStatus(PROMOTE_0)
                .setAppraiseStatus("1");

        //创建历史评分记录
        VoteHistoryScore historyScore = new VoteHistoryScore();
        if (roundIds.size() > 1) {
            int currentIndex = roundIds.indexOf(voteSubmissionDto.getRoundId());
            if (currentIndex >= 0 && currentIndex < roundIds.size() - 1) {
                //获取当前索引的后一个元素
                Long nextRoundId = roundIds.get(currentIndex + 1);
                voteSubmission.setRoundId(nextRoundId);
                voteSubmission.setPromoteStatus(PENDING_1);
                historyScore.setRoundId(nextRoundId);
            }
        }
        VoteEventEvaluationRounds rounds = voteEventEvaluationRoundsMapper.selectById(submission.getRoundId());
        Integer number = rounds.getRoundNumber();
        Object type = rounds.getType();
        if (type.equals("evaluation")) {
            type = "评选轮";
        } else {
            type = "评奖论";
        }
        historyScore.setAlternately("第" + number + "轮" + "(" + type + ")");
        LambdaQueryWrapper<VoteEventRegistrationWorkScores> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VoteEventRegistrationWorkScores::getSubmissionId, submission.getId())
                .eq(VoteEventRegistrationWorkScores::getEventId, submission.getEventId())
                .eq(VoteEventRegistrationWorkScores::getRoundId, submission.getRoundId());
        VoteEventRegistrationWorkScores scores = voteEventRegistrationWorkScoresMapper.selectOne(queryWrapper);
        if (scores == null) {
            historyScore.setScore("暂未评分");
        } else {
            historyScore.setScore(scores.getScore().toString());
        }
        historyScore.setSubmissionId(submission.getId());
        if (rounds.getType().equals("evaluation")) {
            historyScore.setResult(PROMOTE);
        } else {
            historyScore.setResult(submission.getAward());
        }
        historyScore.setRoundId(voteSubmissionDto.getRoundId());
        voteHistoryScoreMapper.insert(historyScore);
        return voteSubmissionMapper.updateById(voteSubmission);
    }


    /**
     * 评选结果 （评奖轮） 全部作品类型数量
     */
    @Override
    public VoteSubmissionCountVo getAllAwardCount(VoteSubmissionDto voteSubmissionDto) {
        List<VoteSubmission> voteSubmissions = getVoteSubmissions(voteSubmissionDto);
        VoteSubmissionCountVo allSubmissionCount = getAllSubmissionCount(voteSubmissionDto);
        List<VoteSubmission> alreadyAwarded = voteSubmissions.stream().filter(voteSubmission -> !voteSubmission.getAward().equals(PROMOTE_0)).collect(Collectors.toList());
        allSubmissionCount.setAlreadyAwarded(alreadyAwarded.size());//已评奖
        VoteEventEvaluationRounds voteEventEvaluationRounds = voteEventEvaluationRoundsMapper.selectById(voteSubmissionDto.getRoundId());
        String[] split = voteEventEvaluationRounds.getAwards().split(",");
        List<Map<String, Integer>> list = new ArrayList<>();
        for (String s : split) {
            Map<String, Integer> map = new HashMap<>();
            VoteEventAwards eventAwards = voteEventAwardsMapper.selectById(s);
            map.put(eventAwards.getName(), eventAwards.getQuantity());
            list.add(map);
        }
        allSubmissionCount.setAwardNameList(list);
        return allSubmissionCount;
    }

    /**
     * 评选结果 （评奖轮） 分页查询
     */
    @Override
    public IPage<VoteSubmissionVo> getResultAward(VoteSubmissionDto voteSubmissionDto) {
        return getResult(voteSubmissionDto);
    }

    /**
     * 评选结果 （评奖轮） 奖项回显
     */
    @Override
    public List<String> awardShow(VoteSubmissionDto voteSubmissionDto) {
        VoteEventEvaluationRounds rounds = voteEventEvaluationRoundsMapper.selectById(voteSubmissionDto.getRoundId());
        String[] split = rounds.getAwards().split(",");
        List<String> awardName = new ArrayList<>();
        for (String s : split) {
            VoteEventAwards awards = voteEventAwardsMapper.selectById(s);
            awardName.add(awards.getName());
        }
        return awardName;
    }

    /**
     * 评奖
     */
    @Override
    @Transactional
    public int distribute(VoteSubmissionDto voteSubmissionDto) {
        VoteSubmission voteSubmission = new VoteSubmission();
        voteSubmission.setId(voteSubmissionDto.getId())
                .setAward(voteSubmissionDto.getAward());
        VoteSubmission submission = voteSubmissionMapper.selectById(voteSubmissionDto.getId());
        VoteHistoryScore historyScore = new VoteHistoryScore();
        historyScore.setSubmissionId(voteSubmissionDto.getId())
                .setRoundId(submission.getRoundId())
                .setResult(voteSubmissionDto.getAward());
        LambdaQueryWrapper<VoteHistoryScore> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VoteHistoryScore::getSubmissionId, voteSubmissionDto.getId())
                .eq(VoteHistoryScore::getRoundId, submission.getRoundId());
        voteHistoryScoreMapper.update(historyScore, wrapper);
        return voteSubmissionMapper.updateById(voteSubmission);
    }

    /**
     * 修改评分回显
     */
    @Override
    @Transactional
    public VoteSubmissionItemVo getSubmissionItem(VoteSubmissionDto voteSubmissionDto) {
        VoteSubmissionItemVo itemVo = new VoteSubmissionItemVo();
        LambdaQueryWrapper<VoteSubmissionItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VoteSubmissionItem::getSubmissionId, voteSubmissionDto.getId());
        List<VoteSubmissionItem> submissionItems = voteSubmissionItemMapper.selectList(wrapper);//单项评分
        List<VoteScoringItem> items = new ArrayList<>();
        Integer totalScore = 0;
        for (VoteSubmissionItem submissionItem : submissionItems) {
            VoteScoringItem scoringItem = voteScoringItemMapper.selectById(submissionItem.getItemId());
            scoringItem.setAggregate(submissionItem.getScore());
            scoringItem.setId(submissionItem.getId());
            scoringItem.setItemId(submissionItem.getItemId());
            items.add(scoringItem);
            totalScore += submissionItem.getScore();
        }
        itemVo.setAggregateScore(totalScore);
        itemVo.setItemList(items);
        return itemVo;
    }


    /**
     * 修改
     *
     * @param voteSubmissionDto
     * @return
     */
    @Override
    @Transactional
    public int editItem(VoteSubmissionItemDto voteSubmissionDto) {
        List<VoteScoringItem> itemList = voteSubmissionDto.getItemList();
        for (VoteScoringItem voteScoringItem : itemList) {
            VoteSubmissionItem voteSubmissionItem = new VoteSubmissionItem();
            voteSubmissionItem.setId(voteScoringItem.getId())
                    .setScore(voteScoringItem.getAggregate());
            voteSubmissionItemMapper.updateById(voteSubmissionItem);
        }
        LambdaQueryWrapper<VoteSubmissionItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VoteSubmissionItem::getSubmissionId, voteSubmissionDto.getId());
        List<VoteSubmissionItem> submissionItems = voteSubmissionItemMapper.selectList(wrapper);
        int total = 0;
        for (VoteSubmissionItem submissionItem : submissionItems) {
            total += submissionItem.getScore();
        }
        LambdaQueryWrapper<VoteEventRegistrationWorkScores> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VoteEventRegistrationWorkScores::getSubmissionId, voteSubmissionDto.getId());
        VoteEventRegistrationWorkScores scores = new VoteEventRegistrationWorkScores();
        scores.setScore(BigDecimal.valueOf(total));
        return voteEventRegistrationWorkScoresMapper.update(scores, queryWrapper);

    }

    /**
     * 评奖晋级
     */
    @Override
    public int promoteAward(VoteSubmissionDto voteSubmissionDto) {
        VoteSubmission submission = voteSubmissionMapper.selectById(voteSubmissionDto.getId());
        if (submission.getPromoteStatus().equals(PROMOTE_0)) {
            throw new ServiceException("已晋级的作品不能继续晋级");
        }
        voteSubmissionDto.setAppraiseStatus("2");
        return voteSubmissionMapper.updateById(voteSubmissionDto);
    }


}




