<?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.ZhmxzDao">
	<select id="selectZhMXZ" parameterType="io.renren.modules.app.entity.FhmxzEntity" resultType="io.renren.modules.app.entity.FhmxzEntity">
		select djrq,djlx,djbh,ifnull(jfbj,'0') jfbj,ifnull(jflx,'0') jflx,ifnull(dfbj,'0')dfbj,ifnull(dflx,'0')dflx,ye,ss from (
      select date_format(#{begdate},'%Y-%m-%d')  djrq,
             '期初余额' djlx ,
             ''   djbh,
             0 jfbj,
             0 jflx,
             0 dfbj,
             0 dflx,
			 '' mh,
             ifnull(sum(case when ywpzmx.jdf='0' then ywpzmx.je else -ywpzmx.je  end),0) ye,
             '0' ss
        from ywpz,
             ywpzmx
       where ywpz.djbh   = ywpzmx.djbh and
             ywpz.djrq   &lt; date_format(#{begdate},'%Y-%m-%d') AND
             ywpzmx.DHBM   = #{dhbm}
      UNION ALL
      select date_format(#{enddate},'%Y-%m-%d') djrq,
              dyxx.dymc djlx,
              '' djbh,
              t.jfbj,
              t.jflx,
              t.dfbj,
              t.dflx,
			  t.mh,
              0 ye,
              '1' ss
       from
         (select fwxx.mh,
                sum(case when ywpzmx.jdf='1' then ywpzmx.bjje else 0 end) jfbj,
                sum(case when ywpzmx.jdf='1' then ywpzmx.lxje else 0 end) jflx,
                sum(case when ywpzmx.jdf='0' then ywpzmx.bjje else 0 end) dfbj,
                sum(case when ywpzmx.jdf='0' then ywpzmx.lxje else 0 end) dflx
         from ywpz,
              ywpzmx,
              fwxx
         where ywpz.djbh   =  ywpzmx.djbh and
               ywpzmx.fwbm =  fwxx.fwbm and
               ywpz.djrq between #{begdate} and #{enddate} AND
               ywpzmx.DHBM   =  #{dhbm}
         group by fwxx.mh,ywpzmx.jdf) t
       left join dyxx on dyxx.fwdybm = t.mh
    ) a1
	</select>

    <select id="selectZhMXZ_2" resultType="io.renren.modules.app.entity.FhmxzEntity">
		select djrq,djlx,djbh,ifnull(jfbj,'0') jfbj,ifnull(jflx,'0') jflx,ifnull(dfbj,'0')dfbj,ifnull(dflx,'0')dflx,ye,ss from (
      select date_format(#{begdate},'%Y-%m-%d')  djrq,
             '期初余额' djlx ,
             ''   djbh,
             0 jfbj,
             0 jflx,
             0 dfbj,
             0 dflx,
			 '' mh,
             ifnull(sum(case when ywpzmx.jdf='0' then ywpzmx.je else -ywpzmx.je  end),0) ye,
             '0' ss
        from ywpz,
             ywpzmx
       where ywpz.djbh   = ywpzmx.djbh and
             ywpz.djrq   &lt; date_format(#{begdate},'%Y-%m-%d') AND
             ywpzmx.DHBM   = #{dhbm}
      UNION ALL
      select date_format(#{enddate},'%Y-%m-%d') djrq,
              dyxx.dymc djlx,
              '' djbh,
              t.jfbj,
              t.jflx,
              t.dfbj,
              t.dflx,
			  t.mh,
              0 ye,
              '1' ss
       from
         (select fwxx.mh,
                sum(case when ywpzmx.jdf='1' then ywpzmx.bjje else 0 end) jfbj,
                sum(case when ywpzmx.jdf='1' then ywpzmx.lxje else 0 end) jflx,
                sum(case when ywpzmx.jdf='0' then ywpzmx.bjje else 0 end) dfbj,
                sum(case when ywpzmx.jdf='0' then ywpzmx.lxje else 0 end) dflx
         from ywpz,
              ywpzmx,
              fwxx
         where ywpz.djbh   =  ywpzmx.djbh and
               ywpzmx.fwbm =  fwxx.fwbm and
               ywpz.djrq between #{begdate} and #{enddate} AND
               ywpzmx.DHBM   =  #{dhbm}
         group by fwxx.mh,ywpzmx.jdf) t
       left join dyxx on dyxx.fwdybm = t.mh
    ) a1 limit 0,#{limit}
	</select>
</mapper>