package com.xunwang.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xunwang.education.entity.Strategy;
import com.xunwang.education.mapper.StrategyMapper;
import com.xunwang.education.service.StrategyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 攻略表 服务实现类
 * </p>
 *
 * @author Long
 * @since 2023-04-26
 */
@Service
public class StrategyServiceImpl extends ServiceImpl<StrategyMapper, Strategy> implements StrategyService {

    /**
     * 获取攻略子图
     *
     * @return
     */
    @Override
    public List<String> getSmallPig() {
        return getSinglePic(false);
    }

    /**
     * 获取攻略图大图
     * @return
     */
    @Override
    public List<String> getBigPig() {
        return getSinglePic(true);
    }

    private List<String> getSinglePic(boolean isParent) {
        LambdaQueryWrapper<Strategy> wrapper = new LambdaQueryWrapper<>();
        if (isParent) {
            wrapper.eq(Strategy::getStatus, "1").eq(Strategy::getParentId, 0);
        } else {
            wrapper.eq(Strategy::getStatus, "1").ne(Strategy::getParentId, 0);
        }
        List<Strategy> list = baseMapper.selectList(wrapper);
        Collections.sort(list, new Comparator<Strategy>() {
            @Override
            public int compare(Strategy o1, Strategy o2) {
                if (o1.getSort() >= o2.getSort()) {
                    return 1;
                } else {
                    return -1;
                }
            }
        });
        ArrayList<String> imgList = new ArrayList<>();
        list.forEach(item -> imgList.add(item.getUrl()));
        return imgList;
    }
}
