| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- package com.hr.controller;
- import com.alibaba.fastjson2.JSONObject;
- import com.hr.dto.*;
- import com.hr.fegin.dto.MyDemoDto;
- import com.hr.repository.jpa.TestJpa;
- import com.hr.repository.domain.TestEntity;
- import com.hr.repository.service.DemoService;
- import com.hr.repository.service.TransServer;
- import com.github.pagehelper.PageHelper;
- import com.hr.system.api.domain.SysDictData;
- import com.yy.basedevelop.common.data.BasePageResult;
- import com.yy.basedevelop.common.data.BaseReturnDto;
- import com.yy.basedevelop.common.http.config.RedisService;
- import com.yy.basedevelop.common.util.DictUtil;
- import com.yy.basedevelop.common.util.ExcelUtil;
- import com.yy.basedevelop.common.util.HttpClientUtils;
- import com.yy.basedevelop.common.util.OSSUtil;
- import jakarta.servlet.http.HttpServletResponse;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.codec.multipart.FilePart;
- import org.springframework.http.server.reactive.ServerHttpResponse;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.server.ServerWebExchange;
- import reactor.core.publisher.Mono;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.TimeUnit;
- import java.util.random.RandomGenerator;
- @Slf4j
- @RestController
- @RequestMapping("/test")
- public class DemoController {
- @Autowired
- private TestJpa testJpa;
- @Autowired
- private DemoService sv;
- @Autowired
- private TransServer transServer;
- @Autowired
- private DemoService demoService;
- @Autowired
- private RedisService redisService;
- @Autowired
- private OSSUtil oSSUtil;
- /**
- * 测试字典
- * @param dto
- * @return
- */
- @PostMapping("listdict")
- public Mono<TestDemoDto> listdict(@RequestBody TestDemoDto dto) {
- log.info("listdict=:{}", dto);
- TestDemoDto dto1 = new TestDemoDto();
- //将中文的 字典类型,字典标签转换为字典的value
- //比如 用户性别,男 转换为 1
- dto1.setDictValue(DictUtil.getDictCodeStr(dto.getDictTypeNameCn(),dto.getDictNameCn()));
- Integer val=DictUtil.getDictCodeInt(dto.getDictTypeNameCn(),dto.getDictNameCn());
- log.info("val=:{}", val);
- return Mono.just(dto1);
- }
- /**
- * 测试Oss
- * @param filePart
- * @param description
- * @param path
- * @return
- */
- @PostMapping("ossTest")
- public Mono<TestDemoDto> ossTest(@RequestPart("file") FilePart filePart,
- @RequestPart("description") String description,
- @RequestPart("path") String path) {
- log.info("接收的listoss=:{},path={}", description,path);
- String ossstr=oSSUtil.upLoadInputStream(filePart, "esgin" + path);
- TestDemoDto dto1 = new TestDemoDto();
- dto1.setName("返回名字oss");
- dto1.setAge(25);
- dto1.setOssUrl(ossstr);
- return Mono.just(dto1);
- }
- /**
- * 测试Mono
- * @param dto
- * @return
- */
- @PostMapping("MonoTest")
- public Mono<TestDemoDto> MonoTest(@RequestBody TestDemoDto dto) {
- log.info("接收的DTO777=:{}", dto);
- TestDemoDto dto1 = new TestDemoDto();
- dto1.setName("返回的名字1");
- dto1.setAge(18);
- return Mono.just(dto1);
- }
- /**
- * 测试redis
- * @param dto
- * @return
- */
- @PostMapping("redisTest")
- public Mono<TestDemoDto> list5668(@RequestBody TestDemoDto dto) {
- log.info("接收的DTO8=:{}", dto);
- Boolean baaa=redisService.hasKey("aaa");
- log.info("在redis中aaa的key是否存在={}", baaa);
- TestDemoDto dto1 = new TestDemoDto();
- dto1.setName("直接返回Dto");
- dto1.setAge(26);
- return Mono.just(dto1);
- }
- /***
- * 通过http请求访问,一定要使用封装的HttpClientUtils,因为日志中同步了TraceId过去的,在日志追踪非常有用
- * @param response
- * @return
- */
- @GetMapping("httpClientDemo")
- public Mono<TestDemoDto> listname6(ServerHttpResponse response) {
- JSONObject json = new JSONObject();
- json.put("name", "传输名字");
- json.put("age", "20");
- Mono<TestDemoDto> js=HttpClientUtils.post("http://localhost:9700/test/listname7",json,TestDemoDto.class);
- js.subscribe(js1->{
- log.info("当前返回的Dto:{}",js1);
- });
- TestDemoDto d=js.block();
- log.info("在Mono外部打印日志,返回值:{}",d);
- String returnstr=HttpClientUtils.post("http://localhost:9700/test/listname8",json);
- log.info("直接返回String:{}",returnstr);
- return js;
- }
- /**
- * 测试Excel导出
- * @param response
- * @return
- */
- @GetMapping("exportExcel")
- public Mono<Void> exportExcel(ServerHttpResponse response) {
- ExcelUtil<DemoExcelDto> util = new ExcelUtil<DemoExcelDto>(DemoExcelDto.class);
- List<DemoExcelDto> list = new ArrayList<>();
- DemoExcelDto d1 = new DemoExcelDto();
- d1.setName("名字1");
- d1.setTestCode("test1");
- d1.setMydate(new Date());
- d1.setSex("1");
- list.add(d1);
- DemoExcelDto d2 = new DemoExcelDto();
- d2.setName("名字2");
- d2.setTestCode("test2");
- d2.setMydate(new Date());
- d2.setSex("2");
- list.add(d2);
- DemoExcelDto d3 = new DemoExcelDto();
- d3.setName("名字3");
- d3.setTestCode("test3");
- d3.setMydate(new Date());
- d3.setSex("2");
- list.add(d3);
- DemoExcelDto d4 = new DemoExcelDto();
- d4.setName("名字4");
- d4.setTestCode("test4");//没有值则导出来是空白
- d4.setMydate(new Date());
- d4.setSex("2");
- list.add(d4);
- return util.exportExcel(response, list, "字典数据");
- }
- /**
- * 返回标准Dto数据
- * @param sysDictData
- * @return
- */
- @PostMapping("normReturn")
- public Mono<BaseReturnDto> normReturn(@RequestBody SysDictData sysDictData) {
- List<SysDictData> list=DictUtil.getSysDictDataByKey(sysDictData.getDictType());
- return Mono.just(BaseReturnDto.success(list));
- }
- /***
- * 查询例子
- * sql写在Jpa中
- * @param dto
- * @return
- */
- @PostMapping("listPage")
- public Mono<BasePageResult<MyDemoDto>> listPage(@RequestBody QueryDto dto) {
- BasePageResult<MyDemoDto> lst=demoService.qyeryList(dto);
- return Mono.just(lst.success());
- }
- /***
- * 查询例子,直接传Dto查询
- * sql写在mapper.xml中
- * @param dto
- * @return
- */
- @PostMapping("listpage2")
- public Mono<BasePageResult<MyDemoDto>> listpage2(@RequestBody QueryDto dto) {
- BasePageResult<MyDemoDto> lst=demoService.qyeryList3(dto);
- return Mono.just(lst.success());
- }
- /**
- * 分页查询
- * @param name
- * @return
- */
- @GetMapping("listname2")
- public Mono<BasePageResult<MyDemoDto>> select1(@RequestParam("name") String name) {
- PageHelper.startPage(2, 10);
- return Mono.just(demoService.selectList1(name));
- }
- /**
- * 多线程测试1,子线程中的TraceId和主线程日志的TraceId保持一直,这样便于追踪日志
- * @return
- */
- @GetMapping("threadTest1")
- public Mono<MyDto> threadTest1() {
- log.info("当前访问了threadTest1");
- transServer.testsubThread();
- return Mono.just(MyDto.success());
- }
- /**
- * 返回自定义Json
- * @return
- */
- @GetMapping("retunJson")
- public Mono<MyDto<JSONObject>> retunJson() {
- log.info("list1");
- return Mono.just(MyDto.success(JSONObject.parseObject("{\"a\":1}")));
- }
- /**
- * 多线程测试
- * @param dto
- * @return
- */
- @PostMapping("/mothod1")
- public Mono<MyDto> threadTest(@RequestBody MyParmDto dto) {
- log.info("当前访问了mothod1");
- transServer.threadTest1(dto.getName());
- return Mono.just(MyDto.success());
- }
- /**
- * 接收参数
- * @param name
- * @return
- */
- @GetMapping("username")
- public Mono<MyDto> username(@RequestParam(value = "name", required = false) String name) {
- if (name == null || name.trim().isEmpty()) {
- JSONObject error = new JSONObject();
- error.put("error", "Name parameter is required and cannot be empty");
- MyDto d= MyDto.success(error);
- return Mono.just(d);
- }
- JSONObject response = new JSONObject();
- response.put("name", name);
- MyDto d= MyDto.error(response);
- return Mono.just(d);
- }
- /**
- * 返回JSON对象
- * @return
- */
- @GetMapping("returnJson")
- public Mono<BaseReturnDto<JSONObject>> anniu1(){
- JSONObject response = new JSONObject();
- response.put("name", "按钮1,编辑权限");
- return Mono.just(BaseReturnDto.success(response));
- }
- /**
- * 分布式事务测试,注意要在service方法上面加@GlobalTransactional
- * 如果只是本地事务那么只加@Transactional
- * @return
- */
- @GetMapping("remotCall")
- public Mono<BaseReturnDto<JSONObject>> remotCall(){
- JSONObject response = new JSONObject();
- transServer.remotCall();
- return Mono.just(BaseReturnDto.success(response));
- }
- }
|