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

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Maps;

import com.pcloud.book.advertising.dto.GroupTagDTO;
import com.pcloud.book.advertising.dto.WeixinClassifyInfoDTO;
import com.pcloud.book.es.entity.ESBookGroupQrcode;
import com.pcloud.book.group.dao.GroupQrcodeDao;
import com.pcloud.book.group.dto.ChangeGroupNameDTO;
import com.pcloud.book.group.dto.GroupAndUserNumberDTO;
import com.pcloud.book.group.dto.GroupMaterialDTO;
import com.pcloud.book.group.dto.GroupQrcodeDTO;
import com.pcloud.book.group.dto.GroupQrcodeFoAdDTO;
import com.pcloud.book.group.dto.GroupQrcodeInfo4Advertising;
import com.pcloud.book.group.dto.GroupQrcodeInfoDTO;
import com.pcloud.book.group.dto.GroupQrcodeServerDTO;
import com.pcloud.book.group.dto.UpdateQrDTO;
import com.pcloud.book.group.entity.GroupQrcode;
import com.pcloud.book.group.vo.ClassifyQrcodeVO;
import com.pcloud.book.group.vo.GroupQrcodeBaseInfoVO;
import com.pcloud.book.group.vo.ListGroupQrcodeResponseVO;
import com.pcloud.common.core.dao.BaseDaoImpl;

import org.springframework.stereotype.Component;

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

/**
 * @author lily
 * @date 2019/4/17 16:38
 */
@Component("groupQrcodeDao")
public class GroupQrcodeDaoImpl extends BaseDaoImpl<GroupQrcode> implements GroupQrcodeDao {

    @Override
    public List<GroupQrcode> getGroupUserNum(List<String> list) {
        return this.getSqlSession().selectList("getGroupUserNum", list);
    }

    @Override
    public List<GroupQrcodeServerDTO> getWxGroupIdByServerId(List<Long> list) {
        return this.getSqlSession().selectList(this.getStatement("getWxGroupIdByServerId"), list);
    }

    @Override
    public Integer updateGroupCount(String wxGroupId, Integer num) {
        final Map<String, Object> map = Maps.newHashMap();
        map.put("weixinGroupId", wxGroupId);
        map.put("userNumber", num);
        return this.getSqlSession().update(this.getStatement("updateGroupCount"), map);
    }

    @Override
    public void updateGroupQrcode(Long groupQrcodeId, String qrcodeUrl, Long userId) {
        final Map<String, Object> map = Maps.newHashMap();
        map.put("groupQrcodeId", groupQrcodeId);
        map.put("qrcodeUrl", qrcodeUrl);
        map.put("userId", userId);
        this.getSqlSession().update(this.getStatement("updateGroupQrcode"), map);
    }

    @Override
    public List<ListGroupQrcodeResponseVO> listAllGroupQrcode(String groupName) {
        return this.getSqlSession().selectList(this.getStatement("listAllGroupQrcode"), groupName);
    }

    @Override
    public void deleteByClassifyId(Long classifyId) {
        this.getSqlSession().update(this.getStatement("deleteByClassifyId"), classifyId);
    }

    @Override
    public Map<Long, GroupAndUserNumberDTO> listGroupAndUserNumber(List<Long> classifyIds) {
        return  this.getSqlSession().selectMap(this.getStatement("listGroupAndUserNumber"), classifyIds, "classifyId");

    }

    @Override
    public void modifyNameById(String wechatGroupName, Long id, Long updateUser) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", id);
        paramMap.put("wechatGroupName", wechatGroupName);
        paramMap.put("updateUser", updateUser);
        this.getSqlSession().update(this.getStatement("modifyNameById"), paramMap);
    }

    @Override
    public void updateQrUrl(UpdateQrDTO updateQrDTO) {
        this.getSqlSession().update(this.getStatement("updateQrUrl"), updateQrDTO);
    }

    @Override
    public List<ChangeGroupNameDTO> getWechatGroupInfo(List<Long> ids) {
        return this.getSqlSession().selectList(this.getStatement("getWechatGroupInfo"), ids);
    }

    @Override
    public void syncWeixinGroupId(Long weixinQrcodeId, String weixinGroupId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("weixinQrcodeId", weixinQrcodeId);
        paramMap.put("weixinGroupId", weixinGroupId);
        this.getSqlSession().update(this.getStatement("syncWeixinGroupId"), paramMap);
    }

    @Override
    public void updateUserNumber(String wechatGroupId, Integer memberCount) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("memberCount", memberCount);
        paramMap.put("weixinGroupId", wechatGroupId);
        this.getSqlSession().update(this.getStatement("updateUserNumber"), paramMap);
    }

    @Override
    public GroupQrcodeDTO getGroupQrcodeInfo(String weixinGroupId) {
        return this.getSqlSession().selectOne(this.getStatement("getGroupQrcodeInfo"), weixinGroupId);

    }

    @Override
    public void updateUseState(Long id, Integer useState) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", id);
        paramMap.put("useState", useState);
        this.getSqlSession().update(this.getStatement("updateUseState"), paramMap);
    }

    @Override
    public Integer changeQrcodeState(Long id, Integer qrcodeState) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", id);
        paramMap.put("qrcodeState", qrcodeState);
        return this.getSqlSession().update(this.getStatement("changeQrcodeState"), paramMap);
    }


    @Override
    public Integer changeQrcodeStateForNotOver(Long id, Integer qrcodeState) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", id);
        paramMap.put("qrcodeState", qrcodeState);
        return this.getSqlSession().update(this.getStatement("changeQrcodeStateForNotOver"), paramMap);
    }

    @Override
    public Map<Long, GroupQrcodeInfoDTO> listQrcodeInfoByIds(List<Long> groupQrcodeIds) {
        return this.getSqlSession().selectMap(this.getStatement("listQrcodeInfoByIds"), groupQrcodeIds, "id");
    }

    @Override
    public GroupQrcodeBaseInfoVO getBaseById(Long groupQrcodeId) {
        return this.getSqlSession().selectOne(this.getStatement("getBaseById"), groupQrcodeId);
    }

    @Override
    public ChangeGroupNameDTO getUpdateGroupNameParam(Long id) {
        return this.getSqlSession().selectOne(this.getStatement("getUpdateGroupNameParam"), id);
    }

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

    @Override
    public List<String> getWeixinGroupIdsByClassifyId(Long classifyId) {
        return this.getSqlSession().selectList(this.getStatement("getWeixinGroupIdsByClassifyId"), classifyId);
    }

    @Override
    public GroupQrcode getGroupQrcodeByGroupId(String wechatGroupId) {
        return this.getSqlSession().selectOne(this.getStatement("getGroupQrcodeByGroupId"), wechatGroupId);
    }

    @Override
    public GroupQrcode getGroupQrcodeByqrcodeId(Long qrcodeId) {
        return this.getSqlSession().selectOne(this.getStatement("getGroupQrcodeByqrcodeId"), qrcodeId);
    }

    @Override
    public Integer countByClassifyIds(List<Long> classifyIds) {
        return this.getSqlSession().selectOne(this.getStatement("countByClassifyIds"), classifyIds);
    }

    @Override
    public List<String> getWeixinGroupIdsByClassifyIdList(List<Long> classifyIds) {
        return this.getSqlSession().selectList(this.getStatement("getWeixinGroupIdsByClassifyIdList"), classifyIds);
    }

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

    @Override
    public void updateGroupName(Long id, String groupName) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", id);
        paramMap.put("groupName", groupName);
        this.getSqlSession().update(this.getStatement("updateGroupName"), paramMap);
    }

    @Override
    public List<GroupQrcode> getListByUserCount(Integer userCount) {
        return this.getSqlSession().selectList(this.getStatement("getListByUserCount"), userCount);
    }

    @Override
    public List<GroupQrcodeInfo4Advertising> getGroupInfo4AdvertisingByWxGroupIds(List<String> wxGroupIds) {
        return this.getSqlSession().selectList(this.getStatement("getGroupInfo4AdvertisingByWxGroupIds"), wxGroupIds);
    }

    @Override
    public List<Long> listQrcodeIdsByLabelQuery(Map<String, Object> paramMap) {
        return this.getSqlSession().selectList(getStatement("listQrcodeIdsByLabelQuery"),paramMap);
    }

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

    @Override
    public List<Long> filterDeleteQrcodeId(List<String> wxGroupIdList) {
        return this.getSqlSession().selectList(this.getStatement("filterDeleteQrcodeId"), wxGroupIdList);
    }

    @Override
    public List<GroupQrcodeFoAdDTO> GroupQrcodeFoAdDTOSByClassifyIds(List<Long> classifyIds) {
        return this.getSqlSession().selectList(this.getStatement("GroupQrcodeFoAdDTOSByClassifyIds"), classifyIds);
    }

    @Override
    public void updateRiddleOpenStatus(Long qrcodeId, Boolean status) {
        Map<String, Object> map = new HashMap<>();
        map.put("qrcodeId", qrcodeId);
        map.put("status", status);
        getSessionTemplate().update(getStatement("updateRiddleOpenStatus"), map);
    }


    public List<ClassifyQrcodeVO> getQrcodeByClassify(Long classifyId) {
        return this.getSqlSession().selectList(this.getStatement("getQrcodeByClassify"), classifyId);
    }

    public List<ClassifyQrcodeVO> getQrcodeByClassifyId(Long classifyId) {
        return this.getSqlSession().selectList(this.getStatement("getQrcodeByClassifyId"), classifyId);
    }

    @Override
    public List<GroupQrcode> getListByBookGroupIds(List<Long> bookGroupIds) {
        return this.getSqlSession().selectList(this.getStatement("getListByBookGroupIds"), bookGroupIds);
    }

    @Override
    public Map<Long, GroupQrcodeInfo4Advertising> getWechatGroupInfoMap(List<Long> qrcodeIds) {
        return this.getSqlSession().selectMap(this.getStatement("getWechatGroupInfoMap"), qrcodeIds,"groupQrcodeId");
    }

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

    @Override
    public Long getBookIdByGroupQrcodeId(Long qrcodeId) {
        return getSessionTemplate().selectOne(getStatement("getBookIdByGroupQrcodeId"), qrcodeId);
    }

    @Override
    public Map<String, Object> getGroupCountUserCountByBookId(Long bookId) {
        return getSessionTemplate().selectOne(getStatement("getGroupCountUserCountByBookId"), bookId);
    }

    @Override
    public List<GroupTagDTO> listPageGroup4Brand(Map<String, Object> map) {
        return getSessionTemplate().selectList(getStatement("listPageGroup4Brand"), map);
    }

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

    @Override
    public GroupQrcode getByWxGroupId(String wxGroupId) {
        return super.getSessionTemplate().selectOne(getStatement("getByWxGroupId"), wxGroupId);
    }

    @Override
    public List<GroupQrcode> getUsingGroupQrcodeByClassifyId(Long classifyId, Integer changeNumber) {
        Map<String, Object> map = new HashMap<>();
        map.put("classifyId", classifyId);
        map.put("changeNumber", changeNumber);
        return this.getSessionTemplate().selectList(getStatement("getUsingGroupQrcodeByClassifyId"), map);
    }

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

    @Override
    public List<GroupQrcodeDTO> getGroupQrcodeListByBookGroupId(Long bookGroupId) {
        return getSessionTemplate().selectList(getStatement("getGroupQrcodeListByBookGroupId"), bookGroupId);
    }

    @Override
    public List<GroupQrcodeDTO> listClassifyLatestGroupByBookGroup(Long bookGroupId) {
        return getSessionTemplate().selectList(getStatement("listClassifyLatestGroupByBookGroup"), bookGroupId);
    }

    @Override
    public List<GroupQrcodeDTO> getGroupQrcodeByClassifyIds(List<Long> list) {
        return super.getSqlSession().selectList("getGroupQrcodeByClassifyIds",list);
    }

    @Override
    public List<String> getWeiXinGroupIds() {
        return super.getSqlSession().selectList("getGroupQrcodeByClassifyIds",null);
    }

    @Override
    public long countAll() {
        return getSessionTemplate().selectOne("com.pcloud.book.group.dao.impl.GroupQrcodeDaoImpl.countAll");
    }

    @Override
    public List<ESBookGroupQrcode> selectPage(long start, long offset) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("start",start);
        map.put("offset",offset);
        return getSessionTemplate().selectList("selectPage",map);
    }

    @Override
    public String getMediaId(Long id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",id);
        return getSessionTemplate().selectOne(getStatement("getMediaId"),map);
    }

    @Override
    public void updateMediaId(Long id, String mediaId, Date date) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",id);
        map.put("mediaId",mediaId);
        map.put("date",date);
        getSqlSession().update(getStatement("updateMediaId"),map);
    }

    @Override
    public List<GroupQrcodeDTO> getMigrateGroup(List<String> groups) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("list",groups);
        return getSessionTemplate().selectList(getStatement("getMigrateGroup"),map);
    }

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

    @Override
    public void removeMediaIdByIds(List<Long> ids) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("list",ids);
        getSessionTemplate().delete(getStatement("removeMediaIdByIds"),map);
    }

}
