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

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.filing.earlyEngineering.entity.EarlyEngineering;
import com.jeeplus.modules.filing.earlyEngineering.entity.LegalCompliance;
import com.jeeplus.modules.filing.earlyEngineering.mapper.EarlyEngineeringMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 依法合规MAPPER接口
 * @author qy
 * @version 2021-04-06
 */
@Service
@Transactional(readOnly = true)
public class EarlyEngineeringService extends CrudService<EarlyEngineeringMapper, EarlyEngineering> {


	@Override
	public EarlyEngineering get(EarlyEngineering entity) {
		return super.get(entity);
	}

	@Override
	public List<EarlyEngineering> findList(EarlyEngineering entity) {
		return super.findList(entity);
	}

	@Override
	public Page<EarlyEngineering> findPage(Page<EarlyEngineering> page, EarlyEngineering entity) {
		return super.findPage(page, entity);
	}

	public Page<EarlyEngineering> findAllPage(Page<EarlyEngineering> page, EarlyEngineering entity){
		dataRuleFilter(entity);
		entity.setPage(page);
		page.setList(mapper.findAllList(entity));
		return page;
	}

	public Page<EarlyEngineering> findChildPage(Page<EarlyEngineering> page, EarlyEngineering entity) {
		dataRuleFilter(entity);
		entity.setPage(page);
		page.setList(mapper.findChildList(entity));
		return page;
	}



	@Override
	public void delete(EarlyEngineering entity) {
		super.delete(entity);
	}

	@Override
	@Transactional(readOnly = false)
	public void save(EarlyEngineering entity) {
		//根据projectCode查询每个表的类变是否存在
		//查找初设阶段
		Integer designStage = mapper.findDesignStage(entity);
		if (0 == designStage || null == designStage) {
			entity.preInsert();
			mapper.insertDesign(entity);
		} else {
			entity.preUpdate();
			mapper.updateDesign(entity);
		}
		//查找批概阶段
		Integer reviewStage = mapper.findReviewStage(entity);
		if (0 == reviewStage || null == reviewStage) {
			entity.preInsert();
			mapper.insertReviewStage(entity);
		} else {
			entity.preUpdate();
			mapper.updateReviewStage(entity);
		}
		//查找招标与合同管理
		Integer biddingContract = mapper.findBiddingContract(entity);
		if (0 == biddingContract || null == biddingContract) {
			entity.preInsert();
			mapper.insertBiddingContract(entity);
		} else {
			entity.preUpdate();
			mapper.updateBiddingContract(entity);
		}
		//查找报建审批
		Integer applicationFor = mapper.findApplicationFor(entity);
		if (0 == applicationFor || null == applicationFor) {
			entity.preInsert();
			mapper.insertApplicationFor(entity);
		} else {
			entity.preUpdate();
			mapper.updateApplicationFor(entity);
		}
		//查找依法合规
		Integer legalCompliance = mapper.findLegalCompliance(entity);
		if (0 == legalCompliance || null == legalCompliance) {
			entity.preInsert();
			mapper.insertLegalCompliance(entity);
		} else {
			entity.preUpdate();
			mapper.updateLegalCompliance(entity);
		}
	}

	public Integer findByCode(EarlyEngineering entity) {
		return mapper.findByCode(entity);
	}

	public List<EarlyEngineering> findChildList(EarlyEngineering entity) {
		return mapper.findChildList(entity);
	}
}