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.DateUtils;
import com.ruoyi.common.utils.WebUtils;
import com.ruoyi.system.export.LargeSocialWorkerImport;
import com.ruoyi.system.export.LargeVolunteerImport;
import com.ruoyi.system.model.TLargeSocialWorker;
import com.ruoyi.system.model.TLargeVolunteer;
import com.ruoyi.system.query.TLargeVolunteerQuery;
import com.ruoyi.system.service.TLargeVolunteerService;
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;
/**
*
* 大屏-社区志愿者 前端控制器
*
*
* @author xiaochen
* @since 2025-11-11
*/
@Slf4j
@Api(tags = "大屏-社区志愿者")
@RestController
@RequestMapping("/t-large-volunteer")
public class TLargeVolunteerController {
private final TLargeVolunteerService largeVolunteerService;
@Autowired
public TLargeVolunteerController(TLargeVolunteerService largeVolunteerService) {
this.largeVolunteerService = largeVolunteerService;
}
/**
* 获取社区志愿者管理列表
*/
@ApiOperation(value = "获取社区志愿者分页列表")
@PostMapping(value = "/pageList")
public R> pageList(@RequestBody TLargeVolunteerQuery query) {
return R.ok(largeVolunteerService.pageList(query));
}
/**
* 获取社区志愿者管理列表
*/
@ApiOperation(value = "获取社区志愿者分页列表-大屏使用")
@PostMapping(value = "/pageListLarge")
public R> pageListLarge(@RequestBody TLargeVolunteerQuery query) {
return R.ok(largeVolunteerService.pageList(query));
}
/**
* 获取社区志愿者管理列表
*/
@ApiOperation(value = "获取社区志愿者列表")
@PostMapping(value = "/list")
public R> list() {
return R.ok(largeVolunteerService.list(Wrappers.lambdaQuery(TLargeVolunteer.class).orderByDesc(TLargeVolunteer::getCreateTime)));
}
/**
* 添加社区志愿者管理
*/
@Log(title = "社区志愿者信息-新增社区志愿者", businessType = BusinessType.INSERT)
@ApiOperation(value = "添加社区志愿者")
@PostMapping(value = "/add")
public R add(@Validated @RequestBody TLargeVolunteer dto) {
return R.ok(largeVolunteerService.save(dto));
}
/**
* 修改社区志愿者
*/
@Log(title = "社区志愿者信息-修改社区志愿者", businessType = BusinessType.UPDATE)
@ApiOperation(value = "修改社区志愿者")
@PostMapping(value = "/update")
public R update(@Validated @RequestBody TLargeVolunteer dto) {
return R.ok(largeVolunteerService.updateById(dto));
}
/**
* 查看社区志愿者详情
*/
@ApiOperation(value = "查看社区志愿者详情")
@GetMapping(value = "/getDetailById")
public R getDetailById(@RequestParam String id) {
return R.ok(largeVolunteerService.getById(id));
}
/**
* 删除社区志愿者
*/
@Log(title = "社区志愿者信息-删除社区志愿者", businessType = BusinessType.DELETE)
@ApiOperation(value = "删除社区志愿者")
@DeleteMapping(value = "/deleteById")
public R deleteById(@RequestParam String id) {
return R.ok(largeVolunteerService.removeById(id));
}
/**
* 批量删除社区志愿者
*/
@Log(title = "社区志愿者信息-删除社区志愿者", businessType = BusinessType.DELETE)
@ApiOperation(value = "批量删除社区志愿者")
@DeleteMapping(value = "/deleteByIds")
public R deleteByIds(@RequestBody List ids) {
return R.ok(largeVolunteerService.removeByIds(ids));
}
@ApiOperation(value = "社区志愿者信息导入模板下载")
@GetMapping("/import-template")
public void importTemplate() {
List largeLargeVolunteerImports = new ArrayList<>();
LargeVolunteerImport largeLargeVolunteerImport = new LargeVolunteerImport();
largeLargeVolunteerImports.add(largeLargeVolunteerImport);
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), LargeVolunteerImport.class, largeLargeVolunteerImports);
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 importInfo(@RequestPart("file") MultipartFile file) {
List largeVolunteers = new ArrayList<>();
ImportParams params = new ImportParams();
params.setHeadRows(1); //表头行数
InputStream inputStream = null;
List largeVolunteerImports;
try {
inputStream = file.getInputStream();
largeVolunteerImports = ExcelImportUtil.importExcel(inputStream,
LargeVolunteerImport.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(largeVolunteerImports)) {
throw new ServiceException("社区志愿者信息数据为空!");
}
JSONObject result = new JSONObject();
int i = 1;
for (LargeVolunteerImport largeVolunteerImport : largeVolunteerImports) {
if (!StringUtils.hasLength(largeVolunteerImport.getUserName())){
result.append("第"+i+"行:[", "姓名"+"]不能为空");
i++;
continue;
}
if (!StringUtils.hasLength(largeVolunteerImport.getUserSexStr())){
result.append("第"+i+"行:[", "性别"+"]不能为空");
i++;
continue;
}
if (!StringUtils.hasLength(largeVolunteerImport.getBirthDateStr())){
result.append("第"+i+"行:[", "出生日期"+"]不能为空");
i++;
continue;
}
if (!StringUtils.hasLength(largeVolunteerImport.getServiceArea())){
result.append("第"+i+"行:[", "服务区域"+"]不能为空");
i++;
continue;
}
if (!StringUtils.hasLength(largeVolunteerImport.getRegisterDateStr())){
result.append("第"+i+"行:[", "注册日期"+"]不能为空");
i++;
continue;
}
if (Objects.isNull(largeVolunteerImport.getTotalServiceDuration())){
result.append("第"+i+"行:[", "总服务时长"+"]不能为空");
i++;
continue;
}
if (!StringUtils.hasLength(largeVolunteerImport.getJoinTeamDateStr())){
result.append("第"+i+"行:[", "加入队伍时间"+"]不能为空");
i++;
continue;
}
TLargeVolunteer largeVolunteer = new TLargeVolunteer();
BeanUtils.copyProperties(largeVolunteerImport, largeVolunteer);
switch (largeVolunteerImport.getUserSexStr()){
case "男":
largeVolunteer.setUserSex(1);
break;
case "女":
largeVolunteer.setUserSex(2);
break;
default:
result.append("第"+i+"行:[", "性别"+"]格式错误");
i++;
continue;
}
try{
largeVolunteer.setBirthDate(DateUtils.stringToLocalDate(largeVolunteerImport.getBirthDateStr()));
}catch (Exception e){
result.append("第"+i+"行:[", "出生日期"+"]格式错误");
i++;
continue;
}
try{
largeVolunteer.setRegisterDate(DateUtils.stringToLocalDateTime(largeVolunteerImport.getRegisterDateStr()));
}catch (Exception e){
result.append("第"+i+"行:[", "注册日期"+"]格式错误");
i++;
continue;
}
try{
largeVolunteer.setJoinTeamDate(DateUtils.stringToLocalDateTime(largeVolunteerImport.getJoinTeamDateStr()));
}catch (Exception e){
result.append("第"+i+"行:[", "加入队伍时间"+"]格式错误");
i++;
continue;
}
largeVolunteers.add(largeVolunteer);
i++;
}
largeVolunteerService.saveBatch(largeVolunteers);
if(!result.isEmpty()){
return R.fail(result.toString());
}
return R.ok();
}
}