package com.gic.haoban.manage.service.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.EntityUtil;
import com.gic.enterprise.api.dto.EnterpriseSettingDTO;
import com.gic.enterprise.api.service.EnterpriseService;
import com.gic.haoban.manage.service.dao.mapper.WxEnterpriseMapper;
import com.gic.haoban.manage.service.entity.TabHaobanWxEnterprise;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gic.haoban.manage.service.dao.mapper.WxEnterpriseRelatedMapper;
import com.gic.haoban.manage.service.entity.TabHaobanWxEnterpriseRelated;
import com.gic.haoban.manage.service.pojo.bo.WxEnterpriseRelationBO;
import com.gic.haoban.manage.service.service.WxEnterpriseRelatedService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static org.slf4j.LoggerFactory.getLogger;

@Service
public class WxEnterpriseRelatedServiceImpl implements WxEnterpriseRelatedService {

    private static final Logger logger = getLogger(WxEnterpriseRelatedServiceImpl.class);

	@Autowired
	private WxEnterpriseRelatedMapper mapper;

    @Autowired
    private WxEnterpriseMapper wxEnterpriseMapper;
    @Autowired
    private EnterpriseService enterpriseService ;

	@Override
	public TabHaobanWxEnterpriseRelated getByGicEnterpriseIdByEnterpriseRelatedId(String wxEnterpriseRelatedId) {
		
		return mapper.selectByPrimaryKey(wxEnterpriseRelatedId);
	}


	@Override
	public List<TabHaobanWxEnterpriseRelated> getByWxEnterpriseId(String wxEnterpriseId) {
		List<TabHaobanWxEnterpriseRelated> list = mapper.listByWxenterpriseId(wxEnterpriseId);
		return list;
	}

    @Override
    public List<TabHaobanWxEnterprise> listByEnterpriseId(String enterpriseId) {
        List<String> eids = new ArrayList<>();
        eids.add(enterpriseId);
        List<TabHaobanWxEnterpriseRelated> relateds = mapper.listEnterpriseByEnterpriseIds(eids);
        if (CollectionUtils.isEmpty(relateds)) {
            return null;
        }
        Set<String> wxEnterpriseIds = relateds.stream().map(tab -> tab.getWxEnterpriseId()).collect(Collectors.toSet());
        return wxEnterpriseMapper.listByIds(wxEnterpriseIds);
    }

    @Override
    public TabHaobanWxEnterpriseRelated getByEnterpriseId(String wxEnterpriseId, String enterpriseId) {
        return mapper.findOneByEIdAndWxEid(enterpriseId, wxEnterpriseId);
    }

    @Override
    public void setClerkEdit(String id, int clerkEditFlag) {
        TabHaobanWxEnterpriseRelated record = new TabHaobanWxEnterpriseRelated();
        record.setClerkEditFlag(clerkEditFlag);
        record.setWxEnterpriseRelatedId(id);
        record.setUpdateTime(new Date());
        mapper.updateByPrimaryKeySelective(record);
    }
    
    @Override
    public int getRelationCount(String enterpriseId) {
        List<TabHaobanWxEnterpriseRelated> list = this.mapper.listByEnterpriseId(enterpriseId) ;
        return list.size();
    }
    
    @Override
    public WxEnterpriseRelationBO getHeadEnterprise(String enterpriseId) {
        TabHaobanWxEnterpriseRelated root = this.mapper.findRootWxEnterpriseByEnterpriseId(enterpriseId) ;
        if(null != root) {
            return EntityUtil.changeEntityByJSON(WxEnterpriseRelationBO.class, root);
        }
        return null;
    }
    
    
    @Override
    public boolean getAddByWelcomeFlag(TabHaobanWxEnterpriseRelated wxEnterpriseRelated, String unionid , boolean isHmUnionid) {
        int openCardFlag = wxEnterpriseRelated.getMemberOpenCardFlag() ;
        int wxEnterpriseType = wxEnterpriseRelated.getWxEnterpriseType() ;
        // 活码
        if(StringUtils.isNotBlank(unionid) && isHmUnionid) {
            return false ;
        }
        // unionid存在,并且在同一开放平台,可以创建客户
        if(StringUtils.isNotBlank(unionid) && openCardFlag==0) {
            return false ;
        }
        String enterpriseId = wxEnterpriseRelated.getEnterpriseId() ;
        EnterpriseSettingDTO setting = this.enterpriseService.getEnterpriseSettingByEnterpriseId(enterpriseId) ;
        String customSetting = setting.getCustomSetting() ;
        boolean settFlag = true ;
        if(StringUtils.isNotBlank(customSetting)) {
            JSONObject json = JSON.parseObject(customSetting) ;
            String pendingidFlag = json.getString("qwPendingidFlag") ;
            if(StringUtils.isBlank(pendingidFlag) || "0".equals(pendingidFlag)) {
                settFlag = false ;
            }
        }
        if(openCardFlag == 0 && wxEnterpriseType ==0 && settFlag)  {
            return false ;
        }
        return true ;
    }

    @Override
    public ServiceResponse<Integer> getEnterprieseQwType(String enterpriseId) {
        List<TabHaobanWxEnterpriseRelated> list = this.mapper.listByEnterpriseId(enterpriseId) ;
        if(CollectionUtils.isEmpty(list)) {
            return ServiceResponse.success(0) ;
        }
        logger.info("gic商户={}关联={}",enterpriseId,list.size());
        Set<String> wxEidList = list.stream().map(o->o.getWxEnterpriseId()).collect(Collectors.toSet()) ;
        List<TabHaobanWxEnterprise> wxList = this.wxEnterpriseMapper.listByIds(wxEidList) ;
        if(CollectionUtils.isEmpty(wxList)) {
            return ServiceResponse.success(0) ;
        }
        boolean selfFlag = false , thirdFlag = false ;
        for (TabHaobanWxEnterprise o : wxList) {
            if(o.getWxSecurityType()==5) {
                selfFlag = true ;
            }else {
                thirdFlag = true ;
            }
        }
        if(selfFlag && thirdFlag) {
            return ServiceResponse.success(3) ;
        }else if(selfFlag) {
            return ServiceResponse.success(1) ;
        }else {
            return ServiceResponse.success(2) ;
        }
    }
}
