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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.enums.DeleteMark;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.activity.dto.ActivityEnrollExportDto;
import com.xjrsoft.module.activity.dto.ActivityEnrollPageDto;
import com.xjrsoft.module.activity.dto.AddActivityEnrollDto;
import com.xjrsoft.module.activity.dto.AddActivityEnrollRangeDto;
import com.xjrsoft.module.activity.entity.ActivityEnroll;
import com.xjrsoft.module.activity.mapper.ActivityEnrollMapper;
import com.xjrsoft.module.activity.service.IActivityEnrollService;
import com.xjrsoft.module.activity.vo.ActivityEnrollPageVo;
import com.xjrsoft.module.organization.entity.User;
import com.xjrsoft.module.organization.entity.UserDeptRelation;
import com.xjrsoft.module.organization.service.IUserService;
import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
import com.xjrsoft.module.student.service.IBaseStudentSchoolRollService;
import com.xjrsoft.module.teacher.entity.XjrUser;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @title: 活动信息
* @Author dzx
* @Date: 2024-07-19
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class ActivityEnrollServiceImpl extends MPJBaseServiceImpl<ActivityEnrollMapper, ActivityEnroll> implements IActivityEnrollService {
    private final IUserService userService;
    private final IBaseStudentSchoolRollService rollService;
    @Override
    public Boolean save(AddActivityEnrollDto dto) {
        List<ActivityEnroll> insertList = new ArrayList<>();
        if(dto.getEnrollRangeList() != null){
            List<ActivityEnroll> existList = this.list(
                    new QueryWrapper<ActivityEnroll>().lambda()
                            .eq(ActivityEnroll::getActivityInfoId, dto.getActivityInfoId())
                            .eq(ActivityEnroll::getDeleteMark, DeleteMark.NODELETE.getCode())
            );
            Date createDate = new Date();
            Set<Long> existUserId = existList.stream().map(ActivityEnroll::getUserId).collect(Collectors.toSet());
            for (AddActivityEnrollRangeDto enrollRange : dto.getEnrollRangeList()) {
                if(enrollRange.getUserId() != null){
                    if(!existUserId.contains(enrollRange.getUserId() )){
                        insertList.add(
                                new ActivityEnroll(){{
                                    setUserId(enrollRange.getUserId());
                                    setCreateDate(createDate);
                                    setActivityInfoId(dto.getActivityInfoId());
                                    setStatus(2);
                                }}
                        );
                    }
                }else if(enrollRange.getDeptId() != null){
                    List<User> userList = userService.list(
                            new MPJLambdaWrapper<User>()
                                    .select(XjrUser::getId)
                                    .select(XjrUser.class, x -> VoToColumnUtil.fieldsToColumns(XjrUser.class).contains(x.getProperty()))
                                    .innerJoin(UserDeptRelation.class, UserDeptRelation::getUserId, User::getId)
                                    .eq(UserDeptRelation::getDeptId, enrollRange.getDeptId())
                    );
                    for (User user : userList) {
                        if(!existUserId.contains(user.getId() )) {
                            insertList.add(
                                    new ActivityEnroll() {{
                                        setUserId(user.getId());
                                        setCreateDate(createDate);
                                        setActivityInfoId(dto.getActivityInfoId());
                                        setStatus(2);
                                    }}
                            );
                        }
                    }
                }else if(enrollRange.getClassId() != null){
                    List<BaseStudentSchoolRoll> userList = rollService.list(
                            new MPJLambdaWrapper<BaseStudentSchoolRoll>()
                                    .select(BaseStudentSchoolRoll::getId)
                                    .select(BaseStudentSchoolRoll.class, x -> VoToColumnUtil.fieldsToColumns(BaseStudentSchoolRoll.class).contains(x.getProperty()))
                                    .innerJoin(XjrUser.class, XjrUser::getId, BaseStudentSchoolRoll::getUserId)
                                    .eq(BaseStudentSchoolRoll::getClassId, enrollRange.getClassId())
                    );
                    for (BaseStudentSchoolRoll user : userList) {
                        if(!existUserId.contains(user.getId() )) {
                            insertList.add(
                                    new ActivityEnroll() {{
                                        setUserId(user.getUserId());
                                        setCreateDate(createDate);
                                        setActivityInfoId(dto.getActivityInfoId());
                                        setStatus(2);
                                    }}
                            );
                        }
                    }
                }
            }
        }
        if(!insertList.isEmpty()){
            this.saveBatch(insertList);
        }
        return true;
    }

    @Override
    public Boolean update(ActivityEnroll activityInfo) {
        return null;
    }

    @Override
    public Boolean delete(List<Long> ids) {
        this.baseMapper.deleteBatchIds(ids);
        return true;
    }

    public Page<ActivityEnrollPageVo> getPage(Page<ActivityEnrollPageVo> page, ActivityEnrollPageDto dto) {
        return this.baseMapper.getPage(page, dto);
    }

    @Override
    public List<ActivityEnrollPageVo> getList(ActivityEnrollExportDto dto) {
        return this.baseMapper.getList(dto);
    }
}
