package top.cardone.authority.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import top.cardone.authority.dao.UserRoleDao;
import top.cardone.data.service.impl.PageServiceImpl;

import java.util.List;
import java.util.Map;

/**
 * 用户与角色服务
 *
 * @author yao hai tao
 */
@Transactional(readOnly = true)
public class UserRoleServiceImpl extends PageServiceImpl<UserRoleDao> implements top.cardone.authority.service.UserRoleService {
    @Override
    public Page<Map<String, Object>> pageCache(Object page) {
        return this.page(page);
    }

    @Override
    public <P> Page<P> pageCache(Class<P> mappedClass, Object page) {
        return this.page(mappedClass, page);
    }

    @Override
    public <P> List<P> findListCache(Class<P> mappedClass, Object findList) {
        return this.findList(mappedClass, findList);
    }

    @Override
    public <P> P findOneCache(Class<P> mappedClass, Object findOne) {
        return this.findOne(mappedClass, findOne);
    }

    @Override
    public <R> List<R> readListCache(Class<R> requiredType, Object readList) {
        return this.readList(requiredType, readList);
    }

    @Override
    public <R> R readOneCache(Class<R> requiredType, Object readOne) {
        return this.readOne(requiredType, readOne);
    }

    @Override
    @Transactional
    public int deleteCache(Object delete) {
        return this.delete(delete);
    }

    @Override
    @Transactional
    public int deleteAllCache() {
        return this.deleteAll();
    }

    @Override
    @Transactional
    public int deleteByIdsCache(Object ids) {
        return this.deleteByIds(ids);
    }

    @Override
    @Transactional
    public int[] deleteListCache(List<Object> deleteList) {
        return this.deleteList(deleteList);
    }

    @Override
    public List<Map<String, Object>> findListCache(Object findList) {
        return this.findList(findList);
    }

    @Override
    public Map<String, Object> findOneCache(Object findOne) {
        return this.findOne(findOne);
    }

    @Override
    @Transactional
    public int insertCache(Object insert) {
        return this.insert(insert);
    }

    @Override
    @Transactional
    public int insertByNotExistsCache(Object insert) {
        return this.insertByNotExists(insert);
    }

    @Override
    @Transactional
    public int[] insertListCache(List<Object> insertList) {
        return this.insertList(insertList);
    }

    @Override
    @Transactional
    public int[] insertListByNotExistsCache(List<Object> insertList) {
        return this.insertListByNotExists(insertList);
    }

    @Override
    public List<Object> readListCache(Object readList) {
        return this.readList(readList);
    }

    @Override
    public Object readOneCache(Object readOne) {
        return this.readOne(readOne);
    }

    @Override
    @Transactional
    public Integer saveCache(Object save) {
        return this.save(save);
    }

    @Override
    @Transactional
    public int updateCache(Object update) {
        return this.update(update);
    }

    @Override
    @Transactional
    public int[] updateListCache(List<Object> updateList) {
        return this.updateList(updateList);
    }

    @Override
    @Transactional
    public int[] insertListByNotExistsForRolesCache(Map<String, Object> insert) {
        return this.insertListByNotExistsForRoles(insert);
    }

    @Override
    @Transactional
    public int[] insertListByNotExistsForRoles(Map<String, Object> insert) {
        String userCode = MapUtils.getString(insert, "userCode");

        Object roleCodes = MapUtils.getObject(insert, "roleCodes");

        String[] roleCodeList;

        if (roleCodes instanceof String) {
            roleCodeList = new String[]{(String) roleCodes};
        } else if (roleCodes instanceof String[]) {
            roleCodeList = (String[]) roleCodes;
        } else {
            return new int[]{};
        }

        if (ArrayUtils.isEmpty(roleCodeList)) {
            return new int[]{};
        }

        List<Object> insertList = Lists.newArrayList();

        for (String roleCode : roleCodeList) {
            Map<String, Object> newInsert = Maps.newHashMap();

            newInsert.put("userCode", userCode);
            newInsert.put("roleCode", roleCode);

            insertList.add(newInsert);
        }

        return this.insertListByNotExists(insertList);
    }

    @Override
    @Transactional
    public int[] updateListForRolesCache(Map<String, Object> update) {
        return this.updateListForRoles(update);
    }

    @Override
    @Transactional
    public int[] updateListForRoles(Map<String, Object> update) {
        Map<String, Object> delete = Maps.newHashMap();

        delete.put("userCode", update.get("userCode"));

        this.delete(delete);

        return this.insertListByNotExistsForRoles(update);
    }

    @Override
    @Transactional
    public int deleteByUserCodesCache(Map<String, Object> delete) {
        return this.deleteByUserCodes(delete);
    }

    @Override
    @Transactional
    public int deleteByUserCodes(Map<String, Object> delete) {
        return this.dao.deleteByUserCodes(delete);
    }

    @Override
    public Map<String, Object> findOneByUserCode(Map<String, Object> findOne) {
        return this.dao.findOneByUserCode(findOne);
    }
}
@@ -155,19 +155,26 @@
 ections.
-Map
+Collection
 Utils;%0Ai
@@ -202,19 +202,23 @@
 ons.
-lang3.Array
+collections.Map
 Util
@@ -3886,24 +3886,28 @@
         
+List%3C
 String
-%5B%5D
+%3E
  roleCod
@@ -3988,21 +3988,27 @@
 t = 
-new String%5B%5D%7B
+Lists.newArrayList(
 (Str
@@ -4021,17 +4021,17 @@
 oleCodes
-%7D
+)
 ;%0A      
@@ -4064,24 +4064,20 @@
 tanceof 
-String%5B%5D
+List
 ) %7B%0A    
@@ -4104,16 +4104,20 @@
  = (
+List%3C
 String
-%5B%5D
+%3E
 ) ro
@@ -4197,21 +4197,26 @@
     if (
-Array
+Collection
 Utils.is

fix bug