package com.gyee.wisdom.dao.sql.system;

import com.gyee.wisdom.common.data.DataUtil;
import com.gyee.wisdom.common.data.system.model.Role;
import com.gyee.wisdom.dao.sql.system.entity.MenuEntity;
import com.gyee.wisdom.dao.sql.system.entity.RoleEntity;
import com.gyee.wisdom.dao.sql.system.entity.UserEntity;
import com.gyee.wisdom.dao.sql.system.repository.MenuRepository;
import com.gyee.wisdom.dao.sql.system.repository.RoleRepository;
import com.gyee.wisdom.dao.sql.system.repository.UserRepository;
import com.gyee.wisdom.dao.system.IRoleDao;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;


import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author songwb<songwb                                                                                                                               @                                                                                                                               aliyun.com>
 */
@Component
public class JpaRoleDao implements IRoleDao {

    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private MenuRepository menuRepository;

    @Autowired
    private UserRepository userRepository;

    @Override
    public void add(Role role) {
        RoleEntity entity = new RoleEntity(role);
        roleRepository.save(entity);
    }

    @Override
    public void update(Role role) {
        if (StringUtils.isEmpty(role.getId())) {
            return;
        } else {
            RoleEntity entity = new RoleEntity(role);
            roleRepository.save(entity);
        }

    }

    @Override
    public void updateMenus(String rid, List<String> mids) {

        if (!CollectionUtils.isEmpty(mids)) {
            List<MenuEntity> menuEntityList = menuRepository.findAllById(mids);
            RoleEntity roleEntity = roleRepository.findById(rid).get();

            if (roleEntity != null && menuEntityList.size() >= 0) {
                roleEntity.setMenuEntityList(menuEntityList);
                roleRepository.save(roleEntity);
            }
        }
    }

    @Override
    public void updateResources(String rid, List<String> resources) {

    }

    @Override
    public boolean contains(String roleName) {

        Specification querySpecifi = new Specification<RoleEntity>() {
            @Override
            public Predicate toPredicate(Root<RoleEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                List<Predicate> predicates = new ArrayList<>();
                if (roleName != null && !roleName.trim().equals("")) {
                    predicates.add(criteriaBuilder.like(root.get("userName"), "%" + roleName + "%"));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        List<UserEntity> resultList = roleRepository.findAll(querySpecifi);
        if (resultList.isEmpty() || resultList.size() <= 0)
            return false;
        else
            return true;
    }

    @Override
    public Role get(String id) {
        RoleEntity entity = roleRepository.findById(id).get();
        List<MenuEntity> list = entity.getMenuEntityList();
        return roleRepository.findById(id).get().toData();
    }

    @Override
    public List<Role> list() {
        List<RoleEntity> entityList = roleRepository.findAll();
        List<Role> dataList = new ArrayList<>();

        for (RoleEntity data : entityList) {
            dataList.add(data.toData());
        }
        return dataList;
    }

    @Override
    public void remove(String id) {
        roleRepository.deleteById(id);
    }

    @Override
    public void removeRoleMenuByMenuId(String menuId) {

        MenuEntity menuEntity=menuRepository.findById(menuId).get();
        menuEntity.setRoles(new ArrayList<>());
        menuRepository.save(menuEntity);

    }

    @Override
    public void removeRoleResourceByResourceId(String resourceId) {

    }

    @Override
    public void switchStatus(String id, boolean disabled) {
        RoleEntity entity = roleRepository.findById(id).get();
        entity.setDisabled(disabled);
        roleRepository.save(entity);
    }

    @Override
    public List<Role> getRoles(String userId) {
        UserEntity userEntity = userRepository.findById(userId).get();
        return DataUtil.convertDataList(userEntity.getRoles());

    }

    @Override
    public String getRoleid(String uid) {
        String roleId="";
        UserEntity userEntity=userRepository.findById(uid).get();
        if(userEntity!=null&&userEntity.getRoles().size()>0){
            roleId =userEntity.getRoles().get(0).getId();
            return roleId;
        }
        else
            return "";

    }
}
