package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.EssentialModuleInformation;
import shop.alien.entity.store.vo.EssentialModuleInformationVo;
import shop.alien.mapper.EssentialModuleInformationMapper;
import shop.alien.store.service.EssentialModuleInformationService;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ssk
 * @since 2025-03-20
 */
@Service
@RequiredArgsConstructor
public class EssentialModuleInformationServiceImpl extends ServiceImpl<EssentialModuleInformationMapper, EssentialModuleInformation> implements EssentialModuleInformationService {

    private final EssentialModuleInformationMapper essentialModuleInformationMapper;

    @Override
    public List<EssentialModuleInformationVo> getEssentialModuleInformationList(String typeId) {
        List<EssentialModuleInformation> essentialModuleInformations = essentialModuleInformationMapper.selectList(null);
        List<EssentialModuleInformationVo> list = new ArrayList<>();
        if (StringUtils.isEmpty(typeId)) {
            for (EssentialModuleInformation essentialModuleInformation : essentialModuleInformations) {
                //找到一级数据
                if (essentialModuleInformation.getParentId() == null) {
                    EssentialModuleInformationVo essentialModuleInformationVo = new EssentialModuleInformationVo();
                    BeanUtils.copyProperties(essentialModuleInformation, essentialModuleInformationVo);
                    list.add(essentialModuleInformationVo);
                }
            }
        } else {
            EssentialModuleInformation essentialModuleInformation = essentialModuleInformationMapper.selectById(typeId);
            EssentialModuleInformationVo essentialModuleInformationVo = new EssentialModuleInformationVo();
            BeanUtils.copyProperties(essentialModuleInformation, essentialModuleInformationVo);
            list.add(essentialModuleInformationVo);
        }
        for (EssentialModuleInformationVo essentialModuleInformation : list) {
            List<EssentialModuleInformationVo> childList = getChildList(essentialModuleInformation);
            essentialModuleInformation.setChildren(childList);
        }
        return list;
    }

    @Override
    public List<EssentialModuleInformationVo> getList() {

        List<EssentialModuleInformation> essentialModuleInformations = essentialModuleInformationMapper.selectList(null);
        List<EssentialModuleInformationVo> list = new ArrayList<>();
        for (EssentialModuleInformation essentialModuleInformation : essentialModuleInformations) {
            //找到一级数据
            if (essentialModuleInformation.getParentId() == null) {
                EssentialModuleInformationVo essentialModuleInformationVo = new EssentialModuleInformationVo();
                BeanUtils.copyProperties(essentialModuleInformation, essentialModuleInformationVo);
                essentialModuleInformationVo.setLabel(essentialModuleInformation.getInformationKey());
                list.add(essentialModuleInformationVo);
            }
        }

        for (EssentialModuleInformationVo essentialModuleInformation : list) {
            List<EssentialModuleInformationVo> childList = getChildList(essentialModuleInformation);
            essentialModuleInformation.setChildren(childList);
        }

        return list;
    }

    @Override
    public void addInformation(EssentialModuleInformation essentialModuleInformation) {
        essentialModuleInformationMapper.insert(essentialModuleInformation);
    }

    @Override
    public void editInformation(EssentialModuleInformation essentialModuleInformation) {
        essentialModuleInformationMapper.updateById(essentialModuleInformation);
    }

    @Override
    public void deleteInformation(String id) {
        //查询本级数据
        EssentialModuleInformation essentialModuleInformation = essentialModuleInformationMapper.selectById(id);
        //删除下级数据
        delChild(essentialModuleInformation);
        //删除本级数据
        essentialModuleInformationMapper.deleteById(id);
    }

    // 递归查下级数据的方法
    public List<EssentialModuleInformationVo> getChildList(EssentialModuleInformationVo parent) {
        List<EssentialModuleInformation> essentialModuleInformations = essentialModuleInformationMapper.selectList(new LambdaQueryWrapper<EssentialModuleInformation>().eq(EssentialModuleInformation::getParentId, parent.getId()));
        List<EssentialModuleInformationVo> list = new ArrayList<>();
        for (EssentialModuleInformation essentialModuleInformation : essentialModuleInformations) {
            EssentialModuleInformationVo vo = new EssentialModuleInformationVo();
            BeanUtils.copyProperties(essentialModuleInformation, vo);
            vo.setLabel(essentialModuleInformation.getInformationKey());
            list.add(vo);
        }
        for (EssentialModuleInformationVo essentialModuleInformationVo : list) {
            //查询子数据是否为空
            List<EssentialModuleInformation> childCountList =
                    essentialModuleInformationMapper
                            .selectList(new LambdaQueryWrapper<EssentialModuleInformation>().eq(EssentialModuleInformation::getParentId, essentialModuleInformationVo.getId()));
            if (childCountList.size() > 0) {
                List<EssentialModuleInformationVo> childList = getChildList(essentialModuleInformationVo);
                essentialModuleInformationVo.setChildren(childList);
            }
        }
        return list;
    }

    //获取下级数据的ids
    public void delChild(EssentialModuleInformation parent) {
        //查询本级的自己数据
        List<EssentialModuleInformation> essentialModuleInformations =
                essentialModuleInformationMapper.selectList(new LambdaQueryWrapper<EssentialModuleInformation>().eq(EssentialModuleInformation::getParentId, parent.getId()));
        //自旋一下子集的数据
        for (EssentialModuleInformation essentialModuleInformation : essentialModuleInformations) {
            delChild(essentialModuleInformation);
        }
        //删除本级的子级数据
        essentialModuleInformationMapper.delete(new LambdaQueryWrapper<EssentialModuleInformation>().eq(EssentialModuleInformation::getParentId, parent.getId()));
    }

}
