package com.usoft.credit.bl;

import com.usoft.credit.dal.entity.User;
import com.usoft.credit.dal.mapper.UserMapper;
import com.usoft.credit.grpc.api.protobuf.EnterpriseQueryRequest;
import com.usoft.credit.grpc.api.protobuf.PersonQueryRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liuam
 * @since 2018/8/7 0007 下午 14:32
 */
@Service
public class UserBL {

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private UserMapper userMapper;

    /**
     * 验证 EnterpriseQueryRequest 的 user 信息是否正确
     * @return true 正确
     */
    public boolean verifyEnterprise(EnterpriseQueryRequest request) {
        String password = request.getUser().getPassword();
        int useruu = request.getUser().getUseruu();
        int enuu = request.getUser().getEnuu();
        return verify(password, enuu, useruu);
    }

    /**
     * 验证 PersonQueryRequest 的 user 信息是否正确
     * @return true 正确
     */
    public boolean verifyPerson(PersonQueryRequest request) {
        String password = request.getUser().getPassword();
        int useruu = request.getUser().getUseruu();
        int enuu = request.getUser().getEnuu();
        return verify(password, enuu, useruu);
    }

    private boolean verify(String password, Integer enuu, Integer useruu) {
        User user = new User();
        user.setPassword(password);
        user.setEnuu(enuu);
        user.setUseruu(useruu);
        User selectUser = userMapper.selectByUser(user);
        if (selectUser == null) {
            return false;
        }
        return true;
    }

}
