2024-06-11 10:54:34 +08:00
|
|
|
package com.whdc.controller;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
2024-06-11 17:42:39 +08:00
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
2024-06-11 10:54:34 +08:00
|
|
|
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
|
|
|
|
|
import com.whdc.exception.MyException;
|
|
|
|
|
import com.whdc.model.dto.AddressBookOldDto;
|
|
|
|
|
import com.whdc.model.entity.AddressBookOld;
|
|
|
|
|
import com.whdc.model.vo.ExcelDataVo;
|
2024-06-11 17:42:39 +08:00
|
|
|
import com.whdc.model.vo.ExcelOldDataVo;
|
2024-06-11 10:54:34 +08:00
|
|
|
import com.whdc.service.IAddressBookOldService;
|
|
|
|
|
import com.whdc.utils.ExcelCommon;
|
|
|
|
|
import com.whdc.utils.ResultJson;
|
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.time.LocalDateTime;
|
2024-06-12 14:52:22 +08:00
|
|
|
import java.util.Date;
|
2024-06-11 10:54:34 +08:00
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
import static com.whdc.model.MyConstant.REDIS_KEY;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Description:
|
|
|
|
|
* Created by XuSan on 2024/6/11.
|
|
|
|
|
*
|
|
|
|
|
* @author XuSan
|
|
|
|
|
* @version 1.0
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Api(tags = "web通讯录 - Controller")
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/addressbookOld")
|
|
|
|
|
@Transactional
|
|
|
|
|
public class AddressBookOldController {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private IAddressBookOldService service;
|
|
|
|
|
|
|
|
|
|
public static final String ADDRESS_BOOK_REDIS_KEY = REDIS_KEY + "addressbookOld";
|
|
|
|
|
public static final String ADDRESS_BOOK_TEMP_REDIS_KEY = ADDRESS_BOOK_REDIS_KEY + "temp:";
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private RedisTemplate<String,String> redisTemplate;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ApiOperation(value = "查询所有联系人")
|
|
|
|
|
@PostMapping(value = "find")
|
|
|
|
|
public ResultJson<List<AddressBookOld>> find(@RequestBody AddressBookOldDto dto) {
|
|
|
|
|
|
|
|
|
|
LambdaQueryChainWrapper<AddressBookOld> query = service.lambdaQuery();
|
|
|
|
|
|
|
|
|
|
String name = dto.getName();
|
|
|
|
|
if (StringUtils.isNotBlank(name)){
|
|
|
|
|
query.like(AddressBookOld::getName, name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String phone = dto.getPhone();
|
|
|
|
|
if (StringUtils.isNotBlank(phone)){
|
|
|
|
|
query.like(AddressBookOld::getPhone, phone);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<AddressBookOld> data = query.list();
|
|
|
|
|
return ResultJson.ok(data);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ApiOperation(value = "下载excel")
|
|
|
|
|
@GetMapping(value = "downloadExcel")
|
|
|
|
|
public void downloadExcel( HttpServletResponse response) {
|
|
|
|
|
|
|
|
|
|
List<AddressBookOld> list = service.lambdaQuery()
|
|
|
|
|
.list();
|
|
|
|
|
ExcelCommon.exportExcel(list,
|
2024-06-11 17:42:39 +08:00
|
|
|
null, "防汛抗旱通讯录", AddressBookOld.class, "湖北省防汛抗旱通信录模版" + LocalDateTime.now() + ".xlsx",
|
2024-06-11 10:54:34 +08:00
|
|
|
response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ApiOperation(value = "解析文件数据")
|
|
|
|
|
@PostMapping(value = "getExcelData")
|
2024-06-11 17:42:39 +08:00
|
|
|
public ResultJson<ExcelOldDataVo> getExcelData(MultipartFile file) {
|
2024-06-11 10:54:34 +08:00
|
|
|
|
|
|
|
|
List<AddressBookOld> appends = ExcelCommon.importExcel(file, 0, 1, AddressBookOld.class);
|
|
|
|
|
|
2024-06-11 17:42:39 +08:00
|
|
|
ExcelOldDataVo excelDataVo = new ExcelOldDataVo();
|
2024-06-11 10:54:34 +08:00
|
|
|
if (CollectionUtils.isNotEmpty(appends)){
|
2024-06-11 17:42:39 +08:00
|
|
|
appends = appends.stream().filter(vo ->
|
|
|
|
|
StringUtils.isNotBlank(vo.getName())
|
|
|
|
|
&& StringUtils.isNotBlank(vo.getCtnm())
|
|
|
|
|
&& StringUtils.isNotBlank(vo.getCnnm())
|
|
|
|
|
).collect(Collectors.toList());
|
2024-06-11 10:54:34 +08:00
|
|
|
String key = UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
|
|
redisTemplate.opsForValue().set(ADDRESS_BOOK_TEMP_REDIS_KEY + key, JSON.toJSONString(appends), 30, TimeUnit.MINUTES);
|
|
|
|
|
excelDataVo.setKey(key);
|
2024-06-11 17:42:39 +08:00
|
|
|
excelDataVo.setList(appends);
|
2024-06-11 10:54:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResultJson.ok(excelDataVo);
|
|
|
|
|
}
|
|
|
|
|
@ApiOperation(value = "联系人导入")
|
|
|
|
|
@PostMapping(value = "uploadExcel")
|
|
|
|
|
public ResultJson uploadExcel(@RequestBody ExcelDataVo data) {
|
|
|
|
|
|
|
|
|
|
String s = redisTemplate.opsForValue().get(ADDRESS_BOOK_TEMP_REDIS_KEY + data.getKey());
|
|
|
|
|
if (StringUtils.isBlank(s)){
|
|
|
|
|
throw new MyException("文件已过期");
|
|
|
|
|
}
|
|
|
|
|
List<AddressBookOld> appends = JSON.parseArray(s, AddressBookOld.class);
|
|
|
|
|
|
|
|
|
|
if (CollectionUtils.isEmpty(appends)) {
|
|
|
|
|
throw new MyException("文件数据为空");
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-11 17:42:39 +08:00
|
|
|
// Set<String> set = appends.stream().map(vo -> vo.getName() + "_" + vo.getPhone()).collect(Collectors.toSet());
|
|
|
|
|
// if (set.size() != appends.size()){
|
|
|
|
|
// throw new MyException("联系人重复");
|
|
|
|
|
// }
|
2024-06-11 10:54:34 +08:00
|
|
|
|
2024-06-11 17:42:39 +08:00
|
|
|
boolean r1 = service.remove(Wrappers.emptyWrapper());
|
2024-06-11 10:54:34 +08:00
|
|
|
if (!r1) {
|
|
|
|
|
throw new MyException("通讯录数据删除失败");
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 14:52:22 +08:00
|
|
|
// 新增创建时间
|
|
|
|
|
Date date = new Date();
|
|
|
|
|
appends = appends.stream().map(vo -> vo.setCreateTime(date))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
2024-06-11 10:54:34 +08:00
|
|
|
if (!service.saveBatch(appends)) {
|
|
|
|
|
throw new MyException("表格数据新增失败");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResultJson.ok("数据新增成功");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|