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.conditions.query.QueryWrapper;
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.BasePage;
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.framework.web.service.TokenService;
import com.ruoyi.system.export.MerchantImport;
import com.ruoyi.system.export.OldsterImport;
import com.ruoyi.system.model.TMerchant;
import com.ruoyi.system.model.TMerchantType;
import com.ruoyi.system.model.TOldster;
import com.ruoyi.system.query.TMerchantQuery;
import com.ruoyi.system.service.TMerchantService;
import com.ruoyi.system.service.TMerchantTypeService;
import com.ruoyi.system.vo.TMerchantVO;
import com.ruoyi.web.controller.tool.ExcelUtil;
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;
import java.util.stream.Collectors;
/**
*
* 商户信息 前端控制器
*
*
* @author xiaochen
* @since 2025-11-06
*/
@Slf4j
@Api(tags = "商户信息管理")
@RestController
@RequestMapping("/t-merchant")
public class TMerchantController {
private final TMerchantTypeService merchantTypeService;
private final TMerchantService merchantService;
private final TokenService tokenService;
@Autowired
public TMerchantController(TMerchantTypeService merchantTypeService, TMerchantService merchantService, TokenService tokenService) {
this.merchantTypeService = merchantTypeService;
this.merchantService = merchantService;
this.tokenService = tokenService;
}
/**
* 获取商户信息管理管理列表
*/
@ApiOperation(value = "获取商户信息管理分页列表")
@PostMapping(value = "/pageList")
public R> pageList(@RequestBody TMerchantQuery query) {
return R.ok(merchantService.pageList(query));
}
/**
* 获取商户信息管理管理列表
*/
@ApiOperation(value = "获取商户信息管理列表")
@PostMapping(value = "/list")
public R> list() {
return R.ok(merchantService.list(Wrappers.lambdaQuery(TMerchant.class).orderByDesc(TMerchant::getCreateTime)));
}
/**
* 添加商户信息管理管理
*/
@Log(title = "商户信息管理信息-新增商户信息管理", businessType = BusinessType.INSERT)
@ApiOperation(value = "添加商户信息管理")
@PostMapping(value = "/add")
public R add(@Validated @RequestBody TMerchant dto) {
if (merchantService.isExit(dto)) {
return R.fail("商户名称已存在");
}
return R.ok(merchantService.save(dto));
}
/**
* 修改商户信息管理
*/
@Log(title = "商户信息管理信息-修改商户信息管理", businessType = BusinessType.UPDATE)
@ApiOperation(value = "修改商户信息管理")
@PostMapping(value = "/update")
public R update(@Validated @RequestBody TMerchant dto) {
if (merchantService.isExit(dto)) {
return R.fail("商户名称已存在");
}
return R.ok(merchantService.updateById(dto));
}
/**
* 查看商户信息管理详情
*/
@ApiOperation(value = "查看商户信息管理详情")
@GetMapping(value = "/getDetailById")
public R getDetailById(@RequestParam String id) {
return R.ok(merchantService.getById(id));
}
/**
* 删除商户信息管理
*/
@Log(title = "商户信息管理信息-删除商户信息管理", businessType = BusinessType.DELETE)
@ApiOperation(value = "删除商户信息管理")
@DeleteMapping(value = "/deleteById")
public R deleteById(@RequestParam String id) {
return R.ok(merchantService.removeById(id));
}
/**
* 批量删除商户信息管理
*/
@Log(title = "商户信息管理信息-删除商户信息管理", businessType = BusinessType.DELETE)
@ApiOperation(value = "批量删除商户信息管理")
@DeleteMapping(value = "/deleteByIds")
public R deleteByIds(@RequestBody List ids) {
return R.ok(merchantService.removeByIds(ids));
}
@ApiOperation(value = "商户导入模板下载")
@GetMapping("/import-template")
public void importTemplate() {
List merchantImports = new ArrayList<>();
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), MerchantImport.class, merchantImports);
// 查询分类列表
List merchantTypes = merchantTypeService.list(Wrappers.lambdaQuery(TMerchantType.class).orderByDesc(TMerchantType::getCreateTime));
List dataList = new ArrayList<>();
if (!CollectionUtils.isEmpty(merchantTypes)) {
dataList = merchantTypes.stream().map(TMerchantType::getTypeName).collect(Collectors.toList());
}
ExcelUtil.selectList(workbook, 1, 65535, 2, 2, dataList.toArray(new String[dataList.size()]), 1);
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 merchants = new ArrayList<>();
ImportParams params = new ImportParams();
params.setHeadRows(1); //表头行数
InputStream inputStream = null;
List merchantImports;
try {
inputStream = file.getInputStream();
merchantImports = ExcelImportUtil.importExcel(inputStream,
MerchantImport.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(merchantImports)) {
throw new ServiceException("商户信息数据为空!");
}
JSONObject result = new JSONObject();
int i = 1;
for (MerchantImport merchantImport : merchantImports) {
if (!StringUtils.hasLength(merchantImport.getMerchantName())){
result.append("第"+i+"行:[", "商户名称"+"]不能为空");
i++;
continue;
}
if (!StringUtils.hasLength(merchantImport.getMerchantPhone())){
result.append("第"+i+"行:[", "商户电话"+"]不能为空");
i++;
continue;
}
if (!StringUtils.hasLength(merchantImport.getTypeName())){
result.append("第"+i+"行:[", "商户分类"+"]不能为空");
i++;
continue;
}
if (!StringUtils.hasLength(merchantImport.getAddress())){
result.append("第"+i+"行:[", "商户地址"+"]不能为空");
i++;
continue;
}
TMerchantType merchantType = merchantTypeService.getOne(Wrappers.lambdaQuery(TMerchantType.class)
.eq(TMerchantType::getTypeName, merchantImport.getTypeName())
.last("LIMIT 1"));
if(Objects.isNull(merchantType)){
result.append("第"+i+"行:[", "商户分类"+"]不存在");
i++;
continue;
}
TMerchant merchant = new TMerchant();
BeanUtils.copyProperties(merchantImport, merchant);
merchants.add(merchant);
i++;
}
merchantService.saveBatch(merchants);
if(!result.isEmpty()){
return R.fail(result.toString());
}
return R.ok();
}
}