package com.ruoyi.web.controller.api;
|
|
|
import cn.afterturn.easypoi.excel.ExcelExportUtil;
|
import cn.afterturn.easypoi.excel.ExcelImportUtil;
|
import cn.afterturn.easypoi.excel.entity.ExportParams;
|
import cn.afterturn.easypoi.excel.entity.ImportParams;
|
import cn.hutool.json.JSONObject;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.ruoyi.common.annotation.Log;
|
import com.ruoyi.common.basic.PageInfo;
|
import com.ruoyi.common.core.domain.R;
|
import com.ruoyi.common.core.exception.ServiceException;
|
import com.ruoyi.common.enums.BusinessType;
|
import com.ruoyi.common.utils.WebUtils;
|
import com.ruoyi.system.export.LargeResourceImport;
|
import com.ruoyi.system.model.TLargeResource;
|
import com.ruoyi.system.model.TLargeResourceType;
|
import com.ruoyi.system.query.TLargeResourceQuery;
|
import com.ruoyi.system.service.TLargeResourceService;
|
import com.ruoyi.system.service.TLargeResourceTypeService;
|
import com.ruoyi.system.vo.TLargeResourceVO;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiOperation;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.poi.ss.usermodel.Workbook;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.transaction.annotation.Propagation;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.StringUtils;
|
import org.springframework.validation.annotation.Validated;
|
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.servlet.ServletOutputStream;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.net.URLEncoder;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Objects;
|
|
/**
|
* <p>
|
* 大屏-社区资源 前端控制器
|
* </p>
|
*
|
* @author xiaochen
|
* @since 2025-11-11
|
*/
|
@Slf4j
|
@Api(tags = "大屏-社区资源")
|
@RestController
|
@RequestMapping("/t-large-resource")
|
public class TLargeResourceController {
|
|
private final TLargeResourceService largeResourceService;
|
private final TLargeResourceTypeService largeResourceTypeService;
|
@Autowired
|
public TLargeResourceController(TLargeResourceService largeResourceService, TLargeResourceTypeService largeResourceTypeService) {
|
this.largeResourceService = largeResourceService;
|
this.largeResourceTypeService = largeResourceTypeService;
|
}
|
|
/**
|
* 获取社区资源管理列表
|
*/
|
@ApiOperation(value = "获取社区资源分页列表")
|
@PostMapping(value = "/pageList")
|
public R<PageInfo<TLargeResourceVO>> pageList(@RequestBody TLargeResourceQuery query) {
|
return R.ok(largeResourceService.pageList(query));
|
}
|
/**
|
* 获取社区资源管理列表
|
*/
|
@ApiOperation(value = "获取社区资源分页列表-大屏使用")
|
@PostMapping(value = "/pageListLarge")
|
public R<PageInfo<TLargeResourceVO>> pageListLarge(@RequestBody TLargeResourceQuery query) {
|
return R.ok(largeResourceService.pageList(query));
|
}
|
|
/**
|
* 获取社区资源管理列表
|
*/
|
@ApiOperation(value = "获取社区资源列表")
|
@PostMapping(value = "/list")
|
public R<List<TLargeResource>> list() {
|
return R.ok(largeResourceService.list(Wrappers.lambdaQuery(TLargeResource.class).orderByDesc(TLargeResource::getCreateTime)));
|
}
|
|
/**
|
* 添加社区资源管理
|
*/
|
@Log(title = "社区资源信息-新增社区资源", businessType = BusinessType.INSERT)
|
@ApiOperation(value = "添加社区资源")
|
@PostMapping(value = "/add")
|
public R<Boolean> add(@Validated @RequestBody TLargeResource dto) {
|
return R.ok(largeResourceService.save(dto));
|
}
|
|
/**
|
* 修改社区资源
|
*/
|
@Log(title = "社区资源信息-修改社区资源", businessType = BusinessType.UPDATE)
|
@ApiOperation(value = "修改社区资源")
|
@PostMapping(value = "/update")
|
public R<Boolean> update(@Validated @RequestBody TLargeResource dto) {
|
return R.ok(largeResourceService.updateById(dto));
|
}
|
|
/**
|
* 查看社区资源详情
|
*/
|
@ApiOperation(value = "查看社区资源详情")
|
@GetMapping(value = "/getDetailById")
|
public R<TLargeResource> getDetailById(@RequestParam String id) {
|
return R.ok(largeResourceService.getById(id));
|
}
|
|
/**
|
* 删除社区资源
|
*/
|
@Log(title = "社区资源信息-删除社区资源", businessType = BusinessType.DELETE)
|
@ApiOperation(value = "删除社区资源")
|
@DeleteMapping(value = "/deleteById")
|
public R<Boolean> deleteById(@RequestParam String id) {
|
return R.ok(largeResourceService.removeById(id));
|
}
|
|
/**
|
* 批量删除社区资源
|
*/
|
@Log(title = "社区资源信息-删除社区资源", businessType = BusinessType.DELETE)
|
@ApiOperation(value = "批量删除社区资源")
|
@DeleteMapping(value = "/deleteByIds")
|
public R<Boolean> deleteByIds(@RequestBody List<String> ids) {
|
return R.ok(largeResourceService.removeByIds(ids));
|
}
|
|
@ApiOperation(value = "社区资源信息导入模板下载")
|
@GetMapping("/import-template")
|
public void importTemplate() {
|
List<LargeResourceImport> largeLargeResourceImports = new ArrayList<>();
|
LargeResourceImport largeLargeResourceImport = new LargeResourceImport();
|
largeLargeResourceImports.add(largeLargeResourceImport);
|
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), LargeResourceImport.class, largeLargeResourceImports);
|
HttpServletResponse response = WebUtils.response();
|
ServletOutputStream outputStream = null;
|
try {
|
String fileName = URLEncoder.encode("社区资源信息导入模板.xls", "utf-8");
|
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
|
response.setHeader("content-Type", "application/vnd.ms-excel");
|
response.setHeader("Pragma", "no-cache");
|
response.setHeader("Cache-Control", "no-cache");
|
outputStream = response.getOutputStream();
|
workbook.write(outputStream);
|
} catch (IOException e) {
|
e.printStackTrace();
|
System.out.println("社区资源信息导入模板下载失败!");
|
} finally {
|
try {
|
outputStream.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
@Log(title = "社区资源信息-社区资源信息导入", businessType = BusinessType.INSERT)
|
@ApiOperation(value = "社区资源信息导入")
|
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
|
@PostMapping("/import")
|
public R<String> importInfo(@RequestPart("file") MultipartFile file) {
|
List<TLargeResource> largeResources = new ArrayList<>();
|
ImportParams params = new ImportParams();
|
params.setHeadRows(1); //表头行数
|
InputStream inputStream = null;
|
List<LargeResourceImport> largeResourceImports;
|
try {
|
inputStream = file.getInputStream();
|
largeResourceImports = ExcelImportUtil.importExcel(inputStream,
|
LargeResourceImport.class, params);
|
} catch (Exception e) {
|
e.printStackTrace();
|
log.error("社区资源信息导入失败:{}", e.getMessage());
|
throw new ServiceException("社区资源信息导入失败!");
|
} finally {
|
try {
|
inputStream.close();
|
} catch (IOException e) {
|
throw new ServiceException(e.getMessage());
|
}
|
}
|
if (CollectionUtils.isEmpty(largeResourceImports)) {
|
throw new ServiceException("社区资源信息数据为空!");
|
}
|
JSONObject result = new JSONObject();
|
int i = 1;
|
for (LargeResourceImport largeResourceImport : largeResourceImports) {
|
if (!StringUtils.hasLength(largeResourceImport.getTypeParentName())){
|
result.append("第"+i+"行:[", "一级类型名称"+"]不能为空");
|
i++;
|
continue;
|
}
|
if (!StringUtils.hasLength(largeResourceImport.getTypeChildrenName())){
|
result.append("第"+i+"行:[", "二级类型名称"+"]不能为空");
|
i++;
|
continue;
|
}
|
if (!StringUtils.hasLength(largeResourceImport.getResourceUnit())){
|
result.append("第"+i+"行:[", "资源单位"+"]不能为空");
|
i++;
|
continue;
|
}
|
if (!StringUtils.hasLength(largeResourceImport.getResourceNameDesc())){
|
result.append("第"+i+"行:[", "资源名称描述"+"]不能为空");
|
i++;
|
continue;
|
}
|
if (!StringUtils.hasLength(largeResourceImport.getResourceSource())){
|
result.append("第"+i+"行:[", "资源来源"+"]不能为空");
|
i++;
|
continue;
|
}
|
if (!StringUtils.hasLength(largeResourceImport.getQuantityScale())){
|
result.append("第"+i+"行:[", "数量规模"+"]不能为空");
|
i++;
|
continue;
|
}
|
if (!StringUtils.hasLength(largeResourceImport.getContactPerson())){
|
result.append("第"+i+"行:[", "联系人"+"]不能为空");
|
i++;
|
continue;
|
}
|
if (!StringUtils.hasLength(largeResourceImport.getContactNumber())){
|
result.append("第"+i+"行:[", "联系电话"+"]不能为空");
|
i++;
|
continue;
|
}
|
if (!largeResourceImport.getContactNumber().matches("^$|^1[3-9]\\d{9}$")){
|
result.append("第"+i+"行:[", "联系电话"+"]格式不正确");
|
i++;
|
continue;
|
}
|
TLargeResource largeResource = new TLargeResource();
|
BeanUtils.copyProperties(largeResourceImport, largeResource);
|
|
TLargeResourceType parent = largeResourceTypeService.getOne(Wrappers.lambdaQuery(TLargeResourceType.class)
|
.eq(TLargeResourceType::getTypeName, largeResourceImport.getTypeParentName())
|
.last("LIMIT 1"));
|
if(Objects.isNull(parent)){
|
result.append("第"+i+"行:[", "一级类型名称"+"]不存在");
|
i++;
|
continue;
|
}
|
largeResource.setTypeParentId(parent.getId());
|
TLargeResourceType children = largeResourceTypeService.getOne(Wrappers.lambdaQuery(TLargeResourceType.class)
|
.eq(TLargeResourceType::getTypeName, largeResourceImport.getTypeChildrenName())
|
.eq(TLargeResourceType::getParentId, parent.getId())
|
.last("LIMIT 1"));
|
if(Objects.isNull(children)){
|
result.append("第"+i+"行:[", "二级类型名称"+"]不存在");
|
i++;
|
continue;
|
}
|
largeResource.setTypeChildrenId(children.getId());
|
largeResources.add(largeResource);
|
i++;
|
}
|
largeResourceService.saveBatch(largeResources);
|
if(!result.isEmpty()){
|
return R.fail(result.toString());
|
}
|
return R.ok();
|
}
|
|
}
|