package com.vote.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.vctgo.common.core.exception.ServiceException;
import com.vctgo.common.core.text.Convert;
import com.vctgo.common.core.utils.ServletUtils;
import com.vctgo.common.mybatisplus.constant.MybatisPageConstants;
import com.vote.system.domain.*;
import com.vote.system.domain.vo.VoteEventsVo;
import com.vote.system.mapper.*;
import com.vote.system.service.VoteEventsService;
import com.vote.system.service.VoteWorkTypesService;
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.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.vote.system.util.ConstantUtils.IS_DELETED_0;
import static com.vote.system.util.ConstantUtils.commit_shelves;

/**
 * @author MacBook
 * @description 针对表【vote_events(活动表)】的数据库操作Service实现
 * @createDate 2024-08-11 17:38:19
 */
@Service
public class VoteEventsServiceImpl extends ServiceImpl<VoteEventsMapper, VoteEvents>
        implements VoteEventsService {

    @Autowired
    private VoteEventsMapper voteEventsMapper;

    @Autowired
    private VoteRegistrationFormsMapper voteRegistrationFormsMapper;//报名表

    @Autowired
    private VoteOrganizersMapper voteOrganizersMapper;//主办方


    @Override
    public Long addEvents(VoteEvents voteEvents) {
        voteEventsMapper.insert(voteEvents);
        return voteEvents.getId();
    }


    /**
     * 分页查询
     *
     * @param voteEvents 查询条件对象
     * @return 分页结果
     */
    @Override
    public Page<VoteEventsVo> getEventsList(VoteEvents voteEvents) {
        long pageNum = Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_NUM), 1L);
        long pageSize = Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_SIZE), 10L);
        Page<VoteEvents> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<VoteEvents> wrapper = new LambdaQueryWrapper<>();
        if (voteEvents != null) {
            if (voteEvents.getKeyword() != null && !voteEvents.getKeyword().isEmpty()) {
                wrapper.like(VoteEvents::getName, voteEvents.getKeyword());
            }
            if (voteEvents.getStatus() != null) {
                wrapper.eq(VoteEvents::getStatus, voteEvents.getStatus());
            }
        }
        wrapper.eq(VoteEvents::getIsDeleted, IS_DELETED_0)
                .orderByDesc(VoteEvents::getUpdatedAt);
        IPage<VoteEvents> resultPage = voteEventsMapper.selectPage(page, wrapper);
        List<VoteEvents> voteEventsList = resultPage.getRecords();
        List<VoteEventsVo> eventsVos = voteEventsList.stream().map(item -> {
            VoteEventsVo eventsVo = new VoteEventsVo();
            eventsVo.setQuantity(voteEventsMapper.queryById(item.getId()));
            eventsVo.setNumber(voteEventsMapper.queryNumberById(item.getId()));
            BeanUtils.copyProperties(item, eventsVo);
            return eventsVo;
        }).collect(Collectors.toList());
        Page<VoteEventsVo> voPage = new Page<>(pageNum, pageSize);
        voPage.setTotal(resultPage.getTotal());
        voPage.setRecords(eventsVos);
        return voPage;
    }

    /**
     * 查询报名内容详情
     *
     * @param id
     * @return
     */
    @Override
    public VoteEventsVo getEventsById(Long id) {

        VoteEventsVo eventsVo = new VoteEventsVo();

        // 获取活动信息
        VoteEvents voteEvents = voteEventsMapper.selectById(id);
        // 获取主办方信息
        VoteOrganizers voteOrganizers = getVoteOrganizers(voteEvents.getOrganizerId());
        //获取报名表信息
        VoteRegistrationForms registrationForms = getVoteRegistrationForms(voteEvents.getRegistrationFormId());


        if (registrationForms != null) {
            eventsVo.setFormName(registrationForms.getFormName());
        } else {
            eventsVo.setFormName(null);
        }

        if (voteOrganizers != null) {
            eventsVo.setOrganizers(voteOrganizers.getName());
        } else {
            eventsVo.setOrganizers(null);
        }
        // 设置返回值
        BeanUtils.copyProperties(voteEvents, eventsVo);

        return eventsVo;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int deleteEvents(Long id) {
        VoteEvents voteEvents = voteEventsMapper.selectById(id);
        if (commit_shelves.equals(voteEvents.getStatus())) {
            throw new ServiceException("已上架活动不能删除，请先下架该活动！");
        }
        return voteEventsMapper.deleteEvents(id);
    }

    @Override
    public boolean changeStatus(Long id, String status) {
        VoteEvents voteEvents = new VoteEvents();
        voteEvents.setStatus(status);
        voteEvents.setId(id);
        voteEvents.setUpdatedAt(new Date());
        return this.updateById(voteEvents);
    }

    /**
     * 编辑
     *
     * @param voteEvents
     * @return
     */
    @Override
    @Transactional
    public int edit(VoteEvents voteEvents) {
        return voteEventsMapper.updateById(voteEvents);
    }


    //主办方信息
    private VoteOrganizers getVoteOrganizers(Long organizerId) {
        return voteOrganizersMapper.selectById(organizerId);
    }

    //报名表信息
    private VoteRegistrationForms getVoteRegistrationForms(Long registrationFormId) {
        return voteRegistrationFormsMapper.selectById(registrationFormId);
    }


}




