package com.fdkanfang.dao.backend.provider;

import cn.hutool.core.util.StrUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;

/**
 * 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, " +
                        "z.audit_result, z.can_not_edit, z.can_not_edit_desc, " +
                        "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();
    }

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

        if (null != uploadId) {
            sql.append(" and house.user_id = ").append(uploadId);
        }
        sql.append(" ORDER BY house.create_time DESC");
        log.info("sql: {}", sql.toString());
        return sql.toString();

    }



}
