package com.linkwechat.wecom.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.linkwechat.common.utils.DateUtils;
import com.linkwechat.common.utils.SecurityUtils;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.client.WeCropTagClient;
import com.linkwechat.wecom.domain.WeGroup;
import com.linkwechat.wecom.domain.WeTagGroup;
import com.linkwechat.wecom.domain.dto.WeTagDto;
import com.linkwechat.wecom.domain.dto.tag.WeCropGroupTagDto;
import com.linkwechat.wecom.domain.dto.tag.WeCropGroupTagListDto;
import com.linkwechat.wecom.domain.dto.tag.WeCropTagDto;
import com.linkwechat.wecom.domain.dto.tag.WeFindCropTagParam;
import com.linkwechat.wecom.service.IWeTagGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.linkwechat.wecom.mapper.WeTagMapper;
import com.linkwechat.wecom.domain.WeTag;
import com.linkwechat.wecom.service.IWeTagService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 企业微信标签Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-09-07
 */
@Service
public class WeTagServiceImpl extends ServiceImpl<WeTagMapper,WeTag> implements IWeTagService
{
    @Autowired
    private WeCropTagClient weCropTagClient;

    /**
     * 查询企业微信标签
     *
     * @param id 企业微信标签ID
     * @return 企业微信标签
     */
    @Override
    public WeTag selectWeTagById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询企业微信标签列表
     * 
     * @param weTag 企业微信标签
     * @return 企业微信标签
     */
    @Override
    public List<WeTag> selectWeTagList(WeTag weTag)
    {
        LambdaQueryWrapper<WeTag> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(weTag.getGroupId())){
            wrapper.eq(WeTag::getGroupId,weTag.getGroupId());
        }
        if(StringUtils.isNotEmpty(weTag.getName())){
            wrapper.like(WeTag::getName,weTag.getName());
        }
        if(StringUtils.isNotEmpty(weTag.getTagId())){
            wrapper.eq(WeTag::getTagId,weTag.getTagId());
        }

        if(StringUtils.isNotEmpty(weTag.getTagId())){
            wrapper.eq(WeTag::getTagId,weTag.getTagId());
        }
        return list(wrapper);
    }

    /**
     * 新增企业微信标签
     *
     * @param weTag 企业微信标签
     * @return 结果
     */
    @Override
    public boolean insertWeTag(WeTag weTag)
    {
        return save(weTag);
    }

    /**
     * 修改企业微信标签
     *
     * @param weTag 企业微信标签
     * @return 结果
     */
    @Override
    public boolean updateWeTag(WeTag weTag)
    {
        return updateById(weTag);
    }

    /**
     * 批量删除企业微信标签
     *
     * @param ids 需要删除的企业微信标签ID
     * @return 结果
     */
    @Override
    public boolean deleteWeTagByIds(List<String> ids)
    {
        return removeByIds(ids);
    }

    /**
     * 删除企业微信标签信息
     *
     * @param id 企业微信标签ID
     * @return 结果
     */
    @Override
    public boolean deleteWeTagById(String id)
    {
        return removeById(id);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void creatTag(String tagId) {
        if (StringUtils.isNotEmpty(tagId)){
            List<String> tagList = ListUtil.toList(tagId);
            WeCropGroupTagListDto weCropGroupTagListDto = weCropTagClient.getCorpTagListByTagIds(WeFindCropTagParam.builder().tag_id(tagList).build());
            List<WeCropGroupTagDto> tagGroups = weCropGroupTagListDto.getTag_group();
            if (CollectionUtil.isNotEmpty(tagGroups)) {
                tagGroups.forEach(tagGroup -> {
                    List<WeCropTagDto> tags = tagGroup.getTag();
                    if (CollectionUtil.isNotEmpty(tags)) {
                        List<WeTag> weTags = new ArrayList<>();
                        tags.forEach(tag -> WeTag.builder()
                                .tagId(tag.getId())
                                .groupId(tagGroup.getGroup_id())
                                .name(tag.getName())
                                .build());
                        if(CollectionUtil.isNotEmpty(weTags)){
                            List<List<WeTag>> lists = Lists.partition(weTags, 500);
                            for (List<WeTag> item : lists) {
                                this.baseMapper.insertWeTagBatch(item);
                            }
                        }
                    }
                });
            }
        }
    }

    @Override
    public boolean deleteByTagId(String tagId) {
        return this.remove(new LambdaQueryWrapper<WeTag>().eq(WeTag::getTagId,tagId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTag(String tagId) {
        if (StringUtils.isNotEmpty(tagId)){
            List<String> tagList = ListUtil.toList(tagId);
            WeCropGroupTagListDto weCropGroupTagListDto = weCropTagClient.getCorpTagListByTagIds(WeFindCropTagParam.builder().tag_id(tagList).build());
            List<WeCropGroupTagDto> tagGroups = weCropGroupTagListDto.getTag_group();
            if (CollectionUtil.isNotEmpty(tagGroups)) {
                tagGroups.forEach(tagGroup -> {
                    List<WeCropTagDto> tags = tagGroup.getTag();
                    if (CollectionUtil.isNotEmpty(tags)) {
                        List<WeTag> weTags = new ArrayList<>();
                        tags.forEach(tag -> WeTag.builder()
                                .tagId(tag.getId())
                                .groupId(tagGroup.getGroup_id())
                                .name(tag.getName())
                                .build());
                        if(CollectionUtil.isNotEmpty(weTags)){
                            List<List<WeTag>> lists = Lists.partition(weTags, 500);
                            for (List<WeTag> item : lists) {
                                this.baseMapper.insertWeTagBatch(item);
                            }
                        }

                    }
                });
            }
        }
    }

    @Override
    public void insertWeTagBatch(List<WeTag> weTags) {
        this.baseMapper.insertWeTagBatch(weTags);
    }


}
