<?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.JlrhJws.mapper.JlrhJwsMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="JlrhJwsResultMap" type="org.springblade.modules.JlrhJws.entity.JlrhJwsEntity">
        <result column="id" property="id"/>
        <result column="ssbm" property="ssbm"/>
        <result column="jwsmc" property="jwsmc"/>
        <result column="szlb" property="szlb"/>
        <result column="jwslx" property="jwslx"/>
        <result column="szd" property="szd"/>
        <result column="glmj" property="glmj"/>
        <result column="glmjdh" property="glmjdh"/>
        <result column="jwzzrr" property="jwzzrr"/>
        <result column="sfjwzl" property="sfjwzl"/>
        <result column="zrrlxdh" property="zrrlxdh"/>
        <result column="jwzzp" property="jwzzp"/>
        <result column="jwzjd" property="jwzjd"/>
        <result column="jwzwd" property="jwzwd"/>
        <result column="jwzdz" property="jwzdz"/>
        <result column="jwzjj" property="jwzjj"/>
        <result column="create_user" property="createUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>


    <select id="selectJlrhJwsPage" resultType="org.springblade.modules.JlrhJws.vo.JlrhJwsVO">
        SELECT
            jj.id,
            fj.dept_name AS ssfj,
            fj.id AS ssfjBm,
            pcs.dept_name AS ssbm,
            pcs.id AS ssbmBm,
            jj.sssq,
            jj.sssq_bm,
            jj.jwsmc,
            jj.szlb,
            jj.jwslx,
            jj.szd,
            jj.glmj,
            jj.glmjdh,
            jj.jwzzrr,
            jj.sfjwzl,
            jj.zrrlxdh,
            jj.jwzjd,
            jj.jwzwd,
            jj.jwzdz,
            jj.jwzjj,
            jj.dt_jd,
            jj.dt_wd,
            jj.dt_dz
        FROM
            jlrh_jws jj
        LEFT JOIN blade_dept bd ON bd.id = jj.sssq_bm
        LEFT JOIN blade_dept pcs ON pcs.id = bd.parent_id
        LEFT JOIN blade_dept fj ON fj.id = pcs.parent_id
        WHERE
            ((#{params.deptId} LIKE '%' || bd.id || '%') OR (bd.ancestors LIKE '%' || #{params.deptId} || '%')) and jj.is_deleted = 0
        <if test="null != params.sssjBm and '' != params.sssjBm">
            AND fj.parent_id :: text = #{params.sssjBm}
        </if>
        <if test="null != params.sspcsBm and '' != params.sspcsBm">
            AND pcs.id :: text = #{params.sspcsBm}
        </if>
        <if test="null != params.ssbm and '' != params.ssbm">
            AND pcs.dept_name like concat( '%',#{params.ssbm},'%')
        </if>
        <if test="null != params.jwsmc and '' != params.jwsmc">
            AND jj.jwsmc like concat( '%',#{params.jwsmc},'%')
        </if>
        <if test="null != params.ssfjBm and '' != params.ssfjBm">
            AND fj.id :: text = #{params.ssfjBm}
        </if>
        <if test="null != params.ssbmBm and '' != params.ssbmBm">
            AND pcs.id :: text = #{params.ssbmBm}
        </if>
        <if test="null != params.sssqBm and '' != params.sssqBm">
            AND jj.sssq_bm :: text = #{params.sssqBm}
        </if>
        <if test="null != params.szlb and '' != params.szlb">
            AND jj.szlb :: text = #{params.szlb}
        </if>
        <if test="null != params.jwslx and '' != params.jwslx">
            AND jj.jwslx :: text = #{params.jwslx}
        </if>
        <if test="null != params.glmj and '' != params.glmj">
            AND jj.glmj :: text like concat('%',#{params.glmj},'%')
        </if>
        <if test="null != params.timeBegin and '' != params.timeBegin and null != params.timeEnd and '' != params.timeEnd">
            AND jj.create_time :: text BETWEEN #{params.timeBegin} AND #{params.timeEnd}
        </if>
        ORDER BY jj.create_time DESC
    </select>

    <select id="getDetail" resultType="org.springblade.modules.JlrhJws.vo.JlrhJwsVO">
        SELECT
            jj.id,
            sj.dept_name AS sssj,
            bd.dept_name AS sssq,
            fj.dept_name AS ssfj,
            fj.id AS ssfjBm,
            pcs.dept_name AS ssbm,
            pcs.id AS ssbmBm,
            jj.sssq,
            jj.sssq_bm,
            jj.jwsmc,
            jj.szlb,
            jj.jwslx,
            jj.szd,
            jj.glmj,
            jj.glmjdh,
            jj.jwzzrr,
            jj.sfjwzl,
            jj.zrrlxdh,
            jj.jwzjd,
            jj.jwzwd,
            jj.jwzdz,
            jj.jwzjj,
            jj.dt_jd,
            jj.dt_wd,
            jj.dt_dz,
            jj.jwzzp
        FROM
            jlrh_jws jj
        LEFT JOIN blade_dept bd ON bd.id = jj.sssq_bm
        LEFT JOIN blade_dept pcs ON pcs.id = bd.parent_id
        LEFT JOIN blade_dept fj ON fj.id = pcs.parent_id
        LEFT JOIN blade_dept sj ON sj.id = fj.parent_id
        WHERE jj.is_deleted = 0
            AND jj.id = #{id}
    </select>

    <select id="exportJlrhJws" resultType="org.springblade.modules.JlrhJws.excel.JlrhJwsExcel">
        SELECT
            jj.id,
            fj.dept_name AS ssfj,
            fj.id AS ssfjBm,
            pcs.dept_name AS ssbm,
            pcs.id AS ssbmBm,
            jj.sssq,
            jj.sssq_bm,
            jj.jwsmc,
            jj.szlb,
            jj.jwslx,
            jj.szd,
            jj.glmj,
            jj.glmjdh,
            jj.jwzzrr,
            jj.sfjwzl,
            jj.zrrlxdh,
            jj.jwzjd,
            jj.jwzwd,
            jj.jwzdz,
            jj.jwzjj,
            jj.dt_jd,
            jj.dt_wd,
            jj.dt_dz
        FROM
            jlrh_jws jj
        LEFT JOIN blade_dept bd ON bd.id = jj.sssq_bm
        LEFT JOIN blade_dept pcs ON pcs.id = bd.parent_id
        LEFT JOIN blade_dept fj ON fj.id = pcs.parent_id
        WHERE
        ((#{params.deptId} LIKE '%' || bd.id || '%') OR (bd.ancestors LIKE '%' || #{params.deptId} || '%')) and jj.is_deleted = 0
        <if test="null != params.ssbm and '' != params.ssbm">
            AND pcs.dept_name like concat( '%',#{params.ssbm},'%')
        </if>
        <if test="null != params.jwsmc and '' != params.jwsmc">
            AND jj.jwsmc like concat( '%',#{params.jwsmc},'%')
        </if>
        <if test="null != params.ssfjBm and '' != params.ssfjBm">
            AND fj.id :: text = #{params.ssfjBm}
        </if>
        <if test="null != params.ssbmBm and '' != params.ssbmBm">
            AND pcs.id :: text = #{params.ssbmBm}
        </if>
        <if test="null != params.sssqBm and '' != params.sssqBm">
            AND jj.sssq_bm :: text = #{params.sssqBm}
        </if>
        <if test="null != params.szlb and '' != params.szlb">
            AND jj.szlb :: text = #{params.szlb}
        </if>
        <if test="null != params.jwslx and '' != params.jwslx">
            AND jj.jwslx :: text = #{params.jwslx}
        </if>
        <if test="null != params.glmj and '' != params.glmj">
            AND jj.glmj :: text like concat('%',#{params.glmj},'%')
        </if>
        <if test="null != params.timeBegin and '' != params.timeBegin and null != params.timeEnd and '' != params.timeEnd">
            AND jj.create_time :: text BETWEEN #{params.timeBegin} AND #{params.timeEnd}
        </if>
        ORDER BY jj.create_time DESC
    </select>

    <select id="getDkfw" resultType="org.springblade.modules.JlrhJws.vo.JlrhJwsVO">
        SELECT
            jws.jwsmc,
            jws.jwzjd,
            jws.jwzwd,
            jws.dt_jd,
            jws.dt_wd,
            jws.dkfw
        FROM
            jlrh_jws jws
        LEFT JOIN blade_dept sq ON sq.ID = jws.sssq_bm
        WHERE
            jws.szlb = '警务室'
        AND jws.is_deleted = 0
        AND (sq.id :: TEXT = #{deptId}
        OR sq.ancestors LIKE '%' || #{deptId} || '%')
    </select>

    <select id="getPcsJwd" resultType="org.springblade.modules.JlrhJws.vo.JlrhJwsVO">
        SELECT
            pj.dept_name AS jwsmc,
            pj.jd AS jwzjd,
            pj.wd AS jwzwd,
            pj.dkfw
        FROM
            blade_dept sq
        LEFT JOIN blade_dept pcs ON sq.ancestors LIKE '%' || pcs.ID || '%' AND pcs.dept_category = 6
        LEFT JOIN pcs_jwd pj ON pj.dept_id = pcs.id
        WHERE
            sq.id :: text = #{deptId}
    </select>

    <select id="selectJwsPcs" resultMap="JlrhJwsResultMap">
        select t2.dept_name,t2.id,count(0) as jwssl from
        ( select a.*,(case when b.dept_category = 6 then b.id else b.parent_id end) from jdrh.jlrh_jws a,blade_dept b
        where a.sssq_bm = b.id and a.is_deleted = 0)t1,blade_dept t2 where t1.parent_id = t2.id and t2.ancestors like '%' || #{deptId} || '%'
        <if test="jlrhJws.deptName != null and jlrhJws.deptName != ''">
            and t2.dept_name like '%' || #{jlrhJws.deptName} || '%'
        </if>
        group by t2.dept_name,t2.id
        order by jwssl desc
    </select>

    <select id="selectJwsSq" resultMap="JlrhJwsResultMap">
        SELECT b.dept_name,b.id,COUNT(0) AS jwssl FROM jdrh.jlrh_jws a
        INNER JOIN blade_dept b ON a.sssq_bm = b.id
        WHERE a.is_deleted = 0 AND b.dept_category = '7' AND b.parent_id = #{id}
        GROUP BY b.dept_name,b.id
        ORDER BY jwssl desc
    </select>

    <select id="selectJwsxx" resultMap="JlrhJwsResultMap">
        SELECT
            jj.id,
            fj.dept_name AS ssfj,
            fj.id AS ssfjBm,
            pcs.dept_name AS ssbm,
            pcs.id AS ssbmBm,
            jj.sssq,
            jj.sssq_bm,
            jj.jwsmc,
            jj.szlb,
            jj.jwslx,
            jj.szd,
            jj.glmj,
            jj.glmjdh,
            jj.jwzzrr,
            jj.sfjwzl,
            jj.zrrlxdh,
            jj.jwzjd,
            jj.jwzwd,
            jj.jwzdz,
            jj.jwzjj,
            jj.dt_jd,
            jj.dt_wd,
            jj.dt_dz
        FROM
            jlrh_jws jj
        LEFT JOIN blade_dept bd ON bd.id = jj.sssq_bm
        LEFT JOIN blade_dept pcs ON pcs.id = bd.parent_id
        LEFT JOIN blade_dept fj ON fj.id = pcs.parent_id
        WHERE (jj.sssq_bm = #{jlrhJws.id} or bd.parent_id = #{jlrhJws.id}) and jj.is_deleted = 0
    </select>

    <select id="selectSqByDeptname" resultMap="JlrhJwsResultMap">
        SELECT b.dept_name,b.id,COUNT(0) AS jwssl FROM jdrh.jlrh_jws a
        INNER JOIN blade_dept b ON a.sssq_bm = b.id
        WHERE a.is_deleted = 0 AND b.dept_category = '7' and b.ancestors like '%' || #{deptId} || '%'
        <if test="jlrhJws.deptName != null and jlrhJws.deptName != '' ">
            AND b.dept_name like '%' || #{jlrhJws.deptName} || '%'
        </if>
        GROUP BY b.dept_name,b.id
        ORDER BY jwssl desc
    </select>

    <select id="getJwsSl" parameterType="java.lang.Long" resultType="int">
        SELECT
            COUNT(id)
        FROM
            jdrh.jlrh_jws
        WHERE
            create_user = #{userId}
          AND is_deleted = 0
    </select>
</mapper>
