/**
 * 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.ModuleBlockVersion;
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.ModuleBlockVersionMapper;
import com.jeeplus.modules.sg.managementcenter.moduleacquisition.util.ModuleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


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

	@Autowired
	private ModuleListingService moduleListingService;

	@Autowired
	private ModuleBlockVersionMapper moduleBlockVersionMapper;


	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, ModuleBlockVersion moduleBlockVersion){
		moduleBlockVersion.preInsert();
		String version = moduleBlockVersion.getId();
		String versionName = moduleBlockVersion.getVersionName();
		moduleBlockVersionMapper.insert(moduleBlockVersion);
		for (ModuleBlock moduleBlock:blocks){
			moduleBlock.preInsert();
			moduleBlock.setMdVersion(version);
			moduleBlock.setMdVersionName(versionName);
		}
		List<ModuleListing> moduleListings = ModuleUtil.getModuleListing(listings, blocks);
		moduleBlockMapper.insertList(blocks);
		moduleListingService.savaList(moduleListings);
	}


}