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

import cn.hutool.core.collection.CollectionUtil;
import com.gic.commons.util.EntityUtil;
import com.gic.haoban.manage.api.dto.ApplicationDTO;
import com.gic.haoban.manage.api.dto.ApplicationTemplateDTO;
import com.gic.haoban.manage.api.dto.TemplateEnterpriseRelatedDTO;
import com.gic.haoban.manage.api.dto.WxEnterpriseDTO;
import com.gic.haoban.manage.api.service.ApplicationApiService;
import com.gic.haoban.manage.service.dao.mapper.ApplicationMapper;
import com.gic.haoban.manage.service.entity.TabHaobanApplication;
import com.gic.haoban.manage.service.entity.TabHaobanTemplateApplicationRelated;
import com.gic.haoban.manage.service.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class ApplicationApiServiceImpl implements ApplicationApiService {

    @Autowired
    private ApplicationService applicationService;
    @Autowired
    private ApplicationMapper applicationMapper;
    @Autowired
    private TemplateEnterpriseRelatedService templateEnterpriseRelatedService;
    @Autowired
    private TemplateApplicationRelatedService templateApplicationRelatedService;
    @Autowired
    private ApplicationTemplateService applicationTemplateService;
    @Autowired
    private WxEnterpriseService wxEnterpriseService;

    @Override
    public List<ApplicationDTO> listApplication() {
        List<TabHaobanApplication> list = applicationService.listApplication();
        return EntityUtil.changeEntityListByJSON(ApplicationDTO.class, list);
    }

//    @Override
//    public List<ApplicationDTO> listByApplications(List<String> applicationIds) {
//        List<TabHaobanApplication> list = applicationMapper.listByApplicationIds(applicationIds);
//        if (CollectionUtil.isEmpty(list)) {
//            return null;
//        }
//        List<ApplicationDTO> result = EntityUtil.changeEntityListByJSON(ApplicationDTO.class, list);
//        return result;
//    }


    @Override
    public List<ApplicationDTO> listApplicationByWxEnterpriseId(String wxEnterpriseId) {
        List<TabHaobanApplication> list = applicationService.listApplication();
        TemplateEnterpriseRelatedDTO template = templateEnterpriseRelatedService.selectByWxEnterpriseId(wxEnterpriseId);
        if (template == null) {
            return Collections.EMPTY_LIST;
        }
        List<TabHaobanTemplateApplicationRelated> applicatinList = templateApplicationRelatedService.listByTemplateId(template.getApplicationTemplateId());
        List<String> applicationIds = applicatinList.stream().map(TabHaobanTemplateApplicationRelated::getApplicationId).collect(Collectors.toList());

        List<TabHaobanApplication> result = new ArrayList<TabHaobanApplication>();
        for (TabHaobanApplication tabHaobanApplication : list) {
            if (applicationIds.contains(tabHaobanApplication.getApplicationId())) {
                result.add(tabHaobanApplication);
            }
        }
        return EntityUtil.changeEntityListByJSON(ApplicationDTO.class, result);
    }

    @Override
    public void insertApplicationRelation(String wxEnterpriseId) {
        TemplateEnterpriseRelatedDTO dto = templateEnterpriseRelatedService.selectByWxEnterpriseId(wxEnterpriseId);
        if (dto == null) {
            ApplicationTemplateDTO defaultTemplate = applicationTemplateService.selectDefaultTemplate();
            if (defaultTemplate != null) {
                dto = new TemplateEnterpriseRelatedDTO();
                dto.setApplicationTemplateId(defaultTemplate.getApplicationTemplateId());
                dto.setWxEnterpriseId(wxEnterpriseId);
                WxEnterpriseDTO enterprise = wxEnterpriseService.selectById(wxEnterpriseId);
                if (enterprise != null) {
                    dto.setWxEnterpriseName(enterprise.getCorpName());
                }
                templateEnterpriseRelatedService.insert(dto);
            }
        }
    }

}
