package com.jeeplus.pubmodules.luckyDraw.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
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.google.common.collect.Lists;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.pubmodules.luckyDraw.domain.LuckyDrawAwards;
import com.jeeplus.pubmodules.luckyDraw.domain.LuckyDrawEvents;
import com.jeeplus.pubmodules.luckyDraw.mapper.LuckyDrawAwardsMapper;
import com.jeeplus.pubmodules.luckyDraw.mapper.LuckyDrawEventsMapper;
import com.jeeplus.pubmodules.luckyDraw.service.dto.LuckyDrawAwardsDTO;
import com.jeeplus.pubmodules.luckyDraw.service.dto.LuckyDrawEventsDTO;
import com.jeeplus.pubmodules.luckyDraw.service.mapstruct.LuckyDrawAwardsWrapper;
import com.jeeplus.pubmodules.luckyDraw.service.mapstruct.LuckyDrawEventsWrapper;
import com.jeeplus.utils.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

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

@Service
public class LuckyDrawEventsService extends ServiceImpl<LuckyDrawEventsMapper, LuckyDrawEvents> {

    @Resource
    private LuckyDrawEventsMapper luckyDrawEventsMapper;

    @Resource
    private LuckyDrawAwardsMapper luckyDrawAwardsMapper;

    @Resource
    private LuckyDrawAwardsService luckyDrawAwardsService;

    public IPage<LuckyDrawEventsDTO> findList(Page<LuckyDrawEventsDTO> page, LuckyDrawEventsDTO luckyDrawEventsDTO) throws Exception{
        QueryWrapper<LuckyDrawEvents> queryWrapper = QueryWrapperGenerator.buildQueryCondition ( LuckyDrawEventsWrapper.INSTANCE.toEntity(luckyDrawEventsDTO), LuckyDrawEvents.class );
        queryWrapper.eq("lde.del_flag","0");
        queryWrapper.orderByDesc("lde.create_time");
        IPage<LuckyDrawEventsDTO> list = luckyDrawEventsMapper.findList(page, queryWrapper);
        return list;
    }

    public LuckyDrawEventsDTO queryById(String id) {

        LuckyDrawEventsDTO luckyDrawEventsDTO = luckyDrawEventsMapper.queryById(id);

        return luckyDrawEventsDTO;
    }

    public String saveForm(LuckyDrawEventsDTO luckyDrawEventsDTO) throws Exception{
        LuckyDrawEvents luckyDrawEvents = LuckyDrawEventsWrapper.INSTANCE.toEntity(luckyDrawEventsDTO);
        if (ObjectUtil.isNotEmpty(luckyDrawEventsDTO)) {
            if (StringUtils.isBlank(luckyDrawEventsDTO.getId())) {
                luckyDrawEvents.setSwitchFlag("1");
            }
        }

        // 保存或修改活动信息
        this.saveOrUpdate(luckyDrawEvents);

        if (ObjectUtil.isNotEmpty(luckyDrawEventsDTO)) {
            // 保存奖项数据
            if (CollectionUtil.isNotEmpty(luckyDrawEventsDTO.getLuckyDrawAwardsDTOList())) {
                // 获取奖项数据中拥有id的数据id
                List<String> haveIdList = luckyDrawEventsDTO.getLuckyDrawAwardsDTOList().stream().filter(item -> {
                    if (StringUtils.isNotBlank(item.getId())) {
                        return true;
                    }
                    return false;
                }).map(LuckyDrawAwardsDTO::getId).collect(Collectors.toList());
                // 获取到的数据id除外的数据全部删除
                if (CollectionUtil.isNotEmpty(haveIdList)) {
                    luckyDrawAwardsMapper.delete(new QueryWrapper<LuckyDrawAwards>().lambda()
                            .eq(LuckyDrawAwards::getEventsId,luckyDrawEvents.getId())
                            .notIn(LuckyDrawAwards::getId,haveIdList)
                    );
                } else {
                    luckyDrawAwardsMapper.delete(new QueryWrapper<LuckyDrawAwards>().lambda().eq(LuckyDrawAwards::getEventsId,luckyDrawEvents.getId()));
                }
                luckyDrawEventsDTO.getLuckyDrawAwardsDTOList().stream().forEach(dto -> {
                    LuckyDrawAwards luckyDrawAwards = LuckyDrawAwardsWrapper.INSTANCE.toEntity(dto);
                    luckyDrawAwards.setEventsId(luckyDrawEvents.getId());
                    luckyDrawAwardsService.saveOrUpdate(luckyDrawAwards);
                });
            } else {
                luckyDrawAwardsMapper.delete(new QueryWrapper<LuckyDrawAwards>().lambda().eq(LuckyDrawAwards::getEventsId,luckyDrawEvents.getId()));
            }
        }
        return luckyDrawEvents.getId();
    }

    public ResponseEntity deleteByIds(String ids) {
        String idArray[] =ids.split(",");
        ArrayList<String> strings = Lists.newArrayList(idArray);
        if (CollectionUtil.isNotEmpty(strings)) {
            // 删除活动
            this.removeByIds (strings);
            // 根据活动id删除奖项数据
            luckyDrawAwardsMapper.delete(new QueryWrapper<LuckyDrawAwards>().lambda().in(LuckyDrawAwards::getEventsId,strings));
        }

        return ResponseEntity.ok ("删除成功");
    }

    /**
     * 开启活动
     * @param id
     * @return
     */
    public ResponseEntity changeSwitchOpen (String id) {
        luckyDrawEventsMapper.changeSwitchOpen(id);
        return ResponseEntity.ok ("活动开启成功");
    }

    /**
     * 关闭活动
     * @param id
     * @return
     */
    public ResponseEntity changeSwitchClose (String id) {
        luckyDrawEventsMapper.changeSwitchClose(id);
        return ResponseEntity.ok ("活动关闭成功");
    }
}
