package com.jpsoft.shinestar.modules.base.service.impl;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.annotation.Resource;

import com.jpsoft.shinestar.modules.base.entity.IconInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.jpsoft.shinestar.modules.base.dao.PersonRoleIconDAO;
import com.jpsoft.shinestar.modules.base.entity.PersonRoleIcon;
import com.jpsoft.shinestar.modules.base.service.PersonRoleIconService;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.github.pagehelper.PageHelper;

@Transactional
@Component(value="personRoleIconService")
public class PersonRoleIconServiceImpl implements PersonRoleIconService {
	@Resource(name="personRoleIconDAO")
	private PersonRoleIconDAO personRoleIconDAO;

	@Override
	public PersonRoleIcon get(String id) {
		// TODO Auto-generated method stub
		return personRoleIconDAO.get(id);
	}

	@Override
	public int insert(PersonRoleIcon model) {
		// TODO Auto-generated method stub
		//model.setId(UUID.randomUUID().toString());

		return personRoleIconDAO.insert(model);
	}

	@Override
	public int update(PersonRoleIcon model) {
		// TODO Auto-generated method stub
		return personRoleIconDAO.update(model);
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return personRoleIconDAO.delete(id);
	}

	@Override
	public boolean exist(String id) {
		// TODO Auto-generated method stub
		int count = personRoleIconDAO.exist(id);

		return count > 0 ? true : false;
	}

	@Override
	public List<PersonRoleIcon> list() {
		// TODO Auto-generated method stub
		return personRoleIconDAO.list();
	}

	@Override
	public Page<PersonRoleIcon> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize,boolean count,List<Sort> sortList) {
        Page<PersonRoleIcon> page = PageHelper.startPage(pageNumber,pageSize,count).doSelectPage(()->{
            personRoleIconDAO.search(searchParams,sortList);
        });

        return page;
	}

	@Override
	public List<PersonRoleIcon> findByPersonRoleId(String personRoleId) {
		return personRoleIconDAO.findByPersonRoleId(personRoleId);
	}

	@Override
	public String getDefaultIcon(String personRoleId) {

		String defaultIcon = "";
		List<PersonRoleIcon> personRoleIconList = findByPersonRoleId(personRoleId);
		if (personRoleIconList.size()>0){
			int i = 0;
			for (PersonRoleIcon personRoleIcon : personRoleIconList){
				//默认图标状态为true，最多设置7个默认图标
				if (!personRoleIcon.getCommonStatus() && personRoleIcon.getDefaultStatus() && i<7){
					if (StringUtils.isBlank(defaultIcon)){
						defaultIcon = personRoleIcon.getIconId();
					}else {
						defaultIcon = defaultIcon + "," + personRoleIcon.getIconId();
					}
					i++;
				}
			}
		}

		return defaultIcon;
	}

	@Override
	public List<PersonRoleIcon> findByPersonRoleIdAndCommonStatus(String personRoleId, boolean commonStatus) {
		return personRoleIconDAO.findByPersonRoleIdAndCommonStatus(personRoleId,commonStatus);
	}

	@Override
	public List<IconInfo> findCommonByPersonRoleId(String personRoleId) {
		return personRoleIconDAO.findCommonByPersonRoleId(personRoleId);
	}

	@Override
	public List<IconInfo> findDefaultIconByPersonRoleId(String personRoleId) {
		return personRoleIconDAO.findDefaultIconByPersonRoleId(personRoleId);
	}

	@Override
	public int deleteCommonByPersonRoleId(String personRoleId) {
		return personRoleIconDAO.deleteCommonByPersonRoleId(personRoleId);
	}

	@Override
	public int deleteDefaultIconByPersonRoleId(String personRoleId) {
		return personRoleIconDAO.deleteDefaultIconByPersonRoleId(personRoleId);
	}

	@Override
	public PersonRoleIcon findByPersonRoleIdAndIconId(String personRoleId, String iconId) {
		return personRoleIconDAO.findByPersonRoleIdAndIconId(personRoleId,iconId);
	}

	@Override
	public List<PersonRoleIcon> findByIconName(String iconName) {
		return personRoleIconDAO.findByIconName(iconName);
	}
}