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

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

import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.modules.act.service.ActivityXmlService;
import com.jeeplus.modules.sys.dao.RoleDao;
import com.jeeplus.modules.sys.dao.SysRoleActivityDao;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.Role;
import com.jeeplus.modules.sys.entity.SysRoleActivity;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workactivity.entity.Activity;
import com.jeeplus.modules.workactivity.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.common.service.TreeService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.workactivitymenu.entity.WorkActivityMenu;
import com.jeeplus.modules.workactivitymenu.dao.WorkActivityMenuDao;

/**
 * 流程表Service
 * @author yangfan
 * @version 2018-01-17
 */
@Service
@Transactional(readOnly = true)
public class WorkActivityMenuService extends TreeService<WorkActivityMenuDao, WorkActivityMenu> {

	@Autowired
	private SysRoleActivityDao roleActivityDao;
	@Autowired
	private ActivityService activityService;
	@Autowired
	private ActivityXmlService activityXmlService;

	public WorkActivityMenu get(String id) {
		WorkActivityMenu workActivityMenu = super.get(id);
		List<Activity> list = activityService.findByActivityMenuId(id);
		for (Activity activity:list){
			if (activity.getUser()!=null && StringUtils.isNotBlank(activity.getUser().getId())){
				User user;
				if (activity.getUser().getId().equals("1")){
					user = UserUtils.getUser();
				}else {
					user = UserUtils.get(activity.getUser().getId());
				}

				activity.setUser(user);
			}
			if( activity.getRole()!=null && StringUtils.isNotBlank(activity.getRole().getEnname())){
				SysRoleActivity role = this.getByEnname(activity.getRole(),workActivityMenu.getCompanyId(),workActivityMenu.getBranchCompanyId());
				activity.setRole(role);
			}
		}
		workActivityMenu.setActivities(list);

		return workActivityMenu;
	}
	
	public List<WorkActivityMenu> findList(WorkActivityMenu workActivityMenu) {
		if (StringUtils.isNotBlank(workActivityMenu.getParentIds())){
			workActivityMenu.setParentIds(","+workActivityMenu.getParentIds()+",");
		}
		return super.findList(workActivityMenu);
	}
	public List<WorkActivityMenu> findTreeData(WorkActivityMenu workActivityMenu) {
		if (StringUtils.isNotBlank(workActivityMenu.getParentIds())){
			workActivityMenu.setParentIds(","+workActivityMenu.getParentIds()+",");
		}
		return dao.findTreeData(workActivityMenu);
	}
	public List<WorkActivityMenu> findByList(WorkActivityMenu workActivityMenu) {
		return dao.findByList(workActivityMenu);
	}

	public WorkActivityMenu findByParentAndOffice(String parentId,Office office) {
		try {
			WorkActivityMenu activityMenu = new WorkActivityMenu();
			activityMenu.setParent(new WorkActivityMenu(parentId));
			activityMenu.setOfficeId(office.getId());
			List<WorkActivityMenu> list = dao.findByParentAndOffice(activityMenu);
			if (list!=null && list.size()!=0){
				activityMenu = list.get(0);
			}else {
				String[] parents = office.getParentIds().split(",");
				for (int i =parents.length-1 ; i > 0 ; i--){
					String parentsId = parents[i];
					activityMenu.setOfficeId(parentsId);
					List<WorkActivityMenu> lists  = dao.findByParentAndOffice(activityMenu);
					if (lists!=null && lists.size()!=0){
						activityMenu = lists.get(0);
						break;
					}
				}
			}
			if (activityMenu!=null && StringUtils.isNotBlank(activityMenu.getId())) {
				activityMenu = get(activityMenu.getId());
				return activityMenu;
			}else{
				return new WorkActivityMenu();
			}
		}catch (Exception e){
			return new WorkActivityMenu();
		}
	}

	public List<WorkActivityMenu> findByParent(String parentId,Office office) {
		try {
			WorkActivityMenu activityMenu = new WorkActivityMenu();
			activityMenu.setParent(new WorkActivityMenu(parentId));
			activityMenu.setOfficeId(office.getId());
			List<WorkActivityMenu> list =new ArrayList<WorkActivityMenu>();
			list.addAll(dao.findByParentAndOffice(activityMenu));
			String[] parents = office.getParentIds().split(",");
			for (int i =parents.length-1 ; i > 0 ; i--){
				String parentsId = parents[i];
				activityMenu.setOfficeId(parentsId);
				list.addAll(dao.findByParentAndOffice(activityMenu));
			}
			return list;
		}catch (Exception e){
			return new ArrayList<>();
		}
	}

	@Transactional(readOnly = false)
	public void save(WorkActivityMenu workActivityMenu) {
		try {
			if (StringUtils.isBlank(workActivityMenu.getId())){
				List<WorkActivityMenu> workActivityMenus = dao.findByParentAndOffice(workActivityMenu);
				if (workActivityMenus!=null && workActivityMenus.size()!=0){
					workActivityMenu.setId(workActivityMenus.get(0).getId());
				}
			}

			if (workActivityMenu.getActivities()!=null && workActivityMenu.getActivities().size()!=0 && StringUtils.isNotBlank(workActivityMenu.getProcessKey())) {
				//List<Activity> activityList= activityService.findByActivityMenuId(workActivityMenu.getId());
				//List<Activity> activities = workActivityMenu.getActivities();
				List<Activity> list = new ArrayList<Activity>();
				boolean b = false;
				//String processId = "process_"+System.currentTimeMillis();
				/*if (activityList.size() == 0 || activityList.size() != Integer.parseInt(workActivityMenu.getProcessKey())){
					b = true;
				}else if (activityList.size() == Integer.parseInt(workActivityMenu.getProcessKey())){
					for (int i=0 ; i<Integer.parseInt(workActivityMenu.getProcessKey()); i++){
						Activity oldActivity = activityList.get(i);
						Activity activity = activities.get(i);
						if (!oldActivity.getReturnBack().equals(activity.getReturnBack())){
							b = true;
							break;
						}
						if (oldActivity.getRole()!=null && StringUtils.isNotBlank(oldActivity.getRole().getEnname())){
							if (activity.getRole()==null || StringUtils.isBlank(activity.getRole().getEnname())){
								b = true;
								break;
							}
						}else {
							if (activity.getRole()!=null && StringUtils.isNotBlank(activity.getRole().getEnname())){
								b = true;
								break;
							}
						}
						if (oldActivity.getUser()!=null && StringUtils.isNotBlank(oldActivity.getUser().getId())){
							if (activity.getUser()==null || StringUtils.isBlank(activity.getUser().getId())){
								b = true;
								break;
							}
						}else {
							if (activity.getUser()!=null && StringUtils.isNotBlank(activity.getUser().getId())){
								b = true;
								break;
							}
						}
					}
				}*/
				if (!b){
					activityService.deleteToDelflag(workActivityMenu.getId());
					//workActivityMenu.setProcessType(processId);
					super.save(workActivityMenu);
					for (int i = 0 ;i<Integer.parseInt(workActivityMenu.getProcessKey()) ;i++) {
						Activity activity = workActivityMenu.getActivities().get(i);
						activity.setCount(i+1);
						activity.setActivityMenuId(workActivityMenu.getId());
						activity.setIsApproval("0");
						activity.setProcessKey(workActivityMenu.getProcessType());
						activity.setId("");
						list.add(activity);
						activityService.save(activity);
					}
					//activityXmlService.createActivityXml(processId,workActivityMenu.getName()+System.currentTimeMillis(),list);
				}else {
					super.save(workActivityMenu);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(WorkActivityMenu workActivityMenu) {
		activityService.deleteToDelflag(workActivityMenu.getId());
		super.delete(workActivityMenu);
	}

	public SysRoleActivity getByEnname(SysRoleActivity selectRole, String companyId, String branchCompanyId){
		try {
			String enname = selectRole.getEnname();
			if (StringUtils.isNotBlank(branchCompanyId) && StringUtils.isNotBlank(companyId) && companyId.equals("0") && branchCompanyId.equals("0")) {
				String buffer = UserUtils.getRoleActivity(UserUtils.getSelectCompany().getId());
				buffer += enname;
				selectRole.setEnname(buffer);
			}
			SysRoleActivity returnRole = roleActivityDao.getByEnname(enname);
			return returnRole;
		}catch (Exception e){
			return new SysRoleActivity();
		}

	}

	public void sortStringArray(String[] arrStr) {
		String temp;
		for (int i = 0; i < arrStr.length; i++) {
			for (int j = arrStr.length - 1; j > i; j--) {
				if (arrStr[i].length() > arrStr[j].length()) {
					temp = arrStr[i];
					arrStr[i] = arrStr[j];
					arrStr[j] = temp;
				}
			}
		}
	}

}