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

import com.gyee.wisdom.common.data.DataUtil;
import com.gyee.wisdom.common.data.system.model.User;
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.RoleRepository;
import com.gyee.wisdom.dao.sql.system.repository.UserRepository;
import com.gyee.wisdom.dao.system.IUserDao;
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 JpaUserDao implements IUserDao {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private RoleRepository roleRepository;


    public List<User> findAll() {
        return DataUtil.convertDataList(userRepository.findAll());
    }

    public User findByUserName(String userName) {
        List<UserEntity> list=userRepository.findAll();
        return userRepository.findFirstByUserName(userName).toData();
    }

    public User findById(String id) {
        return userRepository.findById(id).get().toData();
    }


    public boolean removeById(String id) {
        UserEntity et = userRepository.findById(id).get();
        if (et != null) {
            et.setDisabled(true);
            userRepository.save(et);
            return true;
        }
        return false;
    }

    @Override
    public List<User> getUser(String userName, String telePhone, String email) {
        Specification querySpecifi = new Specification<UserEntity>() {
            @Override
            public Predicate toPredicate(Root<UserEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                List<Predicate> predicates = new ArrayList<>();
                if (userName != null && !userName.trim().equals("")) {
                    predicates.add(criteriaBuilder.like(root.get("userName"), "%" + userName + "%"));
                }
                if (telePhone != null && !telePhone.trim().equals("")) {
                    predicates.add(criteriaBuilder.like(root.get("telePhone"), "%" + telePhone + "%"));
                }
                if (email != null && !email.trim().equals("")) {
                    predicates.add(criteriaBuilder.like(root.get("email"), "%" + email + "%"));
                }
//                if (productId != null && productId > 0) {
//                    predicates.add(criteriaBuilder.equal(root.get("productId"), productId));
//                }

                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };

        List<UserEntity> resultList = userRepository.findAll(querySpecifi);
        return DataUtil.convertDataList(resultList);
    }

    @Override
    public void add(User user) {
        if(user.getRoles()==null)
            user.setRoles(new ArrayList<>());
        UserEntity entity = new UserEntity(user);
        userRepository.save(entity);
        return;
    }

    @Override
    public void update(User user) {
        UserEntity entity = new UserEntity(user);
        userRepository.saveAndFlush(entity);
        return;
    }

    @Override
    public void updateRoles(String uid, List<String> rids) {
        UserEntity userEntity = userRepository.findById(uid).get();

        //执行删除user_role操作
        userEntity.setRoles(new ArrayList<RoleEntity>());
        userRepository.save(userEntity);

        //新增user_role操作
        List<RoleEntity> roleList=   roleRepository.findAllById(rids);

        userEntity.setRoles(roleList);

        userRepository.save(userEntity);

    }

    @Override
    public User get(String id) {
        return userRepository.findById(id).get().toData();
    }

    @Override
    public boolean contains(String name) {
        return false;
    }

    @Override
    public List<User> list() {
        List<User> list = DataUtil.convertDataList(userRepository.findByUserNameNot("admin"));
        List<User> userList = userNameIsNot("admin");
        return list;

    }

    public List<User> userNameIsNot(String userName) {
        Specification querySpecifi = new Specification<UserEntity>() {
            @Override
            public Predicate toPredicate(Root<UserEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                List<Predicate> predicates = new ArrayList<>();
                if (userName != null && !userName.trim().equals("")) {
                    predicates.add(criteriaBuilder.like(root.get("userName"), userName));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };

        List<UserEntity> resultList = userRepository.findAll(querySpecifi);
        return DataUtil.convertDataList(resultList);
    }

    @Override
    public boolean hasResourcePermission(String uid, String resourceCode) {
        return false;
    }

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

    @Override
    public void switchStatus(String id, boolean disabled) {
        UserEntity entity=userRepository.findById(id).get();
        entity.setDisabled(disabled);

        userRepository.save(entity);

    }

    @Override
    public User findByPhone(String phone) {
        return null;
    }

}
