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

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

</mapper>