<?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.nantian.cloud.jljc.mapper.TDualcontrolProgressApplyMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.nantian.cloud.jljc.entity.TDualcontrolProgressApply">
        <id column="id" property="id" />
        <result column="progress_id" property="progressId" />
        <result column="modify_user_id" property="modifyUserId" />
        <result column="modify_user_name" property="modifyUserName" />
        <result column="modify_time" property="modifyTime" />
        <result column="modify_comments" property="modifyComments" />
        <result column="audit_result" property="auditResult" />
        <result column="audit_user_id" property="auditUserId" />
        <result column="audit_user_name" property="auditUserName" />
        <result column="audit_comments" property="auditComments" />
        <result column="year" property="year" />
        <result column="quarter" property="quarter" />
        <result column="item_code" property="itemCode" />
        <result column="item_name" property="itemName" />
        <result column="gdp_total" property="gdpTotal" />
        <result column="energy_total" property="energyTotal" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, progress_id, modify_user_id, audit_user_name, modify_time, modify_comments, audit_result, audit_user_id, audit_user_name, audit_time, audit_comments
    </sql>
    <select id="pageByScope" resultType="com.nantian.cloud.jljc.entity.TDualcontrolProgressApply">
        select a.id, a.progress_id, a.modify_user_id, a.modify_user_name, a.modify_time, a.modify_comments, a.audit_result, a.audit_user_id, a.audit_user_name, a.audit_time, a.audit_comments,
               p.year, p.quarter, p.item_code, p.item_name, p.gdp_total, p.energy_total
        from t_dualcontrol_progress_apply a
        left join t_dualcontrol_progress p on a.progress_id = p.id
        where 1=1 and a.audit_result != 3
        <if test="query.year != null and query.year != ''">
            and p.year = #{query.year}
        </if>
        <if test="query.quarter != null and query.quarter != ''">
            and p.quarter = #{query.quarter}
        </if>
        <if test="query.itemName != null and query.itemName != ''">
            and p.item_name like concat('%',#{query.itemName},'%')
        </if>
        <if test="query.auditResult != null and query.auditResult != ''">
            <choose>
                <when test=" query.auditResult=='0'.toString() ">
                    and a.audit_result = '0'
                </when>
                <otherwise>
                    and a.audit_result != '0'
                </otherwise>
            </choose>
        </if>
        <choose>
            <when test=' deptId.contains("0000") '>
                and p.item_code like concat(#{query.itemCode},'%') and right(p.item_code,2) = '00'
            </when>
            <otherwise>
                and p.item_code like concat(#{query.itemCode},'%') and p.item_code != #{deptId}
            </otherwise>
        </choose>
        order by a.id desc
    </select>
    <select id="countByScope" resultType="java.util.Map">
        SELECT IFNULL(count(a1.id),0) amount, IFNULL(count(a2.id),0) noAudit
        from t_dualcontrol_progress p
        left join t_dualcontrol_progress_apply a1 on a1.progress_id = p.id and a1.audit_result != 3
        left join t_dualcontrol_progress_apply a2 on a2.progress_id = p.id and a2.audit_result = 0
        <if test="query.year != null and query.year != ''">
            and p.year = #{query.year}
        </if>
        <if test="query.quarter != null and query.quarter != ''">
            and p.quarter = #{query.quarter}
        </if>
        <if test="query.itemName != null and query.itemName != ''">
            and p.item_name like concat('%',#{query.itemName},'%')
        </if>
        <choose>
            <when test=' deptId.contains("0000") '>
                and p.item_code like concat(#{query.itemCode},'%') and right(p.item_code,2) = '00'
            </when>
            <otherwise>
                and p.item_code like concat(#{query.itemCode},'%')  and p.item_code != #{deptId}
            </otherwise>
        </choose>
    </select>
<!--    <select id="countByScope" resultType="java.lang.Integer">
        select count(1)
        from t_dualcontrol_progress_apply a
        left join t_dualcontrol_progress p on a.progress_id = p.id
        where a.audit_result != 3
        <if test="query.year != null and query.year != ''">
            and p.year = #{query.year}
        </if>
        <if test="query.quarter != null and query.quarter != ''">
            and p.quarter = #{query.quarter}
        </if>
        <if test="query.itemName != null and query.itemName != ''">
            and p.item_name like concat('%',#{query.itemName},'%')
        </if>
        <choose>
            <when test=' deptId.contains("0000") '>
                and p.item_code like concat(#{query.itemCode},'%') and right(p.item_code,2) = '00'
            </when>
            <otherwise>
                and p.item_code like concat(#{query.itemCode},'%')  and p.item_code != #{deptId}
            </otherwise>
        </choose>
        union
        select count(1)
        from t_dualcontrol_progress_apply a
        left join t_dualcontrol_progress p on a.progress_id = p.id
        where a.audit_result = 0
        <if test="query.year != null and query.year != ''">
            and p.year = #{query.year}
        </if>
        <if test="query.quarter != null and query.quarter != ''">
            and p.quarter = #{query.quarter}
        </if>
        <if test="query.itemName != null and query.itemName != ''">
            and p.item_name like concat('%',#{query.itemName},'%')
        </if>
        <choose>
            <when test=' deptId.contains("0000") '>
                and p.item_code like concat(#{query.itemCode},'%') and right(p.item_code,2) = '00'
            </when>
            <otherwise>
                and p.item_code like concat(#{query.itemCode},'%')  and p.item_code != #{deptId}
            </otherwise>
        </choose>
    </select>-->
    <select id="ownApply" resultType="com.nantian.cloud.jljc.entity.TDualcontrolProgressApply">
        select a.id, a.progress_id, a.modify_user_id, a.modify_user_name, a.modify_time, a.modify_comments, a.audit_result, a.audit_user_id, a.audit_user_name, a.audit_time, a.audit_comments,
               p.year, p.quarter, p.item_code, p.item_name, p.gdp_total, p.energy_total
        from t_dualcontrol_progress_apply a
        left join t_dualcontrol_progress p on a.progress_id = p.id
        where 1=1 and a.audit_result != 3 and p.item_code = #{deptId}
        <if test="query.year != null and query.year != ''">
            and p.year = #{query.year}
        </if>
        <if test="query.quarter != null and query.quarter != ''">
            and p.quarter = #{query.quarter}
        </if>
        order by a.id desc
    </select>

</mapper>
