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

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.filing.basicInformation.entity.BasicInformation;
import com.jeeplus.modules.filing.basicInformation.entity.BasicInformationChild;
import com.jeeplus.modules.filing.basicInformation.mapper.BasicInformationMapper;
import com.jeeplus.modules.test.onetomany.dialog.entity.TestDataChild12;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.security.auth.login.CredentialNotFoundException;
import java.util.List;

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


	public BasicInformation get(String id) {
		BasicInformation basicInformation = mapper.findOne(id);
		basicInformation.setBasicChildList(mapper.findChildListBy(basicInformation));
		return basicInformation;
	}

	public List<BasicInformation> findList(BasicInformation basicInformation) {
		return super.findList(basicInformation);
	}

	public Page<BasicInformation> findPage(Page<BasicInformation> page, BasicInformation basicInformation) {
		return super.findPage(page, basicInformation);
	}

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

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

	@Transactional(readOnly = false)
	public Boolean saveAll(BasicInformation basicInformation) {
		//通过查询projectCode判断项目是否存在
		BasicInformation byProjectCode = mapper.findByProjectCode(basicInformation.getProjectCode());
		if (null != byProjectCode) {
			return false;
		}
		for (BasicInformationChild b : basicInformation.getBasicChildList()) {
			if (b.getId() == null) {
				continue;
			}
			if (BasicInformationChild.DEL_FLAG_NORMAL.equals(b.getDelFlag())) {
				if (StringUtils.isBlank(b.getId())) {
					b.preInsert();
					BasicInformation byProjectCode1 = mapper.findByProjectCode(b.getProjectCode());
					if (null != byProjectCode1) {
						return false;
					}
					b.setParentId(basicInformation.getProjectCode());
					mapper.insertChild(b);
				} else {
					b.preUpdate();
					mapper.updateChild(b);
				}
			} else {
				mapper.deleteChild(b);
			}
		}
		super.save(basicInformation);
		return true;
	}


	@Transactional(readOnly = false)
	public Boolean updateAll(BasicInformation basicInformation) {
		//通过查询projectCode判断项目是否存在
		for (BasicInformationChild b : basicInformation.getBasicChildList()) {
			if (b.getId() == null) {
				continue;
			}
			if (BasicInformationChild.DEL_FLAG_NORMAL.equals(b.getDelFlag())) {
				if (b.getIsNewRecord()) {
					BasicInformation byProjectCode = mapper.findByProjectCode(b.getProjectCode());
					if (null != byProjectCode) {
						return true;
					}
					b.setParentId(basicInformation.getProjectCode());
					b.preInsert();
					mapper.insertChild(b);
				} else {
					BasicInformation one = mapper.findOne(b.getId());
					BasicInformation 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 (basicInformation.getIsNewRecord()) {
			basicInformation.preInsert();
			mapper.insert(basicInformation);
		} else {
			BasicInformation one = mapper.findOne(basicInformation.getId());
			BasicInformation byProjectCode = mapper.findByProjectCode(basicInformation.getProjectCode());
			if (!one.getProjectCode().equals(basicInformation.getProjectCode()) && null != byProjectCode) {
				return true;
			}
			if (!one.getProjectCode().equals(basicInformation.getProjectCode())) {
				BasicInformationChild basicInformationChild = new BasicInformationChild();
				basicInformationChild.setProjectCode(one.getProjectCode());
				basicInformationChild.setParentId(basicInformation.getProjectCode());
				basicInformation.preUpdate();
				mapper.updateChildParent(basicInformationChild);
			}
			basicInformation.preUpdate();
			mapper.update(basicInformation);
		}
		return false;
	}

	@Transactional(readOnly = false)
	public void delete(BasicInformation basicInformation) {
		super.delete(basicInformation);
	}


	@Transactional(readOnly = false)
	public Boolean updateChild(BasicInformationChild basicInformationChild) {
		BasicInformation byProjectCode = mapper.findByProjectCode(basicInformationChild.getProjectCode());
		BasicInformation 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(BasicInformationChild basicInformationChild) {
		BasicInformation 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(BasicInformation entity) {
		BasicInformation 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<BasicInformationChild> findChildList(BasicInformationChild basicInformation) {
		return mapper.findChildList(basicInformation);
	}

	public List<BasicInformation> findAllChildList(BasicInformation basicInformation) {
		return mapper.findAllChildList(basicInformation);
	}
}