package com.xjrsoft.module.activity.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.module.activity.dto.ActivityInfoPageDto;
import com.xjrsoft.module.activity.dto.AddActivityInfoDto;
import com.xjrsoft.module.activity.dto.UpdateActivityInfoDto;
import com.xjrsoft.module.activity.entity.ActivityEnroll;
import com.xjrsoft.module.activity.entity.ActivityEnrollRange;
import com.xjrsoft.module.activity.entity.ActivityInfo;
import com.xjrsoft.module.activity.mapper.ActivityEnrollMapper;
import com.xjrsoft.module.activity.mapper.ActivityEnrollRangeMapper;
import com.xjrsoft.module.activity.mapper.ActivityInfoMapper;
import com.xjrsoft.module.activity.service.IActivityInfoService;
import com.xjrsoft.module.activity.vo.ActivityInfoPageVo;
import com.yomahub.liteflow.util.JsonUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @title: 活动信息
* @Author dzx
* @Date: 2024-07-19
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class ActivityInfoServiceImpl extends MPJBaseServiceImpl<ActivityInfoMapper, ActivityInfo> implements IActivityInfoService {
    private final ActivityInfoMapper activityInfoActivityInfoMapper;

    private final ActivityEnrollRangeMapper activityInfoActivityEnrollRangeMapper;
    private final ActivityEnrollMapper activityInfoActivityEnrollMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(AddActivityInfoDto dto) {
        ActivityInfo activityInfo = BeanUtil.toBean(dto, ActivityInfo.class);
        if(activityInfo.getActivityEnrollRangeList() != null){
            for (ActivityEnrollRange activityEnrollRange : activityInfo.getActivityEnrollRangeList()) {
                activityEnrollRange.setActivityInfoId(activityInfo.getId());
                activityInfoActivityEnrollRangeMapper.insert(activityEnrollRange);
            }
            activityInfo.setEnrollRange(JsonUtil.toJsonString(dto.getActivityEnrollRangeList()));
        }
        activityInfoActivityInfoMapper.insert(activityInfo);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(UpdateActivityInfoDto dto) {
        ActivityInfo activityInfo = BeanUtil.toBean(dto, ActivityInfo.class);
        activityInfo.setEnrollRange(JsonUtil.toJsonString(dto.getActivityEnrollRangeList()));
        activityInfoActivityInfoMapper.updateById(activityInfo);
        //********************************* ActivityEnrollRange  增删改  开始 *******************************************/
        {
            // 查出所有子级的id
            List<ActivityEnrollRange> activityEnrollRangeList = activityInfoActivityEnrollRangeMapper.selectList(Wrappers.lambdaQuery(ActivityEnrollRange.class).eq(ActivityEnrollRange::getActivityInfoId, activityInfo.getId()).select(ActivityEnrollRange::getId));
            List<Long> activityEnrollRangeIds = activityEnrollRangeList.stream().map(ActivityEnrollRange::getId).collect(Collectors.toList());
            //原有子表单 没有被删除的主键
            List<Long> activityEnrollRangeOldIds = activityInfo.getActivityEnrollRangeList().stream().map(ActivityEnrollRange::getId).filter(Objects::nonNull).collect(Collectors.toList());
            //找到需要删除的id
            List<Long> activityEnrollRangeRemoveIds = activityEnrollRangeIds.stream().filter(item -> !activityEnrollRangeOldIds.contains(item)).collect(Collectors.toList());

            for (ActivityEnrollRange activityEnrollRange : activityInfo.getActivityEnrollRangeList()) {
                //如果不等于空则修改
                if (activityEnrollRange.getId() != null) {
                    activityInfoActivityEnrollRangeMapper.updateById(activityEnrollRange);
                }
                //如果等于空 则新增
                else {
                    //已经不存在的id 删除
                    activityEnrollRange.setActivityInfoId(activityInfo.getId());
                    activityInfoActivityEnrollRangeMapper.insert(activityEnrollRange);
                }
            }
            //已经不存在的id 删除
            if(activityEnrollRangeRemoveIds.size() > 0){
                activityInfoActivityEnrollRangeMapper.deleteBatchIds(activityEnrollRangeRemoveIds);
            }
        }
        //********************************* ActivityEnrollRange  增删改  结束 *******************************************/
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> ids) {
        activityInfoActivityInfoMapper.deleteBatchIds(ids);
        activityInfoActivityEnrollRangeMapper.delete(Wrappers.lambdaQuery(ActivityEnrollRange.class).in(ActivityEnrollRange::getActivityInfoId, ids));
        activityInfoActivityEnrollMapper.delete(Wrappers.lambdaQuery(ActivityEnroll.class).in(ActivityEnroll::getActivityInfoId, ids));

        return true;
    }

    @Override
    public Page<ActivityInfoPageVo> getPage(Page<ActivityInfoPageVo> page, ActivityInfoPageDto dto) {
        return this.baseMapper.getPage(page, dto);
    }

    @Override
    public Page<ActivityInfoPageVo> getMyPage(Page<ActivityInfoPageVo> page, ActivityInfoPageDto dto) {
        return this.baseMapper.getMyPage(page, dto);
    }
}
