递归的精髓是第一次查询的结果作为下一次查询的条件继续执行下去
/**
*通过活动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;
}
//增加的分数
intaddScroll = scroll;
//客户集赞的积分+原来的积分=现在的总积分
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;
}
//增加的分数
intaddScroll = scroll;
//客户集赞的积分+原来的积分=现在的总积分
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 '集赞数量',
`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