package com.vote.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.domain.dto.SysBranchKindergartenDto;
import com.vote.system.domain.VoteEventWorkSettings;
import com.vote.system.domain.VoteRegistrations;
import com.vote.system.domain.VoteSubmission;
import com.vote.system.domain.dto.VoteRegistrationsDto;
import com.vote.system.domain.vo.VoteRegistrationsVo;
import com.vote.system.mapper.VoteEventWorkSettingsMapper;
import com.vote.system.service.VoteRegistrationsService;
import com.vote.system.mapper.VoteRegistrationsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

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

/**
 * @author MacBook
 * @description 针对表【vote_registrations(报名表)】的数据库操作Service实现
 * @createDate 2024-08-19 10:19:48
 */
@Service
public class VoteRegistrationsServiceImpl extends ServiceImpl<VoteRegistrationsMapper, VoteRegistrations>
        implements VoteRegistrationsService {

    @Autowired
    private VoteRegistrationsMapper voteRegistrationsMapper;

    @Autowired
    private VoteEventWorkSettingsMapper voteEventWorkSettingsMapper;

    @Autowired
    private RemoteBranchKindergartenService remoteBranchKindergartenService;


    /**
     * 分页查询 报名用户
     *
     * @param voteRegistrationsDto
     * @return
     */
    @Override
    public Page<VoteRegistrationsVo> selectPage(VoteRegistrationsDto voteRegistrationsDto) {
        long pageNum = Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_NUM), 1L);
        long pageSize = Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_SIZE), 10L);
        Page<VoteRegistrationsVo> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<VoteRegistrations> queryWrapper = new LambdaQueryWrapper<>();
        if (voteRegistrationsDto.getEventId() == null) {
            throw new RuntimeException("活动id为空！暂时无法查询");
        }
        // 构建查询条件
        queryWrapper.eq(VoteRegistrations::getEventId, voteRegistrationsDto.getEventId());
        if (voteRegistrationsDto.getKeyword() != null) {
            queryWrapper.like(VoteRegistrations::getName, voteRegistrationsDto.getKeyword());
        }
        if (voteRegistrationsDto.getProvinceId() != null) {
            queryWrapper.eq(VoteRegistrations::getProvinceId, voteRegistrationsDto.getProvinceId());
        }
        if (voteRegistrationsDto.getCityId() != null) {
            queryWrapper.eq(VoteRegistrations::getCityId, voteRegistrationsDto.getCityId());
        }
        if (voteRegistrationsDto.getDistrictId() != null) {
            queryWrapper.eq(VoteRegistrations::getDistrictId, voteRegistrationsDto.getDistrictId());
        }

        if (voteRegistrationsDto.getLocationId() != null) {
            queryWrapper.eq(VoteRegistrations::getLocationId, voteRegistrationsDto.getLocationId());
        }
        if (voteRegistrationsDto.getStatus() != null) {
            queryWrapper.eq(VoteRegistrations::getStatus, voteRegistrationsDto.getStatus());
        }
        queryWrapper.orderByDesc(VoteRegistrations::getCreatedAt);

        // 执行查询并分页
        List<VoteRegistrations> voteRegistrations = voteRegistrationsMapper.selectList(queryWrapper);
        if (voteRegistrations.isEmpty()) {
            page.setRecords(Collections.emptyList());
            return page;
        }

        // 获取工作类型数据
        Map<Long, VoteEventWorkSettings> eventWorkSettingsMap = voteEventWorkSettingsMapper.selectList(new LambdaQueryWrapper<>()).stream()
                .collect(Collectors.toMap(VoteEventWorkSettings::getId, Function.identity()));
        List<VoteRegistrationsVo> voList = voteRegistrations.stream().map(item -> {
            VoteRegistrationsVo vo = new VoteRegistrationsVo();
            vo.setId(item.getId());
            vo.setCreatedAt(item.getCreatedAt());
            vo.setPhone(item.getPhone());
            vo.setUserInfo(getUserInfoDescription(item.getUserInfo()));
            vo.setStatus(getStatusDescription(item.getStatus()));
            vo.setName(item.getName());
            VoteEventWorkSettings eventWorkSettings = eventWorkSettingsMap.get(item.getWorkSettingsId());
            vo.setTypeName(eventWorkSettings != null ? eventWorkSettings.getAlias() : "暂未设置类型别称");
            vo.setLocationName(getSysBranchKindergartenDto(item).getKindergartenName());
            return vo;
        }).collect(Collectors.toList());
        page.setRecords(voList);
        page.setTotal(voList.size());
        return page;
    }

    @Override
    @Transactional
    public int editStatus(VoteRegistrations voteRegistrations) {
        if (voteRegistrations != null && voteRegistrations.getId() != null) {
            return voteRegistrationsMapper.updateById(voteRegistrations);
        }
        throw new RuntimeException("操作失败");
    }


    // 查询单个用户信息
    @Override
    public VoteRegistrationsVo getUserInfo(Long id) {
        if (id != null) {
            VoteRegistrations voteRegistrations = voteRegistrationsMapper.selectById(id);
            if (voteRegistrations != null) {
                VoteRegistrationsVo vo = new VoteRegistrationsVo();
                SysBranchKindergartenDto data = remoteBranchKindergartenService.getById(voteRegistrations.getLocationId()).getData();
                vo.setName(voteRegistrations.getName())
                        .setLocationName(data.getKindergartenName())
                        .setAddress(data.getBelongRegion())
                        .setCreatedAt(voteRegistrations.getCreatedAt())
                        .setPhone(voteRegistrations.getPhone())
                        .setStatus(voteRegistrations.getStatus())
                        .setSex(voteRegistrations.getSex())
                        .setPost(voteRegistrations.getPost())
                        .setProfessional(voteRegistrations.getProfessional())
                        .setCertificateNumber(voteRegistrations.getCertificateNumber());
                String ifBackbone = BACKBONE_FLAG.equals(voteRegistrations.getIfBackbone()) ? BACKBONE_YES : BACKBONE_NO;
                vo.setIfBackbone(ifBackbone);
                return vo;
            }
        }
        throw new RuntimeException("获取用户信息失败");
    }


    // 获取用户信息描述
    private String getUserInfoDescription(String userInfo) {
        switch (userInfo) {
            case USER_INFO_0:
                return USER_INFO_0;
            case USER_INFO_1:
                return USER_INFO_1;
            case USER_INFO_2:
                return USER_INFO_2;
            default:
                return "未知状态";
        }
    }

    // 获取状态描述
    private String getStatusDescription(String status) {
        switch (status) {
            case ACCOUNT_STATUS_0:
                return ACCOUNT_STATUS_0;
            case ACCOUNT_STATUS_1:
                return ACCOUNT_STATUS_1;
            default:
                return "未知状态";
        }
    }

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

}





