package org.jeecg.modules.duan.java.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.jeecg.common.api.vo.Result;

import org.jeecg.modules.duan.java.entity.Ranking;
import org.jeecg.modules.duan.java.mapper.MusicMapper;
import org.jeecg.modules.duan.java.service.MusicService;
import org.jeecg.modules.duan.java.mapper.MusicMapper;
import org.jeecg.modules.duan.java.service.MusicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MusicServiceImpl extends ServiceImpl<MusicMapper, Ranking> implements MusicService {

    @Autowired
    private MusicMapper musicMapper;

    @Override
    public Result<JSONArray> getRanking() {
        ComparatorList comparatorList = new ComparatorList();
        List<Ranking> rankings = musicMapper.selectList(null);
        JSONArray result = new JSONArray();
        System.out.println(rankings);
        Collections.sort(rankings, comparatorList);
        for (Ranking ranking : rankings) {
            System.out.println("\n\n\n\n\n" + ranking);
            JSONObject object = new JSONObject();
            object.put("comment", ranking.getComment());
            object.put("singer", ranking.getSinger());
            object.put("songName", ranking.getName());
            result.add(object);
        }
        UpdateWrapper<Ranking> updateWrapper = new UpdateWrapper<>();
        updateWrapper.setSql("comment=comment- FLOOR(   RAND()*100  )");
        musicMapper.update(null,updateWrapper);

        return Result.OK(result);
    }
}

class ComparatorList implements Comparator<Ranking> {

    @Override
    public int compare(Ranking o1, Ranking o2) {
        return o1.getComment() > o2.getComment() ? 1 : -1;
    }
}
