<?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.log.sjSp.mapper.SjSpMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="sjSpResultMap" type="org.springblade.modules.log.sjSp.entity.SjSpEntity">
        <result column="id" property="id"/>
        <result column="ywlb" property="ywlb"/>
        <result column="ywid" property="ywid"/>
        <result column="params" property="params"/>
        <result column="qqyy" property="qqyy"/>
        <result column="spjg" property="spjg"/>
        <result column="spyy" property="spyy"/>
        <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="selectSjSpPage" resultType="org.springblade.modules.log.sjSp.vo.SjSpVO">
        SELECT
            sp.id,
            sp.ywlb,
            sp.ywid,
            sp.params,
            sp.qqyy,
            sp.spjg,
            sp.spyy,
            sqr.name,
            pcs.dept_name AS pcsName,
            sp.create_user,
            sp.create_time,
            sp.update_user,
            sp.update_time,
            ci.id AS csId,
            ci.csmc,
            ci.yyzt,
            md.jfmc,
            md.id AS mdId
        FROM
            log.sj_sp sp
        LEFT JOIN jdrh.cs_info ci ON (ci.id = sp.ywid AND sp.ywlb IN (1, 2))
        LEFT JOIN jdrh.mdjf_dj md ON (md.id = sp.ywid AND sp.ywlb = 3)
        LEFT JOIN jdrh.blade_user sqr ON sqr.id = sp.create_user
        LEFT JOIN jdrh.blade_dept sq ON sq.id = sp.create_dept
        LEFT JOIN jdrh.blade_dept pcs ON pcs.id = sq.parent_id
        LEFT JOIN jdrh.blade_dept fj ON fj.id = pcs.parent_id
        WHERE
            (sq.parent_id = #{params.deptId} :: int8 OR pcs.parent_id = #{params.deptId} :: int8)
          AND sp.is_deleted = 0
          AND sp.spjg = 0
        <if test="null != params.type and 1 == params.type">
            AND sp.ywlb IN (1, 2)
        </if>
        <if test="null != params.type and 2 == params.type">
            AND sp.ywlb = 3
        </if>
        <if test="null != params.ywlb and '' != params.ywlb">
            AND sp.ywlb = #{params.ywlb} :: int8
        </if>
        ORDER BY sp.create_time DESC
    </select>

    <select id="exportSjSp" resultType="org.springblade.modules.log.sjSp.excel.SjSpExcel">
        SELECT * FROM sj_sp ${ew.customSqlSegment}
    </select>

    <select id="getIsExist" resultType="int">
        SELECT
            COUNT(1)
        FROM
            log.sj_sp
        WHERE
            is_deleted = 0
          AND spjg = 0
          AND ywlb = #{ywlb}
          AND ywid = #{id} :: int8
    </select>

    <select id="selectSjSpPageToPt" resultType="org.springblade.modules.log.sjSp.vo.SjSpVO">
        SELECT
        sp.id,
        sp.ywlb,
        sp.ywid,
        sp.params,
        sp.qqyy,
        sp.spjg,
        sp.spyy,
        sqr.name,
        case when sp.spjg = 0 then null
        when sp.spjg != 0 then spr.name end as sprName,
        pcs.dept_name AS pcsName,
        sp.create_user,
        sp.create_time,
        sp.update_user,
        sp.update_time,
        ci.csmc,
        md.jfmc,
        md.id AS mdId
        FROM
        log.sj_sp sp
        LEFT JOIN jdrh.cs_info ci ON (ci.id = sp.ywid AND sp.ywlb IN (1, 2))
        LEFT JOIN jdrh.mdjf_dj md ON (md.id = sp.ywid AND sp.ywlb = 3)
        LEFT JOIN jdrh.blade_user sqr ON sqr.id = sp.create_user
        LEFT JOIN jdrh.blade_user spr ON spr.id = sp.update_user
        LEFT JOIN jdrh.blade_dept sq ON sq.id = sp.create_dept
        LEFT JOIN jdrh.blade_dept pcs ON pcs.id = sq.parent_id
        LEFT JOIN jdrh.blade_dept fj ON fj.id = pcs.parent_id
        LEFT JOIN jdrh.blade_dept sj ON sj.id = fj.parent_id
        WHERE
        sp.is_deleted = 0
        AND (sq.ancestors like '%' || #{params.deptId} || '%')
        <if test="null != params.type and 1 == params.type">
            AND sp.ywlb IN (1, 2)
        </if>
        <if test="null != params.type and 2 == params.type">
            AND sp.ywlb = 3
        </if>
        <if test="null != params.ywlb and '' != params.ywlb">
            AND sp.ywlb = #{params.ywlb} :: int8
        </if>
        <if test="null != params.spjg and '' != params.spjg">
            AND sp.spjg :: text = #{params.spjg} :: text
        </if>
        <if test="params.sssjBm != null and params.sssjBm != ''">
            and sj.id :: text = #{params.sssjBm}
        </if>
        <if test="params.ssfjBm != null and params.ssfjBm != ''">
            and fj.id :: text = #{params.ssfjBm}
        </if>
        <if test="params.sspcsBm != null and params.sspcsBm != ''">
            and pcs.id :: text = #{params.sspcsBm}
        </if>
        ORDER BY sp.create_time DESC
    </select>
</mapper>
