<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.springblade.modules.xxy.mapper.XxyMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="XxyResultMap" type="org.springblade.modules.xxy.entity.XxyEntity">
        <result column="id" property="id"/>
        <result column="szlb" property="szlb"/>
        <result column="xm" property="xm"/>
        <result column="sfzh" property="sfzh"/>
        <result column="lxfs" property="lxfs"/>
        <result column="zzmm" property="zzmm"/>
        <result column="dwzw" property="dwzw"/>
        <result column="sqmc" property="sqmc"/>
        <result column="zz" property="zz"/>
        <result column="ld_id" property="ldId"/>
        <result column="create_time" property="createTime"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>


    <select id="selectXxyPage" parameterType="org.springblade.modules.xxy.entity.XxyEntity" resultType="org.springblade.modules.xxy.entity.XxyEntity">
        SELECT
            xxy.xm,
            xxy.sfzh,
            xxy.szlb,
            xxy.ld_id,
            xxy.zzmm,
            xxy.dwzw,
            xxy.lxfs,
            ld.b_name AS bName,
            ld.unit,
            ld.sqmc,
            ld.sq_id,
            ld.ss_org_name,
            ld.ss_org_id
        FROM
            xxy_info xxy
        LEFT JOIN ld_info ld ON xxy.ld_id = ld.id
        WHERE
            xxy.is_deleted = 0
            <if test="Xxy.xm !='' and Xxy.xm !=null">
                and xxy.xm = #{Xxy.xm}
            </if>
            <if test="Xxy.sfzh !='' and Xxy.sfzh !=null">
                and xxy.sfzh = #{Xxy.sfzh}
            </if>
            <if test="Xxy.lxfs !='' and Xxy.lxfs !=null">
                and xxy.lxfs = #{Xxy.lxfs}
            </if>
            <if test="Xxy.sqmc !='' and Xxy.sqmc !=null">-->
                and ld.sqmc like concat('%', #{Xxy.sqmc},'%')
            </if>
    </select>


    <select id="exportXxy" resultType="org.springblade.modules.xxy.excel.XxyExcel">
        SELECT
            xi.id,
            xi.xm,
            xi.sfzh,
            xi.lxfs,
            xi.szlb,
            xi.zzmm,
            xi.dwzw,
            li.b_name AS ldmc,
            li.sqmc
        FROM
            xxy_info xi
        LEFT JOIN ld_info li ON li.ID = xi.ld_id
        LEFT JOIN blade_dept bd ON bd.ID = li.sq_id
        WHERE
            xi.is_deleted = 0
            AND ((#{params.deptId} LIKE '%' || bd.id || '%') OR (bd.ancestors LIKE '%' || #{params.deptId} || '%'))
        <if test="null != params.xm and '' != params.xm">
            AND xi.xm LIKE '%' || #{params.xm} || '%'
        </if>
        <if test="null != params.bname and '' != params.bname">
            AND li.b_name LIKE '%' || #{params.bname} || '%'
        </if>
        <if test="null != params.sqmc and '' != params.sqmc">
            AND li.sqmc LIKE '%' || #{params.sqmc} || '%'
        </if>
    </select>

    <select id="getXxyList" resultType="org.springblade.modules.xxy.vo.XxyVO">
        SELECT
            xi.id,
            xi.xm,
            xi.sfzh,
            xi.lxfs,
            xi.szlb,
            xi.zzmm,
            xi.dwzw,
            li.b_name AS bName,
            li.sqmc
        FROM
            xxy_info xi
        LEFT JOIN ld_info li ON li.ID = xi.ld_id
        LEFT JOIN blade_dept bd ON bd.ID = li.sq_id
        WHERE
            xi.is_deleted = 0
        AND ((#{params.deptId} LIKE '%' || bd.id || '%') OR (bd.ancestors LIKE '%' || #{params.deptId} || '%'))
        <if test="null != params.xm and '' != params.xm">
        AND xi.xm LIKE '%' || #{params.xm} || '%'
        </if>
        <if test="null != params.bname and '' != params.bname">
        AND li.b_name LIKE '%' || #{params.bname} || '%'
        </if>
        <if test="null != params.sqmc and '' != params.sqmc">
        AND li.sqmc LIKE '%' || #{params.sqmc} || '%'
        </if>
    </select>

    <select id="getJzwList" resultType="org.springblade.modules.rhzf.fwInfo.vo.LdBzdzVO">
        SELECT tem.*, mph.jlxmc AS jlxxqzdzrcmc, mph.sqjcwhmc FROM (
        SELECT
        ld.xlh AS ldphxlh,
        ld.mphxlh,
        ld.mlph,
        ld.ldph,
        ld.pointx AS dzjd,
        ld.pointy AS dzwd
        FROM
        sspt.rk_ldphxx ld
        WHERE
        ld.cxbs = 0
        <if test="params.yhjh != null and params.yhjh != ''">
            AND ld.cjrjh = #{params.yhjh}
        </if>
        AND ld.pcsjgdm = #{params.gajgdm}
        ) tem
        LEFT JOIN sspt.rk_mphxx mph ON (mph.xlh = tem.mphxlh AND mph.cxbs = 0)
        ORDER BY COALESCE(NULLIF(regexp_replace( tem.ldph, '[^0-9]', '', 'g' ), '') , '0') :: int8
    </select>

    <select id="getDyList" resultType="org.springblade.modules.xxy.vo.DyVO">
        SELECT
            CASE WHEN dyh IS NULL THEN '平房' ELSE dyh END AS dyh
        FROM
            sspt.rk_dzxx
        WHERE
            ldphxlh = #{params.ldphxlh} :: int8
        AND lcnpmh IS NOT NULL
        GROUP BY
            dyh
        ORDER BY
            dyh
    </select>

    <select id="selectXxyList" resultType="org.springblade.modules.xxy.vo.XxyVO">
        SELECT
            id,
            xm,
            sfzh,
            lxfs,
            sqmc,
            zzmm,
            szlb,
            dwzw,
            zz
        FROM
            jdrh.xxy_info
        WHERE
            is_deleted = 0
        AND ld_id :: text = #{params.ldphxlh}
        AND dyh = #{params.dyh}
    </select>
</mapper>
