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

import cn.hutool.core.collection.CollectionUtil;
import com.gic.commons.util.EntityUtil;
import com.gic.enterprise.api.dto.EnterpriseDTO;
import com.gic.enterprise.api.service.EnterpriseService;
import com.gic.haoban.manage.api.dto.*;
import com.gic.haoban.manage.api.service.ApplicationTemplateApiService;
import com.gic.haoban.manage.api.service.WxEnterpriseApiService;
import com.gic.haoban.manage.api.service.WxEnterpriseRelatedApiService;
import com.gic.haoban.manage.service.entity.TabHaobanTemplateApplicationRelated;
import com.gic.haoban.manage.service.service.ApplicationTemplateService;
import com.gic.haoban.manage.service.service.TemplateApplicationRelatedService;
import com.gic.haoban.manage.service.service.TemplateEnterpriseRelatedService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ApplicationTemplateApiServiceImpl implements ApplicationTemplateApiService {

	private static Logger logger  = LoggerFactory.getLogger(ApplicationTemplateApiServiceImpl.class);
	
	@Autowired
	private ApplicationTemplateService applicationTemplateService;
	
	@Autowired
	private TemplateEnterpriseRelatedService templateEnterpriseRelatedService;
	
	@Autowired
	private TemplateApplicationRelatedService templateApplicationRelatedService;
	
	@Autowired
	private WxEnterpriseApiService wxEnterpriseApiService;
	
	@Autowired
	private EnterpriseService enterpriseService;
	
	@Autowired
	private WxEnterpriseRelatedApiService wxEnterpriseRelatedApiService;
	
	@Override
	public ApplicationTemplateDTO selectByTemplateName(String templateName) {
		return applicationTemplateService.selectByTemplateName(templateName);
	}
	
	@Override
	public String insertTemplate(ApplicationTemplateDTO template,String applicationIds) {
		String templateId = applicationTemplateService.insertTemplate(template);
		String[] applicationIdArr = applicationIds.split(",");
		for (String applicationId : applicationIdArr) {
			TabHaobanTemplateApplicationRelated tab  = new TabHaobanTemplateApplicationRelated();
			tab.setTemplateId(templateId);
			tab.setApplicationId(applicationId);
			templateApplicationRelatedService.insert(tab);
		}
		return templateId;
	}

	@Override
	public List<TemplateEnterpriseRelatedDTO> listEnterpriseByTemplateId(String templateId) {
		return templateEnterpriseRelatedService.listEnterpriseByTemplateId(templateId);
	}

	@Override
	public void delTemplate(String templateId) {
		List<TemplateEnterpriseRelatedDTO> list = templateEnterpriseRelatedService.listEnterpriseByTemplateId(templateId);
		if(list != null && !list.isEmpty()){
			return;
		}
		applicationTemplateService.delTemplate(templateId);
		
	}

	@Override
	public void editTemplate(String applicationTemplateId, String applicationTemplateName, String applicationIds) {
		ApplicationTemplateDTO template = applicationTemplateService.selectByTemplateName(applicationTemplateName);
		if(template != null && !template.getApplicationTemplateId().equals(applicationTemplateId)){
			return;
		}
		ApplicationTemplateDTO old = applicationTemplateService.selectByTemplateId(applicationTemplateId);
		old.setApplicationTemplateName(applicationTemplateName);
		
		templateApplicationRelatedService.update(applicationTemplateId,applicationIds);
		applicationTemplateService.editTemplate(old);
		
	}

	@Override
	public List<ApplicationTemplateDTO> listByTemplateName(String applicationTemplateName) {
		List<ApplicationTemplateDTO> list = applicationTemplateService.listByTemplateName(applicationTemplateName);
		for (ApplicationTemplateDTO applicationTemplateDTO : list) {
			List<TabHaobanTemplateApplicationRelated> applicationList  = templateApplicationRelatedService.listByTemplateId(applicationTemplateDTO.getApplicationTemplateId());
			List<String> applicationIdList = applicationList.stream().map(s->s.getApplicationId()).collect(Collectors.toList());
			applicationTemplateDTO.setApplicationIds(applicationIdList);
		}
		return list;
	}

	@Override
	public void addTemplateEnterprise(String applicationTemplateId, String wxEnterpriseIds) {
		String[] wxEnterpriseIdArr = wxEnterpriseIds.split(",");
		for (String wxEnterpriseId : wxEnterpriseIdArr) {
			TemplateEnterpriseRelatedDTO dto = templateEnterpriseRelatedService.selectByWxEnterpriseId(wxEnterpriseId);
			if(dto != null){
				if(!dto.getApplicationTemplateId().equals(applicationTemplateId)){
					dto.setApplicationTemplateId(applicationTemplateId);
					templateEnterpriseRelatedService.update(dto);
				}
				continue;
			}
			dto = new TemplateEnterpriseRelatedDTO();
			dto.setWxEnterpriseId(wxEnterpriseId);
			WxEnterpriseDTO enterprise = wxEnterpriseApiService.getOne(wxEnterpriseId);
			if(enterprise != null){
				dto.setWxEnterpriseName(enterprise.getCorpName());
			}
			dto.setApplicationTemplateId(applicationTemplateId);
			templateEnterpriseRelatedService.insert(dto);
		}
	}

	@Override
	public void delTemplateEnterprise(String wxEnterpriseId) {
		TemplateEnterpriseRelatedDTO dto = templateEnterpriseRelatedService.selectByWxEnterpriseId(wxEnterpriseId);
		if(dto != null){
			dto.setStatusFlag(0);
			templateEnterpriseRelatedService.update(dto);
		}
		
	}

	@Override
	public List<ApplicationEnterpriseRelatedDTO> listTemplateEnterprise(String applicationTemplateId, String wxEnterpriseName, String enterpriseName) {
		List<String> wxEnterpriseIds = new ArrayList<String>();
		if(!StringUtils.isEmpty(enterpriseName)){
			List<EnterpriseDTO> enterpriseNames = enterpriseService.getEnterpriseByName(enterpriseName);
			if(CollectionUtil.isNotEmpty(enterpriseNames)){
				List<String>enterpriseIds = enterpriseNames.stream().map(s->s.getEnterpriseId()).collect(Collectors.toList());
				List<EnterpriseDetailDTO> wxEnterpriseIdDTOs = wxEnterpriseRelatedApiService.listEnterpriseByEnterpriseIds(enterpriseIds);
				if(CollectionUtil.isNotEmpty(wxEnterpriseIdDTOs)){
					wxEnterpriseIds = wxEnterpriseIdDTOs.stream().map(s->s.getWxEnterpriseId()).collect(Collectors.toList());
				}
			}
			if(CollectionUtil.isEmpty(wxEnterpriseIds)){
				wxEnterpriseIds = Arrays.asList("-9999");
			}
		}
		return EntityUtil.changeEntityListByJSON(ApplicationEnterpriseRelatedDTO.class, templateEnterpriseRelatedService.listEnterpriseByParams(applicationTemplateId,wxEnterpriseIds,wxEnterpriseName));
	}

	@Override
	public void setDefaultTemplateEnterprise(String applicationTemplateId) {
		List<ApplicationTemplateDTO> list = this.listByTemplateName("");
		for (ApplicationTemplateDTO applicationTemplateDTO : list) {
			if(applicationTemplateDTO.getDefaultFlag() != null && applicationTemplateDTO.getDefaultFlag() == 1){
				applicationTemplateDTO.setDefaultFlag(0);
				applicationTemplateService.editTemplate(applicationTemplateDTO);
			}
		}
		
		ApplicationTemplateDTO dto = applicationTemplateService.selectByTemplateId(applicationTemplateId);
		dto.setDefaultFlag(1);
		applicationTemplateService.editTemplate(dto);
	}

}
