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

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.filing.summary.entity.Summary;
import com.jeeplus.modules.filing.summary.entity.Summary;
import com.jeeplus.modules.filing.summary.mapper.SummaryMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 基础信息管理Service
 *
 * @author qy
 * @version 2021-03-23
 */
@Service
@Transactional(readOnly = true)
public class SummaryService extends CrudService<SummaryMapper, Summary> {


	public Summary get(String id) {
		Summary summary = mapper.get(id);
		return summary;
	}

	public List<Summary> findList(Summary summary) {
		return super.findList(summary);
	}

	public Page<Summary> findPage(Page<Summary> page, Summary summary) {
		return super.findPage(page, summary);
	}

	public Page<Summary> findOnePage(Page<Summary> page, Summary summary) {
		dataRuleFilter(summary);
		summary.setPage(page);
		page.setList(mapper.findChildList(summary));
		return page;
	}

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

	@Transactional(readOnly = false)
	public Boolean saveAll(Summary entity) {
		Integer byCode = mapper.findByCode(entity);
		Integer designStage = mapper.findDesignStage(entity);
		Integer reviewStage = mapper.findReviewStage(entity);
		Integer biddingContract = mapper.findBiddingContract(entity);
		Integer applicationFor = mapper.findApplicationFor(entity);
		Integer legalCompliance = mapper.findLegalCompliance(entity);
		Integer constructionStage = mapper.findConstructionStage(entity);
		if (null==byCode && 0==byCode) {
			entity.preInsert();
			mapper.insertEarlyStage(entity);
		}else {
			entity.preUpdate();
			mapper.updateEarlyStage(entity);
		}

		if (null==designStage && 0==designStage) {
			entity.preInsert();
			mapper.insertDesign(entity);
		}else {
			entity.preUpdate();
			mapper.updateDesign(entity);
		}

		if (null==reviewStage && 0==reviewStage) {
			entity.preInsert();
			mapper.insertReviewStage(entity);
		}else {
			entity.preUpdate();
			mapper.updateReviewStage(entity);
		}

		if (null==biddingContract && 0==biddingContract) {
			entity.preInsert();
			mapper.insertBiddingContract(entity);
		}else {
			entity.preUpdate();
			mapper.updateBiddingContract(entity);
		}

		if (null==applicationFor && 0==applicationFor) {
			entity.preInsert();
			mapper.insertApplicationFor(entity);
		}else {
			entity.preUpdate();
			mapper.updateApplicationFor(entity);
		}

		if (null==legalCompliance && 0==legalCompliance) {
			entity.preInsert();
			mapper.insertLegalCompliance(entity);
		}else {
			entity.preUpdate();
			mapper.updateLegalCompliance(entity);
		}

		if (null==constructionStage && 0==constructionStage) {
			entity.preInsert();
			mapper.insertConstructionStage(entity);
		}else {
			entity.preUpdate();
			mapper.updateConstructionStage(entity);
		}

		return true;
	}


	@Transactional(readOnly = false)
	public Boolean updateAll(Summary summary) {
		//通过查询projectCode判断项目是否存在
		for (Summary b : summary.getChildList()) {
			if (b.getId() == null) {
				continue;
			}
			if (Summary.DEL_FLAG_NORMAL.equals(b.getDelFlag())) {
				if (b.getIsNewRecord()) {
					Summary byProjectCode = mapper.findByProjectCode(b.getProjectCode());
					if (null != byProjectCode) {
						return true;
					}
					b.setParentId(summary.getProjectCode());
					b.preInsert();
					mapper.insertChild(b);
				} else {
					Summary one = mapper.findOne(b.getId());
					Summary byProjectCode = mapper.findByProjectCode(b.getProjectCode());
					if (!one.getProjectCode().equals(b.getProjectCode()) && null != byProjectCode) {
						return true;
					}
					b.preUpdate();
					mapper.updateChild(b);
				}
			} else {
				mapper.deleteChild(b);
			}
		}
		if (summary.getIsNewRecord()) {
			summary.preInsert();
			mapper.insert(summary);
		} else {
			Summary one = mapper.findOne(summary.getId());
			Summary byProjectCode = mapper.findByProjectCode(summary.getProjectCode());
			if (!one.getProjectCode().equals(summary.getProjectCode()) && null != byProjectCode) {
				return true;
			}
			if (!one.getProjectCode().equals(summary.getProjectCode())) {
				Summary basicInformationChild = new Summary();
				basicInformationChild.setProjectCode(one.getProjectCode());
				basicInformationChild.setParentId(summary.getProjectCode());
				summary.preUpdate();
				mapper.updateChildParent(basicInformationChild);
			}
			summary.preUpdate();
			mapper.update(summary);
		}
		return false;
	}

	@Transactional(readOnly = false)
	public void delete(Summary summary) {
		super.delete(summary);
	}


	@Transactional(readOnly = false)
	public Boolean updateChild(Summary basicInformationChild) {
		Summary byProjectCode = mapper.findByProjectCode(basicInformationChild.getProjectCode());
		Summary one = mapper.findOne(basicInformationChild.getProjectCode());
		if (!one.getProjectCode().equals(basicInformationChild.getProjectCode()) && null != byProjectCode) {
			return true;
		}
		mapper.updateChild(basicInformationChild);
		return false;
	}

	@Transactional(readOnly = false)
	public void insertChild(Summary basicInformationChild) {
		Summary byProjectCode = mapper.findByProjectCode(basicInformationChild.getProjectCode());
		if (null == byProjectCode) {
			basicInformationChild.preInsert();
			mapper.insertChild(basicInformationChild);
		} else {
			basicInformationChild.preUpdate();
			basicInformationChild.setId(byProjectCode.getId());
			mapper.updateChild(basicInformationChild);
		}
	}

	@Override
	@Transactional(readOnly = false)
	public void save(Summary entity) {
		Summary byProjectCode = mapper.findByProjectCode(entity.getProjectCode());
		if (null == byProjectCode) {
			entity.preInsert();
			mapper.insert(entity);
		} else {
			entity.setId(byProjectCode.getId());
			entity.preUpdate();
			mapper.update(entity);
		}
	}

	public List<String> findAllId() {
		return mapper.findALlId();
	}

	public List<Summary> findChildList(Summary summary) {
		return mapper.findChildList(summary);
	}

	public List<Summary> findAllChildList(Summary summary) {
		return mapper.findAllChildList(summary);
	}
}