package com.changhong.epc.admin.service.initguide; import com.changhong.epc.admin.mapper.centen.InitDataMapper; import com.changhong.epc.admin.mapper.centen.initguide.BatchInitCompanyMapper; import com.changhong.epc.admin.mapper.centen.initguide.InitCompanyMapper; import com.changhong.epc.bean.PageResult; import com.changhong.epc.bean.admin.BatchInitCompany; import com.changhong.epc.bean.admin.CostGroupType; import com.changhong.epc.bean.admin.InitCompany; import com.changhong.epc.bean.admin.InitPowerInfo; import com.changhong.epc.bean.tenant.system.permission.PowerKey; import com.changhong.epc.constter.base.BaseConst; import com.changhong.epc.constter.system.SystemClients; import com.changhong.epc.rely.api.bean.Company; import com.changhong.epc.rely.api.bean.Tenant; import com.changhong.epc.rely.api.epc.admin.InitCompanyApi; import com.changhong.epc.rely.api.epc.tenant.TenantInitApi; import com.changhong.epc.rely.api.service.CscApiService; import com.changhong.epc.rely.api.service.workFlow.WorkFlowService; import com.changhong.epc.rely.api.tool.PowerTool; import com.iemsoft.framework.cloud.core.thread.Keys; import com.iemsoft.framework.cloud.core.thread.ThreadData; import com.iemsoft.framework.cloud.core.tools.Assert; import com.iemsoft.framework.cloud.core.tools.StringUtil; import com.iemsoft.framework.cloud.redis.annotation.CacheRm; import org.apache.commons.lang3.math.NumberUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Optional; import static com.changhong.epc.constter.cache.admin.InitCompanyCache.INIT_COMPANY_INFO; import static com.changhong.epc.constter.cache.admin.InitCompanyCache.INIT_COMPANY_INFO_LIST; @Service public class InitGuideServiceImpl implements InitGuideService,SystemClients,BaseConst { @Resource private InitCompanyMapper initCompanyMapper; @Resource private InitDataMapper initDataMapper; @Resource private CscApiService cscApiService; @Resource private WorkFlowService workFlowService; @Resource private InitCompanyApi initCompanyApi; @Resource private TenantInitApi tenantInitApi; @Resource private BatchInitCompanyMapper batchInitCompanyMapper; public PageResult getTenants(Tenant tenant){ return cscApiService.getTenants(tenant); } @Override public PageResult getCompanys(Company company) { // List companies = cscApiService.getCompanyOrgs(tenantId); // // 过滤掉已经初始化成功的公司 // Set initCompanyIds = Arrays.asList( // Optional.ofNullable( // initCompanyMapper.selectInitCompanyId(tenantId) // ).map(ids->ids.split(",")) // .orElse(new String[0]) // ).stream() // .map(Integer::valueOf) // .collect(Collectors.toSet()); // // return companies // .stream() // .filter(comp->!initCompanyIds.contains(comp.getId())) // .collect(Collectors.toList()); return cscApiService.getCompanys(company); } public int doInit(List companys,int id,List tenants){ try{ int count = 0; List costGroupType = initCompanyMapper.getCostGroupType(id); for(Company c:companys){ String tenantName = null; Tenant tenant = null; for(Tenant t:tenants){ if(t.getId() == c.getTenantId() || t.getId().equals(c.getTenantId())){ // System.out.println(t.getName()); tenant = t; tenantName = t.getName(); break; } } //初始化公司 //========================== PowerKey powerKey = PowerTool.createChildSystem(c, tenant); //创建子系统 //========================== //添加初始化得公司 count += initCompanyMapper.addInitCompany(c,powerKey); //添加初始化得租户 count += initDataMapper.initTenant(c.getTenantId(), tenantName); // System.out.println("isok"+count); //通过租户id、公司id 创建相应得业务表 count += initDataMapper.createTable(c.getTenantId(),c.getId()); // System.out.println("isok"+count); //初始化租户费用类型表,从云上行同步 count += initDataMapper.initdataToCompType(costGroupType,id); //初始化公司费用类型表,从云上行同步 count += initDataMapper.initdataToComp(c.getTenantId(), c.getId(), costGroupType,id); //新租户,同步租户主数据 //调用工作流初始化 //表单初始化 // System.out.println("isok"+count); } return count; }catch (Exception e){ // System.out.println("发生异常---initGuideServiceImpl.doInit"); return 0; } } @Override @Transactional @CacheRm(value = {INIT_COMPANY_INFO, INIT_COMPANY_INFO_LIST}, tenant = false, company = false) public int initTenantCompany(InitCompany initCompanyP) { ThreadData.set(BaseConst.OPEN_ID , initCompanyP.getOpenId()); initCompanyP.setDataStart(0); InitCompany initCompany = initCompanyMapper.selectOne(initCompanyP); // 必须先初始化 if(initCompany == null){ InitPowerInfo initPowerInfo = new InitPowerInfo(); initPowerInfo.setTenantId(initCompanyP.getTenantId()); initPowerInfo.setCompanyId(initCompanyP.getCompanyId()); initCompany = addCompanyPowerInfo(initPowerInfo); } // 已经初始化 if(Objects.equals(initCompany.getInitState(), 0)){ return 0; } Integer tenantId = initCompany.getTenantId() , companyId = initCompany.getCompanyId(); ThreadData.set(Keys.TENANT_ID , Objects.toString(tenantId)); ThreadData.set(Keys.COMPANY_ID , Objects.toString(companyId)); ThreadData.set(BaseConst.TENANT_ID , Objects.toString(tenantId)); ThreadData.set(BaseConst.COMPANY_ID , companyId); // 初始化菜单列表 initCompanyMapper.initMenu(tenantId, companyId); // 初始化工作流 workFlowService.initialization(tenantId, companyId); // 初始化租户数据 tenantInitApi.init(initCompany); // 修改初始化状态 initCompany.setInitState(0); initCompanyMapper.updateByPrimaryKeySelective(initCompany); return 0; } /** * 查寻初始化过的公司 */ @Override public List getInitCompany(InitCompany initCompany) { return initCompanyMapper.selectInitCompany(initCompany); } @Override public InitCompany addCompanyPowerInfo(InitPowerInfo initPowerInfo) { initCompanyApi.removeInte(); Integer tenantId = initPowerInfo.getTenantId(); Integer companyId = initPowerInfo.getCompanyId(); Tenant tenant = cscApiService.getTenantAndCompany(tenantId, companyId); Company company = Optional.ofNullable(tenant).map(Tenant::getCompanys).orElse(new ArrayList<>()).stream().findFirst().orElse(null); Assert.empty(tenant, "no this tenant:{0}", Objects.toString(tenantId)); Assert.empty(company, "no this company:{0}", Objects.toString(companyId)); InitCompany initCompany = new InitCompany(); initCompany.setTenantId(initPowerInfo.getTenantId()); initCompany.setCompanyId(initPowerInfo.getCompanyId()); if(initPowerInfo.getAppKey() == null) { // 查询权限 PowerKey powerKey = cscApiService.getSubCompanySystem(initCompany.getCompanyId()); if(powerKey == null) { // 创建权限 powerKey = PowerTool.createChildSystem(company, tenant); } initPowerInfo.setAppKey(powerKey.getAppKey()); initPowerInfo.setSecretKey(powerKey.getSecretKey()); } initCompany.setPowerAppKey(initPowerInfo.getAppKey()); initCompany.setPowerSecretKey(initPowerInfo.getSecretKey()); initCompany.setInitState(2); initCompany.setDbName(getDataSourceRef(initPowerInfo.getCompanyId())); initCompany.setCompanyMasterId(Optional.ofNullable(company.getMasterCompanyId()).map(NumberUtils::createInteger).orElse(0)); initCompany.setCompanyName(company.getName()); initCompany.setTenantName(tenant.getName()); initCompany.initParam(); initCompanyMapper.insertSelective(initCompany); return initCompany; } @Override public String initCompanys(List batchInitCompany, Integer costTypeTempId) { String groupId = StringUtil.getUUID(); batchInitCompany .stream() .map(comp->{ comp.setCostTypeTempId(costTypeTempId); comp.setGroupId(groupId); comp.initParam(); return comp; }) .forEach(batchInitCompanyMapper::insertSelective); return groupId; } @Override public List selectInitCompanys(String groupId) { BatchInitCompany param = new BatchInitCompany(); param.setGroupId(groupId); return batchInitCompanyMapper.select(param); } @Override public int addInitCompany(InitCompany initCompany) { // 初始化菜单列表 initCompanyMapper.initMenu(initCompany.getTenantId(), initCompany.getCompanyId()); initCompany.initParam(); return initCompanyMapper.insertSelective(initCompany); } @Override public List selectBatchCompanys() { BatchInitCompany batchInitCompany = new BatchInitCompany(); batchInitCompany.setDataStart(0); return batchInitCompanyMapper.select(batchInitCompany); } @Override public int updateBatchCompanys(BatchInitCompany batchInitCompany) { return batchInitCompanyMapper.updateByPrimaryKeySelective(batchInitCompany); } public static final int DATA_SOURCE_SIZE = 2; public static final String[] DATA_SOURCE_INFO = {"tenant_1", "tenant_2"}; static final String getDataSourceRef(Integer companyId){ return DATA_SOURCE_INFO[(DATA_SOURCE_SIZE - 1) & companyId]; } }