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

import com.pcloud.book.group.dao.JoinGroupCipherDao;
import com.pcloud.book.group.dto.DayCountDTO;
import com.pcloud.book.group.dto.JoinGroupCipherDTO;
import com.pcloud.book.group.entity.JoinGroupCipher;
import com.pcloud.common.core.dao.BaseDaoImpl;

import org.springframework.stereotype.Component;

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

/**
 * @Description
 * @Author ruansiyuan
 * @Date 2019/8/5 16:18
 **/
@Component("joinGroupCipherDao")
public class JoinGroupCipherDaoImpl extends BaseDaoImpl<JoinGroupCipher> implements JoinGroupCipherDao {
    @Override
    public JoinGroupCipher getByWechatUserId(Long wechatUserId,Long classifyId) {
        Map<String,Object> map=new HashMap<>();
        map.put("wechatUserId",wechatUserId);
        map.put("classifyId",classifyId);
        return this.getSqlSession().selectOne(this.getStatement("getByWechatUserId"), map);
    }

    @Override
    public JoinGroupCipher getByCipher(String cipher) {
        return this.getSqlSession().selectOne(this.getStatement("getByCipher"), cipher);
    }

    @Override
    public JoinGroupCipherDTO getDTOByCipher(String cipher) {
        return this.getSqlSession().selectOne(this.getStatement("getDTOByCipher"), cipher);
    }

    @Override
    public void updateCipherStateToUsed(String cipher, String wxId) {
        Map<String,Object> map=new HashMap<>();
        map.put("cipher",cipher);
        map.put("wxId",wxId);
        this.getSqlSession().update(this.getStatement("updateCipherStateToUsed"), map);
    }

    @Override
    public List<JoinGroupCipherDTO> getClassifyIdsByWxIdAndAltId(String wxId, String altId) {
        Map<String,Object> map=new HashMap<>();
        map.put("wxId",wxId);
        map.put("altId",altId);
        return this.getSqlSession().selectList(this.getStatement("getClassifyIdsByWxIdAndAltId"), map);
    }

    @Override
    public void updateQrcodeId(String cipher, String wxId, Long qrcodeId) {
        Map<String,Object> map=new HashMap<>();
        map.put("cipher",cipher);
        map.put("wxId",wxId);
        map.put("qrcodeId",qrcodeId);
        this.getSqlSession().update(this.getStatement("updateQrcodeId"), map);
    }

    @Override
    public BigDecimal getPayPrice(String wxId, Long qrcodeId) {
        Map<String, Object> map = new HashMap<>();
        map.put("wxId", wxId);
        map.put("qrcodeId", qrcodeId);
        return getSessionTemplate().selectOne(getStatement("getPayPrice"), map);
    }

    @Override
    public JoinGroupCipher getByCondition(Long wechatUserId, Long bookGroupId, Integer type) {
        Map<String,Object> map=new HashMap<>();
        map.put("wechatUserId",wechatUserId);
        map.put("bookGroupId",bookGroupId);
        map.put("type",type);
        return this.getSqlSession().selectOne(this.getStatement("getByCondition"), map);
    }

    @Override
    public List<JoinGroupCipher> getListByAltIdAndWxIdAndType(String robotId, String userWxId, Integer type) {
        Map<String,Object> map=new HashMap<>();
        map.put("robotId",robotId);
        map.put("userWxId",userWxId);
        map.put("type",type);
        return this.getSqlSession().selectList(this.getStatement("getListByAltIdAndWxIdAndType"), map);
    }

    public JoinGroupCipher getByWechatUserIdAndQrcodeId(Long wechatUserId, Long qrcodeId, Integer type) {
        Map<String,Object> map=new HashMap<>();
        map.put("wechatUserId",wechatUserId);
        map.put("qrcodeId",qrcodeId);
        map.put("type",type);
        return this.getSqlSession().selectOne(this.getStatement("getByWechatUserIdAndQrcodeId"), map);
    }

    @Override
    public void updateCipher(Long id, String cipher) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("cipher", cipher);
        getSessionTemplate().update(getStatement("updateCipher"), map);
    }
}
