//package com.gree.mall.miniapp.logic.tag;
//
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.google.gson.JsonObject;
//import com.gree.mall.miniapp.bean.tag.*;
//import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
//import com.gree.mall.miniapp.commonmapper.WxCustomerMapper;
//import com.gree.mall.miniapp.exception.RemoteServiceException;
//import com.gree.mall.miniapp.logic.common.CommonLogic;
//import com.gree.mall.miniapp.logic.common.WorkWechatLogic;
//import com.gree.mall.miniapp.plus.entity.*;
//import com.gree.mall.miniapp.plus.service.*;
//import com.gree.mall.miniapp.utils.CommonUtils;
//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.contact.ExternalContact;
//import me.chanjar.weixin.cp.bean.external.contact.WxCpExternalContactBatchInfo;
//import me.chanjar.weixin.cp.bean.external.contact.WxCpExternalContactInfo;
//import org.apache.commons.lang3.StringUtils;
//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.servlet.http.HttpServletRequest;
//import java.util.*;
//import java.util.stream.Collectors;
//
//@Slf4j
//@Service
//public class TagLogic {
//    @Autowired
//    private UserTagService userTagService;
//    @Autowired
//    private UserService userService;
//    @Autowired
//    private WxCustomerMapper wxCustomerMapper;
//    @Autowired
//    private ExternalUserService externalUserService;
//    @Autowired
//    private TagService tagService;
//    @Autowired
//    private TagGroupService tagGroupService;
//    @Autowired
//    private CustomUserTagService customUserTagService;
//    @Autowired
//    private WorkWechatLogic workWechatLogic;
//    @Autowired
//    CommonLogic commonLogic;
//
//    @Transactional()
//    public void edit(HttpServletRequest request, WxCustomerTagBean wxCustomerTagBean) throws RemoteServiceException {
//
//        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);
//
//        User user = userService.lambdaQuery()
//                .eq(User::getUserId, currentCompanyWechat.getUserId())
//                .eq(User::getStatus,true)
//                .one();
//        if (user == null || StringUtils.isBlank(user.getWorkUserId())) {
//            throw new RemoteServiceException("用户不存在");
//        }
//
//        if (wxCustomerTagBean != null) {
//            //添加自定义标签
//            if (wxCustomerTagBean.getSaveTags() != null && wxCustomerTagBean.getSaveTags().size() > 0) {
//                customUserTagService.lambdaUpdate()
//                        .eq(CustomUserTag::getUnionId, wxCustomerTagBean.getUnionId())
//                        .eq(CustomUserTag::getUserid, user.getWorkUserId())
//                        .remove();
//                List<CustomUserTag> customUserTagList = new ArrayList<>();
//                wxCustomerTagBean.getSaveTags().forEach(s -> {
//                    CustomUserTag customUserTag = new CustomUserTag();
//                    customUserTag.setTagName(s);
//                    customUserTag.setUnionId(wxCustomerTagBean.getUnionId());
//                    customUserTag.setUserid(user.getWorkUserId());
//                    customUserTag.setCreateTime(new Date());
//                    customUserTagList.add(customUserTag);
//                });
//                customUserTagService.saveBatch(customUserTagList);
//            }
//
//            List<String> addWxTag = new ArrayList<>();
//            List<String> delWxTag = new ArrayList<>();
//            if (wxCustomerTagBean.getAddTags() != null) {
//                addWxTag = wxCustomerTagBean.getAddTags().stream().map(SimpleTagBean::getTagId).collect(Collectors.toList());
//            }
//            if (wxCustomerTagBean.getDelTags() != null) {
//                delWxTag = wxCustomerTagBean.getDelTags();
//            }
//
//            if (addWxTag.size() < 1 && delWxTag.size() < 1) {
//                return;
//            }
//            String externalUserId = null;
//            List<String> externalList =  workWechatLogic.getExternalUserIdByWorkUserId(user.getWorkUserId(),currentCompanyWechat);
//            if (externalList != null && externalList.size() > 0) {
//                externalUserId = getContactDetail(externalList, wxCustomerTagBean.getUnionId(),currentCompanyWechat);
//            }
//            if (externalUserId == null) {
//                throw new RemoteServiceException("请先用企业微信添加客户微信为好友，再添加客户标签");
//            }
//
//            try {
//                WxCpBaseResp wxCpBaseResp = workWechatLogic.miniTagApp(currentCompanyWechat)
//                        .getExternalContactService().markTag(user.getWorkUserId(), externalUserId, addWxTag.toArray(new String[0]), delWxTag.toArray(new String[0]));
//                if (wxCpBaseResp.success()) {
//                    if (delWxTag.size() > 0) {
//                        userTagService.lambdaUpdate()
//                                .eq(UserTag::getUnionId, wxCustomerTagBean.getUnionId())
//                                .in(UserTag::getTagId, delWxTag)
//                                .remove();
//                    }
//                    if (addWxTag.size() > 0) {
//                        for (SimpleTagBean simpleTagBean : wxCustomerTagBean.getAddTags()) {
//                            this.saveUserTag(wxCustomerTagBean.getUnionId(), user.getWorkUserId(), simpleTagBean.getTagId(), simpleTagBean.getTagName());
//                        }
//                    }
//
//                }
//            } catch (WxErrorException e) {
//                throw new RemoteServiceException("微信标签同步失败");
//            }
//
//
//        }
//
//    }
//
//    private String getContactDetail(List<String> externalList, String unionId,AdminCompanyWechat adminCompanyWechat) {
//
//        for (String externalUserId : externalList) {
//            WxCpExternalContactInfo wxCpExternalContactInfoInfo = workWechatLogic.getCustomerDetail(externalUserId, adminCompanyWechat);
//            if (wxCpExternalContactInfoInfo == null) {
//                continue;
//            }
//            if (!unionId.equals(wxCpExternalContactInfoInfo.getExternalContact().getUnionId())) {
//                continue;
//            }
//            ExternalUser externalUser = new ExternalUser();
//            BeanUtils.copyProperties(wxCpExternalContactInfoInfo.getExternalContact(), externalUser, "externalProfile");
//            externalUser.setExternalProfile(JSONObject.toJSONString(wxCpExternalContactInfoInfo.getExternalContact().getExternalProfile()));
//            externalUser.setCreateTime(new Date());
//            externalUser.setAddType(true);
//            externalUser.setCorpId(adminCompanyWechat.getCorpId());
//            externalUserService.saveOrUpdate(externalUser);
//            return externalUserId;
//        }
//        return null;
//    }
//
//
//    public List<UserWxCustomerBean> wxCustomerList(HttpServletRequest request) {
//        String userId = CommonUtils.getUserId(request);
//        User user = userService.lambdaQuery()
//                .eq(User::getUserId, userId)
//                .eq(User::getStatus,true)
//                .one();
//        if (user == null || StringUtils.isBlank(user.getWorkUserId())) {
//            return null;
//        }
//        return wxCustomerMapper.queryCustomerByUser(userId, user.getWorkUserId());
//    }
//
//    public TagBean tagList(HttpServletRequest request, String unionId) {
//        String userId = CommonUtils.getUserId(request);
//        User user = userService.lambdaQuery()
//                .eq(User::getUserId, userId)
//                .eq(User::getStatus,true)
//                .one();
//        if (user == null || StringUtils.isBlank(user.getWorkUserId())) {
//            return null;
//        }
//
//        TagBean tagBean = new TagBean();
//        tagBean.setTagGroupBeanList(wxCustomerMapper.queryTagList(unionId));
//        tagBean.setTagNameList(wxCustomerMapper.queryCustomTagList(unionId, user.getWorkUserId()));
//        return tagBean;
//    }
//
//
//    public Tag addTag(String tagGroupId, String tagName) {
//        TagGroup tagGroup = tagGroupService.getById(tagGroupId);
//        Tag tag = new Tag();
//        tag.setGroupId(tagGroupId);
//        tag.setGroupName(tagGroup.getTagGroupName());
//        tag.setName(tagName);
//        tag.setCreateTime(new Date());
//        tagService.save(tag);
//        return tag;
//    }
//
//    public void delTag(String tagId) {
//        tagService.removeById(tagId);
//    }
//
//    private void saveUserTag(String unionId, String workUserId, String tagId, String tagName) {
//        UserTag userTag = new UserTag();
//        userTag.setUserTagId(null);
//        userTag.setUnionId(unionId);
//        userTag.setUserid(workUserId);
//        userTag.setTagId(tagId);
//        userTag.setTagName(tagName);
//        userTag.setCreateTime(new Date());
//        userTagService.save(userTag);
//    }
//
//
//}
