<?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.hzya.frame.grpU8.nxproof.controlindex.dao.impl.ControlIndexDaoImpl">
    <resultMap id="get-ControlIndexEntity-result" type="com.hzya.frame.grpU8.nxproof.controlindex.entity.ControlIndexEntity">
        <result property="YSDH" column="YSDH" />
        <result property="ZBID" column="ZBID" />
        <result property="JE" column="JE" />
        <result property="KJND" column="KJND" />
        <result property="ZT" column="ZT" />
        <result property="ZCMC" column="ZCMC"/>
        <result property="ZBTBZT" column="ZBTBZT"/>
    </resultMap>

   <sql id="ControlIndexEntity_Base_Column_List">
YSDH,ZBID,JE,KJND,ZT,ZCMC,ZBTBZT
   </sql>

    <!--根据状态查询数据-->
    <select id="ControlIndexEntity_list_base" resultMap="get-ControlIndexEntity-result" parameterType="com.hzya.frame.grpU8.nxproof.controlindex.entity.ControlIndexEntity">
        select
            YSDH,ZBID,JE,KJND,ZT,ZCMC,ZBTBZT
        from T_ZCCW_CONTROLINDEX where ZT= #{ZT}
    </select>


<!--    查询最大单据编号+1-->
    <select id="ControlIndexEntity_list_base_djbhMax" resultMap="get-ControlIndexEntity-result" parameterType="com.hzya.frame.grpU8.nxproof.controlindex.entity.ControlIndexEntity">
            select
                case  when
                          len(isnull(max(right(DJBH,'4')), '0')+1)=2 then
                          concat(#{fph}+'BXD'+#{djdate}+'00',isnull(max(right(DJBH,'4')), '0')+1)
                      when len(isnull(max(right(DJBH,'4')), '0')+1)=1 then
                          concat(#{fph}+'BXD'+#{djdate}+'000',isnull(max(right(DJBH,'4')), '0')+1)
                      when len(isnull(max(right(DJBH,'4')), '0')+1)=3 then
                          concat(#{fph}+'BXD'+#{djdate}+'0',isnull(max(right(DJBH,'4')), '0')+1)
                      else concat(#{fph}+'BXD'+#{djdate}+'',isnull(max(right(DJBH,'4')), '0')+1) end
                    as djbh from T_ZCCW_CONTROLINDEX where GSDM=#{gsdm} and DJBH like '%BXD'+#{djdate}+'%';
    </select>

    <!--    查询最大mlid+1-->
    <!--<select id="ControlIndexEntity_list_base_mlidMax" resultMap="get-ControlIndexEntity-result" parameterType="com.hzya.frame.grpU8.nxproof.controlindex.entity.ControlIndexEntity">
        select
               case  when
                         length(ifnull(max(right(mlid,'4')), '0')+1)=2 then
                         concat(concat(substring(replace(DATE_FORMAT(now(),'%Y-%m-%d'),'-',''),1,6),'00'),ifnull(max(right(mlid,'4')), '0')+1)
                     when length(ifnull(max(right(mlid,'4')), '0')+1)=1 then
                         concat(concat(substring(replace(DATE_FORMAT(now(),'%Y-%m-%d'),'-',''),1,6),'000'),ifnull(max(right(mlid,'4')), '0')+1)
                     when length(ifnull(max(right(mlid,'4')), '0')+1)=3 then
                         concat(concat(substring(replace(DATE_FORMAT(now(),'%Y-%m-%d'),'-',''),1,6),'0'),ifnull(max(right(mlid,'4')), '0')+1)
                     else concat(substring(replace(DATE_FORMAT(now(),'%Y-%m-%d'),'-',''),1,6),ifnull(max(right(mlid,'4')), '0')+1) end
                   as mlId from T_ZCCW_CONTROLINDEX where GSDM=#{gsdm} and mlid like concat(substring(replace(DATE_FORMAT(now(),'%Y-%m-%d'),'-',''),1,6),'%')
    </select>-->
    <select id="ControlIndexEntity_list_base_mlidMax" resultMap="get-ControlIndexEntity-result" parameterType="com.hzya.frame.grpU8.nxproof.controlindex.entity.ControlIndexEntity">
        select (isnull(max(MLID),concat(substring(#{djdate},0,7),0000)) + 1) as mlId
        from T_ZCCW_CONTROLINDEX where 1=1 and GSDM=#{gsdm} and MLID like concat(substring(#{djdate},0,7),'%')
    </select>


    <insert id="ControlIndexEntity_insert" parameterType="com.hzya.frame.grpU8.nxproof.controlindex.entity.ControlIndexEntity">
        insert into T_ZCCW_CONTROLINDEX(
        <if test="gsdm != null and gsdm !='' "> GSDM, </if>
        <if test="kjnd != null and kjnd !='' "> KJND, </if>
        <if test="djlxId != null and djlxId !='' "> DJLXID, </if>
        <if test="mlId != null and mlId !='' "> MLID, </if>
        <if test="zt != null and zt !='' "> ZT, </if>
        <if test="crerdm != null and crerdm !=''"> CRerDM, </if>
        <if test="crermc != null and crermc !='' "> CRerMC, </if>
        <if test="crerdate != null and crerdate !='' "> CRerDate, </if>
        <if test="flowcode != null and flowcode !='' "> FlowCode,</if>
        <if test="curshjd != null and curshjd !='' "> CurSHJD, </if>
        <if test="nextshjd != null and nextshjd !='' "> NextSHJD,</if>
        <if test="shrs != null and shrs !='' "> SHR,</if>
        <if test="shrId != null and shrId !=''"> SHRID, </if>
        <if test="shrq != null and shrq !='' "> SHRQ, </if>
        <if test="jsfs != null and jsfs !='' "> JSFS, </if>
        <if test="djbh != null and djbh !='' "> DJBH, </if>
        <if test="djdate != null and djdate !='' "> DJDate, </if>
        <if test="fjs != null and fjs !='' "> FJS,</if>
        <if test="bmdm != null and bmdm !='' "> BMDM, </if>
        <if test="bmmc != null and bmmc !='' "> BMMC,</if>
        <if test="grdm != null and grdm !='' ">GRDM, </if>
        <if test="grmc != null and grmc !='' ">GRMC,</if>
        <if test="tel != null and tel !='' "> TEL, </if>
        <if test="yjje != null and yjje !='' "> YJJE, </if>
        <if test="thje != null and thje !='' "> THJE,</if>
        <if test="blje != null and blje !='' "> BLJE, </if>
        <if test="je != null and je !='' "> JE, </if>
        <if test="whje != null and whje !='' "> WHJE,</if>
        <if test="jedx != null and jedx !='' "> JEDX </if>
        )
        values (
        <if test="gsdm != null and gsdm !='' "> #{gsdm}, </if>
        <if test="kjnd != null and kjnd !='' "> #{kjnd}, </if>
        <if test="djlxId != null and djlxId !='' "> ${djlxId}, </if>
        <if test="mlId != null and mlId !='' "> ${mlId}, </if>
        <if test="zt != null and zt !='' "> #{zt}, </if>
        <if test="crerdm != null and crerdm !=''"> #{crerdm}, </if>
        <if test="crermc != null and crermc !='' "> #{crermc}, </if>
        <if test="crerdate != null and crerdate !='' "> #{crerdate}, </if>
        <if test="flowcode != null and flowcode !='' "> #{flowcode},</if>
        <if test="curshjd != null and curshjd !='' "> ${curshjd}, </if>
        <if test="nextshjd != null and nextshjd !='' "> ${nextshjd},</if>
        <if test="shrs != null and shrs !='' "> #{shrs}, </if>
        <if test="shrId != null and shrId !='' "> #{shrId}, </if>
        <if test="shrq != null and shrq !='' "> #{shrq}, </if>
        <if test="jsfs != null and jsfs !='' "> #{jsfs}, </if>
        <if test="djbh != null and djbh !='' "> #{djbh}, </if>
        <if test="djdate != null and djdate !='' "> #{djdate}, </if>
        <if test="fjs != null and fjs !='' "> ${fjs},</if>
        <if test="bmdm != null and bmdm !='' "> #{bmdm}, </if>
        <if test="bmmc != null and bmmc !='' "> #{bmmc},</if>
        <if test="grdm != null and grdm !='' "> #{grdm}, </if>
        <if test="grmc != null and grmc !='' "> #{grmc},</if>
        <if test="tel != null and tel !='' "> #{tel}, </if>
        <if test="yjje != null and yjje !='' "> #{yjje}, </if>
        <if test="thje != null and thje !='' "> #{thje},</if>
        <if test="blje != null and blje !='' "> #{blje}, </if>
        <if test="je != null and je !='' "> #{je}, </if>
        <if test="whje != null and whje !='' "> #{whje},</if>
        <if test="jedx != null and jedx !='' "> #{jedx} </if>
        )
    </insert>

    <update id="ControlIndexEntity_update" parameterType="com.hzya.frame.grpU8.nxproof.controlindex.entity.ControlIndexEntity">
        update T_ZCCW_CONTROLINDEX set ZBTBZT= #{ZBTBZT}
        where KJND = #{KJND} and YSDH=#{YSDH} and ZBID=${ZBID}
    </update>

    <!--删除，用于单据删除时使用-->
    <delete id="ControlIndexEntity_delete" parameterType="com.hzya.frame.grpU8.nxproof.controlindex.entity.ControlIndexEntity">
        DELETE
        FROM
            T_ZCCW_CONTROLINDEX
        WHERE
            GSDM = #{gsdm}
          AND KJND = #{kjnd}
          AND DJLXID = #{djlxId}
          AND MLID = #{mlId}
    </delete>

    <!--修改 用于单据送审和审批时使用-->
    <update id="ControlIndexEntity_update_songs" parameterType="com.hzya.frame.grpU8.nxproof.controlindex.entity.ControlIndexEntity">
        UPDATE T_ZCCW_CONTROLINDEX
        SET ZT = #{zt},
            FlowCode = #{flowcode},
            CurSHJD =#{curshjd},
            NextSHJD = #{nextshjd},
            SHRID =#{shrId},
            SHR = #{shrs},
            SHRQ = #{shrq}
        WHERE
            gsdm = #{gsdm}
          AND kjnd = #{kjnd}
          AND djlxid = #{djlxId}
          AND mlid = #{mlId}
    </update>

</mapper>
