<?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="io.renren.modules.app.dao.RjzDao">
	<select id="rjzSelect_2" parameterType="io.renren.modules.app.entity.RjzEntity" resultType="io.renren.modules.app.entity.RjzEntity">
	select pzrq,pztype,pzh,zymc,ifnull(jfje,'0') jfje,ifnull(dfje,'0') dfje,ifnull(qcye,'0') qcye,ss,#{kmsx} kmsx from (
select #{qsrq} pzrq,'' pztype,0 pzh,'期初余额' zymc, 0 jfje,0 dfje ,sum(qcye) qcye ,'0' ss from h_kmye where kjqj = substr(#{cxrq},1,7) and ztbm=#{ztbm} and hskmdm like '${hskmdm}%'
union all
select pzrq,pztype,pzh ,zymc,jfje,dfje,0 qcye,'1' ss from h_pz where kjqj=substr(#{cxrq},1,7) and ztbm=#{ztbm} and km like '${hskmdm}%'
 )b order by ss,pzh,pzrq
	</select>
    
    <select id="rjzSelect_1" parameterType="io.renren.modules.app.entity.RjzEntity" resultType="io.renren.modules.app.entity.RjzEntity">
    select pzrq,pztype,pzh,zymc,ifnull(jfje,'0') jfje,ifnull(dfje,'0') dfje,ifnull(qcye,'0') qcye,ss,#{kmsx} kmsx from (

     select #{zzrq} pzrq,'' pztype,0 pzh,'期初余额' zymc, 0 jfje,0 dfje , sum(case when #{kmsx}= '0' then qcye + jfje - dfje else qcye - jfje + dfje end) qcye ,'0' ss from
        (

     select #{hskmdm} km,0 jfje,0 dfje,sum(qcye) qcye  from h_kmye where ztbm=#{ztbm} and hskmdm like '${hskmdm}%' and kjqj = substr(#{cxrq},1,7)
     union all
     select #{hskmdm} km ,jfje,dfje,0 qcye from h_pz where pzrq between STR_TO_DATE(#{qsrq},'%Y-%m-%d') and STR_TO_DATE(#{zzrq},'%Y-%m-%d') and ztbm=#{ztbm} and km like '${hskmdm}%'
        ) grye
     union all
      select pzrq,pztype,pzh ,zymc,jfje,dfje,0 qcye,'1' ss from h_pz where pzrq = STR_TO_DATE(#{cxrq},'%Y-%m-%d') and ztbm=#{ztbm} and km like '${hskmdm}%'
      
	) a order by ss,pzh,pzrq
    </select>
    
    <select id="select_kmjb" resultType="String">
    	select kmjb from h_zt where ztbm = #{ztbm}
    </select>
    
    <select id="select_pz1" parameterType="io.renren.modules.app.entity.HPzEntity2" resultType="io.renren.modules.app.entity.HPzEntity2">
    	select t1.*,t1.km km,t3.billname from h_pz t1
                                   <!-- inner join h_km t2 on  t1.km = t2.km and t1.ztbm = t2.ztbm -->
                                   left outer join h_jsfs t3 on  t1.ywlx = t3.billcode
                                   where t1.pzh = #{pzh}
                                         and t1.ztbm = #{ztbm}
                                         and t1.pztype = #{pztype}
                                         and t1.kjqj = #{kjqj}
                                         and t1.flxh = #{flxh}
                                 order by t1.flxh
    </select>
    
    <select id="select_pz2" parameterType="io.renren.modules.app.entity.HPzEntity2" resultType="io.renren.modules.app.entity.HPzEntity2">
    	select t1.*,t1.km km,t3.billname from h_pz t1
                                   <!-- inner join h_km t2 on  t1.km = t2.km and t1.ztbm = t2.ztbm -->
                                   left outer join h_jsfs t3 on t1.ywlx = t3.billcode
                                   where t1.pzh = #{pzh}
                                         and t1.ztbm = #{ztbm}
                                         and t1.kjqj = #{kjqj}
                                         and t1.flxh = #{flxh}
                                 order by t1.flxh
    </select>
    
    <select id="select_pz3" parameterType="io.renren.modules.app.entity.HPzEntity2" resultType="io.renren.modules.app.entity.HPzEntity2">
    	select t1.*,t1.km km,t3.billname from h_pz t1
                                   <!-- inner join h_km t2 on  t1.km = t2.km and t1.ztbm = t2.ztbm -->
                                   left outer join h_jsfs t3 on t1.ywlx = t3.billcode
                                   where t1.pzh = #{pzh}
                                         and t1.ztbm = #{ztbm}
                                         and t1.kjqj = #{kjqj}
                                         and t1.pztype = #{pztype}
                                    order by t1.flxh
    </select>
    
    <select id="select_pz4" parameterType="io.renren.modules.app.entity.HPzEntity2" resultType="io.renren.modules.app.entity.HPzEntity2">
    	select t1.*,t1.km km,t3.billname from h_pz t1
                                   <!-- inner join h_km t2 on  t1.km = t2.km and t1.ztbm = t2.ztbm -->
                                   left outer join h_jsfs t3 on t1.ywlx = t3.billcode
                                   where t1.pzh = #{pzh}
                                         and t1.ztbm = #{ztbm}
                                         and t1.kjqj = #{kjqj}
                                  order by t1.flxh
    </select>
    
    <select id="select_kmmc" resultType="String">
    	select trim(kmmc)  kmmc from h_km where km=substr(trim(#{kmdm}),1,#{len}) and ztbm=#{ztbm}
    </select>
    
    <select id="select_kmjb2" resultType="String">
    	select substr(#{kmjb},#{len3},1) from dual
    </select>
    
    <select id="select_pz" parameterType="io.renren.modules.app.entity.HPzEntity2" resultType="io.renren.modules.app.entity.HPzEntity2">
    	select * from h_pz where kjqj=#{kjqj} and pzh=#{pzh} order by flxh
    </select>
</mapper>