1. 程式人生 > >Oracle資料庫使用身份證計算處於哪個年齡段(Java的SSM框架)

Oracle資料庫使用身份證計算處於哪個年齡段(Java的SSM框架)

這個需求我是實現了,但是在程式碼的優化上沒有做到位,其實可以寫的更加簡潔,但是因為時間緊,就沒有想那麼多。下面附上Java介面邏輯程式碼和mybatis的SQL程式碼:

    /**
     * 使用身份證判斷年齡段
     * @param addressid
     * @return
     */
    @RequestMapping("/toAge")
    @ResponseBody
    public Object toAge(String id,String type){
        Map<String, Object> map=new
HashMap<String, Object>(); Subject currentUser = SecurityUtils.getSubject(); String userid = currentUser.getSession().getAttribute("_USER_ID").toString(); List<Community> age=dataAnalyzeService.selectAddressAge(id,type,userid); //初始化引數 int c1=0;int c2=
0;int c3=0; int c4=0;int c5=0;int c6=0; for(int i=0;i<age.size();i++){ String group=age.get(i).getAge_group();//迴圈得出年齡段對比後傳值 Integer count=age.get(i).getAge_count(); if(group.equals("0~18")){ c1=count; } if(group.equals
("19~30")){ c2=count; } if(group.equals("31~40")){ c3=count; } if(group.equals("41~50")){ c4=count; } if(group.equals("51~60")){ c5=count; } if(group.equals("60以上")){ c6=count; } } map.put("c1", c1);//以此存入map中 map.put("c2", c2); map.put("c3", c3); map.put("c4", c4); map.put("c5", c5); map.put("c6", c6); return map; }
    <!--使用身份證判斷年齡段 -->
    <select id="selectAddressAge" resultType="com.oracle.pojo.Community" parameterType="java.lang.String">
    select  
    case 
        when months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &gt;= 0
                     and months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &lt; 19
                     then '0~18'
         when months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &gt;= 19
                     and months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &lt; 31
                     then '19~30'
         when months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &gt;= 31
                     and months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &lt; 41
                     then '31~40'
         when months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &gt;= 41
                     and months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &lt; 51
                     then '41~50'
         when months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &gt;= 51
                     and months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &lt; 60
                     then '51~60'
         when months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &gt;= 60
                     then '60以上' 
    end age_group, count(1) AS age_count    from cmu_userdevice e
        LEFT JOIN cmu_subscriber a ON a.COMMUNITYKEY =e.COMMUNITYKEY
        LEFT JOIN CMU_CUSTOMER t ON t.SUBSCRIBERKEY = a.SUBSCRIBERKEY
        LEFT JOIN CMU_COMMUNITY c ON c.COMMUNITYKEY = a.COMMUNITYKEY
        RIGHT OUTER JOIN 
        (select * from CMU_USERS start with USER_ID = #{userid,jdbcType=VARCHAR} connect by prior USER_ID = PID) f ON e.USER_ID = f.USER_ID 
        WHERE
            <if test="type == 1">
                c.PROVINCEID = #{id, jdbcType=VARCHAR}
            </if> 
            <if test="type == 2">
                c.cityid = #{id, jdbcType=VARCHAR}
            </if> 
            <if test="type == 3">
                c.areaid = #{id, jdbcType=VARCHAR}
            </if>
            <if test="type == 4">
                c.ADDRESSID = #{id, jdbcType=VARCHAR}
            </if>
            AND t.identity is NOT NULL
    group by 
    case
        when months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &gt;= 0
                     and months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &lt; 19
                     then '0~18'
        when months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &gt;= 19
                     and months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &lt; 31
                     then '19~30'
        when months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &gt;= 31
                     and months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &lt; 41
                     then '31~40'
        when months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &gt;= 41
                     and months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &lt; 51
                     then '41~50'
        when months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &gt;= 51
                     and months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &lt; 60
                     then '51~60'
        when months_between(sysdate,to_date(substr(t.identity,7,8),'yyyymmdd'))/12 &gt;= 60
                     then '60以上'
    end 
    ORDER BY instr('0~18 19~30 31~40 41~50 51~60 60以上',NULL)
    </select>

詳解:在介面中,我直接簡單的使用的if進行區分,取出值以後就直接通過Map返回給了前端,前端使用ECharts圖表對資料進行展示,因為前後端都是我一個人編寫的,所以優化方面做的不太到位!請不要噴我!
SQL解釋:首先SQL的if塊你們不需要去看,這是我自己需求需要的 還有相關的表結構必須寫的關聯表語句,大家只需要瞭解sql語句的頭部和尾部,t.identity是身份證的列,你需要更換為自己的列,還有年齡範圍的更改,也可以改為你自己劃分的範圍,但是group up 需要與其同步更改!
前端效果圖:
這裡寫圖片描述