<?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.qianhe.mapper.SjLhscMapper">
    
    <resultMap type="SjLhsc" id="SjLhscResult">
        <result property="id"    column="id"    />
        <result property="hymc"    column="hymc"    />
        <result property="hyrq"    column="hyrq"    />
        <result property="deptId"    column="dept_id"    />
        <result property="hydd"    column="hydd"    />
        <result property="chry"    column="chry"    />
        <result property="yxqk"    column="yxqk"    />
        <result property="jjwt"    column="jjwt"    />
        <result property="czwt"    column="czwt"    />
        <result property="tgjy"    column="tgjy"    />
        <result property="ldyq"    column="ldyq"    />
        <result property="zt"    column="zt"    />
        <result property="dwjb"    column="dwjb"    />
        <result property="tjr"    column="tjr"    />
        <result property="tjrq"    column="tjrq"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="yl1"    column="yl1"    />
        <result property="yl2"    column="yl2"    />
        <result property="yl3"    column="yl3"    />
        <result property="yl4"    column="yl4"    />
        <result property="yl5"    column="yl5"    />
        <result property="deptName"    column="dept_name"    />
        <result property="parentId"   column="parent_id"   />
    </resultMap>

    <resultMap id="SjLhscSjLhscWtResult" type="SjLhsc" extends="SjLhscResult">
        <collection property="sjLhscWtList" notNullColumn="sub_id" javaType="java.util.List" resultMap="SjLhscWtResult" />
    </resultMap>

    <resultMap type="SjLhscWt" id="SjLhscWtResult">
        <result property="id"    column="id"    />
        <result property="lhscId"    column="lhsc_id"    />
        <result property="wt"    column="wt"    />
        <result property="remark"    column="remark"    />
        <result property="yl1"    column="yl1"    />
        <result property="yl2"    column="yl2"    />
        <result property="yl3"    column="yl3"    />
        <result property="yl4"    column="yl4"    />
        <result property="yl5"    column="yl5"    />
    </resultMap>

    <sql id="selectSjLhscVo">
        select id, hymc, hyrq,
               a.dept_id, hydd, chry,
               yxqk, jjwt, czwt, tgjy,
               ldyq, zt, a.dwjb, tjr, tjrq,
               a.create_by, a.create_time, a.update_by,
               a.update_time, remark, yl1, yl2, yl3, yl4, yl5,d.dept_name
        from sj_lhsc a
        left join  sys_dept d on a.dept_id=d.dept_id
    </sql>

    <select id="selectSjLhscList" parameterType="SjLhsc" resultMap="SjLhscResult">
        <include refid="selectSjLhscVo"/>
        <where>  
            <if test="hymc != null  and hymc != ''"> and hymc like concat('%', #{hymc}, '%')</if>
            <if test="hyrq != null  and hyrq != ''"> and hyrq = #{hyrq}</if>
            <if test="deptId != null  and deptId != ''"> and a.dept_id = #{deptId}</if>
            <if test="hydd != null  and hydd != ''"> and hydd = #{hydd}</if>
            <if test="chry != null  and chry != ''"> and chry = #{chry}</if>
            <if test="yxqk != null  and yxqk != ''"> and yxqk = #{yxqk}</if>
            <if test="jjwt != null  and jjwt != ''"> and jjwt = #{jjwt}</if>
            <if test="czwt != null  and czwt != ''"> and czwt = #{czwt}</if>
            <if test="tgjy != null  and tgjy != ''"> and tgjy = #{tgjy}</if>
            <if test="ldyq != null  and ldyq != ''"> and ldyq = #{ldyq}</if>
            <if test="zt != null  and zt != ''"> and zt = #{zt}</if>
            <if test="dwjb != null  and dwjb != ''"> and a.dwjb = #{dwjb}</if>
            <if test="tjr != null  and tjr != ''"> and tjr = #{tjr}</if>
            <if test="tjrq != null  and tjrq != ''"> and tjrq = #{tjrq}</if>
            <if test="yl1 != null  and yl1 != ''"> and yl1 = #{yl1}</if>
            <if test="yl2 != null  and yl2 != ''"> and yl2 = #{yl2}</if>
            <if test="yl3 != null  and yl3 != ''"> and yl3 = #{yl3}</if>
            <if test="yl4 != null  and yl4 != ''"> and yl4 = #{yl4}</if>
            <if test="yl5 != null  and yl5 != ''"> and yl5 = #{yl5}</if>
            <if test="startJd !=null and startJd !=''"> and DATE_FORMAT(hyrq,'%Y-%m')>=#{startJd}  </if>
            <if test="endJd !=null and endJd !=''"> and DATE_FORMAT(hyrq,'%Y-%m')&lt;=#{endJd}  </if>

            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
        order by  hyrq desc
    </select>
    <select id="selectSjLhscLhgxList" parameterType="SjLhsc" resultMap="SjLhscResult">
        <include refid="selectSjLhscVo"/>
        <where>
            <if test="hymc != null  and hymc != ''"> and hymc like concat('%', #{hymc}, '%')</if>
            <if test="hyrq != null  and hyrq != ''"> and hyrq = #{hyrq}</if>
            <if test="deptId != null  and deptId != ''"> and a.dept_id = #{deptId}</if>
            <if test="hydd != null  and hydd != ''"> and hydd = #{hydd}</if>
            <if test="chry != null  and chry != ''"> and chry = #{chry}</if>
            <if test="yxqk != null  and yxqk != ''"> and yxqk = #{yxqk}</if>
            <if test="jjwt != null  and jjwt != ''"> and jjwt = #{jjwt}</if>
            <if test="czwt != null  and czwt != ''"> and czwt = #{czwt}</if>
            <if test="tgjy != null  and tgjy != ''"> and tgjy = #{tgjy}</if>
            <if test="ldyq != null  and ldyq != ''"> and ldyq = #{ldyq}</if>
            <if test="zt != null  and zt != ''"> and zt = #{zt}</if>
            <if test="dwjb != null  and dwjb != ''"> and a.dwjb = #{dwjb}</if>
            <if test="tjr != null  and tjr != ''"> and tjr = #{tjr}</if>
            <if test="tjrq != null  and tjrq != ''"> and tjrq = #{tjrq}</if>
            <if test="yl1 != null  and yl1 != ''"> and yl1 = #{yl1}</if>
            <if test="yl2 != null  and yl2 != ''"> and yl2 = #{yl2}</if>
            <if test="yl3 != null  and yl3 != ''"> and yl3 = #{yl3}</if>
            <if test="yl4 != null  and yl4 != ''"> and yl4 = #{yl4}</if>
            <if test="yl5 != null  and yl5 != ''"> and yl5 = #{yl5}</if>
            <if test="startJd !=null and startJd !=''"> and DATE_FORMAT(hyrq,'%Y-%m')>=#{startJd}  </if>
            <if test="endJd !=null and endJd !=''"> and DATE_FORMAT(hyrq,'%Y-%m')&lt;=#{endJd}  </if>
        </where>
    </select>
    
    <select id="selectSjLhscById" parameterType="Long" resultMap="SjLhscResult">
        select a.id, a.hymc, a.hyrq, a.dept_id, a.hydd, a.chry, a.yxqk, a.jjwt, a.czwt, a.tgjy, a.ldyq, a.zt, a.dwjb, a.tjr, a.tjrq, a.create_by, a.create_time, a.update_by, a.update_time, a.remark, a.yl1, a.yl2, a.yl3, a.yl4, a.yl5,d.dept_name
        from sj_lhsc a
                 left join  sys_dept d on a.dept_id=d.dept_id
        where a.id = #{id}
    </select>
    <select id="selectSjLhscWtListById" resultMap="SjLhscWtResult">
        select *from sj_lhsc_wt where lhsc_id=#{id}
    </select>
    <select id="tjBysfsc" resultType="com.qianhe.domain.Vo.SjLhscVo">
        select d.dept_id,d.dept_name,IFNULL(a.zt, '未提交') zt from sys_dept d left join
        (
        select d.dept_id, d.dept_name, IFNULL(lh.zt, '未提交') zt
        from sys_dept d
        left join sj_lhsc lh on d.dept_id = lh.dept_id
        where d.dwjb =#{dwjb}
        <if test="deptId != null  and deptId != ''"> and  (lh.dept_id = #{deptId} or  find_in_set(#{deptId},d.ancestors))</if>
        <if test="startJd !=null and startJd !=''"> and DATE_FORMAT(hyrq,'%Y-%m')>=#{startJd}  </if>
        <if test="endJd !=null and endJd !=''"> and DATE_FORMAT(hyrq,'%Y-%m')&lt;=#{endJd}  </if>
        group by d.dept_id,d.dept_name,lh.zt
        ) a on d.dept_id=a.dept_id
        where d.dwjb = #{dwjb}
        <if test="deptId != null  and deptId != ''"> and  (d.dept_id = #{deptId} or  find_in_set(#{deptId},d.ancestors))</if>

        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>
    <select id="tjByEjJjwtSl" resultType="com.qianhe.domain.Vo.SjLhscVo">

        select d.dept_id,d.dept_name,IFNULL(jjwtsl,0) jjwtsl from sys_dept d left join
        (
        select d.dept_id,d.dept_name,count(wt.id) jjwtsl
        from sys_dept d
        left join sj_lhsc lh on d.dept_id =lh.dept_id
        left join sj_lhsc_wt wt on lh.id=wt.lhsc_id
        where d.dwjb =#{dwjb}
        <if test="deptId != null  and deptId != ''"> and  (lh.dept_id = #{deptId} or  find_in_set(#{deptId},d.ancestors))</if>
        <if test="startJd !=null and startJd !=''"> and DATE_FORMAT(hyrq,'%Y-%m')>=#{startJd}  </if>
        <if test="endJd !=null and endJd !=''"> and DATE_FORMAT(hyrq,'%Y-%m')&lt;=#{endJd}  </if>

        group by d.dept_id ,d.dept_name) a
        on d.dept_id=a.dept_id
        where d.dwjb =#{dwjb}
        <if test="deptId != null  and deptId != ''"> and  (d.dept_id = #{deptId} or  find_in_set(#{deptId},d.ancestors))</if>

        <!-- 数据范围过滤 -->
        ${params.dataScope}

    </select>



    <insert id="insertSjLhsc" parameterType="SjLhsc" useGeneratedKeys="true" keyProperty="id">
        insert into sj_lhsc
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="hymc != null">hymc,</if>
            <if test="hyrq != null">hyrq,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="hydd != null">hydd,</if>
            <if test="chry != null">chry,</if>
            <if test="yxqk != null">yxqk,</if>
            <if test="jjwt != null">jjwt,</if>
            <if test="czwt != null">czwt,</if>
            <if test="tgjy != null">tgjy,</if>
            <if test="ldyq != null">ldyq,</if>
            <if test="zt != null">zt,</if>
            <if test="dwjb != null">dwjb,</if>
            <if test="tjr != null">tjr,</if>
            <if test="tjrq != null">tjrq,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="yl1 != null">yl1,</if>
            <if test="yl2 != null">yl2,</if>
            <if test="yl3 != null">yl3,</if>
            <if test="yl4 != null">yl4,</if>
            <if test="yl5 != null">yl5,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="hymc != null">#{hymc},</if>
            <if test="hyrq != null">#{hyrq},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="hydd != null">#{hydd},</if>
            <if test="chry != null">#{chry},</if>
            <if test="yxqk != null">#{yxqk},</if>
            <if test="jjwt != null">#{jjwt},</if>
            <if test="czwt != null">#{czwt},</if>
            <if test="tgjy != null">#{tgjy},</if>
            <if test="ldyq != null">#{ldyq},</if>
            <if test="zt != null">#{zt},</if>
            <if test="dwjb != null">#{dwjb},</if>
            <if test="tjr != null">#{tjr},</if>
            <if test="tjrq != null">#{tjrq},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="yl1 != null">#{yl1},</if>
            <if test="yl2 != null">#{yl2},</if>
            <if test="yl3 != null">#{yl3},</if>
            <if test="yl4 != null">#{yl4},</if>
            <if test="yl5 != null">#{yl5},</if>
         </trim>
    </insert>

    <update id="updateSjLhsc" parameterType="SjLhsc">
        update sj_lhsc
        <trim prefix="SET" suffixOverrides=",">
            <if test="hymc != null">hymc = #{hymc},</if>
            <if test="hyrq != null">hyrq = #{hyrq},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="hydd != null">hydd = #{hydd},</if>
            <if test="chry != null">chry = #{chry},</if>
            <if test="yxqk != null">yxqk = #{yxqk},</if>
            <if test="jjwt != null">jjwt = #{jjwt},</if>
            <if test="czwt != null">czwt = #{czwt},</if>
            <if test="tgjy != null">tgjy = #{tgjy},</if>
            <if test="ldyq != null">ldyq = #{ldyq},</if>
            <if test="zt != null">zt = #{zt},</if>
            <if test="dwjb != null">dwjb = #{dwjb},</if>
            <if test="tjr != null">tjr = #{tjr},</if>
            <if test="tjrq != null">tjrq = #{tjrq},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="yl1 != null">yl1 = #{yl1},</if>
            <if test="yl2 != null">yl2 = #{yl2},</if>
            <if test="yl3 != null">yl3 = #{yl3},</if>
            <if test="yl4 != null">yl4 = #{yl4},</if>
            <if test="yl5 != null">yl5 = #{yl5},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSjLhscById" parameterType="Long">
        delete from sj_lhsc where id = #{id}
    </delete>

    <delete id="deleteSjLhscByIds" parameterType="String">
        delete from sj_lhsc where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    
    <delete id="deleteSjLhscWtByLhscIds" parameterType="String">
        delete from sj_lhsc_wt where lhsc_id in 
        <foreach item="lhscId" collection="array" open="(" separator="," close=")">
            #{lhscId}
        </foreach>
    </delete>

    <delete id="deleteSjLhscWtByLhscId" parameterType="Long">
        delete from sj_lhsc_wt where lhsc_id = #{lhscId}
    </delete>

    <insert id="batchSjLhscWt">
        insert into sj_lhsc_wt( id, lhsc_id, wt, remark, yl1, yl2, yl3, yl4, yl5) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.id}, #{item.lhscId}, #{item.wt}, #{item.remark}, #{item.yl1}, #{item.yl2}, #{item.yl3}, #{item.yl4}, #{item.yl5})
        </foreach>
    </insert>


    <select id="selectSjLhscList_main" parameterType="SjLhsc" resultMap="SjLhscResult">
        select id, hymc, hyrq,
        a.dept_id, hydd, chry,
        yxqk, jjwt, czwt, tgjy,
        ldyq, zt, a.dwjb, tjr, tjrq,
        a.create_by, a.create_time, a.update_by,
        a.update_time, remark, yl1, yl2, yl3, yl4, yl5,d.dept_name from sj_lhsc a
        left join  sys_dept d on a.dept_id=d.dept_id
        <where>
            <if test="hymc != null  and hymc != ''"> and hymc like concat('%', #{hymc}, '%')</if>
            <if test="hyrq != null  and hyrq != ''"> and hyrq = #{hyrq}</if>
            <if test="deptId != null  and deptId != ''"> and a.dept_id = #{deptId}</if>
            <if test="parentId != null and parentId != ''"> AND d.parent_id = #{parentId} </if>
            <if test="hydd != null  and hydd != ''"> and hydd = #{hydd}</if>
            <if test="chry != null  and chry != ''"> and chry = #{chry}</if>
            <if test="yxqk != null  and yxqk != ''"> and yxqk = #{yxqk}</if>
            <if test="jjwt != null  and jjwt != ''"> and jjwt = #{jjwt}</if>
            <if test="czwt != null  and czwt != ''"> and czwt = #{czwt}</if>
            <if test="tgjy != null  and tgjy != ''"> and tgjy = #{tgjy}</if>
            <if test="ldyq != null  and ldyq != ''"> and ldyq = #{ldyq}</if>
            <if test="zt != null  and zt != ''"> and zt = #{zt}</if>
            <if test="dwjb != null  and dwjb != ''"> and a.dwjb = #{dwjb}</if>
            <if test="tjr != null  and tjr != ''"> and tjr = #{tjr}</if>
            <if test="tjrq != null  and tjrq != ''"> and tjrq = #{tjrq}</if>
            <if test="yl1 != null  and yl1 != ''"> and yl1 = #{yl1}</if>
            <if test="yl2 != null  and yl2 != ''"> and yl2 = #{yl2}</if>
            <if test="yl3 != null  and yl3 != ''"> and yl3 = #{yl3}</if>
            <if test="yl4 != null  and yl4 != ''"> and yl4 = #{yl4}</if>
            <if test="yl5 != null  and yl5 != ''"> and yl5 = #{yl5}</if>
            <if test="startJd !=null and startJd !=''"> and DATE_FORMAT(hyrq,'%Y-%m')>=#{startJd}  </if>
            <if test="endJd !=null and endJd !=''"> and DATE_FORMAT(hyrq,'%Y-%m')&lt;=#{endJd}  </if>
        </where>
        order by  hyrq desc
    </select>

</mapper>