遞歸的精髓是第一次查詢的結果作為下一次查詢的條件繼續(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