package com.fdkanfang.dao.backend.provider;

import cn.hutool.core.util.StrUtil;
import lombok.extern.log4j.Log4j2;

/**
 * Created by Hb_zzZ on 2020/3/3.
 */
@Log4j2
public class UserProvider {

    /**
     * 自己写sql mybatis 不懂识别下划线 user_name，需要用别名
     *
     * 一对多的条件：(select * FROM tb_user_role GROUP BY user_id)
     */
    public String findAllBySearchKey(String searchKey){
        StringBuffer sql = new StringBuffer("" +
                "SELECT z.id, z.user_name as userName, z.real_name as realName,  z.phone, z.sex, z.status, z.email, z.identity,z.nickname, z.create_time, z.update_time," +
                " c.role_key, c.role_name " +
                "FROM tb_user z LEFT JOIN (select * FROM tb_user_role GROUP BY user_id) a ON a.user_id = z.id LEFT JOIN `tb_role` c ON c.id = a.role_id  where z.rec_status = 'A' ");
        if(StrUtil.isNotBlank(searchKey)){
            sql.append(" and (");
            sql.append(" z.real_name like '%").append(searchKey).append("%'");
            sql.append(" or z.phone like '%").append(searchKey).append("%'");
            sql.append( ")");
        }
        sql.append(" ORDER BY z.create_time DESC");
        log.info("sql: {}", sql.toString());
        return sql.toString();
    }



}
