package com.ruoyi.excel.wecom.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.excel.wecom.domain.WecomTagDomain;
import com.ruoyi.excel.wecom.domain.WecomTagGroupDomain;
import com.ruoyi.excel.wecom.mapper.WecomTagMapper;
import com.ruoyi.excel.wecom.mapper.WecomTagGroupMapper;
import com.ruoyi.excel.wecom.model.WecomTag;
import com.ruoyi.excel.wecom.model.WecomTagGroup;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 企业微信标签服务类
 */
@Service
public class WecomTagService {
    @Autowired
    private WecomTagGroupMapper wecomTagGroupMapper;
    @Autowired
    private WecomTagMapper wecomTagMapper;


    /**
     * 同步企业标签库到数据库
     *
     * @param tagGroupList 标签库列表
     * @return 同步结果
     */
    public boolean syncCorpTagList(List<WecomTagGroup> tagGroupList) {
        try {
            // 遍历标签组列表
            for (WecomTagGroup tagGroup : tagGroupList) {
                //保存标签组
                WecomTagGroupDomain groupDomain = new WecomTagGroupDomain();
                groupDomain.setTagGroupId(tagGroup.getGroupId());
                groupDomain.setName(tagGroup.getGroupName());
                groupDomain.setCreateTime(new Date(tagGroup.getCreateTime() * 1000));
                groupDomain.setOrderNo(tagGroup.getOrder());
                wecomTagGroupMapper.insert(groupDomain);
                // 保存标签
                List<WecomTag> tagList = tagGroup.getTag();
                if (tagList != null && !tagList.isEmpty()) {
                    for (WecomTag tag : tagList) {
                        WecomTagDomain tagDomain = new WecomTagDomain();
                        BeanUtils.copyProperties(tag, tagDomain);
                        tagDomain.setTagId(tag.getId());
                        tagDomain.setTagGroupId(tagGroup.getGroupId());
                        tagDomain.setOrderNo(tag.getOrder());
                        tagDomain.setCreateTime(new Date(tag.getCreateTime() * 1000));
                        tagDomain.setSyncTime(new Date());

                        // 插入标签
                        wecomTagMapper.insert(tagDomain);
                    }
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取所有标签组
     *
     * @return 标签组列表
     */
    public List<WecomTagGroupDomain> getAllTagGroups() {
        if (wecomTagGroupMapper == null) {
            System.out.println("Mapper 接口未初始化，无法获取标签组列表");
            return new ArrayList<>();
        }
        return wecomTagGroupMapper.selectList(null);
    }

    /**
     * 获取所有标签
     *
     * @return 标签列表
     */
    public List<WecomTagDomain> getAllTags() {
        if (wecomTagMapper == null) {
            System.out.println("Mapper 接口未初始化，无法获取标签列表");
            return new ArrayList<>();
        }
        return wecomTagMapper.selectList(null);
    }
}
