package com.gic.haoban.manage.service.service.impl;

import com.gic.commons.util.ToolUtil;
import com.gic.haoban.manage.api.dto.QywxTagInfoDTO;
import com.gic.haoban.manage.api.dto.QywxTagItemDTO;
import com.gic.haoban.manage.api.enums.QywxTagRelationSyncFlagEnum;
import com.gic.haoban.manage.api.enums.QywxTagRelationTypeEnum;
import com.gic.haoban.manage.service.dao.mapper.QywxTagItemMapper;
import com.gic.haoban.manage.service.dao.mapper.QywxTagMapper;
import com.gic.haoban.manage.service.dao.mapper.QywxTagRelationMapper;
import com.gic.haoban.manage.service.entity.TabQywxTag;
import com.gic.haoban.manage.service.entity.TabQywxTagItem;
import com.gic.haoban.manage.service.entity.TabQywxTagRelation;
import com.gic.haoban.manage.service.service.QywxTagService;
import com.gic.redis.data.util.RedisUtil;
import com.gic.wechat.api.dto.qywx.QywxTagDTO;
import com.gic.wechat.api.dto.qywx.QywxTagGroupDTO;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * Created 2021/6/21.
 *
 * @author hua
 */
@Service
public class QywxTagServiceImpl implements QywxTagService {

    @Autowired
    private QywxTagMapper qywxTagMapper;

    @Autowired
    private QywxTagItemMapper qywxTagItemMapper;

    @Autowired
    private QywxTagRelationMapper qywxTagRelationMapper;

    @Override
    public TabQywxTag getQywxTagByQywxGroupKey(String wxEnterpriseId, String groupKey) {
        return qywxTagMapper.getByQywxGroupKey(wxEnterpriseId, groupKey);
    }

    @Override
    public TabQywxTag getQywxTagByQywxTagId(String wxEnterpriseId, String id) {
        return qywxTagMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TabQywxTagItem> listByQywxTagId(String wxEnterpriseId, String qywxTagId) {
        return qywxTagItemMapper.listByQywxTagId(wxEnterpriseId, qywxTagId);
    }

    @Override
    public List<TabQywxTagRelation> listAllQywxRelation(String wxEnterpriseId) {
        return qywxTagRelationMapper.listBySyncStatus(wxEnterpriseId);
    }

    @Override
    public Pair<TabQywxTag, List<TabQywxTagItem>> saveQywxTagByQywxGroupKey(String wxEnterpriseId, QywxTagGroupDTO groupDTO) {
        String lockKey = "group-add-" + groupDTO.getGroupId();
        RedisUtil.lock(lockKey, 3L);
        //判断保存 分组
        TabQywxTag qywxTag = this.getQywxTagByQywxGroupKey(wxEnterpriseId, groupDTO.getGroupId());
        if (null == qywxTag) {
            qywxTag = new TabQywxTag();
            qywxTag.setOrder(groupDTO.getOrder());
            qywxTag.setCreateTime(new Date());
            qywxTag.setUpdateTime(new Date());
            qywxTag.setQywxGroupKey(groupDTO.getGroupId());
            qywxTag.setQywxGroupName(groupDTO.getGroupName());
            qywxTag.setQywxTagId(ToolUtil.randomUUID());
            qywxTag.setStatusFlag(1);
            qywxTag.setWxEnterpriseId(wxEnterpriseId);
            qywxTagMapper.insert(qywxTag);
        } else {
            qywxTag.setQywxGroupName(groupDTO.getGroupName());
            qywxTag.setOrder(groupDTO.getOrder());
            qywxTag.setUpdateTime(new Date());
            qywxTagMapper.updateByPrimaryKeySelective(qywxTag);
        }

        List<QywxTagDTO> dtos = groupDTO.getTag();
        if (CollectionUtils.isEmpty(dtos)) {
            return null;
        }
        Map<String, TabQywxTagItem> itemMap = new HashMap<>();
        List<TabQywxTagItem> orgItems = this.listByQywxTagId(wxEnterpriseId, qywxTag.getQywxTagId());
        if (!CollectionUtils.isEmpty(orgItems)) {
            itemMap = orgItems.stream().collect(Collectors.toMap(tab -> tab.getQywxTagKey(), dto -> dto));
        }

        List<TabQywxTagItem> items = new ArrayList<>();
        //判断保存标签项
        for (QywxTagDTO tagItem : dtos) {
            TabQywxTagItem qywxTagItem = itemMap.get(tagItem.getId());
            if (qywxTagItem == null) {
                qywxTagItem = new TabQywxTagItem();
                qywxTagItem.setQywxGroupKey(qywxTag.getQywxGroupKey());
                qywxTagItem.setCreateTime(new Date());
                qywxTagItem.setOrder(tagItem.getOrder());
                qywxTagItem.setQywxTagId(qywxTag.getQywxTagId());
                qywxTagItem.setQywxTagItemId(ToolUtil.randomUUID());
                qywxTagItem.setQywxTagKey(tagItem.getId());
                qywxTagItem.setQywxTagName(tagItem.getName());
                qywxTagItem.setStatusFlag(1);
                qywxTagItem.setUpdateTime(new Date());
                qywxTagItem.setWxEnterpriseId(wxEnterpriseId);
                qywxTagItemMapper.insert(qywxTagItem);
            } else {
                qywxTagItem.setQywxTagName(tagItem.getName());
                qywxTagItem.setOrder(tagItem.getOrder());
                qywxTagItem.setUpdateTime(new Date());
                qywxTagItemMapper.updateByPrimaryKeySelective(qywxTagItem);
            }
            items.add(qywxTagItem);
        }
        RedisUtil.unlock(lockKey);
        return Pair.of(qywxTag, items);
    }

    @Override
    public void saveQywxTagByQywxGroupKey(String wxEnterpriseId, QywxTagInfoDTO infoDTO, List<QywxTagItemDTO> items) {
        infoDTO.setQywxTagId(ToolUtil.randomUUID());
        infoDTO.setCreateTime(new Date());
        infoDTO.setStatusFlag(1);
        infoDTO.setWxEnterpriseId(wxEnterpriseId);
        infoDTO.setUpdateTime(new Date());
        infoDTO.setStatusFlag(1);

    }

    @Override
    public boolean closeSync(String wxEnterpriseId, String memberTagId) {
        qywxTagRelationMapper.delByMemberTagId(wxEnterpriseId, memberTagId);
        return true;
    }

    @Override
    public boolean saveQywxRelation(List<TabQywxTagRelation> list) {
        list.forEach(dto -> {
            dto.setCreateTime(new Date());
            dto.setUpdateTime(new Date());
            dto.setOrder(1L);
            qywxTagRelationMapper.insert(dto);
        });
        return true;
    }

//    @Override
//    public TabQywxTagRelation getQywxTagRelationById(String relationId) {
//        return qywxTagRelationMapper.selectByPrimaryKey(relationId);
//    }

    @Override
    public boolean delQywxTag(String groupKey, String wxEnterpriseId) {
        qywxTagMapper.delByGroupKey(wxEnterpriseId, groupKey);
        qywxTagItemMapper.delByQywxTagGroupKey(wxEnterpriseId, groupKey);
        return true;
    }

    @Override
    public List<TabQywxTagRelation> listQywxTagRelationByMemberTagId(String wxEnterpriseId, String memberTagId) {
        return qywxTagRelationMapper.listByMemberTagId(wxEnterpriseId, memberTagId);
    }

    @Override
    public TabQywxTagRelation getQywxTagRelationByTagItemId(String wxEnterpriseId, String tagItemId) {
        return qywxTagRelationMapper.getQywxTagRelationByTagItemId(wxEnterpriseId, tagItemId);
    }

    @Override
    public List<TabQywxTagRelation> listQywxTagRelationByQywxTagId(String wxEnterpriseId, String qywxTagId) {
        List<TabQywxTagRelation> ret = qywxTagRelationMapper.listByQywxTagId(wxEnterpriseId, qywxTagId);
        return ret;
    }

    @Override
    public TabQywxTagItem getQywxTagItemByQywxItemKey(String wxEnterpriseId, String qywxTagKey) {
        return qywxTagItemMapper.getByQywxTagKey(wxEnterpriseId, qywxTagKey);
    }

    @Override
    public boolean delQywxTagItemById(String wxEnterpriseId, String id) {
        TabQywxTagItem item = new TabQywxTagItem();
        item.setQywxTagItemId(id);
        item.setUpdateTime(new Date());
        item.setStatusFlag(0);
        qywxTagItemMapper.updateByPrimaryKeySelective(item);
        return true;
    }

    @Override
    public TabQywxTagRelation getQywxTagRelationByQywxKey(String wxEnterpriseId, String key, Integer qywxTagRelationType) {
        if (qywxTagRelationType == QywxTagRelationTypeEnum.TAG_GROUP.getType()) {
            TabQywxTag qywxGroupKey = qywxTagMapper.getByQywxGroupKey(wxEnterpriseId, key);
            if (null == qywxGroupKey) {
                return null;
            }
            return qywxTagRelationMapper.getQywxGroupByQywxTagId(wxEnterpriseId, qywxGroupKey.getQywxTagId());
        } else {
            TabQywxTagItem qywxTagItem = qywxTagItemMapper.getByQywxTagKey(wxEnterpriseId, key);
            if (null == qywxTagItem) {
                return null;
            }
            return qywxTagRelationMapper.getQywxTagByQywxTagItemId(wxEnterpriseId, qywxTagItem.getQywxTagItemId());
        }
    }

    @Override
    public List<TabQywxTagItem> listQywxTagItemByIds(String wxEnterpriseId, List<String> ids) {
        return qywxTagItemMapper.listByQywxTagItemsIds(wxEnterpriseId, ids);
    }

    @Override
    public void changeTagRelationStatus(String id, int status) {
        TabQywxTagRelation tabQywxTagRelation = new TabQywxTagRelation();
        tabQywxTagRelation.setQywxTagRelationId(id);
        tabQywxTagRelation.setStatusFlag(status);
        tabQywxTagRelation.setUpdateTime(new Date());
        qywxTagRelationMapper.updateByPrimaryKeySelective(tabQywxTagRelation);
    }

    @Override
    public TabQywxTagItem getQywxTagItemById(String wxEnterpriseId, String id) {
        return qywxTagItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean saveQywxTagOrTagItemName(String id, String name, Integer relationType) {
        if (relationType == QywxTagRelationTypeEnum.TAG_GROUP.getType()) {
            TabQywxTag tabQywxTag = new TabQywxTag();
            tabQywxTag.setQywxTagId(id);
            tabQywxTag.setQywxGroupName(name);
            tabQywxTag.setUpdateTime(new Date());
            qywxTagMapper.updateByPrimaryKeySelective(tabQywxTag);
        } else if (relationType == QywxTagRelationTypeEnum.TAG_ITEM.getType()) {
            TabQywxTagItem tabQywxTagItem = new TabQywxTagItem();
            tabQywxTagItem.setQywxTagItemId(id);
            tabQywxTagItem.setQywxTagName(name);
            tabQywxTagItem.setUpdateTime(new Date());
            qywxTagItemMapper.updateByPrimaryKeySelective(tabQywxTagItem);
        }
        return true;
    }

    @Override
    public boolean changeRelationSyncFlagByMemberTagId(String wxEnterpriseId, String memberTagId, Integer syncFlag) {
        if (syncFlag == QywxTagRelationSyncFlagEnum.DEL.getType() || syncFlag == QywxTagRelationSyncFlagEnum.FAIL.getType()) {
            this.closeSync(wxEnterpriseId, memberTagId);
            return true;
        }
        qywxTagRelationMapper.changeRelationSyncFlag(wxEnterpriseId, memberTagId, syncFlag);
        return true;
    }

    @Override
    public boolean changeRelationSyncFlagByTagItemId(String wxEnterpriseId, String tagItemId, Integer syncFlag) {
        TabQywxTagRelation qywxTagRelation = qywxTagRelationMapper.getQywxTagRelationByTagItemId(wxEnterpriseId, tagItemId);
        if (null == qywxTagRelation) {
            return true;
        }
        qywxTagRelation.setSyncFlag(syncFlag);
        qywxTagRelation.setUpdateTime(new Date());
        qywxTagRelationMapper.updateByPrimaryKeySelective(qywxTagRelation);
        return true;
    }
}
