liujie
2025-12-25 484fc0b88bbcec139a35ef3850cb1f614320cf75
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
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.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
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.BigGiftPackageImport;
import com.ruoyi.system.export.TPhysicalGoodsImport;
import com.ruoyi.system.model.TBigGiftPackage;
import com.ruoyi.system.model.THtmlConfig;
import com.ruoyi.system.model.TPhysicalGoods;
import com.ruoyi.system.query.BigGiftPackageQuery;
import com.ruoyi.system.service.THtmlConfigService;
import com.ruoyi.system.service.TPhysicalGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
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.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.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
 
import static net.sf.jsqlparser.parser.feature.Feature.update;
 
/**
 * <p>
 * 实物商品 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2025-12-08
 */
@RestController
@RequestMapping("/t-physical-goods")
@Api(tags = "(嘉视达)实物商品")
public class TPhysicalGoodsController {
 
    private final TPhysicalGoodsService tPhysicalGoodsService;
    private final TokenService tokenService;
    private final THtmlConfigService tHtmlConfigService;
 
    public TPhysicalGoodsController(TPhysicalGoodsService tPhysicalGoodsService, TokenService tokenService, THtmlConfigService tHtmlConfigService) {
        this.tPhysicalGoodsService = tPhysicalGoodsService;
        this.tokenService = tokenService;
        this.tHtmlConfigService = tHtmlConfigService;
 
    }
 
    @ApiOperation(value = "实物商品分页列表")
    @PostMapping(value = "/pageList")
    public R<Page<TPhysicalGoods>> pageList(@RequestBody BigGiftPackageQuery query) {
        Page<TPhysicalGoods> tPhysicalGoodsPage = new Page<>(query.getPageNum(), query.getPageSize());
        LambdaQueryWrapper<TPhysicalGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TPhysicalGoods::getStatus,0);
        wrapper.like(Objects.nonNull(query.getName()), TPhysicalGoods::getName, query.getName());
        wrapper.orderByDesc(TPhysicalGoods::getCreateTime);
        Page<TPhysicalGoods> pageInfo = tPhysicalGoodsService.page(tPhysicalGoodsPage, wrapper);
        return R.ok(pageInfo);
    }
 
    @ApiOperation(value = "实物商品详情")
    @GetMapping(value = "/detail/{id}")
    public R<TPhysicalGoods> detail(@PathVariable Integer id ) {
        TPhysicalGoods tPhysicalGoods = tPhysicalGoodsService.getById(id);
        return R.ok(tPhysicalGoods);
    }
 
    @ApiOperation(value = "实物商品添加")
    @PostMapping(value = "/add")
    @Log(title = "实物商品-添加", businessType = BusinessType.INSERT)
    public R<String> add(@RequestBody TPhysicalGoods tPhysicalGoods) {
        LoginUser loginUser = tokenService.getLoginUser();
        tPhysicalGoods.setCreateUserId(loginUser.getUserId());
        boolean save = tPhysicalGoodsService.save(tPhysicalGoods);
        return save ? R.ok() : R.fail();
    }
 
    @ApiOperation(value = "实物商品修改")
    @PostMapping(value = "/update")
    @Log(title = "实物商品-修改", businessType = BusinessType.UPDATE)
    public R<String> update(@RequestBody TPhysicalGoods tPhysicalGoods) {
        LoginUser loginUser = tokenService.getLoginUser();
        tPhysicalGoods.setUpdateUserId(loginUser.getUserId());
        boolean update = tPhysicalGoodsService.updateById(tPhysicalGoods);
        return update ? R.ok() : R.fail();
    }
 
    @ApiOperation(value = "实物商品删除")
    @DeleteMapping(value = "/delete/{id}")
    @Log(title = "实物商品-删除", businessType = BusinessType.DELETE)
    public R<String> delete(@PathVariable Integer id ) {
        boolean remove = tPhysicalGoodsService.update(new LambdaUpdateWrapper<TPhysicalGoods>().eq(TPhysicalGoods::getId, id).set(TPhysicalGoods::getStatus, 1).set(TPhysicalGoods::getUpdateUserId,tokenService.getLoginUser().getUserId()));
        return remove ? R.ok() : R.fail();
    }
 
    @ApiOperation(value = "实物商品是否展示(0不展示 1展示)")
    @PostMapping(value = "/isShow")
    @Log(title = "实物商品-展示", businessType = BusinessType.UPDATE)
    public R<?> isShow(@RequestParam String isShow) {
        THtmlConfig config = tHtmlConfigService.getById(8);
        config.setContent(isShow);
        boolean update = tHtmlConfigService.updateById(config);
        return update ? R.ok() : R.fail();
    }
 
 
 
    @ApiOperation(value = "实物商品导入模板下载")
    @GetMapping("/import-template")
    public void importTemplate() {
        List<TPhysicalGoodsImport> physicalGoodsImports = new ArrayList<>();
        TPhysicalGoodsImport physicalGoodsImport = new TPhysicalGoodsImport();
        physicalGoodsImports.add(physicalGoodsImport);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TPhysicalGoodsImport.class, physicalGoodsImports);
        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.IMPORT)
    @ApiOperation(value = "实物商品-导入")
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    @PostMapping("/import")
    public R<String> importInfo(@RequestPart("file") MultipartFile file) {
        List<TPhysicalGoodsImport> physicalGoodsImports = new ArrayList<>();
        ImportParams params = new ImportParams();
        params.setHeadRows(1); //表头行数
        InputStream inputStream = null;
        List<TPhysicalGoods> physicalGoods = new ArrayList<>();
        try {
            inputStream = file.getInputStream();
            physicalGoodsImports = ExcelImportUtil.importExcel(inputStream,
                    TPhysicalGoodsImport.class, params);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("实物商品导入失败!");
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                throw new ServiceException(e.getMessage());
            }
        }
        if (CollectionUtils.isEmpty(physicalGoodsImports)) {
            throw new ServiceException("实物商品数据为空!");
        }
        JSONObject result = new JSONObject();
        int i = 1;
        for (TPhysicalGoodsImport physicalGoodsImport : physicalGoodsImports) {
            if (!StringUtils.hasLength(physicalGoodsImport.getName())){
                result.append("第"+i+"行:[", "商品名称"+"]不能为空");
                continue;
            }
            if (!StringUtils.hasLength(physicalGoodsImport.getTotalPrice())){
                result.append("第"+i+"行:[", "商品价格"+"]不能为空");
                continue;
            }
            if (!StringUtils.hasLength(physicalGoodsImport.getIntroduction())){
                result.append("第"+i+"行:[", "商品简介"+"]不能为空");
                continue;
            }
            if (!StringUtils.hasLength(physicalGoodsImport.getSales())){
                result.append("第"+i+"行:[", "销量"+"]不能为空");
                continue;
            }
            TPhysicalGoods goods = new TPhysicalGoods();
            goods.setName(physicalGoodsImport.getName());
            goods.setIntroduction(physicalGoodsImport.getIntroduction());
            try {
                BigDecimal totalPrice = new BigDecimal(physicalGoodsImport.getTotalPrice());
                goods.setTotalPrice(totalPrice);
            } catch (Exception e) {
                result.append("第"+i+"行:[", "商品价格"+"]格式错误");
                continue;
            }
 
 
            try {
                Integer sales = Integer.parseInt(physicalGoodsImport.getSales());
                goods.setSales(sales);
            } catch (Exception e) {
                result.append("第"+i+"行:[", "销量"+"]格式错误");
                continue;
            }
 
 
            physicalGoods.add(goods);
            i++;
        }
        tPhysicalGoodsService.saveBatch(physicalGoods);
        if(!result.isEmpty()){
            return R.fail(result.toString());
        }
        return R.ok();
    }
 
 
    @ApiOperation(value = "实物导出")
    @PostMapping(value = "/export")
    @Log(title = "实物-导出", businessType = BusinessType.EXPORT)
    public void exportRegistration(@RequestBody BigGiftPackageQuery query){
 
        LambdaQueryWrapper<TPhysicalGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TPhysicalGoods::getStatus,0);
        wrapper.like(Objects.nonNull(query.getName()), TPhysicalGoods::getName, query.getName());
        wrapper.orderByDesc(TPhysicalGoods::getCreateTime);
        List<TPhysicalGoods> list = tPhysicalGoodsService.list(wrapper);
        ArrayList<TPhysicalGoodsImport> physicalGoodsImports = new ArrayList<>();
        for (TPhysicalGoods goods : list) {
            TPhysicalGoodsImport physicalGoodsImport = new TPhysicalGoodsImport();
            physicalGoodsImport.setName(goods.getName());
            physicalGoodsImport.setTotalPrice(goods.getTotalPrice().toString());
            physicalGoodsImport.setIntroduction(goods.getIntroduction());
            physicalGoodsImport.setSales(goods.getSales().toString());
            physicalGoodsImports.add(physicalGoodsImport);
        }
 
 
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TPhysicalGoodsImport.class, physicalGoodsImports);
        HttpServletResponse response = WebUtils.response();
        assert response != null;
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        ServletOutputStream outputStream = null;
        try {
            String fileName = URLEncoder.encode("实物导出信息.xls", "utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
}