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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.teacher.entity.TeacherAward;
import com.xjrsoft.module.teacher.entity.TeacherAwardItem;
import com.xjrsoft.module.teacher.mapper.TeacherAwardItemMapper;
import com.xjrsoft.module.teacher.mapper.TeacherAwardMapper;
import com.xjrsoft.module.teacher.service.ITeacherAwardService;
import com.xjrsoft.module.teacher.vo.TeacherAwardItemPageVo;
import com.xjrsoft.module.teacher.vo.TeacherAwardItemVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @title: 教师获奖登记
* @Author dzx
* @Date: 2024-06-06
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class TeacherAwardServiceImpl extends MPJBaseServiceImpl<TeacherAwardMapper, TeacherAward> implements ITeacherAwardService {

    private final TeacherAwardMapper teacherAwardMapper;

    private final TeacherAwardItemMapper teacherAwardItemMapper;
    @Override
    @Transactional
    public Boolean dataHandleAddTeacherAwardNode(Long dataId) {
        TeacherAward teacherAward = teacherAwardMapper.selectById(dataId);
        if(teacherAward.getTeacherAwardItemId() == null){
            return false;
        }

        //处理父级
        LambdaQueryWrapper<TeacherAwardItem> teacherAwardItemLambdaQueryWrapper = new LambdaQueryWrapper<>();
        teacherAwardItemLambdaQueryWrapper
                .orderByDesc(TeacherAwardItem::getId)
                .select(TeacherAwardItem.class,x -> VoToColumnUtil.fieldsToColumns(TeacherAwardItemPageVo.class).contains(x.getProperty()));
        List<TeacherAwardItem> list = teacherAwardItemMapper.selectList(teacherAwardItemLambdaQueryWrapper);
        List<TeacherAwardItemVo> itemVos = BeanUtil.copyToList(list, TeacherAwardItemVo.class);

        List<TeacherAwardItemVo> ancestors = findAncestors(itemVos, teacherAward.getTeacherAwardItemId());
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ancestors.size(); i++) {
            TeacherAwardItemVo teacherAwardItemVo = ancestors.get(i);
            if(teacherAwardItemVo != null && teacherAwardItemVo.getName() != null && !teacherAwardItemVo.getName().equals("")){
                sb.insert(0, ">");
                sb.insert(0, teacherAwardItemVo.getName());
            }
        }
        sb.deleteCharAt(sb.length() - 1);

        TeacherAward updateTeacherAward = new TeacherAward();
        updateTeacherAward.setId(teacherAward.getId());
        updateTeacherAward.setStatus(1);
        updateTeacherAward.setWholeCompetitionName(sb.toString());

        return this.updateById(updateTeacherAward);
    }

    private List<TeacherAwardItemVo> findAncestors(List<TeacherAwardItemVo> itemVos, Long targetId) {
        List<TeacherAwardItemVo> ancestors = new ArrayList<>();
        Map<Long, TeacherAwardItemVo> itemMap = itemVos.stream()
                .collect(Collectors.toMap(TeacherAwardItemVo::getId, item -> item));
        findAncestorsHelper(itemMap, targetId, ancestors);
        return ancestors;
    }

    private void findAncestorsHelper(Map<Long, TeacherAwardItemVo> itemMap, Long targetId, List<TeacherAwardItemVo> ancestors) {
        if (itemMap.get(targetId) != null) {
            TeacherAwardItemVo teacherAwardItemVo = itemMap.get(targetId);
            ancestors.add(teacherAwardItemVo);
            if(teacherAwardItemVo.getParentId() != null && teacherAwardItemVo.getParentId() > 0){
                findAncestorsHelper(itemMap, teacherAwardItemVo.getParentId(), ancestors);
            }
        }
    }

}
