//package com.gree.mall.manager.logic.tag;
//
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
//import com.baomidou.mybatisplus.core.toolkit.IdWorker;
//import com.baomidou.mybatisplus.core.toolkit.Sequence;
//import com.gree.mall.manager.bean.admin.AdminUserCom;
//import com.gree.mall.manager.bean.tag.CustomTagGroup;
//import com.gree.mall.manager.bean.tag.TagGroupBean;
//import com.gree.mall.manager.commonmapper.CustomTagGroupMapper;
//import com.gree.mall.manager.constant.Constant;
//import com.gree.mall.manager.exception.RemoteServiceException;
//import com.gree.mall.manager.logic.common.CommonLogic;
//import com.gree.mall.manager.plus.entity.Tag;
//import com.gree.mall.manager.plus.entity.TagGroup;
//import com.gree.mall.manager.plus.entity.UserTag;
//import com.gree.mall.manager.plus.service.TagGroupService;
//import com.gree.mall.manager.plus.service.TagService;
//import com.gree.mall.manager.plus.service.UserTagService;
//import com.gree.mall.manager.utils.CommonUtils;
//import io.swagger.annotations.ApiParam;
//import lombok.extern.slf4j.Slf4j;
//import me.chanjar.weixin.common.error.WxErrorException;
//import me.chanjar.weixin.cp.bean.WxCpBaseResp;
//import me.chanjar.weixin.cp.bean.external.WxCpUserExternalTagGroupInfo;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//import org.springframework.web.bind.annotation.RequestParam;
//
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import java.rmi.RemoteException;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//import java.util.stream.Collectors;
//
//@Slf4j
//@Service
//public class TagGroupLogic {
//
//    @Autowired
//    private TagService tagService;
//    @Autowired
//    private TagGroupService tagGroupService;
//    @Autowired
//    private CustomTagGroupMapper tagGroupMapper;
//    @Autowired
//    private WorkWechatLogic workWechatLogic;
//    @Autowired
//    CommonLogic commonLogic;
//
//
//    public List<CustomTagGroup> list(HttpServletRequest request) {
//        String userId = CommonUtils.getUserId(request);
//        List<String> companyWechatIds = new ArrayList<>();
//        if(StringUtils.isBlank(userId)){
//            companyWechatIds.add(Constant.GD_COMPANY_WECHAT_ID);
//        }else {
//            AdminUserCom adminUser = commonLogic.getAdminUser(request);
//            if(CollectionUtils.isNotEmpty(adminUser.getCompanyWechatIds())) {
//                companyWechatIds.addAll(adminUser.getCompanyWechatIds());
//            }
//        }
//        return tagGroupMapper.queryTagList(companyWechatIds);
//    }
//
//    public List<Tag> listTag(String tagGroupId) {
//        return tagService.lambdaQuery()
//                .eq(Tag::getGroupId, tagGroupId)
//                .list();
//    }
//
//
//    public TagGroup add(String groupName, HttpServletRequest request) throws RemoteServiceException {
//        AdminUserCom adminUser = commonLogic.getAdminUser(request);
//
//        WxCpUserExternalTagGroupInfo wxTagInfo = new WxCpUserExternalTagGroupInfo();
//        WxCpUserExternalTagGroupInfo.TagGroup wxTagGroup = new WxCpUserExternalTagGroupInfo.TagGroup();
//        wxTagGroup.setGroupName(groupName);
//        wxTagGroup.setTag(defaultTag());
//        wxTagInfo.setTagGroup(wxTagGroup);
//        TagGroup tagGroup = new TagGroup();
//        try {
//            WxCpUserExternalTagGroupInfo wxCpUserExternalTagGroupInfo = workWechatLogic.miniTagApp(adminUser.getAdminCompanyWechat()).getExternalContactService().addCorpTag(wxTagInfo);
//            if (wxCpUserExternalTagGroupInfo.success()) {
//                tagGroup.setTagGroupId(wxCpUserExternalTagGroupInfo.getTagGroup().getGroupId());
//                tagGroup.setTagGroupName(groupName);
//                tagGroup.setSortNum(wxCpUserExternalTagGroupInfo.getTagGroup().getOrder());
//                tagGroup.setDeleted(wxCpUserExternalTagGroupInfo.getTagGroup().getDeleted());
//                tagGroup.setCreateTime(new Date());
//                tagGroup.setCompanyWechatId(adminUser.getCompanyWechatId());
//                tagGroup.setCompanyName(adminUser.getCompanyName());
//                tagGroupService.save(tagGroup);
//            }
//        } catch (WxErrorException e) {
//            e.printStackTrace();
//            throw new RemoteServiceException("添加失败");
//        }
//
//        return tagGroup;
//    }
//
//    public Tag addTag(String tagGroupId, String tagName, HttpServletRequest request) throws RemoteServiceException {
//        AdminUserCom adminUser = commonLogic.getAdminUser(request);
//        WxCpUserExternalTagGroupInfo wxTagInfo = new WxCpUserExternalTagGroupInfo();
//        WxCpUserExternalTagGroupInfo.TagGroup wxTagGroup = new WxCpUserExternalTagGroupInfo.TagGroup();
//        List<WxCpUserExternalTagGroupInfo.Tag> tagList = new ArrayList<>();
//        WxCpUserExternalTagGroupInfo.Tag tag = new WxCpUserExternalTagGroupInfo.Tag();
//        wxTagGroup.setGroupId(tagGroupId);
//        tag.setName(tagName);
//        tagList.add(tag);
//        wxTagGroup.setTag(tagList);
//        wxTagInfo.setTagGroup(wxTagGroup);
//        Tag tagResp = new Tag();
//        try {
//            WxCpUserExternalTagGroupInfo wxCpUserExternalTagGroupInfo = workWechatLogic.miniTagApp(adminUser.getAdminCompanyWechat()).getExternalContactService().addCorpTag(wxTagInfo);
//            if (wxCpUserExternalTagGroupInfo.success()) {
//                List<WxCpUserExternalTagGroupInfo.Tag> respTags = wxCpUserExternalTagGroupInfo.getTagGroup().getTag();
//                for (WxCpUserExternalTagGroupInfo.Tag wxTag : respTags) {
//                    TagGroup tagGroup = tagGroupService.getById(tagGroupId);
//                    tagResp.setTagId(wxTag.getId());
//                    tagResp.setGroupId(tagGroupId);
//                    tagResp.setGroupName(tagGroup.getTagGroupName());
//                    tagResp.setName(tagName);
//                    tagResp.setCreateTime(new Date());
//                    tagResp.setSortNum(wxTag.getOrder());
//                    tagResp.setDeleted(false);
//                    tagResp.setCompanyWechatId(adminUser.getCompanyWechatId());
//                    tagResp.setCompanyName(adminUser.getCompanyName());
//                    tagService.save(tagResp);
//                }
//            }
//        } catch (WxErrorException e) {
//            e.printStackTrace();
//            throw new RemoteServiceException("添加失败");
//        }
//        return tagResp;
//
//    }
//
//    /**
//     * 删除所有标签组和用户关系
//     *
//     * @param tagGroupId
//     */
//    @Transactional
//    public void del(String tagGroupId,HttpServletRequest request) throws RemoteServiceException {
//        AdminUserCom adminUser = commonLogic.getAdminUser(request);
//        List<Tag> tagList = tagService.lambdaQuery()
//                .eq(Tag::getGroupId, tagGroupId)
//                .list();
//        List<String> tagIds = new ArrayList<>();
//        if (tagList != null) {
//            tagIds = tagList.stream().map(Tag::getTagId).collect(Collectors.toList());
//        }
//        try {
//            WxCpBaseResp wxCpBaseResp = workWechatLogic.miniTagApp(adminUser.getAdminCompanyWechat()).getExternalContactService().delCorpTag(tagIds.toArray(new String[0]), new String[]{tagGroupId});
//            if (wxCpBaseResp.success()) {
//                tagService.lambdaUpdate()
//                        .eq(Tag::getGroupId, tagGroupId)
//                        .remove();
//                tagGroupService.removeById(tagGroupId);
//            }
//        } catch (WxErrorException e) {
//            e.printStackTrace();
//            throw new RemoteServiceException("删除失败");
//        }
//
//    }
//
//    @Transactional
//    public void update(TagGroupBean tagGroupBean,HttpServletRequest request) throws RemoteServiceException {
//        AdminUserCom adminUser = commonLogic.getAdminUser(request);
//        String tagGroupName = null;
//        if (StringUtils.isNotBlank(tagGroupBean.getTagGroupId()) && StringUtils.isNotBlank(tagGroupBean.getGroupName())) {
//            try {
//                WxCpBaseResp wxCpBaseGroupResp = workWechatLogic.miniTagApp(adminUser.getAdminCompanyWechat()).getExternalContactService().editCorpTag(tagGroupBean.getTagGroupId(), tagGroupBean.getGroupName(), null);
//                if (wxCpBaseGroupResp.success()) {
//                    TagGroup tagGroup = new TagGroup();
//                    tagGroupName = tagGroupBean.getGroupName();
//                    tagGroup.setTagGroupId(tagGroupBean.getTagGroupId());
//                    tagGroup.setTagGroupName(tagGroupBean.getGroupName());
//                    tagGroupService.updateById(tagGroup);
//                }
//            } catch (WxErrorException e) {
//                log.error("编辑标签组失败", e);
//                throw new RemoteServiceException("编辑标签组失败");
//            }
//        }
//        if (tagGroupBean.getTags() != null && tagGroupBean.getTags().size() > 0) {
//            for (Tag tag : tagGroupBean.getTags()) {
//                if (StringUtils.isBlank(tag.getTagId()) || StringUtils.isBlank(tag.getName())) {
//                    continue;
//                }
//                try {
//                    WxCpBaseResp wxCpBaseResp = workWechatLogic.miniTagApp(adminUser.getAdminCompanyWechat()).getExternalContactService().editCorpTag(tag.getTagId(), tag.getName(), null);
//                    if (wxCpBaseResp.success()) {
//                        Tag saveTag = new Tag();
//                        saveTag.setTagId(tag.getTagId());
//                        saveTag.setName(tag.getName());
//                        tagService.updateById(tag);
//                    }
//                } catch (WxErrorException e) {
//                    log.error("编辑标签失败", e);
//                    throw new RemoteServiceException("编辑标签失败");
//                }
//            }
//        }
//
//
//
//    }
//
//    private List<WxCpUserExternalTagGroupInfo.Tag> defaultTag() {
//        List<WxCpUserExternalTagGroupInfo.Tag> tagList = new ArrayList<>();
//        WxCpUserExternalTagGroupInfo.Tag tag = new WxCpUserExternalTagGroupInfo.Tag();
//        tag.setName("默认分组");
//        tagList.add(tag);
//        return tagList;
//    }
//
//}
