//package com.gree.mall.manager.logic.tag;
//
//import com.gree.mall.manager.logic.admin.AdminWebsitLogic;
//import com.gree.mall.manager.logic.common.CommonLogic;
//import com.gree.mall.manager.logic.common.WorkWechatLogic;
//import com.gree.mall.manager.plus.entity.AdminCompanyWechat;
//import com.gree.mall.manager.plus.entity.AdminWebsit;
//import com.gree.mall.manager.plus.entity.EnterpriseWechatWorker;
//import com.gree.mall.manager.plus.service.AdminWebsitService;
//import com.gree.mall.manager.plus.service.EnterpriseWechatWorkerService;
//import lombok.extern.slf4j.Slf4j;
//import me.chanjar.weixin.common.error.WxErrorException;
//import me.chanjar.weixin.cp.bean.WxCpDepart;
//import me.chanjar.weixin.cp.bean.WxCpUser;
//import me.chanjar.weixin.cp.bean.external.WxCpContactWayInfo;
//import me.chanjar.weixin.cp.bean.external.WxCpContactWayResult;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import java.util.*;
//import java.util.stream.Collectors;
//
//@Service
//@Slf4j
//public class DepartmentSyncLogic {
//    @Autowired
//    private EnterpriseWechatWorkerService enterpriseWechatWorkerService;
//    @Autowired
//    private AdminWebsitService adminWebsitService;
//    @Autowired
//    private WorkWechatLogic workWechatLogic;
//    @Autowired
//    CommonLogic commonLogic;
//    @Autowired
//    AdminWebsitLogic adminWebsitLogic;
//
//
//    public void syncDepartmentData(AdminCompanyWechat adminCompanyWechat) throws WxErrorException {
//
//        //先删除手机号为空的
//        //enterpriseWechatWorkerService.lambdaUpdate().ne(EnterpriseWechatWorker::getMobile, "").remove();
//        int count = enterpriseWechatWorkerService.lambdaQuery()
//                .eq(EnterpriseWechatWorker::getCorpId,adminCompanyWechat.getCorpId()).count();
//        if(count > 0){
//            return;
//        }
//        //企微所有成员
//        List<WxCpUser> wxCpUsers = workWechatLogic.miniApp(adminCompanyWechat).getUserService().listByDepartment(1L, true, 0);
//        List<EnterpriseWechatWorker> list = new ArrayList<>();
//        for (WxCpUser wxCpUser : wxCpUsers) {
//            EnterpriseWechatWorker bean = new EnterpriseWechatWorker();
//            if (StringUtils.isBlank(wxCpUser.getMobile())) {
//                continue;
//            }
//            if(wxCpUser.getMainDepartment() != null) {
//                AdminWebsit adminWebsit = adminWebsitLogic.detail(Long.parseLong(wxCpUser.getMainDepartment()), adminCompanyWechat.getCorpId());
//                if(adminWebsit != null){
//                    bean.setDepartmentName(adminWebsit.getName());
//                }
//            }
//
//            bean.setWechatUserId(wxCpUser.getUserId());
////            bean.setName(wxCpUser.getName());
////            bean.setEmail(wxCpUser.getEmail());
////            bean.setMainDepartment(wxCpUser.getMainDepartment());
////            Long[] departmentIds = wxCpUser.getDepartIds();
////            Arrays.sort(departmentIds);
////            String departIdS = StringUtils.join(departmentIds, ",");
////            bean.setDepartmentIds(departIdS);
////            bean.setMobile(wxCpUser.getMobile());
////            bean.setQrCode(wxCpUser.getQrCode());
////            bean.setPosition(wxCpUser.getPosition());
////            bean.setCorpId(adminCompanyWechat.getCorpId());
////            bean.setCreateTime(new Date());
//            list.add(bean);
//        }
//        //新增新的成员
//        if (list.size() > 0) {
//            enterpriseWechatWorkerService.updateBatchById(list);
//        }
//
//    }
//
//
////    public void pullDepartmentList(AdminCompanyWechat adminCompanyWechat) {
////        try {
////            int count = adminWebsitService.lambdaQuery().eq(AdminWebsit::getCorpId,adminCompanyWechat.getCorpId()).count();
////            if(count > 0){
////                return;
////            }
////
////            List<WxCpDepart> wxCpDeparts = workWechatLogic.miniApp(adminCompanyWechat).getDepartmentService().list(null);
////            if (wxCpDeparts != null && wxCpDeparts.size() > 0) {
////                List<AdminWebsit> currDeparts = adminWebsitService.lambdaQuery().select(AdminWebsit::getId).list();
////                List<Long> currDepartIds = currDeparts.stream().map(AdminWebsit::getWebsitId).collect(Collectors.toList());
////
////                List<AdminWebsit> adminWebsits = new ArrayList<>();
////                for (WxCpDepart wxCpDepart : wxCpDeparts) {
////                    if (currDepartIds.contains(wxCpDepart.getId())) {
////                        continue;
////                    }
////                    AdminWebsit adminWebsit = new AdminWebsit();
////                    BeanUtils.copyProperties(wxCpDepart, adminWebsit);
////                    adminWebsit.setWebsitId(wxCpDepart.getId());
////                    adminWebsit.setSortNum(wxCpDepart.getOrder());
////                    adminWebsit.setCorpId(adminCompanyWechat.getCorpId());
////                    adminWebsits.add(adminWebsit);
////
////                }
////                adminWebsitService.saveBatch(adminWebsits);
////            }
////        } catch (WxErrorException e) {
////            log.error("获取部门结构错误", e);
////        }
////    }
//}
