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

    <select id="selectXMMXZ_2" resultType="io.renren.modules.app.entity.FhmxzEntity">
		select djrq,djlx,djbh,jfbj,jflx,dfbj,dflx,ye,ss from (
      select date_format(#{begdate},'%Y-%m-%d')  djrq,
             '期初余额' djlx ,
             ''   djbh,
             0 jfbj,
             0 jflx,
             0 dfbj,
             0 dflx,
             ifnull(sum(case when ywpzmx.jdf='0' then ywpzmx.je else -ywpzmx.je  end),0) ye,
             '0' ss
        from ywpz,
             ywpzmx,
             ZHXX
       where ywpz.djbh   = ywpzmx.djbh and
             ywpzmx.DHBM = ZHXX.DHBM and
             ywpz.djrq   &lt; date_format(#{begdate},'%Y-%m-%d') AND
             ZHXX.XMBM   = #{xmbm}
      UNION ALL
      select date_format(#{enddate},'%Y-%m-%d') djrq,
              ZHXX.Dhmc djlx,
              '' djbh,
              t.jfbj,
              t.jflx,
              t.dfbj,
              t.dflx,
              0 ye,
              '1' ss
       from
         (select ywpzmx.DHBM,
                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,
              ZHXX
         where ywpz.djbh   =  ywpzmx.djbh and
               ywpzmx.DHBM = ZHXX.DHBM and
               ywpz.djrq between #{begdate} and #{enddate} AND
               ZHXX.XMBM   = #{xmbm}
         group by ywpzmx.DHBM,ywpzmx.jdf) t
       left join ZHXX on ZHXX.Dhbm = t.DHBM
    ) a1
    order by ss,djrq limit 0,#{limit}
	</select>

</mapper>