//package com.gree.mall.manager.logic.tag;
//
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
//import org.apache.commons.lang3.StringUtils;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.gree.mall.manager.bean.admin.AdminUserCom;
//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.logic.common.WorkWechatLogic;
//
//import com.gree.mall.manager.plus.entity.Tag;
//import com.gree.mall.manager.plus.entity.TagGroup;
//import com.gree.mall.manager.plus.service.TagGroupService;
//import com.gree.mall.manager.plus.service.TagService;
//import me.chanjar.weixin.common.error.WxErrorException;
//import me.chanjar.weixin.cp.bean.external.WxCpUserExternalTagGroupList;
//import me.chanjar.weixin.cp.bean.message.WxCpMessage;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.integration.redis.util.RedisLockRegistry;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//import org.springframework.web.bind.annotation.GetMapping;
//
//import javax.servlet.http.HttpServletRequest;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//
//@Service
//public class TagLogic {
//    @Autowired
//    WorkWechatLogic workWechatLogic;
//    @Autowired
//    CommonLogic commonLogic;
//    @Autowired
//    TagService tagService;
//    @Autowired
//    private TagGroupService tagGroupService;
//
//    /**
//     * 标签列表
//     *
//     * @param pageNum
//     * @param pageSize
//     * @return
//     */
//    public IPage<Tag> list(HttpServletRequest request, Integer pageNum, Integer pageSize) {
//        AdminUserCom adminUser = commonLogic.getAdminUser(request);
//        List<String> companyWechatIds = adminUser.getCompanyWechatIds();
//
//        return tagService.lambdaQuery()
//                .in(CollectionUtils.isNotEmpty(companyWechatIds), Tag::getCompanyWechatId, companyWechatIds)
//                .orderByDesc(Tag::getGroupId)
//                .page(new Page<>(pageNum, pageSize));
//    }
//
//    /**
//     * 同步企业标签
//     *
//     * @return
//     * @throws WxErrorException
//     */
//    public void sync(HttpServletRequest request) throws WxErrorException {
//        AdminUserCom adminUser = commonLogic.getAdminUser(request);
//        String loginCompanyWechatId = adminUser.getCompanyWechatId();
//        if(StringUtils.isNotBlank(loginCompanyWechatId)){
//            throw new RemoteServiceException("运营账号暂无权限操作");
//        }
//
//        List<WxCpUserExternalTagGroupList.TagGroup> tags = workWechatLogic.getTag(adminUser.getAdminCompanyWechat());
//        if (tags == null || tags.size() < 1) {
//            return;
//        }
//        tagGroupService.lambdaUpdate()
//                .isNotNull(TagGroup::getTagGroupId)
//                .eq(TagGroup::getCompanyWechatId, loginCompanyWechatId)
//                .remove();
//        tagService.lambdaUpdate()
//                .isNotNull(Tag::getTagId)
//                .eq(Tag::getCompanyWechatId, loginCompanyWechatId)
//                .remove();
//
//        List<TagGroup> saveTagGroup = new ArrayList<>();
//        List<Tag> list = new ArrayList<>();
//        for (WxCpUserExternalTagGroupList.TagGroup tag : tags) {
//            String groupName = tag.getGroupName();
//            String groupId = tag.getGroupId();
//            TagGroup tagGroup = new TagGroup();
//            tagGroup.setTagGroupName(groupName);
//            tagGroup.setTagGroupId(groupId);
//            tagGroup.setDeleted(tag.getDeleted());
//            tagGroup.setSortNum(tag.getOrder());
//            tagGroup.setCompanyWechatId(adminUser.getCompanyWechatId());
//            tagGroup.setCompanyName(adminUser.getCompanyName());
//            tagGroup.setCreateTime(new Date());
//            saveTagGroup.add(tagGroup);
//
//            tag.getTag().forEach(t -> {
//                Tag bean = new Tag();
//                bean.setCreateTime(new Date());
//                bean.setGroupId(groupId);
//                bean.setGroupName(groupName);
//                bean.setTagId(t.getId());
//                bean.setName(t.getName());
//                bean.setDeleted(t.getDeleted());
//                bean.setSortNum(t.getOrder());
//                bean.setCompanyWechatId(adminUser.getCompanyWechatId());
//                bean.setCompanyName(adminUser.getCompanyName());
//                list.add(bean);
//            });
//        }
//
//        tagService.saveBatch(list);
//        tagGroupService.saveBatch(saveTagGroup);
//    }
//
//
//
//}
