<?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.yjjl.mapper.YjjlMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="yjjlResultMap" type="org.springblade.modules.yjjl.entity.YjjlEntity">
    </resultMap>


    <select id="selectYjjlPage" resultMap="yjjlResultMap">
        select * from csjc_yjjl where is_deleted = 0
    </select>


    <select id="exportYjjl" resultType="org.springblade.modules.yjjl.excel.YjjlExcel">
        SELECT * FROM csjc_yjjl ${ew.customSqlSegment}
    </select>

    <select id="getJdDept" resultType="org.springblade.modules.system.vo.DeptListVo">
        SELECT
            id AS value,
            dept_name AS label
        FROM
            blade_dept
        WHERE
            ancestors :: text LIKE '%' || #{params.deptId} || '%'
          AND dept_category :: text = #{params.deptCategory}
    </select>

    <select id="getHzXfDept" resultType="org.springblade.modules.system.vo.DeptListVo">
        SELECT
            xf.id AS value,
            xf.dept_name AS label
        FROM
            blade_dept fj
            LEFT JOIN blade_dept xf ON (xf.parent_id = fj.parent_id AND xf.dept_category :: text = #{params.deptCategory})
        WHERE
            fj.id :: text = #{params.deptId}
    </select>

    <select id="getFjDept" resultType="org.springblade.modules.system.vo.DeptListVo">
        SELECT
            fj.id AS value,
            fj.dept_name AS label
        FROM
            blade_dept pcs
            LEFT JOIN blade_dept fj ON fj.id = pcs.parent_id
        WHERE
            pcs.id :: text = #{params.deptId}
    </select>

    <select id="getXqxjw" resultType="org.springblade.modules.system.vo.DeptListVo">
        SELECT
            xqxjw.id AS value,
	        xqxjw.dept_name AS label
        FROM
            blade_dept jdxjw
            LEFT JOIN blade_dept pcs ON pcs.id = jdxjw.parent_id
            LEFT JOIN blade_dept xqxjw ON ( xqxjw.ancestors = pcs.ancestors AND xqxjw.dept_category :: text = #{params.deptCategory} )
        WHERE
            jdxjw.id :: text = #{params.deptId}
    </select>

    <select id="getYjjlPage" resultType="org.springblade.modules.yjjl.vo.YjjlVO">
        SELECT
            *
        FROM
            (
                SELECT
                    ROW_NUMBER () OVER ( PARTITION BY cy.csjc_id ORDER BY cy.create_time DESC ) maxRow,
                    jc.id,
                    jc.bgsy,
                    jc.zgzt,
                    cy.yjdw,
                    cy.bz,
                    cy.create_time,
                    ci.csmc,
                    ci.dwlb,
                    pcs.dept_name as jcdw,
                    bu.real_name,
                    bu.phone,
                    sq.dept_name as deptId,
                    jc.fjlzzt,
                    jc.xjwlzzt,
                    ( SELECT cy2.zt FROM csjc_yjjl cy2 WHERE cy2.csjc_id = cy.csjc_id AND cy2.create_dept :: text = #{params.deptId} ORDER BY cy2.create_time DESC LIMIT 1 ) AS yjzt
                FROM
                    csjc_yjjl cy
                LEFT JOIN csjc_info jc ON jc.id = cy.csjc_id
                LEFT JOIN cs_info ci ON ci.id = jc.cs_id
                LEFT JOIN blade_user bu ON bu.id = jc.create_user
                LEFT JOIN blade_dept sq ON sq.id = ci.sssq_bm
                LEFT JOIN blade_dept pcs ON pcs.id = sq.parent_id
                WHERE cy.yjdw :: text = #{params.deptId} AND cy.is_deleted = 0
        <if test="null != params.fjlzzt and '' != params.fjlzzt">
            AND jc.fjlzzt :: text = #{params.fjlzzt}
        </if>
        <if test="null != params.xjwlzzt and '' != params.xjwlzzt">
            AND jc.xjwlzzt :: text = #{params.xjwlzzt}
        </if>
            ) temp
        WHERE
            temp.maxRow = 1 order by create_time desc
    </select>

    <select id="getHistoryById" resultType="org.springblade.modules.yjjl.vo.YjjlVO">
        SELECT
            cy.create_time,
            cy.bz,
            cjdw.dept_name AS createDept,
            CASE
                cy.zt
                WHEN 1 THEN
                    '已移交'
                WHEN 2 THEN
                    '已退回'
                WHEN 3 THEN
                    '已完成'
                WHEN 4 THEN
                    '已下发'
                WHEN 5 THEN
                    '已签收'
                END AS zt,
            yjdw.dept_name AS yjdw,
            cj.bgsy
        FROM
            csjc_yjjl cy
        LEFT JOIN csjc_info cj ON cy.csjc_id = cj.yjdw
        LEFT JOIN blade_dept cjdw ON cjdw.ID = cy.create_dept
        LEFT JOIN blade_dept yjdw ON yjdw.ID = cy.yjdw
        WHERE
            cy.csjc_id :: text = #{params.id}
        AND cy.type :: text = #{params.type}
        ORDER BY
            cy.create_time DESC
    </select>

    <select id="selectBgsy" resultType="org.springblade.modules.yjjl.vo.YjjlVO">
        select bgsy from csjc_info where id :: text = #{id}
    </select>

    <select id="getXfDept" resultType="org.springblade.modules.system.vo.DeptListVo">
        SELECT
            xf.id AS value,
            xf.dept_name AS label
        FROM
            blade_dept fj
            LEFT JOIN blade_dept xf ON (xf.parent_id = fj.parent_id AND xf.dept_category :: text = #{params.deptCategory})
        WHERE
            fj.id :: text = #{params.deptId}
    </select>
</mapper>
