package com.pcloud.book.keywords.dao.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import com.pcloud.book.group.dto.BookGroupKeywordResourceDTO;
import com.pcloud.book.group.entity.BookGroupApp;
import com.pcloud.book.group.vo.SyncKeyworsVO;
import com.pcloud.book.keywords.dao.BookKeywordDao;
import com.pcloud.book.keywords.dto.KeywordDTO;
import com.pcloud.book.keywords.dto.KeywordResourceDTO;
import com.pcloud.book.keywords.dto.ReplyKeywordDTO;
import com.pcloud.book.keywords.entity.BookKeyword;
import com.pcloud.book.keywords.vo.KeywordVO;
import com.pcloud.book.keywords.vo.ListKeywordVO;
import com.pcloud.common.core.dao.BaseDaoImpl;

import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lily
 * @date 2019/4/25 11:50
 */
@Component("bookKeywordDao")
public class BookKeywordDaoImpl extends BaseDaoImpl<BookKeyword> implements BookKeywordDao {
    @Override
    public void deleteById(Long keywordId, Long partyId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("keywordId", keywordId);
        paramMap.put("updateUser", partyId);
        this.getSqlSession().update(this.getStatement("deleteById"), paramMap);
    }

    @Override
    public void setRank(BookKeyword bookKeyword) {
        this.getSqlSession().update(this.getStatement("setRank"), bookKeyword);
    }

    @Override
    public Integer getMaxRank(Long bookGroupId, Long classifyId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("bookGroupId", bookGroupId);
        paramMap.put("classifyId", classifyId);
        return this.getSqlSession().selectOne(this.getStatement("getMaxRank"), paramMap);
    }

    @Override
    public List<KeywordDTO> listFiveKeyword(Long classifyId, Long bookGroupId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("classifyId", classifyId);
        paramMap.put("bookGroupId", bookGroupId);
        return this.getSqlSession().selectList(this.getStatement("listFiveKeyword"), paramMap);
    }

    @Override
    public ReplyKeywordDTO getKeywordId(Long classifyId, Long bookGroupId, String content) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("classifyId", classifyId);
        paramMap.put("bookGroupId", bookGroupId);
        paramMap.put("content", content);
        return this.getSqlSession().selectOne(this.getStatement("getKeywordId"), paramMap);
    }

    @Override
    public List<ReplyKeywordDTO> listAllKeywordByClassifyIdAndBookGroupId(Long classifyId, Long bookGroupId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("classifyId", classifyId);
        paramMap.put("bookGroupId", bookGroupId);
        return this.getSqlSession().selectList(this.getStatement("listAllKeywordByClassifyIdAndBookGroupId"), paramMap);
    }

    @Override
    public List<ReplyKeywordDTO> getKeywordIds(List<Long> classifyIds, List<Long> bookGroupIds, String content) {
        if (CollectionUtils.isEmpty(classifyIds) || CollectionUtils.isEmpty(bookGroupIds) || StringUtils.isEmpty(content)) {
            return Lists.newArrayList();
        }
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("classifyId", classifyIds.stream().map(Object::toString).collect(Collectors.joining(",")));
        paramMap.put("bookGroupId", bookGroupIds.stream().map(Object::toString).collect(Collectors.joining(",")));
        paramMap.put("content", content);
        return this.getSqlSession().selectList(this.getStatement("getKeywordIds"), paramMap);
    }

    @Override
    public Boolean checkKeyword(String keywords, Long classifyId, Long bookGroupId, Long keywordId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("classifyId", classifyId);
        paramMap.put("bookGroupId", bookGroupId);
        paramMap.put("keywords", keywords);
        paramMap.put("keywordId", keywordId);
        return this.getSqlSession().selectOne(this.getStatement("checkKeyword"), paramMap);
    }

    @Override
    public KeywordVO getByBookKeyword(Long bookKeywordId) {
        return this.getSqlSession().selectOne(this.getStatement("getByBookKeyword"), bookKeywordId);
    }

    @Override
    public void updateIsWarehouse(Long bookKeywordId, Long warehouseId) {
        final Map<String, Object> map = Maps.newHashMap();
        map.put("bookKeywordId", bookKeywordId);
        map.put("warehouseId", warehouseId);
        this.getSqlSession().update("updateIsWarehouse", map);
    }

    @Override
    public Integer getKeywordCount(Long bookGroupId, Long classifyId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("bookGroupId", bookGroupId);
        paramMap.put("classifyId", classifyId);
        return this.getSqlSession().selectOne(this.getStatement("getKeywordCount"), paramMap);
    }

    @Override
    public List<ListKeywordVO> getKeywordsByClassifyId(Long classifyId, Long bookId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("classifyId", classifyId);
        paramMap.put("bookId", bookId);
        return this.getSqlSession().selectList(this.getStatement("getKeywordsByClassifyId"), paramMap);
    }

    @Override
    public List<SyncKeyworsVO> getKeywordsIdsByBookGroupId(Long bookGroupId, Long classifyId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("bookGroupId", bookGroupId);
        paramMap.put("classifyId", classifyId);
        return this.getSqlSession().selectList(this.getStatement("getKeywordsIdsByBookGroupId"), paramMap);
    }

    @Override
    public void deleteKeywords(Long keywordId, Long bookGroupId, Long classifyId, Long partyId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("keywordId", keywordId);
        paramMap.put("bookGroupId", bookGroupId);
        paramMap.put("classifyId", classifyId);
        paramMap.put("partyId", partyId);
        this.getSqlSession().update(this.getStatement("deleteKeywords"), paramMap);
    }

    @Override
    public void updateRank(Long bookGroupId, Integer rank, Long partyId, Long keywordId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("rank", rank);
        paramMap.put("bookGroupId", bookGroupId);
        paramMap.put("partyId", partyId);
        paramMap.put("keywordId", keywordId);
        this.getSqlSession().update(this.getStatement("updateRank"), paramMap);
    }

    @Override
    public List<KeywordDTO> getListByBookGroupId(Long bookGroupId) {
        return this.getSqlSession().selectList(this.getStatement("getListByBookGroupId"), bookGroupId);
    }

    @Override
    public List<KeywordDTO> getListByBookGroupIdsAndType(List<Long> bookGroupIds, Integer replyType) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("bookGroupIds", bookGroupIds);
        paramMap.put("replyType", replyType);
        return this.getSqlSession().selectList(this.getStatement("getListByBookGroupIdsAndType"), paramMap);
    }

    @Override
    public void deleteByBookGroupIds(List<Long> bookGroupIds) {
        this.getSqlSession().update(this.getStatement("deleteByBookGroupIds"), bookGroupIds);
    }

    @Override
    public Map<Long, KeywordResourceDTO> getBookGroupResourceCount(List<Long> bookGroupIds) {
        Map<String, Object> paramMap = new HashMap();
        paramMap.put("bookGroupIds", bookGroupIds);
        return this.getSqlSession().selectMap(this.getStatement("getBookGroupResourceCount"), paramMap, "bookGroupId");
    }

    @Override
    public List<BookGroupKeywordResourceDTO> getBookGroupResourceList(Long bookGroupId) {
        Map<String, Object> paramMap = new HashMap();
        paramMap.put("bookGroupId", bookGroupId);
        return getSessionTemplate().selectList(getStatement("getBookGroupResourceList"), paramMap);
    }

    @Override
    public List<BookGroupApp> getAllAppKeyword() {
        return getSessionTemplate().selectList(getStatement("getAllAppKeyword"));
    }

    @Override
    public List<ListKeywordVO> getAppKeywordsByClassifyId(Long classifyId, Long bookGroupId, List<Long> keywordIdList) {
        Map<String, Object> paramMap = new HashMap();
        paramMap.put("classifyId", classifyId);
        paramMap.put("bookGroupId", bookGroupId);
        paramMap.put("keywordIdList", keywordIdList);
        return getSessionTemplate().selectList(getStatement("getAppKeywordsByClassifyId"), paramMap);
    }

}
