| | |
| | | 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; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | * @author xiaochen |
| | | * @since 2025-11-06 |
| | | */ |
| | | @Slf4j |
| | | @Api(tags = "商户信息管理") |
| | | @RestController |
| | | @RequestMapping("/t-merchant") |
| | |
| | | */ |
| | | @ApiOperation(value = "获取商户信息管理分页列表") |
| | | @PostMapping(value = "/pageList") |
| | | public R<PageInfo<TMerchant>> pageList(@RequestBody TMerchantQuery query) { |
| | | public R<PageInfo<TMerchantVO>> pageList(@RequestBody TMerchantQuery query) { |
| | | return R.ok(merchantService.pageList(query)); |
| | | } |
| | | |
| | |
| | | public R<Boolean> deleteByIds(@RequestBody List<String> ids) { |
| | | return R.ok(merchantService.removeByIds(ids)); |
| | | } |
| | | |
| | | |
| | | @ApiOperation(value = "商户导入模板下载") |
| | | @GetMapping("/import-template") |
| | | public void importTemplate() { |
| | | List<MerchantImport> merchantImports = new ArrayList<>(); |
| | | Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), MerchantImport.class, merchantImports); |
| | | // 查询分类列表 |
| | | List<TMerchantType> merchantTypes = merchantTypeService.list(Wrappers.lambdaQuery(TMerchantType.class).orderByDesc(TMerchantType::getCreateTime)); |
| | | List<String> 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<String> importInfo(@RequestPart("file") MultipartFile file) { |
| | | List<TMerchant> merchants = new ArrayList<>(); |
| | | ImportParams params = new ImportParams(); |
| | | params.setHeadRows(1); //表头行数 |
| | | InputStream inputStream = null; |
| | | List<MerchantImport> 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(); |
| | | } |
| | | |
| | | } |
| | | |