好好用线程去加快要处理的事情

package com.afanticar.live.manager.controller;

import com.afanticar.live.core.system.api.RobotFeignClient;
import com.afanticar.live.core.system.entity.Robot;
import com.afanticar.live.generator.core.system.mapper.UserWechatInfo2Mapper;
import jodd.util.StringUtil;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import com.afanticar.live.common.core.entity.R;
import com.afanticar.live.common.core.entity.PageInfoResp;
import com.afanticar.live.generator.admin.service.AdminUserWechatInfo2Service;
import com.afanticar.live.generator.core.system.entity.UserWechatInfo2;
import com.afanticar.live.generator.core.system.vo.UserWechatInfo2VO;
import com.afanticar.live.generator.core.system.qo.UserWechatInfo2QO;
import com.afanticar.live.generator.core.system.dto.UserWechatInfo2DTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;

import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import javax.validation.constraints.NotNull;
import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sean
 * @since 2020-12-21
 */
@RestController("AdminUserWechatInfo2Controller")
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/tenant/AdminUserWechatInfo2Controller")
@Api(tags = {"用户微信名称"})
public class AdminUserWechatInfo2Controller {
    @Resource
    private AdminUserWechatInfo2Service service;

    private final RobotFeignClient robotFeignClient;

    private final UserWechatInfo2Mapper mapper;

    ExecutorService executor = Executors.newFixedThreadPool(2000);

    /**
     * 列表(分页)
     *
     * @param query    查询条件
     * @param pageNo   当前页
     * @param pageSize 每页显示数
     */
    @GetMapping(value = "/list")
    @ApiOperation(value = "列表(分页)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page_no", value = "当前页 默认1", defaultValue = "1", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "page_size", value = "每页显示数 默认10", defaultValue = "10", dataType = "Integer", paramType = "query")
    })
    public R<PageInfoResp<List<UserWechatInfo2VO>>> getList(@ModelAttribute UserWechatInfo2QO query,
                                                            @RequestParam(value = "page_no", defaultValue = "1") Integer pageNo, @RequestParam(value = "page_size", defaultValue = "10") Integer pageSize) {
        Map<String, Object> params = BeanUtil.beanToMap(query);
        Page<UserWechatInfo2VO> page = new Page<>(pageNo, pageSize);
        IPage<UserWechatInfo2VO> list = service.getUserWechatInfo2VOListByParams(page, params);
        return R.success(new PageInfoResp((Page) list));
    }

    /**
     * 详情(通过主键id获取详情信息)
     *
     * @param id
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "详情(通过主键id获取详情信息)")
    public R<UserWechatInfo2VO> getInfoById(@PathVariable(value = "id") @NotNull String id) {
        UserWechatInfo2VO entity = service.getOneUserWechatInfo2VOBy("id", id);
        return R.success(entity);
    }

    /**
     * 添加
     *
     * @param dto
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "添加")
    public R<Boolean> add(@RequestBody @Validated UserWechatInfo2DTO dto) {
        UserWechatInfo2 entity = new UserWechatInfo2();
        BeanUtil.copyProperties(dto, entity);
        return R.success(service.edit(entity));
    }

    /**
     * 编辑
     *
     * @param dto
     */
    @PostMapping(value = "/edit")
    @ApiOperation(value = "编辑")
    public R<Boolean> edit(@RequestBody @Validated UserWechatInfo2DTO dto) {
        UserWechatInfo2 entity = new UserWechatInfo2();
        BeanUtil.copyProperties(dto, entity);
        return R.success(service.edit(entity));
    }

    /**
     * 删除(通过主键批量删除)
     *
     * @param idList
     */
    @DeleteMapping(value = "/batchDeleteByIds")
    @ApiOperation(value = "删除(通过主键批量删除)")
    public R<Boolean> batchDeleteByUserIds(@RequestBody @NotNull List<Integer> idList) {
        return R.success(service.batchDeleteByUserIds(idList));
    }

    /**
     * 从这张表里获取用户名称和logo,放进tb_robot表里
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/robot/save")
    @ApiOperation(value = "将用户和名称塞进robot机器人里面")
    public R<Boolean> saveUserNameAndLogo() throws Exception {

        Page<UserWechatInfo2VO> page = new Page<>(1, Integer.MAX_VALUE);
        Map<String, Object> params = new HashMap<>();
        IPage<UserWechatInfo2VO> listPage = service.getUserWechatInfo2VOListByParams(page, params);
        List<UserWechatInfo2VO> records = listPage.getRecords();

        for (UserWechatInfo2VO userWechatInfo2VO : records) {
            //拿取图片
            String headimgurl = userWechatInfo2VO.getHeadimgurl();
            //拿取名称
            String name = userWechatInfo2VO.getNickname();
            //解码
            String decodeName = URLDecoder.decode(name, "UTF-8");

            Runnable task = new Runnable() {
                @Override
                public void run() { // 覆盖重写抽象方法
                    if (!StringUtil.isBlank(headimgurl)) {
                        //图片是否有效,
                        URL url = null;
                        try {
                            url = new URL(headimgurl);
                            URLConnection conn = url.openConnection();
                            int contentLength = conn.getContentLength();
                            if (contentLength != 5093 && contentLength!=-1) {
                                //塞进tb_robot里面
                                Robot robot = new Robot();

                                robot.setName(decodeName);
                                robot.setPhoto(headimgurl);
                                Date date = new Date();
                                robot.setCreateTime(DateUtil.getCurrentTimestamp());
                                robotFeignClient.edit(robot);
                                System.out.println("图片地址->" + headimgurl+" ---大小-->"+contentLength);

                            }
                            System.out.println(decodeName + "文件大小-->" + contentLength);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }


                    }
                }
            };
            executor.submit(task);
            executor.execute(task);
            //不为空进去,这段代码里可以放进线程里面执行
//            System.out.println("统计了->"+atomicInteger.intValue());

        }
        return R.success(true);
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值