<?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="com.tofly.xrtymis.mapper.FinishingRateMapper">

    <select id="workOrderCount" resultType="map">
        select GDLX,gdlx_name, sum_num, ycl_num, wcl_num, round((ycl_num / nullif(sum_num, 0)) * 100, 2) rate
        from (select GDLX,
        (
        <include refid="gd_type">
            <property name="type" value="gdlx"/>
        </include>
        ) gdlx_name,
        count(*) sum_num,
        sum(case when FINISH_TIME is not null then 1 else 0 end) ycl_num,
        sum(case when FINISH_TIME is null then 1 else 0 end) wcl_num
        from TF_YWPN_GDINFO_W
        where IS_RXLX = '1'
        <if test="dto.gdlx != null and dto.gdlx.size() != 0">
            and GDLX in
            <foreach collection="dto.gdlx" item="l" open="(" separator="," close=")">
                #{l}
            </foreach>
        </if>
        <if test="dto.gdxf1 != null and dto.gdxf1.size() != 0">
            and gdxf1 in
            <foreach collection="dto.gdxf1" item="l" open="(" separator="," close=")">
                #{l}
            </foreach>
        </if>
        <if test="dto.gdxf2 != null and dto.gdxf2.size() != 0">
            and gdxf2 in
            <foreach collection="dto.gdxf2" item="l" open="(" separator="," close=")">
                #{l}
            </foreach>
        </if>
        <if test="dto.djry != null and dto.djry != ''">
            and djry = #{dto.djry}
        </if>
        <choose>
            <when test="dto.start != null and dto.end != null">
                and to_char(djsj,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        group by GDLX)
    </select>

    <select id="workOrderInfo" resultType="map">
        select i.id,
        i.gdbh,
        (
        <include refid="gd_type">
            <property name="type" value="i.gdlx"/>
        </include>
        ) gdlx_name,
        i.gdlx,
        (
        <include refid="gd_type">
            <property name="type" value="i.gdxf1"/>
        </include>
        ) gdxf1_name,
        i.gdxf1,
        (
        <include refid="gd_type">
            <property name="type" value="i.gdxf2"/>
        </include>
        ) gdxf2_name,
        i.gdxf2,
        i.gdms,
        decode(jjcd,0,'一般',1,'重要','紧急') yxjb,
        i.ldhm,
        i.slbm,
        i.slry,
        i.sjdh,
        i.djry,
        (
        <include refid="user_query">
            <property name="user" value="i.djry"/>
        </include>
        ) djry_name,
        (<include refid="dept_query"><property name="dept" value="i.slbm"/></include> ) clbm_name,
        (<include refid="user_query"><property name="user" value="i.slry"/></include> ) clry_name,
        i.djsj,
        i.khbh,
        i.JDSX,
        i.djsj pdsj,
        i.khxm,
        i.khdz,
        i.lxdh,
        r.return_user,
        (
        <include refid="user_query">
            <property name="user" value="r.return_user"/>
        </include>
        ) return_user_name,
        r.return_time
        from TF_YWPN_GDINFO_W i left join
        TF_YWPN_GDRETURN_W r on i.GDBH = r.GDBH
        where IS_RXLX = '1'
        <if test="dto.gdlx != null and dto.gdlx.size() != 0">
            and GDLX in
            <foreach collection="dto.gdlx" item="l" open="(" separator="," close=")">
                #{l}
            </foreach>
        </if>
        <if test="dto.gdxf1 != null and dto.gdxf1.size() != 0">
            and gdxf1 in
            <foreach collection="dto.gdxf1" item="l" open="(" separator="," close=")">
                #{l}
            </foreach>
        </if>
        <if test="dto.gdxf2 != null and dto.gdxf2.size() != 0">
            and gdxf2 in
            <foreach collection="dto.gdxf2" item="l" open="(" separator="," close=")">
                #{l}
            </foreach>
        </if>
        <if test="dto.djry != null and dto.djry != ''">
            and djry = #{dto.djry}
        </if>
        <choose>
            <when test="dto.start != null and dto.end != null">
                and to_char(djsj,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        <choose>
            <when test="dto.state == 1">
                and FINISH_TIME is not null
            </when>
            <when test="dto.state == 2">
                and FINISH_TIME is null
            </when>
        </choose>
    </select>

    <sql id="dept_query">
        select name
        from TF_SMPN_DEPARTMENT_X
        where id = #{dept}
    </sql>

    <sql id="user_query">
        select REAL_NAME
        from TF_SMPN_USER_X
        where id = ${user}
    </sql>

    <sql id="gd_type">
        select GDLX_MC
        from TF_YWPN_GDGLTYPEBM_W
        where id = ${type}
    </sql>

</mapper>