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

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sg.managementcenter.privilegePre.entity.PrivilegePre;
import com.jeeplus.modules.sg.managementcenter.privilegePre.mapper.PrivilegePreMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 请假表单Service
 * @author lgf
 * @version 2018-06-12
 */
@Service
@Transactional(readOnly = true)
public class PrivilegePreService extends CrudService<PrivilegePreMapper, PrivilegePre> {
    @Autowired
	private PrivilegePreMapper privilegePreMapper;

	public PrivilegePre get(String id) {
		PrivilegePre project = super.get(id);
		return project;
	}

	public Page<PrivilegePre> findPage(Page<PrivilegePre> page, PrivilegePre entity) {
		dataRuleFilter(entity);
		entity.setPage(page);
		PrivilegePre listUser = privilegePreMapper.findListUser(entity.getUid());
		if (listUser!=null&&!"".equals(listUser)) {
			String pid = listUser.getPid();
			String[] split = pid.split(";");
			List<String> needPid = new ArrayList<>();
			for (int i = 0; i < split.length; i++) {
				needPid.add(split[i]);
			}
			List<PrivilegePre> procdef = privilegePreMapper.findProcdef(needPid);
			page.setList(procdef);
		}
		return page;
	}

	public List<PrivilegePre> findProcdef(List<String> needPid){
		return privilegePreMapper.findProcdef(needPid);
	}

	public PrivilegePre findListUser(String uid){
		return privilegePreMapper.findListUser(uid);
	}

	@Transactional(readOnly = false)
	public void updatePersonal(String pid, String uid){
		privilegePreMapper.updatePersonal(pid, uid);
	}

	@Transactional(readOnly = false)
	public void addPersonal(PrivilegePre privilegePre){
		privilegePreMapper.addPersonal(privilegePre);
	}



	public Page<PrivilegePre> findAllDefPage(Page<PrivilegePre> page, PrivilegePre entity) {
		dataRuleFilter(entity);
		entity.setPage(page);
		Map<String, PrivilegePre> map = new HashMap<>();
		List<PrivilegePre> allProcDef = privilegePreMapper.findAllProcDef();
		List<PrivilegePre> showProcDef = new ArrayList<>();
		for (PrivilegePre p : allProcDef) {
			String Key = p.getOneKey();
			if (map.containsKey(Key)) {
				int s = map.get(Key).getOneVersion();
				if (p.getOneVersion() > s) {
					map.put(Key, p);
				}
			} else {
				map.put(Key, p);
			}
		}
		Set<String> afterMap = map.keySet();
		Iterator<String> it = afterMap.iterator();
		while (it.hasNext()) {
			showProcDef.add(map.get(it.next()));
		}

		PrivilegePre listUser = privilegePreMapper.findListUser(entity.getUid());
		if (listUser != null&&!"".equals(listUser)) {
			String pid = listUser.getPid();
			String[] split = pid.split(";");
			for (int i = 0; i < split.length; i++) {
				Iterator<PrivilegePre> iterator = showProcDef.iterator();
				while (iterator.hasNext()) {
					PrivilegePre next = iterator.next();
					if (next.getId().equals(split[i])) {
						iterator.remove();
					}
				}
			}
		}
		page.setList(showProcDef);
		return page;
	}


/*	public Page<User> findUser(Page<User> page, User user) {
		dataRuleFilter(user);
		// 设置分页参数
		user.setPage(page);
		// 执行分页查询
		page.setList(userMapper.findList(user));
		return page;
	}*/

}