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

import com.pcloud.book.applet.dao.AppletUserBookcaseDao;
import com.pcloud.book.applet.dto.AppletUserBookcaseDTO;
import com.pcloud.book.applet.dto.UserLastBookReDTO;
import com.pcloud.book.applet.entity.AppletUserBookcase;
import com.pcloud.book.applet.entity.AppletUserClickRecord;
import com.pcloud.book.rightsSetting.entity.BaseTempletClassify;
import com.pcloud.common.core.dao.BaseDaoImpl;

import org.springframework.stereotype.Component;

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

/**
 * 小程序用户书架
 */
@Component
public class AppletUserBookcaseDaoImpl extends BaseDaoImpl<AppletUserBookcase> implements AppletUserBookcaseDao {


    @Override
    public void insertClickRecord(AppletUserClickRecord appletUserClickRecord) {
        getSessionTemplate().insert(getStatement("insertClickRecord"),appletUserClickRecord);
    }

    @Override
    public Integer getUserClickServerCount(Long wechatUserId, Long bookId, Long channelId, Long adviserId,List<Long> serveIds, String fromType) {
        Map<String,Object> map = new HashMap<>();
        map.put("wechatUserId", wechatUserId);
        map.put("bookId", bookId);
        map.put("channelId", channelId);
        map.put("adviserId", adviserId);
        map.put("list",serveIds);
        map.put("fromType", fromType);
        return getSessionTemplate().selectOne(getStatement("getUserClickServerCount"), map);
    }

    @Override
    public String getMinClickTime(Long wechatUserId, Long bookId, Long channelId, Long adviserId,List<Long> serveIds, String fromType) {
        Map<String,Object> map = new HashMap<>();
        map.put("wechatUserId", wechatUserId);
        map.put("bookId", bookId);
        map.put("channelId", channelId);
        map.put("adviserId", adviserId);
        map.put("list",serveIds);
        map.put("fromType", fromType);
        return getSessionTemplate().selectOne(getStatement("getMinClickTime"),map);
    }

    @Override
    public void deleteByBookIdUserId(AppletUserBookcase appletUserBookcase) {
        getSessionTemplate().delete(getStatement("deleteByBookIdUserId"),appletUserBookcase);
    }

    @Override
    public UserLastBookReDTO getUserLastGroupBookRe(Long wechatUserId) {
        return getSessionTemplate().selectOne(getStatement("getUserLastGroupBookRe"),wechatUserId);
    }

    @Override
    public UserLastBookReDTO getUserLastRe(Long wechatUserId) {
        return getSessionTemplate().selectOne(getStatement("getUserLastRe"),wechatUserId);
    }

    @Override
    public void deleteByIds(List<Long> list) {
        getSessionTemplate().delete(getStatement("deleteByIds"), list);
    }

    @Override
    public void updateUserReadType(AppletUserBookcase appletUserBookcase) {
        getSessionTemplate().update(getStatement("updateUserReadType"),appletUserBookcase);
    }

    @Override
    public AppletUserBookcase getUserReadType(Long wechatUserId, Long bookId, Long adviserId, Long channelId) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("wechatUserId",wechatUserId);
        paramMap.put("bookId",bookId);
        paramMap.put("adviserId",adviserId);
        paramMap.put("channelId",channelId);
        return getSessionTemplate().selectOne(getStatement("getUserReadType"),paramMap);
    }

    @Override
    public List<AppletUserBookcase> getListByUserId(Long wechatUserId) {
        return getSessionTemplate().selectList(getStatement("getListByUserId"), wechatUserId);
    }

    @Override
    public List<Long> getBookcaseIdListByUser(Long wechatUserId) {
        return getSessionTemplate().selectList(getStatement("getBookcaseIdListByUser"), wechatUserId);
    }

    @Override
    public void updateRightsSettingId(Long rightsSettingId, Long bookId, Long adviserId, Long channelId) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("rightsSettingId",rightsSettingId);
        paramMap.put("bookId",bookId);
        paramMap.put("adviserId",adviserId);
        paramMap.put("channelId",channelId);
        getSessionTemplate().update(getStatement("updateRightsSettingId"), paramMap);
    }

    @Override
    public List<AppletUserBookcaseDTO> getBookUserCountList(List<Long> bookIds) {
        Map<String, Object> map = new HashMap<>();
        map.put("bookIds", bookIds);
        return getSessionTemplate().selectList(getStatement("getBookUserCountList"), map);
    }

    @Override
    public List<Long> getBookIdsByIds(List<Long> ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        return getSessionTemplate().selectList(getStatement("getBookIdsByIds"), map);
    }

    @Override
    public AppletUserBookcaseDTO getUserBookInfoByWechatUserId(Long bookId, Long channelId, Long adviserId) {
        Map<String, Long> map = new HashMap<>();
        map.put("channelId", channelId);
        map.put("adviserId", adviserId);
        map.put("bookId", bookId);
        return getSessionTemplate().selectOne(getStatement("getUserBookInfoByWechatUserId"),map);
    }

    @Override
    public AppletUserBookcase getUserReadBook(Long wechatUserId) {
        return getSessionTemplate().selectOne(getStatement("getUserReadBook"), wechatUserId);
    }

    @Override
    public BaseTempletClassify getBookClassifyInfo(Long bookId, Long channelId, Long adviserId) {
        Map<String, Long> map = new HashMap<>();
        map.put("channelId", channelId);
        map.put("adviserId", adviserId);
        map.put("bookId", bookId);
        return getSessionTemplate().selectOne(getStatement("getBookClassifyInfo"),map);
    }

    @Override
    public BaseTempletClassify getLastReadBookInfo(Long wechatUserId) {
        Map<String, Long> map = new HashMap<>();
        map.put("wechatUserId", wechatUserId);
        return getSessionTemplate().selectOne(getStatement("getLastReadBookInfo"),map);
    }

    @Override
    public UserLastBookReDTO getUserLastGroupBook(Long wechatUserId) {
        return getSessionTemplate().selectOne(getStatement("getUserLastGroupBook"),wechatUserId);
    }
}
