package com.jpsoft.shinestar.modules.workflow.service.impl;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.annotation.Resource;

import com.jpsoft.shinestar.modules.workflow.entity.ProcessActivity;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.jpsoft.shinestar.modules.workflow.dao.ProcessTemplateDAO;
import com.jpsoft.shinestar.modules.workflow.entity.ProcessTemplate;
import com.jpsoft.shinestar.modules.workflow.service.ProcessTemplateService;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.github.pagehelper.PageHelper;

@Transactional
@Component(value="processTemplateService")
public class ProcessTemplateServiceImpl implements ProcessTemplateService {
	@Resource(name="processTemplateDAO")
	private ProcessTemplateDAO processTemplateDAO;

	@Override
	public ProcessTemplate get(String id) {
		// TODO Auto-generated method stub
		return processTemplateDAO.get(id);
	}

	@Override
	public int insert(ProcessTemplate model) {
		// TODO Auto-generated method stub
		//model.setId(UUID.randomUUID().toString());
		
		return processTemplateDAO.insert(model);
	}

	@Override
	public int update(ProcessTemplate model) {
		// TODO Auto-generated method stub
		return processTemplateDAO.update(model);		
	}

	@Override
	public int updateMove(ProcessTemplate model, ProcessTemplate swapProcessTemplate){
		int count = processTemplateDAO.update(model);
		int affectCount=0;
		if(count>0) {
			affectCount = processTemplateDAO.update(swapProcessTemplate);
		}
		return affectCount;
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return processTemplateDAO.delete(id);
	}

	@Override
	public boolean exist(String id) {
		// TODO Auto-generated method stub
		int count = processTemplateDAO.exist(id);
		
		return count > 0 ? true : false;
	}
	
	@Override
	public List<ProcessTemplate> list() {
		// TODO Auto-generated method stub
		return processTemplateDAO.list();
	}


	@Override
	public ProcessTemplate findBySort(int sort){
		return processTemplateDAO.findBySort(sort);
	}

	@Override
	public ProcessTemplate findByProcessSetId(String processSetId){
		return processTemplateDAO.findByProcessSetId(processSetId);
	}
		
	@Override
	public Page<ProcessTemplate> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize,boolean count,List<Sort> sortList) {
        Page<ProcessTemplate> page = PageHelper.startPage(pageNumber,pageSize,count).doSelectPage(()->{
            processTemplateDAO.search(searchParams,sortList);
        });
        
        return page;
	}
}