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

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.common.utils.MyBeanUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workactivity.dao.ActivityDao;
import com.jeeplus.modules.workactivity.dao.WorkActivityProcessDao;
import com.jeeplus.modules.workactivity.entity.Activity;
import com.jeeplus.modules.workprojectnotify.entity.WorkProjectNotify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 工作流附加类Service
 * @author 杨帆
 * @version 2018-01-24
 */
@Service
@Transactional(readOnly = true)
public class ActivityService extends CrudService<ActivityDao, Activity> {
	@Autowired
	private WorkActivityProcessDao workActivityProcessDao;

	public Activity get(String id) {
		Activity activity = super.get(id);
		return activity;
	}

	public List<Activity> getByProcessInstanceId(String processInstanceId) {
		List<Activity> activitys = dao.getByProcessInstanceId(processInstanceId);
		return activitys;
	}
	public WorkProjectNotify getByFlagAndTaskDefKey(List<Activity> activities,WorkProjectNotify workProjectNotify, String taskDefKey, String flag,String count,User user) {
		try {
			Activity activity = new Activity();
			if (taskDefKey.contains("audit")) {
				if (activities.size() == 1 || count.equals("1")){
					activity = activities.get(0);
				}else if (activities.size()==Integer.parseInt(count)){
					activity = activities.get(activities.size()-1);
					String returnBack = activity.getReturnBack();
					if (returnBack.equals("1")){
						activity = activities.get(Integer.parseInt(count)-2);
					}else if (returnBack.equals("2")){
						activity = activities.get(0);
					}
				}else {
					activity = activities.get(Integer.parseInt(count)-1);
					String returnBack = activity.getReturnBack();
					if (returnBack.equals("1")){
						activity = activities.get(Integer.parseInt(count)-2);
					}else if (returnBack.equals("2")){
						activity = activities.get(0);
					}
				}
			}else if(!"yes".equals(flag)){
				workProjectNotify.setRemarks("重新申请");
				activity = new Activity();
			}else {
				activity = activities.get(Integer.parseInt(count));
			}
			if (activity!=null && StringUtils.isNotBlank(activity.getId())){
				workProjectNotify.setUser(activity.getUser());
				workProjectNotify.setNotifyRole(activity.getRole().getEnname());
			}else {
				workProjectNotify.setUser(user);
			}
		}catch (Exception e){
			logger.info("Exception e:"+e);
		}

		return workProjectNotify;
	}

	public List<WorkProjectNotify> getByFlagAndTaskDefKeyList(List<Activity> activities,WorkProjectNotify workProjectNotify, String taskDefKey, String flag,String count,User user,String officeId,String type) {
		List<WorkProjectNotify> list = new ArrayList<>();
		try {
			Activity activity = new Activity();
			if (taskDefKey.equals("modifyApply")) {
				activity = activities.get(0);
				if (activities.size() == 1 || count.equals("1")){
					activity = activities.get(0);
				}else if (activities.size()==Integer.parseInt(count)){
					activity = activities.get(activities.size()-1);
					String returnBack = activity.getReturnBack();
					if (returnBack.equals("1")){
						activity = activities.get(0);
					}else if (returnBack.equals("2")){
						activity = activities.get(0);
					}
				}else {
					activity = activities.get(0);
					String returnBack = activity.getReturnBack();
					if (returnBack.equals("1")){
						activity = activities.get(0);
					}else if (returnBack.equals("2")){
						activity = activities.get(0);
					}
				}
			}else if(!"yes".equals(flag)){
				workProjectNotify.setRemarks("重新申请");
				activity = new Activity();
			}else {
				activity = activities.get(Integer.parseInt(count));
			}
			if (activity!=null && StringUtils.isNotBlank(activity.getId())){
				if (activity.getUser()!=null && StringUtils.isNotBlank(activity.getUser().getId())){
					workProjectNotify.setUser(activity.getUser());
					list.add(workProjectNotify);
				}else {
					List<User> users = UserUtils.getByRoleActivityEnnames(activity.getRole().getEnname(),officeId,type,user.getCreateBy());
					for (User user1:users){
						WorkProjectNotify projectNotify = new WorkProjectNotify();
						MyBeanUtils.copyBeanNotNull2Bean(workProjectNotify, projectNotify);
						projectNotify.setUser(user1);
						list.add(projectNotify);
					}
				}

			}else {
				workProjectNotify.setUser(user);
				list.add(workProjectNotify);
			}
		}catch (Exception e){
			logger.info("Exception e:"+e);
			list.add(workProjectNotify);
		}

		return list;
	}
	public List<WorkProjectNotify> getByFlagAndTaskDefKeyListSpecial(List<Activity> activities,WorkProjectNotify workProjectNotify, String taskDefKey, String flag,String count,User user,String officeId,String type) {
		List<WorkProjectNotify> list = new ArrayList<>();
		try {
			Activity activity = new Activity();
			if (taskDefKey.equals("modifyApply")) {
				activity = activities.get(0);
				if (activities.size() == 1 || count.equals("1")){
					activity = activities.get(0);
				}else if (activities.size()==Integer.parseInt(count)){
					activity = activities.get(activities.size()-1);
					String returnBack = activity.getReturnBack();
					if (returnBack.equals("1")){
						activity = activities.get(0);
					}else if (returnBack.equals("2")){
						activity = activities.get(0);
					}
				}else {
					activity = activities.get(0);
					String returnBack = activity.getReturnBack();
					if (returnBack.equals("1")){
						activity = activities.get(0);
					}else if (returnBack.equals("2")){
						activity = activities.get(0);
					}
				}
			}else if("2".equals(flag)){
				workProjectNotify.setRemarks("重新申请");
				activity = new Activity();
			}else {
				activity = activities.get(Integer.parseInt(count));
			}
			if (activity!=null && StringUtils.isNotBlank(activity.getId())){
				if (activity.getUser()!=null && StringUtils.isNotBlank(activity.getUser().getId())){
					workProjectNotify.setUser(activity.getUser());
					list.add(workProjectNotify);
				}else {
					List<User> users = UserUtils.getByRoleActivityEnnames(activity.getRole().getEnname(),officeId,type,user.getCreateBy());
					for (User user1:users){
						WorkProjectNotify projectNotify = new WorkProjectNotify();
						MyBeanUtils.copyBeanNotNull2Bean(workProjectNotify, projectNotify);
						projectNotify.setUser(user1);
						list.add(projectNotify);
					}
				}

			}else {
				workProjectNotify.setUser(user);
				list.add(workProjectNotify);
			}
		}catch (Exception e){
			logger.info("Exception e:"+e);
			list.add(workProjectNotify);
		}

		return list;
	}
	
	public List<Activity> findList(Activity activity) {
		return super.findList(activity);
	}

	public List<Activity> findByActivityMenuId(String activityMenuId) {
		return dao.findByActivityMenuId(activityMenuId);
	}

	public List<Activity> groupByActivityMenu(String parentId,String companyId) {
		return dao.groupByActivityMenu(parentId,companyId);
	}

	public Page<Activity> findPage(Page<Activity> page, Activity activity) {
		return super.findPage(page, activity);
	}
	
	@Transactional(readOnly = false)
	public void save(Activity activity) {
		super.save(activity);
	}

	@Transactional(readOnly = false)
	public void deleteToDelflag(String activityMenuId) {
		dao.deleteToDelflag(activityMenuId);
	}

	@Transactional(readOnly = false)
	public void delete(Activity activity) {
		super.delete(activity);
	}
	
	public Activity findUniqueByProperty(String propertyName, Object value){
		return super.findUniqueByProperty(propertyName,value);
	}
	
	
}