package com.willalp.event.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.common.utils.StringUtils;
import com.willalp.event.domain.HsEvent;
import com.willalp.event.domain.HsEventBusinessRelation;
import com.willalp.event.domain.vo.ClubActivityMoreVo;
import com.willalp.event.mapper.HsEventMapper;
import com.willalp.event.service.IHsAssociationsInfoService;
import com.willalp.event.service.IHsEventBusinessRelationService;
import com.willalp.event.service.IHsEventService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 活动配置Service业务层处理
 *
 * @author willalp
 * @date 2023-01-29
 */
@Service
public class HsEventServiceImpl extends ServiceImpl<HsEventMapper, HsEvent> implements IHsEventService {

    protected final static Logger logger = LoggerFactory.getLogger(HsEventServiceImpl.class);

    @Resource
    private HsEventMapper hsEventMapper;

    /**
     * 查询活动配置列表
     *
     * @param hsEvent 活动配置
     * @return 活动配置
     */
    @Override
    public List<HsEvent> selectHsEventList(HsEvent hsEvent) {
        return hsEventMapper.selectHsEventList(hsEvent);
    }

    @Override
    public List<HsEvent> getNowEventList(HsEvent hsEvent) {
        return hsEventMapper.getNowEventList(hsEvent);
    }

    @Resource
    private IHsEventBusinessRelationService relationService;

    @Override
    public void timingUpdateEventStatus() {
        logger.info("=============开始扫描事件状态=============");
        //过期或结束
        List<HsEvent> formerlyEventList = baseMapper.getFormerlyEventList(null);
        //进行中
        List<HsEvent> nowEventList = baseMapper.getNowEventList(null);
        List<HsEvent> formerlyEvents = formerlyEventList.stream().filter(item -> StringUtils.isNotBlank(item.getEventStatus()) && !("4".equals(item.getEventStatus()))).collect(Collectors.toList());
        List<HsEvent> nowEvents = nowEventList.stream().filter(item -> StringUtils.isNotBlank(item.getEventStatus()) && !("3".equals(item.getEventStatus()))).collect(Collectors.toList());
        if (formerlyEvents.size() > 0) {
            formerlyEvents.forEach(item -> item.setEventStatus("4"));
        }
        if (nowEvents.size() > 0) {
            nowEvents.forEach(item -> item.setEventStatus("3"));
        }
        List<HsEvent> sumList = new ArrayList<>();
        sumList.addAll(formerlyEvents);
        sumList.addAll(nowEvents);
        if (sumList.size() > 0) {
            updateBatchById(sumList);
            List<String> collect = sumList.stream().map(HsEvent::getId).collect(Collectors.toList());
            List<HsEventBusinessRelation> relations = relationService.getBaseMapper().selectList(new QueryWrapper<HsEventBusinessRelation>()
                    .in("event_id", collect));
            relations.forEach(item -> {
                relationService.agencyDeleteShopCar(item.getBusinessId());
            });
            //删除关系表数据
            relationService.getBaseMapper().delete(new QueryWrapper<HsEventBusinessRelation>()
                    .in("event_id", collect));
            logger.info("事件状态更新成功 - 共【" + sumList.size() + "】条，开始进行【" + nowEvents.size() + "】条，已结束【" + formerlyEvents.size() + "】条 ==> 已删除相关数据");
        }
    }

    @Override
    public List<HsEvent> appGetEvents(String associationsInfoId) {
        return baseMapper.appGetEvents(associationsInfoId);
    }

    @Resource
    private IHsAssociationsInfoService associationsInfoService;

    @Override
    public ClubActivityMoreVo appGetMoreById(String eventId) {
        if (StringUtils.isBlank(eventId)) {
            return null;
        }
        ClubActivityMoreVo clubActivityMoreVo = baseMapper.appGetMoreById(eventId);
        if (clubActivityMoreVo == null) {
            return null;
        }
        return clubActivityMoreVo;
    }
}
