/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.sg.managementcenter.moduleacquisition.service;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sg.managementcenter.moduleacquisition.entity.ModuleBlock;
import com.jeeplus.modules.sg.managementcenter.moduleacquisition.entity.ModuleListing;
import com.jeeplus.modules.sg.managementcenter.moduleacquisition.entity.ModuleVersion;
import com.jeeplus.modules.sg.managementcenter.moduleacquisition.mapper.ModuleBlockMapper;
import com.jeeplus.modules.sg.managementcenter.moduleacquisition.mapper.ModuleListingMapper;
import com.jeeplus.modules.sg.managementcenter.moduleacquisition.util.ModuleUtil;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
@Transactional(readOnly = true)
public class ModuleBlockService extends CrudService<ModuleBlockMapper,ModuleBlock> {
	@Autowired
	private ModuleBlockMapper moduleBlockMapper;

	@Autowired
	private ModuleListingService moduleListingService;


	public ModuleBlock get(String id) {
		return super.get(id);
	}
	
	public List<ModuleBlock> findList(ModuleBlock moduleBlock) {
		return super.findList(moduleBlock);
	}

	public Page<ModuleBlock> findPage(Page<ModuleBlock> page, ModuleBlock moduleBlock) {
		return super.findPage(page, moduleBlock);
	}
	
	@Transactional(readOnly = false)
	public void save(ModuleBlock moduleBlock) {
		super.save(moduleBlock);
	}

	//文件导入
	@Transactional(readOnly = false)
	public void savaList(List<ModuleBlock> blocks, List<ModuleListing> listings,String version){
		Integer integer = 1;//版本号默认为1
		if (null!=version&&!version.equals("")){
			integer = new Integer(version)+1;
		}
		for (ModuleBlock moduleBlock:blocks){
			moduleBlock.preInsert();
			moduleBlock.setMdVersion(String.valueOf(integer));
		}
		List<ModuleListing> moduleListings = ModuleUtil.getModuleListing(listings, blocks);
		moduleBlockMapper.insertList(blocks);
		moduleListingService.savaList(moduleListings);
	}

	/*
		获得最新版本号
	 */
	public String findVersion(){
		return moduleBlockMapper.findMaxVersion();
	}

	/*
		获得所有版本号
	 */
	public Page<ModuleVersion> findPage(Page<ModuleVersion> page, ModuleVersion moduleVersion) {
		List<ModuleVersion> allVersion = moduleBlockMapper.findAllVersion(moduleVersion);
		for (ModuleVersion moduleVersion1:allVersion){
			moduleVersion1.setVersionName("版本"+moduleVersion1.getVersion());
		}
		dataRuleFilter(moduleVersion);
		moduleVersion.setPage(page);
		page.setList(allVersion);
		return page;
	}
}