package com.jsc.oscm.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jsc.oscm.dao.WordBankRepository;
import com.jsc.oscm.entity.WordBank;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class WordBankService {

    @Resource
    WordBankRepository wordBankRepository;


    public WordBank save(WordBank wordBank) {
        wordBank.setUpdateTime(new Date());
        return wordBankRepository.save(wordBank);
    }

    public boolean delete(List<String> ids) {
        for (String id : ids) {
            WordBank wordBank = queryById(Integer.valueOf(id));
            wordBank.setDelFlag(1);
            save(wordBank);
        }
        return true;
    }

    public boolean delete(Integer id) {
        WordBank wordBank = queryById(id);
        wordBank.setDelFlag(1);
        save(wordBank);
        return true;
    }

    public WordBank queryById(Integer id) {
        return wordBankRepository.getOne(id);
    }

    public List<WordBank> queryPageable(WordBank wordBank, Integer pageIndex, Integer pageSize) {
        Pageable pageable = PageRequest.of(pageIndex, pageSize, Sort.by(Sort.Order.desc("id")));
        Example<WordBank> example = Example.of(wordBank);
        if (wordBank.getWord() != null) {
            ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("word", ExampleMatcher.GenericPropertyMatchers.contains());
            example = Example.of(wordBank, matcher);
        }
        return wordBankRepository.findAll(example, pageable).getContent();
    }

    public Long queryCount(WordBank wordBank) {
        Example<WordBank> example = Example.of(wordBank);
        if (wordBank.getWord() != null) {
            ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("word", ExampleMatcher.GenericPropertyMatchers.contains());
            example = Example.of(wordBank, matcher);
        }
        return wordBankRepository.count(example);
    }

    public List<WordBank> queryAll(Map<String, Object> params) {
        List<WordBank> resultList;
        if (params.containsKey("category")) {
            List<String> queryCategoryList = JSONObject.parseArray(JSON.toJSONString(params.get("category")), String.class);
            resultList = wordBankRepository.findByCategoryIn(queryCategoryList);
            if (params.containsKey("lang")) {
                List<String> langList = JSONObject.parseArray(JSON.toJSONString(params.get("lang")), String.class);
                List<WordBank> resultListByLang = wordBankRepository.findByLangIn(langList);
                resultList.retainAll(resultListByLang);
            }
        } else {
            resultList = wordBankRepository.findAll();
        }
        if (resultList != null) {
            resultList.removeIf(checkedWord -> checkedWord.getDelFlag() != 0);
            Collections.shuffle(resultList);
        }
        return resultList;
    }

}
