package com.fdkanfang.dao.backend.provider;

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

/**
 * Created by Abner on 2020/7/23.
 */
@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'  and house.status is not null ");
        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();
    }

    /**
     *通用获取房源列表
     */
    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' and house.status is not null ");
        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 || null != uploadId) {
            if(null != handlerId && null == uploadId){
                //只有编辑权限
                sql.append(" and house.handler = ").append(handlerId);
            }else if(null == handlerId && null != uploadId){
                //只有上传权限，没有编辑权限
                sql.append(" and house.user_id = ").append(uploadId);
            }else{
                //同时具有上传权限和编辑权限
                sql.append(" and (");
                sql.append(" house.user_id = ").append(uploadId);
                sql.append(" or house.handler = ").append(handlerId);
                sql.append( ")");
            }
        }
        sql.append(" ORDER BY house.create_time DESC");
        log.info("sql: {}", sql.toString());
        return sql.toString();

    }



}
