package com.ruoyi.kaohe.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.kaohe.domain.KhVoteRecard;
import com.ruoyi.kaohe.domain.KhVoteRecardParam;
import com.ruoyi.kaohe.mapper.KhVoteRecardMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.kaohe.mapper.KhVoteEmpMapper;
import com.ruoyi.kaohe.domain.KhVoteEmp;
import com.ruoyi.kaohe.service.IKhVoteEmpService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 投票选项Service业务层处理
 * 
 * @author hs
 * @date 2025-06-17
 */
@Service
public class KhVoteEmpServiceImpl implements IKhVoteEmpService 
{
    @Autowired
    private KhVoteEmpMapper khVoteEmpMapper;
    @Autowired
    private KhVoteRecardMapper khVoteRecardMapper;

    /**
     * 查询投票选项
     * 
     * @param id 投票选项主键
     * @return 投票选项
     */
    @Override
    public KhVoteEmp selectKhVoteEmpById(Long id)
    {
        return khVoteEmpMapper.selectKhVoteEmpById(id);
    }

    /**
     * 查询投票选项列表
     * 
     * @param khVoteEmp 投票选项
     * @return 投票选项
     */
    @Override
    public List<KhVoteEmp> selectKhVoteEmpList(KhVoteEmp khVoteEmp)
    {
        return khVoteEmpMapper.selectKhVoteEmpList(khVoteEmp);
    }

    /**
     * 新增投票选项
     * 
     * @param khVoteEmp 投票选项
     * @return 结果
     */
    @Override
    public int insertKhVoteEmp(KhVoteEmp khVoteEmp)
    {
        khVoteEmp.setCreateTime(DateUtils.getNowDate());
        return khVoteEmpMapper.insertKhVoteEmp(khVoteEmp);
    }

    /**
     * 修改投票选项
     * 
     * @param khVoteEmp 投票选项
     * @return 结果
     */
    @Override
    public int updateKhVoteEmp(KhVoteEmp khVoteEmp)
    {
        khVoteEmp.setUpdateTime(DateUtils.getNowDate());
        return khVoteEmpMapper.updateKhVoteEmp(khVoteEmp);
    }

    /**
     * 批量删除投票选项
     * 
     * @param ids 需要删除的投票选项主键
     * @return 结果
     */
    @Override
    public int deleteKhVoteEmpByIds(Long[] ids)
    {
        return khVoteEmpMapper.deleteKhVoteEmpByIds(ids);
    }

    /**
     * 删除投票选项信息
     * 
     * @param id 投票选项主键
     * @return 结果
     */
    @Override
    public int deleteKhVoteEmpById(Long id)
    {
        return khVoteEmpMapper.deleteKhVoteEmpById(id);
    }

    @Override
    @Transactional
    public AjaxResult voteSubmit(KhVoteRecardParam param) {
        Map<String, BigDecimal> xuanxiangDetails = new HashMap<>();
        BigDecimal num = new BigDecimal("1");//投票人数+1
        List<KhVoteEmp> voteEmpList = param.getVoteEmpList();
        Date nowDate = DateUtils.getNowDate();
        Long typeid = param.getKhitemTypeid();
        BigDecimal a = new BigDecimal("100");
        BigDecimal b = new BigDecimal("80");
        BigDecimal c = new BigDecimal("60");
        BigDecimal d = new BigDecimal("40");
        for(KhVoteEmp voteEmp:voteEmpList){
            voteEmp.setUpdateTime(nowDate);
            //根据id查询数据得到之前的值
            KhVoteEmp oldvoteEmp = khVoteEmpMapper.selectKhVoteEmpById(voteEmp.getId());
            if (typeid.equals(1L)) {//选项评分
                //存储投票详情到map
                if(voteEmp.getOptionA().compareTo(new BigDecimal("0"))==1){
                    xuanxiangDetails.put(voteEmp.getBkhdxName(),voteEmp.getOptionA());
                }
                if(voteEmp.getOptionB().compareTo(new BigDecimal("0"))==1){
                    xuanxiangDetails.put(voteEmp.getBkhdxName(),voteEmp.getOptionB());
                }
                if(voteEmp.getOptionC().compareTo(new BigDecimal("0"))==1){
                    xuanxiangDetails.put(voteEmp.getBkhdxName(),voteEmp.getOptionC());
                }
                if(voteEmp.getOptionD().compareTo(new BigDecimal("0"))==1){
                    xuanxiangDetails.put(voteEmp.getBkhdxName(),voteEmp.getOptionD());
                }
                //修改投票情况
                BigDecimal optionA = voteEmp.getOptionA().add(oldvoteEmp.getOptionA());
                BigDecimal optionB = voteEmp.getOptionB().add(oldvoteEmp.getOptionB());
                BigDecimal optionC = voteEmp.getOptionC().add(oldvoteEmp.getOptionC());
                BigDecimal optionD = voteEmp.getOptionD().add(oldvoteEmp.getOptionD());
                voteEmp.setVoteNum(oldvoteEmp.getVoteNum().add(num));
                BigDecimal avgScore = optionA.multiply(a).add(
                        optionB.multiply(b).add(optionC.multiply(c).add(optionD.multiply(d)))
                ).divide(voteEmp.getVoteNum());
                voteEmp.setAvgScore(avgScore);//投票后计算平均分
                if(voteEmp.getPercentage()!=null){  //计算最后得分
                    voteEmp.setEndScore(avgScore.multiply(voteEmp.getPercentage()));
                }
            }else if(typeid.equals(2L)){//占比评分
                BigDecimal sumScore = voteEmp.getScore().add(oldvoteEmp.getVoteScore());
                voteEmp.setVoteScore(sumScore);
                //修改投票人数   平均分*占比=最后得分
                voteEmp.setVoteNum(oldvoteEmp.getVoteNum().add(num));
                voteEmp.setAvgScore(sumScore.divide(voteEmp.getVoteNum()));
                voteEmp.setEndScore(voteEmp.getAvgScore().multiply(oldvoteEmp.getPercentage()));
            }else{//单独评分    不占比   以平均分为最后得分
                BigDecimal sumScore = voteEmp.getScore().add(oldvoteEmp.getVoteScore());
                voteEmp.setVoteNum(oldvoteEmp.getVoteNum()
                        .add(num));
                voteEmp.setVoteScore(sumScore);
                voteEmp.setAvgScore(sumScore.divide(voteEmp.getVoteNum()));
                voteEmp.setEndScore(voteEmp.getAvgScore());
            }
            khVoteEmpMapper.updateKhVoteEmp(voteEmp);
        }
        //存储投票记录
        KhVoteRecard recard = new KhVoteRecard();
        recard.setVoteId(param.getVoteId());
        recard.setUserId(param.getUserId());
        recard.setUserName(param.getUserName());
        recard.setUid(param.getUid());
        recard.setVoteTime(nowDate);
        if (typeid.equals(1L)) {//选项评分
            recard.setVoteDetails(JSON.toJSONString(xuanxiangDetails));
        }else{
            Map<String, BigDecimal> details = voteEmpList.stream().collect(Collectors.toMap(KhVoteEmp::getBkhdxName, KhVoteEmp::getScore));
            recard.setVoteDetails(JSON.toJSONString(details));
        }
        khVoteRecardMapper.insertKhVoteRecard(recard);
        return AjaxResult.success();
    }
}
