<?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.DymxzDao">
	<select id="selectDYMXZ" 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,
             ifnull(sum(case when ywpzmx.jdf='0' then ywpzmx.je else -ywpzmx.je  end),0) ye,
             '0' ss
        from ywpz,
             ywpzmx,
             fwxx
       where ywpz.djbh   = ywpzmx.djbh and
             ywpzmx.fwbm = fwxx.fwbm and
             ywpz.djrq   &lt; date_format(#{begdate},'%Y-%m-%d') AND
             fwxx.MH     = #{fwdybm}
      UNION ALL
      select  date_format(t.djrq,'%Y-%m-%d') djrq,
              CASE WHEN t.djlx='01' THEN '单位缴款'
                  WHEN t.djlx='02' THEN '单位维修基金分配'
                  WHEN t.djlx='03' THEN '个人维修基金'
                  WHEN t.djlx='04' THEN '公共收益分配'
                  WHEN t.djlx='21' THEN '首次拨付分摊'
                  WHEN t.djlx='22' THEN '余额拨付分摊'
                  WHEN t.djlx='23' THEN '维修基金销户'
                  WHEN t.djlx='05' THEN '个人续缴'
                  WHEN t.djlx='07' THEN '个人补缴'
                  WHEN t.djlx='10' THEN '年度结息'
                  WHEN t.djlx='13' THEN '个人退款'
                  WHEN t.djlx='82' THEN '单位交款分配冲账'
                  WHEN t.djlx='83' THEN '个人维修资金冲账'
                  WHEN t.djlx='84' THEN '公共设施收益分配冲账'
                  WHEN t.djlx='85' THEN '个人续缴冲账'
                  WHEN t.djlx='87' THEN '个人补缴冲账'
                  else '未定' end djlx,
              t.djbh,
              t.jfbj,
              t.jflx,
              t.dfbj,
              t.dflx,
              0 ye,
              '1' ss
       from
         (select ywpzmx.fwbm,ywpz.djbh,ywpz.djrq,ywpz.djlx,
                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
               fwxx.MH = #{fwdybm}
         group by ywpzmx.fwbm,ywpzmx.jdf,ywpz.djbh,ywpz.djrq) t
       left join fwxx on fwxx.fwbm = t.fwbm
    ) a1
    order by ss,djbh
	</select>

    <select id="selectDYMXZ_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,
             ifnull(sum(case when ywpzmx.jdf='0' then ywpzmx.je else -ywpzmx.je  end),0) ye,
             '0' ss
        from ywpz,
             ywpzmx,
             fwxx
       where ywpz.djbh   = ywpzmx.djbh and
             ywpzmx.fwbm = fwxx.fwbm and
             ywpz.djrq   &lt; date_format(#{begdate},'%Y-%m-%d') AND
             fwxx.MH     = #{fwdybm}
      UNION ALL
      select  date_format(t.djrq,'%Y-%m-%d') djrq,
              CASE WHEN t.djlx='01' THEN '单位缴款'
                  WHEN t.djlx='02' THEN '单位维修基金分配'
                  WHEN t.djlx='03' THEN '个人维修基金'
                  WHEN t.djlx='04' THEN '公共收益分配'
                  WHEN t.djlx='21' THEN '首次拨付分摊'
                  WHEN t.djlx='22' THEN '余额拨付分摊'
                  WHEN t.djlx='23' THEN '维修基金销户'
                  WHEN t.djlx='05' THEN '个人续缴'
                  WHEN t.djlx='07' THEN '个人补缴'
                  WHEN t.djlx='10' THEN '年度结息'
                  WHEN t.djlx='13' THEN '个人退款'
                  WHEN t.djlx='82' THEN '单位交款分配冲账'
                  WHEN t.djlx='83' THEN '个人维修资金冲账'
                  WHEN t.djlx='84' THEN '公共设施收益分配冲账'
                  WHEN t.djlx='85' THEN '个人续缴冲账'
                  WHEN t.djlx='87' THEN '个人补缴冲账'
                  else '未定' end djlx,
              t.djbh,
              t.jfbj,
              t.jflx,
              t.dfbj,
              t.dflx,
              0 ye,
              '1' ss
       from
         (select ywpzmx.fwbm,ywpz.djbh,ywpz.djrq,ywpz.djlx
                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
               fwxx.MH = #{fwdybm}
         group by ywpzmx.fwbm,ywpzmx.jdf,ywpz.djbh,ywpz.djrq) t
       left join fwxx on fwxx.fwbm = t.fwbm
    ) a1
    order by ss,djbh limit 0,#{limit}
    </select>


</mapper>