import com.alibaba.fastjson.JSON;
import com.gic.haoban.manage.api.dto.MemberUnionidRelatedDTO;
import com.gic.haoban.manage.api.dto.StaffDTO;
import com.gic.haoban.manage.api.dto.qdto.hm.WxUserAddLogQDTO;
import com.gic.haoban.manage.api.enums.SyncTaskTypeEnum;
import com.gic.haoban.manage.api.service.MemberUnionidRelatedApiService;
import com.gic.haoban.manage.service.dao.mapper.TabHaobanExternalClerkRelatedMapper;
import com.gic.haoban.manage.service.entity.TabHaobanExternalClerkRelated;
import com.gic.haoban.manage.service.service.ExternalClerkRelatedService;
import com.gic.haoban.manage.service.service.out.impl.MemberUnionidRelatedApiServiceImpl;
import com.gic.member.api.service.MemberApiService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 删除无效外部联系人
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-conf.xml"})
public class DelInvalidExternalUserTest {


    private static final Logger log = LogManager.getLogger(DelInvalidExternalUserTest.class);

    @Autowired
    private MemberUnionidRelatedApiService memberUnionidRelatedApiService ;
    @Autowired
    private TabHaobanExternalClerkRelatedMapper tabHaobanExternalClerkRelatedMapper ;
    @Autowired
    private MemberApiService memberApiService;
    @Autowired
    private ExternalClerkRelatedService externalClerkRelatedService ;


    @Test
    public void test() {
       String wxEnterpriseId = "8477c149d8774f06ab841f042645832b" ;
       String wxUserId = "51301";
       String staffId = "4eaf610dde494b4bb05ea4529b86a328" ;
        this.doSelf(wxEnterpriseId,wxUserId,staffId);
    }

    public void doSelf(String wxEnterpriseId,String wxUserId , String staffId) {
        List<String> list = memberUnionidRelatedApiService.listSelfExterialList(wxEnterpriseId,wxUserId) ;
        log.info("企微={}",list);


    /*    List<TabHaobanExternalClerkRelated> gicList = this.tabHaobanExternalClerkRelatedMapper.listByWxEnterpriseIdAndWxUserId(staffId,wxEnterpriseId) ;

        if(CollectionUtils.isNotEmpty(gicList)) {
            for(TabHaobanExternalClerkRelated item : gicList) {
                String extid = item.getExternalUserId() ;
                if(!list.contains(extid)) {
                    log.info("不存在好友关系={}",extid);

                    this.delByUserIdAndExternalUserId(item) ;

                }
            }
        }*/
    }

    public void delByUserIdAndExternalUserId(TabHaobanExternalClerkRelated dto) {
        String staffId = dto.getStaffId();
        String externalUserid = dto.getExternalUserId();
        String wxEnterpriseId = dto.getWxEnterpriseId();
        int count = tabHaobanExternalClerkRelatedMapper.delByUserIdAndExternalUserId(externalUserid,staffId);
        if (count <= 0) {
            log.error("无需删除的好友数据：{}", JSON.toJSONString(dto));
            return;
        }
        List<TabHaobanExternalClerkRelated> deleteRelatedList = tabHaobanExternalClerkRelatedMapper.listByExTernalUseridAndWxUserId(externalUserid, staffId, 2);
        if (CollectionUtils.isEmpty(deleteRelatedList)) {
            log.info("无解除的好友关系：externalUserid:{},staffId:{}", externalUserid,staffId);
            return;
        }
        TabHaobanExternalClerkRelated related = deleteRelatedList.get(0);
        String enterpriseId = related.getEnterpriseId() ;
        //该企微该会员所有关联关系解除后通知会员
        List<TabHaobanExternalClerkRelated> list = tabHaobanExternalClerkRelatedMapper.listByExternalUseridAndWxEnterpriseId(externalUserid, wxEnterpriseId, Arrays.asList("1", "3", "4"));
        if (CollectionUtils.isEmpty(list)) {
            //删除的好友关系
            List<TabHaobanExternalClerkRelated> haobanExternalClerkRelatedList = tabHaobanExternalClerkRelatedMapper.listByExternalUseridAndWxEnterpriseId(externalUserid, wxEnterpriseId, Collections.singletonList("2"));
            for (TabHaobanExternalClerkRelated externalClerkRelated : haobanExternalClerkRelatedList) {
                String memberId = externalClerkRelated.getMemberId();
                log.info("通知会员 删除会员后更新为非企微好友 memberId:{}", memberId);
                List<TabHaobanExternalClerkRelated> memberList = this.externalClerkRelatedService.getAddTimeByMemberId(memberId, enterpriseId,null);
                Date date = new Date();
                Date opTime = new Date(0);
                if (CollectionUtils.isNotEmpty(memberList)){
                    for (TabHaobanExternalClerkRelated a : memberList){
                        if ((a.getStatusFlag() == 0 || a.getStatusFlag() == 2) && a.getUpdateTime().after(opTime)){
                            opTime= a.getUpdateTime();
                            date = opTime;
                        }
                    }
                }
                memberApiService.updateMemberQywxEveryOccasion(enterpriseId, memberId, 0, date,null);
            }
        }
    }






}
