package com.ruoyi.frequency.voterecord.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.frequency.customer.service.UserService;
import com.ruoyi.frequency.voterecord.entity.VoteRecord;
import com.ruoyi.frequency.voterecord.mapper.VoteRecordMapper;
import com.ruoyi.frequency.voterecord.service.VoteRecordService;
import com.ruoyi.utils.SnowIdUtils;
import com.ruoyi.vo.UserInfo;
import com.ruoyi.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 投票记录Service业务层处理
 *
 * @author liwenlong
 * @date 2023-10-12
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class VoteRecordServiceImpl extends ServiceImpl<VoteRecordMapper, VoteRecord>  implements VoteRecordService
{
    @Autowired
    private VoteRecordMapper voteRecordMapper;

    /**
     * 查询投票记录
     *
     * @param id 投票记录主键
     * @return 投票记录
     */
    @Override
    public VoteRecord selectVoteRecordById(Long id)
    {
        return voteRecordMapper.selectVoteRecordById(id);
    }


    @Autowired
    private UserService userService;

    /**
     * 查询投票记录列表
     *
     * @param voteRecord 投票记录
     * @return 投票记录
     */
    @Override
    public List<VoteRecord> selectVoteRecordList(VoteRecord voteRecord)
    {
        List<VoteRecord> voteRecords = voteRecordMapper.selectVoteRecordList(voteRecord);

        if (voteRecords.size() > 0){
            Set<UserVo> userVoSet = voteRecords.stream().map(s -> new UserVo(s.getUserType(), s.getUserId())).collect(Collectors.toSet());

            //查询用户信息
            Map<UserVo, UserInfo> userMap = userService.selectUserInfoMap(userVoSet,null);

            voteRecords.stream().forEach(s ->{
                s.setUserInfo(userMap.get(new UserVo(s.getUserType(), s.getUserId())));
            });
        }

        return voteRecords;
    }

    /**
     * 新增投票记录
     *
     * @param voteRecord 投票记录
     * @return 结果
     */
    @Override
    public int insertVoteRecord(VoteRecord voteRecord)
    {
        return voteRecordMapper.insertVoteRecord(voteRecord);
    }

    /**
     * 修改投票记录
     *
     * @param voteRecord 投票记录
     * @return 结果
     */
    @Override
    public int updateVoteRecord(VoteRecord voteRecord)
    {
        voteRecord.setUpdateTime(DateUtils.getNowDate());
        return voteRecordMapper.updateVoteRecord(voteRecord);
    }

    /**
     * 批量删除投票记录
     *
     * @param ids 需要删除的投票记录主键
     * @return 结果
     */
    @Override
    public int deleteVoteRecordByIds(Long[] ids)
    {
        this.lambdaUpdate().set(VoteRecord::getDelFlag,PublicCommon.删除).in(VoteRecord::getId,ids).update();
        return ids.length;
    }

    /**
     * 删除投票记录信息
     *
     * @param id 投票记录主键
     * @return 结果
     */
    @Override
    public int deleteVoteRecordById(Long id)
    {
        this.lambdaUpdate().set(VoteRecord::getDelFlag,PublicCommon.删除).eq(VoteRecord::getId,id).update();
        return 1;
    }

    @Override
    public void addVoteRecord(Long voteId, List<Long> voteOptionIds, UserVo userVo) {

        if (voteOptionIds.isEmpty()){
            return;
        }

        List<VoteRecord> voteRecords = new ArrayList<>();

        voteOptionIds.stream().forEach(voteOptionId->{
            VoteRecord voteRecord = new VoteRecord();

            voteRecord.setOptionId(voteOptionId)
                    .setUserId(userVo.getUserId())
                    .setUserType(userVo.getUserType())
                    .setVoteId(voteId);
            voteRecords.add(voteRecord);
        });

        this.saveBatch(voteRecords);
    }
}
