package com.dingtalk.h5app.quickstart.service.impl;

import com.dingtalk.h5app.quickstart.entity.CiKu;
import com.dingtalk.h5app.quickstart.entity.Grade;
import com.dingtalk.h5app.quickstart.mapper.CiKuMapper;
import com.dingtalk.h5app.quickstart.service.CiKuService;
import com.dingtalk.h5app.quickstart.vo.CiKuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author Eric
 */
@Service
public class CiKuServiceImpl implements CiKuService {


    @Autowired
    private CiKuMapper ciKuMapper;


    @Override
    public List<CiKuVO> findByRandom() {

        List<CiKu> ciKus = ciKuMapper.findByRandom();
        List<CiKuVO> ciKuVOS = new ArrayList<>();

        for (CiKu kus : ciKus) {
            int length = kus.getWord().length();
            Random random = new Random();
            // 返回length值以内的整数,不含length
            int i = random.nextInt(length);
            // 讲字符串转为字符数组
            char[] chars = kus.getWord().toCharArray();
            // 替换为下划线单词
            String newWord = kus.getWord().replaceFirst(String.valueOf(chars[i]), "_");
            // 封装VO对象
            CiKuVO ciKuVO = new CiKuVO();
            ciKuVO.setSubWord(newWord);
            ciKuVO.setKey(String.valueOf(chars[i]));
            ciKuVO.setChinese(kus.getChinese());
            ciKuVO.setOnline(kus.getOnline());

            ciKuVOS.add(ciKuVO);
        }

        return ciKuVOS;
    }

    @Override
    public int insertGrade(String name, String score) {
        return ciKuMapper.insertGrade(name,score);
    }

    @Override
    public List<Grade> getGrade(String name) {
        return ciKuMapper.getGrade(name);
    }
}
