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

import java.util.ArrayList;
import java.util.List;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.workactivity.entity.Activity;
import com.jeeplus.modules.workactivity.entity.WorkActivityProcessUser;
import com.jeeplus.modules.workactivitymenu.entity.WorkActivityMenu;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.workactivity.entity.WorkActivityProcess;
import com.jeeplus.modules.workactivity.dao.WorkActivityProcessDao;

/**
 * 工作流审批信息Service
 * @author 杨帆
 * @version 2018-01-30
 */
@Service
@Transactional(readOnly = true)
public class WorkActivityProcessService extends CrudService<WorkActivityProcessDao, WorkActivityProcess> {

	public WorkActivityProcess get(String id) {
		return super.get(id);
	}
	
	public List<WorkActivityProcess> findByKeyAndCount(WorkActivityProcess workActivityProcess) {
		return dao.findByKeyAndCount(workActivityProcess);
	}
	public List<WorkActivityProcess> findList(WorkActivityProcess workActivityProcess) {
		return super.findList(workActivityProcess);
	}
	public List<WorkActivityProcess> findByProcInsId(WorkActivityProcess workActivityProcess) {
		return dao.findByProcInsId(workActivityProcess);
	}
	public List<WorkActivityProcess> findByProcInsIdAudit(WorkActivityProcess workActivityProcess) {
		return dao.findByProcInsIdAudit(workActivityProcess);
	}
	public List<WorkActivityProcess> findByProcInsIdAndIsApproval(WorkActivityProcess workActivityProcess) {
		return dao.findByProcInsIdAndIsApproval(workActivityProcess);
	}

	public Page<WorkActivityProcess> findPage(Page<WorkActivityProcess> page, WorkActivityProcess workActivityProcess) {
		return super.findPage(page, workActivityProcess);
	}
	
	@Transactional(readOnly = false)
	public void save(WorkActivityProcess workActivityProcess) {
		super.save(workActivityProcess);
	}
	@Transactional(readOnly = false)
	public void insert(WorkActivityProcess workActivityProcess) {
		workActivityProcess.preInsert();
		dao.insert(workActivityProcess);
	}

	@Transactional(readOnly = false)
	public void insertAudits(WorkActivityProcess workActivityProcess) {
		dao.insertAudits(workActivityProcess);
	}
	@Transactional(readOnly = false)
	public void insertAuditsByType(List<User> users,String processId,int count,int type) {
		WorkActivityProcess workActivityProcess = new WorkActivityProcess();
		List<WorkActivityProcessUser> processUserList = new ArrayList<>();
		if(users!=null&&users.size()!=0){
			for (User u :users){
				WorkActivityProcessUser process = new WorkActivityProcessUser();
				process.setCount(count);
				process.setUserId(u.getId());
				process.setProcessId(processId);
				process.setType(type);
				processUserList.add(process);
			}
			workActivityProcess.setProcessUserList(processUserList);
			if(count>1 && type==1) {
				dao.deleteAudits(processId,count);
			}
			dao.insertAudits(workActivityProcess);
		}

	}
	@Transactional(readOnly = false)
	public void delFlag(WorkActivityProcess workActivityProcess) {
		dao.delFlag(workActivityProcess);
	}
	@Transactional(readOnly = false)
	public void updateProcessInstanceId(String processInstanceId,String processId) {
		dao.updateProcessInstanceId(processInstanceId,processId);
	}
	@Transactional(readOnly = false)
	public void deleteProcessInstanceId(String processId) {
		dao.deleteProcessInstanceId(processId);
	}
	@Transactional(readOnly = false)
	public void deleteProcessInstanceById(String processId) {
		dao.deleteProcessInstanceById(processId);
	}
	@Transactional(readOnly = false)
	public void updateProcess(WorkActivityProcess workActivityProcess, WorkActivityMenu workActivityMenu,int key, String taskCount, String processInstanceId, String taskDefKey,String audit, String flag, String comment, List<Activity> activities) {
		if (workActivityProcess.getCount()!=0){
			workActivityProcess.setId("");
		}else {
			WorkActivityProcess workActivityProcess1 = new WorkActivityProcess();
			workActivityProcess1.setIsApproval("0");
			workActivityProcess1.setCount(0);
			workActivityProcess1.setProcessInstanceId(processInstanceId);
			this.deleteByAudit(workActivityProcess1);
		}

		workActivityProcess.setRemarks(comment);
		workActivityProcess.setProcessInstanceId(processInstanceId);
		workActivityProcess.setProcessKey(workActivityMenu.getProcessType());
		if (workActivityProcess.getCount()!=0) {
			workActivityProcess.setCount(Integer.parseInt(taskCount));
		}
		workActivityProcess.setIsApproval("yes".equals(flag)?"1":"2");
		if (workActivityProcess!=null && StringUtils.isNotBlank(workActivityProcess.getIsApproval())){

			WorkActivityProcess w = new WorkActivityProcess();
			w.setProcessInstanceId(processInstanceId);
			w.setIsApproval("0");
			w.setDelFlag("0");
			this.delFlag(w);
			WorkActivityProcess process = w;
			if((taskDefKey.equals("modifyApply") && key==1) || (taskDefKey.equals(audit)&& key==0)) {
				if ("yes".equals(flag)){
					String returnBack = "";
					for (Activity activity : activities) {
						if (activity.getCount() == Integer.parseInt(taskCount)) {
							returnBack = activity.getReturnBack();
						}
					}
					if (!returnBack.equals("0")) {
						if (returnBack.equals("1")) {
							if (!taskCount.equals("1")) {
								process.setCount(1);
							}else {
								process.setCount(1);
							}
						} else if (returnBack.equals("2")) {
							process.setCount(1);
						} else{
							process.setCount(1);
						}
					}
					workActivityProcess.setId("");
				}else {
					process.setCount(11);
				}
			}else {
				if ("yes".equals(flag)) {
					process.setCount(Integer.parseInt(taskCount) + 1);
				} else {
					String returnBack = "";
					for (Activity activity : activities) {
						if (activity.getCount() == Integer.parseInt(taskCount)) {
							returnBack = activity.getReturnBack();
						}
					}
					if (!returnBack.equals("0")) {
						if (returnBack.equals("1")) {
							if (!taskCount.equals("1")) {
								process.setCount(1);
							}else {
								process.setCount(1);
							}
						} else if (returnBack.equals("2")) {
							process.setCount(1);
						} else{
							process.setCount(1);
						}
						WorkActivityProcess workActivityProcess1 = new WorkActivityProcess();
						workActivityProcess1.setCount(0);
						workActivityProcess1.setProcessInstanceId(processInstanceId);
						workActivityProcess1.setProcessKey(workActivityMenu.getProcessType());
						workActivityProcess1.setIsApproval("0");
						workActivityProcess1.setActivityTask("modifyApply");
						this.save(workActivityProcess1);
					}
				}
			}
			process.setDelFlag("1");
			this.delFlag(process);
			if (workActivityProcess.getCount()==0){
				workActivityProcess.setDelFlag("0");
			}
			this.save(workActivityProcess);
		}
	}
	@Transactional(readOnly = false)
	public void updateProcessSpecial(WorkActivityProcess workActivityProcess, WorkActivityMenu workActivityMenu,int key, String taskCount, String processInstanceId, String taskDefKey,String audit, String flag, String comment, List<Activity> activities) {
		if (workActivityProcess.getCount()!=0){
			workActivityProcess.setId("");
		}else {
			WorkActivityProcess workActivityProcess1 = new WorkActivityProcess();
			workActivityProcess1.setIsApproval("0");
			workActivityProcess1.setCount(0);
			workActivityProcess1.setProcessInstanceId(processInstanceId);
			this.deleteByAudit(workActivityProcess1);
		}

		workActivityProcess.setRemarks(comment);
		workActivityProcess.setProcessInstanceId(processInstanceId);
		workActivityProcess.setProcessKey(workActivityMenu.getProcessType());
		if (workActivityProcess.getCount()!=0) {
			workActivityProcess.setCount(Integer.parseInt(taskCount));
		}
		//1 -3 通过 2驳回
		workActivityProcess.setIsApproval("2".equals(flag)?"2":"1");
		if (workActivityProcess!=null && StringUtils.isNotBlank(workActivityProcess.getIsApproval())){

			WorkActivityProcess w = new WorkActivityProcess();
			w.setProcessInstanceId(processInstanceId);
			w.setIsApproval("0");
			w.setDelFlag("0");
			this.delFlag(w);
			WorkActivityProcess process = w;
			if((taskDefKey.equals("modifyApply") && key==1) || (taskDefKey.equals(audit)&& key==0)) {
				if ("1".equals(flag) || "3".equals(flag)){
					String returnBack = "";
					for (Activity activity : activities) {
						if (activity.getCount() == Integer.parseInt(taskCount)) {
							returnBack = activity.getReturnBack();
						}
					}
					if (!returnBack.equals("0")) {
						if (returnBack.equals("1")) {
							if (!taskCount.equals("1")) {
								process.setCount(1);
							}else {
								process.setCount(1);
							}
						} else if (returnBack.equals("2")) {
							process.setCount(1);
						} else{
							process.setCount(1);
						}
					}
					workActivityProcess.setId("");
				}else {
					process.setCount(11);
				}
			}else {
				if ("1".equals(flag) || "3".equals(flag)) {
					process.setCount(Integer.parseInt(taskCount) + 1);
				} else {
					String returnBack = "";
					for (Activity activity : activities) {
						if (activity.getCount() == Integer.parseInt(taskCount)) {
							returnBack = activity.getReturnBack();
						}
					}
					if (!returnBack.equals("0")) {
						if (returnBack.equals("1")) {
							if (!taskCount.equals("1")) {
								process.setCount(1);
							}else {
								process.setCount(1);
							}
						} else if (returnBack.equals("2")) {
							process.setCount(1);
						} else{
							process.setCount(1);
						}
						WorkActivityProcess workActivityProcess1 = new WorkActivityProcess();
						workActivityProcess1.setCount(0);
						workActivityProcess1.setProcessInstanceId(processInstanceId);
						workActivityProcess1.setProcessKey(workActivityMenu.getProcessType());
						workActivityProcess1.setIsApproval("0");
						workActivityProcess1.setActivityTask("modifyApply");
						this.save(workActivityProcess1);
					}
				}
			}
			process.setDelFlag("1");
			this.delFlag(process);
			if (workActivityProcess.getCount()==0){
				workActivityProcess.setDelFlag("0");
			}
			this.save(workActivityProcess);
		}
	}
	@Transactional(readOnly = false)
	public void deleteByAudit(WorkActivityProcess workActivityProcess) {
		dao.deleteByAudit(workActivityProcess);
	}
	@Transactional(readOnly = false)
	public void deleteProcessIdAuditUsers(String processInstanceId) {
		dao.deleteProcessIdAuditUsers(processInstanceId);
	}

	@Transactional(readOnly = false)
	public void saveList(List<Activity> list,String processInstanceId) {
		for (Activity activity:list){
			WorkActivityProcess workActivityProcess = new WorkActivityProcess();
			workActivityProcess.setProcessKey(activity.getProcessKey());
			workActivityProcess.setCount(activity.getCount());
			workActivityProcess.setProcessInstanceId(processInstanceId);
			workActivityProcess.setIsApproval("0");
			workActivityProcess.setActivity(activity);
			workActivityProcess.setActivityTask(activity.getActivityTask());
			super.save(workActivityProcess);
		}
	}

	@Transactional(readOnly = false)
	public void delete(WorkActivityProcess workActivityProcess) {
		super.delete(workActivityProcess);
	}
	
	public void deleteyProcess(String id){
		dao.deleteyProcess(id);
	}

	@Transactional(readOnly = false)
	public void updateProcessRegular(WorkActivityProcess workActivityProcess, WorkActivityMenu workActivityMenu,int key, String taskCount, String processInstanceId, String taskDefKey,String audit, String flag, String comment, List<Activity> activities) {
		if (workActivityProcess.getCount()!=0){
			workActivityProcess.setId("");
		}else {
			WorkActivityProcess workActivityProcess1 = new WorkActivityProcess();
			workActivityProcess1.setIsApproval("0");
			workActivityProcess1.setCount(0);
			workActivityProcess1.setProcessInstanceId(processInstanceId);
			this.deleteByAudit(workActivityProcess1);
		}

		workActivityProcess.setRemarks(comment);
		workActivityProcess.setProcessInstanceId(processInstanceId);
		workActivityProcess.setProcessKey(workActivityMenu.getProcessType());
		if (workActivityProcess.getCount()!=0) {
			workActivityProcess.setCount(Integer.parseInt(taskCount));
		}
		workActivityProcess.setIsApproval("yes".equals(flag)?"1":"2");
		if (workActivityProcess!=null && StringUtils.isNotBlank(workActivityProcess.getIsApproval())){

			WorkActivityProcess w = new WorkActivityProcess();
			w.setProcessInstanceId(processInstanceId);
			w.setIsApproval("0");
			w.setDelFlag("0");
			this.delFlag(w);
			WorkActivityProcess process = w;
			if((taskDefKey.equals("modifyApply") && key==1) || (taskDefKey.equals(audit)&& key==0)) {
				if ("yes".equals(flag)){
					String returnBack = "";
					for (Activity activity : activities) {
						if (activity.getCount() == Integer.parseInt(taskCount)) {
							returnBack = activity.getReturnBack();
						}
					}
					if (!returnBack.equals("0")) {
						if (returnBack.equals("1")) {
							if (!taskCount.equals("1")) {
								process.setCount(1);
							}else {
								process.setCount(1);
							}
						} else if (returnBack.equals("2")) {
							process.setCount(1);
						} else{
							process.setCount(1);
						}
					}
					workActivityProcess.setId("");
				}else {
					process.setCount(11);
				}
			}else {
				if ("yes".equals(flag)) {
					process.setCount(Integer.parseInt(taskCount) + 1);
				} else {
					String returnBack = "";
					for (Activity activity : activities) {
						if (activity.getCount() == Integer.parseInt(taskCount)) {
							returnBack = activity.getReturnBack();
						}
					}
					if (!returnBack.equals("0")) {
						if (returnBack.equals("1")) {
							if (!taskCount.equals("1")) {
								process.setCount(1);
							}else {
								process.setCount(1);
							}
						} else if (returnBack.equals("2")) {
							process.setCount(1);
						} else{
							process.setCount(1);
						}

					}
				}
			}
			process.setDelFlag("1");
			this.delFlag(process);
			if (workActivityProcess.getCount()==0){
				workActivityProcess.setDelFlag("0");
			}
			this.save(workActivityProcess);
		}
	}

	@Transactional(readOnly = false)
	public void updateType(String processInstanceId ,String type) {
		dao.updateType(processInstanceId,type);
	}
}