xuhy
2025-12-11 79d5402492a82421b94fcbf90be01adbf3c8e0e5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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 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-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<PageInfo<TMerchantVO>> pageList(@RequestBody TMerchantQuery query) {
        return R.ok(merchantService.pageList(query));
    }
 
    /**
     * 获取商户信息管理管理列表
     */
    @ApiOperation(value = "获取商户信息管理列表")
    @PostMapping(value = "/list")
    public R<List<TMerchant>> 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<Boolean> 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<Boolean> 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<TMerchant> getDetailById(@RequestParam String id) {
        return R.ok(merchantService.getById(id));
    }
 
    /**
     * 删除商户信息管理
     */
    @Log(title = "商户信息管理信息-删除商户信息管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除商户信息管理")
    @DeleteMapping(value = "/deleteById")
    public R<Boolean> deleteById(@RequestParam String id) {
        return R.ok(merchantService.removeById(id));
    }
 
    /**
     * 批量删除商户信息管理
     */
    @Log(title = "商户信息管理信息-删除商户信息管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "批量删除商户信息管理")
    @DeleteMapping(value = "/deleteByIds")
    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<>();
        MerchantImport merchantImport = new MerchantImport();
        merchantImports.add(merchantImport);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), MerchantImport.class, merchantImports);
        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();
    }
 
}