<?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.DwmxzDao">
	<select id="selectDWMXZ" parameterType="io.renren.modules.app.entity.FhmxzEntity" resultType="io.renren.modules.app.entity.FhmxzEntity">
		select djrq,djlx,djbh,ifnull(jfbj,'0') jfbj,ifnull(dfbj,'0') dfbj,ye,ss from (
      select date_format(#{begdate},'%Y-%m-%d')  djrq,
             '期初余额' djlx ,
             '' djbh,
             0 jfbj,
             0 dfbj,
             ifnull(sum(case when DJLX in ('01','81','82') then je else -je  end),0) ye,
             '0' ss
        from ywpz
       where djrq &lt; date_format(#{begdate},'%Y-%m-%d') AND
             DWBM   =  #{dwbm} AND
             DJLX IN ('01','02','81','82')
      UNION ALL
      select date_format(djrq,'%Y-%m-%d'),
             CASE WHEN djlx='01' THEN concat(BZ,'(单位缴款)')
                  WHEN djlx='02' THEN concat('','(单位维修基金分配)')
                  WHEN djlx='81' THEN concat(BZ,'(冲单位缴款)')
                  WHEN djlx='82' THEN concat(BZ,'(冲单位维修基金分配)')
                  else concat(BZ,'(未定)') end djlx,
             djbh,
             case when DJLX in ('02','82')  then JE else 0 end JFBJ, 
             case when DJLX in ('01','81') then je else 0 end DFBJ, 
             0 ye,
             '1' ss
        from ywpz
       where date_format(djrq,'%Y-%m-%d')  between #{begdate} and #{enddate} AND
             DWBM   =  #{dwbm} AND
             DJLX IN ('01','02','81','82')
    ) a1
    order by substring(djbh,3) 

	</select>

    <select id="selectDWMXZ_2" resultType="io.renren.modules.app.entity.FhmxzEntity">
		select djrq,djlx,djbh,ifnull(jfbj,'0') jfbj,ifnull(dfbj,'0') dfbj,ye,ss from (
      select date_format(#{begdate},'%Y-%m-%d')  djrq,
             '期初余额' djlx ,
             '' djbh,
             0 jfbj,
             0 dfbj,
             ifnull(sum(case when DJLX in ('01','81','82') then je else -je  end),0) ye,
             '0' ss
        from ywpz
       where djrq &lt; date_format(#{begdate},'%Y-%m-%d') AND
             DWBM   =  #{dwbm} AND
             DJLX IN ('01','02','81','82')
      UNION ALL
      select date_format(djrq,'%Y-%m-%d'),
             CASE WHEN djlx='01' THEN concat(BZ,'(单位缴款)')
                  WHEN djlx='02' THEN concat(BZ,'(单位维修基金分配)')
                  WHEN djlx='81' THEN concat(BZ,'(冲单位缴款)')
                  WHEN djlx='82' THEN concat(BZ,'(冲单位维修基金分配)')
                  else concat(BZ,'(未定)') end djlx,
             djbh,
             case when DJLX in ('02','82')  then JE else 0 end JFBJ,
             case when DJLX in ('01','81') then je else 0 end DFBJ,
             0 ye,
             '1' ss
        from ywpz
       where date_format(djrq,'%Y-%m-%d')  between #{begdate} and #{enddate} AND
             DWBM   =  #{dwbm} AND
             DJLX IN ('01','02','81','82')
    ) a1
    order by substring(djbh,3) limit 0,#{limit}

	</select>

</mapper>