package com.jeeplus.pubmodules.luckyDraw.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.pubmodules.luckyDraw.domain.LuckyDrawInfo;
import com.jeeplus.pubmodules.luckyDraw.mapper.LuckyDrawInfoMapper;
import com.jeeplus.pubmodules.luckyDraw.mapper.LuckyDrawMembersMapper;
import com.jeeplus.pubmodules.luckyDraw.service.dto.LuckyDrawInfoDto;
import com.jeeplus.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author: 王强
 * @create: 2023-02-28 11:02
 **/
@Service
@Transactional
public class LuckyDrawInfoService extends ServiceImpl<LuckyDrawInfoMapper, LuckyDrawInfo> {

    @Resource
    private LuckyDrawInfoMapper drawInfoMapper;

    @Resource
    private LuckyDrawMembersMapper membersMapper;


    /**
     * 列表查询
     */
    public IPage<LuckyDrawInfoDto> findList(Page<LuckyDrawInfoDto> page , LuckyDrawInfoDto dto) throws Exception{
        QueryWrapper<LuckyDrawInfoDto> queryWrapper = QueryWrapperGenerator.buildQueryCondition(dto, LuckyDrawInfoDto.class);

        queryWrapper.eq("lda.del_flag", "0");
        queryWrapper.eq("lde.switch_flag", "1");
        if (StringUtils.isNotEmpty(dto.getId())) {
            queryWrapper.eq("lda.events_id",dto.getId());
        }

        return drawInfoMapper.findList(page, queryWrapper);
    }

    /**
     * 获取已抽奖人数和本活动剩余未抽奖人数
     * @param dto
     * @return
     */
    public Map<String,Integer> getLuckyDrawCount (LuckyDrawInfoDto dto){
        Map<String,Integer> map =new HashMap<>();
        //查询活动中奖人数
        Integer luckyDrawUserCount = drawInfoMapper.getLuckyDrawUserCount(dto.getId());
        map.put("luckyDrawUserCount",luckyDrawUserCount);
        //查询活动参与人数
        Integer luckyDrawMemberCount = drawInfoMapper.getLuckyDrawMemberCount(dto.getId());
        map.put("luckyDrawMemberCount",luckyDrawMemberCount);
        return map;
    }

    public LuckyDrawInfoDto findByEventId(String id) {
        LuckyDrawInfoDto dto = drawInfoMapper.findByEventId(id);
        return dto;
    }

    public String luckyDraw(LuckyDrawInfoDto dto) throws Exception{
        return add(dto);
    }

    public String luckyDrawAgain(LuckyDrawInfoDto dto) throws Exception{
        //根据活动id和奖项id对该奖项数据信息进行删除
        drawInfoMapper.deleteByEventIdAndAwardId(dto);
        return add(dto);
    }

    public String luckyDrawClear(LuckyDrawInfoDto dto){
        //根据活动id和奖项id对该奖项数据信息进行删除
        drawInfoMapper.deleteByEventIdAndAwardId(dto);
        return "清除成功";
    }

    /**
     *根据奖品数量随机抽取用户进行抽奖
     * @param dto
     * @return
     * @throws Exception
     */
    public String add(LuckyDrawInfoDto dto) throws Exception{
        LuckyDrawInfo members = new LuckyDrawInfo();
        BeanUtils.copyProperties(dto, members);

        members.setCreateById("1");
        members.setCreateTime(new Date());
        members.setUpdateById("1");
        members.setUpdateTime(new Date());
        members.setDelFlag(0);
        members.setEventId(dto.getId());
        members.setAwardId(dto.getAwardId());
        members.setLuckyDrawFlag("1");
        //当抽奖前
        int num = Integer.parseInt(dto.getNumber());
        //根据奖品数量和活动id随机抽取用户进行抽奖
        List<String> list = drawInfoMapper.selectUserId(num,dto.getId());
        list.forEach(userId->{
            // 生成id
            String id = UUID.randomUUID().toString().replace("-", "");
            members.setId(id);
            members.setMemberId(userId);
            drawInfoMapper.insert(members);
        });
        if(list.size() < num){
            return "活动剩余参与人数小于奖项人数，按剩余人数进行抽奖";
        }
        return "抽奖成功";
    }
}
