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

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
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.common.utils.StringUtils;
import com.jeeplus.modules.extension.entity.TaskDefExtension;
import com.jeeplus.modules.extension.mapper.TaskDefExtensionMapper;
import com.jeeplus.modules.extension.entity.FlowButton;
import com.jeeplus.modules.extension.mapper.FlowButtonMapper;

/**
 * 工作流扩展Service
 * @author liugf
 * @version 2019-09-26
 */
@Service
@Transactional(readOnly = true)
public class TaskDefExtensionService extends CrudService<TaskDefExtensionMapper, TaskDefExtension> {

	@Autowired
	private FlowButtonMapper flowButtonMapper;

	public TaskDefExtension get(String id) {
		TaskDefExtension taskDefExtension = super.get(id);
		taskDefExtension.setFlowButtonList(flowButtonMapper.findList(new FlowButton(taskDefExtension)));
		return taskDefExtension;
	}

	public List<TaskDefExtension> findList(TaskDefExtension taskDefExtension) {
		return super.findList(taskDefExtension);
	}

	public Page<TaskDefExtension> findPage(Page<TaskDefExtension> page, TaskDefExtension taskDefExtension) {
		return super.findPage(page, taskDefExtension);
	}

	@Transactional(readOnly = false)
	public void save(TaskDefExtension taskDefExtension) {
		super.save(taskDefExtension);
		for (FlowButton flowButton : taskDefExtension.getFlowButtonList()){
			if (flowButton.getId() == null){
				continue;
			}
			if (FlowButton.DEL_FLAG_NORMAL.equals(flowButton.getDelFlag())){
				if (StringUtils.isBlank(flowButton.getId())){
					flowButton.setTaskDef(taskDefExtension);
					flowButton.preInsert();
					flowButtonMapper.insert(flowButton);
				}else{
					flowButton.preUpdate();
					flowButtonMapper.update(flowButton);
				}
			}else{
				flowButtonMapper.delete(flowButton);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(TaskDefExtension taskDefExtension) {
		super.delete(taskDefExtension);
		flowButtonMapper.delete(new FlowButton(taskDefExtension));
	}
	@Transactional(readOnly = false)
	public void deleteByProcessDefId(String processDefId) {
		TaskDefExtension taskDefExtension = new TaskDefExtension();
		taskDefExtension.setProcessDefId(processDefId);
		List<TaskDefExtension> list = mapper.findList(taskDefExtension);
		for(TaskDefExtension taskDefExtension1: list){
			super.delete(taskDefExtension1);
			flowButtonMapper.delete(new FlowButton(taskDefExtension1));
		}

	}

}
