用遞歸查詢集贊排名并增加相應的積分

遞歸的精髓是第一次查詢的結果作為下一次查詢的條件繼續(xù)執(zhí)行下去

/**

*通過活動id和修改活動步驟,流程推薦

*

*@return

*/

@RequestMapping("/updateById")

publicResponseEntity updateyou(Activity activity)

{

JsonResult r =newJsonResult();

List allCus =newArrayList<>();

if(activity.getStep_type() ==4)

{

//第1名

Integer thisMax =pictureMapper.getByOne();

doNext(allCus,thisMax,40);

//2-3名

for(inti =0; i <2; i++)

{

thisMax =pictureMapper.getByNext(thisMax);

doNext(allCus,thisMax,30);

}

//4-10名

for(inti =0; i <7; i++)

{

thisMax =pictureMapper.getByNext(thisMax);

doNext(allCus,thisMax,20);

}

//11名及以后

thisMax = doButtom(allCus,thisMax,10);

for(Customer cus : allCus)

{

inti =customerMapper.updateIntegral(cus);

}

inti =activityService.updateyou(activity);

if(i<0){

r.setResult(i);

}

else

{

r.setResult(i);

}

returnResponseEntity.ok(r);

}

else

{

inti =activityService.updateyou(activity);

if(i<0){

r.setResult(i);

}

else

{

r.setResult(i);

}

returnResponseEntity.ok(r);

}

}

privateInteger doNext(List allCus,Integer thisMax,intscroll)

{

List cuss =pictureMapper.getByTotal(thisMax);

for(Customer cus : cuss)

{

Customer customer =customerMapper.getByIdOne(cus.getId());

if(customer ==null)

{

continue;

}

//2.獲得客戶的積分

Integer integral = customer.getIntegral();

if(integral ==null)

{

integral =0;

}

//增加的分數(shù)

intaddScroll = scroll;

//客戶集贊的積分+原來的積分=現(xiàn)在的總積分

intnowintegral = addScroll + integral;

customer.setIntegral(nowintegral);

allCus.add(customer);

}

returnthisMax;

}

privateInteger doButtom(List allCus,Integer thisMax,intscroll)

{

List cuss =pictureMapper.getToButtom(thisMax);

for(Customer cus : cuss)

{

Customer customer =customerMapper.getByIdOne(cus.getId());

if(customer ==null)

{

continue;

}

//2.獲得客戶的積分

Integer integral = customer.getIntegral();

if(integral ==null)

{

integral =0;

}

//增加的分數(shù)

intaddScroll = scroll;

//客戶集贊的積分+原來的積分=現(xiàn)在的總積分

intnowintegral = addScroll + integral;

customer.setIntegral(nowintegral);

allCus.add(customer);

//int i = customerMapper.updateIntegral(customer);

}

returnthisMax;

}


sql語句如下:

@Select("SELECT MAX(e.total) AS total FROM (SELECT customer_id,SUM(nicenum) AS total FROM t_picture GROUP BY customer_id ORDER BY total DESC) e")

publicInteger getByOne();

@Select("SELECT MAX(e.total) AS total FROM (SELECT customer_id,SUM(nicenum) AS total FROM t_picture GROUP BY customer_id ORDER BY total DESC) e WHERE e.total < #{lastTheMax}")

publicInteger getByNext(Integer lastTheMax);

@Select("SELECT e.customer_id AS id,e.total AS total FROM (SELECT customer_id,SUM(nicenum) AS total FROM t_picture GROUP BY customer_id ORDER BY total DESC) e WHERE e.total = #{lastTheMax}")

publicList getByTotal(Integer lastTheMax);

@Select("SELECT e.customer_id AS id,e.total AS total FROM (SELECT customer_id,SUM(nicenum) AS total FROM t_picture GROUP BY customer_id ORDER BY total DESC) e WHERE e.total < #{lastTheMax}")

publicList getToButtom(Integer lastTheMax);

建表語句:

CREATE?TABLE?`t_picture`?(

`id`?int(11)?NOT?NULL?AUTO_INCREMENT?COMMENT?'圖片id',

`showname`?varchar(20000)?DEFAULT?NULL?COMMENT?'圖片',

`nicenum`?int(11)?DEFAULT?NULL?COMMENT?'集贊數(shù)量',

`customer_id`?int(11)?DEFAULT?NULL?COMMENT?'客戶id',

`customername`?varchar(25)?DEFAULT?NULL?COMMENT?'客戶姓名',

`rel_id`?int(11)?DEFAULT?'0'?COMMENT?'親屬id',

`headname`?varchar(25)?DEFAULT?NULL?COMMENT?'戶主名字',

PRIMARY?KEY?(`id`),

)?ENGINE=InnoDB?AUTO_INCREMENT=76?DEFAULT?CHARSET=utf8

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,754評論 18 399
  • 什么是SQL數(shù)據(jù)庫: SQL是Structured Query Language(結構化查詢語言)的縮寫。SQL是...
    西貝巴巴閱讀 1,845評論 0 10
  • 我的孩子今年九歲,從她出生前,我就買了很多教育和養(yǎng)育方面的書,正像現(xiàn)在的流行說法:第一個孩子照書養(yǎng)。她出生后,我也...
    筱瑤兒閱讀 637評論 0 0
  • (近日,有媒體報載,一大學生因掏了鳥窩被判刑十年半) 我想變成一只鳥你們來捉啊捉啊我把巢就筑在你家屋檐下進門給你撒...
    張了了閱讀 136評論 0 0
  • 小時候特別喜歡媽媽做的羊肉水餃,吃水餃的時候我總是先一個一個地吃掉包在外面的餃子皮,碗里留下一個個飽滿的肉丸子,爸...
    米咔閱讀 840評論 0 3