用递归查询集赞排名并增加相应的积分

递归的精髓是第一次查询的结果作为下一次查询的条件继续执行下去

/**

*通过活动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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,779评论 18 399
  • 什么是SQL数据库: SQL是Structured Query Language(结构化查询语言)的缩写。SQL是...
    西贝巴巴阅读 1,863评论 0 10
  • 我的孩子今年九岁,从她出生前,我就买了很多教育和养育方面的书,正像现在的流行说法:第一个孩子照书养。她出生后,我也...
    筱瑶儿阅读 645评论 0 0
  • (近日,有媒体报载,一大学生因掏了鸟窝被判刑十年半) 我想变成一只鸟你们来捉啊捉啊我把巢就筑在你家屋檐下进门给你撒...
    张了了阅读 136评论 0 0
  • 小时候特别喜欢妈妈做的羊肉水饺,吃水饺的时候我总是先一个一个地吃掉包在外面的饺子皮,碗里留下一个个饱满的肉丸子,爸...
    米咔阅读 844评论 0 3