/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.constructionunit.service;

import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.constructionunit.entity.ConstructionUnit;
import com.jeeplus.modules.constructionunit.mapper.ConstructionUnitMapper;

/**
 * ConstructionUnitService
 * @author 邱悦
 * @version 2021-12-29
 */
@Service
@Transactional(readOnly = true)
public class ConstructionUnitService extends CrudService<ConstructionUnitMapper, ConstructionUnit> {

	public ConstructionUnit get(String id) {
		return super.get(id);
	}
	
	public List<ConstructionUnit> findList(ConstructionUnit constructionUnit) {
		return super.findList(constructionUnit);
	}
	
	public Page<ConstructionUnit> findPage(Page<ConstructionUnit> page, ConstructionUnit constructionUnit) {
		return super.findPage(page, constructionUnit);
	}



	@Transactional(readOnly = false)
	public void save(ConstructionUnit entity) {
		super.save(entity);
	}

	@Transactional(readOnly = false)
	public Boolean saveName(ConstructionUnit entity) {
		if (null == mapper.getYearName(entity)) {
			entity.preInsert();
			mapper.insert(entity);
			return true;
		} else {
			return false;
		}
	}

	@Transactional(readOnly = false)
	public Boolean update(ConstructionUnit entity) {
		if (null == mapper.getYearName(entity)) {
			entity.preUpdate();
			mapper.update(entity);
			return true;
		} else {
			ConstructionUnit constructionUnit = mapper.get(entity);
			String unitName = constructionUnit.getUnitName();
			String yearOfEvaluation = constructionUnit.getYearOfEvaluation();
			String unitName1 = entity.getUnitName();
			String yearOfEvaluation1 = entity.getYearOfEvaluation();
			if (unitName.equals(unitName1) && yearOfEvaluation.equals(yearOfEvaluation1)) {
				entity.preUpdate();
				mapper.update(entity);
				return true;
			}else{
				return false;
			}
		}
	}

	@Transactional(readOnly = false)
	public HashMap<String,String> insertList(List<ConstructionUnit> list,String yearOfEvaluation) {
		HashMap<String, String> returnMap = new HashMap<>();
		StringBuilder builder = new StringBuilder();

		List<ConstructionUnit> yearList = mapper.findListByYearName(list,yearOfEvaluation);
		if (null != yearList && yearList.size() > 0) {
			builder.append("插入失败");
			for (ConstructionUnit c :
					yearList) {
				String unitName = c.getUnitName();
				builder.append("单位名称《" + unitName + "》已存在;");
			}
			returnMap.put("insertFlag","false");
		}else{
			builder.append("插入" + list.size() + "条数据");
			returnMap.put("insertFlag","true");
			for (ConstructionUnit c :
					list) {
				c.preInsert();
				c.setYearOfEvaluation(yearOfEvaluation);
			}
			mapper.insertList(list);
		}

		returnMap.put("builder", builder.toString());
		return returnMap;
	}



	@Transactional(readOnly = false)
	public void delete(ConstructionUnit constructionUnit) {
		super.delete(constructionUnit);
	}
	
}