package com.vote.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vctgo.common.core.exception.ServiceException;
import com.vote.system.domain.VoteRule;
import com.vote.system.domain.VoteScoringItem;
import com.vote.system.domain.dto.VoteScoringItemDto;
import com.vote.system.mapper.VoteRuleMapper;
import com.vote.system.mapper.VoteScoringItemMapper;
import com.vote.system.service.VoteScoringItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author MacBook
 * @description 针对表【vote_scoring_item】的数据库操作Service实现
 * @createDate 2024-08-06 15:39:41
 */
@Service
public class VoteScoringItemServiceImpl extends ServiceImpl<VoteScoringItemMapper, VoteScoringItem>
        implements VoteScoringItemService {

    @Autowired
    private VoteScoringItemMapper voteScoringItemMapper;

    @Autowired
    private VoteRuleMapper voteRuleMapper;

    @Override
    public List<VoteScoringItem> queryByRuleId(Long ruleId) {
        LambdaQueryWrapper<VoteScoringItem> wrapper = new LambdaQueryWrapper<>();
        //构建查询条件
        if (ruleId != null) {
            wrapper.eq(VoteScoringItem::getRuleId, ruleId);
        }
        return voteScoringItemMapper.selectList(wrapper);
    }

    /**
     * 添加
     *
     * @param voteScoringItem
     * @return
     */
    @Override
    @Transactional
    public int insertVoteScoringItem(VoteScoringItem voteScoringItem) {
        //添加评分项时 要计算分值和总数 插入rule表
        LambdaQueryWrapper<VoteScoringItem> wrapper = new LambdaQueryWrapper<>();
        int i = voteScoringItemMapper.insert(voteScoringItem);
        if (i > 0) {
            if (voteScoringItem.getRuleId() != null) {
                // 获取该规则下的所有评分项 构建查询条件
                wrapper.eq(VoteScoringItem::getRuleId, voteScoringItem.getRuleId());
            }
            List<VoteScoringItem> scoringItems = voteScoringItemMapper.selectList(wrapper);
            int size = scoringItems.size();//总项
            double sum = scoringItems.stream().mapToDouble(VoteScoringItem::getScore).sum();//总分
            // 更新规则表中的评分项总数和总分值
            VoteRule rule = voteRuleMapper.selectById(voteScoringItem.getRuleId());
            if (rule == null) {
                throw new IllegalArgumentException("指定ID的投票规则不存在");
            }
            rule.setTotal(size);
            rule.setAggregate(sum);
            return voteRuleMapper.updateById(rule);
        } else {
            return 0;
        }
    }


    /**
     * 保存
     *
     * @param voteScoringItemDto
     * @return
     */
    @Override
    @Transactional
    public Integer saveVoteScoringItem(VoteScoringItemDto voteScoringItemDto) {
        //先删除 在更新
        LambdaQueryWrapper<VoteScoringItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VoteScoringItem::getRuleId, voteScoringItemDto.getRuleId());
        voteScoringItemMapper.delete(wrapper);
        insertVoteScoringItems(voteScoringItemDto.getItems(), voteScoringItemDto.getRuleId());
        //改名字
        LambdaQueryWrapper<VoteRule> queryWrapper = new LambdaQueryWrapper<>();
        if (voteScoringItemDto.getName() != null) {
            queryWrapper.eq(VoteRule::getRuleId, voteScoringItemDto.getRuleId());
        }
        VoteRule voteRule = voteRuleMapper.selectOne(queryWrapper);
        if (voteRule.getName().equals(voteScoringItemDto.getName())) {
            throw new ServiceException("修改失败存在重复表单名" + voteScoringItemDto.getName());
        }
        voteRule.setName(voteScoringItemDto.getName());
        return voteRuleMapper.updateById(voteRule);
    }


    //新增评分项方法
    private void insertVoteScoringItems(List<VoteScoringItem> items, Long ruleId) {
        if (items != null && !items.isEmpty()) {
            for (VoteScoringItem item : items) {
                item.setRuleId(ruleId);
                insertVoteScoringItem(item);
            }
        }
    }

}




