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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sys.dao.RoleDao;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.Role;
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.entity.Activity;
import org.h2.mvstore.DataUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.sys.entity.SysRoleActivity;
import com.jeeplus.modules.sys.dao.SysRoleActivityDao;

/**
 * 工作流角色信息Service
 * @author 杨帆
 * @version 2018-04-23
 */
@Service
@Transactional(readOnly = true)
public class SysRoleActivityService extends CrudService<SysRoleActivityDao, SysRoleActivity> {

	@Autowired
	private SysRoleActivityDao sysRoleActivityDao;
	@Autowired
	private ActivityDao activityDao;
	@Autowired
	private RoleDao roleDao;

	public SysRoleActivity get(String id) {
		SysRoleActivity sysRoleActivity = super.get(id);
		String name=sysRoleActivity.getName();
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			Office company = UserUtils.getSelectCompany();
			Activity activity = new Activity();
			sysRoleActivity.setName(sysRoleActivity.getEnname().replace(UserUtils.getRoleActivityEnname(company.getId(),user),""));
			activity.setRole(sysRoleActivity);
			activity.setCompanyId(company.getId());
			List<Activity> activityList = activityDao.findListByMenu(activity);
			if (activityList!=null && activityList.size()!=0){
				sysRoleActivity.setActivityList(activityList);
			}
			Role role = new Role();
			role.setCompany(company);
			role.setActivityId(id);
			List<Role> roleList = roleDao.getActivityByRole(role);
			if (roleList!=null && roleList.size()!=0){
				sysRoleActivity.setRoleList(roleList);
			}
		}
        sysRoleActivity.setName(name);
		return sysRoleActivity;
	}
	public SysRoleActivity getByEnname(String enname) {
		return sysRoleActivityDao.getByEnname(enname);
	}

	public List<SysRoleActivity> findList(SysRoleActivity sysRoleActivity) {
		return super.findList(sysRoleActivity);
	}
	
	public Page<SysRoleActivity> findPage(Page<SysRoleActivity> page, SysRoleActivity sysRoleActivity) {
		if(!UserUtils.getUser().getId().equals("1")){
			sysRoleActivity.setCompanyId(UserUtils.getSelectCompany().getId());
		}
		return super.findPage(page, sysRoleActivity);
	}
	
	@Transactional(readOnly = false)
	public void save(SysRoleActivity sysRoleActivity) {
		try {
			if(StringUtils.isBlank(sysRoleActivity.getId())){
				if (StringUtils.isBlank(sysRoleActivity.getEnname())){
					Office company = UserUtils.getSelectCompany();
					StringBuffer buffer = DateUtils.getByEnnme(UserUtils.getRoleActivityEnname(company.getId(),sysRoleActivity.getCreateBy()==null?UserUtils.getUser():sysRoleActivity.getCreateBy()));
					buffer.append(StringUtils.getPinYinHeadChar(sysRoleActivity.getName(),1));
					String enname = buffer.toString()+ UserUtils.getpingyin(4);
					SysRoleActivity s = sysRoleActivityDao.getByEnname(enname);
					if (s!=null && StringUtils.isNotBlank(s.getId())){
						enname = enname+UserUtils.getpingyin(4) ;
					}
					sysRoleActivity.setEnname(enname);
				}
				if (StringUtils.isBlank(sysRoleActivity.getCompanyId())){
					sysRoleActivity.setCompanyId(UserUtils.getSelectCompany().getId());
				}

			}
			super.save(sysRoleActivity);
		}catch (Exception e){
			logger.info("Exception e:"+e);
		}

	}
	
	@Transactional(readOnly = false)
	public void delete(SysRoleActivity sysRoleActivity) {
		super.delete(sysRoleActivity);
	}

	public String isSysRoleActivityRelation(SysRoleActivity sysRoleActivity) {
		String count = dao.isSysRoleActivityRelation(sysRoleActivity);
		return count;
	}

}