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 HouseProvider {

    /**
     * 自己写sql mybatis 不懂识别下划线 user_name，需要用别名
     *
     * 显示对应的名字sql
     *
     */
    public String findAllBySearchKey(String searchKey, Long userId, Long handlerId){
        StringBuffer sql = new StringBuffer(
                "SELECT z.id, z.num, z.address, z.district_name, z.unit_type, z.area, z.floor, z.orientation, z.status, " +
                        "z.requirement, z.handler, z.create_time, z.update_time, z.web_site, z.scene_code, " +
                        "u.real_name as upload_name, c.real_name as handler_name FROM " +
                "tb_house z left join tb_user u ON z.user_id = u.id left join tb_user c ON z.handler = c.id where z.rec_status = 'A' ");
        if(StrUtil.isNotBlank(searchKey)){
            sql.append(" and (");
            sql.append("  z.district_name like '%").append(searchKey).append("%'");
            sql.append(" or z.num like '%").append(searchKey).append("%'");
            sql.append(" or u.real_name like '%").append(searchKey).append("%'");
            sql.append(" or c.real_name like '%").append(searchKey).append("%'");
            sql.append( ")");
        }

        if (userId != null) {
            sql.append(" and z.user_id = ").append(userId);
        }

        if (handlerId != null) {
            sql.append(" and z.handler = ").append(handlerId);
        }

        sql.append(" ORDER BY z.create_time DESC");
        log.info("sql: {}", sql.toString());
        return sql.toString();
    }


    /**
     * 只给edit、upload权限用户获取列表使用
     *
     * 不是很通用
     */
    public String findBySearchKey(String searchKey, Long userId, Long handlerId){
        StringBuffer sql = new StringBuffer(
                "SELECT z.id, z.num, z.address, z.district_name, z.unit_type, z.area, z.floor, z.orientation, z.status, " +
                        "z.requirement, z.handler, z.create_time, z.update_time, z.web_site, z.scene_code, " +
                        "u.real_name as upload_name, c.real_name as handler_name FROM " +
                        "tb_house z left join tb_user u ON z.user_id = u.id left join tb_user c ON z.handler = c.id where z.rec_status = 'A' ");
        if(StrUtil.isNotBlank(searchKey)){
            sql.append(" and (");
            sql.append("  z.district_name like '%").append(searchKey).append("%'");
            sql.append(" or z.num like '%").append(searchKey).append("%'");
            sql.append(" or u.real_name like '%").append(searchKey).append("%'");
            sql.append(" or c.real_name like '%").append(searchKey).append("%'");
            sql.append( ")");
        }

        if (userId != null) {
            sql.append(" and z.user_id = ").append(userId);
        }

        // or会影响搜索结果, 不加rec_status， 会过滤不了
        if (handlerId != null) {
//            sql.append(" or z.handler = ").append(handlerId);
            sql.append(" or ( z.handler = ").append(handlerId).append(" AND z.rec_status = 'A' ) ");
        }

        sql.append(" ORDER BY z.create_time DESC");
        log.info("sql: {}", sql.toString());
        return sql.toString();
    }



}
